sum_load.js 21 KB

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