bills_pos_convert.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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',
  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']);
  66. return xmj;
  67. }
  68. // v1
  69. _loadField(node, data, fields) {
  70. for (const prop in data) {
  71. if (fields.indexOf(prop) >= 0) node[prop] = data[prop];
  72. }
  73. }
  74. // v2
  75. _loadPosCalcFields(node, data) {
  76. const tpDecimal = this.ctx.tender.info.decimal.tp;
  77. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  78. node.total_price = this.ctx.helper.add(node.total_price, this.ctx.helper.mul(node.unit_price, data.quantity, tpDecimal));
  79. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  80. node.contract_tp = this.ctx.helper.add(node.contract_tp, this.ctx.helper.mul(node.unit_price, data.contract_qty, tpDecimal));
  81. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  82. node.qc_tp = this.ctx.helper.add(node.qc_tp, this.ctx.helper.mul(node.unit_price, data.qc_qty, tpDecimal));
  83. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  84. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  85. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, this.ctx.helper.mul(node.unit_price, data.pre_contract_qty, tpDecimal));
  86. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  87. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, this.ctx.helper.mul(node.unit_price, data.pre_qc_qty, tpDecimal));
  88. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  89. }
  90. _loadBillsCalcFields(node, data) {
  91. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  92. node.total_price = this.ctx.helper.add(node.total_price, data.total_price);
  93. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  94. node.contract_tp = this.ctx.helper.add(node.contract_tp, data.contract_tp);
  95. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  96. node.qc_tp = this.ctx.helper.add(node.qc_tp, );
  97. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  98. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  99. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, data.pre_contract_tp);
  100. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  101. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, data.pre_qc_tp);
  102. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  103. }
  104. _convertGcl(node, xmj) {
  105. if (!xmj) return;
  106. if (!xmj.unitTree) {
  107. xmj.unitTree = new Ledger.filterGatherTree(this.ctx, this.resultTreeSetting);
  108. }
  109. const pos = this.bpcPos.getLedgerPos(node.id);
  110. if (pos && pos.length > 0) {
  111. for (const p of pos) {
  112. const posUnit = xmj.unitTree.addNode({pos_name: p.name,
  113. b_code: null, name: '', unit: null, unit_price: null});
  114. if (p.drawing_code && posUnit.drawing_code.indexOf(p.drawing_code) < 0)
  115. posUnit.drawing_code.push(p.drawing_code);
  116. if (p.memo) posUnit.memo.push(p.memo);
  117. if (node.postil) posUnit.postil.push(node.postil);
  118. const gclUnit = xmj.unitTree.addNode({pos_name: '',
  119. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price
  120. }, posUnit);
  121. if (node.drawing_code && gclUnit.drawing_code.indexOf(node.drawing_code) < 0)
  122. gclUnit.drawing_code.push(node.drawing_code);
  123. if (node.memo) gclUnit.memo.push(node.memo);
  124. if (node.postil) gclUnit.postil.push(node.postil);
  125. //loadField(gclUnit, p, baseCalcFields);
  126. this._loadPosCalcFields(gclUnit, p);
  127. if (!gclUnit.changes) gclUnit.changes = [];
  128. for (const c of this.bpcChange) {
  129. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  130. gclUnit.changes.push(c);
  131. }
  132. }
  133. }
  134. } else {
  135. const unit = xmj.unitTree.addNode({
  136. pos_name: '',
  137. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price,
  138. });
  139. if (node.drawing_code && unit.drawing_code.indexOf(node.drawing_code) < 0)
  140. unit.drawing_code.push(node.drawing_code);
  141. if (node.memo) unit.memo.push(node.memo);
  142. if (node.postil) unit.postil.push(node.postil);
  143. this._loadBillsCalcFields(unit, node);
  144. if (!unit.changes) unit.changes = [];
  145. for (const c of this.bpcChange) {
  146. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  147. unit.changes.push(c);
  148. }
  149. }
  150. }
  151. }
  152. _recursiveConvertNode(nodes, xmj = null) {
  153. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  154. for (const node of nodes) {
  155. if (node.b_code && node.b_code !== '') {
  156. if (!node.children || node.children.length === 0) {
  157. this._convertGcl(node, xmj);
  158. }
  159. this._recursiveConvertNode(node.children, xmj);
  160. } else {
  161. const curXmj = this._convertXmj(node);
  162. this._recursiveConvertNode(node.children, curXmj);
  163. if (!node.children || node.children.length === 0) {
  164. this._loadField(curXmj, node, this.tpFields);
  165. }
  166. }
  167. }
  168. }
  169. _calculateChild(child) {
  170. //const tpDecimal = this.ctx.tender.info.decimal.tp;
  171. child.gather_qty = this.ctx.helper.add(child.contract_qty, child.qc_qty);
  172. child.pre_gather_qty = this.ctx.helper.add(child.pre_contract_qty, child.pre_qc_qty);
  173. child.end_contract_qty = this.ctx.helper.add(child.contract_qty, child.pre_contract_qty);
  174. child.end_qc_qty = this.ctx.helper.add(child.qc_qty, child.pre_qc_qty);
  175. child.end_gather_qty = this.ctx.helper.add(child.gather_qty, child.pre_gather_qty);
  176. // v1
  177. // child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, tpDecimal);
  178. // child.contract_tp = this.ctx.helper.mul(child.unit_price, child.contract_qty, tpDecimal);
  179. // child.qc_tp = this.ctx.helper.mul(child.unit_price, child.qc_qty, tpDecimal);
  180. // child.gather_tp = this.ctx.helper.mul(child.unit_price, child.gather_qty, tpDecimal);
  181. // child.pre_contract_tp = this.ctx.helper.mul(child.unit_price, child.pre_contract_qty, tpDecimal);
  182. // child.pre_qc_tp = this.ctx.helper.mul(child.unit_price, child.pre_qc_qty, tpDecimal);
  183. // child.pre_gather_tp = this.ctx.helper.mul(child.unit_price, child.pre_gather_qty, tpDecimal);
  184. // child.end_contract_tp = this.ctx.helper.mul(child.unit_price, child.end_contract_qty, tpDecimal);
  185. // child.end_qc_tp = this.ctx.helper.mul(child.unit_price, child.end_qc_qty, tpDecimal);
  186. // child.end_gather_tp = this.ctx.helper.mul(child.unit_price, child.end_gather_qty, tpDecimal);
  187. // v2
  188. child.gather_tp = this.ctx.helper.add(child.contract_tp, child.qc_tp);
  189. child.pre_gather_tp = this.ctx.helper.add(child.pre_contract_tp, child.pre_qc_tp);
  190. child.end_contract_tp = this.ctx.helper.add(child.pre_contract_tp, child.contract_tp);
  191. child.end_qc_tp = this.ctx.helper.add(child.pre_qc_tp, child.qc_tp);
  192. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  193. child.final_tp = this.ctx.helper.add(child.total_price, child.end_qc_tp);
  194. child.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.final_tp, 4), 100);
  195. child.bgl_code = this.ctx.helper._.uniq(this.ctx.helper._.map(child.changes, 'c_code')).join(';');
  196. }
  197. _calculateNode(node, children) {
  198. for (const child of children) {
  199. node.total_price = this.ctx.helper.add(node.total_price, child.total_price);
  200. node.contract_tp = this.ctx.helper.add(node.contract_tp, child.contract_tp);
  201. node.qc_tp = this.ctx.helper.add(node.qc_tp, child.qc_tp);
  202. node.gather_tp = this.ctx.helper.add(node.gather_tp, child.gather_tp);
  203. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, child.pre_contract_tp);
  204. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, child.pre_qc_tp);
  205. node.pre_gather_tp = this.ctx.helper.add(node.pre_gather_tp, child.pre_gather_tp);
  206. node.end_contract_tp = this.ctx.helper.add(node.end_contract_tp, child.end_contract_tp);
  207. node.end_qc_tp = this.ctx.helper.add(node.end_qc_tp, child.end_qc_tp);
  208. node.end_gather_tp = this.ctx.helper.add(node.end_gather_tp, child.end_gather_tp);
  209. }
  210. node.final_tp = this.ctx.helper.add(node.total_price, node.end_qc_tp);
  211. node.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  212. }
  213. _recursiveCalcUnitNodes(nodes) {
  214. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  215. for (const node of nodes) {
  216. this._recursiveCalcUnitNodes(node.children);
  217. if (node.children && node.children.length > 0) {
  218. this._calculateNode(node, node.children);
  219. } else {
  220. this._calculateChild(node);
  221. }
  222. if (node.drawing_code && node.drawing_code.length > 0)
  223. node.drawing_code_merge = node.drawing_code.join(mergeChar);
  224. if (node.memo && node.memo.length > 0)
  225. node.memo_merge = node.memo.join(mergeChar);
  226. if (node.postil && node.postil.length > 0)
  227. node.postil_merge = node.postil.join(mergeChar);
  228. }
  229. }
  230. _recursiveCalculateAndSort(nodes) {
  231. const self = this;
  232. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  233. for (const node of nodes) {
  234. this._recursiveCalculateAndSort(node.children);
  235. if (node.unitTree) {
  236. node.unitTree.sortTreeNodeCustom('b_code', function (a, b) {
  237. const numReg = /^[0-9]+$/;
  238. const aCodes = a ? a.split('-') : [], bCodes = b ? b.split('-') : [];
  239. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  240. const iCompare = self.ctx.helper.compareCode(aCodes[i], bCodes[i]);
  241. if (iCompare !== 0) {
  242. return iCompare;
  243. }
  244. }
  245. }, false);
  246. this._recursiveCalcUnitNodes(node.unitTree.children);
  247. this._calculateNode(node, node.unitTree.children, this.tpFields);
  248. } else if (node.children && node.children.length > 0) {
  249. this._calculateNode(node, node.children, this.tpFields);
  250. } else {
  251. this._calculateChild(node);
  252. }
  253. }
  254. }
  255. _calculateAndSortResult() {
  256. this.resultTree.sortTreeNode();
  257. this._recursiveCalculateAndSort(this.resultTree.children);
  258. }
  259. _generateCodeByWbsCode(wbsCode) {
  260. let needUpdate = false;
  261. for (const node of this.resultTree.nodes) {
  262. if (!node.unitTree) continue;
  263. let xmj = wbsCode.find(function (x) {
  264. return x.xmj_code === node.code;
  265. });
  266. for (const unit of node.unitTree.nodes) {
  267. if (!unit.pos_name || unit.pos_name === '') continue;
  268. if (!xmj) {
  269. xmj = {xmj_code: node.code, pos: []};
  270. wbsCode.push(xmj);
  271. needUpdate = true;
  272. }
  273. let index = xmj.pos.indexOf(unit.pos_name);
  274. if (index < 0) {
  275. unit.code = xmj.xmj_code + splitChar + (xmj.pos.length + 1);
  276. xmj.pos.push(unit.pos_name);
  277. needUpdate = true;
  278. } else {
  279. unit.code = xmj.xmj_code + splitChar + (index + 1);
  280. }
  281. }
  282. }
  283. return needUpdate;
  284. }
  285. _getResultData(filterFun) {
  286. const result = this.resultTree.getDefaultDatas(filterFun);
  287. for (const r of result) {
  288. if (r.unitTree) {
  289. r.unitTreeData = r.unitTree.getDefaultDatas(filterFun);
  290. delete r.unitTree;
  291. }
  292. }
  293. return result;
  294. }
  295. // 转换数据
  296. convert(filter) {
  297. this._recursiveConvertNode(this.bpcTree.children);
  298. this._calculateAndSortResult();
  299. this._generateCodeByWbsCode([]);
  300. switch (filter) {
  301. case 'cur':
  302. return this._getResultData(function (node) {
  303. for (const field of ['contract_tp', 'qc_tp', 'gather_tp']) {
  304. if (node[field]) {
  305. return false;
  306. }
  307. }
  308. return true;
  309. });
  310. case 'end':
  311. return this._getResultData(function (node) {
  312. for (const field of ['end_contract_tp', 'end_qc_tp', 'end_gather_tp']) {
  313. if (node[field]) {
  314. return false;
  315. }
  316. }
  317. return true;
  318. });
  319. default:
  320. return this._getResultData();
  321. }
  322. }
  323. /**
  324. * 转换数据,会根据wbsCodeHis生成计量单元的code
  325. * 如果计量单元在wbsCodeHis中没有历史,则会更新传入的wbsCodeHis,并返回[result, true], 反之返回[result, false]
  326. * @param wbsCodeHis
  327. * @returns {*[]}
  328. */
  329. convertByWbsCode(wbsCodeHis) {
  330. this._recursiveConvertNode(this.bpcTree.children);
  331. this._calculateAndSortResult();
  332. const needUpdate = this._generateCodeByWbsCode(wbsCodeHis);
  333. return [this._getResultData(), needUpdate];
  334. }
  335. }
  336. module.exports = BillsPosConvert;