bills_pos_convert.js 14 KB

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