bills_pos_convert.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('./ledger');
  10. const splitChar = '-';
  11. class BillsPosConvert {
  12. constructor(ctx) {
  13. const self = this;
  14. this.ctx = ctx;
  15. this._ = this.ctx.helper._;
  16. this.tpFields = ['total_price', 'contract_tp', 'qc_tp', 'gather_tp',
  17. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp'];
  18. this.baseCalcFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty',
  19. 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  20. // mainData
  21. this.bpcTree = new Ledger.billsTree(this.ctx, {
  22. id: 'ledger_id',
  23. pid: 'ledger_pid',
  24. order: 'order',
  25. level: 'level',
  26. rootId: -1,
  27. keys: ['id', 'tender_id', 'ledger_id'],
  28. stageId: 'id',
  29. updateFields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'],
  30. calcFields: [],
  31. });
  32. this.bpcPos = new Ledger.pos({
  33. id: 'id', ledgerId: 'lid',
  34. updateFields: ['contract_qty', 'qc_qty'],
  35. });
  36. this.bpcChange = []
  37. // reusltData
  38. this.resultTreeSetting = {
  39. id: 'ledger_id',
  40. pid: 'ledger_pid',
  41. order: 'order',
  42. level: 'level',
  43. rootId: -1,
  44. fullPath: 'full_path',
  45. };
  46. this.resultTree = new Ledger.filterTree(this.ctx, this.resultTreeSetting);
  47. }
  48. // 加载基础数据
  49. _loadLedgerData (ledger) {
  50. const self = this;
  51. // 加载树结构数据
  52. this.bpcTree.loadDatas(ledger);
  53. }
  54. _loadPosData(pos) {
  55. this.bpcPos.loadDatas(pos);
  56. }
  57. loadData (ledger, pos, change) {
  58. this._loadLedgerData(ledger);
  59. this._loadPosData(pos);
  60. this.bpcChange = change;
  61. }
  62. _convertXmj(data) {
  63. const xmj = this.resultTree.addData(data, ['ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path',
  64. 'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'drawing_code', 'postil', 'memo']);
  65. return xmj;
  66. }
  67. // v1
  68. _loadField(node, data, fields) {
  69. for (const prop in data) {
  70. if (fields.indexOf(prop) >= 0) node[prop] = data[prop];
  71. }
  72. }
  73. // v2
  74. _loadCalcFields(node, data) {
  75. const tpDecimal = this.ctx.tender.info.decimal.tp;
  76. node.quantity = this.ctx.helper.add(node.quantity, data.quantity);
  77. node.total_price = this.ctx.helper.add(node.total_price, this.ctx.helper.mul(node.unit_price, data.quantity, tpDecimal));
  78. node.contract_qty = this.ctx.helper.add(node.contract_qty, data.contract_qty);
  79. node.contract_tp = this.ctx.helper.add(node.contract_tp, this.ctx.helper.mul(node.unit_price, data.contract_qty, tpDecimal));
  80. node.qc_qty = this.ctx.helper.add(node.qc_qty, data.qc_qty);
  81. node.qc_tp = this.ctx.helper.add(node.qc_tp, this.ctx.helper.mul(node.unit_price, data.qc_qty, tpDecimal));
  82. node.gather_qty = this.ctx.helper.add(node.gather_qty, data.gather_qty);
  83. node.pre_contract_qty = this.ctx.helper.add(node.pre_contract_qty, data.pre_contract_qty);
  84. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, this.ctx.helper.mul(node.unit_price, data.pre_contract_qty, tpDecimal));
  85. node.pre_qc_qty = this.ctx.helper.add(node.pre_qc_qty, data.pre_qc_qty);
  86. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, this.ctx.helper.mul(node.unit_price, data.pre_qc_qty, tpDecimal));
  87. node.pre_gather_qty = this.ctx.helper.add(node.pre_gather_qty, data.pre_gather_qty);
  88. }
  89. _convertGcl(node, xmj) {
  90. if (!xmj) return;
  91. if (!xmj.unitTree) {
  92. xmj.unitTree = new Ledger.filterGatherTree(this.ctx, this.resultTreeSetting);
  93. }
  94. const pos = this.bpcPos.getLedgerPos(node.id);
  95. if (pos && pos.length > 0) {
  96. for (const p of pos) {
  97. const posUnit = xmj.unitTree.addNode({pos_name: p.name,
  98. b_code: null, name: '', unit: null, unit_price: null});
  99. const gclUnit = xmj.unitTree.addNode({pos_name: '',
  100. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price
  101. }, posUnit);
  102. //loadField(gclUnit, p, baseCalcFields);
  103. this._loadCalcFields(gclUnit, p);
  104. if (!gclUnit.changes) gclUnit.changes = [];
  105. for (const c of this.bpcChange) {
  106. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  107. gclUnit.changes.push(c);
  108. }
  109. }
  110. }
  111. } else {
  112. const unit = xmj.unitTree.addNode({pos_name: null,
  113. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price});
  114. //loadField(unit, node, baseCalcFields);
  115. this._loadCalcFields(unit, node);
  116. if (!unit.changes) unit.changes = [];
  117. for (const c of this.bpcChange) {
  118. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  119. unit.changes.push(c);
  120. }
  121. }
  122. }
  123. }
  124. _recursiveConvertNode(nodes, xmj = null) {
  125. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  126. for (const node of nodes) {
  127. if (node.b_code && node.b_code !== '') {
  128. if (!node.children || node.children.length === 0) {
  129. this._convertGcl(node, xmj);
  130. }
  131. this._recursiveConvertNode(node.children, xmj);
  132. } else {
  133. const curXmj = this._convertXmj(node);
  134. this._recursiveConvertNode(node.children, curXmj);
  135. if (!node.children || node.children.length === 0) {
  136. this._loadField(curXmj, node, this.tpFields);
  137. }
  138. }
  139. }
  140. }
  141. _calculateChild(child) {
  142. //const tpDecimal = this.ctx.tender.info.decimal.tp;
  143. child.gather_qty = this.ctx.helper.add(child.contract_qty, child.qc_qty);
  144. child.pre_gather_qty = this.ctx.helper.add(child.pre_contract_qty, child.pre_gather_qty);
  145. child.end_contract_qty = this.ctx.helper.add(child.contract_qty, child.pre_contract_qty);
  146. child.end_qc_qty = this.ctx.helper.add(child.qc_qty, child.pre_qc_qty);
  147. child.end_gather_qty = this.ctx.helper.add(child.gather_qty, child.pre_gather_qty);
  148. // v1
  149. // child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, tpDecimal);
  150. // child.contract_tp = this.ctx.helper.mul(child.unit_price, child.contract_qty, tpDecimal);
  151. // child.qc_tp = this.ctx.helper.mul(child.unit_price, child.qc_qty, tpDecimal);
  152. // child.gather_tp = this.ctx.helper.mul(child.unit_price, child.gather_qty, tpDecimal);
  153. // child.pre_contract_tp = this.ctx.helper.mul(child.unit_price, child.pre_contract_qty, tpDecimal);
  154. // child.pre_qc_tp = this.ctx.helper.mul(child.unit_price, child.pre_qc_qty, tpDecimal);
  155. // child.pre_gather_tp = this.ctx.helper.mul(child.unit_price, child.pre_gather_qty, tpDecimal);
  156. // child.end_contract_tp = this.ctx.helper.mul(child.unit_price, child.end_contract_qty, tpDecimal);
  157. // child.end_qc_tp = this.ctx.helper.mul(child.unit_price, child.end_qc_qty, tpDecimal);
  158. // child.end_gather_tp = this.ctx.helper.mul(child.unit_price, child.end_gather_qty, tpDecimal);
  159. // v2
  160. child.gather_tp = this.ctx.helper.add(child.contract_tp, child.qc_tp);
  161. child.pre_gather_tp = this.ctx.helper.add(child.pre_contract_tp, child.pre_qc_tp);
  162. child.end_contract_tp = this.ctx.helper.add(child.pre_contract_tp, child.contract_tp);
  163. child.end_qc_tp = this.ctx.helper.add(child.pre_qc_tp, child.qc_tp);
  164. child.end_gather_tp = this.ctx.helper.add(child.end_contract_tp, child.end_qc_tp);
  165. child.final_tp = this.ctx.helper.add(child.total_price, child.end_qc_tp);
  166. child.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(child.end_gather_tp, child.final_tp, 4), 100);
  167. child.bgl_code = this.ctx.helper._.uniq(this.ctx.helper._.map(child.changes, 'c_code')).join(';');
  168. }
  169. _calculateNode(node, children) {
  170. for (const child of children) {
  171. node.total_price = this.ctx.helper.add(node.total_price, child.total_price);
  172. node.contract_tp = this.ctx.helper.add(node.contract_tp, child.contract_tp);
  173. node.qc_tp = this.ctx.helper.add(node.qc_tp, child.qc_tp);
  174. node.gather_tp = this.ctx.helper.add(node.gather_tp, child.gather_tp);
  175. node.pre_contract_tp = this.ctx.helper.add(node.pre_contract_tp, child.pre_contract_tp);
  176. node.pre_qc_tp = this.ctx.helper.add(node.pre_qc_tp, child.pre_qc_tp);
  177. node.pre_gather_tp = this.ctx.helper.add(node.pre_gather_tp, child.pre_gather_tp);
  178. node.end_contract_tp = this.ctx.helper.add(node.end_contract_tp, child.end_contract_tp);
  179. node.end_qc_tp = this.ctx.helper.add(node.end_qc_tp, child.end_qc_tp);
  180. node.end_gather_tp = this.ctx.helper.add(node.end_gather_tp, child.end_gather_tp);
  181. }
  182. node.final_tp = this.ctx.helper.add(node.total_price, node.end_qc_tp);
  183. node.end_gather_percent = this.ctx.helper.mul(this.ctx.helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  184. }
  185. _recursiveCalcUnitNodes(nodes) {
  186. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  187. for (const node of nodes) {
  188. this._recursiveCalcUnitNodes(node.children);
  189. if (node.children && node.children.length > 0) {
  190. this._calculateNode(node, node.children);
  191. } else {
  192. this._calculateChild(node);
  193. }
  194. }
  195. }
  196. _recursiveCalculateAndSort(nodes) {
  197. const self = this;
  198. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  199. for (const node of nodes) {
  200. this._recursiveCalculateAndSort(node.children);
  201. if (node.unitTree) {
  202. node.unitTree.sortTreeNodeCustom('b_code', function (a, b) {
  203. const numReg = /^[0-9]+$/;
  204. const aCodes = a ? a.split('-') : [], bCodes = b ? b.split('-') : [];
  205. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  206. const iCompare = self.ctx.helper.compareCode(aCodes[i], bCodes[i]);
  207. if (iCompare !== 0) {
  208. return iCompare;
  209. }
  210. }
  211. }, false);
  212. this._recursiveCalcUnitNodes(node.unitTree.children);
  213. this._calculateNode(node, node.unitTree.children, this.tpFields);
  214. } else if (node.children && node.children.length > 0) {
  215. this._calculateNode(node, node.children, this.tpFields);
  216. } else {
  217. this._calculateChild(node);
  218. }
  219. }
  220. }
  221. _calculateAndSortResult() {
  222. this.resultTree.sortTreeNode();
  223. this._recursiveCalculateAndSort(this.resultTree.children);
  224. }
  225. _generateCodeByWbsCode(wbsCode) {
  226. let needUpdate = false;
  227. for (const node of this.resultTree.nodes) {
  228. if (!node.unitTree) continue;
  229. let xmj = wbsCode.find(function (x) {
  230. return x.xmj_code === node.code;
  231. });
  232. for (const unit of node.unitTree.nodes) {
  233. if (!unit.pos_name || unit.pos_name === '') continue;
  234. if (!xmj) {
  235. xmj = {xmj_code: node.code, pos: []};
  236. wbsCode.push(xmj);
  237. needUpdate = true;
  238. }
  239. let index = xmj.pos.indexOf(unit.pos_name);
  240. if (index < 0) {
  241. unit.code = xmj.xmj_code + splitChar + (xmj.pos.length + 1);
  242. xmj.pos.push(unit.pos_name);
  243. needUpdate = true;
  244. } else {
  245. unit.code = xmj.xmj_code + splitChar + (index + 1);
  246. }
  247. }
  248. }
  249. return needUpdate;
  250. }
  251. _getResultData() {
  252. const result = this.resultTree.getDefaultDatas();
  253. for (const r of result) {
  254. if (r.unitTree) {
  255. r.unitTreeData = r.unitTree.getDefaultDatas();
  256. delete r.unitTree;
  257. }
  258. }
  259. return result;
  260. }
  261. // 转换数据
  262. convert() {
  263. this._recursiveConvertNode(this.bpcTree.children);
  264. this._calculateAndSortResult();
  265. return this._getResultData();
  266. }
  267. /**
  268. * 转换数据,会根据wbsCodeHis生成计量单元的code
  269. * 如果计量单元在wbsCodeHis中没有历史,则会更新传入的wbsCodeHis,并返回[result, true], 反之返回[result, false]
  270. * @param wbsCodeHis
  271. * @returns {*[]}
  272. */
  273. convertByWbsCode(wbsCodeHis) {
  274. this._recursiveConvertNode(this.bpcTree.children);
  275. this._calculateAndSortResult();
  276. const needUpdate = this._generateCodeByWbsCode(wbsCodeHis);
  277. return [this._getResultData(), needUpdate];
  278. }
  279. }
  280. module.exports = BillsPosConvert;