contract_register.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. package fms
  2. import (
  3. "hongze/fms_api/global"
  4. "hongze/fms_api/models/base"
  5. "hongze/fms_api/utils"
  6. "time"
  7. )
  8. // ContractRegister 合同登记表
  9. type ContractRegister struct {
  10. ContractRegisterId int `gorm:"primaryKey;column:contract_register_id" json:"contract_register_id" description:"登记ID"`
  11. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  12. RelateContractCode string `gorm:"column:relate_contract_code" json:"relate_contract_code" description:"关联合同编号"`
  13. RelateContractMainCode string `gorm:"column:relate_contract_main_code" json:"relate_contract_main_code" description:"关联主合同编号"`
  14. CrmContractId int `gorm:"column:crm_contract_id" json:"crm_contract_id" description:"CRM系统-合同ID"`
  15. ContractSource int `gorm:"column:contract_source" json:"contract_source" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  16. CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
  17. ActualCompanyName string `gorm:"column:actual_company_name" json:"actual_company_name" description:"实际使用方"`
  18. ProductIds string `gorm:"column:product_ids" json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  19. SellerId int `gorm:"column:seller_id" json:"seller_id" description:"CRM系统-销售ID"`
  20. SellerName string `gorm:"column:seller_name" json:"seller_name" description:"CRM系统-销售名称"`
  21. RaiSellerId int `gorm:"column:rai_seller_id" json:"rai_seller_id" description:"CRM系统-权益销售ID"`
  22. RaiSellerName string `gorm:"column:rai_seller_name" json:"rai_seller_name" description:"CRM系统-权益销售名称"`
  23. ContractType int `gorm:"column:contract_type" json:"contract_type" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
  24. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  25. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  26. PaymentAmount float64 `gorm:"column:payment_amount" json:"payment_amount" description:"到款金额"`
  27. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  28. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  29. StartDate time.Time `gorm:"column:start_date" json:"start_date" description:"合同开始日期"`
  30. EndDate time.Time `gorm:"column:end_date" json:"end_date" description:"合同结束日期"`
  31. SignDate time.Time `gorm:"column:sign_date" json:"sign_date" description:"合同签订日期"`
  32. AgreedPayTime string `gorm:"column:agreed_pay_time" json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  33. ContractStatus int `gorm:"column:contract_status" json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  34. RegisterStatus int `gorm:"column:register_status" json:"register_status" description:"登记状态: 1-进行中; 2-已完成;"`
  35. Remark string `gorm:"column:remark" json:"remark" description:"备注信息"`
  36. ServiceRemark string `gorm:"column:service_remark" json:"service_remark" description:"套餐备注信息"`
  37. HasPayment int `gorm:"column:has_payment" json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  38. HasInvoice int `gorm:"column:has_invoice" json:"has_invoice" description:"是否需要开票到款: 0-无; 1-有"`
  39. NewCompany int `gorm:"column:new_company" json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  40. IsDeleted int `gorm:"column:is_deleted" json:"is_deleted" description:"是否已删除: 0-正常; 1-已删除"`
  41. ActualPayCompanies string `gorm:"column:actual_pay_companies" json:"actual_pay_companies" description:"合同关联的所有代付方名称, 英文逗号拼接"`
  42. base.TimeBase
  43. }
  44. func (c *ContractRegister) TableName() string {
  45. return "contract_register"
  46. }
  47. func (c *ContractRegister) Create() (err error) {
  48. err = global.DEFAULT_MYSQL.Create(c).Error
  49. return
  50. }
  51. func (c *ContractRegister) AddInBatches(list []*ContractRegister) (err error) {
  52. err = global.DEFAULT_MYSQL.CreateInBatches(list, len(list)).Error
  53. return
  54. }
  55. func (c *ContractRegister) Update(updateCols []string) (err error) {
  56. err = global.DEFAULT_MYSQL.Model(c).Select(updateCols).Updates(c).Error
  57. return
  58. }
  59. func (c *ContractRegister) Fetch(id int) (item *ContractRegister, err error) {
  60. err = global.DEFAULT_MYSQL.Model(c).Where("is_deleted = 0 AND contract_register_id = ?", id).First(&item).Error
  61. return
  62. }
  63. func (c *ContractRegister) FetchByCondition(condition string, pars []interface{}) (item *ContractRegister, err error) {
  64. err = global.DEFAULT_MYSQL.Model(c).
  65. Where("is_deleted = 0").
  66. Where(condition, pars...).
  67. First(&item).Error
  68. return
  69. }
  70. func (c *ContractRegister) List(condition string, pars []interface{}) (list []*ContractRegister, err error) {
  71. list = make([]*ContractRegister, 0)
  72. err = global.DEFAULT_MYSQL.Model(c).
  73. Where("is_deleted = 0").
  74. Where("register_status <> 0").
  75. Where(condition, pars...).
  76. Find(&list).Error
  77. return
  78. }
  79. func (c *ContractRegister) PageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractRegister, err error) {
  80. results = make([]*ContractRegister, 0)
  81. query := global.DEFAULT_MYSQL.Model(c).
  82. Where("is_deleted = 0").
  83. Where(condition, pars...)
  84. query.Count(&count)
  85. if len(page.GetOrderItemsString()) > 0 {
  86. query = query.Order(page.GetOrderItemsString())
  87. }
  88. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  89. return
  90. }
  91. // ContractRegisterListReq 合同登记列表请求体
  92. type ContractRegisterListReq struct {
  93. Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
  94. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"合同开始日期"`
  95. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"合同结束日期"`
  96. ServiceTypes string `json:"service_types" form:"service_types" description:"套餐类型"`
  97. ContractType string `json:"contract_type" form:"contract_type" description:"合同类型"`
  98. RegisterStatus int `json:"register_status" form:"register_status" description:"登记状态"`
  99. ListParam int `json:"list_param" form:"list_param" description:"套餐类型: 0-全部; 1-ficc; 2-权益"`
  100. base.PageReq
  101. }
  102. // ContractRegisterItem 合同登记
  103. type ContractRegisterItem struct {
  104. ContractRegisterId int `json:"contract_register_id" description:"登记ID"`
  105. ContractCode string `json:"contract_code" description:"合同编号"`
  106. RelateContractCode string `json:"relate_contract_code" description:"关联合同编号"`
  107. CrmContractId int `json:"crm_contract_id" description:"CRM系统-合同ID"`
  108. ContractSource int `json:"contract_source" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  109. CompanyName string `json:"company_name" description:"客户名称"`
  110. ActualCompanyName string `json:"actual_company_name" description:"实际使用方"`
  111. ProductIds string `json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  112. SellerId int `json:"seller_id" description:"CRM系统-销售ID"`
  113. SellerName string `json:"seller_name" description:"CRM系统-销售名称"`
  114. RaiSellerId int `json:"rai_seller_id" description:"CRM系统-权益销售ID"`
  115. RaiSellerName string `json:"rai_seller_name" description:"CRM系统-权益销售名称"`
  116. ContractType int `json:"contract_type" description:"合同类型: 1-新签; 2-续约"`
  117. ContractAmount float64 `json:"contract_amount" description:"合同金额"`
  118. InvoicedAmount float64 `json:"invoiced_amount" description:"开票金额"`
  119. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  120. CurrencyUnit string `json:"currency_unit" description:"货币国际代码"`
  121. RMBRate float64 `json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  122. StartDate string `json:"start_date" description:"合同开始日期"`
  123. EndDate string `json:"end_date" description:"合同结束日期"`
  124. SignDate string `json:"sign_date" description:"合同签订日期"`
  125. AgreedPayTime string `json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  126. ContractStatus int `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  127. RegisterStatus int `json:"register_status" description:"登记状态: 1-进行中; 2-已完成;"`
  128. Remark string `json:"remark" description:"备注信息"`
  129. ServiceRemark string `json:"service_remark" description:"套餐备注信息"`
  130. HasPayment int `json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  131. HasInvoice int `json:"has_invoice" description:"是否需要开票到款: 0-无; 1-有"`
  132. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  133. ActualPayCompanies string `json:"actual_pay_companies" description:"合同关联的所有代付方名称, 英文逗号拼接"`
  134. CreateTime string `json:"create_time" description:"登记时间"`
  135. SellerIds string `json:"seller_ids"`
  136. }
  137. // ContractRegisterList 合同登记列表
  138. type ContractRegisterList struct {
  139. *ContractRegisterItem
  140. ContractTypeString string `json:"contract_type_string" description:"合同类型"`
  141. ServicesName string `json:"services" description:"套餐名称"`
  142. InvoiceList []*ContractInvoiceItem `json:"invoice_list" description:"开票信息"`
  143. PaymentList []*ContractInvoiceItem `json:"payment_list" description:"到款信息"`
  144. }
  145. // ContractRegisterDelReq 合同登记-删除请求体
  146. type ContractRegisterDelReq struct {
  147. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  148. }
  149. // ContractRegisterUpdateStatusReq 合同登记-修改合同状态请求体
  150. type ContractRegisterUpdateStatusReq struct {
  151. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  152. ContractStatus int `json:"contract_status" binding:"oneof=1 2 3 4 5" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回; 4-已终止, 5-邮件备案"`
  153. }
  154. // ContractRegisterAddReq 新增合同登记请求体
  155. type ContractRegisterAddReq struct {
  156. ContractCode string `json:"contract_code" binding:"required" description:"合同编号"`
  157. RelateContractCode string `json:"relate_contract_code" description:"关联合同编号"`
  158. RelateContractMainCode string `json:"relate_contract_main_code" description:"关联主合同编号"`
  159. CrmContractId int `json:"crm_contract_id" description:"CRM系统-合同ID"`
  160. ContractSource int `json:"contract_source" binding:"oneof=0 1" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  161. CompanyName string `json:"company_name" binding:"required" description:"客户名称"`
  162. SellerIds string `json:"seller_ids" binding:"required" description:"CRM系统-销售ID"`
  163. ContractType int `json:"contract_type" binding:"oneof=1 2 3 4" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
  164. ContractAmount float64 `json:"contract_amount" binding:"required" description:"合同金额"`
  165. CurrencyUnit string `json:"currency_unit" binding:"required" description:"货币单位"`
  166. StartDate string `json:"start_date" binding:"required" description:"合同开始日期"`
  167. EndDate string `json:"end_date" binding:"required" description:"合同结束日期"`
  168. SignDate string `json:"sign_date" description:"合同签订日期"`
  169. AgreedPayTime string `json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  170. ContractStatus int `json:"contract_status" binding:"oneof=1 2 3 4 5" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回; 4-已终止; 5-邮件备案"`
  171. Remark string `json:"remark" description:"备注信息"`
  172. ProductIds string `json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  173. ServiceRemark string `json:"service_remark" description:"套餐备注"`
  174. HasPayment int `json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  175. HasInvoice int `json:"has_invoice" description:"是否需要开票到款流程: 0-无; 1-有"`
  176. Supplement int `json:"supplement" description:"是否为补录合同: 0-否; 1-是"`
  177. ContractRegisterId int `json:"contract_register_id" description:"登记ID"`
  178. ActualCompanyName string `json:"actual_company_name" description:"实际使用方"`
  179. Services []ContractServiceAddReq `json:"services" description:"服务套餐内容"`
  180. ServiceAmount []ContractServiceAmountAddReq `json:"service_amount" description:"服务套餐金额"`
  181. ActualPayCompanies string `json:"actual_pay_companies" description:"合同关联的所有代付方名称, 英文逗号拼接"`
  182. }
  183. // ContractRegisterEditReq 编辑合同登记请求体
  184. type ContractRegisterEditReq struct {
  185. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  186. ContractRegisterAddReq
  187. }
  188. // CreateContractRegisterAndServices 新增合同登记及套餐
  189. func CreateContractRegisterAndServices(item *ContractRegister, serviceDetail []*ContractServiceAndDetail, serviceAmount []*ContractServiceAmount) (err error) {
  190. tx := global.DEFAULT_MYSQL.Begin()
  191. defer func() {
  192. if err != nil {
  193. tx.Rollback()
  194. } else {
  195. tx.Commit()
  196. }
  197. }()
  198. // 合同登记
  199. tx.Create(item)
  200. //nowTime := time.Now().Local()
  201. for i := 0; i < len(serviceDetail); i++ {
  202. // 合同服务
  203. t := serviceDetail[i]
  204. contractService := &ContractService{
  205. ContractRegisterId: item.ContractRegisterId,
  206. ProductId: t.ProductId,
  207. ServiceTemplateId: t.ServiceTemplateId,
  208. Title: t.Title,
  209. Value: t.Value,
  210. TableValue: t.TableValue,
  211. HasDetail: t.HasDetail,
  212. ChartPermissionId: t.ChartPermissionId,
  213. ChartPermissionIds: t.ChartPermissionIds,
  214. }
  215. contractService.Set()
  216. tx.Create(contractService)
  217. //// 合同服务详情
  218. //for j := 0; j < len(t.Detail); j++ {
  219. // contractServiceDetail := t.Detail[j]
  220. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  221. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  222. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  223. // contractServiceDetail.CreateTime = nowTime
  224. // tx.Create(contractServiceDetail)
  225. // t.Detail[j] = contractServiceDetail
  226. //}
  227. }
  228. for i := 0; i < len(serviceAmount); i++ {
  229. // 合同服务
  230. t := serviceAmount[i]
  231. tmp := &ContractServiceAmount{
  232. ContractRegisterId: item.ContractRegisterId,
  233. ProductId: t.ProductId,
  234. ServiceAmount: t.ServiceAmount,
  235. CurrencyUnit: t.CurrencyUnit,
  236. }
  237. tx.Create(tmp)
  238. }
  239. return
  240. }
  241. // UpdateContractRegister 更新合同登记
  242. func UpdateContractRegister(item *ContractRegister, updateCols []string, serviceDetail []*ContractServiceAndDetail,
  243. invoiceList []*ContractInvoice, invoiceUpdateCols []string, invoiceHandleType int, serviceAmount []*ContractServiceAmount) (err error) {
  244. tx := global.DEFAULT_MYSQL.Begin()
  245. defer func() {
  246. if err != nil {
  247. tx.Rollback()
  248. } else {
  249. tx.Commit()
  250. }
  251. }()
  252. // 更新合同登记
  253. if e := tx.Model(item).Select(updateCols).Updates(item).Error; e != nil {
  254. err = e
  255. return
  256. }
  257. // 删除原服务及详情信息
  258. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  259. tx.Exec(sql, item.ContractRegisterId)
  260. sql = `DELETE FROM contract_service_detail WHERE contract_register_id = ?`
  261. tx.Exec(sql, item.ContractRegisterId)
  262. sql = `DELETE FROM contract_service_amount WHERE contract_register_id = ?`
  263. tx.Exec(sql, item.ContractRegisterId)
  264. // 新增合同服务
  265. //nowTime := time.Now().Local()
  266. for i := 0; i < len(serviceDetail); i++ {
  267. // 合同服务
  268. t := serviceDetail[i]
  269. contractService := &ContractService{
  270. ContractRegisterId: item.ContractRegisterId,
  271. ProductId: t.ProductId,
  272. ServiceTemplateId: t.ServiceTemplateId,
  273. Title: t.Title,
  274. Value: t.Value,
  275. TableValue: t.TableValue,
  276. HasDetail: t.HasDetail,
  277. ChartPermissionId: t.ChartPermissionId,
  278. ChartPermissionIds: t.ChartPermissionIds,
  279. }
  280. contractService.Set()
  281. tx.Create(contractService)
  282. //// 合同服务详情
  283. //for j := 0; j < len(t.Detail); j++ {
  284. // contractServiceDetail := t.Detail[j]
  285. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  286. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  287. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  288. // contractServiceDetail.CreateTime = nowTime
  289. // tx.Create(contractServiceDetail)
  290. // t.Detail[j] = contractServiceDetail
  291. //}
  292. }
  293. for i := 0; i < len(serviceAmount); i++ {
  294. // 合同服务
  295. t := serviceAmount[i]
  296. tmp := &ContractServiceAmount{
  297. ContractRegisterId: item.ContractRegisterId,
  298. ProductId: t.ProductId,
  299. ServiceAmount: t.ServiceAmount,
  300. CurrencyUnit: t.CurrencyUnit,
  301. }
  302. tx.Create(tmp)
  303. }
  304. // 开票到款操作类型: 0-无; 1-更新; 2-删除;
  305. if invoiceHandleType == 2 {
  306. sql = `UPDATE contract_invoice SET is_deleted = 1 WHERE contract_register_id = ?`
  307. if e := tx.Exec(sql, item.ContractRegisterId).Error; e != nil {
  308. err = e
  309. return
  310. }
  311. }
  312. if invoiceHandleType == 1 {
  313. for i := range invoiceList {
  314. if e := tx.Model(invoiceList[i]).Select(invoiceUpdateCols).Updates(invoiceList[i]).Error; e != nil {
  315. err = e
  316. return
  317. }
  318. }
  319. }
  320. return
  321. }
  322. // ContractRegisterDetailReq 合同登记-详情请求体
  323. type ContractRegisterDetailReq struct {
  324. ContractRegisterId int `json:"contract_register_id" form:"contract_register_id" binding:"required,gte=1"`
  325. }
  326. // ContractRegisterDetail 合同登记详情
  327. type ContractRegisterDetail struct {
  328. *ContractRegisterItem
  329. ServiceList []*ContractServiceAndDetail `json:"service_list" description:"合同服务及详情"`
  330. InvoiceList []*ContractInvoiceItem `json:"invoice_list" description:"开票信息"`
  331. PaymentList []*ContractInvoiceItem `json:"payment_list" description:"到款信息"`
  332. Logs []*ContractRegisterLogItem `json:"logs" description:"登记日志"`
  333. ServiceAmountList []*ContractServiceAmountItem `json:"service_amount_list" description:"合同套餐金额"`
  334. }
  335. // GetContractRegisterItemById 获取合同登记信息
  336. func GetContractRegisterItemById(id int) (result *ContractRegisterItem, err error) {
  337. item := new(ContractRegister)
  338. err = global.DEFAULT_MYSQL.Model(ContractRegister{}).
  339. Where("is_deleted = 0 AND contract_register_id = ?", id).
  340. First(&item).Error
  341. if err != nil && err != utils.ErrNoRow {
  342. return
  343. }
  344. result = formatContractRegister2Item(item)
  345. return
  346. }
  347. // GetContractRegisterItemPageList 获取合同登记列表-分页
  348. func GetContractRegisterItemPageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractRegisterItem, err error) {
  349. list := make([]*ContractRegister, 0)
  350. query := global.DEFAULT_MYSQL.Table("contract_register").
  351. Where("is_deleted = 0").
  352. Where("register_status <> 0").
  353. Where(condition, pars...)
  354. query.Count(&count)
  355. if len(page.GetOrderItemsString()) > 0 {
  356. query = query.Order(page.GetOrderItemsString())
  357. }
  358. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&list).Error
  359. if err != nil {
  360. return
  361. }
  362. for i := range list {
  363. results = append(results, formatContractRegister2Item(list[i]))
  364. }
  365. return
  366. }
  367. // formatContractRegister2Item 格式化ContractRegister
  368. func formatContractRegister2Item(item *ContractRegister) (formatItem *ContractRegisterItem) {
  369. formatItem = new(ContractRegisterItem)
  370. formatItem.ContractRegisterId = item.ContractRegisterId
  371. formatItem.ContractCode = item.ContractCode
  372. formatItem.RelateContractCode = item.RelateContractCode
  373. formatItem.CrmContractId = item.CrmContractId
  374. formatItem.ContractSource = item.ContractSource
  375. formatItem.CompanyName = item.CompanyName
  376. formatItem.ActualCompanyName = item.ActualCompanyName
  377. formatItem.ProductIds = item.ProductIds
  378. formatItem.SellerId = item.SellerId
  379. formatItem.SellerName = item.SellerName
  380. formatItem.RaiSellerId = item.RaiSellerId
  381. formatItem.RaiSellerName = item.RaiSellerName
  382. formatItem.ContractType = item.ContractType
  383. formatItem.ContractAmount = item.ContractAmount
  384. formatItem.InvoicedAmount = item.InvoicedAmount
  385. formatItem.PaymentAmount = item.PaymentAmount
  386. formatItem.CurrencyUnit = item.CurrencyUnit
  387. formatItem.RMBRate = item.RMBRate
  388. formatItem.StartDate = utils.TimeTransferString(utils.FormatDate, item.StartDate)
  389. formatItem.EndDate = utils.TimeTransferString(utils.FormatDate, item.EndDate)
  390. formatItem.SignDate = utils.TimeTransferString(utils.FormatDate, item.SignDate)
  391. formatItem.AgreedPayTime = item.AgreedPayTime
  392. formatItem.ContractStatus = item.ContractStatus
  393. formatItem.RegisterStatus = item.RegisterStatus
  394. formatItem.Remark = item.Remark
  395. formatItem.ServiceRemark = item.ServiceRemark
  396. formatItem.HasPayment = item.HasPayment
  397. formatItem.HasInvoice = item.HasInvoice
  398. formatItem.NewCompany = item.NewCompany
  399. formatItem.ActualPayCompanies = item.ActualPayCompanies
  400. formatItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
  401. return
  402. }
  403. // CreateImportContractRegister 新增导入的合同登记及套餐、开票到款信息
  404. func CreateImportContractRegister(item *ContractRegister, serviceList []*ContractService, invoiceList []*ContractInvoice, serviceAmountList []*ContractServiceAmount) (newId int, err error) {
  405. tx := global.DEFAULT_MYSQL.Begin()
  406. defer func() {
  407. if err != nil {
  408. tx.Rollback()
  409. } else {
  410. tx.Commit()
  411. }
  412. }()
  413. // 新增合同登记
  414. if e := tx.Create(item).Error; e != nil {
  415. err = e
  416. return
  417. }
  418. newId = item.ContractRegisterId
  419. // 新增套餐
  420. if len(serviceList) > 0 {
  421. // 过滤重复的数组
  422. serviceMap := make(map[int]struct{})
  423. addServiceList := make([]*ContractService, 0)
  424. for i := range serviceList {
  425. if _, ok := serviceMap[serviceList[i].ServiceTemplateId]; !ok {
  426. serviceList[i].ContractRegisterId = newId
  427. addServiceList = append(addServiceList, serviceList[i])
  428. serviceMap[serviceList[i].ServiceTemplateId] = struct{}{}
  429. }
  430. }
  431. if e := tx.CreateInBatches(addServiceList, len(addServiceList)).Error; e != nil {
  432. err = e
  433. return
  434. }
  435. }
  436. // 新增合同套餐金额相关信息
  437. if len(serviceAmountList) > 0 {
  438. for i := range serviceAmountList {
  439. serviceAmountList[i].ContractRegisterId = newId
  440. }
  441. if e := tx.CreateInBatches(serviceAmountList, len(serviceAmountList)).Error; e != nil {
  442. err = e
  443. return
  444. }
  445. }
  446. // 新增开票/到款
  447. if len(invoiceList) > 0 {
  448. for i := range invoiceList {
  449. invoiceList[i].ContractRegisterId = newId
  450. }
  451. if e := tx.CreateInBatches(invoiceList, len(invoiceList)).Error; e != nil {
  452. err = e
  453. return
  454. }
  455. }
  456. return
  457. }
  458. func GetContractInfoByRegisterIds(registerIds []int) (list []ContractRegister, err error) {
  459. err = global.DEFAULT_MYSQL.Model(ContractRegister{}).
  460. Where("is_deleted = 0 AND contract_register_id in ?", registerIds).
  461. Find(&list).Error
  462. return
  463. }
  464. // CreateContractRegisterAndServices 新增合同登记及套餐
  465. func CreateContractRegisterAndServicesAndPayMent(item *ContractRegister, serviceDetail []*ContractServiceAndDetail) (err error) {
  466. tx := global.DEFAULT_MYSQL.Begin()
  467. defer func() {
  468. if err != nil {
  469. tx.Rollback()
  470. } else {
  471. tx.Commit()
  472. }
  473. }()
  474. // 合同登记
  475. tx.Create(item)
  476. //nowTime := time.Now().Local()
  477. for i := 0; i < len(serviceDetail); i++ {
  478. // 合同服务
  479. t := serviceDetail[i]
  480. contractService := &ContractService{
  481. ContractRegisterId: item.ContractRegisterId,
  482. ProductId: t.ProductId,
  483. ServiceTemplateId: t.ServiceTemplateId,
  484. Title: t.Title,
  485. Value: t.Value,
  486. TableValue: t.TableValue,
  487. HasDetail: t.HasDetail,
  488. ChartPermissionId: t.ChartPermissionId,
  489. ChartPermissionIds: t.ChartPermissionIds,
  490. }
  491. contractService.Set()
  492. tx.Create(contractService)
  493. //// 合同服务详情
  494. //for j := 0; j < len(t.Detail); j++ {
  495. // contractServiceDetail := t.Detail[j]
  496. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  497. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  498. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  499. // contractServiceDetail.CreateTime = nowTime
  500. // tx.Create(contractServiceDetail)
  501. // t.Detail[j] = contractServiceDetail
  502. //}
  503. }
  504. return
  505. }
  506. type CheckContractNameReq struct {
  507. CompanyName string `json:"company_name" form:"company_name" description:"客户名称"`
  508. StartDate string `json:"start_date" form:"start_date" description:"合同开始日期"`
  509. EndDate string `json:"end_date" form:"end_date" description:"合同结束日期"`
  510. }
  511. type CheckContractNameResp struct {
  512. Exist int `json:"exist" description:"是否存在相似的合同:0不存在,1存在"`
  513. }
  514. // UpdateContractPreRegister 更新合同预登记
  515. func UpdateContractPreRegister(item *ContractRegister, updateCols []string, serviceDetail []*ContractServiceAndDetail,
  516. invoiceList []*ContractInvoice, invoiceUpdateCols []string, delInvoiceIds, delPreRegisterIds []int) (err error) {
  517. tx := global.DEFAULT_MYSQL.Begin()
  518. defer func() {
  519. if err != nil {
  520. tx.Rollback()
  521. } else {
  522. tx.Commit()
  523. }
  524. }()
  525. // 更新合同登记
  526. if e := tx.Model(item).Select(updateCols).Updates(item).Error; e != nil {
  527. err = e
  528. return
  529. }
  530. // 删除原服务及详情信息
  531. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  532. tx.Exec(sql, item.ContractRegisterId)
  533. // 新增合同服务
  534. //nowTime := time.Now().Local()
  535. for i := 0; i < len(serviceDetail); i++ {
  536. // 合同服务
  537. t := serviceDetail[i]
  538. contractService := &ContractService{
  539. ContractRegisterId: item.ContractRegisterId,
  540. ProductId: t.ProductId,
  541. ServiceTemplateId: t.ServiceTemplateId,
  542. Title: t.Title,
  543. Value: t.Value,
  544. TableValue: t.TableValue,
  545. HasDetail: t.HasDetail,
  546. ChartPermissionId: t.ChartPermissionId,
  547. ChartPermissionIds: t.ChartPermissionIds,
  548. }
  549. contractService.Set()
  550. tx.Create(contractService)
  551. //// 合同服务详情
  552. //for j := 0; j < len(t.Detail); j++ {
  553. // contractServiceDetail := t.Detail[j]
  554. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  555. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  556. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  557. // contractServiceDetail.CreateTime = nowTime
  558. // tx.Create(contractServiceDetail)
  559. // t.Detail[j] = contractServiceDetail
  560. //}
  561. }
  562. for i := range invoiceList {
  563. if invoiceList[i].ContractInvoiceId == 0 {
  564. if e := invoiceList[i].Create(); e != nil {
  565. err = e
  566. return
  567. }
  568. } else {
  569. if e := tx.Model(invoiceList[i]).Select(invoiceUpdateCols).Updates(invoiceList[i]).Error; e != nil {
  570. err = e
  571. return
  572. }
  573. }
  574. }
  575. //更新另一类型的invoice表数据
  576. invoiceItem := ContractInvoice{
  577. ContractRegisterId: item.ContractRegisterId,
  578. StartDate: item.StartDate,
  579. EndDate: item.EndDate,
  580. CurrencyUnit: item.CurrencyUnit,
  581. }
  582. tx.Model(&invoiceItem).Select([]string{"StartDate", "EndDate", "CurrencyUnit"}).Where("ContractRegisterId", item.ContractRegisterId).Updates(invoiceItem)
  583. //删除
  584. if len(delInvoiceIds) > 0 {
  585. for _, id := range delInvoiceIds {
  586. ob := ContractInvoice{ContractInvoiceId: id}
  587. tx.Model(&ob).Delete(ob)
  588. }
  589. }
  590. if len(delPreRegisterIds) > 0 {
  591. for _, id := range delPreRegisterIds {
  592. ob := ContractPreRegister{PreRegisterId: id}
  593. tx.Model(&ob).Delete(ob)
  594. }
  595. }
  596. return
  597. }
  598. // UpdateContractRegister 预登记保存和更新
  599. func UpdateContractRegisterPre(item *ContractRegister, updateCols []string, serviceDetail []*ContractServiceAndDetail,
  600. invoiceList []*ContractInvoice, invoiceUpdateCols []string, invoiceHandleType int, ppList []*ContractPreRegister) (err error) {
  601. tx := global.DEFAULT_MYSQL.Begin()
  602. defer func() {
  603. if err != nil {
  604. tx.Rollback()
  605. } else {
  606. tx.Commit()
  607. }
  608. }()
  609. // 更新合同登记
  610. if e := tx.Model(item).Select(updateCols).Updates(item).Error; e != nil {
  611. err = e
  612. return
  613. }
  614. // 删除原服务及详情信息
  615. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  616. tx.Exec(sql, item.ContractRegisterId)
  617. sql = `DELETE FROM contract_service_detail WHERE contract_register_id = ?`
  618. tx.Exec(sql, item.ContractRegisterId)
  619. sql = `DELETE FROM contract_service_amount WHERE contract_register_id = ?`
  620. tx.Exec(sql, item.ContractRegisterId)
  621. // 新增合同服务
  622. //nowTime := time.Now().Local()
  623. for i := 0; i < len(serviceDetail); i++ {
  624. // 合同服务
  625. t := serviceDetail[i]
  626. contractService := &ContractService{
  627. ContractRegisterId: item.ContractRegisterId,
  628. ProductId: t.ProductId,
  629. ServiceTemplateId: t.ServiceTemplateId,
  630. Title: t.Title,
  631. Value: t.Value,
  632. TableValue: t.TableValue,
  633. HasDetail: t.HasDetail,
  634. ChartPermissionId: t.ChartPermissionId,
  635. ChartPermissionIds: t.ChartPermissionIds,
  636. }
  637. contractService.Set()
  638. tx.Create(contractService)
  639. //// 合同服务详情
  640. //for j := 0; j < len(t.Detail); j++ {
  641. // contractServiceDetail := t.Detail[j]
  642. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  643. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  644. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  645. // contractServiceDetail.CreateTime = nowTime
  646. // tx.Create(contractServiceDetail)
  647. // t.Detail[j] = contractServiceDetail
  648. //}
  649. }
  650. //更新另一类型的invoice表数据
  651. invoiceItem := ContractInvoice{
  652. ContractRegisterId: item.ContractRegisterId,
  653. StartDate: item.StartDate,
  654. EndDate: item.EndDate,
  655. CurrencyUnit: item.CurrencyUnit,
  656. }
  657. tx.Model(&invoiceItem).Select([]string{"StartDate", "EndDate", "CurrencyUnit"}).Where("ContractRegisterId", item.ContractRegisterId).Updates(invoiceItem)
  658. // 开票到款操作类型: 0-无; 1-更新; 2-新增;
  659. if invoiceHandleType == 2 {
  660. for _, v := range invoiceList {
  661. if e := v.Create(); e != nil {
  662. err = e
  663. return
  664. }
  665. if len(ppList) > 0 {
  666. pp := &ContractPreRegister{
  667. PreRegisterId: ppList[0].PreRegisterId,
  668. }
  669. ppUpdateCols := make([]string, 0)
  670. if v.InvoiceType == 3 {
  671. pp.InvoiceId = v.ContractInvoiceId
  672. ppUpdateCols = append(ppUpdateCols, "InvoiceId")
  673. } else {
  674. pp.ArriveId = v.ContractInvoiceId
  675. ppUpdateCols = append(ppUpdateCols, "ArriveId")
  676. }
  677. if e := pp.Update(ppUpdateCols); e != nil {
  678. err = e
  679. return
  680. }
  681. }
  682. }
  683. }
  684. if invoiceHandleType == 1 {
  685. for i := range invoiceList {
  686. if e := tx.Model(invoiceList[i]).Select(invoiceUpdateCols).Updates(invoiceList[i]).Error; e != nil {
  687. err = e
  688. return
  689. }
  690. }
  691. }
  692. return
  693. }
  694. // UpdateSupplementContractPreRegister 更新补录合同预登记
  695. func UpdateSupplementContractPreRegister(item *ContractRegister, serviceAmountList []*ContractServiceAmount, serviceDetail []*ContractServiceAndDetail) (err error) {
  696. tx := global.DEFAULT_MYSQL.Begin()
  697. defer func() {
  698. if err != nil {
  699. tx.Rollback()
  700. } else {
  701. tx.Commit()
  702. }
  703. }()
  704. for i := 0; i < len(serviceAmountList); i++ {
  705. // 合同服务
  706. t := serviceAmountList[i]
  707. tmp := &ContractServiceAmount{
  708. ContractRegisterId: item.ContractRegisterId,
  709. ProductId: t.ProductId,
  710. ServiceAmount: t.ServiceAmount,
  711. CurrencyUnit: t.CurrencyUnit,
  712. }
  713. if e := tmp.Create(); e != nil {
  714. err = e
  715. return
  716. }
  717. }
  718. // 删除原服务及详情信息
  719. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  720. tx.Exec(sql, item.ContractRegisterId)
  721. // 新增合同服务
  722. //nowTime := time.Now().Local()
  723. for i := 0; i < len(serviceDetail); i++ {
  724. // 合同服务
  725. t := serviceDetail[i]
  726. contractService := &ContractService{
  727. ContractRegisterId: item.ContractRegisterId,
  728. ProductId: t.ProductId,
  729. ServiceTemplateId: t.ServiceTemplateId,
  730. Title: t.Title,
  731. Value: t.Value,
  732. TableValue: t.TableValue,
  733. HasDetail: t.HasDetail,
  734. ChartPermissionId: t.ChartPermissionId,
  735. ChartPermissionIds: t.ChartPermissionIds,
  736. }
  737. contractService.Set()
  738. tx.Create(contractService)
  739. //// 合同服务详情
  740. //for j := 0; j < len(t.Detail); j++ {
  741. // contractServiceDetail := t.Detail[j]
  742. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  743. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  744. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  745. // contractServiceDetail.CreateTime = nowTime
  746. // tx.Create(contractServiceDetail)
  747. // t.Detail[j] = contractServiceDetail
  748. //}
  749. }
  750. return
  751. }