bills_calc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.CalcFlag = CalcFlag;
  235. this.calcField = JSON.parse(JSON.stringify(feeType));
  236. for (let field of this.calcField) {
  237. // unitFeeCalcFlag
  238. if (this.calcFlag === rationContent) {
  239. field.unitFeeFlag = rationContentUnitFeeFlag;
  240. } else if ( this.calcFlag === billsPrice) {
  241. field.unitFeeFlag = billsPriceUnitFeeFlag;
  242. } else {
  243. field.unitFeeFlag = averageQtyUnitFeeFlag;
  244. }
  245. // totalFeeCalcFlag
  246. if (field.type === 'common') {
  247. if (this.CalcFlag === rationPriceConverse) {
  248. field.totalFeeFlag = sumTotalFeeFlag;
  249. } else {
  250. field.totalFeeFlag = totalFeeFlag;
  251. }
  252. } else {
  253. field.totalFeeFlag = sumTotalFeeFlag;
  254. }
  255. }
  256. this.InitFields(this.calcField);
  257. };
  258. getBillsGLjs (node) {
  259. let rations = this.project.Ration.getBillsSortRation(node.source.getID());
  260. let gljs = this.project.ration_glj.getGatherGljArrByRations(rations);
  261. for (let glj of gljs) {
  262. glj.quantity = (glj.quantity / calcFees.getFee(node.data, 'quantity')).toDecimal(4);
  263. }
  264. return gljs;
  265. };
  266. calcRationLeaf (node, fields) {
  267. nodeCalcObj.node = node;
  268. nodeCalcObj.digit = this.project.Decimal.unitFee;
  269. calcFees.checkFields(node.data, fields);
  270. let nodeCalc = nodeCalcObj, virData= null;
  271. // 清单单价:套用定额计算程序
  272. if (this.CalcFlag === billsPrice) {
  273. rationCalcObj.calcGljs = this.getBillsGLjs(node);
  274. console.log(rationCalcObj.calcGljs);
  275. rationCalcObj.calcFields = rationCalcFields;
  276. virData = rationCalcObj.calculate();
  277. }
  278. for (let field of fields) {
  279. nodeCalcObj.field = field;
  280. switch (field.unitFeeFlag) {
  281. case rationContentUnitFeeFlag:
  282. node.data.feesIndex[field.type].unitFee = nodeCalcObj.rationContentUnitFee().toDecimal(this.project.Decimal.common.unitFee);
  283. break;
  284. case averageQtyUnitFeeFlag:
  285. node.data.feesIndex[field.type].unitFee = nodeCalcObj.averageQty().toDecimal(this.project.Decimal.common.unitFee);
  286. break;
  287. case billsPriceUnitFeeFlag:
  288. node.data.feesIndex[field.type].unitFee = virData[field.type];
  289. break;
  290. default:
  291. node.data.feesIndex[field.type].unitFee = 0;
  292. }
  293. switch (field.totalFeeFlag) {
  294. case sumTotalFeeFlag:
  295. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.project.Decimal.common.totalFee);
  296. break;
  297. case totalFeeFlag:
  298. node.data.feesIndex[field.type].totalFee = nodeCalcObj.totalFee().toDecimal(this.project.Decimal.common.totalFee);
  299. break;
  300. default:
  301. node.data.feesIndex[field.type].totalFee = 0;
  302. }
  303. }
  304. };
  305. calcVolumePriceLeaf (node, fields) {
  306. let total = 0;
  307. for (let child of this.node.children) {
  308. total += this.getFee(child.data, 'feesIndex.common.totalFee');
  309. }
  310. };
  311. calcParent (node, fields) {
  312. nodeCalcObj.node = node;
  313. calcFees.checkFields(node.data, fields);
  314. for (let field of fields) {
  315. nodeCalcObj.field = field;
  316. node.data.feesIndex[field.type].totalFee = nodeCalcObj.sumTotalFee().toDecimal(this.project.Decimal.common.totalFee);
  317. }
  318. };
  319. calcNodes (nodes) {
  320. for (let node of nodes) {
  321. if (node.sourceType !== this.project.Bills.getSourceType()) {
  322. return;
  323. }
  324. if (node.source.children.length > 0) {
  325. this.calcNodes(node.children);
  326. this.calcParent(node, this.calcField);
  327. } else {
  328. if (node.children.length > 0) {
  329. if (node.firstChild().sourceType === this.project.Ration.getSourceType()) {
  330. this.calcRationLeaf(node, this.calcField);
  331. } else {
  332. this.calcVolumePriceLeaf(node, this.calcField);
  333. }
  334. } else {
  335. }
  336. }
  337. }
  338. };
  339. calcAll () {
  340. this.calcNodes(this.project.mainTree.roots);
  341. };
  342. InitFields (fields) {
  343. for (let field of fields) {
  344. if (field.unitFee) return;
  345. field.unitFee = 'feesIndex.' + field.type + '.unitFee';
  346. field.unitFeeSplit = field.unitFee.split('.');
  347. field.totalFee = 'feesIndex.' + field.type + '.totalFee';
  348. field.totalFeeSplit = field.totalFee.split('.');
  349. field.tenderUnitFee = 'feesIndex.'+ field.type + '.tenderUnitFee';
  350. field.tenderUnitFeeSplit = field.tenderUnitFee.split('.');
  351. field.tenderTotalFee = 'feesIndex.' + field.type + '.tenderTotalFee';
  352. field.tenderTotalFeeSplit = field.tenderTotalFee.split('.');
  353. }
  354. }
  355. }