settle.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if (!sp) continue;
  46. sp.is_settle = true;
  47. sp.selected = 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. sb.selected = true;
  57. const parents = this.stageTree.getAllParents(sb);
  58. parents.forEach(p => { p.is_settle = true; });
  59. const posterity = this.stageTree.getPosterity(sb);
  60. for (const p of posterity) {
  61. p.is_settle = true;
  62. const pos = this.stagePos.getLedgerPos(p.id);
  63. if (pos && pos.length > 0) pos.forEach(x => { x.is_settle = true; });
  64. }
  65. }
  66. }
  67. }
  68. async _loadStageChange() {
  69. this.stageChange = await this.ctx.service.stageChangeFinal.getUnSettleChangeData(this.settle.latestStage);
  70. for (const change of this.stageChange) {
  71. const sb = this.stageTree.datas.find(x => { return x.id === change.gcl_id });
  72. if (sb) sb.undoneChange = true;
  73. if (change.mx_id) {
  74. const sp = this.stagePos.getPos(change.mx_id);
  75. if (sp) sp.undoneChange = true;
  76. }
  77. }
  78. }
  79. async _loadPreSettle() {
  80. if (this.settle.settle_order <= 1) return;
  81. const prePos = await this.ctx.service.settlePos.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
  82. for (const pp of prePos) {
  83. const sp = this.stagePos.getPos(pp.pid);
  84. if (sp) {
  85. sp.pre_settle = true;
  86. sp.pre_contract_qty = pp.end_contract_qty;
  87. sp.pre_qc_qty = pp.end_qc_qty;
  88. sp.pre_qc_minus_qty = pp.pre_qc_minus_qty;
  89. sp.settle_status = pp.settle_status;
  90. sp.settle_done_order = pp.settle_done_order;
  91. }
  92. }
  93. const preBills = await this.ctx.service.settleBills.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
  94. for (const pb of preBills) {
  95. const sb = this.stageTree.nodes.find(x => { return x.id === pb.lid });
  96. if (sb) {
  97. sb.pre_settle = true;
  98. sb.pre_contract_qty = pb.pre_contract_qty;
  99. sb.pre_contract_tp = pb.pre_contract_tp;
  100. sb.pre_qc_qty = pb.pre_qc_qty;
  101. sb.pre_qc_tp = pb.pre_qc_tp;
  102. sb.pre_qc_minus_qty = pb.pre_qc_minus_qty;
  103. sb.settle_status = pb.settle_status;
  104. sb.settle_done_order = pb.settle_done_order;
  105. }
  106. }
  107. }
  108. calculateSettle() {
  109. const helper = this.ctx.helper;
  110. const settle = this.settle;
  111. this.stagePos.calculateAll(function(p) {
  112. if (p.is_settle || !p.pre_settle) {
  113. p.cur_contract_qty = p.settle_contract_qty;
  114. p.cur_qc_qty = p.settle_qc_qty;
  115. p.cur_qc_minus_qty = p.settle_qc_minus_qty;
  116. }
  117. if (p.is_settle || p.pre_settle) {
  118. p.end_contract_qty = helper.add(p.cur_contract_qty, p.pre_contract_qty);
  119. p.end_qc_qty = helper.add(p.cur_qc_qty, p.pre_qc_qty);
  120. p.end_qc_minus_qty = helper.add(p.cur_qc_minus_qty, p.pre_qc_minus_qty);
  121. }
  122. if (helper.numEqual(p.end_contract_qty, p.quantity)) {
  123. p.settle_status = 2;
  124. p.settle_done_order = p.settle_done_order || settle.settle_order;
  125. }
  126. });
  127. const self = this, decimal = this.ctx.tender.info.decimal;
  128. this.stageTree.calculateAll(function(b) {
  129. if (b.children && b.children.length > 0) return;
  130. if (b.is_settle) {
  131. const posRange = self.stagePos.getLedgerPos(b.id);
  132. if (posRange && posRange.length > 0) {
  133. posRange.forEach(p => {
  134. b.cur_contract_qty = helper.add(b.cur_contract_qty, p.cur_contract_qty);
  135. b.cur_qc_qty = helper.add(b.cur_qc_qty, p.cur_qc_qty);
  136. b.cur_qc_minus_qty = helper.add(b.cur_qc_minus_qty, p.cur_qc_minus_qty);
  137. });
  138. } else {
  139. if (!b.pre_settle) {
  140. b.cur_contract_qty = b.settle_contract_qty;
  141. b.cur_qc_qty = b.settle_qc_qty;
  142. b.cur_qc_minus_qty = b.settle_qc_minus_qty;
  143. }
  144. }
  145. b.cur_contract_tp = helper.mul(b.unit_price, b.cur_contract_qty, decimal.tp);
  146. b.cur_qc_tp = helper.mul(b.unit_price, b.cur_qc_qty, decimal.tp);
  147. }
  148. if (b.is_settle || b.pre_settle) {
  149. b.end_contract_qty = helper.add(b.cur_contract_qty, b.pre_contract_qty);
  150. b.end_contract_tp = helper.add(b.cur_contract_tp, b.pre_contract_tp);
  151. b.end_qc_qty = helper.add(b.cur_qc_qty, b.pre_qc_qty);
  152. b.end_qc_tp = helper.add(b.cur_qc_tp, b.pre_qc_tp);
  153. b.end_qc_minus_qty = helper.add(b.cur_qc_minus_qty, b.pre_qc_minus_qty);
  154. }
  155. if (b.is_tp) {
  156. if (helper.numEqual(b.end_contract_tp, b.total_price)) {
  157. b.settle_status = 2;
  158. b.settle_done_order = b.settle_done_order || settle.settle_order;
  159. } else {
  160. b.settle_status = 1;
  161. }
  162. } else {
  163. if (helper.numEqual(b.end_contract_qty, b.quantity)) {
  164. b.settle_status = 2;
  165. b.settle_done_order = b.settle_done_order || settle.settle_order;
  166. } else {
  167. b.settle_status = 1;
  168. }
  169. }
  170. })
  171. }
  172. getSettleData() {
  173. const settleBills = [];
  174. const sum = {};
  175. for (const node of this.stageTree.nodes) {
  176. if (!node.is_settle && !node.pre_settle) continue;
  177. settleBills.push({
  178. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  179. lid: node.id, tree_id: node.ledger_id, tree_pid: node.ledger_pid, tree_full_path: node.full_path,
  180. tree_is_leaf: node.is_leaf, tree_level: node.level, tree_order: node.order,
  181. code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '',
  182. unit_price: node.unit_price || 0, quantity: node.quantity || 0, total_price: node.total_price || 0,
  183. drawing_code: node.drawing_code || '', memo: node.memo || '', node_type: node.node_type || 0,
  184. cur_contract_qty: node.cur_contract_qty || 0, cur_contract_tp: node.cur_contract_tp || 0,
  185. 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,
  186. pre_contract_qty: node.pre_contract_qty || 0, pre_contract_tp: node.pre_contract_tp || 0,
  187. 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,
  188. end_contract_qty: node.end_contract_qty || 0, end_contract_tp: node.end_contract_tp || 0,
  189. 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,
  190. is_settle: node.is_settle ? 1 : 0, pre_settle: node.pre_settle ? 1 : 0,
  191. settle_status: node.settle_status || 0, settle_done_order: node.settle_done_order || 0,
  192. });
  193. sum.contract_tp = this.ctx.helper.add(sum.contract_tp, node.cur_contract_tp);
  194. sum.qc_tp = this.ctx.helper.add(sum.qc_tp, node.cur_qc_tp);
  195. }
  196. sum.tp = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  197. const settlePos = [];
  198. for (const pos of this.stagePos.datas) {
  199. if (!pos.is_settle && !pos.pre_settle) continue;
  200. settlePos.push({
  201. tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
  202. lid: pos.lid, pid: pos.id,
  203. name: pos.name || '', drawing_code: pos.drawing_code || '', position: pos.position || '', porder: pos.porder || 1,
  204. 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,
  205. 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,
  206. 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,
  207. is_settle: pos.is_settle ? 1 : 0, pre_settle: pos.pre_settle ? 1 : 0,
  208. settle_status: pos.settle_status || 0, settle_done_order: pos.settle_done_order || 0,
  209. });
  210. }
  211. return [settleBills, settlePos, sum];
  212. }
  213. async doSettle(settle) {
  214. this.settle = settle;
  215. await this._loadLatestStageData(settle);
  216. await this._loadSettleSelect();
  217. await this._loadPreSettle();
  218. this.calculateSettle();
  219. return this.getSettleData();
  220. }
  221. _initPosUndone(pos) {
  222. pos.undoneDeal = pos.quantity ? !this.ctx.helper.numEqual(pos.settle_contract_qty, pos.quantity) : false;
  223. pos.undone = !!pos.undoneDeal || !!pos.undoneChange;
  224. }
  225. _initNodeUndone(node) {
  226. if (node.undoneDeal === undefined) node.undoneDeal = false;
  227. if (node.undoneChange === undefined) node.undoneChange = false;
  228. if (node.children && node.children.length > 0) {
  229. for (const child of node.children) {
  230. this._initNodeUndone(child);
  231. if (child.undoneDeal) node.undoneDeal = true;
  232. if (child.undoneChange) node.undoneChange = true;
  233. }
  234. node.undone = !!node.undoneDeal || !!node.undoneChange;
  235. } else {
  236. const posRange = this.stagePos.getLedgerPos(node.id);
  237. if (posRange && posRange.length > 0) {
  238. for (const pos of posRange) {
  239. this._initPosUndone(pos);
  240. if (pos.undoneDeal) node.undoneDeal = true;
  241. if (pos.undoneChange) node.undoneChange = true;
  242. }
  243. node.undone = !!node.undoneDeal || !!node.undoneChange;
  244. } else {
  245. node.undoneDeal = node.settle_contract_qty
  246. ? !this.ctx.helper.numEqual(node.settle_contract_qty, node.quantity)
  247. : !this.ctx.helper.numEqual(node.settle_contract_tp, node.total_price);
  248. node.undone = !!node.undoneDeal || !!node.undoneChange;
  249. }
  250. }
  251. }
  252. _checkNodeError(node) {
  253. const billsError = !!node.selected && node.undone;
  254. if (billsError) return billsError;
  255. const posRange = !node.children || node.children.length === 0 ? (this.stagePos.getLedgerPos(node.id) || []) : [];
  256. for (const pos of posRange) {
  257. if (!!pos.selected && pos.undone) return true;
  258. }
  259. return false;
  260. }
  261. checkAllSelect() {
  262. for (const node of this.stageTree.children) {
  263. this._initNodeUndone(node);
  264. }
  265. this.error = [];
  266. for (const node of this.stageTree.nodes) {
  267. if (this._checkNodeError(node)) this.error.push(node);
  268. }
  269. return this.error.length === 0;
  270. }
  271. async checkSettle(settle) {
  272. this.settle = settle;
  273. await this._loadLatestStageData(settle);
  274. await this._loadSettleSelect();
  275. await this._loadStageChange();
  276. return this.checkAllSelect();
  277. }
  278. }
  279. module.exports = Settle;