financial_pay_contract.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class FinancialPayContract extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'financial_pay_contract';
  17. this.dataId = 'id';
  18. }
  19. async getContractList(fpid, needCheck = false) {
  20. const list = await this.getAllDataByCondition({ where: { fpid }, orders: [['id', 'asc']] });
  21. const updateData = [];
  22. const needCheckKeys = ['c_code', 'name', 'total_price', 'entity', 'bank', 'bank_account'];
  23. for (const l of list) {
  24. const allList = l.cid ? await this.getAllDataByCondition({ where: { spid: l.spid, cid: l.cid } }) : [];
  25. l.accumulate_pay_price = this._.sumBy(allList, 'pay_price');
  26. l.accumulate_settle_price = this._.sumBy(allList, 'settle_price');
  27. l.files = await this.ctx.service.financialPayAtt.getAtt(l.fpid, l.id);
  28. if (l.cid && needCheck) {
  29. const contractInfo = await this.ctx.service.contract.getDataById(l.cid);
  30. if (contractInfo) {
  31. const condition = {};
  32. for (const key of needCheckKeys) {
  33. if (l[key] !== contractInfo[key]) {
  34. condition[key] = contractInfo[key];
  35. l[key] = contractInfo[key];
  36. }
  37. }
  38. if (Object.keys(condition).length > 0) {
  39. updateData.push({ id: l.id, ...condition });
  40. }
  41. }
  42. }
  43. }
  44. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  45. return list;
  46. }
  47. async addWhiteContract(fp) {
  48. const insertData = {
  49. spid: fp.spid,
  50. tid: fp.tid,
  51. fpid: fp.id,
  52. };
  53. const result = await this.db.insert(this.tableName, insertData);
  54. const result2 = await this.getDataById(result.insertId);
  55. result2.files = [];
  56. return result2;
  57. }
  58. async addContracts(fp, cids) {
  59. const transaction = await this.db.beginTransaction();
  60. try {
  61. const list = await this.getAllDataByCondition({ where: { fpid: fp.id, cid: cids } });
  62. if (list.length > 0) {
  63. throw '合同已存在, 无法重复添加';
  64. }
  65. const contracts = await this.ctx.service.contract.getAllDataByCondition({ where: { id: cids } });
  66. const insertDatas = [];
  67. for (const c of contracts) {
  68. insertDatas.push({
  69. spid: fp.spid,
  70. tid: fp.tid,
  71. fpid: fp.id,
  72. cid: c.id,
  73. c_code: c.c_code,
  74. name: c.name,
  75. total_price: c.total_price,
  76. entity: c.entity,
  77. bank: c.bank,
  78. bank_account: c.bank_account,
  79. });
  80. }
  81. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  82. // 重新算资金支付总额
  83. // const tp = await this.calcCamountSum(fpid, transaction);
  84. await transaction.commit();
  85. return { contractList: await this.getContractList(fp.id) };
  86. } catch (err) {
  87. await transaction.rollback();
  88. throw err;
  89. }
  90. }
  91. async delContract(fpid, ids) {
  92. const transaction = await this.db.beginTransaction();
  93. try {
  94. // 还要删除附件
  95. const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid, fpcid: ids } });
  96. await this.ctx.helper.delFiles(attList);
  97. await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpcid: ids });
  98. // 判断是否可删
  99. await transaction.delete(this.tableName, { id: ids });
  100. // 重新算资金支付总额
  101. const tp = await this.calcCamountSum(fpid, transaction);
  102. await transaction.commit();
  103. return { tp, contractList: await this.getContractList(fpid) };
  104. } catch (err) {
  105. await transaction.rollback();
  106. throw err;
  107. }
  108. }
  109. async updateContract(fpid, data) {
  110. const transaction = await this.db.beginTransaction();
  111. try {
  112. await transaction.update(this.tableName, data);
  113. const tp = await this.calcCamountSum(fpid, transaction);
  114. await transaction.commit();
  115. return { tp };
  116. } catch (err) {
  117. await transaction.rollback();
  118. throw err;
  119. }
  120. }
  121. /**
  122. * 修改变更清单 复制粘贴
  123. * @param {Object} datas 修改内容
  124. * @return {void}
  125. */
  126. async updateContracts(fpid, datas) {
  127. const transaction = await this.db.beginTransaction();
  128. try {
  129. const updateArray = [];
  130. for (const d of datas) {
  131. if (d.id) {
  132. updateArray.push(d);
  133. }
  134. }
  135. if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
  136. const tp = await this.calcCamountSum(fpid, transaction);
  137. await transaction.commit();
  138. return { tp, contractList: await this.getContractList(fpid) };
  139. } catch (err) {
  140. await transaction.rollback();
  141. throw err;
  142. }
  143. }
  144. async calcCamountSum(fpid, transaction) {
  145. // 防止小数位不精确,采用取值计算
  146. const sql = 'SELECT `small_expenses`, `pay_price` FROM ?? WHERE fpid = ?';
  147. const sqlParam = [this.tableName, fpid];
  148. const list = await transaction.query(sql, sqlParam);
  149. let total_price = 0;
  150. let small_expenses_tp = 0;
  151. // const tp_decimal = this.ctx.change.decimal.tp;
  152. for (const l of list) {
  153. total_price = this.ctx.helper.accAdd(total_price, l.pay_price || 0);
  154. small_expenses_tp = this.ctx.helper.accAdd(small_expenses_tp, l.small_expenses ? l.pay_price || 0 : 0);
  155. }
  156. const updateData = {
  157. id: fpid,
  158. total_price,
  159. small_expenses_tp,
  160. };
  161. await transaction.update(this.ctx.service.financialPay.tableName, updateData);
  162. return total_price;
  163. }
  164. async getEntities(fpid) {
  165. const entities = [];
  166. const contracts = await this.getAllDataByCondition({ columns: ['entity'], where: { fpid } });
  167. for (const c of contracts) {
  168. if (c.entity && !this._.includes(entities, c.entity)) {
  169. entities.push(c.entity);
  170. }
  171. }
  172. return entities.join(',');
  173. }
  174. }
  175. return FinancialPayContract;
  176. };