bills_pos_convert.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('./ledger');
  10. const splitChar = '-';
  11. const mergeChar = ';';
  12. class BillsPosConvert {
  13. constructor(ctx) {
  14. const self = this;
  15. this.ctx = ctx;
  16. this._ = this.ctx.helper._;
  17. this.tpFields = ['total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp',
  18. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp'];
  19. this.baseCalcFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty',
  20. 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  21. // mainData
  22. this.bpcTree = new Ledger.billsTree(this.ctx, {
  23. id: 'ledger_id',
  24. pid: 'ledger_pid',
  25. order: 'order',
  26. level: 'level',
  27. rootId: -1,
  28. keys: ['id', 'tender_id', 'ledger_id'],
  29. stageId: 'id',
  30. updateFields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],
  31. calcFields: [],
  32. });
  33. this.bpcPos = new Ledger.pos({
  34. id: 'id', ledgerId: 'lid',
  35. updateFields: ['contract_qty', 'qc_qty'],
  36. });
  37. this.bpcChange = [];
  38. // reusltData
  39. this.resultTreeSetting = {
  40. id: 'ledger_id',
  41. pid: 'ledger_pid',
  42. order: 'order',
  43. level: 'level',
  44. rootId: -1,
  45. fullPath: 'full_path',
  46. };
  47. this.resultTree = new Ledger.filterTree(this.ctx, this.resultTreeSetting);
  48. }
  49. // 加载基础数据
  50. _loadLedgerData (ledger) {
  51. const self = this;
  52. // 加载树结构数据
  53. this.bpcTree.loadDatas(ledger);
  54. }
  55. _loadPosData(pos) {
  56. this.bpcPos.loadDatas(pos);
  57. }
  58. loadData (ledger, pos, change) {
  59. this._loadLedgerData(ledger);
  60. this._loadPosData(pos);
  61. this.bpcChange = change;
  62. }
  63. _convertXmj(data) {
  64. const xmj = this.resultTree.addData(data, ['ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path',
  65. 'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'drawing_code', 'postil', 'memo', 'gxby_status', 'dagl_status', 'gxby_url', 'dagl_url',
  66. 'ex_memo1', 'ex_memo2', 'ex_memo3']);
  67. return xmj;
  68. }
  69. // v1
  70. _loadField(node, data, fields) {
  71. for (const prop in data) {
  72. if (fields.indexOf(prop) >= 0) node[prop] = data[prop];
  73. }
  74. }
  75. // v2
  76. _loadPosCalcFields(node, data) {
  77. const tpDecimal = this.ctx.tender.info.decimal.tp;
  78. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  79. node.total_price = this.ctx.helper.add(node.total_price, this.ctx.helper.mul(node.unit_price, data.quantity, tpDecimal));
  80. const org_price = node.org_price || node.unit_price;
  81. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  82. node.contract_tp = this.ctx.helper.add(node.contract_tp, this.ctx.helper.mul(node.unit_price, data.contract_qty, tpDecimal));
  83. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  84. node.qc_tp = this.ctx.helper.add(node.qc_tp, this.ctx.helper.mul(node.unit_price, data.qc_qty, tpDecimal));
  85. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  86. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  87. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, this.ctx.helper.mul(org_price, data.pre_contract_qty, tpDecimal));
  88. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  89. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, this.ctx.helper.mul(org_price, data.pre_qc_qty, tpDecimal));
  90. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  91. node.real_qty = this.ctx.helper.add(node.real_qty, data.real_qty);
  92. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, data.qc_minus_qty);
  93. node.pre_qc_minus_qty = this.ctx.helper.add(node.pre_qc_minus_qty, data.pre_qc_minus_qty);
  94. }
  95. _loadBillsCalcFields(node, data) {
  96. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  97. node.total_price = this.ctx.helper.add(node.total_price, data.total_price);
  98. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  99. node.contract_tp = this.ctx.helper.add(node.contract_tp, data.contract_tp);
  100. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  101. node.qc_tp = this.ctx.helper.add(node.qc_tp, data.qc_tp);
  102. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  103. node.gather_tp = this.ctx.helper.add(node.gather_tp, data.gather_tp);
  104. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  105. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, data.pre_contract_tp);
  106. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  107. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, data.pre_qc_tp);
  108. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  109. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, data.qc_minus_qty);
  110. node.pre_qc_minus_qty = this.ctx.helper.add(node.pre_qc_minus_qty, data.pre_qc_minus_qty);
  111. }
  112. _convertGcl(node, xmj) {
  113. if (!xmj) return;
  114. if (!xmj.unitTree) {
  115. xmj.unitTree = new Ledger.filterGatherTree(this.ctx, this.resultTreeSetting);
  116. }
  117. const pos = this.bpcPos.getLedgerPos(node.id);
  118. if (pos && pos.length > 0) {
  119. for (const p of pos) {
  120. const posUnit = xmj.unitTree.addNode({pos_name: p.name,
  121. b_code: null, name: '', unit: null, unit_price: null,
  122. gxby_status: p.gxby_status, dagl_status: p.dagl_status, gxby_url: p.gxby_url, dagl_url: p.dagl_url});
  123. if (p.drawing_code && posUnit.drawing_code.indexOf(p.drawing_code) < 0)
  124. posUnit.drawing_code.push(p.drawing_code);
  125. if (p.memo) posUnit.memo.push(p.memo);
  126. if (p.postil) posUnit.postil.push(p.postil);
  127. if (p.ex_memo1) posUnit.ex_memo1.push(p.ex_memo1);
  128. if (p.ex_memo2) posUnit.ex_memo2.push(p.ex_memo2);
  129. if (p.ex_memo3) posUnit.ex_memo3.push(p.ex_memo3);
  130. const gclUnit = xmj.unitTree.addNode({pos_name: '',
  131. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price, org_price: node.org_price,
  132. }, posUnit);
  133. if (node.drawing_code && gclUnit.drawing_code.indexOf(node.drawing_code) < 0)
  134. gclUnit.drawing_code.push(node.drawing_code);
  135. if (node.memo) gclUnit.memo.push(node.memo);
  136. if (node.postil) gclUnit.postil.push(node.postil);
  137. if (node.ex_memo1) gclUnit.ex_memo1.push(node.ex_memo1);
  138. if (node.ex_memo2) gclUnit.ex_memo2.push(node.ex_memo2);
  139. if (node.ex_memo3) gclUnit.ex_memo3.push(node.ex_memo3);
  140. //loadField(gclUnit, p, baseCalcFields);
  141. this._loadPosCalcFields(gclUnit, p);
  142. if (!gclUnit.changes) gclUnit.changes = [];
  143. for (const c of this.bpcChange) {
  144. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  145. gclUnit.changes.push(c);
  146. }
  147. }
  148. }
  149. } else {
  150. const unit = xmj.unitTree.addNode({
  151. pos_name: '',
  152. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price, org_price: node.org_price,
  153. });
  154. if (node.drawing_code && unit.drawing_code.indexOf(node.drawing_code) < 0)
  155. unit.drawing_code.push(node.drawing_code);
  156. if (node.memo) unit.memo.push(node.memo);
  157. if (node.postil) unit.postil.push(node.postil);
  158. if (node.ex_memo1) unit.ex_memo1.push(node.ex_memo1);
  159. if (node.ex_memo2) unit.ex_memo2.push(node.ex_memo2);
  160. if (node.ex_memo3) unit.ex_memo3.push(node.ex_memo3);
  161. this._loadBillsCalcFields(unit, node);
  162. if (!unit.changes) unit.changes = [];
  163. for (const c of this.bpcChange) {
  164. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  165. unit.changes.push(c);
  166. }
  167. }
  168. }
  169. }
  170. _recursiveConvertNode(nodes, xmj = null) {
  171. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  172. for (const node of nodes) {
  173. if (node.b_code && node.b_code !== '') {
  174. if (!node.children || node.children.length === 0) {
  175. this._convertGcl(node, xmj);
  176. }
  177. this._recursiveConvertNode(node.children, xmj);
  178. } else {
  179. const curXmj = this._convertXmj(node);
  180. this._recursiveConvertNode(node.children, curXmj);
  181. if (!node.children || node.children.length === 0) {
  182. this._loadField(curXmj, node, this.tpFields);
  183. }
  184. }
  185. }
  186. }
  187. _calculateChild(child) {
  188. const tpDecimal = this.ctx.tender.info.decimal.tp;
  189. child.gather_qty = this.ctx.helper.add(child.contract_qty, child.qc_qty);
  190. child.pre_gather_qty = this.ctx.helper.add(child.pre_contract_qty, child.pre_qc_qty);
  191. child.end_contract_qty = this.ctx.helper.add(child.contract_qty, child.pre_contract_qty);
  192. child.end_qc_qty = this.ctx.helper.add(child.qc_qty, child.pre_qc_qty);
  193. child.end_gather_qty = this.ctx.helper.add(child.gather_qty, child.pre_gather_qty);
  194. child.estimate_qty = !this.ctx.helper.checkZero(child.real_qty)
  195. ? this.ctx.helper.sub(this.ctx.helper.sub(child.real_qty, child.quantity), child.end_qc_qty)
  196. : null;
  197. // v1
  198. // child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, tpDecimal);
  199. // child.contract_tp = this.ctx.helper.mul(child.unit_price, child.contract_qty, tpDecimal);
  200. // child.qc_tp = this.ctx.helper.mul(child.unit_price, child.qc_qty, tpDecimal);
  201. // child.gather_tp = this.ctx.helper.mul(child.unit_price, child.gather_qty, tpDecimal);
  202. // child.pre_contract_tp = this.ctx.helper.mul(child.unit_price, child.pre_contract_qty, tpDecimal);
  203. // child.pre_qc_tp = this.ctx.helper.mul(child.unit_price, child.pre_qc_qty, tpDecimal);
  204. // child.pre_gather_tp = this.ctx.helper.mul(child.unit_price, child.pre_gather_qty, tpDecimal);
  205. // child.end_contract_tp = this.ctx.helper.mul(child.unit_price, child.end_contract_qty, tpDecimal);
  206. // child.end_qc_tp = this.ctx.helper.mul(child.unit_price, child.end_qc_qty, tpDecimal);
  207. // child.end_gather_tp = this.ctx.helper.mul(child.unit_price, child.end_gather_qty, tpDecimal);
  208. // v2
  209. child.gather_tp = this.ctx.helper.add(child.contract_tp, child.qc_tp);
  210. child.pre_gather_tp = this.ctx.helper.add(child.pre_contract_tp, child.pre_qc_tp);
  211. child.end_contract_tp = this.ctx.helper.add(child.pre_contract_tp, child.contract_tp);
  212. child.end_qc_tp = this.ctx.helper.add(child.pre_qc_tp, child.qc_tp);
  213. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  214. child.final_tp = this.ctx.helper.add(child.total_price, child.end_qc_tp);
  215. child.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.final_tp, 4), 100);
  216. child.real_tp = this.ctx.helper.mul(child.real_qty, child.unit_price, tpDecimal);
  217. child.estimate_tp = this.ctx.helper.mul(child.estimate_qty, child.unit_price, tpDecimal);
  218. child.bgl_code = this.ctx.helper._.uniq(this.ctx.helper._.map(child.changes, 'c_code')).join(';');
  219. // !#台账相关
  220. child.end_qc_minus_qty = this.ctx.helper.add(child.pre_qc_minus_qty, child.qc_minus_qty); // 1#台账
  221. child.final_1_qty = this.ctx.helper.add(child.end_qc_minus_qty, child.quantity); // 1#台账 = 台账+负变更
  222. child.final_1_tp = this.ctx.helper.mul(child.unit_price, child.final_1_qty, tpDecimal);
  223. child.end_final_1_tp = this.ctx.helper.add(child.final_1_tp, child.end_qc_tp);
  224. child.end_final_1_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.end_final_1_tp, 4), 100);
  225. const priceDiff = child.org_price ? this.ctx.helper.sub(child.unit_price, child.org_price) : 0;
  226. if (priceDiff && (child.pre_contract_qty || child.pre_qc_qty)) {
  227. child.contract_pc_tp = this.ctx.helper.sub(this.ctx.helper.mul(child.unit_price, child.pre_contract_qty, this.ctx.tender.info.decimal.tp), child.pre_contract_tp);
  228. child.qc_pc_tp = this.ctx.helper.sub(this.ctx.helper.mul(child.unit_price, child.pre_qc_qty, this.ctx.tender.info.decimal.tp), child.pre_qc_tp);
  229. child.pc_tp = this.ctx.helper.add(child.contract_pc_tp, child.qc_pc_tp);
  230. child.gather_tp = this.ctx.helper.add(child.gather_tp, child.pc_tp);
  231. child.end_contract_tp = this.ctx.helper.add(child.end_contract_tp, child.contract_pc_tp);
  232. child.end_qc_tp = this.ctx.helper.add(child.end_qc_tp, child.qc_pc_tp);
  233. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  234. }
  235. }
  236. _calculateNode(node, children) {
  237. for (const child of children) {
  238. node.total_price = this.ctx.helper.add(node.total_price, child.total_price);
  239. node.contract_tp = this.ctx.helper.add(node.contract_tp, child.contract_tp);
  240. node.qc_tp = this.ctx.helper.add(node.qc_tp, child.qc_tp);
  241. node.pc_tp = this.ctx.helper.add(node.pc_tp, child.pc_tp);
  242. node.gather_tp = this.ctx.helper.add(node.gather_tp, child.gather_tp);
  243. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, child.pre_contract_tp);
  244. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, child.pre_qc_tp);
  245. node.pre_gather_tp = this.ctx.helper.add(node.pre_gather_tp, child.pre_gather_tp);
  246. node.end_contract_tp = this.ctx.helper.add(node.end_contract_tp, child.end_contract_tp);
  247. node.end_qc_tp = this.ctx.helper.add(node.end_qc_tp, child.end_qc_tp);
  248. node.end_gather_tp = this.ctx.helper.add(node.end_gather_tp, child.end_gather_tp);
  249. node.real_tp = this.ctx.helper.add(node.real_tp, child.real_tp);
  250. node.estimate_tp = this.ctx.helper.add(node.estimate_tp, child.estimate_tp);
  251. node.final_1_tp = this.ctx.helper.add(node.final_1_tp, child.final_1_tp);
  252. }
  253. if (node.dgn_qty1)
  254. node.dgn_price = this.ctx.helper.div(node.total_price, node.dgn_qty1, this.ctx.tender.info.decimal.up);
  255. node.final_tp = this.ctx.helper.add(node.total_price, node.end_qc_tp);
  256. node.end_final_1_tp = this.ctx.helper.add(node.final_1_tp, node.end_qc_tp);
  257. node.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  258. node.end_final_1_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.end_final_1_tp, 4), 100);
  259. }
  260. _recursiveCalcUnitNodes(nodes) {
  261. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  262. for (const node of nodes) {
  263. this._recursiveCalcUnitNodes(node.children);
  264. if (node.children && node.children.length > 0) {
  265. this._calculateNode(node, node.children);
  266. } else {
  267. this._calculateChild(node);
  268. }
  269. if (node.drawing_code && node.drawing_code.length > 0)
  270. node.drawing_code_merge = node.drawing_code.join(mergeChar);
  271. if (node.memo && node.memo.length > 0)
  272. node.memo_merge = node.memo.join(mergeChar);
  273. if (node.ex_memo1 && node.ex_memo1.length > 0)
  274. node.ex_memo1_merge = node.ex_memo1.join(mergeChar);
  275. if (node.ex_memo2 && node.ex_memo2.length > 0)
  276. node.ex_memo2_merge = node.ex_memo2.join(mergeChar);
  277. if (node.ex_memo3 && node.ex_memo3.length > 0)
  278. node.ex_memo3_merge = node.ex_memo3.join(mergeChar);
  279. if (node.postil && node.postil.length > 0)
  280. node.postil_merge = node.postil.join(mergeChar);
  281. }
  282. }
  283. _recursiveCalculateAndSort(nodes) {
  284. const self = this;
  285. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  286. for (const node of nodes) {
  287. this._recursiveCalculateAndSort(node.children);
  288. if (node.unitTree) {
  289. node.unitTree.sortTreeNodeCustom(function (a, b) {
  290. if (a.b_code && a.b_code !== '') {
  291. return b.b_code && b.b_code !== '' ? self.ctx.helper.compareCode(a.b_code, b.b_code) : 1;
  292. } else {
  293. return b.b_code && b.b_code !== '' ? -1 : a.order - b.order;
  294. }
  295. });
  296. this._recursiveCalcUnitNodes(node.unitTree.children);
  297. this._calculateNode(node, node.unitTree.children, this.tpFields);
  298. } else if (node.children && node.children.length > 0) {
  299. this._calculateNode(node, node.children, this.tpFields);
  300. } else {
  301. this._calculateChild(node);
  302. }
  303. }
  304. }
  305. _calculateAndSortResult() {
  306. this.resultTree.sortTreeNode();
  307. this._recursiveCalculateAndSort(this.resultTree.children);
  308. }
  309. _generateCodeByWbsCode(wbsCode) {
  310. let needUpdate = false;
  311. for (const node of this.resultTree.nodes) {
  312. if (!node.unitTree) continue;
  313. let xmj = wbsCode.find(function (x) {
  314. return x.xmj_code === node.code;
  315. });
  316. for (const unit of node.unitTree.nodes) {
  317. if (!unit.pos_name || unit.pos_name === '') continue;
  318. if (!xmj) {
  319. xmj = {xmj_code: node.code, pos: []};
  320. wbsCode.push(xmj);
  321. needUpdate = true;
  322. }
  323. let index = xmj.pos.indexOf(unit.pos_name);
  324. if (index < 0) {
  325. unit.code = xmj.xmj_code + splitChar + (xmj.pos.length + 1);
  326. xmj.pos.push(unit.pos_name);
  327. needUpdate = true;
  328. } else {
  329. unit.code = xmj.xmj_code + splitChar + (index + 1);
  330. }
  331. }
  332. }
  333. return needUpdate;
  334. }
  335. _getResultData(filterFun) {
  336. const result = this.resultTree.getDefaultDatas(filterFun);
  337. for (const r of result) {
  338. if (r.unitTree) {
  339. r.unitTreeData = r.unitTree.getDefaultDatas(filterFun);
  340. delete r.unitTree;
  341. }
  342. }
  343. return result;
  344. }
  345. // 转换数据
  346. convert(filter) {
  347. this._recursiveConvertNode(this.bpcTree.children);
  348. this._calculateAndSortResult();
  349. this._generateCodeByWbsCode([]);
  350. switch (filter) {
  351. case 'cur':
  352. return this._getResultData(function (node) {
  353. for (const field of ['contract_tp', 'qc_tp', 'gather_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp']) {
  354. if (node[field]) {
  355. return false;
  356. }
  357. }
  358. return true;
  359. });
  360. case 'end':
  361. return this._getResultData(function (node) {
  362. for (const field of ['end_contract_tp', 'end_qc_tp', 'end_gather_tp']) {
  363. if (node[field]) {
  364. return false;
  365. }
  366. }
  367. return true;
  368. });
  369. default:
  370. return this._getResultData();
  371. }
  372. }
  373. /**
  374. * 转换数据,会根据wbsCodeHis生成计量单元的code
  375. * 如果计量单元在wbsCodeHis中没有历史,则会更新传入的wbsCodeHis,并返回[result, true], 反之返回[result, false]
  376. * @param wbsCodeHis
  377. * @returns {*[]}
  378. */
  379. convertByWbsCode(wbsCodeHis) {
  380. this._recursiveConvertNode(this.bpcTree.children);
  381. this._calculateAndSortResult();
  382. const needUpdate = this._generateCodeByWbsCode(wbsCodeHis);
  383. return [this._getResultData(), needUpdate];
  384. }
  385. }
  386. module.exports = BillsPosConvert;