contract.go 17 KB

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