settle.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('./ledger');
  10. class Settle {
  11. constructor (ctx) {
  12. this.ctx = ctx;
  13. }
  14. async _loadLatestStageData() {
  15. this.settle.latestStage = await this.ctx.service.stage.getLastestCompleteStage(this.settle.tid);
  16. this.ledgerColumn = [
  17. 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
  18. 'code', 'b_code', 'name', 'unit', 'unit_price',
  19. 'quantity', 'total_price', 'memo', 'drawing_code', 'node_type'];
  20. const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({ columns: this.ledgerColumn, where: { tender_id: this.settle.tid } });
  21. const endLedgerData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { sid: this.settle.latestStage.id } });
  22. this.ctx.helper.assignRelaData(ledgerData, [
  23. { data: endLedgerData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'settle_', relaId: 'lid' },
  24. ]);
  25. this.posColumn = ['id', 'tid', 'lid', 'name', 'position', 'porder', 'quantity', 'add_stage_order', 'drawing_code'];
  26. const posData = await this.ctx.service.pos.getAllDataByCondition({ columns: this.posColumn, where: { tid: this.settle.tid } });
  27. const endPosData = await this.ctx.service.stagePosFinal.getAllDataByCondition({ where: { sid: this.settle.latestStage.id } });
  28. this.ctx.helper.assignRelaData(posData, [
  29. { data: endPosData, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'settle_', relaId: 'pid' },
  30. ]);
  31. this.stageTree = new Ledger.billsTree(this.ctx, {
  32. id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1,
  33. calcFields: ['total_price', 'settle_gather_tp', 'settle_contract_tp', 'settle_qc_tp']
  34. });
  35. this.stageTree.loadDatas(ledgerData);
  36. this.stageTree.calculateAll();
  37. this.stagePos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
  38. this.stagePos.loadDatas(posData);
  39. }
  40. async _loadSettleSelect() {
  41. const select = await this.ctx.service.settleSelect.getAllDataByCondition({ where: { settle_id: this.settle.id } });
  42. for (const s of select) {
  43. if (s.pid) {
  44. const sp = this.stagePos.getPos(s.pid);
  45. // console.log(s.pid, sp, this.stagePos.items);
  46. if (!sp) continue;
  47. sp.is_settle = true;
  48. const sb = this.stageTree.nodes.find(x => { return x.id === sp.lid });
  49. sb.is_settle = true;
  50. const parents = this.stageTree.getAllParents(sb);
  51. parents.forEach(p => { p.is_settle = true; });
  52. }
  53. if (s.lid) {
  54. const sb = this.stageTree.nodes.find(x => { return x.id === s.lid });
  55. sb.is_settle = true;
  56. const parents = this.stageTree.getAllParents(sb);
  57. parents.forEach(p => { p.is_settle = true; });
  58. const posterity = this.stageTree.getPosterity(sb);
  59. for (const p of posterity) {
  60. p.is_settle = true;
  61. const pos = this.stagePos.getLedgerPos(p.id);
  62. if (pos && pos.length > 0) pos.forEach(x => { x.is_settle = true; });
  63. }
  64. }
  65. }
  66. }
  67. async _loadPreSettle() {
  68. if (this.settle.settle_order <= 1) return;
  69. const prePos = await this.ctx.service.settlePos.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
  70. for (const pp of prePos) {
  71. const sp = this.stagePos.getPos(pp.pid);
  72. if (sp) {
  73. sp.pre_settle = true;
  74. sp.pre_contract_qty = sp.end_contract_qty;
  75. sp.pre_qc_qty = sp.end_qc_qty;
  76. sp.pre_qc_minus_qty = sp.pre_qc_minus_qty;
  77. }
  78. }
  79. const preBills = await this.ctx.service.settleBills.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
  80. for (const pb of preBills) {
  81. const sb = this.stageTree.nodes.find(x => { return x.id === pb.lid });
  82. if (sb) {
  83. sb.pre_settle = true;
  84. sb.pre_contract_qty = pb.pre_contract_qty;
  85. sb.pre_contract_tp = pb.pre_contract_tp;
  86. sb.pre_qc_qty = pb.pre_qc_qty;
  87. sb.pre_qc_tp = pb.pre_qc_tp;
  88. sb.pre_qc_minus_qty = pb.pre_qc_minus_qty;
  89. }
  90. }
  91. }
  92. calculateSettle() {
  93. const helper = this.ctx.helper;
  94. this.stagePos.calculateAll(function(p) {
  95. if (p.is_settle || !p.pre_settle) {
  96. p.cur_contract_qty = p.settle_contract_qty;
  97. p.cur_qc_qty = p.settle_qc_qty;
  98. p.cur_qc_minus_qty = p.settle_qc_minus_qty;
  99. }
  100. if (p.is_settle || p.pre_settle) {
  101. p.end_contract_qty = helper.add(p.cur_contract_qty, p.pre_contract_qty);
  102. p.end_qc_qty = helper.add(p.cur_qc_qty, p.pre_qc_qty);
  103. p.end_qc_minus_qty = helper.add(p.cur_qc_minus_qty, p.pre_qc_minus_qty);
  104. }
  105. });
  106. const self = this, decimal = this.ctx.tender.info.decimal;
  107. this.stageTree.calculateAll(function(b) {
  108. if (b.children && b.children.length > 0) return;
  109. if (b.is_settle) {
  110. const posRange = self.stagePos.getLedgerPos(b.id);
  111. if (posRange && posRange.length > 0) {
  112. posRange.forEach(p => {
  113. b.cur_contract_qty = helper.add(b.cur_contract_qty, p.cur_contract_qty);
  114. b.cur_qc_qty = helper.add(b.cur_qc_qty, p.cur_qc_qty);
  115. b.cur_qc_minus_qty = helper.add(b.cur_qc_minus_qty, p.cur_qc_minus_qty);
  116. });
  117. } else {
  118. if (!b.pre_settle) {
  119. b.cur_contract_qty = b.settle_contract_qty;
  120. b.cur_qc_qty = b.settle_qc_qty;
  121. b.cur_qc_minus_qty = b.settle_qc_minus_qty;
  122. }
  123. }
  124. b.cur_contract_tp = helper.mul(b.unit_price, b.cur_contract_qty, decimal.tp);
  125. b.cur_qc_tp = helper.mul(b.unit_price, b.cur_qc_qty, decimal.tp);
  126. }
  127. if (b.is_settle || b.pre_settle) {
  128. b.end_contract_qty = helper.add(b.cur_contract_qty, b.pre_contract_qty);
  129. b.end_contract_tp = helper.add(b.cur_contract_tp, b.pre_contract_tp);
  130. b.end_qc_qty = helper.add(b.cur_qc_qty, b.pre_qc_qty);
  131. b.end_qc_tp = helper.add(b.cur_qc_tp, b.pre_qc_tp);
  132. b.end_qc_minus_qty = helper.add(b.cur_qc_minus_qty, b.pre_qc_minus_qty);
  133. }
  134. })
  135. }
  136. getSettleData() {
  137. const settleBills = [];
  138. for (const node of this.stageTree.nodes) {
  139. if (!node.is_settle && !node.pre_settle) continue;
  140. settleBills.push({
  141. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  142. lid: node.id, tree_id: node.ledger_id, tree_pid: node.ledger_pid, tree_full_path: node.full_path,
  143. tree_is_leaf: node.is_leaf, tree_level: node.level, tree_order: node.order,
  144. code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '',
  145. unit_price: node.unit_price || 0, quantity: node.quantity || 0, total_price: node.total_price || 0,
  146. drawing_code: node.drawing_code || '', memo: node.memo || '', node_type: node.node_type || 0,
  147. cur_contract_qty: node.cur_contract_qty || 0, cur_contract_tp: node.cur_contract_tp || 0,
  148. cur_qc_qty: node.cur_qc_qty || 0, cur_qc_tp: node.cur_qc_tp || 0, cur_qc_minus_qty: node.cur_qc_minus_qty || 0,
  149. pre_contract_qty: node.pre_contract_qty || 0, pre_contract_tp: node.pre_contract_tp || 0,
  150. pre_qc_qty: node.pre_qc_qty || 0, pre_qc_tp: node.pre_qc_tp || 0, pre_qc_minus_qty: node.pre_qc_minus_qty || 0,
  151. end_contract_qty: node.end_contract_qty || 0, end_contract_tp: node.end_contract_tp || 0,
  152. end_qc_qty: node.end_qc_qty || 0, end_qc_tp: node.end_qc_tp || 0, end_qc_minus_qty: node.end_qc_minus_qty || 0,
  153. is_settle: node.is_settle ? 1 : 0, pre_settle: node.pre_settle ? 1 : 0,
  154. });
  155. }
  156. const settlePos = [];
  157. for (const pos of this.stagePos.datas) {
  158. if (!pos.is_settle && !pos.pre_settle) continue;
  159. settlePos.push({
  160. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  161. lid: pos.lid, pid: pos.id,
  162. name: pos.name || '', drawing_code: pos.drawing_code || '', position: pos.position || '',
  163. cur_contract_qty: pos.cur_contract_qty || 0, cur_qc_qty: pos.cur_qc_qty || 0, cur_qc_minus_qty: pos.cur_qc_minus_qty || 0,
  164. pre_contract_qty: pos.pre_contract_qty || 0, pre_qc_qty: pos.pre_qc_qty || 0, pre_qc_minus_qty: pos.pre_qc_minus_qty || 0,
  165. end_contract_qty: pos.end_contract_qty || 0, end_qc_qty: pos.end_qc_qty || 0, end_qc_minus_qty: pos.end_qc_minus_qty || 0,
  166. is_settle: pos.is_settle ? 1 : 0, pre_settle: pos.pre_settle ? 1 : 0,
  167. });
  168. }
  169. return [settleBills, settlePos];
  170. }
  171. async doSettle(settle) {
  172. this.settle = settle;
  173. await this._loadLatestStageData(settle);
  174. await this._loadSettleSelect();
  175. await this._loadPreSettle();
  176. this.calculateSettle();
  177. return this.getSettleData();
  178. }
  179. }
  180. module.exports = Settle;