bills_calc.js 8.6 KB

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