settle.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 = pp.end_contract_qty;
  75. sp.pre_qc_qty = pp.end_qc_qty;
  76. sp.pre_qc_minus_qty = pp.pre_qc_minus_qty;
  77. sp.settle_status = pp.settle_status;
  78. sp.settle_done_order = pp.settle_done_order;
  79. }
  80. }
  81. const preBills = await this.ctx.service.settleBills.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
  82. for (const pb of preBills) {
  83. const sb = this.stageTree.nodes.find(x => { return x.id === pb.lid });
  84. if (sb) {
  85. sb.pre_settle = true;
  86. sb.pre_contract_qty = pb.pre_contract_qty;
  87. sb.pre_contract_tp = pb.pre_contract_tp;
  88. sb.pre_qc_qty = pb.pre_qc_qty;
  89. sb.pre_qc_tp = pb.pre_qc_tp;
  90. sb.pre_qc_minus_qty = pb.pre_qc_minus_qty;
  91. sb.settle_status = pb.settle_status;
  92. sb.settle_done_order = pb.settle_done_order;
  93. }
  94. }
  95. }
  96. calculateSettle() {
  97. const helper = this.ctx.helper;
  98. const settle = this.settle;
  99. this.stagePos.calculateAll(function(p) {
  100. if (p.is_settle || !p.pre_settle) {
  101. p.cur_contract_qty = p.settle_contract_qty;
  102. p.cur_qc_qty = p.settle_qc_qty;
  103. p.cur_qc_minus_qty = p.settle_qc_minus_qty;
  104. }
  105. if (p.is_settle || p.pre_settle) {
  106. p.end_contract_qty = helper.add(p.cur_contract_qty, p.pre_contract_qty);
  107. p.end_qc_qty = helper.add(p.cur_qc_qty, p.pre_qc_qty);
  108. p.end_qc_minus_qty = helper.add(p.cur_qc_minus_qty, p.pre_qc_minus_qty);
  109. }
  110. if (helper.checkNumEqual(p.end_contract_qty, p.quantity)) {
  111. p.settle_status = 2;
  112. p.settle_done_order = p.settle_done_order || settle.settle_order;
  113. }
  114. });
  115. const self = this, decimal = this.ctx.tender.info.decimal;
  116. this.stageTree.calculateAll(function(b) {
  117. if (b.children && b.children.length > 0) return;
  118. if (b.is_settle) {
  119. const posRange = self.stagePos.getLedgerPos(b.id);
  120. if (posRange && posRange.length > 0) {
  121. posRange.forEach(p => {
  122. b.cur_contract_qty = helper.add(b.cur_contract_qty, p.cur_contract_qty);
  123. b.cur_qc_qty = helper.add(b.cur_qc_qty, p.cur_qc_qty);
  124. b.cur_qc_minus_qty = helper.add(b.cur_qc_minus_qty, p.cur_qc_minus_qty);
  125. });
  126. } else {
  127. if (!b.pre_settle) {
  128. b.cur_contract_qty = b.settle_contract_qty;
  129. b.cur_qc_qty = b.settle_qc_qty;
  130. b.cur_qc_minus_qty = b.settle_qc_minus_qty;
  131. }
  132. }
  133. b.cur_contract_tp = helper.mul(b.unit_price, b.cur_contract_qty, decimal.tp);
  134. b.cur_qc_tp = helper.mul(b.unit_price, b.cur_qc_qty, decimal.tp);
  135. }
  136. if (b.is_settle || b.pre_settle) {
  137. b.end_contract_qty = helper.add(b.cur_contract_qty, b.pre_contract_qty);
  138. b.end_contract_tp = helper.add(b.cur_contract_tp, b.pre_contract_tp);
  139. b.end_qc_qty = helper.add(b.cur_qc_qty, b.pre_qc_qty);
  140. b.end_qc_tp = helper.add(b.cur_qc_tp, b.pre_qc_tp);
  141. b.end_qc_minus_qty = helper.add(b.cur_qc_minus_qty, b.pre_qc_minus_qty);
  142. }
  143. if (b.is_tp) {
  144. if (helper.checkNumEqual(b.end_contract_tp, b.total_price)) {
  145. b.settle_status = 2;
  146. b.settle_done_order = b.settle_done_order || settle.settle_order;
  147. } else {
  148. b.settle_status = 1;
  149. }
  150. } else {
  151. if (helper.checkNumEqual(b.end_contract_qty, b.quantity)) {
  152. b.settle_status = 2;
  153. b.settle_done_order = b.settle_done_order || settle.settle_order;
  154. } else {
  155. b.settle_status = 1;
  156. }
  157. }
  158. })
  159. }
  160. getSettleData() {
  161. const settleBills = [];
  162. for (const node of this.stageTree.nodes) {
  163. if (!node.is_settle && !node.pre_settle) continue;
  164. settleBills.push({
  165. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  166. lid: node.id, tree_id: node.ledger_id, tree_pid: node.ledger_pid, tree_full_path: node.full_path,
  167. tree_is_leaf: node.is_leaf, tree_level: node.level, tree_order: node.order,
  168. code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '',
  169. unit_price: node.unit_price || 0, quantity: node.quantity || 0, total_price: node.total_price || 0,
  170. drawing_code: node.drawing_code || '', memo: node.memo || '', node_type: node.node_type || 0,
  171. cur_contract_qty: node.cur_contract_qty || 0, cur_contract_tp: node.cur_contract_tp || 0,
  172. 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,
  173. pre_contract_qty: node.pre_contract_qty || 0, pre_contract_tp: node.pre_contract_tp || 0,
  174. 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,
  175. end_contract_qty: node.end_contract_qty || 0, end_contract_tp: node.end_contract_tp || 0,
  176. 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,
  177. is_settle: node.is_settle ? 1 : 0, pre_settle: node.pre_settle ? 1 : 0,
  178. settle_status: node.settle_status || 0, settle_done_order: node.settle_done_order,
  179. });
  180. }
  181. const settlePos = [];
  182. for (const pos of this.stagePos.datas) {
  183. if (!pos.is_settle && !pos.pre_settle) continue;
  184. settlePos.push({
  185. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  186. lid: pos.lid, pid: pos.id,
  187. name: pos.name || '', drawing_code: pos.drawing_code || '', position: pos.position || '',
  188. 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,
  189. 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,
  190. 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,
  191. is_settle: pos.is_settle ? 1 : 0, pre_settle: pos.pre_settle ? 1 : 0,
  192. settle_status: pos.settle_status || 0, settle_done_order: pos.settle_done_order,
  193. });
  194. }
  195. return [settleBills, settlePos];
  196. }
  197. async doSettle(settle) {
  198. this.settle = settle;
  199. await this._loadLatestStageData(settle);
  200. await this._loadSettleSelect();
  201. await this._loadPreSettle();
  202. this.calculateSettle();
  203. return this.getSettleData();
  204. }
  205. }
  206. module.exports = Settle;