contract.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package contract
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_mobile_admin/models/custom/contract"
  6. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  7. "hongze/hongze_mobile_admin/utils"
  8. "time"
  9. )
  10. // 合同
  11. type Contract struct {
  12. ContractId int `orm:"column(contract_id);pk"`
  13. ContractCode string `description:"合同编号,长度32位"`
  14. SellerId int `description:"所属销售id"`
  15. SellerName string `description:"所属销售名称"`
  16. ProductId int `description:"产品id,1:ficc;2:权益"`
  17. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  18. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  19. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已签回','已解约'"`
  20. StartDate time.Time `description:"合同开始日期"`
  21. EndDate time.Time `description:"合同结束日期"`
  22. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  23. Price float64 `description:"实际金额,优惠后的金额"`
  24. PayRemark string `description:"付款方式说明,长度255位"`
  25. PayChannel string `description:"付款渠道,长度255位"`
  26. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  27. CreditCode string `description:"社会统一信用代码,长度64位"`
  28. ProvinceId int `description:"省级id"`
  29. Province string `description:"省级名称,长度16位"`
  30. CityId int `description:"市级id"`
  31. City string `description:"市级名称,长度32位"`
  32. Address string `description:"详细地址"`
  33. Fax string `description:"传真,长度32位"`
  34. Phone string `description:"电话,长度32位"`
  35. Postcode string `description:"邮编,长度16位"`
  36. Remark string `description:"补充内容,长度255位"`
  37. ModifyContent string `description:"修改内容"`
  38. ApprovalRemark string `description:"审核备注"`
  39. FileUrl string `description:"合同文件地址"`
  40. CheckBackFileUrl string `description:"签回合同文件地址"`
  41. RescindFileUrl string `description:"解约合同文件地址"`
  42. TemplateId int `description:"模板id"`
  43. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  44. IsDelete int `description:"是否已经删除,0:未删除,1:已删除",json:"-"`
  45. ApproveTime time.Time `description:"审批时间"`
  46. InvalidTime time.Time `description:"作废时间"`
  47. CheckBackFileTime time.Time `description:"合同签回时间"`
  48. RescindTime time.Time `description:"解约时间"`
  49. ModifyTime time.Time `description:"合同最近一次修改时间"`
  50. CreateTime time.Time `description:"合同添加时间"`
  51. }
  52. // 更新合同基础信息
  53. func (contract *Contract) Update(cols []string) (err error) {
  54. o := orm.NewOrm()
  55. _, err = o.Update(contract, cols...)
  56. return
  57. }
  58. // 根据合同id获取合同信息
  59. func GetContractById(contractId int) (contractInfo *Contract, err error) {
  60. o := orm.NewOrm()
  61. sql := `select * from contract where contract_id = ? `
  62. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  63. return
  64. }
  65. // 合同详情信息(包含服务信息等)
  66. type ContractDetail struct {
  67. ContractId int `description:"合同唯一id"`
  68. ContractCode string `description:"合同编号,长度32位"`
  69. SellerId int `description:"所属销售id"`
  70. SellerName string `description:"所属销售名称"`
  71. ProductId int `description:"产品id,1:ficc;2:权益"`
  72. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  73. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  74. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已解约'"`
  75. StartDate time.Time `description:"合同开始日期"`
  76. EndDate time.Time `description:"合同结束日期"`
  77. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  78. Price float64 `description:"实际金额,优惠后的金额"`
  79. PayRemark string `description:"付款方式说明,长度255位"`
  80. PayChannel string `description:"付款渠道,长度255位"`
  81. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  82. CreditCode string `description:"社会统一信用代码,长度64位"`
  83. ProvinceId int `description:"省级id"`
  84. Province string `description:"省级名称,长度16位"`
  85. CityId int `description:"市级id"`
  86. City string `description:"市级名称,长度32位"`
  87. Address string `description:"详细地址"`
  88. Fax string `description:"传真,长度32位"`
  89. Phone string `description:"电话,长度32位"`
  90. Postcode string `description:"邮编,长度16位"`
  91. Remark string `description:"补充内容,长度255位"`
  92. SellerRemark string `description:"销售备注,长度255位"`
  93. ModifyContent string `description:"修改内容"`
  94. ApprovalRemark string `description:"审核备注"`
  95. FileUrl string `description:"合同文件地址"`
  96. CheckBackFileUrl string `description:"签回合同文件地址"`
  97. RescindFileUrl string `description:"解约合同文件地址"`
  98. TemplateId int `description:"模板id"`
  99. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  100. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  101. ModifyTime time.Time `description:"合同最近一次修改时间"`
  102. CreateTime time.Time `description:"合同添加时间"`
  103. ApproveTime time.Time `description:"审批时间"`
  104. InvalidTime time.Time `description:"作废时间"`
  105. CheckBackFileTime time.Time `description:"合同签回时间"`
  106. RescindTime time.Time `description:"解约时间"`
  107. StartDateStr string `description:"合同起始时间"`
  108. EndDateStr string `description:"合同结束时间"`
  109. ModifyTimeStr string `description:"最近一次更新时间"`
  110. CreateTimeStr string `description:"合同添加时间"`
  111. ApproveTimeStr string `description:"审批时间"`
  112. InvalidTimeStr string `description:"作废时间"`
  113. CheckBackFileTimeStr string `description:"合同签回时间"`
  114. RescindTimeStr string `description:"解约时间"`
  115. Service []*contract.ContractServiceAndDetail
  116. PermissionLookList []*company_report_permission.PermissionLookList `description:"合同里面的权限列表"`
  117. RelationContractDetailList []*ContractDetail `description:"关联合同详情"`
  118. }
  119. // GetContractDetailById 根据合同id获取合同详情信息
  120. func GetContractDetailById(contractId int) (contractInfo *ContractDetail, err error) {
  121. o := orm.NewOrm()
  122. sql := `select * from contract where contract_id = ? `
  123. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  124. return
  125. }
  126. // 合同添加
  127. func AddContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (newContract *Contract, err error) {
  128. o := orm.NewOrm()
  129. tx, err := o.Begin()
  130. if err != nil {
  131. return
  132. }
  133. defer func() {
  134. if err != nil {
  135. _ = tx.Rollback()
  136. } else {
  137. _ = tx.Commit()
  138. }
  139. }()
  140. //合同数据入库
  141. contractId, err := tx.Insert(contractInfo)
  142. if err != nil {
  143. return
  144. }
  145. contractInfo.ContractId = int(contractId)
  146. for i := 0; i < len(contractServiceAndDetailList); i++ {
  147. //合同服务数据入库
  148. tmpContractService := contractServiceAndDetailList[i]
  149. contractService := &contract.ContractService{
  150. ContractId: int(contractId),
  151. ProductId: contractInfo.ProductId,
  152. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  153. Title: tmpContractService.Title,
  154. Value: tmpContractService.Value,
  155. HasDetail: tmpContractService.HasDetail,
  156. CreateTime: time.Now(),
  157. }
  158. contractServiceId, serviceErr := tx.Insert(contractService)
  159. if serviceErr != nil {
  160. err = serviceErr
  161. return
  162. }
  163. contractService.ContractServiceId = int(contractServiceId)
  164. //合同服务详情入库
  165. for j := 0; j < len(tmpContractService.DetailList); j++ {
  166. contractServiceDetail := tmpContractService.DetailList[j]
  167. //合同服务编号
  168. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  169. contractServiceDetail.ContractId = int(contractId)
  170. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  171. //合同服务详情入库
  172. contractServiceDetailId, detailErr := tx.Insert(contractServiceDetail)
  173. if detailErr != nil {
  174. err = detailErr
  175. return
  176. }
  177. contractServiceDetail.Id = int(contractServiceDetailId)
  178. tmpContractService.DetailList[j] = contractServiceDetail
  179. }
  180. }
  181. newContract = contractInfo
  182. return
  183. }
  184. type ContractList struct {
  185. ContractId int `description:"合同唯一id"`
  186. ContractCode string `description:"合同编号,长度32位"`
  187. SellerId int `description:"所属销售id"`
  188. SellerName string `description:"所属销售名称"`
  189. ProductId int `description:"产品id,1:ficc;2:权益"`
  190. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  191. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  192. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"`
  193. StartDate time.Time `description:"合同开始日期"`
  194. EndDate time.Time `description:"合同结束日期"`
  195. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  196. Price float64 `description:"实际金额,优惠后的金额"`
  197. PayRemark string `description:"付款方式说明,长度255位"`
  198. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  199. UseCompanyName string `description:"使用方名称,长度32位"`
  200. CreditCode string `description:"社会统一信用代码,长度64位"`
  201. ProvinceId int `description:"省级id"`
  202. Province string `description:"省级名称,长度16位"`
  203. CityId int `description:"市级id"`
  204. City string `description:"市级名称,长度32位"`
  205. Address string `description:"详细地址"`
  206. Fax string `description:"传真,长度32位"`
  207. Phone string `description:"电话,长度32位"`
  208. Postcode string `description:"邮编,长度16位"`
  209. Remark string `json:"-" description:"补充内容,长度255位"`
  210. SellerRemark string `description:"销售备注,长度255位"`
  211. ApprovalRemark string `description:"审核备注"`
  212. ModifyContent string `description:"修改内容"`
  213. FileUrl string `description:"合同文件地址"`
  214. CheckBackFileUrl string `description:"签回合同文件地址"`
  215. TemplateId int `description:"模板id"`
  216. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  217. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  218. ModifyTime time.Time `description:"合同最近一次修改时间"`
  219. CreateTime time.Time `description:"合同添加时间"`
  220. ApproveTime time.Time `description:"审批时间"`
  221. InvalidTime time.Time `description:"作废时间"`
  222. CheckBackFileTime time.Time `description:"合同签回时间"`
  223. RescindTime time.Time `description:"解约时间"`
  224. StartDateStr string `description:"合同起始时间"`
  225. EndDateStr string `description:"合同结束时间"`
  226. ModifyTimeStr string `description:"最近一次更新时间"`
  227. CreateTimeStr string `description:"合同添加时间"`
  228. ApproveTimeStr string `description:"审批时间"`
  229. InvalidTimeStr string `description:"作废时间"`
  230. CheckBackFileTimeStr string `description:"合同签回时间"`
  231. RescindTimeStr string `description:"解约时间"`
  232. Service []*contract.ContractServiceAndDetail
  233. }
  234. // 获取合同列表数据数量
  235. func GetContractListCount(condition string, pars []interface{}) (count int, err error) {
  236. o := orm.NewOrm()
  237. sql := "select count(*) AS COUNT from contract where 1=1 AND is_delete = 0 "
  238. sql += condition
  239. err = o.Raw(sql, pars).QueryRow(&count)
  240. return
  241. }
  242. // 获取合同列表数据
  243. func GetContractList(condition string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) {
  244. o := orm.NewOrm()
  245. sql := "select * from contract where 1=1 AND is_delete = 0 "
  246. sql += condition
  247. sql += ` order by modify_time desc LIMIT ?,? `
  248. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  249. return
  250. }
  251. // GetJoinContractListCount 获取合同列表数据数量
  252. func GetJoinContractListCount(condition, joinStr string, pars []interface{}) (count int, err error) {
  253. o := orm.NewOrm()
  254. sql := `select count(a.contract_id) AS COUNT from contract a ` + joinStr
  255. sql += ` where a.is_delete = 0 `
  256. sql += condition
  257. err = o.Raw(sql, pars).QueryRow(&count)
  258. return
  259. }
  260. // GetJoinContractList 获取合同列表数据
  261. func GetJoinContractList(condition, joinStr string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) {
  262. o := orm.NewOrm()
  263. sql := "select a.* from contract a " + joinStr
  264. sql += ` where a.is_delete = 0 `
  265. sql += condition
  266. sql += ` order by modify_time desc LIMIT ?,? `
  267. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  268. return
  269. }
  270. // 修改合同
  271. func EditContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (err error) {
  272. o := orm.NewOrm()
  273. tx, err := o.Begin()
  274. if err != nil {
  275. return
  276. }
  277. defer func() {
  278. if err != nil {
  279. _ = tx.Rollback()
  280. } else {
  281. _ = tx.Commit()
  282. }
  283. }()
  284. contractId := contractInfo.ContractId
  285. //合同数据入库
  286. _, err = tx.Update(contractInfo)
  287. if err != nil {
  288. return
  289. }
  290. //删除合同的原始服务信息
  291. sql := `delete from contract_service where contract_id = ?`
  292. _, err = tx.Raw(sql, contractId).Exec()
  293. //删除合同的原始服务详情信息
  294. sql = `delete from contract_service_detail where contract_id = ?`
  295. _, err = tx.Raw(sql, contractId).Exec()
  296. for i := 0; i < len(contractServiceAndDetailList); i++ {
  297. //合同服务数据入库
  298. tmpContractService := contractServiceAndDetailList[i]
  299. contractService := &contract.ContractService{
  300. ContractId: int(contractId),
  301. Title: tmpContractService.Title,
  302. ProductId: contractInfo.ProductId,
  303. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  304. Value: tmpContractService.Value,
  305. HasDetail: tmpContractService.HasDetail,
  306. CreateTime: time.Now(),
  307. }
  308. contractServiceId, serviceErr := tx.Insert(contractService)
  309. if serviceErr != nil {
  310. err = serviceErr
  311. return
  312. }
  313. contractService.ContractServiceId = int(contractServiceId)
  314. //合同服务详情入库
  315. for j := 0; j < len(tmpContractService.DetailList); j++ {
  316. contractServiceDetail := tmpContractService.DetailList[j]
  317. //合同服务编号
  318. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  319. contractServiceDetail.ContractId = contractService.ContractId
  320. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  321. //合同服务详情入库
  322. contractServiceDetailId, detailErr := tx.Insert(contractServiceDetail)
  323. if detailErr != nil {
  324. err = detailErr
  325. return
  326. }
  327. contractServiceDetail.Id = int(contractServiceDetailId)
  328. tmpContractService.DetailList[j] = contractServiceDetail
  329. }
  330. }
  331. return
  332. }
  333. // 添加生成后的合同地址
  334. func AddContractPdf(contractId int, pdfUrl string) (err error) {
  335. o := orm.NewOrm()
  336. //删除合同的原始服务信息
  337. sql := `update contract set file_url=? where contract_id = ?`
  338. _, err = o.Raw(sql, pdfUrl, contractId).Exec()
  339. return
  340. }
  341. // 删除合同
  342. func DeleteContract(contractInfo *Contract) (err error) {
  343. o := orm.NewOrm()
  344. tx, err := o.Begin()
  345. if err != nil {
  346. return
  347. }
  348. defer func() {
  349. if err != nil {
  350. _ = tx.Rollback()
  351. } else {
  352. _ = tx.Commit()
  353. }
  354. }()
  355. contractInfo.IsDelete = 1
  356. contractInfo.ModifyTime = time.Now()
  357. //合同数据入库
  358. _, err = tx.Update(contractInfo)
  359. if err != nil {
  360. return
  361. }
  362. return
  363. }
  364. // 作废合同
  365. func InvalidContract(contractInfo *Contract) (err error) {
  366. o := orm.NewOrm()
  367. tx, err := o.Begin()
  368. if err != nil {
  369. return
  370. }
  371. defer func() {
  372. if err != nil {
  373. _ = tx.Rollback()
  374. } else {
  375. _ = tx.Commit()
  376. }
  377. }()
  378. contractInfo.Status = "已作废"
  379. contractInfo.ModifyTime = time.Now()
  380. contractInfo.InvalidTime = time.Now()
  381. //合同数据入库
  382. _, err = tx.Update(contractInfo, "Status", "ModifyTime", "InvalidTime")
  383. if err != nil {
  384. return
  385. }
  386. return
  387. }
  388. // 生成合同编号
  389. func GetCompanyContractCode(productId int) (companyCode string, err error) {
  390. var num int
  391. o := orm.NewOrm()
  392. today := utils.GetToday(utils.FormatDate)
  393. sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?`
  394. err = o.Raw(sql, today).QueryRow(&num)
  395. if err != nil {
  396. return
  397. }
  398. companyType := ""
  399. switch productId {
  400. case 1:
  401. companyType = "FICC"
  402. case 2:
  403. companyType = "EQ"
  404. }
  405. companyCode = "HZ" + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  406. return
  407. }
  408. type CompanyNameList struct {
  409. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  410. }
  411. // 获取客户名称列表数据
  412. func GetCompanyNameList(sellerId int, keyword, status string) (list []*CompanyNameList, err error) {
  413. o := orm.NewOrm()
  414. sql := `select * from contract where is_delete=0 and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%') `
  415. if status != "" {
  416. sql += ` AND status='` + status + `' `
  417. }
  418. pars := make([]interface{}, 0)
  419. if sellerId > 0 {
  420. sql += ` and seller_id=? `
  421. pars = append(pars, sellerId)
  422. }
  423. sql += ` group by company_name order by modify_time desc `
  424. _, err = o.Raw(sql, pars).QueryRows(&list)
  425. return
  426. }
  427. // UpdateBusinessContractPaidPrice 更新合同已支付金额
  428. func UpdateBusinessContractPaidPrice(contractId int, updatePriceStr string) (err error) {
  429. o := orm.NewOrm()
  430. //删除合同的原始服务信息
  431. sql := `update contract set paid_price=paid_price` + updatePriceStr + ` where contract_id = ?`
  432. _, err = o.Raw(sql, contractId).Exec()
  433. return
  434. }