sum_load.js 21 KB

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