bills_calc.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. getBillsGLjs (node) {
  90. let rations = this.project.Ration.getBillsSortRation(node.source.getID());
  91. let gljs = this.project.ration_glj.getGatherGljArrByRations(rations);
  92. for (let glj of gljs) {
  93. glj.quantity = (glj.quantity / calcFees.getFee(node.data, 'quantity')).toDecimal(4);
  94. }
  95. return gljs;
  96. };
  97. calcLeaf (node, fields) {
  98. nodeCalcObj.node = node;
  99. nodeCalcObj.digit = this.digit;
  100. calcFees.checkFields(node.data, fields);
  101. let nodeCalc = nodeCalcObj, virData= null;
  102. // 清单单价:套用定额计算程序
  103. if (this.CalcFlag === billsPrice) {
  104. rationCalcObj.calcGljs = this.getBillsGLjs(node);
  105. console.log(rationCalcObj.calcGljs);
  106. rationCalcObj.calcFields = rationCalcFields;
  107. virData = rationCalcObj.calculate();
  108. }
  109. for (let field of fields) {
  110. nodeCalcObj.field = field;
  111. switch (field.unitFeeFlag) {
  112. case rationContentUnitFeeFlag:
  113. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.digit);
  114. break;
  115. case averageQtyUnitFeeFlag:
  116. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.digit);
  117. break;
  118. case billsPriceUnitFeeFlag:
  119. node.data.feesIndex[field.type].unitFee = virData[field.type];
  120. break;
  121. default:
  122. node.data.feesIndex[field.type].unitFee = 0;
  123. }
  124. switch (field.totalFeeFlag) {
  125. case sumTotalFeeFlag:
  126. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  127. break;
  128. case totalFeeFlag:
  129. node.data.feesIndex[field.type].totalFee = nodeCalcObj.totalFee().toDecimal(this.digit);
  130. break;
  131. default:
  132. node.data.feesIndex[field.type].totalFee = 0;
  133. }
  134. }
  135. };
  136. calcParent (node, fields) {
  137. nodeCalcObj.node = node;
  138. calcFees.checkFields(node.data, fields);
  139. for (let field of fields) {
  140. nodeCalcObj.field = field;
  141. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  142. }
  143. };
  144. calcNodes (nodes) {
  145. for (let node of nodes) {
  146. if (node.sourceType !== this.project.Bills.getSourceType()) {
  147. return;
  148. }
  149. if (node.source.children.length > 0) {
  150. this.calcNodes(node.children);
  151. this.calcParent(node, this.calcFieldName);
  152. } else {
  153. this.calcLeaf(node, this.calcFieldName);
  154. }
  155. }
  156. };
  157. calcAll () {
  158. this.calcNodes(this.project.mainTree.roots);
  159. };
  160. InitFields (fields) {
  161. for (let field of fields) {
  162. if (field.unitFee) return;
  163. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  164. field.unitFeeSplit = field.unitFee.split('.');
  165. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  166. field.totalFeeSplit = field.totalFee.split('.');
  167. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  168. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  169. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  170. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  171. }
  172. }
  173. }