bills_calc.js 12 KB

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