bills_calc.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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.common.unitFee;
  261. calcFees.checkFields(node.data, fields);
  262. let nodeCalc = nodeCalcObj, virData= null, decimal = this.project.Decimal;
  263. // 清单单价:套用定额计算程序
  264. // if (this.project.projSetting.billsCalcMode === billsPrice) {
  265. /!* if (this.project.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  266. rationCalcObj.calcGljs = this.getBillsGLjs(node);
  267. console.log(rationCalcObj.calcGljs);
  268. rationCalcObj.calcFields = rationCalcFields;
  269. virData = rationCalcObj.calculate();
  270. }*!/
  271. for (let field of fields) {
  272. nodeCalcObj.field = field;
  273. switch (field.unitFeeFlag) {
  274. case rationContentUnitFeeFlag:
  275. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(decimal.common.unitFee);
  276. break;
  277. case averageQtyUnitFeeFlag:
  278. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(decimal.common.unitFee);
  279. break;
  280. case billsPriceUnitFeeFlag:
  281. node.data.feesIndex[field.type].unitFee = virData[field.type];
  282. break;
  283. case converseUnitFeeFlag:
  284. node.data.feesIndex[field.type].unitFee = nodeCalcObj.converseUnitFee(decimal.common.totalFee).toDecimal(decimal.common.unitFee);
  285. break;
  286. default:
  287. node.data.feesIndex[field.type].unitFee = 0;
  288. }
  289. let value = 0;
  290. switch (field.totalFeeFlag) {
  291. case sumTotalFeeFlag:
  292. value = nodeCalcObj.sumTotalFee().toDecimal(decimal.common.totalFee);
  293. break;
  294. case totalFeeFlag:
  295. value = nodeCalcObj.totalFee().toDecimal(decimal.common.totalFee);
  296. break;
  297. default:
  298. value = 0;
  299. }
  300. this.setTotalFee(node, field, value, isIncre);
  301. }
  302. };
  303. calcVolumePriceLeaf (node, fields, isIncre) {
  304. let total = 0;
  305. for (let child of node.children) {
  306. total += calcFees.getFee(child.data, 'feesIndex.common.totalFee');
  307. }
  308. };
  309. calcParent (node, fields, isIncre) {
  310. nodeCalcObj.node = node;
  311. calcFees.checkFields(node.data, fields);
  312. for (let field of fields) {
  313. nodeCalcObj.field = field;
  314. let value = nodeCalcObj.sumTotalFee().toDecimal(this.project.Decimal.common.totalFee);
  315. this.setTotalFee(node, field, value, isIncre);
  316. }
  317. };
  318. clearFeeFields(node, fields, isIncre) {
  319. for (let field of fields) {
  320. node.data.feesIndex[field.type].unitFee = 0;
  321. this.setTotalFee(node, field, 0, isIncre);
  322. }
  323. }
  324. calcNode(node, isIncre) {
  325. if (node.source.children.length > 0) {
  326. this.calcParent(node, this.project.calcFields, isIncre);
  327. } else {
  328. if (node.children.length > 0) {
  329. if (node.firstChild().sourceType === this.project.Ration.getSourceType()) {
  330. this.calcRationLeaf(node, this.project.calcFields, isIncre);
  331. } else {
  332. this.calcVolumePriceLeaf(node, this.project.calcFields, isIncre);
  333. }
  334. } else {
  335. this.clearFeeFields(node, this.project.calcFields, isIncre);
  336. }
  337. }
  338. };
  339. calcNodes (nodes) {
  340. for (let node of nodes) {
  341. if (node.sourceType !== this.project.Bills.getSourceType()) {
  342. return;
  343. }
  344. if (node.source.children.length > 0) {
  345. this.calcNodes(node.children);
  346. }
  347. this.calcNode(node);
  348. }
  349. };
  350. updateParent (parent, field, Incre) {
  351. if (parent && parent.sourceType === this.project.Bills.getSourceType()) {
  352. calcFees.checkFields(parent.data, [field]);
  353. parent.data.feesIndex[field.type].totalFee = (parent.data.feesIndex[field.type].totalFee + Incre).toDecimal(this.project.Decimal.common.totalFee);
  354. parent.data.feesIndex[field.type].unitFee = 0; // AAAAA 临时补上,使存储 unitFee.toFixed(2) 时不出错
  355. this.updateParent(parent.parent, field, Incre);
  356. }
  357. };
  358. setTotalFee (node, field, value, isIncre) {
  359. if (isIncre) {
  360. let incre = value - node.data.feesIndex[field.type].totalFee;
  361. node.data.feesIndex[field.type].totalFee = value;
  362. node.data.feesIndex[field.type].unitFee = 0; // AAAAA 临时补上,使存储 unitFee.toFixed(2) 时不出错
  363. this.updateParent(node.parent, field, incre);
  364. } else {
  365. node.data.feesIndex[field.type].totalFee = value;
  366. node.data.feesIndex[field.type].unitFee = 0; // AAAAA 临时补上,使存储 unitFee.toFixed(2) 时不出错
  367. }
  368. };
  369. converseCalc (node) {
  370. if (node && node.sourceType === this.project.Bills.getSourceType()) {
  371. this.calcNode(node);
  372. this.converseCalc(node.parent);
  373. }
  374. };
  375. calcAll () {
  376. this.calcNodes(this.project.mainTree.roots);
  377. };
  378. InitFields (fields) {
  379. for (let field of fields) {
  380. if (field.unitFee) return;
  381. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  382. field.unitFeeSplit = field.unitFee.split('.');
  383. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  384. field.totalFeeSplit = field.totalFee.split('.');
  385. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  386. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  387. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  388. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  389. }
  390. };
  391. }*/