contract.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package contract
  2. import (
  3. "fmt"
  4. "hongze/hongze_mobile_admin/models/custom/contract"
  5. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  6. "hongze/hongze_mobile_admin/utils"
  7. "rdluck_tools/orm"
  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. //根据合同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. o.Begin()
  130. defer func() {
  131. if err != nil {
  132. o.Rollback()
  133. } else {
  134. o.Commit()
  135. }
  136. }()
  137. //合同数据入库
  138. contractId, err := o.Insert(contractInfo)
  139. if err != nil {
  140. return
  141. }
  142. contractInfo.ContractId = int(contractId)
  143. for i := 0; i < len(contractServiceAndDetailList); i++ {
  144. //合同服务数据入库
  145. tmpContractService := contractServiceAndDetailList[i]
  146. contractService := &contract.ContractService{
  147. ContractId: int(contractId),
  148. ProductId: contractInfo.ProductId,
  149. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  150. Title: tmpContractService.Title,
  151. Value: tmpContractService.Value,
  152. HasDetail: tmpContractService.HasDetail,
  153. CreateTime: time.Now(),
  154. }
  155. contractServiceId, serviceErr := o.Insert(contractService)
  156. if serviceErr != nil {
  157. err = serviceErr
  158. return
  159. }
  160. contractService.ContractServiceId = int(contractServiceId)
  161. //合同服务详情入库
  162. for j := 0; j < len(tmpContractService.DetailList); j++ {
  163. contractServiceDetail := tmpContractService.DetailList[j]
  164. //合同服务编号
  165. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  166. contractServiceDetail.ContractId = int(contractId)
  167. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  168. //合同服务详情入库
  169. contractServiceDetailId, detailErr := o.Insert(contractServiceDetail)
  170. if detailErr != nil {
  171. err = detailErr
  172. return
  173. }
  174. contractServiceDetail.Id = int(contractServiceDetailId)
  175. tmpContractService.DetailList[j] = contractServiceDetail
  176. }
  177. }
  178. newContract = contractInfo
  179. return
  180. }
  181. type ContractList struct {
  182. ContractId int `description:"合同唯一id"`
  183. ContractCode string `description:"合同编号,长度32位"`
  184. SellerId int `description:"所属销售id"`
  185. SellerName string `description:"所属销售名称"`
  186. ProductId int `description:"产品id,1:ficc;2:权益"`
  187. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  188. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  189. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"`
  190. StartDate time.Time `description:"合同开始日期"`
  191. EndDate time.Time `description:"合同结束日期"`
  192. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  193. Price float64 `description:"实际金额,优惠后的金额"`
  194. PayRemark string `description:"付款方式说明,长度255位"`
  195. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  196. UseCompanyName string `description:"使用方名称,长度32位"`
  197. CreditCode string `description:"社会统一信用代码,长度64位"`
  198. ProvinceId int `description:"省级id"`
  199. Province string `description:"省级名称,长度16位"`
  200. CityId int `description:"市级id"`
  201. City string `description:"市级名称,长度32位"`
  202. Address string `description:"详细地址"`
  203. Fax string `description:"传真,长度32位"`
  204. Phone string `description:"电话,长度32位"`
  205. Postcode string `description:"邮编,长度16位"`
  206. Remark string `json:"-" description:"补充内容,长度255位"`
  207. SellerRemark string `description:"销售备注,长度255位"`
  208. ApprovalRemark string `description:"审核备注"`
  209. ModifyContent string `description:"修改内容"`
  210. FileUrl string `description:"合同文件地址"`
  211. CheckBackFileUrl string `description:"签回合同文件地址"`
  212. TemplateId int `description:"模板id"`
  213. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  214. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  215. ModifyTime time.Time `description:"合同最近一次修改时间"`
  216. CreateTime time.Time `description:"合同添加时间"`
  217. ApproveTime time.Time `description:"审批时间"`
  218. InvalidTime time.Time `description:"作废时间"`
  219. CheckBackFileTime time.Time `description:"合同签回时间"`
  220. RescindTime time.Time `description:"解约时间"`
  221. StartDateStr string `description:"合同起始时间"`
  222. EndDateStr string `description:"合同结束时间"`
  223. ModifyTimeStr string `description:"最近一次更新时间"`
  224. CreateTimeStr string `description:"合同添加时间"`
  225. ApproveTimeStr string `description:"审批时间"`
  226. InvalidTimeStr string `description:"作废时间"`
  227. CheckBackFileTimeStr string `description:"合同签回时间"`
  228. RescindTimeStr string `description:"解约时间"`
  229. Service []*contract.ContractServiceAndDetail
  230. }
  231. //获取合同列表数据数量
  232. func GetContractListCount(condition string, pars []interface{}) (count int, err error) {
  233. o := orm.NewOrm()
  234. sql := "select count(*) AS COUNT from contract where 1=1 AND is_delete = 0 "
  235. sql += condition
  236. err = o.Raw(sql, pars).QueryRow(&count)
  237. return
  238. }
  239. //获取合同列表数据
  240. func GetContractList(condition string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) {
  241. o := orm.NewOrm()
  242. sql := "select * from contract where 1=1 AND is_delete = 0 "
  243. sql += condition
  244. sql += ` order by modify_time desc LIMIT ?,? `
  245. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  246. return
  247. }
  248. //修改合同
  249. func EditContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (err error) {
  250. o := orm.NewOrm()
  251. o.Begin()
  252. defer func() {
  253. if err != nil {
  254. o.Rollback()
  255. } else {
  256. o.Commit()
  257. }
  258. }()
  259. contractId := contractInfo.ContractId
  260. //合同数据入库
  261. _, err = o.Update(contractInfo)
  262. if err != nil {
  263. return
  264. }
  265. //删除合同的原始服务信息
  266. sql := `delete from contract_service where contract_id = ?`
  267. _, err = o.Raw(sql, contractId).Exec()
  268. //删除合同的原始服务详情信息
  269. sql = `delete from contract_service_detail where contract_id = ?`
  270. _, err = o.Raw(sql, contractId).Exec()
  271. for i := 0; i < len(contractServiceAndDetailList); i++ {
  272. //合同服务数据入库
  273. tmpContractService := contractServiceAndDetailList[i]
  274. contractService := &contract.ContractService{
  275. ContractId: int(contractId),
  276. Title: tmpContractService.Title,
  277. ProductId: contractInfo.ProductId,
  278. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  279. Value: tmpContractService.Value,
  280. HasDetail: tmpContractService.HasDetail,
  281. CreateTime: time.Now(),
  282. }
  283. contractServiceId, serviceErr := o.Insert(contractService)
  284. if serviceErr != nil {
  285. err = serviceErr
  286. return
  287. }
  288. contractService.ContractServiceId = int(contractServiceId)
  289. //合同服务详情入库
  290. for j := 0; j < len(tmpContractService.DetailList); j++ {
  291. contractServiceDetail := tmpContractService.DetailList[j]
  292. //合同服务编号
  293. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  294. contractServiceDetail.ContractId = contractService.ContractId
  295. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  296. //合同服务详情入库
  297. contractServiceDetailId, detailErr := o.Insert(contractServiceDetail)
  298. if detailErr != nil {
  299. err = detailErr
  300. return
  301. }
  302. contractServiceDetail.Id = int(contractServiceDetailId)
  303. tmpContractService.DetailList[j] = contractServiceDetail
  304. }
  305. }
  306. return
  307. }
  308. //添加生成后的合同地址
  309. func AddContractPdf(contractId int, pdfUrl string) (err error) {
  310. o := orm.NewOrm()
  311. //删除合同的原始服务信息
  312. sql := `update contract set file_url=? where contract_id = ?`
  313. _, err = o.Raw(sql, pdfUrl, contractId).Exec()
  314. return
  315. }
  316. //删除合同
  317. func DeleteContract(contractInfo *Contract) (err error) {
  318. o := orm.NewOrm()
  319. o.Begin()
  320. defer func() {
  321. if err != nil {
  322. o.Rollback()
  323. } else {
  324. o.Commit()
  325. }
  326. }()
  327. contractInfo.IsDelete = 1
  328. contractInfo.ModifyTime = time.Now()
  329. //合同数据入库
  330. _, err = o.Update(contractInfo)
  331. if err != nil {
  332. return
  333. }
  334. return
  335. }
  336. //作废合同
  337. func InvalidContract(contractInfo *Contract) (err error) {
  338. o := orm.NewOrm()
  339. o.Begin()
  340. defer func() {
  341. if err != nil {
  342. o.Rollback()
  343. } else {
  344. o.Commit()
  345. }
  346. }()
  347. contractInfo.Status = "已作废"
  348. contractInfo.ModifyTime = time.Now()
  349. contractInfo.InvalidTime = time.Now()
  350. //合同数据入库
  351. _, err = o.Update(contractInfo, "Status", "ModifyTime", "InvalidTime")
  352. if err != nil {
  353. return
  354. }
  355. return
  356. }
  357. //生成合同编号
  358. func GetCompanyContractCode(productId int) (companyCode string, err error) {
  359. var num int
  360. o := orm.NewOrm()
  361. today := utils.GetToday(utils.FormatDate)
  362. sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?`
  363. err = o.Raw(sql, today).QueryRow(&num)
  364. if err != nil {
  365. return
  366. }
  367. companyType := ""
  368. switch productId {
  369. case 1:
  370. companyType = "FICC"
  371. case 2:
  372. companyType = "EQ"
  373. }
  374. companyCode = "HZ" + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  375. return
  376. }
  377. type CompanyNameList struct {
  378. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  379. }
  380. //获取客户名称列表数据
  381. func GetCompanyNameList(sellerId int, keyword, status string) (list []*CompanyNameList, err error) {
  382. o := orm.NewOrm()
  383. sql := `select * from contract where is_delete=0 and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%') `
  384. if status != "" {
  385. sql += ` AND status='` + status + `' `
  386. }
  387. pars := make([]interface{}, 0)
  388. if sellerId > 0 {
  389. sql += ` and seller_id=? `
  390. pars = append(pars, sellerId)
  391. }
  392. sql += ` group by company_name order by modify_time desc `
  393. _, err = o.Raw(sql, pars).QueryRows(&list)
  394. return
  395. }