sum_load.js 17 KB

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