bills_pos_convert.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. _calcContractTp(node, data) {
  76. const info = this.ctx.tender.info;
  77. const correct = this.ctx.subProject.page_show.correctCalcContractTp;
  78. if (info.calc_type === 'tp') {
  79. let activeQty = this.ctx.helper.add(data.quantity, data.qc_minus_qty);
  80. let end_contract_qty = data.contract_qty;
  81. activeQty = this.ctx.helper.add(activeQty, data.pre_qc_minus_qty);
  82. end_contract_qty = this.ctx.helper.add(end_contract_qty, data.pre_contract_qty);
  83. const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), data.total_price, info.decimal.tp);
  84. return this.ctx.helper.sub(end_contract_tp, data.pre_contract_tp);
  85. } else if (info.calc_type === 'up') {
  86. if (correct) {
  87. let activeQty = this.ctx.helper.add(data.quantity, data.qc_minus_qty);
  88. let end_contract_qty = data.contract_qty;
  89. activeQty = this.ctx.helper.add(activeQty, data.pre_qc_minus_qty);
  90. end_contract_qty = this.ctx.helper.add(end_contract_qty, data.pre_contract_qty);
  91. const end_contract_tp = this.ctx.helper.mul(end_contract_qty, data.total_price, info.decimal.tp);
  92. return activeQty === end_contract_qty ? this.ctx.helper.sub(end_contract_tp, data.pre_contract_tp) : this.ctx.helper.mul(node.unit_price, data.contract_qty, info.decimal.tp);
  93. } else {
  94. return this.ctx.helper.mul(node.unit_price, data.contract_qty, info.decimal.tp);
  95. }
  96. }
  97. }
  98. _calcPreContractTp(node, data) {
  99. const info = this.ctx.tender.info;
  100. const org_price = node.org_price || node.unit_price;
  101. if (info.calc_type === 'tp') {
  102. const activeQty = this.ctx.helper.add(data.quantity, data.pre_qc_minus_qty);
  103. const orgTotalPrice = this.ctx.helper.mul(data.quantity, org_price, info.decimal.tp);
  104. return this.ctx.helper.mul(this.ctx.helper.div(data.pre_contract_qty, activeQty), orgTotalPrice, info.decimal.tp);
  105. } else if (info.calc_type === 'up') {
  106. return this.ctx.helper.mul(org_price, data.pre_contract_qty, info.decimal.tp);
  107. }
  108. }
  109. // v2
  110. _loadPosCalcFields(node, data) {
  111. const tpDecimal = this.ctx.tender.info.decimal.tp;
  112. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  113. data.total_price = this.ctx.helper.mul(node.unit_price, data.quantity, tpDecimal);
  114. node.total_price = this.ctx.helper.add(node.total_price, data.total_price);
  115. const org_price = node.org_price || node.unit_price;
  116. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  117. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  118. node.qc_tp = this.ctx.helper.add(node.qc_tp, this.ctx.helper.mul(node.unit_price, data.qc_qty, tpDecimal));
  119. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  120. data.pre_contract_tp = this._calcPreContractTp(node, data);
  121. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  122. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, data.pre_contract_tp);
  123. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  124. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, this.ctx.helper.mul(org_price, data.pre_qc_qty, tpDecimal));
  125. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  126. node.real_qty = this.ctx.helper.add(node.real_qty, data.real_qty);
  127. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, data.qc_minus_qty);
  128. node.pre_qc_minus_qty = this.ctx.helper.add(node.pre_qc_minus_qty, data.pre_qc_minus_qty);
  129. node.contract_tp = this.ctx.helper.add(node.contract_tp, this._calcContractTp(node, data));
  130. }
  131. _loadBillsCalcFields(node, data) {
  132. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  133. node.total_price = this.ctx.helper.add(node.total_price, data.total_price);
  134. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  135. node.contract_tp = this.ctx.helper.add(node.contract_tp, data.contract_tp);
  136. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  137. node.qc_tp = this.ctx.helper.add(node.qc_tp, data.qc_tp);
  138. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  139. node.gather_tp = this.ctx.helper.add(node.gather_tp, data.gather_tp);
  140. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  141. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, data.pre_contract_tp);
  142. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  143. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, data.pre_qc_tp);
  144. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  145. node.qc_minus_qty = this.ctx.helper.add(node.qc_minus_qty, data.qc_minus_qty);
  146. node.pre_qc_minus_qty = this.ctx.helper.add(node.pre_qc_minus_qty, data.pre_qc_minus_qty);
  147. }
  148. _convertGcl(node, xmj) {
  149. if (!xmj) return;
  150. if (!xmj.unitTree) {
  151. xmj.unitTree = new Ledger.filterGatherTree(this.ctx, this.resultTreeSetting);
  152. }
  153. const pos = this.bpcPos.getLedgerPos(node.id);
  154. if (pos && pos.length > 0) {
  155. for (const p of pos) {
  156. const posUnit = xmj.unitTree.addNode({pos_name: p.name,
  157. b_code: null, name: '', unit: null, unit_price: null,
  158. gxby_status: p.gxby_status, dagl_status: p.dagl_status, gxby_url: p.gxby_url, dagl_url: p.dagl_url});
  159. if (p.drawing_code && posUnit.drawing_code.indexOf(p.drawing_code) < 0)
  160. posUnit.drawing_code.push(p.drawing_code);
  161. if (p.memo) posUnit.memo.push(p.memo);
  162. if (p.postil) posUnit.postil.push(p.postil);
  163. if (p.ex_memo1) posUnit.ex_memo1.push(p.ex_memo1);
  164. if (p.ex_memo2) posUnit.ex_memo2.push(p.ex_memo2);
  165. if (p.ex_memo3) posUnit.ex_memo3.push(p.ex_memo3);
  166. const gclUnit = xmj.unitTree.addNode({pos_name: '',
  167. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price, org_price: node.org_price,
  168. }, posUnit);
  169. if (node.drawing_code && gclUnit.drawing_code.indexOf(node.drawing_code) < 0)
  170. gclUnit.drawing_code.push(node.drawing_code);
  171. if (node.memo) gclUnit.memo.push(node.memo);
  172. if (node.postil) gclUnit.postil.push(node.postil);
  173. if (node.ex_memo1) gclUnit.ex_memo1.push(node.ex_memo1);
  174. if (node.ex_memo2) gclUnit.ex_memo2.push(node.ex_memo2);
  175. if (node.ex_memo3) gclUnit.ex_memo3.push(node.ex_memo3);
  176. //loadField(gclUnit, p, baseCalcFields);
  177. this._loadPosCalcFields(gclUnit, p);
  178. if (!gclUnit.changes) gclUnit.changes = [];
  179. for (const c of this.bpcChange) {
  180. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  181. gclUnit.changes.push(c);
  182. }
  183. }
  184. }
  185. } else {
  186. const unit = xmj.unitTree.addNode({
  187. pos_name: '',
  188. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price, org_price: node.org_price,
  189. });
  190. if (node.drawing_code && unit.drawing_code.indexOf(node.drawing_code) < 0)
  191. unit.drawing_code.push(node.drawing_code);
  192. if (node.memo) unit.memo.push(node.memo);
  193. if (node.postil) unit.postil.push(node.postil);
  194. if (node.ex_memo1) unit.ex_memo1.push(node.ex_memo1);
  195. if (node.ex_memo2) unit.ex_memo2.push(node.ex_memo2);
  196. if (node.ex_memo3) unit.ex_memo3.push(node.ex_memo3);
  197. this._loadBillsCalcFields(unit, node);
  198. if (!unit.changes) unit.changes = [];
  199. for (const c of this.bpcChange) {
  200. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  201. unit.changes.push(c);
  202. }
  203. }
  204. }
  205. }
  206. _recursiveConvertNode(nodes, xmj = null) {
  207. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  208. for (const node of nodes) {
  209. if (node.b_code && node.b_code !== '') {
  210. if (!node.children || node.children.length === 0) {
  211. this._convertGcl(node, xmj);
  212. }
  213. this._recursiveConvertNode(node.children, xmj);
  214. } else {
  215. const curXmj = this._convertXmj(node);
  216. this._recursiveConvertNode(node.children, curXmj);
  217. if (!node.children || node.children.length === 0) {
  218. this._loadField(curXmj, node, this.tpFields);
  219. }
  220. }
  221. }
  222. }
  223. _calculateChild(child) {
  224. const tpDecimal = this.ctx.tender.info.decimal.tp;
  225. child.gather_qty = this.ctx.helper.add(child.contract_qty, child.qc_qty);
  226. child.pre_gather_qty = this.ctx.helper.add(child.pre_contract_qty, child.pre_qc_qty);
  227. child.end_contract_qty = this.ctx.helper.add(child.contract_qty, child.pre_contract_qty);
  228. child.end_qc_qty = this.ctx.helper.add(child.qc_qty, child.pre_qc_qty);
  229. child.end_gather_qty = this.ctx.helper.add(child.gather_qty, child.pre_gather_qty);
  230. child.estimate_qty = !this.ctx.helper.checkZero(child.real_qty)
  231. ? this.ctx.helper.sub(this.ctx.helper.sub(child.real_qty, child.quantity), child.end_qc_qty)
  232. : null;
  233. // v1
  234. // child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, tpDecimal);
  235. // child.contract_tp = this.ctx.helper.mul(child.unit_price, child.contract_qty, tpDecimal);
  236. // child.qc_tp = this.ctx.helper.mul(child.unit_price, child.qc_qty, tpDecimal);
  237. // child.gather_tp = this.ctx.helper.mul(child.unit_price, child.gather_qty, tpDecimal);
  238. // child.pre_contract_tp = this.ctx.helper.mul(child.unit_price, child.pre_contract_qty, tpDecimal);
  239. // child.pre_qc_tp = this.ctx.helper.mul(child.unit_price, child.pre_qc_qty, tpDecimal);
  240. // child.pre_gather_tp = this.ctx.helper.mul(child.unit_price, child.pre_gather_qty, tpDecimal);
  241. // child.end_contract_tp = this.ctx.helper.mul(child.unit_price, child.end_contract_qty, tpDecimal);
  242. // child.end_qc_tp = this.ctx.helper.mul(child.unit_price, child.end_qc_qty, tpDecimal);
  243. // child.end_gather_tp = this.ctx.helper.mul(child.unit_price, child.end_gather_qty, tpDecimal);
  244. // v2
  245. child.gather_tp = this.ctx.helper.add(child.contract_tp, child.qc_tp);
  246. child.pre_gather_tp = this.ctx.helper.add(child.pre_contract_tp, child.pre_qc_tp);
  247. child.end_contract_tp = this.ctx.helper.add(child.pre_contract_tp, child.contract_tp);
  248. child.end_qc_tp = this.ctx.helper.add(child.pre_qc_tp, child.qc_tp);
  249. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  250. child.final_tp = this.ctx.helper.add(child.total_price, child.end_qc_tp);
  251. child.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.final_tp, 4), 100);
  252. child.real_tp = this.ctx.helper.mul(child.real_qty, child.unit_price, tpDecimal);
  253. child.estimate_tp = this.ctx.helper.mul(child.estimate_qty, child.unit_price, tpDecimal);
  254. child.bgl_code = this.ctx.helper._.uniq(this.ctx.helper._.map(child.changes, 'c_code')).join(';');
  255. // !#台账相关
  256. child.end_qc_minus_qty = this.ctx.helper.add(child.pre_qc_minus_qty, child.qc_minus_qty); // 1#台账
  257. child.final_1_qty = this.ctx.helper.add(child.end_qc_minus_qty, child.quantity); // 1#台账 = 台账+负变更
  258. child.final_1_tp = this.ctx.helper.mul(child.unit_price, child.final_1_qty, tpDecimal);
  259. child.end_final_1_tp = this.ctx.helper.add(child.final_1_tp, child.end_qc_tp);
  260. child.end_final_1_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.end_final_1_tp, 4), 100);
  261. const priceDiff = child.org_price ? this.ctx.helper.sub(child.unit_price, child.org_price) : 0;
  262. if (priceDiff && (child.pre_contract_qty || child.pre_qc_qty)) {
  263. if (this.ctx.tender.info.calc_type === 'up') 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);
  264. 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);
  265. child.pc_tp = this.ctx.helper.add(child.contract_pc_tp, child.qc_pc_tp);
  266. child.gather_tp = this.ctx.helper.add(child.gather_tp, child.pc_tp);
  267. child.end_contract_tp = this.ctx.helper.add(child.end_contract_tp, child.contract_pc_tp);
  268. child.end_qc_tp = this.ctx.helper.add(child.end_qc_tp, child.qc_pc_tp);
  269. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  270. }
  271. }
  272. _calculateNode(node, children) {
  273. for (const child of children) {
  274. node.total_price = this.ctx.helper.add(node.total_price, child.total_price);
  275. node.contract_tp = this.ctx.helper.add(node.contract_tp, child.contract_tp);
  276. node.qc_tp = this.ctx.helper.add(node.qc_tp, child.qc_tp);
  277. node.pc_tp = this.ctx.helper.add(node.pc_tp, child.pc_tp);
  278. node.gather_tp = this.ctx.helper.add(node.gather_tp, child.gather_tp);
  279. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, child.pre_contract_tp);
  280. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, child.pre_qc_tp);
  281. node.pre_gather_tp = this.ctx.helper.add(node.pre_gather_tp, child.pre_gather_tp);
  282. node.end_contract_tp = this.ctx.helper.add(node.end_contract_tp, child.end_contract_tp);
  283. node.end_qc_tp = this.ctx.helper.add(node.end_qc_tp, child.end_qc_tp);
  284. node.end_gather_tp = this.ctx.helper.add(node.end_gather_tp, child.end_gather_tp);
  285. node.real_tp = this.ctx.helper.add(node.real_tp, child.real_tp);
  286. node.estimate_tp = this.ctx.helper.add(node.estimate_tp, child.estimate_tp);
  287. node.final_1_tp = this.ctx.helper.add(node.final_1_tp, child.final_1_tp);
  288. }
  289. if (node.dgn_qty1)
  290. node.dgn_price = this.ctx.helper.div(node.total_price, node.dgn_qty1, this.ctx.tender.info.decimal.up);
  291. node.final_tp = this.ctx.helper.add(node.total_price, node.end_qc_tp);
  292. node.end_final_1_tp = this.ctx.helper.add(node.final_1_tp, node.end_qc_tp);
  293. node.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  294. node.end_final_1_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.end_final_1_tp, 4), 100);
  295. }
  296. _recursiveCalcUnitNodes(nodes) {
  297. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  298. for (const node of nodes) {
  299. this._recursiveCalcUnitNodes(node.children);
  300. if (node.children && node.children.length > 0) {
  301. this._calculateNode(node, node.children);
  302. } else {
  303. this._calculateChild(node);
  304. }
  305. if (node.drawing_code && node.drawing_code.length > 0)
  306. node.drawing_code_merge = node.drawing_code.join(mergeChar);
  307. if (node.memo && node.memo.length > 0)
  308. node.memo_merge = node.memo.join(mergeChar);
  309. if (node.ex_memo1 && node.ex_memo1.length > 0)
  310. node.ex_memo1_merge = node.ex_memo1.join(mergeChar);
  311. if (node.ex_memo2 && node.ex_memo2.length > 0)
  312. node.ex_memo2_merge = node.ex_memo2.join(mergeChar);
  313. if (node.ex_memo3 && node.ex_memo3.length > 0)
  314. node.ex_memo3_merge = node.ex_memo3.join(mergeChar);
  315. if (node.postil && node.postil.length > 0)
  316. node.postil_merge = node.postil.join(mergeChar);
  317. }
  318. }
  319. _recursiveCalculateAndSort(nodes) {
  320. const self = this;
  321. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  322. for (const node of nodes) {
  323. this._recursiveCalculateAndSort(node.children);
  324. if (node.unitTree) {
  325. node.unitTree.sortTreeNodeCustom(function (a, b) {
  326. if (a.b_code && a.b_code !== '') {
  327. return b.b_code && b.b_code !== '' ? self.ctx.helper.compareCode(a.b_code, b.b_code) : 1;
  328. } else {
  329. return b.b_code && b.b_code !== '' ? -1 : a.order - b.order;
  330. }
  331. });
  332. this._recursiveCalcUnitNodes(node.unitTree.children);
  333. this._calculateNode(node, node.unitTree.children, this.tpFields);
  334. } else if (node.children && node.children.length > 0) {
  335. this._calculateNode(node, node.children, this.tpFields);
  336. } else {
  337. this._calculateChild(node);
  338. }
  339. }
  340. }
  341. _calculateAndSortResult() {
  342. this.resultTree.sortTreeNode();
  343. this._recursiveCalculateAndSort(this.resultTree.children);
  344. }
  345. _generateCodeByWbsCode(wbsCode) {
  346. let needUpdate = false;
  347. for (const node of this.resultTree.nodes) {
  348. if (!node.unitTree) continue;
  349. let xmj = wbsCode.find(function (x) {
  350. return x.xmj_code === node.code;
  351. });
  352. for (const unit of node.unitTree.nodes) {
  353. if (!unit.pos_name || unit.pos_name === '') continue;
  354. if (!xmj) {
  355. xmj = {xmj_code: node.code, pos: []};
  356. wbsCode.push(xmj);
  357. needUpdate = true;
  358. }
  359. let index = xmj.pos.indexOf(unit.pos_name);
  360. if (index < 0) {
  361. unit.code = xmj.xmj_code + splitChar + (xmj.pos.length + 1);
  362. xmj.pos.push(unit.pos_name);
  363. needUpdate = true;
  364. } else {
  365. unit.code = xmj.xmj_code + splitChar + (index + 1);
  366. }
  367. }
  368. }
  369. return needUpdate;
  370. }
  371. _getResultData(filterFun) {
  372. const result = this.resultTree.getDefaultDatas(filterFun);
  373. for (const r of result) {
  374. if (r.unitTree) {
  375. r.unitTreeData = r.unitTree.getDefaultDatas(filterFun);
  376. delete r.unitTree;
  377. }
  378. }
  379. return result;
  380. }
  381. // 转换数据
  382. convert(filter) {
  383. this._recursiveConvertNode(this.bpcTree.children);
  384. this._calculateAndSortResult();
  385. this._generateCodeByWbsCode([]);
  386. switch (filter) {
  387. case 'cur':
  388. return this._getResultData(function (node) {
  389. for (const field of ['contract_tp', 'qc_tp', 'gather_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp']) {
  390. if (node[field]) {
  391. return false;
  392. }
  393. }
  394. return true;
  395. });
  396. case 'end':
  397. return this._getResultData(function (node) {
  398. for (const field of ['end_contract_tp', 'end_qc_tp', 'end_gather_tp']) {
  399. if (node[field]) {
  400. return false;
  401. }
  402. }
  403. return true;
  404. });
  405. default:
  406. return this._getResultData();
  407. }
  408. }
  409. /**
  410. * 转换数据,会根据wbsCodeHis生成计量单元的code
  411. * 如果计量单元在wbsCodeHis中没有历史,则会更新传入的wbsCodeHis,并返回[result, true], 反之返回[result, false]
  412. * @param wbsCodeHis
  413. * @returns {*[]}
  414. */
  415. convertByWbsCode(wbsCodeHis) {
  416. this._recursiveConvertNode(this.bpcTree.children);
  417. this._calculateAndSortResult();
  418. const needUpdate = this._generateCodeByWbsCode(wbsCodeHis);
  419. return [this._getResultData(), needUpdate];
  420. }
  421. }
  422. module.exports = BillsPosConvert;