contract_approval.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. package contract
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/contract"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/services/flow"
  12. "hongze/hz_crm_api/utils"
  13. "math"
  14. "os"
  15. "strings"
  16. "time"
  17. )
  18. // Apply 提交审批
  19. func Apply(contractId int) (err error, errMsg string) {
  20. //获取合同详情
  21. contractInfo, err := GetContractDetail(contractId)
  22. if err != nil {
  23. err = errors.New("获取合同信息失败,Err:" + err.Error())
  24. return
  25. }
  26. if contractInfo == nil {
  27. err = errors.New("合同异常,没有合同信息")
  28. return
  29. }
  30. //如果是代付合同,那么需要去校验金额
  31. if contractInfo.ContractBusinessType == "代付合同" {
  32. tmpList, tmpErr := contract.GetContractRelationListByPaymentOnBehalfContractId(contractId)
  33. if tmpErr != nil {
  34. return
  35. }
  36. if len(tmpList) < 0 {
  37. err = errors.New("未关联业务合同")
  38. errMsg = "未关联业务合同"
  39. return
  40. }
  41. //查找业务合同信息
  42. businessContractInfo, tmpErr := contract.GetContractById(tmpList[0].ContractId)
  43. if tmpErr != nil {
  44. err = tmpErr
  45. return
  46. }
  47. ok, tmpErr := CheckContractPrice(businessContractInfo, contractInfo.Price)
  48. if tmpErr != nil {
  49. err = tmpErr
  50. return
  51. }
  52. if !ok {
  53. err = errors.New("合同金额异常,不能高于业务合同金额")
  54. errMsg = "合同金额异常,不能高于业务合同金额"
  55. return
  56. }
  57. }
  58. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  59. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  60. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  61. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  62. //合同状态判断
  63. ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
  64. if !strings.Contains(strings.Join(ignoreStatus, ","), contractInfo.Status) {
  65. err = errors.New("合同状态异常,不允许提交审批,当前合同状态:" + contractInfo.Status)
  66. return
  67. }
  68. //待审内容
  69. applyContent := "生成新模板"
  70. //if contractInfo.SourceId > 0 {
  71. // applyContent = "作废重生成"
  72. //}
  73. rejectCount, err := contract.GetRejectContractCountByContractId(contractInfo.ContractId, "contract")
  74. if err != nil {
  75. if err.Error() == utils.ErrNoRow() {
  76. err = errors.New(fmt.Sprint("根据合同编号:", contractInfo.ContractId, " 找不到驳回记录"))
  77. }
  78. return
  79. }
  80. if rejectCount > 0 {
  81. applyContent = "驳回重申请"
  82. }
  83. //获取最近的已审批或者已驳回的合同
  84. lastContractApproval, err := contract.GetLastContractApprovalByContractIdAndStatus(contractInfo.ContractId, "contract")
  85. if err != nil && err.Error() != utils.ErrNoRow() {
  86. return
  87. }
  88. //如果有申请记录,且申请单的状态是已审批,那么审核内容是:修改重审
  89. if lastContractApproval != nil && lastContractApproval.Status == "已审批" {
  90. applyContent = "修改重审"
  91. }
  92. //if contractInfo.Status == "已撤回" {
  93. // applyContent = "撤回重申请"
  94. //}
  95. contractDetailByte, err := json.Marshal(contractInfo)
  96. if err != nil {
  97. err = errors.New(fmt.Sprint("合同状态异常,当前合同数据格式化失败:", err))
  98. return
  99. }
  100. //获取基础审批流
  101. var flowItemInfo *system.ApprovalFlowItem
  102. if contractInfo.ProductId == 1 {
  103. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(3)
  104. if tmpErr != nil {
  105. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  106. return
  107. }
  108. flowItemInfo = tmpFlowItemInfo
  109. } else {
  110. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(4)
  111. if tmpErr != nil {
  112. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  113. return
  114. }
  115. flowItemInfo = tmpFlowItemInfo
  116. }
  117. //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
  118. contractApproval := &contract.ContractApproval{
  119. ContractId: contractInfo.ContractId,
  120. Status: "待审批",
  121. ApprovalType: "contract",
  122. ApplyContent: applyContent,
  123. ContractDetail: string(contractDetailByte),
  124. ApplyUserId: contractInfo.SellerId,
  125. ApplyUserName: contractInfo.SellerName,
  126. FlowId: flowItemInfo.FlowId,
  127. FlowVersion: flowItemInfo.CurrVersion,
  128. CurrNodeId: flowItemInfo.NodeList[0].NodeId,
  129. StartNodeId: flowItemInfo.NodeList[0].NodeId,
  130. ModifyTime: time.Now(),
  131. CreateTime: time.Now(),
  132. }
  133. has, err := contractApproval.CheckPendingByContractId(contractInfo.ContractId, "contract")
  134. if err != nil {
  135. return
  136. }
  137. if has {
  138. err = errors.New("合同异常,不允许提交审批,存在待审核的审批单")
  139. return
  140. }
  141. contractApprovalRecordList := make([]*contract.ContractApprovalRecord, 0)
  142. ApproveUserMap := make(map[int]system.User)
  143. for _, nodeItem := range flowItemInfo.NodeList {
  144. //审批流记录
  145. if len(nodeItem.UserList) <= 0 {
  146. err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
  147. return
  148. }
  149. for _, userItem := range nodeItem.UserList {
  150. contractApprovalRecord := &contract.ContractApprovalRecord{
  151. Status: "待审批",
  152. ApproveRemark: "",
  153. ApproveUserId: userItem.AdminId,
  154. ApproveUserName: userItem.Name,
  155. ApproveRoleTypeCode: userItem.RoleTypeCode,
  156. NodeId: nodeItem.NodeId,
  157. NodeType: nodeItem.NodeType,
  158. NextNodeId: nodeItem.NextNodeId,
  159. PrevNodeId: nodeItem.PrevNodeId,
  160. AuditType: nodeItem.AuditType,
  161. CreateTime: time.Now(),
  162. ModifyTime: time.Now(),
  163. }
  164. contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
  165. ApproveUserMap[userItem.AdminId] = userItem
  166. }
  167. }
  168. err = contractApproval.Apply2(contractApproval, contractApprovalRecordList)
  169. if err != nil {
  170. return
  171. }
  172. //添加操作日志
  173. remark := "提交审批"
  174. _ = contract.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecordList[0].ContractApprovalRecordId, "apply", contractInfo.SellerName, remark)
  175. //待办通知
  176. {
  177. //发送消息下级审批人
  178. go messageToNodeUser(contractApproval.CurrNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, applyContent)
  179. }
  180. return
  181. }
  182. // CancelApplyByContract 撤销合同
  183. func CancelApplyByContract(contractInfo *contract.Contract, opUserId int) (err error) {
  184. if contractInfo == nil {
  185. err = errors.New("审批流异常,没有审批流信息")
  186. return
  187. }
  188. //合同状态判断
  189. if contractInfo.Status != "待审批" {
  190. err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
  191. return
  192. }
  193. //校验操作人与合同申请人是否同一人
  194. if contractInfo.SellerId != opUserId {
  195. err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
  196. return
  197. }
  198. //获取最近一次待审批的审批单信息
  199. contractApproval, err := contract.GetLastPendingContractApprovalByContractId(contractInfo.ContractId, "contract")
  200. if err != nil {
  201. return
  202. }
  203. //是否一级审批节点判断,如果不是一级审批节点,那么不允许撤回
  204. if contractApproval.StartNodeId != contractApproval.CurrNodeId {
  205. err = errors.New("审批单状态异常,不允许撤回申请,当前审批单已被审批操作,进入了下一级审批节点")
  206. return
  207. }
  208. //获取当前待审批的审批流信息
  209. contractApprovalRecord, err := contract.GetCurrContractApprovalRecordByContractId(contractApproval.ContractApprovalId)
  210. if err != nil {
  211. return
  212. }
  213. //判断当前节点是否存在上级节点,如果存在,那么说明
  214. if contractApprovalRecord.PrevNodeId > 0 {
  215. err = errors.New("合同已存在审批操作,不允许撤回申请")
  216. return
  217. }
  218. //获取当前审批单中所有待审批的流程流
  219. contractApprovalRecordList, err := contract.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
  220. if err != nil {
  221. return
  222. }
  223. //撤回审批流
  224. err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
  225. if err != nil {
  226. return
  227. }
  228. //添加操作日志
  229. remark := "撤回申请"
  230. _ = contract.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
  231. //作废原有消息
  232. for _, contractApprovalRecord := range contractApprovalRecordList {
  233. go company.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
  234. }
  235. return
  236. }
  237. // CancelApply 撤回审批单
  238. func CancelApply(contractApprovalRecord *contract.ContractApprovalRecord, opUserId int) (err error) {
  239. if contractApprovalRecord == nil {
  240. err = errors.New("审批流异常,没有审批流信息")
  241. return
  242. }
  243. //审批流状态判断
  244. if contractApprovalRecord.Status != "待审批" {
  245. err = errors.New("审批流状态异常,不允许撤回申请,当前审批流状态:" + contractApprovalRecord.Status)
  246. return
  247. }
  248. //获取审批单详情
  249. contractApproval, err := contract.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
  250. if err != nil {
  251. return
  252. }
  253. //审批单状态判断
  254. if contractApproval.Status != "待审批" {
  255. err = errors.New("审批单状态异常,不允许撤回申请,当前审批单状态:" + contractApproval.Status)
  256. return
  257. }
  258. //是否一级审批节点判断,如果不是一级审批节点,那么不允许撤回
  259. if contractApproval.StartNodeId != contractApproval.CurrNodeId {
  260. err = errors.New("审批单状态异常,不允许撤回申请,当前审批单已被审批操作,进入了下一级审批节点")
  261. return
  262. }
  263. //获取合同信息
  264. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  265. if err != nil {
  266. return
  267. }
  268. //合同状态判断
  269. if contractInfo.Status != "待审批" {
  270. err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
  271. return
  272. }
  273. //校验操作人与合同申请人是否同一人
  274. if contractInfo.SellerId != opUserId {
  275. err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
  276. return
  277. }
  278. //获取当前审批单中所有待审批的流程流
  279. contractApprovalRecordList, err := contract.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
  280. if err != nil {
  281. return
  282. }
  283. err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
  284. if err != nil {
  285. return
  286. }
  287. //添加操作日志
  288. remark := "撤回申请"
  289. _ = contract.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
  290. //作废原有消息
  291. go company.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
  292. return
  293. }
  294. // Reject 驳回审批
  295. func Reject(contractApprovalRecord *contract.ContractApprovalRecord, opUser *system.Admin, rejectRemark string) (err error) {
  296. if contractApprovalRecord == nil {
  297. err = errors.New("审批流异常,没有审批流信息")
  298. return
  299. }
  300. //审批流状态判断
  301. if contractApprovalRecord.Status != "待审批" {
  302. err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + contractApprovalRecord.Status)
  303. return
  304. }
  305. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  306. if contractApprovalRecord.NodeType != "check" {
  307. err = errors.New("当前账号没有审批权限")
  308. return
  309. }
  310. //操作人
  311. opUserId := opUser.AdminId
  312. opUserName := opUser.RealName
  313. //操作权限校验
  314. //if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
  315. // err = errors.New("当前账号没有审批权限")
  316. // return
  317. //}
  318. if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
  319. err = errors.New("当前账号没有审批权限,需要指定人操作")
  320. return
  321. }
  322. //获取审批单详情
  323. contractApproval, err := contract.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
  324. if err != nil {
  325. return
  326. }
  327. //审批单状态判断
  328. if contractApproval.Status != "待审批" {
  329. err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + contractApproval.Status)
  330. return
  331. }
  332. //获取合同信息
  333. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  334. if err != nil {
  335. return
  336. }
  337. //合同状态判断
  338. if contractInfo.Status != "待审批" {
  339. err = errors.New("合同状态异常,不允许驳回申请,当前合同状态:" + contractInfo.Status)
  340. return
  341. }
  342. err = contractApproval.Reject(contractApproval, contractApprovalRecord, opUserId, opUserName, rejectRemark)
  343. if err != nil {
  344. return
  345. }
  346. //添加操作日志
  347. remark := "驳回申请"
  348. _ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "reject", opUserName, remark)
  349. //待办通知
  350. {
  351. content := contractInfo.CompanyName + " 合同模板已驳回"
  352. approvalSysUser, _ := system.GetSysAdminById(contractInfo.SellerId)
  353. go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 3, contractInfo.CompanyName, content, content, approvalSysUser.Mobile)
  354. }
  355. return
  356. }
  357. // Approved 审批通过
  358. func Approved(contractApprovalRecord *contract.ContractApprovalRecord, opUser *system.Admin, approvedRemark string) (err error) {
  359. if contractApprovalRecord == nil {
  360. err = errors.New("审批流异常,没有审批流信息")
  361. return
  362. }
  363. //审批流状态判断
  364. if contractApprovalRecord.Status != "待审批" {
  365. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  366. return
  367. }
  368. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  369. if contractApprovalRecord.NodeType != "check" {
  370. err = errors.New("当前账号没有审批权限")
  371. return
  372. }
  373. //操作人
  374. opUserId := opUser.AdminId
  375. opUserName := opUser.RealName
  376. //操作权限校验
  377. //if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
  378. // err = errors.New("当前账号没有审批权限")
  379. // return
  380. //}
  381. if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
  382. err = errors.New("当前账号没有审批权限,需要指定人操作")
  383. return
  384. }
  385. //获取审批单详情
  386. contractApproval, err := contract.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
  387. if err != nil {
  388. return
  389. }
  390. //审批单状态判断
  391. if contractApproval.Status != "待审批" {
  392. err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + contractApproval.Status)
  393. return
  394. }
  395. //获取合同信息
  396. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  397. if err != nil {
  398. return
  399. }
  400. //合同状态判断
  401. if contractInfo.Status != "待审批" {
  402. err = errors.New("合同状态异常,不允许审批,当前合同状态:" + contractInfo.Status)
  403. return
  404. }
  405. //if contractApprovalRecord
  406. err = contractApproval.Approved(contractApproval, contractApprovalRecord, approvedRemark)
  407. if err != nil {
  408. return
  409. }
  410. //添加操作日志
  411. remark := "审批通过"
  412. _ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
  413. //发送信息给所有抄送人
  414. content := contractInfo.CompanyName + " 合同模板已审核"
  415. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  416. if contractApprovalRecord.NextNodeId == 0 {
  417. //待办通知(通知销售已经审核通过了)
  418. {
  419. content := contractInfo.CompanyName + " 合同模板已审核"
  420. approvalSysUser, _ := system.GetSysAdminById(contractInfo.SellerId)
  421. go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile)
  422. }
  423. //审核通过后的异步操作(pdf生成、修改数据等)
  424. go AfterApproved(contractInfo.ContractId)
  425. } else {
  426. //获取下级节点信息
  427. flowNodeInfo, tmpErr := system.GetByNodeId(contractApprovalRecord.NextNodeId)
  428. if tmpErr != nil {
  429. err = tmpErr
  430. return
  431. }
  432. //如果该级节点是抄送类型,那么需要将该节点给处理掉
  433. if flowNodeInfo.NodeType == "cc" {
  434. go approvedByCc(approvedRemark, contractApprovalRecord)
  435. } else {
  436. //发送消息下级审批人
  437. go messageToNodeUser(contractApprovalRecord.NextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, content)
  438. }
  439. }
  440. return
  441. }
  442. // 审批通过(抄送节点)
  443. func approvedByCc(approvedRemark string, sourceContractApprovalRecord *contract.ContractApprovalRecord) (err error) {
  444. //下个流程节点id
  445. nextNodeId := 0
  446. //获取审批单中抄送节点的所有数据列表
  447. contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(sourceContractApprovalRecord.ContractApprovalId, sourceContractApprovalRecord.NextNodeId)
  448. if err != nil {
  449. return
  450. }
  451. //遍历所有的抄送单
  452. for _, contractApprovalRecord := range contractApprovalRecordList {
  453. nextNodeId = contractApprovalRecord.NextNodeId
  454. //审批流状态判断
  455. if contractApprovalRecord.Status != "待审批" {
  456. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  457. return
  458. }
  459. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  460. if contractApprovalRecord.NodeType != "cc" {
  461. err = errors.New("当前账号不是抄送权限")
  462. return
  463. }
  464. }
  465. //获取审批单详情
  466. contractApproval, err := contract.GetContractApprovalById(sourceContractApprovalRecord.ContractApprovalId)
  467. if err != nil {
  468. return
  469. }
  470. //获取合同信息
  471. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  472. if err != nil {
  473. return
  474. }
  475. //if contractApprovalRecord
  476. err = contractApproval.ApprovedByCc(contractApproval, contractApprovalRecordList, approvedRemark, nextNodeId)
  477. if err != nil {
  478. return
  479. }
  480. //发送消息给抄送人
  481. go messageToNodeUser(contractApprovalRecordList[0].NodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, contractApproval.ApplyContent)
  482. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  483. if nextNodeId == 0 {
  484. //待办通知
  485. {
  486. content := contractInfo.CompanyName + " 合同模板已审核"
  487. approvalSysUser, _ := system.GetSysAdminById(contractInfo.SellerId)
  488. go services.AddCompanyApprovalMessage(sourceContractApprovalRecord.ApproveUserId, contractInfo.SellerId, 0, sourceContractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile)
  489. }
  490. //审核通过后的异步操作(pdf生成、修改数据等)
  491. go AfterApproved(contractInfo.ContractId)
  492. } else {
  493. //获取下级节点信息
  494. flowNodeInfo, tmpErr := system.GetByNodeId(nextNodeId)
  495. if tmpErr != nil {
  496. err = tmpErr
  497. return
  498. }
  499. //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
  500. if flowNodeInfo.NodeType == "cc" {
  501. go approvedByCc(approvedRemark, sourceContractApprovalRecord)
  502. } else {
  503. //如果下级级节点是审批类型
  504. //发送消息下级审批人
  505. go messageToNodeUser(nextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, contractApproval.ApplyContent)
  506. }
  507. }
  508. return
  509. }
  510. // 抄送信息给用户(根据节点来推送)
  511. func messageToNodeUser(flowNodeId, createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  512. //获取审批单中抄送节点的所有数据列表
  513. contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
  514. if err != nil {
  515. return
  516. }
  517. err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
  518. return
  519. }
  520. // 发送信息给所有抄送人
  521. // @param createUserId 消息发起人
  522. // @param contractApprovalId 审批单id
  523. // @param messageType 消息类型:1:申请消息,2:审批结果
  524. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  525. // @param companyName 企业客户名称
  526. // @param content 消息内容
  527. func messageToAllCcUser(createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  528. //获取审批单中抄送节点的所有数据列表
  529. contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
  530. if err != nil {
  531. return
  532. }
  533. err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
  534. return
  535. }
  536. // 抄送信息给用户(发送给所有抄送人)
  537. // @param createUserId 待审批、抄送记录
  538. // @param createUserId 消息发起人
  539. // @param contractApprovalId 审批单id
  540. // @param messageType 消息类型:1:申请消息,2:审批结果
  541. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  542. // @param companyName 企业客户名称
  543. // @param content 消息内容
  544. func messageToUser(contractApprovalRecordList []*contract.ContractApprovalRecord, createUserId, messageType, approvalStatus int, companyName, content string) (err error) {
  545. for _, contractApprovalRecord := range contractApprovalRecordList {
  546. sysUserMobile := ""
  547. sysUser, _ := system.GetSysAdminById(contractApprovalRecord.ApproveUserId)
  548. if sysUser != nil {
  549. sysUserMobile = sysUser.Mobile
  550. }
  551. //接收人,就是审批记录里面的待审核人(抄送人)
  552. receiveUserId := contractApprovalRecord.ApproveUserId
  553. go services.AddCompanyApprovalMessage(createUserId, receiveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, messageType, 2, approvalStatus, companyName, content, content, sysUserMobile)
  554. }
  555. return
  556. }
  557. // AfterApproved 审批完成后操作
  558. func AfterApproved(contractId int) (err error) {
  559. defer func() {
  560. if err != nil {
  561. //fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  562. go alarm_msg.SendAlarmMsg("合同审批通过后,生成pdf失败,Err"+err.Error(), 3)
  563. utils.FileLog.Info(fmt.Sprintf("合同id:%d,合同审批通过后,生成pdf失败,Err"+err.Error(), contractId))
  564. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  565. }
  566. }()
  567. contractDetail, err := GetContractDetail(contractId)
  568. if err != nil {
  569. return
  570. }
  571. // CRM8.8-权益合同主客观合并
  572. if contractDetail.ProductId == 2 {
  573. mergeList := EquityMergeSubjectAndObject(contractDetail.Service)
  574. contractDetail.Service = mergeList
  575. }
  576. //fmt.Println("contractDetail:", contractDetail)
  577. //pdf生成并保存
  578. {
  579. ////获取合同的html模板信息
  580. //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractDetail, "pdf")
  581. //if tmpErr != nil {
  582. // err = tmpErr
  583. // return
  584. //}
  585. //
  586. ////生成pdf
  587. //pdfPath := fmt.Sprint("./static/word/系统生成合同_", contractId, ".pdf")
  588. //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
  589. //if tmpErr != nil {
  590. // err = tmpErr
  591. // return
  592. //}
  593. //生成word
  594. wordPath := fmt.Sprint("./static/word/系统生成合同_", contractDetail.ContractId, ".docx")
  595. tmpErr := services.GenerateWordV2(contractDetail, wordPath)
  596. if tmpErr != nil {
  597. err = tmpErr
  598. return
  599. }
  600. defer os.Remove(wordPath)
  601. ////word转pdf
  602. //converterType := "pdf"
  603. //pdfPath, tmpErr := services.FuncDocs2Pdf(utils.LibreOfficePath, wordPath, "./static/word", converterType)
  604. //if tmpErr != nil {
  605. // err = tmpErr
  606. // return
  607. //}
  608. //defer os.Remove(pdfPath)
  609. //
  610. ////randStr := utils.GetRandStringNoSpecialChar(28)
  611. ////fileName := contractDetail.ContractCode + ".pdf"
  612. //fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".pdf"
  613. ////非正式环境下,文件名上面还是加上随机数
  614. //if utils.RunMode != "release" {
  615. // //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  616. // fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  617. //}
  618. //randStr := utils.GetRandStringNoSpecialChar(28)
  619. //fileName := contractDetail.ContractCode + ".pdf"
  620. fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".docx"
  621. //非正式环境下,文件名上面还是加上随机数
  622. if utils.RunMode != "release" {
  623. fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".docx"
  624. }
  625. //上传到阿里云
  626. resourceUrl, tmpErr := services.UploadAliyunV2(fileName, wordPath)
  627. if tmpErr != nil {
  628. err = tmpErr
  629. return
  630. }
  631. tmpErr = contract.AddContractPdf(contractId, resourceUrl)
  632. if tmpErr != nil {
  633. err = tmpErr
  634. return
  635. }
  636. }
  637. //修改客户名称
  638. {
  639. //根据社会信用码获取客户信息
  640. companyInfo, tmpErr := company.GetCompanyByCreditCode(contractDetail.CreditCode)
  641. //如果查询异常,且并不是在系统中找不到该社会信用码,那么就异常返回
  642. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  643. err = tmpErr
  644. return
  645. }
  646. //如果客户信息不为空的情况下,那么去校验客户名称
  647. if companyInfo != nil {
  648. if companyInfo.CompanyName != contractDetail.CompanyName || companyInfo.Province != contractDetail.Province || companyInfo.City != contractDetail.City {
  649. companyInfo.CompanyName = contractDetail.CompanyName
  650. companyInfo.Province = contractDetail.Province
  651. companyInfo.City = contractDetail.City
  652. companyInfo.Address = contractDetail.Address
  653. tmpErr = company.ModifyCompany(companyInfo)
  654. if tmpErr != nil {
  655. err = tmpErr
  656. return
  657. }
  658. //新增客户日志
  659. {
  660. companyLog := new(company.CompanyLog)
  661. companyLog.CompanyId = companyInfo.CompanyId
  662. companyLog.CompanyName = companyInfo.CompanyName
  663. companyLog.CreditCode = companyInfo.CreditCode
  664. companyLog.Address = companyInfo.Address
  665. companyLog.City = companyInfo.City
  666. companyLog.Province = companyInfo.Province
  667. companyLog.LastUpdatedTime = companyInfo.LastUpdatedTime
  668. companyLog.AdminId = 0
  669. companyLog.LogType = "modify"
  670. companyLog.LogCreateTime = time.Now()
  671. go company.AddCompanyLog(companyLog)
  672. }
  673. }
  674. }
  675. }
  676. //修改业务合同的已支付金额
  677. if contractDetail.ContractBusinessType == "代付合同" {
  678. go updateContractPaidPriceByPaymentOnBehalfContractId(contractDetail.ContractId, contractDetail.Price)
  679. }
  680. return
  681. }
  682. // updateContractPaidPriceByPaymentOnBehalfContractId 根据代付合同id来修改业务合同的支付金额
  683. func updateContractPaidPriceByPaymentOnBehalfContractId(contractId int, price float64) (err error) {
  684. tmpList, tmpErr := contract.GetContractRelationListByPaymentOnBehalfContractId(contractId)
  685. if tmpErr != nil {
  686. return
  687. }
  688. if len(tmpList) < 0 {
  689. err = errors.New("未关联业务合同")
  690. return
  691. }
  692. updatePriceStr := ``
  693. if price > 0 {
  694. updatePriceStr = `+` + fmt.Sprint(price)
  695. } else {
  696. updatePriceStr = `-` + fmt.Sprint(math.Abs(price))
  697. }
  698. err = contract.UpdateBusinessContractPaidPrice(tmpList[0].ContractId, updatePriceStr)
  699. return
  700. }