contract.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. //修改合同
  252. func EditContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (err error) {
  253. o := orm.NewOrm()
  254. tx, err := o.Begin()
  255. if err != nil {
  256. return
  257. }
  258. defer func() {
  259. if err != nil {
  260. _ = tx.Rollback()
  261. } else {
  262. _ = tx.Commit()
  263. }
  264. }()
  265. contractId := contractInfo.ContractId
  266. //合同数据入库
  267. _, err = tx.Update(contractInfo)
  268. if err != nil {
  269. return
  270. }
  271. //删除合同的原始服务信息
  272. sql := `delete from contract_service where contract_id = ?`
  273. _, err = tx.Raw(sql, contractId).Exec()
  274. //删除合同的原始服务详情信息
  275. sql = `delete from contract_service_detail where contract_id = ?`
  276. _, err = tx.Raw(sql, contractId).Exec()
  277. for i := 0; i < len(contractServiceAndDetailList); i++ {
  278. //合同服务数据入库
  279. tmpContractService := contractServiceAndDetailList[i]
  280. contractService := &contract.ContractService{
  281. ContractId: int(contractId),
  282. Title: tmpContractService.Title,
  283. ProductId: contractInfo.ProductId,
  284. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  285. Value: tmpContractService.Value,
  286. HasDetail: tmpContractService.HasDetail,
  287. CreateTime: time.Now(),
  288. }
  289. contractServiceId, serviceErr := tx.Insert(contractService)
  290. if serviceErr != nil {
  291. err = serviceErr
  292. return
  293. }
  294. contractService.ContractServiceId = int(contractServiceId)
  295. //合同服务详情入库
  296. for j := 0; j < len(tmpContractService.DetailList); j++ {
  297. contractServiceDetail := tmpContractService.DetailList[j]
  298. //合同服务编号
  299. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  300. contractServiceDetail.ContractId = contractService.ContractId
  301. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  302. //合同服务详情入库
  303. contractServiceDetailId, detailErr := tx.Insert(contractServiceDetail)
  304. if detailErr != nil {
  305. err = detailErr
  306. return
  307. }
  308. contractServiceDetail.Id = int(contractServiceDetailId)
  309. tmpContractService.DetailList[j] = contractServiceDetail
  310. }
  311. }
  312. return
  313. }
  314. //添加生成后的合同地址
  315. func AddContractPdf(contractId int, pdfUrl string) (err error) {
  316. o := orm.NewOrm()
  317. //删除合同的原始服务信息
  318. sql := `update contract set file_url=? where contract_id = ?`
  319. _, err = o.Raw(sql, pdfUrl, contractId).Exec()
  320. return
  321. }
  322. //删除合同
  323. func DeleteContract(contractInfo *Contract) (err error) {
  324. o := orm.NewOrm()
  325. tx, err := o.Begin()
  326. if err != nil {
  327. return
  328. }
  329. defer func() {
  330. if err != nil {
  331. _ = tx.Rollback()
  332. } else {
  333. _ = tx.Commit()
  334. }
  335. }()
  336. contractInfo.IsDelete = 1
  337. contractInfo.ModifyTime = time.Now()
  338. //合同数据入库
  339. _, err = tx.Update(contractInfo)
  340. if err != nil {
  341. return
  342. }
  343. return
  344. }
  345. //作废合同
  346. func InvalidContract(contractInfo *Contract) (err error) {
  347. o := orm.NewOrm()
  348. tx, err := o.Begin()
  349. if err != nil {
  350. return
  351. }
  352. defer func() {
  353. if err != nil {
  354. _ = tx.Rollback()
  355. } else {
  356. _ = tx.Commit()
  357. }
  358. }()
  359. contractInfo.Status = "已作废"
  360. contractInfo.ModifyTime = time.Now()
  361. contractInfo.InvalidTime = time.Now()
  362. //合同数据入库
  363. _, err = tx.Update(contractInfo, "Status", "ModifyTime", "InvalidTime")
  364. if err != nil {
  365. return
  366. }
  367. return
  368. }
  369. //生成合同编号
  370. func GetCompanyContractCode(productId int) (companyCode string, err error) {
  371. var num int
  372. o := orm.NewOrm()
  373. today := utils.GetToday(utils.FormatDate)
  374. sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?`
  375. err = o.Raw(sql, today).QueryRow(&num)
  376. if err != nil {
  377. return
  378. }
  379. companyType := ""
  380. switch productId {
  381. case 1:
  382. companyType = "FICC"
  383. case 2:
  384. companyType = "EQ"
  385. }
  386. companyCode = "HZ" + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  387. return
  388. }
  389. type CompanyNameList struct {
  390. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  391. }
  392. //获取客户名称列表数据
  393. func GetCompanyNameList(sellerId int, keyword, status string) (list []*CompanyNameList, err error) {
  394. o := orm.NewOrm()
  395. sql := `select * from contract where is_delete=0 and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%') `
  396. if status != "" {
  397. sql += ` AND status='` + status + `' `
  398. }
  399. pars := make([]interface{}, 0)
  400. if sellerId > 0 {
  401. sql += ` and seller_id=? `
  402. pars = append(pars, sellerId)
  403. }
  404. sql += ` group by company_name order by modify_time desc `
  405. _, err = o.Raw(sql, pars).QueryRows(&list)
  406. return
  407. }
  408. // UpdateBusinessContractPaidPrice 更新合同已支付金额
  409. func UpdateBusinessContractPaidPrice(contractId int, updatePriceStr string) (err error) {
  410. o := orm.NewOrm()
  411. //删除合同的原始服务信息
  412. sql := `update contract set paid_price=paid_price` + updatePriceStr + ` where contract_id = ?`
  413. _, err = o.Raw(sql, contractId).Exec()
  414. return
  415. }