sum_load.js 19 KB

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