sum_load.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const auditConst = require('../const/audit');
  11. class loadGclBaseTree {
  12. /**
  13. * 构造函数
  14. * @param {Array} tempData - 清单模板数据
  15. */
  16. constructor (ctx, setting) {
  17. this.ctx = ctx;
  18. this.parent = setting.parent;
  19. this.parent.children = [];
  20. this.defaultData = setting.defaultData;
  21. // 常量
  22. this.splitChar = '-';
  23. // 索引
  24. // 仅通过addNode或addNodeWithoutParent添加节点
  25. this.items = [];
  26. // this.parent下原来的节点
  27. this.baseNodes = [];
  28. this.ignoreParent = setting.ignoreParent;
  29. this.ignoreNotFind = setting.ignoreNotFind;
  30. this.loadChange = setting.loadChange;
  31. this.useSubUp = setting.useSubUp;
  32. // 缓存
  33. this.keyNodeId = setting.maxId ? setting.maxId + 1 : 1;
  34. }
  35. /**
  36. * 根据 编号 查找 父项项目节
  37. * @param {String} code - 子项编号
  38. * @returns {*}
  39. */
  40. findNode(node, parent, check) {
  41. parent = parent || this.parent;
  42. if (!parent.children) return null;
  43. for (const child of parent.children) {
  44. const checkLeaf = (child.is_leaf && node.is_leaf) || (!child.is_leaf && !node.is_leaf);
  45. if (child.b_code === node.b_code && child.name === node.name && child.unit === node.unit
  46. && checkLeaf && !child.hasPos && (!check || check(child, node))) {
  47. return child;
  48. }
  49. }
  50. return null;
  51. }
  52. findBCodeNode(b_code, mustParent) {
  53. for (const bn of this.baseNodes) {
  54. if (bn.b_code === b_code && (!mustParent || !bn.is_leaf)) return bn;
  55. }
  56. for (const i of this.items) {
  57. if (i.b_code === b_code && (!mustParent || !i.is_leaf)) return i;
  58. }
  59. return null;
  60. }
  61. findBCodeParent(b_code) {
  62. const codeArr = b_code.split('-');
  63. if (codeArr.length === 1) return null;
  64. codeArr.length = codeArr.length - 1;
  65. const parentCode = codeArr.join('-');
  66. const result = this.findBCodeNode(parentCode, true);
  67. return result || this.findBCodeParent(parentCode);
  68. }
  69. /**
  70. * 添加 树节点 并完善该节点的树结构
  71. * @param {Object} node - 添加节点
  72. * @param {Object} parent - 父项
  73. * @returns {*}
  74. */
  75. addNode(source, parent, check) {
  76. parent = parent ? parent : this.parent;
  77. let node = this.findNode(source, parent, check);
  78. if (!node) {
  79. if (!parent.children) parent.children = [];
  80. node = {
  81. id: this.ctx.app.uuid.v4(),
  82. tender_id: this.ctx.tender.id,
  83. ledger_id: this.keyNodeId,
  84. ledger_pid: parent.ledger_id,
  85. level: parent.level +1,
  86. full_path: parent.full_path + '-' + this.keyNodeId,
  87. order: parent.children.length + 1,
  88. children: [],
  89. b_code: source.b_code,
  90. name: source.name,
  91. unit: source.unit,
  92. deal_qty: 0,
  93. sgfh_qty: 0,
  94. qtcl_qty: 0,
  95. sjcl_qyt: 0,
  96. quantity: 0,
  97. is_leaf: source.is_leaf,
  98. hasPos: false,
  99. sub_up: source.unit_price || 0,
  100. };
  101. this.keyNodeId += 1;
  102. parent.children.push(node);
  103. this.items.push(node);
  104. }
  105. return node;
  106. }
  107. findLeaf(node, check) {
  108. for (const bn of this.baseNodes) {
  109. if (bn.b_code === node.b_code && bn.name === node.name && bn.unit === node.unit
  110. && bn.is_leaf && !bn.hasPos && (!check || check(bn, node))) return bn;
  111. }
  112. for (const i of this.items) {
  113. if (i.b_code === node.b_code && i.name === node.name && i.unit === node.unit
  114. && i.is_leaf && !i.hasPos && (!check || check(i, node))) return i;
  115. }
  116. return null;
  117. }
  118. addNodeWithoutParent(source, check) {
  119. let node = this.findLeaf(source, check);
  120. if (!node) {
  121. const parent = this.findBCodeParent(source.b_code) || this.parent;
  122. node = {
  123. id: this.ctx.app.uuid.v4(),
  124. tender_id: this.ctx.tender.id,
  125. ledger_id: this.keyNodeId,
  126. ledger_pid: parent.ledger_id,
  127. level: parent.level + 1,
  128. full_path: parent.full_path + '-' + this.keyNodeId,
  129. order: parent.children.length + 1,
  130. b_code: source.b_code,
  131. name: source.name,
  132. unit: source.unit,
  133. deal_qty: 0,
  134. sgfh_qty: 0,
  135. qtcl_qty: 0,
  136. sjcl_qty: 0,
  137. quantity: 0,
  138. is_leaf: source.is_leaf,
  139. hasPos: false,
  140. sub_up: source.unit_price || 0,
  141. };
  142. this.keyNodeId += 1;
  143. parent.children.push(node);
  144. this.items.push(node);
  145. }
  146. return node;
  147. }
  148. gather(source, parent) {}
  149. getUpdateData() {}
  150. resortChildrenByCode(node) {
  151. const helper = this.ctx.helper;
  152. if (!node.children || node.children.length === 0) return;
  153. node.children.sort((x, y) => {
  154. return helper.compareCode(x.b_code, y.b_code);
  155. });
  156. for (const [i, c] of node.children.entries()) {
  157. this.resortChildrenByCode(c);
  158. c.order = i + 1;
  159. }
  160. }
  161. resortByCode() {
  162. this.resortChildrenByCode(this.parent);
  163. }
  164. recursiveCalculate(dealBills, node) {
  165. if (node.children && node.children.length > 0) {
  166. for (const child of node.children) {
  167. this.recursiveCalculate(dealBills, child);
  168. }
  169. } else {
  170. const p = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  171. node.deal_qty = this.ctx.helper.round(node.deal_qty, p.value);
  172. node.sgfh_qty = this.ctx.helper.round(node.sgfh_qty, p.value);
  173. node.sjcl_qty = this.ctx.helper.round(node.sjcl_qty, p.value);
  174. node.qtcl_qty = this.ctx.helper.round(node.qtcl_qty, p.value);
  175. node.quantity = this.ctx.helper.round(node.quantity, p.value);
  176. if (!node.unit_price) {
  177. const db = dealBills.find(x => { return x.code === node.b_code && x.name === node.name && x.unit === node.unit });
  178. if (db) {
  179. node.unit_price = db.unit_price;
  180. } else if (this.useSubUp) {
  181. node.unit_price = this.ctx.helper.round(node.sub_up, this.ctx.tender.info.decimal.up);
  182. }
  183. }
  184. node.deal_tp = this.ctx.helper.mul(node.deal_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  185. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  186. node.sjcl_tp = this.ctx.helper.mul(node.sjcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  187. node.qtcl_tp = this.ctx.helper.mul(node.qtcl_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  188. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  189. }
  190. }
  191. calculateAll(dealBills) {
  192. this.recursiveCalculate(dealBills, this.parent);
  193. }
  194. }
  195. class loadLedgerGclTree extends loadGclBaseTree {
  196. gather(source, parent) {
  197. const node = this.addNode(source, parent);
  198. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  199. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  200. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  201. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  202. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  203. return node;
  204. }
  205. getUpdateData() {
  206. const update = [];
  207. if (this.items.length > 0) update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  208. const create = [];
  209. for (const i of this.items) {
  210. create.push({
  211. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  212. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  213. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  214. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  215. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  216. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  217. });
  218. }
  219. return {update, create};
  220. }
  221. }
  222. class updateReviseGclTree extends loadGclBaseTree {
  223. constructor(ctx, setting) {
  224. super(ctx, setting);
  225. this.errors = [];
  226. }
  227. loadBase(datas, pos) {
  228. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  229. const Index = {};
  230. for (const d of datas) {
  231. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  232. if (!parent) continue;
  233. if (!parent.children) parent.children = [];
  234. const relaPos = pos.filter(x => {return x.lid === d.id});
  235. const baseNode = {
  236. id: d.id,
  237. ledger_id: d.ledger_id,
  238. ledger_pid: d.ledger_pid,
  239. level: d.level,
  240. is_leaf: d.is_leaf,
  241. full_path: d.full_path,
  242. b_code: d.b_code,
  243. name: d.name,
  244. unit: d.unit,
  245. unit_price: d.unit_price,
  246. org_deal_qty: d.deal_qty || 0,
  247. org_sgfh_qty: d.sgfh_qty || 0,
  248. org_sjcl_qty: d.sjcl_qty || 0,
  249. org_qtcl_qty: d.qtcl_qty || 0,
  250. org_qty: d.quantity || 0,
  251. org_order: d.order,
  252. sgfh_qty: 0,
  253. sjcl_qty: 0,
  254. qtcl_qty: 0,
  255. quantity: 0,
  256. hasPos: relaPos.length > 0,
  257. };
  258. parent.children.push(baseNode);
  259. Index[baseNode.ledger_id] = baseNode;
  260. this.baseNodes.push(baseNode);
  261. }
  262. }
  263. gather(source, parent) {
  264. const node = this.ignoreParent ? this.addNodeWithoutParent(source) : this.addNode(source, parent);
  265. node.deal_qty = this.ctx.helper.add(node.deal_qty, source.deal_qty);
  266. node.sgfh_qty = this.ctx.helper.add(node.sgfh_qty, source.sgfh_qty);
  267. node.qtcl_qty = this.ctx.helper.add(node.qtcl_qty, source.qtcl_qty);
  268. node.sjcl_qty = this.ctx.helper.add(node.sjcl_qty, source.sjcl_qty);
  269. node.quantity = this.ctx.helper.add(node.quantity, source.quantity);
  270. return node;
  271. }
  272. getUpdateData() {
  273. const result = {update: [], errors: [], create: []};
  274. if (this.baseNodes.length === 0) {
  275. if (this.items.length > 0) result.update.push({id: this.parent.id, ledger_id: this.parent.ledger_id, is_leaf: 0});
  276. } else {
  277. for (const bn of this.baseNodes) {
  278. if (bn.children && bn.children.length > 0) {
  279. if (bn.order !== bn.org_order) {
  280. result.update.push({
  281. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  282. });
  283. }
  284. continue;
  285. }
  286. 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) {
  287. result.errors.push({
  288. ledger_id: bn.ledger_id,
  289. b_code: bn.b_code, name: bn.name, unit: bn.unit,
  290. 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',
  291. });
  292. if (bn.order !== bn.org_order) {
  293. result.update.push({
  294. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  295. });
  296. }
  297. } else if (bn.sjcl_qty !== bn.org_sjcl_qty || bn.qtcl_qty !== bn.org_qtcl_qty || bn.sgfh_qty !== bn.org_sgfh_qty) {
  298. result.update.push({
  299. id: bn.id, ledger_id: bn.ledger_id, unit_price: bn.unit_price || 0, order: bn.order,
  300. deal_qty: bn.deal_qty, deal_tp: bn.deal_tp || 0,
  301. sgfh_qty: bn.sgfh_qty, sjcl_qty: bn.sjcl_qty, qtcl_qty: bn.qtcl_qty, quantity: bn.quantity,
  302. sgfh_tp: bn.sgfh_tp || 0, sjcl_tp: bn.sjcl_tp || 0, qtcl_tp: bn.qtcl_tp || 0, total_price: bn.total_price || 0,
  303. });
  304. } else if (bn.order !== bn.org_order) {
  305. result.update.push({
  306. id: bn.id, ledger_id: bn.ledger_id, order: bn.order,
  307. });
  308. }
  309. }
  310. }
  311. for (const i of this.items) {
  312. result.create.push({
  313. id: i.id, tender_id: i.tender_id, ledger_id: i.ledger_id, ledger_pid: i.ledger_pid,
  314. level: i.level, order: i.order, full_path: i.full_path, is_leaf: !i.children || i.children.length === 0,
  315. b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price || 0,
  316. deal_qty: i.deal_qty, deal_tp: i.deal_tp || 0,
  317. sgfh_qty: i.sgfh_qty, sjcl_qty: i.sjcl_qty, qtcl_qty: i.qtcl_qty, quantity: i.quantity,
  318. sgfh_tp: i.sgfh_tp || 0, sjcl_tp: i.sjcl_tp || 0, qtcl_tp: i.qtcl_tp || 0, total_price: i.total_price || 0,
  319. });
  320. }
  321. return result;
  322. }
  323. }
  324. class gatherStageGclTree extends loadGclBaseTree {
  325. constructor(ctx, setting) {
  326. super(ctx, setting);
  327. this.cover = setting.cover;
  328. this.settleStatus = this.ctx.service.settle.settleStatus;
  329. }
  330. loadBase(datas, pos) {
  331. datas.sort((x, y) => { return x.level === y.level ? x.order - y.order : x.level - y.level; });
  332. const Index = {};
  333. for (const d of datas) {
  334. const parent = this.parent.ledger_id === d.ledger_pid ? this.parent : Index[d.ledger_pid];
  335. if (!parent) continue;
  336. if (!parent.children) parent.children = [];
  337. const relaPos = pos.filter(x => {return x.lid === d.id});
  338. const baseNode = {
  339. id: d.id,
  340. is_import: d.is_import,
  341. ledger_id: d.ledger_id,
  342. ledger_pid: d.ledger_pid,
  343. level: d.level,
  344. is_leaf: d.is_leaf,
  345. full_path: d.full_path,
  346. b_code: d.b_code,
  347. name: d.name,
  348. unit: d.unit,
  349. unit_price: d.unit_price,
  350. org_contract_qty: d.contract_qty || 0,
  351. org_contract_tp: d.contract_tp || 0,
  352. org_qc_qty: d.qc_qty || 0,
  353. org_qc_tp: d.qc_tp || 0,
  354. org_qc_minus_qty: d.qc_minus_qty || 0,
  355. org_positive_qc_qty: d.positive_qc_qty || 0,
  356. org_positive_qc_tp: d.positive_qc_tp || 0,
  357. org_negative_qc_qty: d.negative_qc_qty || 0,
  358. org_negative_qc_tp: d.negative_qc_tp || 0,
  359. org_order: d.order,
  360. contract_qty: 0,
  361. contract_tp: 0,
  362. qc_qty: 0,
  363. qc_tp: 0,
  364. is_tp: d.is_tp,
  365. hasPos: relaPos.length > 0,
  366. pre_contract_qty: d.pre_contract_qty || 0,
  367. pre_contract_tp: d.pre_contract_tp || 0,
  368. pre_qc_minus_qty: d.pre_qc_minus_qty || 0,
  369. };
  370. parent.children.push(baseNode);
  371. Index[baseNode.ledger_id] = baseNode;
  372. this.baseNodes.push(baseNode);
  373. }
  374. }
  375. _gatherChange(node, source) {
  376. if (!source.change_detail || source.change_detail.length === 0) return;
  377. if (!node.change_detail) node.change_detail = [];
  378. for (const cd of source.change_detail) {
  379. if (!cd.qty) continue;
  380. node.change_detail.push(cd);
  381. }
  382. }
  383. calcContractTp(info, node) {
  384. if (info.calc_type === 'tp') {
  385. let activeQty = this.ctx.helper.add(node.quantity, node.qc_minus_qty);
  386. let end_contract_qty = node.contract_qty;
  387. activeQty = this.ctx.helper.add(activeQty, node.pre_qc_minus_qty);
  388. end_contract_qty = this.ctx.helper.add(end_contract_qty, node.pre_contract_qty);
  389. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), node.total_price, decimal.tp);
  390. return this.ctx.helper.sub(end_contract_tp, node.pre_contract_tp);
  391. } else if (info.calc_type === 'up') {
  392. return this.ctx.helper.mul(node.unit_price, node.contract_qty, info.decimal.tp);
  393. }
  394. }
  395. gather(source, parent) {
  396. parent = parent ? parent : this.parent;
  397. const checkFun = function (node, source) {
  398. return (source.is_tp && node.is_tp) || (!source.is_tp && !node.is_tp);
  399. };
  400. const node = this.ignoreParent ? this.addNodeWithoutParent(source, checkFun) : this.addNode(source, parent, checkFun);
  401. if (node.settle_status === this.settleStatus.finish) return node;
  402. if (node.is_tp) {
  403. node.contract_tp = this.ctx.helper.add(node.contract_tp, source.contract_tp);
  404. } else {
  405. if (this.loadChange) {
  406. node.qc_qty = this.ctx.helper.add(node.qc_qty, source.qc_qty);
  407. node.qc_tp = this.ctx.helper.mul(node.unit_price, node.qc_qty, this.ctx.tender.info.decimal.tp);
  408. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, source.qc_minus_qty);
  409. node.positive_qc_qty = this.ctx.helper.add(node.positive_qc_qty, source.positive_qc_qty);
  410. node.positive_qc_tp = this.ctx.helper.mul(node.unit_price, node.positive_qc_qty, this.ctx.tender.info.decimal.tp);
  411. node.negative_qc_qty = this.ctx.helper.add(node.negative_qc_qty, source.negative_qc_qty);
  412. node.negative_qc_tp = this.ctx.helper.mul(node.unit_price, node.negative_qc_qty, this.ctx.tender.info.decimal.tp);
  413. } else {
  414. node.qc_minus_qty = node.org_qc_minus_qty;
  415. }
  416. node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
  417. node.contract_tp = this.calcContractTp(this.ctx.tender.info, node);
  418. }
  419. if (this.loadChange) this._gatherChange(node, source);
  420. return node;
  421. }
  422. getUpdateData() {
  423. const result = { update: [], errors: [], qc_detail: [] };
  424. for (const bn of this.baseNodes) {
  425. if (!this.cover && !bn.is_import && !bn.contract_qty && !bn.qc_qty && !bn.contract_tp && !bn.qc_minus_qty) continue;
  426. if (!bn.is_import && bn.org_qc_qty !== 0 && bn.qc_qty !== 0) {
  427. result.errors.push({ b_code: bn.b_code, name: bn.name, unit: bn.unit, qc_qty: bn.qc_qty, qc_minus_qty: bn.qc_minus_qty, ledger_id: bn.ledger_id, type: 'qc-conflict'});
  428. continue;
  429. }
  430. if (bn.is_import || this.cover || bn.contract_qty !== bn.org_contract_qty || bn.contract_tp !== bn.org_contract_tp || bn.qc_qty !== bn.org_qc_qty || bn.qc_minus_qty !== bn.org_qc_minus_qty) {
  431. let data = { lid: bn.id, contract_qty: bn.contract_qty, contract_tp: bn.contract_tp };
  432. if (!this.loadChange || (!bn.is_import && bn.org_qc_qty)) {
  433. data.qc_qty = bn.org_qc_qty;
  434. data.qc_tp = bn.org_qc_tp;
  435. data.qc_minus_qty = bn.org_qc_minus_qty || 0;
  436. data.positive_qc_qty = bn.org_positive_qc_qty || 0;
  437. data.positive_qc_tp = bn.org_positive_qc_tp || 0;
  438. data.negative_qc_qty = bn.org_negative_qc_qty || 0;
  439. data.negative_qc_tp = bn.org_negative_qc_tp || 0;
  440. } else {
  441. data.qc_qty = bn.qc_qty;
  442. data.qc_tp = bn.qc_tp;
  443. data.qc_minus_qty = bn.qc_minus_qty || 0;
  444. data.positive_qc_qty = bn.positive_qc_qty || 0;
  445. data.positive_qc_tp = bn.positive_qc_tp || 0;
  446. data.negative_qc_qty = bn.negative_qc_qty || 0;
  447. data.negative_qc_tp = bn.negative_qc_tp || 0;
  448. }
  449. result.update.push(data);
  450. }
  451. if (this.loadChange && bn.change_detail && bn.change_detail.length > 0) {
  452. for (const cd of bn.change_detail) {
  453. result.qc_detail.push({
  454. lid: bn.id, rela_tid: cd.tid, rela_sid: cd.sid, rela_lid: cd.lid, rela_cid: cd.cid, rela_cbid: cd.cbid, rela_qty: cd.qty, rela_minus: cd.minus, rela_no_value: cd.no_value, unit_price: bn.unit_price,
  455. });
  456. }
  457. }
  458. }
  459. if (!this.ignoreNotFind) {
  460. for (const i of this.items) {
  461. result.errors.push({ b_code: i.b_code, name: i.name, unit: i.unit, qty: i.contract_qty, qc_qty: i.qc_qty, qc_minus_qty: i.qc_minus_qty, type: 'miss' });
  462. }
  463. }
  464. return result;
  465. }
  466. }
  467. class sumLoad {
  468. constructor(ctx) {
  469. this.ctx = ctx;
  470. }
  471. recusiveLoadGatherGcl(node, parent, ignoreParent = false, match = false) {
  472. const loadMatch = match || node.match || (parent && parent.match);
  473. const isLeaf = !node.children || node.children.length === 0;
  474. const cur = loadMatch && (!ignoreParent || isLeaf) && node.b_code ? this.loadTree.gather(node, parent) : parent;
  475. if (isLeaf) return;
  476. for (const child of node.children) {
  477. this.recusiveLoadGatherGcl(child, cur, ignoreParent, loadMatch);
  478. }
  479. }
  480. async loadGatherGcl(select, maxId, tenders, defaultData, subUp) {
  481. this.loadTree = new loadLedgerGclTree(this.ctx, {
  482. parent: select, maxId, type: 'ledger', defaultData, useSubUp: subUp,
  483. });
  484. for (const tender of tenders) {
  485. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  486. const billsTree = new Ledger.billsTree(this.ctx, {
  487. id: 'ledger_id',
  488. pid: 'ledger_pid',
  489. order: 'order',
  490. level: 'level',
  491. rootId: -1,
  492. keys: ['id', 'tender_id', 'ledger_id'],
  493. stageId: 'id',
  494. });
  495. billsTree.loadDatas(billsData);
  496. billsTree.nodes.forEach(x => {
  497. x.match = tender.match_code ? x.code === tender.match_code : true;
  498. });
  499. for (const top of billsTree.children) {
  500. if ([1].indexOf(top.node_type) < 0) continue;
  501. this.recusiveLoadGatherGcl(top, null);
  502. }
  503. }
  504. this.loadTree.resortByCode();
  505. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  506. this.loadTree.calculateAll(dealBills);
  507. return this.loadTree;
  508. }
  509. async updateGatherGcl(select, maxId, tenders, defaultData, subUp) {
  510. const ignoreParent = this.ctx.tender.info.fun_rela.sum_load.ignoreParent;
  511. this.loadTree = new updateReviseGclTree(this.ctx, {
  512. parent: select, maxId, type: 'ledger', defaultData, ignoreParent, useSubUp: subUp,
  513. });
  514. const posterity = await this.ctx.service.reviseBills.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  515. const pos = await this.ctx.service.revisePos.getData(this.ctx.tender.id);
  516. this.loadTree.loadBase(posterity, pos);
  517. for (const tender of tenders) {
  518. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  519. const billsTree = new Ledger.billsTree(this.ctx, {
  520. id: 'ledger_id',
  521. pid: 'ledger_pid',
  522. order: 'order',
  523. level: 'level',
  524. rootId: -1,
  525. keys: ['id', 'tender_id', 'ledger_id'],
  526. stageId: 'id',
  527. });
  528. billsTree.loadDatas(billsData);
  529. billsTree.nodes.forEach(x => {
  530. x.match = tender.match_code ? x.code === tender.match_code : true;
  531. });
  532. for (const top of billsTree.children) {
  533. if ([1].indexOf(top.node_type) < 0) continue;
  534. this.recusiveLoadGatherGcl(top, null, ignoreParent);
  535. }
  536. }
  537. this.loadTree.resortByCode();
  538. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  539. this.loadTree.calculateAll(dealBills);
  540. return this.loadTree;
  541. }
  542. _loadCurStageAndChange(billsData, curStageBills, curStageChange) {
  543. const billsIndex = {};
  544. for (const b of billsData) {
  545. billsIndex[b.id] = b;
  546. }
  547. for (const csb of curStageBills) {
  548. const b = billsIndex[csb.lid];
  549. if (!b) continue;
  550. b.contract_qty = csb.contract_qty;
  551. b.contract_tp = csb.contract_tp;
  552. b.qc_qty = csb.qc_qty;
  553. b.qc_minus_qty = csb.qc_minus_qty;
  554. b.positive_qc_qty = csb.positive_qc_qty;
  555. b.negative_qc_qty = csb.negative_qc_qty;
  556. }
  557. for (const csc of curStageChange) {
  558. if (!csc.qty) continue;
  559. const b = billsIndex[csc.lid];
  560. if (!b) continue;
  561. if (!b.change_detail) b.change_detail = [];
  562. let c = b.change_detail.find(x => { return x.cbid === csc.cbid });
  563. if (!c) {
  564. c = { tid: csc.tid, sid: csc.sid, cid: csc.cid, cbid: csc.cbid, lid: csc.lid, c_code: csc.c_code, minus: csc.minus, no_value: csc.no_value };
  565. b.change_detail.push(c);
  566. }
  567. c.qty = this.ctx.helper.add(c.qty, csc.qty);
  568. }
  569. }
  570. async stageGatherGcl(select, maxId, tenders, defaultData, cover, ignore, loadChange) {
  571. const ignoreParent = this.ctx.tender.info.fun_rela.sum_load.ignoreParent;
  572. this.loadTree = new gatherStageGclTree(this.ctx, {
  573. parent: select, maxId, type: 'ledger', defaultData, ignoreParent, cover, ignoreNotFind: ignore, loadChange
  574. });
  575. const posterity = await this.ctx.service.ledger.getPosterityByParentId(this.ctx.tender.id, select.ledger_id);
  576. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  577. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  578. const importLid = await this.ctx.service.stageImportChange.getLeafXmjImportLid(this.ctx.stage.id, select.id);
  579. const settleStatusBills = this.ctx.stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: this.ctx.stage.readySettle.id }}) : [];
  580. const preStageBills = await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender.data, this.ctx.stage.preCheckedStage.order);
  581. this.ctx.helper.assignRelaData(posterity, [
  582. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  583. { data: importLid, fields: ['is_import'], prefix: '', relaId: 'lid' },
  584. { data: stageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'positive_qc_qty', 'positive_qc_tp', 'negative_qc_qty', 'negative_qc_tp'], prefix: '', relaId: 'lid' },
  585. { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  586. { data: preStageBills, fields: ['contract_qty', 'contract_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid'},
  587. ]);
  588. const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  589. const settleStatusPos = this.ctx.stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: this.ctx.stage.readySettle.id }}) : [];
  590. this.ctx.helper.assignRelaData(pos, [
  591. { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  592. ]);
  593. this.loadTree.loadBase(posterity, pos);
  594. for (const tender of tenders) {
  595. if (!tender.stage) continue;
  596. const billsData = await this.ctx.service.ledger.getData(tender.tid);
  597. const billsExtraData = await this.ctx.service.ledgerExtra.getData(tender.tid, ['is_tp']);
  598. this.ctx.helper.assignRelaData(billsData, [
  599. { data: billsExtraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  600. ]);
  601. const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.tid, order: tender.stage});
  602. if (!stage) throw '选择的期不存在';
  603. const curStageData = await this.ctx.service.stageBills.getLastestStageData2(tender.tid, stage.id);
  604. const curStageChange = stage.status === auditConst.stage.status.checked
  605. ? await this.ctx.service.stageChangeFinal.getSumLoadFinalData(stage.id)
  606. : await this.ctx.service.stageChange.getSumLoadFinalData(stage.id);
  607. this._loadCurStageAndChange(billsData, curStageData, curStageChange);
  608. const billsTree = new Ledger.billsTree(this.ctx, {
  609. id: 'ledger_id',
  610. pid: 'ledger_pid',
  611. order: 'order',
  612. level: 'level',
  613. rootId: -1,
  614. keys: ['id', 'tender_id', 'ledger_id'],
  615. stageId: 'id',
  616. });
  617. billsTree.loadDatas(billsData);
  618. billsTree.nodes.forEach(x => {
  619. x.match = tender.match_code ? x.code === tender.match_code : true;
  620. });
  621. for (const top of billsTree.children) {
  622. if ([1].indexOf(top.node_type) < 0) continue;
  623. this.recusiveLoadGatherGcl(top, null, ignoreParent);
  624. }
  625. }
  626. return this.loadTree;
  627. }
  628. }
  629. module.exports = sumLoad;