contract_pay.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const contractConst = require('../const/contract');
  7. const auditConst = require('../const/audit').contract;
  8. const auditType = require('../const/audit').auditType;
  9. module.exports = app => {
  10. class ContractPay extends BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'contract_pay';
  20. this.dataId = 'id';
  21. }
  22. async getPays(options, cid) {
  23. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND `cid` = ? ORDER BY `create_time` DESC';
  24. const sqlParams = [this.tableName, cid];
  25. const list = await this.db.query(sql, sqlParams);
  26. const pay_shenpi_status = this.ctx.contract_tender ? this.ctx.subProject.page_show.openContractPayTenderShenpi : this.ctx.subProject.page_show.openContractPaySubProjectShenpi;
  27. if (list.length > 0) {
  28. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: list.map(item => item.uid) } });
  29. for (const l of list) {
  30. const userInfo = userList.find(item => item.id === l.uid);
  31. l.username = userInfo ? userInfo.name : '';
  32. l.files = await this.ctx.service.contractPayAtt.getAtt(l.id);
  33. if (pay_shenpi_status) {
  34. await this.ctx.service.contractSpAudit.loadUser(l);
  35. await this.ctx.service.contractSpAudit.loadAuditViewData(l);
  36. await this.ctx.service.contractSpAudit.checkShenpi(l);
  37. if (l.status !== auditConst.status.checked || !l.final_auditor_str) {
  38. l.curAuditors2 = await this.ctx.service.contractSpAudit.getAuditorsByStatus(l.cid, l.id, l.status, l.times);
  39. if (l.status === auditConst.status.checked && !l.final_auditor_str) {
  40. const final_auditor_str = l.curAuditors2[0].audit_type === auditType.key.common
  41. ? l.curAuditors2[0].name + (l.curAuditors2[0].role ? '-' + l.curAuditors2[0].role : '')
  42. : this.ctx.helper.transFormToChinese(l.curAuditors2[0].audit_order) + '审';
  43. await this.defaultUpdate({ final_auditor_str, id: l.id });
  44. l.final_auditor_str = final_auditor_str;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. return list;
  51. }
  52. async add(options, cid, data) {
  53. const node = await this.ctx.service.contract.getDataById(cid);
  54. if (!node) {
  55. throw '合同不存在';
  56. }
  57. const transaction = await this.db.beginTransaction();
  58. try {
  59. const insertData = {
  60. spid: options.spid || null,
  61. tid: options.tid || null,
  62. contract_type: options.contract_type,
  63. cid,
  64. uid: this.ctx.session.sessionUser.accountId,
  65. pay_time: data.pay_time,
  66. used: data.used || '合同',
  67. pay_price: data.pay_price,
  68. debit_price: data.debit_price,
  69. yf_price: data.yf_price,
  70. sf_price: data.sf_price,
  71. pay_type: data.pay_type,
  72. remark: data.remark,
  73. create_time: new Date(),
  74. need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractPaySubProjectShenpi : this.ctx.subProject.page_show.openContractPayTenderShenpi,
  75. };
  76. await transaction.insert(this.tableName, insertData);
  77. await this.calcContract(transaction, node);
  78. await transaction.commit();
  79. } catch (err) {
  80. await transaction.rollback();
  81. throw err;
  82. }
  83. return { pays: await this.getPays(options, cid), node: { update: node } };
  84. }
  85. async save(options, cid, data) {
  86. if (!data.id) {
  87. throw '参数有误';
  88. }
  89. const node = await this.ctx.service.contract.getDataById(cid);
  90. if (!node) {
  91. throw '合同不存在';
  92. }
  93. const cpInfo = await this.getDataById(data.id);
  94. if (!cpInfo) {
  95. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  96. }
  97. const transaction = await this.db.beginTransaction();
  98. try {
  99. await transaction.update(this.tableName, data);
  100. await this.calcContract(transaction, node);
  101. await transaction.commit();
  102. } catch (err) {
  103. await transaction.rollback();
  104. throw err;
  105. }
  106. return { pays: await this.getPays(options, cid), node: { update: node } };
  107. }
  108. async del(options, cid, cpid) {
  109. if (!cpid) {
  110. throw '参数有误';
  111. }
  112. const node = await this.ctx.service.contract.getDataById(cid);
  113. if (!node) {
  114. throw '合同不存在';
  115. }
  116. const cpInfo = await this.getDataById(cpid);
  117. if (!cpInfo) {
  118. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  119. }
  120. if (cpInfo.fpcid) {
  121. throw '该合同' + contractConst.typeName[cpInfo.contract_type] + '关联了资金支付明细,不能删除';
  122. }
  123. const transaction = await this.db.beginTransaction();
  124. try {
  125. await transaction.delete(this.tableName, { id: cpid });
  126. // 删除合同附件
  127. const attList = await this.ctx.service.contractPayAtt.getAllDataByCondition({ where: { cpid } });
  128. await this.ctx.helper.delFiles(attList);
  129. await transaction.delete(this.ctx.service.contractPayAtt.tableName, { cpid });
  130. await transaction.delete(this.ctx.service.contractSpAudit.tableName, { cpid });
  131. await this.calcContract(transaction, node);
  132. await transaction.commit();
  133. } catch (err) {
  134. await transaction.rollback();
  135. throw err;
  136. }
  137. return { pays: await this.getPays(options, cid), node: { update: node } };
  138. }
  139. async calcContract(transaction, node) {
  140. const paysList = await transaction.query('SELECT * FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
  141. let pay_price = 0;
  142. let debit_price = 0;
  143. let yf_price = 0;
  144. let sf_price = 0;
  145. for (const l of paysList) {
  146. pay_price = this.ctx.helper.add(pay_price, l.pay_price);
  147. debit_price = this.ctx.helper.add(debit_price, l.debit_price);
  148. yf_price = this.ctx.helper.add(yf_price, l.yf_price);
  149. sf_price = this.ctx.helper.add(sf_price, l.sf_price);
  150. }
  151. node.pay_price = pay_price;
  152. node.debit_price = debit_price;
  153. node.yf_price = yf_price;
  154. node.sf_price = sf_price;
  155. node.exist_pay = paysList.length === 0 ? 0 : 1;
  156. await transaction.update(this.ctx.service.contract.tableName, node);
  157. }
  158. async createContractPays(transaction, fpid, uid, times, pays) {
  159. const addPays = [];
  160. const contracts = await transaction.select(this.ctx.service.contract.tableName, { where: { id: this._.uniq(this._.map(pays, 'cid')) } });
  161. for (const p of pays) {
  162. const contract = contracts.find(c => c.id === p.cid);
  163. if (contract) {
  164. addPays.push({
  165. spid: contract.spid || null,
  166. tid: contract.tid || null,
  167. contract_type: contract.contract_type,
  168. cid: p.cid,
  169. uid,
  170. fpid,
  171. fpcid: p.id,
  172. pay_time: times,
  173. used: p.used || '合同',
  174. pay_price: p.pay_price || 0,
  175. debit_price: 0,
  176. yf_price: p.pay_price || 0,
  177. sf_price: p.settle_price || 0,
  178. pay_type: p.pay_type || '',
  179. remark: '',
  180. create_time: times,
  181. });
  182. }
  183. }
  184. if (addPays.length > 0) {
  185. await transaction.insert(this.tableName, addPays);
  186. for (const c of contracts) {
  187. await this.calcContract(transaction, c);
  188. }
  189. const commonJson = this.ctx.subProject.common_json ? JSON.parse(this.ctx.subProject.common_json) : {};
  190. const used = commonJson && commonJson.tender_contract_used ? commonJson.tender_contract_used : [];
  191. const addUsed = this._.uniq(this._.map(pays, 'used'));
  192. // 还要判断里面有叫"合同"的值,如果有则去除
  193. const index = addUsed.indexOf('合同');
  194. if (index > -1) {
  195. addUsed.splice(index, 1);
  196. }
  197. // 判断addUsed和used对比是否有新增的值,如果有则更新分项目的common_json
  198. if (this._.difference(addUsed, used).length > 0) {
  199. const newUsed = this._.uniq([...used, ...addUsed]);
  200. commonJson.tender_contract_used = newUsed;
  201. await transaction.update(this.ctx.service.subProject.tableName, { id: this.ctx.subProject.id, common_json: JSON.stringify(commonJson) });
  202. }
  203. }
  204. }
  205. async removeContractPays(transaction, fpid, pays) {
  206. await transaction.delete(this.tableName, { fpid });
  207. const contracts = await transaction.select(this.ctx.service.contract.tableName, { where: { id: this._.uniq(this._.map(pays, 'cid')) } });
  208. if (contracts.length > 0) {
  209. for (const c of contracts) {
  210. await this.calcContract(transaction, c);
  211. }
  212. }
  213. }
  214. }
  215. return ContractPay;
  216. };