sum_load.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. class loadGclBaseTree {
  11. /**
  12. * 构造函数
  13. * @param {Array} tempData - 清单模板数据
  14. */
  15. constructor (ctx, setting) {
  16. this.ctx = ctx;
  17. this.parent = setting.parent;
  18. this.parent.children = [];
  19. this.defaultData = setting.defaultData;
  20. // 常量
  21. this.splitChar = '-';
  22. // 索引
  23. // 仅通过addNode或addNodeWithoutParent添加节点
  24. this.items = [];
  25. // this.parent下原来的节点
  26. this.baseNodes = [];
  27. this.ignoreParent = setting.ignoreParent;
  28. // 缓存
  29. this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1;
  30. }
  31. /**
  32. * 根据 编号 查找 父项项目节
  33. * @param {String} code - 子项编号
  34. * @returns {*}
  35. */
  36. findNode(node, parent, check) {
  37. parent = parent || this.parent;
  38. if (!parent.children) return null;
  39. for (const child of parent.children) {
  40. const checkLeaf = (child.is_leaf && node.is_leaf) || (!child.is_leaf && !node.is_leaf);
  41. if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit
  42. && checkLeaf && !child.hasPos && (!check || check(child, node))) {
  43. return child;
  44. }
  45. }
  46. return null;
  47. }
  48. /**
  49. * 添加 树节点 并完善该节点的树结构
  50. * @param {Object} node - 添加节点
  51. * @param {Object} parent - 父项
  52. * @returns {*}
  53. */
  54. addNode(source, parent, check) {
  55. parent = parent ? parent : this.parent;
  56. let node = this.findNode(source, parent, check);
  57. if (!node) {
  58. if (!parent.children) parent.children = [];
  59. node = {
  60. id: this.ctx.app.uuid.v4(),
  61. tender_id: this.ctx.tender.id,
  62. ledger_id: this.keyNodeId,
  63. ledger_pid: parent.ledger_id,
  64. level: parent.level +1,
  65. full_path: parent.full_path + '-' + this.keyNodeId,
  66. order: parent.children.length + 1,
  67. children: [],
  68. b_code: source.b_code,
  69. name: source.name,
  70. unit: source.unit,
  71. deal_qty: 0,
  72. sgfh_qty: 0,
  73. qtcl_qty: 0,
  74. sjcl_qyt: 0,
  75. quantity: 0,
  76. is_leaf: source.is_leaf,
  77. hasPos: false,
  78. };
  79. this.keyNodeId += 1;
  80. parent.children.push(node);
  81. this.items.push(node);
  82. }
  83. return node;
  84. }
  85. findLeaf(node, check) {
  86. for (const bn of this.baseNodes) {
  87. if (bn.b_code === node.b_code && bn.name === node.name && bn.unit === node.unit
  88. && bn.is_leaf && !bn.hasPos && (!check || check(bn, node))) return bn;
  89. }
  90. for (const i of this.items) {
  91. if (i.b_code === node.b_code && i.name === node.name && i.unit === node.unit
  92. && i.is_leaf && !i.hasPos && (!check || check(i, node))) return i;
  93. }
  94. return null;
  95. }
  96. addNodeWithoutParent(source, check) {
  97. let node = this.findLeaf(source, check);
  98. if (!node) {
  99. node = {
  100. id: this.ctx.app.uuid.v4(),
  101. tender_id: this.ctx.tender.id,
  102. ledger_id: this.keyNodeId,
  103. ledger_pid: this.parent.ledger_id,
  104. level: this.parent.level + 1,
  105. full_path: this.parent.full_path + '-' + this.keyNodeId,
  106. order: this.parent.children.length + 1,
  107. b_code: source.b_code,
  108. name: source.name,
  109. unit: source.unit,
  110. deal_qty: 0,
  111. sgfh_qty: 0,
  112. qtcl_qty: 0,
  113. sjcl_qty: 0,
  114. quantity: 0,
  115. is_leaf: source.is_leaf,
  116. hasPos: false,
  117. };
  118. this.keyNodeId += 1;
  119. this.parent.children.push(node);
  120. this.items.push(node);
  121. }
  122. return node;
  123. }
  124. gather(source, parent) {}
  125. getUpdateData() {}
  126. recursiveCalculate(dealBills, node) {
  127. if (node.children && node.children.length > 0) {
  128. for (const child of node.children) {
  129. this.recursiveCalculate(dealBills, child);
  130. }
  131. } else {
  132. const p = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  133. node.deal_qty = this.ctx.helper.round(node.deal_qty, p.value);
  134. node.sgfh_qty = this.ctx.helper.round(node.sgfh_qty, p.value);
  135. node.sjcl_qty = this.ctx.helper.round(node.sjcl_qty, p.value);
  136. node.qtcl_qty = this.ctx.helper.round(node.qtcl_qty, p.value);
  137. node.quantity = this.ctx.helper.round(node.quantity, p.value);
  138. if (!node.unit_price) {
  139. const db = dealBills.find(x => { return x.code === node.b_code && x.name === node.name && x.unit === node.unit });
  140. if (!db) return;
  141. node.unit_price = db.unit_price;
  142. }
  143. node.deal_tp = this.ctx.helper.mul(node.deal_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  144. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  145. node.sjcl_tp = this.ctx.helper.mul(node.sjcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  146. node.qtcl_tp = this.ctx.helper.mul(node.qtcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  147. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  148. }
  149. }
  150. calculateAll(dealBills) {
  151. this.recursiveCalculate(dealBills, this.parent);
  152. }
  153. }
  154. class loadLedgerGclTree extends loadGclBaseTree {
  155. gather(source, parent) {
  156. const node = this.addNode(source, parent);
  157. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  158. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  159. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  160. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  161. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  162. return node;
  163. }
  164. getUpdateData() {
  165. const update = [];
  166. if (this.items.length > 0) update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  167. const create = [];
  168. for (const i of this.items) {
  169. create.push({
  170. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  171. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  172. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  173. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  174. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  175. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  176. });
  177. }
  178. return {update, create};
  179. }
  180. }
  181. class updateReviseGclTree extends loadGclBaseTree {
  182. constructor (ctx, setting) {
  183. super(ctx, setting);
  184. this.errors = [];
  185. }
  186. loadBase(datas, pos) {
  187. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  188. const Index = {};
  189. for (const d of datas) {
  190. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  191. if (!parent) continue;
  192. if (!parent.children) parent.children = [];
  193. const relaPos = pos.filter(x => {return x.lid === d.id});
  194. const baseNode = {
  195. id: d.id,
  196. ledger_id: d.ledger_id,
  197. ledger_pid: d.ledger_pid,
  198. level: d.level,
  199. is_leaf: d.is_leaf,
  200. full_path: d.full_path,
  201. b_code: d.b_code,
  202. name: d.name,
  203. unit: d.unit,
  204. unit_price: d.unit_price,
  205. org_deal_qty: d.deal_qty || 0,
  206. org_sgfh_qty: d.sgfh_qty || 0,
  207. org_sjcl_qty: d.sjcl_qty || 0,
  208. org_qtcl_qty: d.qtcl_qty || 0,
  209. org_qty: d.quantity || 0,
  210. org_order: d.order,
  211. sgfh_qty: 0,
  212. sjcl_qty: 0,
  213. qtcl_qty: 0,
  214. quantity: 0,
  215. hasPos: relaPos.length > 0,
  216. };
  217. parent.children.push(baseNode);
  218. Index[baseNode.ledger_id] = baseNode;
  219. this.baseNodes.push(baseNode);
  220. }
  221. }
  222. gather(source, parent) {
  223. const node = this.addNode(source, parent);
  224. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  225. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  226. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  227. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  228. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  229. return node;
  230. }
  231. getUpdateData() {
  232. const result = {update: [], errors: [], create: []};
  233. if (this.baseNodes.length === 0) {
  234. if (this.items.length > 0) result.update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  235. } else {
  236. for (const bn of this.baseNodes) {
  237. if (bn.children && bn.children.length > 0) continue;
  238. if (bn.deal_qty < bn.org_deal_qty || bn.sjcl_qty < bn.org_sjcl_qty || bn.qtcl_qty < bn.org_qtcl_qty || bn.sgfh_qty < bn.org_sgfh_qty) {
  239. result.errors.push({
  240. ledger_id: bn.ledger_id,
  241. b_code: bn.b_code, name: bn.name, unit: bn.unit,
  242. deal_qty: bn.deal_qty, sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, qty: bn.quantity, type: 'less',
  243. });
  244. } else if (bn.sjcl_qty !== bn.org_sjcl_qty || bn.qtcl_qty !== bn.org_qtcl_qty || bn.sgfh_qty !== bn.org_sgfh_qty) {
  245. result.update.push({
  246. id: bn.id, ledger_id: bn.ledger_id, unit_price: bn.unit_price || 0,
  247. deal_qty: bn.deal_qty, deal_tp: bn.deal_tp || 0,
  248. sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, quantity: bn.quantity,
  249. sgfh_tp: bn.sgfh_tp || 0, sjcl_tp: bn.sjcl_tp || 0, qtcl_tp: bn.qtcl_tp || 0, total_price: bn.total_price || 0,
  250. })
  251. }
  252. }
  253. }
  254. for (const i of this.items) {
  255. result.create.push({
  256. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  257. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  258. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  259. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  260. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  261. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  262. })
  263. }
  264. return result;
  265. }
  266. }
  267. class gatherStageGclTree extends loadGclBaseTree {
  268. loadBase(datas, pos) {
  269. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  270. const Index = {};
  271. for (const d of datas) {
  272. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  273. if (!parent) continue;
  274. if (!parent.children) parent.children = [];
  275. const relaPos = pos.filter(x => {return x.lid === d.id});
  276. const baseNode = {
  277. id: d.id,
  278. ledger_id: d.ledger_id,
  279. ledger_pid: d.ledger_pid,
  280. level: d.level,
  281. is_leaf: d.is_leaf,
  282. full_path: d.full_path,
  283. b_code: d.b_code,
  284. name: d.name,
  285. unit: d.unit,
  286. unit_price: d.unit_price,
  287. org_contract_qty: d.contract_qty || 0,
  288. org_contract_tp: d.contract_tp || 0,
  289. org_order: d.order,
  290. contract_qty: 0,
  291. contract_tp: 0,
  292. is_tp: d.is_tp,
  293. hasPos: relaPos.length > 0,
  294. };
  295. parent.children.push(baseNode);
  296. Index[baseNode.ledger_id] = baseNode;
  297. this.baseNodes.push(baseNode);
  298. }
  299. }
  300. _gatherChange(node, source) {
  301. if (!source.change_detail || source.change_detail.length === 0) return;
  302. if (!node.change_detail) node.change_detail = [];
  303. for (const cd of source.change_detail) {
  304. if (!cd.qty) continue;
  305. let ncd = node.change_detail.find(x => { return x.cid === cd.cid; });
  306. if (!ncd) {
  307. ncd = { cid: cd.cid, c_code: cd.c_code };
  308. node.change_detail.push(ncd);
  309. }
  310. ncd.qty = this.ctx.helper.add(ncd.qty, cd.qty);
  311. }
  312. }
  313. gather(source, parent) {
  314. parent = parent ? parent : this.parent;
  315. const checkFun = function (node, source) {
  316. return (source.is_tp && node.is_tp) || (!source.is_tp && !node.is_tp);
  317. };
  318. const node = this.ignoreParent ? this.addNodeWithoutParent(source, checkFun) : this.addNode(source, parent, checkFun);
  319. if (node.is_tp) {
  320. node.contract_tp = this.ctx.helper.add(node.contract_tp, source.contract_tp);
  321. } else {
  322. node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
  323. node.contract_tp = this.ctx.helper.mul(node.unit_price, node.contract_qty, this.ctx.tender.info.decimal.tp);
  324. }
  325. this._gatherChange(node, source);
  326. return node;
  327. }
  328. getUpdateData() {
  329. const result = {update: [], errors: []};
  330. for (const bn of this.baseNodes) {
  331. if (bn.contract_qty !== bn.org_contract_qty || bn.contract_tp !== bn.org_contract_tp) {
  332. result.update.push({lid: bn.id, contract_qty: bn.contract_qty, contract_tp: bn.contract_tp });
  333. }
  334. if (bn.change_detail && bn.change_detail.length > 0) {
  335. for (const cd of bn.change_detail) {
  336. result.errors.push({
  337. ledger_id: bn.ledger_id,
  338. b_code: bn.b_code, name: bn.name, unit: bn.unit,
  339. c_code: cd.c_code, qty: cd.qty, type: 'qc',
  340. });
  341. }
  342. }
  343. }
  344. for (const i of this.items) {
  345. result.errors.push({ b_code: i.b_code, name: i.name, unit: i.unit, qty: i.contract_qty, type: 'miss' });
  346. if (i.change_detail && i.change_detail.length > 0) {
  347. for (const cd of i.change_detail) {
  348. result.errors.push({
  349. b_code: i.b_code, name: i.name, unit: i.unit,
  350. c_code: cd.c_code, qty: cd.qty, type: 'miss-qc',
  351. });
  352. }
  353. }
  354. }
  355. return result;
  356. }
  357. }
  358. class sumLoad {
  359. constructor (ctx) {
  360. this.ctx = ctx;
  361. }
  362. recusiveLoadGatherGcl(node, parent, ignoreParent = false) {
  363. const isLeaf = !node.children || node.children.length === 0;
  364. const cur = (!ignoreParent || isLeaf) && node.b_code ? this.loadTree.gather(node, parent) : parent;
  365. if (isLeaf) return;
  366. for (const child of node.children) {
  367. this.recusiveLoadGatherGcl(child, cur, ignoreParent);
  368. }
  369. }
  370. async loadGatherGcl(select, maxId, tenders, defaultData) {
  371. this.loadTree = new loadLedgerGclTree(this.ctx, {
  372. parent: select, maxId, type: 'ledger', defaultData,
  373. });
  374. for (const tender of tenders) {
  375. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  376. const billsTree = new Ledger.billsTree(this.ctx, {
  377. id: 'ledger_id',
  378. pid: 'ledger_pid',
  379. order: 'order',
  380. level: 'level',
  381. rootId: -1,
  382. keys: ['id', 'tender_id', 'ledger_id'],
  383. stageId: 'id',
  384. });
  385. billsTree.loadDatas(billsData);
  386. for (const top of billsTree.children) {
  387. if ([1].indexOf(top.node_type) < 0) continue;
  388. this.recusiveLoadGatherGcl(top, null);
  389. }
  390. }
  391. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  392. this.loadTree.calculateAll(dealBills);
  393. return this.loadTree;
  394. }
  395. async updateGatherGcl(select, maxId, tenders, defaultData) {
  396. this.loadTree = new updateReviseGclTree(this.ctx, {
  397. parent: select, maxId, type: 'ledger', defaultData,
  398. });
  399. const posterity = await this.ctx.service.reviseBills.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  400. const pos = await this.ctx.service.revisePos.getData(this.ctx.tender.id);
  401. this.loadTree.loadBase(posterity, pos);
  402. for (const tender of tenders) {
  403. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  404. const billsTree = new Ledger.billsTree(this.ctx, {
  405. id: 'ledger_id',
  406. pid: 'ledger_pid',
  407. order: 'order',
  408. level: 'level',
  409. rootId: -1,
  410. keys: ['id', 'tender_id', 'ledger_id'],
  411. stageId: 'id',
  412. });
  413. billsTree.loadDatas(billsData);
  414. for (const top of billsTree.children) {
  415. if ([1].indexOf(top.node_type) < 0) continue;
  416. this.recusiveLoadGatherGcl(top, null);
  417. }
  418. }
  419. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  420. this.loadTree.calculateAll(dealBills);
  421. return this.loadTree;
  422. }
  423. _loadCurStageAndChange(billsData, curStageBills, curStageChange) {
  424. const billsIndex = {};
  425. for (const b of billsData) {
  426. billsIndex[b.id] = b;
  427. }
  428. for (const csb of curStageBills) {
  429. const b = billsIndex[csb.lid];
  430. if (!b) continue;
  431. b.contract_qty = csb.contract_qty;
  432. b.contract_tp = csb.contract_tp;
  433. }
  434. for (const csc of curStageChange) {
  435. if (!csc.qty) continue;
  436. const b = billsIndex[csc.lid];
  437. if (!b) continue;
  438. if (!b.change_detail) b.change_detail = [];
  439. let c = b.change_detail.find(x => { return x.cid === csc.cid });
  440. if (!c) {
  441. c = { cid: csc.cid };
  442. b.change_detail.push(c);
  443. }
  444. c.qty = this.ctx.helper.add(c.qty, csc.qty);
  445. }
  446. }
  447. async stageGatherGcl(select, maxId, tenders, defaultData) {
  448. const ignoreParent = this.ctx.tender.info.fun_rela.sum_load.ignoreParent;
  449. this.loadTree = new gatherStageGclTree(this.ctx, {
  450. parent: select, maxId, type: 'ledger', defaultData, ignoreParent,
  451. });
  452. const posterity = await this.ctx.service.ledger.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  453. const pos = await this.ctx.service.revisePos.getData(this.ctx.tender.id);
  454. this.loadTree.loadBase(posterity, pos);
  455. for (const tender of tenders) {
  456. if (!tender.stage) continue;
  457. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  458. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.tid, order: tender.stage});
  459. if (!stage) throw '选择的期不存在';
  460. const curStageData = await this.ctx.service.stageBills.getLastestStageData(tender.tid, stage.id);
  461. const curStageChange = await this.ctx.service.stageChangeFinal.getSumLoadFinalData(stage.id);
  462. this._loadCurStageAndChange(billsData, curStageData, curStageChange);
  463. const billsTree = new Ledger.billsTree(this.ctx, {
  464. id: 'ledger_id',
  465. pid: 'ledger_pid',
  466. order: 'order',
  467. level: 'level',
  468. rootId: -1,
  469. keys: ['id', 'tender_id', 'ledger_id'],
  470. stageId: 'id',
  471. });
  472. billsTree.loadDatas(billsData);
  473. for (const top of billsTree.children) {
  474. if ([1].indexOf(top.node_type) < 0) continue;
  475. this.recusiveLoadGatherGcl(top, null, ignoreParent);
  476. }
  477. }
  478. return this.loadTree;
  479. }
  480. }
  481. module.exports = sumLoad;