bills_calc.js 13 KB

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