bills_calc.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. findFee: function (fieldName) {
  36. if (!this.node.data.fees) {
  37. this.node.data.fees = [];
  38. }
  39. for (let fee of this.node.data.fees) {
  40. if (fee.fieldName === fieldName) {
  41. return fee;
  42. }
  43. }
  44. return null;
  45. },
  46. AddFee: function (fieldName) {
  47. let fee = {
  48. 'fieldName': fieldName,
  49. 'unitFee': 0,
  50. 'totalFee': 0,
  51. 'tenderUnitFee': 0,
  52. 'tenderTotalFee': 0
  53. };
  54. this.node.data.fees.push(fee);
  55. this.node.data.feesIndex[fieldName] = fee;
  56. },
  57. checkFields: function (fields) {
  58. for (let field of fields) {
  59. if (!this.findFee(field.type)) {
  60. this.AddFee(field.type);
  61. }
  62. }
  63. },
  64. getFee: function (data, fullField) {
  65. let fields = fullField.split('.'), value = data;
  66. for (let field of fields) {
  67. if (value[field]) {
  68. value = value[field];
  69. } else {
  70. return 0;
  71. }
  72. }
  73. return value;
  74. },
  75. getFeeSplit: function (data, fullFields) {
  76. let value = data;
  77. for (let field of fullFields) {
  78. if (value[field]) {
  79. value = value[field];
  80. } else {
  81. return 0;
  82. }
  83. }
  84. return value;
  85. },
  86. sumTotalFee: function() {
  87. let result = 0, child;
  88. for (child of this.node.children) {
  89. result += this.getFee(child.data, this.field.totalFee);
  90. }
  91. return result;
  92. },
  93. averageQty: function() {
  94. let result = 0, child, qty;
  95. result = this.sumTotalFee(this.field);
  96. qty = this.getFee(this.node.data, 'quantity');
  97. if (qty !== 0) {
  98. result = result / qty;
  99. }
  100. return result;
  101. },
  102. totalFee: function () {
  103. return this.getFee(this.node.data, this.field.unitFee) * this.getFee(this.node.data, 'quantity');
  104. },
  105. rationContentUnitFee: function () {
  106. let result = 0, child, qty = this.getFee(this.node.data, 'quantity');
  107. if (qty === 0) {
  108. qty = 1;
  109. }
  110. for (child of this.node.children) {
  111. result += (this.getFee(child.data, this.field.unitFee) * this.getFee(child.data, 'quantity') / qty).toDecimal(this.digit);
  112. }
  113. return result;
  114. }
  115. };
  116. class BillsCalc {
  117. constructor (project, CalcFlag) {
  118. this.project = project;
  119. this.CalcFlag = CalcFlag;
  120. this.digit = 2;
  121. switch (this.CalcFlag) {
  122. case rationContent:
  123. this.calcFieldName = rationContentCalcFields;
  124. break;
  125. case rationPrice:
  126. this.calcFieldName = rationPriceCalcFields;
  127. break;
  128. case rationPriceConverse:
  129. this.calcFieldName = rationPriceConverseCalcFields;
  130. break;
  131. case billsPrice:
  132. this.calcFieldName = billsPriceCalcFields;
  133. break;
  134. default:
  135. this.calcFieldName = [];
  136. }
  137. this.InitFields(this.calcFieldName);
  138. };
  139. calcLeaf (node, fields) {
  140. nodeCalcObj.node = node;
  141. nodeCalcObj.digit = this.digit;
  142. nodeCalcObj.checkFields(fields);
  143. let nodeCalc = nodeCalcObj;
  144. for (let field of fields) {
  145. nodeCalcObj.field = field;
  146. switch (field.unitFeeFlag) {
  147. case rationContentUnitFeeFlag:
  148. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.digit);
  149. break;
  150. case averageQtyUnitFeeFlag:
  151. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.digit);
  152. break;
  153. // to do billsPriceUnitFeeFlag(套用定额计算程序)
  154. // case billsPriceUnitFeeFlag:
  155. // break;
  156. default:
  157. node.data.feesIndex[field.type].unitFee = 0;
  158. }
  159. switch (field.totalFeeFlag) {
  160. case sumTotalFeeFlag:
  161. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  162. break;
  163. case totalFeeFlag:
  164. node.data.feesIndex[field.type].totalFee = nodeCalcObj.totalFee().toDecimal(this.digit);
  165. break;
  166. default:
  167. node.data.feesIndex[field.type].totalFee = 0;
  168. }
  169. }
  170. };
  171. calcParent (node, fields) {
  172. nodeCalcObj.node = node;
  173. nodeCalcObj.checkFields(fields);
  174. for (let field of fields) {
  175. nodeCalcObj.field = field;
  176. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.digit);
  177. }
  178. };
  179. calcNodes (nodes) {
  180. for (let node of nodes) {
  181. if (node.sourceType !== this.project.Bills.getSourceType()) {
  182. return;
  183. }
  184. if (node.source.children.length > 0) {
  185. this.calcNodes(node.children);
  186. this.calcParent(node, this.calcFieldName);
  187. } else {
  188. this.calcLeaf(node, this.calcFieldName);
  189. }
  190. }
  191. };
  192. calcAll () {
  193. this.calcNodes(this.project.mainTree.roots);
  194. };
  195. InitFields (fields) {
  196. for (let field of fields) {
  197. if (field.unitFee) return;
  198. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  199. field.unitFeeSplit = field.unitFee.split('.');
  200. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  201. field.totalFeeSplit = field.totalFee.split('.');
  202. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  203. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  204. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  205. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  206. }
  207. }
  208. }