bills_calc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. let value = this.getFee(child.data, this.field.totalFee);
  40. if (Object.prototype.toString.apply(value) === "[object String]") {
  41. value = parseFloat(value);
  42. }
  43. result += value;
  44. }
  45. return result;
  46. },
  47. averageQty: function() {
  48. let result = 0, child, qty;
  49. result = this.sumTotalFee(this.field);
  50. qty = this.getFee(this.node.data, 'quantity');
  51. if (Object.prototype.toString.apply(qty) === "[object String]") {
  52. qty = parseFloat(qty);
  53. }
  54. if (qty !== 0) {
  55. result = result / qty;
  56. }
  57. return result;
  58. },
  59. totalFee: function () {
  60. return this.getFee(this.node.data, this.field.unitFee) * this.getFee(this.node.data, 'quantity');
  61. },
  62. rationContentUnitFee: function () {
  63. let result = 0, child, qty = this.getFee(this.node.data, 'quantity');
  64. if (Object.prototype.toString.apply(qty) === "[object String]") {
  65. qty = parseFloat(qty);
  66. }
  67. if (qty === 0) {
  68. qty = 1;
  69. }
  70. for (child of this.node.children) {
  71. let childUnitFee = this.getFee(child.data, this.field.unitFee);
  72. if (Object.prototype.toString.apply(childUnitFee) === "[object String]") {
  73. childUnitFee = parseFloat(childUnitFee);
  74. }
  75. let childQuantity = this.getFee(child.data, 'quantity');
  76. if (Object.prototype.toString.apply(childQuantity) === "[object String]") {
  77. childQuantity = parseFloat(childQuantity);
  78. }
  79. result += (childUnitFee * childQuantity / qty).toDecimal(this.digit);
  80. }
  81. return result;
  82. }
  83. };
  84. let baseCalcField = [
  85. {
  86. ID: 1,
  87. // 序号
  88. serialNo: '一',
  89. // 费用代号
  90. code: "A",
  91. // 名称
  92. name: "定额直接费",
  93. // 计算基数
  94. dispExpr: "A1+A2+A3",
  95. // 基数说明
  96. statement: "人工费+材料费+机械费",
  97. // 费率
  98. feeRate: null,
  99. // 费用类别
  100. type: 'RationDirect',
  101. // 备注
  102. memo: ''
  103. }, {
  104. ID: 2,
  105. // 序号
  106. serialNo: '1',
  107. // 费用代号
  108. code: "A1",
  109. // 名称
  110. name: "人工费",
  111. // 计算基数
  112. dispExpr: "H_J",
  113. // 基数说明
  114. statement: "合计",
  115. // 费率
  116. feeRate: 0,
  117. // 费用类别
  118. type: 'labour',
  119. // 备注
  120. memo: ''
  121. }, {
  122. ID: 3,
  123. // 序号
  124. serialNo: '2',
  125. // 费用代号
  126. code: "A2",
  127. // 名称
  128. name: "材料费",
  129. // 计算基数
  130. dispExpr: "H_J",
  131. // 基数说明
  132. statement: "合计",
  133. // 费率
  134. feeRate: 100,
  135. // 费用类别
  136. type: 'material',
  137. // 备注
  138. memo: ''
  139. }, {
  140. ID: 4,
  141. // 序号
  142. serialNo: '3',
  143. // 费用代号
  144. code: "A3",
  145. // 名称
  146. name: "机械费",
  147. // 计算基数
  148. dispExpr: "H_J",
  149. // 基数说明
  150. statement: "合计",
  151. // 费率
  152. feeRate: 0,
  153. // 费用类别
  154. type: 'machine',
  155. // 备注
  156. memo: ''
  157. }, {
  158. ID: 5,
  159. // 序号
  160. serialNo: '二',
  161. // 费用代号
  162. code: "A4",
  163. // 名称
  164. name: "管理费",
  165. // 计算基数
  166. dispExpr: "A",
  167. // 基数说明
  168. statement: "定额直接费",
  169. // 费率
  170. feeRate: 0,
  171. // 费用类别
  172. type: 'management',
  173. // 备注
  174. memo: ''
  175. }, {
  176. ID: 6,
  177. // 序号
  178. serialNo: '三',
  179. // 费用代号
  180. code: "B",
  181. // 名称
  182. name: "利润",
  183. // 计算基数
  184. dispExpr: "A",
  185. // 基数说明
  186. statement: "定额直接费",
  187. // 费率
  188. feeRate: 0,
  189. // 费用类别
  190. type: 'profit',
  191. // 备注
  192. memo: ''
  193. }, {
  194. ID: 7,
  195. // 序号
  196. serialNo: '四',
  197. // 费用代号
  198. code: "C",
  199. // 名称
  200. name: "风险费用",
  201. // 计算基数
  202. dispExpr: "",
  203. // 基数说明
  204. statement: "",
  205. // 费率
  206. feeRate: null,
  207. // 费用类别
  208. type: 'risk',
  209. // 备注
  210. memo: ''
  211. }, {
  212. ID: 8,
  213. // 序号
  214. serialNo: '',
  215. // 费用代号
  216. code: "",
  217. // 名称
  218. name: "综合单价",
  219. // 计算基数
  220. dispExpr: "A+B",
  221. // 基数说明
  222. statement: "定额直接费+利润",
  223. // 费率
  224. feeRate: NaN,
  225. // 费用类别
  226. type: 'common',
  227. // 备注
  228. memo: ''
  229. }
  230. ];
  231. class BillsCalcHelper {
  232. constructor (project, calcFlag) {
  233. this.project = project;
  234. this.InitFields(project.calcFields);
  235. };
  236. getBillsGLjs (node) {
  237. let rations = this.project.Ration.getBillsSortRation(node.source.getID());
  238. let gljs = this.project.ration_glj.getGatherGljArrByRations(rations);
  239. for (let glj of gljs) {
  240. glj.quantity = (glj.quantity / calcFees.getFee(node.data, 'quantity')).toDecimal(4);
  241. }
  242. return gljs;
  243. };
  244. calcRationLeaf (node, fields, isIncre) {
  245. nodeCalcObj.node = node;
  246. nodeCalcObj.digit = this.project.Decimal.unitFee;
  247. calcFees.checkFields(node.data, fields);
  248. let nodeCalc = nodeCalcObj, virData= null;
  249. // 清单单价:套用定额计算程序
  250. if (this.project.calcFlag === billsPrice) {
  251. rationCalcObj.calcGljs = this.getBillsGLjs(node);
  252. console.log(rationCalcObj.calcGljs);
  253. rationCalcObj.calcFields = rationCalcFields;
  254. virData = rationCalcObj.calculate();
  255. }
  256. for (let field of fields) {
  257. nodeCalcObj.field = field;
  258. switch (field.unitFeeFlag) {
  259. case rationContentUnitFeeFlag:
  260. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.project.Decimal.common.unitFee);
  261. break;
  262. case averageQtyUnitFeeFlag:
  263. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.project.Decimal.common.unitFee);
  264. break;
  265. case billsPriceUnitFeeFlag:
  266. node.data.feesIndex[field.type].unitFee = virData[field.type];
  267. break;
  268. default:
  269. node.data.feesIndex[field.type].unitFee = 0;
  270. }
  271. let value = 0;
  272. switch (field.totalFeeFlag) {
  273. case sumTotalFeeFlag:
  274. value = nodeCalcObj.sumTotalFee().toDecimal(this.project.Decimal.common.totalFee);
  275. break;
  276. case totalFeeFlag:
  277. value = nodeCalcObj.totalFee().toDecimal(this.project.Decimal.common.totalFee);
  278. break;
  279. default:
  280. value = 0;
  281. }
  282. this.setTotalFee(node, field, value, isIncre);
  283. }
  284. };
  285. calcVolumePriceLeaf (node, fields, isIncre) {
  286. let total = 0;
  287. for (let child of node.children) {
  288. total += calcFees.getFee(child.data, 'feesIndex.common.totalFee');
  289. }
  290. };
  291. calcParent (node, fields, isIncre) {
  292. nodeCalcObj.node = node;
  293. calcFees.checkFields(node.data, fields);
  294. for (let field of fields) {
  295. nodeCalcObj.field = field;
  296. let value = nodeCalcObj.sumTotalFee().toDecimal(this.project.Decimal.common.totalFee);
  297. this.setTotalFee(node, field, value, isIncre);
  298. }
  299. };
  300. calcNode(node, isIncre) {
  301. if (node.source.children.length > 0) {
  302. this.calcParent(node, this.project.calcFields, isIncre);
  303. } else {
  304. if (node.children.length > 0) {
  305. if (node.firstChild().sourceType === this.project.Ration.getSourceType()) {
  306. this.calcRationLeaf(node, this.project.calcFields, isIncre);
  307. } else {
  308. this.calcVolumePriceLeaf(node, this.project.calcFields, isIncre);
  309. }
  310. } else {
  311. }
  312. }
  313. };
  314. calcNodes (nodes) {
  315. for (let node of nodes) {
  316. if (node.sourceType !== this.project.Bills.getSourceType()) {
  317. return;
  318. }
  319. if (node.source.children.length > 0) {
  320. this.calcNodes(node.children);
  321. }
  322. this.calcNode(node);
  323. }
  324. };
  325. updateParent (parent, field, Incre) {
  326. if (parent && parent.sourceType === this.project.Bills.getSourceType()) {
  327. calcFees.checkFields(parent.data, [field]);
  328. parent.data.feesIndex[field.type].totalFee = (parent.data.feesIndex[field.type].totalFee + Incre).toDecimal(this.project.Decimal.common.totalFee);
  329. this.updateParent(parent.parent, field, Incre);
  330. }
  331. };
  332. setTotalFee (node, field, value, isIncre) {
  333. if (isIncre) {
  334. let incre = value - node.data.feesIndex[field.type].totalFee;
  335. node.data.feesIndex[field.type].totalFee = value;
  336. this.updateParent(node.parent, field, incre);
  337. } else {
  338. node.data.feesIndex[field.type].totalFee = value;
  339. }
  340. };
  341. converseCalc (node) {
  342. if (node && node.sourceType === this.project.Bills.getSourceType()) {
  343. this.calcNode(node);
  344. this.converseCalc(node.parent);
  345. }
  346. };
  347. calcAll () {
  348. this.calcNodes(this.project.mainTree.roots);
  349. };
  350. InitFields (fields) {
  351. for (let field of fields) {
  352. if (field.unitFee) return;
  353. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  354. field.unitFeeSplit = field.unitFee.split('.');
  355. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  356. field.totalFeeSplit = field.totalFee.split('.');
  357. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  358. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  359. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  360. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  361. }
  362. };
  363. }