bills_calc.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Created by Mai on 2017/7/5.
  3. */
  4. const rationContent = 0, rationPrice = 1, rationPriceConverse = 2, billsPrice = 3;
  5. const sumTotalFeeFlag = 0, totalFeeFlag = 1;
  6. const rationContentUnitFeeFlag = 0, averageQtyUnitFeeFlag = 1, billsPriceUnitFeeFlag = 2;
  7. let rationContentCalcFields = [
  8. {'type': 'common', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': totalFeeFlag},
  9. {'type': 'labour', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  10. {'type': 'material', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  11. {'type': 'machine', 'unitFeeFlag': rationContentUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  12. ];
  13. let rationPriceCalcFields = [
  14. {'type': 'common', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': totalFeeFlag},
  15. {'type': 'labour', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  16. {'type': 'material', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  17. {'type': 'machine', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  18. ];
  19. let rationPriceConverseCalcFields = [
  20. {'type': 'common', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  21. {'type': 'labour', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  22. {'type': 'material', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  23. {'type': 'machine', 'unitFeeFlag': averageQtyUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  24. ];
  25. let billsPriceCalcFields = [
  26. {'type': 'common', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': totalFeeFlag},
  27. {'type': 'labour', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  28. {'type': 'material', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  29. {'type': 'machine', 'unitFeeFlag': billsPriceUnitFeeFlag, 'totalFeeFlag': sumTotalFeeFlag},
  30. ];
  31. let nodeCalcObj = {
  32. node: null,
  33. digit: 2,
  34. field: null,
  35. getFee: calcFees.getFee,
  36. sumTotalFee: function() {
  37. let result = 0, child;
  38. for (child of this.node.children) {
  39. result += this.getFee(child.data, this.field.totalFee);
  40. }
  41. return result;
  42. },
  43. averageQty: function() {
  44. let result = 0, child, qty;
  45. result = this.sumTotalFee(this.field);
  46. qty = this.getFee(this.node.data, 'quantity');
  47. if (qty !== 0) {
  48. result = result / qty;
  49. }
  50. return result;
  51. },
  52. totalFee: function () {
  53. return this.getFee(this.node.data, this.field.unitFee) * this.getFee(this.node.data, 'quantity');
  54. },
  55. rationContentUnitFee: function () {
  56. let result = 0, child, qty = this.getFee(this.node.data, 'quantity');
  57. if (qty === 0) {
  58. qty = 1;
  59. }
  60. for (child of this.node.children) {
  61. result += (this.getFee(child.data, this.field.unitFee) * this.getFee(child.data, 'quantity') / qty).toDecimal(this.digit);
  62. }
  63. return result;
  64. }
  65. };
  66. class BillsCalc {
  67. constructor (project, CalcFlag) {
  68. this.project = project;
  69. this.CalcFlag = CalcFlag;
  70. this.digit = 2;
  71. switch (this.CalcFlag) {
  72. case rationContent:
  73. this.calcFieldName = rationContentCalcFields;
  74. break;
  75. case rationPrice:
  76. this.calcFieldName = rationPriceCalcFields;
  77. break;
  78. case rationPriceConverse:
  79. this.calcFieldName = rationPriceConverseCalcFields;
  80. break;
  81. case billsPrice:
  82. this.calcFieldName = billsPriceCalcFields;
  83. break;
  84. default:
  85. this.calcFieldName = [];
  86. }
  87. this.InitFields(this.calcFieldName);
  88. };
  89. calcLeaf (node, fields) {
  90. nodeCalcObj.node = node;
  91. nodeCalcObj.digit = this.digit;
  92. calcFees.checkFields(node.data, fields);
  93. let nodeCalc = nodeCalcObj, virData= null;
  94. // 清单单价:套用定额计算程序
  95. if (this.CalcFlag === billsPrice) {
  96. let rations = this.project.Ration.getBillsSortRation(node.source.getID());
  97. rationCalcObj.calcGljs = this.project.ration_glj.getGatherGljArrByRations(rations);
  98. rationCalcObj.calcFields = rationCalcFields;
  99. virData = rationCalcObj.calculate();
  100. }
  101. for (let field of fields) {
  102. nodeCalcObj.field = field;
  103. switch (field.unitFeeFlag) {
  104. case rationContentUnitFeeFlag:
  105. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.digit);
  106. break;
  107. case averageQtyUnitFeeFlag:
  108. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.digit);
  109. break;
  110. case billsPriceUnitFeeFlag:
  111. node.data.feesIndex[field.type].unitFee = virData[field.type];
  112. break;
  113. default:
  114. node.data.feesIndex[field.type].unitFee = 0;
  115. }
  116. switch (field.totalFeeFlag) {
  117. case sumTotalFeeFlag:
  118. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  119. break;
  120. case totalFeeFlag:
  121. node.data.feesIndex[field.type].totalFee = nodeCalcObj.totalFee().toDecimal(this.digit);
  122. break;
  123. default:
  124. node.data.feesIndex[field.type].totalFee = 0;
  125. }
  126. }
  127. };
  128. calcParent (node, fields) {
  129. nodeCalcObj.node = node;
  130. calcFees.checkFields(node.data, fields);
  131. for (let field of fields) {
  132. nodeCalcObj.field = field;
  133. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  134. }
  135. };
  136. calcNodes (nodes) {
  137. for (let node of nodes) {
  138. if (node.sourceType !== this.project.Bills.getSourceType()) {
  139. return;
  140. }
  141. if (node.source.children.length > 0) {
  142. this.calcNodes(node.children);
  143. this.calcParent(node, this.calcFieldName);
  144. } else {
  145. this.calcLeaf(node, this.calcFieldName);
  146. }
  147. }
  148. };
  149. calcAll () {
  150. this.calcNodes(this.project.mainTree.roots);
  151. };
  152. InitFields (fields) {
  153. for (let field of fields) {
  154. if (field.unitFee) return;
  155. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  156. field.unitFeeSplit = field.unitFee.split('.');
  157. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  158. field.totalFeeSplit = field.totalFee.split('.');
  159. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  160. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  161. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  162. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  163. }
  164. }
  165. }