bills_pos_convert.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const billsPosConvertModel = (function () {
  10. // 需要汇总计算的字段
  11. const tpFields = ['total_price', 'contract_tp', 'qc_tp', 'gather_tp',
  12. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'end_contract_tp', 'end_qc_tp'];
  13. const baseCalcFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty',
  14. 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  15. // 基础数据
  16. const bpcTreeSetting = {
  17. id: 'ledger_id',
  18. pid: 'ledger_pid',
  19. order: 'order',
  20. level: 'level',
  21. rootId: -1,
  22. keys: ['id', 'tender_id', 'ledger_id'],
  23. stageId: 'id',
  24. updateFields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp']
  25. };
  26. const bpcTree = createNewPathTree('stage', bpcTreeSetting);
  27. const bpcPosSetting = {
  28. id: 'id', ledgerId: 'lid',
  29. updateFields: ['contract_qty', 'qc_qty'],
  30. };
  31. const bpcPos = new StagePosData(bpcPosSetting);
  32. let bpcChange, tpDecimal;
  33. // 结果
  34. const resultTreeSetting = {
  35. id: 'ledger_id',
  36. pid: 'ledger_pid',
  37. order: 'order',
  38. level: 'level',
  39. rootId: -1,
  40. fullPath: 'full_path',
  41. };
  42. const resultTree = createNewPathTree('filter', resultTreeSetting);
  43. // 加载基础数据
  44. function loadLedgerData (ledger) {
  45. // 加载树结构数据
  46. bpcTree.loadDatas(ledger);
  47. }
  48. function loadPosData(pos) {
  49. bpcPos.loadDatas(pos);
  50. }
  51. function loadData(ledger, pos, change, decimal) {
  52. loadLedgerData(ledger);
  53. loadPosData(pos);
  54. bpcChange = change;
  55. tpDecimal = decimal;
  56. }
  57. function convertXmj(data) {
  58. const xmj = resultTree.addData(data, ['ledger_id', 'ledger_pid', 'order', 'level', 'tender_id', 'full_path',
  59. 'code', 'name', 'unit', 'dgn_qty1', 'dgn_qty2', 'drawing_code', 'postil', 'memo']);
  60. return xmj;
  61. }
  62. // v1
  63. function loadField(node, data, fields) {
  64. for (const prop in data) {
  65. if (fields.indexOf(prop) >= 0) node[prop] = data[prop];
  66. }
  67. }
  68. // v2
  69. function loadCalcFields(node, data) {
  70. node.quantity = ZhCalc.add(node.quantity, data.quantity);
  71. node.total_price = ZhCalc.add(node.total_price, ZhCalc.mul(node.unit_price, data.quantity, tpDecimal));
  72. node.contract_qty = ZhCalc.add(node.contract_qty, data.contract_qty);
  73. node.contract_tp = ZhCalc.add(node.contract_tp, ZhCalc.mul(node.unit_price, data.contract_qty, tpDecimal));
  74. node.qc_qty = ZhCalc.add(node.qc_qty, data.qc_qty);
  75. node.qc_tp = ZhCalc.add(node.qc_tp, ZhCalc.mul(node.unit_price, data.qc_qty, tpDecimal));
  76. node.gather_qty = ZhCalc.add(node.gather_qty, data.gather_qty);
  77. node.pre_contract_qty = ZhCalc.add(node.pre_contract_qty, data.pre_contract_qty);
  78. node.pre_contract_tp = ZhCalc.add(node.pre_contract_tp, ZhCalc.mul(node.unit_price, data.pre_contract_qty, tpDecimal));
  79. node.pre_qc_qty = ZhCalc.add(node.pre_qc_qty, data.pre_qc_qty);
  80. node.pre_qc_tp = ZhCalc.add(node.pre_qc_tp, ZhCalc.mul(node.unit_price, data.pre_qc_qty, tpDecimal));
  81. node.pre_gather_qty = ZhCalc.add(node.pre_gather_qty, data.pre_gather_qty);
  82. node.end_contract_qty = ZhCalc.add(node.end_contract_qty, data.end_contract_qty);
  83. node.end_contract_tp = ZhCalc.add(node.end_contract_tp, ZhCalc.mul(node.unit_price, data.end_contract_qty, tpDecimal));
  84. node.end_qc_qty = ZhCalc.add(node.end_qc_qty, data.end_qc_qty);
  85. node.end_qc_tp = ZhCalc.add(node.end_qc_tp, ZhCalc.mul(node.unit_price, data.end_qc_qty, tpDecimal));
  86. node.end_gather_qty = ZhCalc.add(node.end_gather_qty, data.end_gather_qty);
  87. }
  88. function convertGcl(node, xmj) {
  89. if (!xmj) return;
  90. if (!xmj.unitTree) {
  91. xmj.unitTree = createNewPathTree('gather', resultTreeSetting);
  92. }
  93. const pos = bpcPos.getLedgerPos(node.id);
  94. if (pos && pos.length > 0) {
  95. for (const p of pos) {
  96. const posUnit = xmj.unitTree.addNode({pos_name: p.name,
  97. b_code: null, name: null, unit: null, unit_price: null});
  98. const gclUnit = xmj.unitTree.addNode({pos_name: null,
  99. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price
  100. }, posUnit);
  101. //loadField(gclUnit, p, baseCalcFields);
  102. loadCalcFields(gclUnit, p);
  103. if (!gclUnit.changes) gclUnit.changes = [];
  104. for (const c of bpcChange) {
  105. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  106. gclUnit.changes.push(c);
  107. }
  108. }
  109. }
  110. } else {
  111. const unit = xmj.unitTree.addNode({pos_name: null,
  112. b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price});
  113. //loadField(unit, node, baseCalcFields);
  114. loadCalcFields(unit, node);
  115. if (!unit.changes) unit.changes = [];
  116. for (const c of bpcChange) {
  117. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  118. unit.changes.push(c);
  119. }
  120. }
  121. }
  122. }
  123. function recursiveConvertNode(nodes, xmj = null) {
  124. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  125. for (const node of nodes) {
  126. if (node.b_code && node.b_code !== '') {
  127. if (!node.children || node.children.length === 0) {
  128. convertGcl(node, xmj);
  129. }
  130. recursiveConvertNode(node.children, xmj);
  131. } else {
  132. recursiveConvertNode(node.children, convertXmj(node));
  133. }
  134. }
  135. }
  136. function calculateChild(child) {
  137. child.gather_qty = ZhCalc.add(child.contract_qty, child.qc_qty);
  138. child.pre_gather_qty = ZhCalc.add(child.pre_contract_qty, child.pre_gather_qty);
  139. child.end_contract_qty = ZhCalc.add(child.contract_qty, child.pre_contract_qty);
  140. child.end_qc_qty = ZhCalc.add(child.qc_qty, child.pre_qc_qty);
  141. child.end_gather_qty = ZhCalc.add(child.gather_qty, child.pre_gather_qty);
  142. // v1
  143. // child.total_price = ZhCalc.mul(child.unit_price, child.quantity, tpDecimal);
  144. // child.contract_tp = ZhCalc.mul(child.unit_price, child.contract_qty, tpDecimal);
  145. // child.qc_tp = ZhCalc.mul(child.unit_price, child.qc_qty, tpDecimal);
  146. // child.gather_tp = ZhCalc.mul(child.unit_price, child.gather_qty, tpDecimal);
  147. // child.pre_contract_tp = ZhCalc.mul(child.unit_price, child.pre_contract_qty, tpDecimal);
  148. // child.pre_qc_tp = ZhCalc.mul(child.unit_price, child.pre_qc_qty, tpDecimal);
  149. // child.pre_gather_tp = ZhCalc.mul(child.unit_price, child.pre_gather_qty, tpDecimal);
  150. // child.end_contract_tp = ZhCalc.mul(child.unit_price, child.end_contract_qty, tpDecimal);
  151. // child.end_qc_tp = ZhCalc.mul(child.unit_price, child.end_qc_qty, tpDecimal);
  152. // child.end_gather_tp = ZhCalc.mul(child.unit_price, child.end_gather_qty, tpDecimal);
  153. // v2
  154. child.gather_tp = ZhCalc.add(child.contract_tp, child.qc_tp);
  155. child.pre_gather_tp = ZhCalc.add(child.pre_contract_tp, child.pre_qc_tp);
  156. child.end_gather_tp = ZhCalc.add(child.end_contract_tp, child.end_qc_tp);
  157. child.final_tp = ZhCalc.add(child.total_price, child.end_qc_tp);
  158. child.end_gather_percent = ZhCalc.mul(ZhCalc.div(child.end_gather_tp, child.final_tp, 4), 100);
  159. child.bgl_code = _.uniq(_.map(child.changes, 'c_code')).join(';');
  160. }
  161. function calculateNode(node, children) {
  162. for (const child of children) {
  163. node.total_price = ZhCalc.add(node.total_price, child.total_price);
  164. node.contract_tp = ZhCalc.add(node.contract_tp, child.contract_tp);
  165. node.qc_tp = ZhCalc.add(node.qc_tp, child.qc_tp);
  166. node.gather_tp = ZhCalc.add(node.gather_tp, child.gather_tp);
  167. node.pre_contract_tp = ZhCalc.add(node.pre_contract_tp, child.pre_contract_tp);
  168. node.pre_qc_tp = ZhCalc.add(node.pre_qc_tp, child.pre_qc_tp);
  169. node.pre_gather_tp = ZhCalc.add(node.pre_gather_tp, child.pre_gather_tp);
  170. node.end_contract_tp = ZhCalc.add(node.end_contract_tp, child.end_contract_tp);
  171. node.end_qc_tp = ZhCalc.add(node.end_qc_tp, child.end_qc_tp);
  172. node.end_gather_tp = ZhCalc.add(node.end_gather_tp, child.end_gather_tp);
  173. }
  174. node.final_tp = ZhCalc.add(node.total_price, node.end_qc_tp);
  175. node.end_gather_percent = ZhCalc.mul(ZhCalc.div(node.end_gather_tp, node.final_tp, 4), 100);
  176. }
  177. function recursiveCalcUnitNodes(nodes) {
  178. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  179. for (const node of nodes) {
  180. recursiveCalcUnitNodes(node.children);
  181. if (node.children && node.children.length > 0) {
  182. calculateNode(node, node.children);
  183. } else {
  184. calculateChild(node);
  185. }
  186. }
  187. }
  188. function recursiveCalculateAndSort(nodes) {
  189. if (!nodes || !nodes instanceof Array || nodes.length === 0) return;
  190. for (const node of nodes) {
  191. recursiveCalculateAndSort(node.children);
  192. if (node.unitTree) {
  193. node.unitTree.sortTreeNodeCustom('b_code', function (a, b) {
  194. function compareCode(code1, code2) {
  195. if (numReg.test(code1)) {
  196. if (numReg.test(code2)) {
  197. return _.toNumber(code1) - _.toNumber(code2);
  198. } else {
  199. return -1
  200. }
  201. } else {
  202. if (numReg.test(code2)) {
  203. return 1;
  204. } else {
  205. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1);
  206. }
  207. }
  208. }
  209. const numReg = /^[0-9]+$/;
  210. const aCodes = a ? a.split('-') : [], bCodes = b ? b.split('-') : [];
  211. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  212. const iCompare = compareCode(aCodes[i], bCodes[i]);
  213. if (iCompare !== 0) {
  214. return iCompare;
  215. }
  216. }
  217. }, false);
  218. recursiveCalcUnitNodes(node.unitTree.children);
  219. calculateNode(node, node.unitTree.children, tpFields);
  220. } else if (node.children && node.children.length > 0) {
  221. calculateNode(node, node.children, tpFields);
  222. }
  223. }
  224. }
  225. function CalculateAndSortResult() {
  226. resultTree.sortTreeNode();
  227. recursiveCalculateAndSort(resultTree.children);
  228. }
  229. // 转换数据
  230. function convert() {
  231. recursiveConvertNode(bpcTree.children);
  232. CalculateAndSortResult();
  233. return resultTree;
  234. }
  235. return { loadData, convert }
  236. })();