sum_load.js 17 KB

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