contract_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * @description: 合同数据相关操作
  3. * @Author: CP
  4. * @Date: 2020-10-27 11:28:24
  5. * @FilePath: \construction_management\services\contract_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "fmt"
  11. "html"
  12. "log"
  13. "strconv"
  14. "time"
  15. "github.com/kataras/iris/v12"
  16. "github.com/shopspring/decimal"
  17. "go.mod/comm"
  18. "go.mod/conf"
  19. "go.mod/dao"
  20. "go.mod/datasource"
  21. "go.mod/models"
  22. "go.mod/web/viewmodels"
  23. )
  24. // 定义项目用户Service接口
  25. type ContractService interface {
  26. ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  27. ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  28. ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  29. ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  30. ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  31. ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  32. ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  33. ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error)
  34. ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error)
  35. ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error)
  36. ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error)
  37. ValidRuleContractRetrunAdd(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  38. ValidRuleContractRetrun(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  39. ValidRuleContractRetrunDel(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  40. ValidRuleContractPaidAdd(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  41. ValidRuleContractPaid(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  42. ValidRuleContractPaidDel(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  43. // 项目节
  44. Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  45. GetSectionTreeContract(attribution string, bidsectionId int, projectId int, treeType int) []*viewmodels.Contracts
  46. GetSecionTree(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  47. SetSection(templateNumber int, bidsectionId int, projectIdInt int, treeType int) error
  48. SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  49. ContractSectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  50. SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  51. UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  52. SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error
  53. MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  54. MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  55. GetSecionTreeNotContract(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  56. GetContract(contractId int) *viewmodels.Contracts
  57. Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  58. Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  59. Delete(projectId int, bidsectionId int, treeId int, id int) error
  60. Close(projectId int, bidsectionId int, treeId int, id int, treeType int, settlementCode string) error
  61. Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error
  62. GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{}
  63. ReturnCreate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  64. ReturnUpdate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, id int) error
  65. ReturnAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsReturn
  66. ReturnDelete(projectId int, bidsectionId int, contractsId int, id int) error
  67. //支出合同
  68. AddExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  69. UpdateExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  70. DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error
  71. // 已支付
  72. PaidAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsPaid
  73. PaidCreate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  74. PaidUpdate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, id int) error
  75. PaidDelete(projectId int, bidsectionId int, contractsId int, id int) error
  76. }
  77. // 返回service操作类
  78. type contractService struct {
  79. treeContractDao *dao.TreeContractDao
  80. contractDao *dao.ContractDao
  81. contractReturnDao *dao.ContractReturnDao
  82. contractPaidDao *dao.ContractPaidDao
  83. treeDao *dao.TreeDao
  84. annexDao *dao.AnnexDao
  85. }
  86. // 创建项目用户service
  87. func NewContractService() ContractService {
  88. return &contractService{
  89. treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()),
  90. contractDao: dao.NewContractDao(datasource.InstanceDbMaster()),
  91. contractReturnDao: dao.NewContractReturnDao(datasource.InstanceDbMaster()),
  92. contractPaidDao: dao.NewContractPaidDao(datasource.InstanceDbMaster()),
  93. treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  94. annexDao: dao.NewAnnexDao(datasource.InstanceDbMaster()),
  95. }
  96. }
  97. // 升级降级规则验证
  98. func (s *contractService) ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  99. treeSectionVaild := &viewmodels.TreeSectionContract{}
  100. err := ctx.ReadJSON(treeSectionVaild)
  101. if err != nil {
  102. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  103. return treeSectionVaild, err
  104. }
  105. err = treeSectionVaild.ValidateDepth()
  106. if err != nil {
  107. log.Println("参数验证错误, error=", err)
  108. return treeSectionVaild, err
  109. }
  110. return treeSectionVaild, nil
  111. }
  112. // 模板规则验证
  113. func (s *contractService) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  114. treeSectionVaild := &viewmodels.TreeSectionContract{}
  115. err := ctx.ReadJSON(treeSectionVaild)
  116. if err != nil {
  117. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  118. return treeSectionVaild, err
  119. }
  120. err = treeSectionVaild.ValidateTemplate()
  121. if err != nil {
  122. log.Println("参数验证错误, error=", err)
  123. return treeSectionVaild, err
  124. }
  125. return treeSectionVaild, nil
  126. }
  127. // 模板规则新增项目节
  128. func (s *contractService) ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  129. treeSectionVaild := &viewmodels.TreeSectionContract{}
  130. err := ctx.ReadJSON(treeSectionVaild)
  131. if err != nil {
  132. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  133. return treeSectionVaild, err
  134. }
  135. err = treeSectionVaild.ValidateSectionAdd()
  136. if err != nil {
  137. log.Println("参数验证错误, error=", err)
  138. return treeSectionVaild, err
  139. }
  140. return treeSectionVaild, nil
  141. }
  142. // 模板规则新增项目节
  143. func (s *contractService) ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  144. treeSectionVaild := &viewmodels.TreeSectionContract{}
  145. err := ctx.ReadForm(treeSectionVaild)
  146. if err != nil {
  147. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  148. return treeSectionVaild, err
  149. }
  150. err = treeSectionVaild.ValidateSectionDelete()
  151. if err != nil {
  152. log.Println("参数验证错误, error=", err)
  153. return treeSectionVaild, err
  154. }
  155. return treeSectionVaild, nil
  156. }
  157. func (s *contractService) ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  158. treeSectionVaild := &viewmodels.TreeSectionContract{}
  159. err := ctx.ReadForm(treeSectionVaild)
  160. if err != nil {
  161. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  162. return treeSectionVaild, err
  163. }
  164. err = treeSectionVaild.ValidateSectionNot()
  165. if err != nil {
  166. log.Println("参数验证错误, error=", err)
  167. return treeSectionVaild, err
  168. }
  169. return treeSectionVaild, nil
  170. }
  171. func (s *contractService) ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  172. treeSectionVaild := &viewmodels.TreeSectionContract{}
  173. err := ctx.ReadForm(treeSectionVaild)
  174. if err != nil {
  175. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  176. return treeSectionVaild, err
  177. }
  178. err = treeSectionVaild.ValidateSectionDelete()
  179. if err != nil {
  180. log.Println("参数验证错误, error=", err)
  181. return treeSectionVaild, err
  182. }
  183. return treeSectionVaild, nil
  184. }
  185. // 验证序号相关
  186. func (s *contractService) ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  187. treeSectionVaild := &viewmodels.TreeSectionContract{}
  188. err := ctx.ReadJSON(treeSectionVaild)
  189. if err != nil {
  190. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  191. return treeSectionVaild, err
  192. }
  193. err = treeSectionVaild.ValidateSectionSerial()
  194. if err != nil {
  195. log.Println("参数验证错误, error=", err)
  196. return treeSectionVaild, err
  197. }
  198. return treeSectionVaild, nil
  199. }
  200. // 新增合同参数验证
  201. func (s *contractService) ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error) {
  202. // 创建一个存放前端传过来参数
  203. contractsVaild := &viewmodels.Contracts{}
  204. // 存放raw的值,放入到contractsVaild
  205. err := ctx.ReadJSON(contractsVaild)
  206. if err != nil {
  207. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  208. return contractsVaild, err
  209. }
  210. // 验证合同传参
  211. err = contractsVaild.ValidateAdd()
  212. if err != nil {
  213. log.Println("参数验证错误, error=", err)
  214. return contractsVaild, err
  215. }
  216. // xss
  217. contractsVaild.Code = html.EscapeString(contractsVaild.Code)
  218. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  219. // contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  220. return contractsVaild, nil
  221. }
  222. // 校验编辑接口
  223. func (s *contractService) ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error) {
  224. // 创建一个存放前端传过来参数
  225. contractsVaild := &viewmodels.Contracts{}
  226. // 存放raw的值,放入到contractsVaild
  227. err := ctx.ReadJSON(contractsVaild)
  228. if err != nil {
  229. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  230. return contractsVaild, err
  231. }
  232. // 验证合同传参
  233. err = contractsVaild.ValidateEdi()
  234. if err != nil {
  235. log.Println("参数验证错误, error=", err)
  236. return contractsVaild, err
  237. }
  238. contractsVaild.Content = html.EscapeString(contractsVaild.Content)
  239. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  240. // contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  241. contractsVaild.PartyA = html.EscapeString(contractsVaild.PartyA)
  242. contractsVaild.PartyASigner = html.EscapeString(contractsVaild.PartyASigner)
  243. contractsVaild.PartyB = html.EscapeString(contractsVaild.PartyB)
  244. contractsVaild.PartyBSigner = html.EscapeString(contractsVaild.PartyBSigner)
  245. return contractsVaild, nil
  246. }
  247. // 校验删除合同参数
  248. func (s *contractService) ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error) {
  249. // 创建一个存放前端传过来参数
  250. contractsVaild := &viewmodels.Contracts{}
  251. // 存放raw的值,放入到contractsVaild
  252. err := ctx.ReadForm(contractsVaild)
  253. if err != nil {
  254. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  255. return contractsVaild, err
  256. }
  257. // 验证合同传参
  258. err = contractsVaild.ValidateDel()
  259. if err != nil {
  260. log.Println("参数验证错误, error=", err)
  261. return contractsVaild, err
  262. }
  263. return contractsVaild, nil
  264. }
  265. // 校验关闭合同参数
  266. func (s *contractService) ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error) {
  267. // 创建一个存放前端传过来参数
  268. contractsVaild := &viewmodels.Contracts{}
  269. // 存放raw的值,放入到contractsVaild
  270. err := ctx.ReadJSON(contractsVaild)
  271. if err != nil {
  272. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  273. return contractsVaild, err
  274. }
  275. // 验证合同传参
  276. err = contractsVaild.ValidateDel()
  277. if err != nil {
  278. log.Println("参数验证错误, error=", err)
  279. return contractsVaild, err
  280. }
  281. return contractsVaild, nil
  282. }
  283. // ------------------------------------------------------------
  284. // 获得项目节
  285. func (s *contractService) Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract {
  286. // 1.获得项目节
  287. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  288. // 2.构造数据
  289. sectionVM := s.makeSectionView(section)
  290. // 3.更新 上移和下一的限制
  291. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  292. if len(youngerBrotherList) == 0 {
  293. sectionVM.IsEnd = true
  294. }
  295. sectionVM.ElderBrother = true
  296. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  297. if len(elderBrotherList) == 0 {
  298. sectionVM.ElderBrother = false
  299. }
  300. return sectionVM
  301. }
  302. // 获得合同详情
  303. func (s *contractService) GetContract(contractId int) *viewmodels.Contracts {
  304. contract := s.contractDao.Get(contractId)
  305. contractsVM := s.makeContractVM(contract)
  306. return &contractsVM
  307. }
  308. // 新增合同
  309. func (s *contractService) Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  310. // 1. 项目节存在
  311. contracts := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  312. if contracts.Id == 0 {
  313. return errors.New("未找到项目节")
  314. }
  315. // 2.项目节是没有合同
  316. if contracts.ContractId != 0 {
  317. return errors.New("该项目节上已经存在合同")
  318. }
  319. // 2-1.查找合同编号是否存在
  320. codeData := s.contractDao.GetByCode(projectId, bidsectionId, contractData.Code, 1)
  321. if len(codeData) != 0 {
  322. return errors.New("该合同编号已经存在")
  323. }
  324. // 3.新增合同 --合计标段上的金额
  325. contractsCm := &models.CmContracts{}
  326. contractsCm.Code = contractData.Code
  327. contractsCm.Name = contractData.Name
  328. contractsCm.ContractsType = 1
  329. contractsCm.Price = fmt.Sprintf("%.2f", contractData.Price)
  330. // contractsCm.Price = contractData.Price
  331. contractsCm.Returned = "0"
  332. contractsCm.Paid = "0"
  333. contractsCm.TreeId = treeId
  334. contractsCm.ProjectId = projectId
  335. contractsCm.BidsectionId = bidsectionId
  336. contractsCm.Status = 0
  337. contractsCm.CreateTime = time.Now()
  338. contractsCm.UpdateTime = time.Now()
  339. err := s.contractDao.Add(contractsCm)
  340. if err != nil {
  341. return err
  342. }
  343. // 3.获得该标段下合同总数 - 总收入金额
  344. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  345. // 更新标段目录上合同金额和总数
  346. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  347. if err != nil {
  348. return err
  349. }
  350. return nil
  351. }
  352. // 更新合同
  353. func (s *contractService) Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  354. // 1. 项目节存在
  355. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  356. if contractsTree.Id == 0 {
  357. return errors.New("未找到项目节")
  358. }
  359. // 2.项目节是没有合同
  360. if contractsTree.ContractId == 0 {
  361. return errors.New("该项目节上没有找到合同")
  362. }
  363. // 3.合同锁定 不能删除
  364. if contractsTree.ContractLocking == 1 {
  365. return errors.New("该合同已锁定")
  366. }
  367. // 4.合同金额不能小于回款金额
  368. returnedPrice, _ := decimal.NewFromString(contractsTree.ContractReturned)
  369. price := decimal.NewFromFloat(contractData.Price)
  370. // price, err := decimal.NewFromString(contractData.Price)
  371. // if err != nil {
  372. // return errors.New("金额填写有误")
  373. // }
  374. if returnedPrice.GreaterThan(price) {
  375. return errors.New(fmt.Sprintf("合同金额不能低于%s", contractsTree.ContractReturned))
  376. }
  377. contractsCm := &models.CmContracts{}
  378. contractsCm.Id = contractsTree.ContractId
  379. contractsCm.Content = contractData.Content
  380. contractsCm.Name = contractData.Name
  381. contractsCm.Price = fmt.Sprintf("%.2f", contractData.Price)
  382. // strconv.FormatFloat(contractData.Price, 'f', 1, 64)
  383. // contractsCm.Price = contractData.Price
  384. contractsCm.PartyA = contractData.PartyA
  385. contractsCm.PartyASigner = contractData.PartyASigner
  386. contractsCm.PartyB = contractData.PartyB
  387. contractsCm.PartyBSigner = contractData.PartyBSigner
  388. loc, _ := time.LoadLocation("Local")
  389. SignerTime, err := time.ParseInLocation(conf.SysTimeform, contractData.SignerTime, loc)
  390. if err != nil {
  391. return errors.New("签约时间填写异常")
  392. }
  393. contractsCm.SignerTime = SignerTime
  394. contractsCm.Remarks = contractData.Remarks
  395. columns := []string{"Content", "Name", "Price", "PartyA", "PartyASigner", "PartyB", "PartyBSigner"}
  396. err = s.contractDao.Update(contractsCm, columns, projectId, bidsectionId, treeId)
  397. if err != nil {
  398. return err
  399. }
  400. // 3.获得该标段下合同总数 - 总收入金额
  401. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  402. // 更新标段目录上合同金额和总数
  403. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  404. if err != nil {
  405. return err
  406. }
  407. return nil
  408. }
  409. // 删除合同
  410. func (s *contractService) Delete(projectId int, bidsectionId int, treeId int, id int) error {
  411. // 1. 项目节存在
  412. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  413. if contractsTree.Id == 0 {
  414. return errors.New("未找到项目节")
  415. }
  416. // 2.项目节是没有合同
  417. if contractsTree.ContractId == 0 {
  418. return errors.New("该项目节上没有找到合同")
  419. }
  420. // 3.合同锁定 不能删除
  421. if contractsTree.ContractLocking == 1 {
  422. return errors.New("该合同已锁定")
  423. }
  424. // 删除合同
  425. err := s.contractDao.Delete(projectId, bidsectionId, treeId, id)
  426. if err != nil {
  427. return err
  428. }
  429. // 3.获得该标段下合同总数 - 总收入金额
  430. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  431. // 更新标段目录上合同金额和总数
  432. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  433. if err != nil {
  434. return err
  435. }
  436. // 4.更新回款总金额
  437. err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, id)
  438. if err != nil {
  439. return err
  440. }
  441. return nil
  442. }
  443. // 关闭合同
  444. func (s *contractService) Close(projectId int, bidsectionId int, treeId int, id int, treeType int, settlementCode string) error {
  445. // 1. 项目节存在
  446. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  447. if contractsTree.Id == 0 {
  448. return errors.New("未找到项目节")
  449. }
  450. // 2.项目节是没有合同
  451. if contractsTree.ContractId == 0 {
  452. return errors.New("该项目节上没有找到合同")
  453. }
  454. // 3.合同锁定 不能删除
  455. if contractsTree.ContractLocking == 1 {
  456. return errors.New("该合同已锁定")
  457. }
  458. // 关闭合同
  459. err := s.contractDao.Close(projectId, bidsectionId, treeId, id, settlementCode)
  460. if err != nil {
  461. return err
  462. }
  463. return nil
  464. }
  465. // 解锁合同
  466. func (s *contractService) Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error {
  467. // 1. 项目节存在
  468. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  469. if contractsTree.Id == 0 {
  470. return errors.New("未找到项目节")
  471. }
  472. // 2.项目节是没有合同
  473. if contractsTree.ContractId == 0 {
  474. return errors.New("该项目节上没有找到合同")
  475. }
  476. // 解锁合同
  477. err := s.contractDao.Unlock(projectId, bidsectionId, treeId, id)
  478. if err != nil {
  479. return err
  480. }
  481. return nil
  482. }
  483. // 获得合同收入概况
  484. func (s *contractService) GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{} {
  485. // 1.获得收入合同
  486. year := time.Now().Year()
  487. incomeList := s.contractDao.GetTypeYear(bidsectionId, projectId, contractsType, year)
  488. // 2.初始化
  489. totalContractPrice := 0.00
  490. totalTypePrice := 0.00
  491. performNumber := 0
  492. closeNumber := 0
  493. uncloseNumber := 0
  494. // 3.当年数据初始化
  495. returnDate := map[string]float64{
  496. fmt.Sprintf("%d-01", year): 0.00,
  497. fmt.Sprintf("%d-02", year): 0.00,
  498. fmt.Sprintf("%d-03", year): 0.00,
  499. fmt.Sprintf("%d-04", year): 0.00,
  500. fmt.Sprintf("%d-05", year): 0.00,
  501. fmt.Sprintf("%d-06", year): 0.00,
  502. fmt.Sprintf("%d-07", year): 0.00,
  503. fmt.Sprintf("%d-08", year): 0.00,
  504. fmt.Sprintf("%d-09", year): 0.00,
  505. fmt.Sprintf("%d-10", year): 0.00,
  506. fmt.Sprintf("%d-11", year): 0.00,
  507. fmt.Sprintf("%d-12", year): 0.00,
  508. }
  509. contractIds := []int{}
  510. for _, item := range incomeList {
  511. contractPrice, _ := strconv.ParseFloat(item.Price, 64)
  512. totalContractPrice = totalContractPrice + contractPrice
  513. // 回款金额或已支付金额合计
  514. contractIds = append(contractIds, item.Id)
  515. // typePrice := 0.00
  516. // if contractsType == 1 {
  517. // typePrice, _ = strconv.ParseFloat(item.Returned, 64)
  518. // totalTypePrice = totalTypePrice + typePrice
  519. // } else {
  520. // typePrice, _ = strconv.ParseFloat(item.Paid, 64)
  521. // totalTypePrice = totalTypePrice + typePrice
  522. // }
  523. if item.Status == 0 {
  524. performNumber = performNumber + 1
  525. } else if item.Status == 1 {
  526. uncloseNumber = uncloseNumber + 1
  527. } else if item.Status == 2 {
  528. closeNumber = closeNumber + 1
  529. }
  530. // returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] = returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] + typePrice
  531. }
  532. fmt.Println(contractIds)
  533. // contractInId := strings.Join(contractIds, ",")
  534. if contractsType == 1 {
  535. list := s.contractReturnDao.GetInContractsIds(contractIds)
  536. for _, item := range list {
  537. typePrice := 0.00
  538. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  539. totalTypePrice = totalTypePrice + typePrice
  540. fmt.Println(item)
  541. // :=decimal.NewFromFloat(typePrice)
  542. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  543. }
  544. } else {
  545. list := s.contractPaidDao.GetInContractsIds(contractIds)
  546. for _, item := range list {
  547. typePrice := 0.00
  548. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  549. totalTypePrice = totalTypePrice + typePrice
  550. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  551. }
  552. }
  553. totalContractPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalContractPrice), 64)
  554. totalTypePrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalTypePrice), 64)
  555. // ac := accounting.Accounting{Symbol: "", Precision: 2}
  556. surveryData := map[string]interface{}{
  557. "totalContractPrice": totalContractPrice,
  558. "totalContractPriceShow": totalContractPrice,
  559. // "totalReturnPrice": ac.FormatMoney(totalReturnPrice),
  560. "performNumber": performNumber,
  561. "closeNumber": closeNumber,
  562. "uncloseNumber": uncloseNumber,
  563. "returnDate": returnDate,
  564. }
  565. if contractsType == 1 {
  566. surveryData["totalReturnPriceShow"] = totalTypePrice
  567. } else {
  568. surveryData["totalPaidPriceShow"] = totalTypePrice
  569. }
  570. return surveryData
  571. }
  572. // 获得合同总数量和总金额
  573. func (s *contractService) getContractTotalAndPrice(bidsectionId int, projectId int, treeType int) (contractTotal int, priceTotal float64) {
  574. contractListAll := s.treeContractDao.GetContractAll(bidsectionId, projectId)
  575. // 获得收入合同
  576. contractList := s.treeContractDao.GetContract(bidsectionId, projectId, treeType)
  577. priceTotal = 0.00
  578. for _, item := range contractList {
  579. contractPrice, _ := strconv.ParseFloat(item.ContractPrice, 64)
  580. priceTotal = priceTotal + contractPrice
  581. }
  582. // 合同总数
  583. // contractTotal = len(contractList) + 1
  584. contractTotal = len(contractListAll)
  585. // 合同收入总金额
  586. // price, _ := strconv.ParseFloat(priceString, 64)
  587. // priceTotal = priceTotal + price
  588. // 保留2位小数
  589. priceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", priceTotal), 64)
  590. return contractTotal, priceTotal
  591. }
  592. // 构建合同详情视图
  593. func (s *contractService) makeContractVM(contract *models.CmContracts) viewmodels.Contracts {
  594. contractsVM := viewmodels.Contracts{}
  595. if contract.Id == 0 {
  596. return contractsVM
  597. }
  598. id, _ := comm.AesEncrypt(strconv.Itoa(contract.Id), conf.SignSecret)
  599. treeId, _ := comm.AesEncrypt(strconv.Itoa(contract.TreeId), conf.SignSecret)
  600. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(contract.BidsectionId), conf.SignSecret)
  601. contractsVM.Id = id
  602. contractsVM.TreeId = treeId
  603. contractsVM.ContractsType = contract.ContractsType
  604. contractsVM.BidsectionId = bidsectionId
  605. contractsVM.Name = contract.Name
  606. contractsVM.Content = contract.Content
  607. contractsVM.Code = contract.Code
  608. contractsVM.PartyA = contract.PartyA
  609. contractsVM.PartyASigner = contract.PartyASigner
  610. contractsVM.PartyB = contract.PartyB
  611. contractsVM.PartyBSigner = contract.PartyBSigner
  612. contractsVM.Remarks = contract.Remarks
  613. // price, _ := strconv.ParseFloat(contract.Price, 64)
  614. // returned, _ := strconv.ParseFloat(contract.Returned, 64)
  615. // paid, _ := strconv.ParseFloat(contract.Paid, 64)
  616. price, _ := strconv.ParseFloat(contract.Price, 64)
  617. // contractsVM.PriceFloat = price
  618. contractsVM.Price = price
  619. contractsVM.Returned = contract.Returned
  620. contractsVM.Paid = contract.Paid
  621. contractsVM.Status = contract.Status
  622. contractsVM.Locking = contract.Locking
  623. contractsVM.ContractDeductionTotal = contract.ContractDeductionTotal
  624. contractsVM.CreateTime = contract.CreateTime.Format(conf.SysTimeform)
  625. contractsVM.UpdateTime = contract.UpdateTime.Format(conf.SysTimeform)
  626. // nilTime := time.Time{}
  627. // contract.SignerTime != nilTime
  628. if !contract.SignerTime.IsZero() {
  629. contractsVM.SignerTime = contract.SignerTime.Format(conf.SysTimeform)
  630. }
  631. return contractsVM
  632. }