contract_pay.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. module.exports = app => {
  8. class ContractPay extends BaseService {
  9. /**
  10. * 构造函数
  11. *
  12. * @param {Object} ctx - egg全局变量
  13. * @return {void}
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'contract_pay';
  18. this.dataId = 'id';
  19. }
  20. async getPays(options, cid) {
  21. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND `cid` = ? ORDER BY `create_time` DESC';
  22. const sqlParams = [this.tableName, cid];
  23. const list = await this.db.query(sql, sqlParams);
  24. if (list.length > 0) {
  25. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: list.map(item => item.uid) } });
  26. for (const l of list) {
  27. const userInfo = userList.find(item => item.id === l.uid);
  28. l.username = userInfo ? userInfo.name : '';
  29. l.files = await this.ctx.service.contractPayAtt.getAtt(l.id);
  30. }
  31. }
  32. return list;
  33. }
  34. async add(options, cid, data) {
  35. const node = await this.ctx.service.contract.getDataById(cid);
  36. if (!node) {
  37. throw '合同不存在';
  38. }
  39. const transaction = await this.db.beginTransaction();
  40. try {
  41. const insertData = {
  42. spid: options.spid || null,
  43. tid: options.tid || null,
  44. contract_type: options.contract_type,
  45. cid,
  46. uid: this.ctx.session.sessionUser.accountId,
  47. pay_time: data.pay_time,
  48. pay_price: data.pay_price,
  49. debit_price: data.debit_price,
  50. yf_price: data.yf_price,
  51. sf_price: data.sf_price,
  52. pay_type: data.pay_type,
  53. remark: data.remark,
  54. create_time: new Date(),
  55. };
  56. await transaction.insert(this.tableName, insertData);
  57. await this.calcContract(transaction, node);
  58. await transaction.commit();
  59. } catch (err) {
  60. await transaction.rollback();
  61. throw err;
  62. }
  63. return { pays: await this.getPays(options, cid), node: { update: node } };
  64. }
  65. async save(options, cid, data) {
  66. if (!data.id) {
  67. throw '参数有误';
  68. }
  69. const node = await this.ctx.service.contract.getDataById(cid);
  70. if (!node) {
  71. throw '合同不存在';
  72. }
  73. const cpInfo = await this.getDataById(data.id);
  74. if (!cpInfo) {
  75. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  76. }
  77. const transaction = await this.db.beginTransaction();
  78. try {
  79. await transaction.update(this.tableName, data);
  80. await this.calcContract(transaction, node);
  81. await transaction.commit();
  82. } catch (err) {
  83. await transaction.rollback();
  84. throw err;
  85. }
  86. return { pays: await this.getPays(options, cid), node: { update: node } };
  87. }
  88. async del(options, cid, cpid) {
  89. if (!cpid) {
  90. throw '参数有误';
  91. }
  92. const node = await this.ctx.service.contract.getDataById(cid);
  93. if (!node) {
  94. throw '合同不存在';
  95. }
  96. const cpInfo = await this.getDataById(cpid);
  97. if (!cpInfo) {
  98. throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
  99. }
  100. if (cpInfo.fpcid) {
  101. throw '该合同' + contractConst.typeName[cpInfo.contract_type] + '关联了资金支付明细,不能删除';
  102. }
  103. const transaction = await this.db.beginTransaction();
  104. try {
  105. await transaction.delete(this.tableName, { id: cpid });
  106. // 删除合同附件
  107. const attList = await this.ctx.service.contractPayAtt.getAllDataByCondition({ where: { cpid } });
  108. await this.ctx.helper.delFiles(attList);
  109. await transaction.delete(this.ctx.service.contractPayAtt.tableName, { cpid });
  110. await this.calcContract(transaction, node);
  111. await transaction.commit();
  112. } catch (err) {
  113. await transaction.rollback();
  114. throw err;
  115. }
  116. return { pays: await this.getPays(options, cid), node: { update: node } };
  117. }
  118. async calcContract(transaction, node) {
  119. const paysList = await transaction.query('SELECT * FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
  120. let pay_price = 0;
  121. let debit_price = 0;
  122. let yf_price = 0;
  123. let sf_price = 0;
  124. for (const l of paysList) {
  125. pay_price = this.ctx.helper.add(pay_price, l.pay_price);
  126. debit_price = this.ctx.helper.add(debit_price, l.debit_price);
  127. yf_price = this.ctx.helper.add(yf_price, l.yf_price);
  128. sf_price = this.ctx.helper.add(sf_price, l.sf_price);
  129. }
  130. node.pay_price = pay_price;
  131. node.debit_price = debit_price;
  132. node.yf_price = yf_price;
  133. node.sf_price = sf_price;
  134. node.exist_pay = paysList.length === 0 ? 0 : 1;
  135. await transaction.update(this.ctx.service.contract.tableName, node);
  136. }
  137. async createContractPays(transaction, fpid, uid, times, pays) {
  138. const addPays = [];
  139. const contracts = await transaction.select(this.ctx.service.contract.tableName, { where: { id: this._.uniq(this._.map(pays, 'cid')) } });
  140. for (const p of pays) {
  141. const contract = contracts.find(c => c.id === p.cid);
  142. if (contract) {
  143. addPays.push({
  144. spid: contract.spid || null,
  145. tid: contract.tid || null,
  146. contract_type: contract.contract_type,
  147. cid: p.cid,
  148. uid,
  149. fpid,
  150. fpcid: p.id,
  151. pay_time: times,
  152. pay_price: p.pay_price || 0,
  153. debit_price: 0,
  154. yf_price: p.pay_price || 0,
  155. sf_price: p.settle_price || 0,
  156. pay_type: p.pay_type || '',
  157. remark: '',
  158. create_time: times,
  159. });
  160. }
  161. }
  162. if (addPays.length > 0) {
  163. await transaction.insert(this.tableName, addPays);
  164. for (const c of contracts) {
  165. await this.calcContract(transaction, c);
  166. }
  167. }
  168. }
  169. async removeContractPays(transaction, fpid, pays) {
  170. await transaction.delete(this.tableName, { fpid });
  171. const contracts = await transaction.select(this.ctx.service.contract.tableName, { where: { id: this._.uniq(this._.map(pays, 'cid')) } });
  172. if (contracts.length > 0) {
  173. for (const c of contracts) {
  174. await this.calcContract(transaction, c);
  175. }
  176. }
  177. }
  178. }
  179. return ContractPay;
  180. };