sum_load.js 15 KB

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