calc_program.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /**
  2. * Created by CSL on 2017-07-19.
  3. * 计算程序。所有定额、清单、父清单的计算都从此入。
  4. */
  5. /* 新版GLD 取消了默认清单模板,所以这里废弃。先留着,预防不时之需。
  6. let defaultBillTemplate = {
  7. ID: 15,
  8. name: "清单公式",
  9. calcItems: [
  10. {
  11. ID: 1,
  12. code: "1",
  13. name: "定额直接费",
  14. dispExpr: "F2+F3+F4",
  15. statement: "人工费+材料费+机械费",
  16. feeRate: null,
  17. memo: ''
  18. },
  19. {
  20. ID: 2,
  21. code: "1.1",
  22. name: "人工费",
  23. dispExpr: "HJ",
  24. statement: "合计",
  25. feeRate: 50,
  26. fieldName: 'labour',
  27. memo: ''
  28. },
  29. {
  30. ID: 3,
  31. code: "1.2",
  32. name: "材料费",
  33. dispExpr: "HJ",
  34. statement: "合计",
  35. feeRate: 30,
  36. fieldName: 'material',
  37. memo: ''
  38. },
  39. {
  40. ID: 4,
  41. code: "1.3",
  42. name: "机械费",
  43. dispExpr: "HJ",
  44. statement: "合计",
  45. feeRate: 20,
  46. fieldName: 'machine',
  47. memo: ''
  48. },
  49. {
  50. ID: 5,
  51. code: "2",
  52. name: "企业管理费",
  53. dispExpr: "F1",
  54. statement: "定额直接费",
  55. feeRate: null,
  56. fieldName: 'manage',
  57. memo: ''
  58. },
  59. {
  60. ID: 6,
  61. code: "3",
  62. name: "利润",
  63. dispExpr: "F1",
  64. statement: "定额直接费",
  65. feeRate: null,
  66. fieldName: 'profit',
  67. memo: ''
  68. },
  69. {
  70. ID: 7,
  71. code: "4",
  72. name: "风险费用",
  73. dispExpr: "F1",
  74. statement: "定额直接费",
  75. feeRate: null,
  76. fieldName: 'risk',
  77. memo: ''
  78. },
  79. {
  80. ID: 8,
  81. code: "5",
  82. name: "综合单价",
  83. dispExpr: "F1+F5+F6+F7",
  84. statement: "定额直接费+企业管理费+利润+风险费用",
  85. feeRate: null,
  86. fieldName: 'common',
  87. memo: ''
  88. }
  89. ]
  90. };*/
  91. let calcTools = {
  92. getNodeByFlag: function (flag) {
  93. let bill = cbTools.findBill(flag);
  94. if (bill) return this.getNodeByID(bill.ID)
  95. else return null;
  96. },
  97. getNodeByID: function (ID){
  98. return cbTools.getNodeByID(ID);
  99. },
  100. isBill: function(treeNode){
  101. return treeNode.sourceType === ModuleNames.bills;
  102. },
  103. isParentBill: function (treeNode) {
  104. return this.isBill(treeNode) &&
  105. treeNode.source.children &&
  106. treeNode.source.children.length > 0;
  107. },
  108. isLeafBill: function(treeNode){
  109. return this.isBill(treeNode) &&
  110. treeNode.source.children &&
  111. treeNode.source.children.length === 0;
  112. },
  113. isNullBill: function (treeNode) {
  114. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  115. },
  116. isCalcBaseBill: function(treeNode){
  117. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (treeNode.data.calcBase);
  118. },
  119. isTotalCostBill: function (treeNode) {
  120. return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
  121. treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
  122. },
  123. isRationCategory: function(treeNode){
  124. return treeNode.sourceType === ModuleNames.ration;
  125. },
  126. isRationItem: function(treeNode){
  127. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.ration;
  128. },
  129. isVolumePrice: function (treeNode) {
  130. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.volumePrice;
  131. },
  132. isGljRation: function (treeNode) {
  133. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.gljRation;
  134. },
  135. isInheritFrom: function (treeNode, flagsArr){
  136. let cur = treeNode;
  137. while (cur.parent) {
  138. cur = cur.parent;
  139. };
  140. let flag = -1;
  141. if (cur.data.flagsIndex && cur.data.flagsIndex.fixed && cur.data.flagsIndex.fixed.flag)
  142. flag = cur.data.flagsIndex.fixed.flag;
  143. return flagsArr.includes(flag);
  144. },
  145. getGLJList: function (treeNode, needOneBill) {
  146. delete treeNode.data.gljList;
  147. if (this.isRationCategory(treeNode)) {
  148. if (treeNode.data.type != rationType.volumePrice) {
  149. treeNode.data.gljList = projectObj.project.ration_glj.getGljArrByRation(treeNode.data);
  150. };
  151. }
  152. else if (this.isBill(treeNode)){
  153. let nodeQ = this.uiNodeQty(treeNode);
  154. let q = nodeQ ? nodeQ : 1;
  155. let rNodes = projectObj.project.Ration.getRationNodes(treeNode);
  156. let rations = rNodes.map(function (node) {return node.data});
  157. treeNode.data.gljList = projectObj.project.ration_glj.getGatherGljArrByRations(rations, needOneBill, q);
  158. };
  159. },
  160. initFees: function (treeNode){
  161. if (!treeNode.data.fees) {
  162. treeNode.data.fees = [];
  163. treeNode.data.feesIndex = {};
  164. treeNode.changed = true;
  165. }
  166. else if (!treeNode.data.feesIndex){
  167. treeNode.data.feesIndex = {};
  168. for (let fee of treeNode.data.fees){
  169. treeNode.data.feesIndex[fee.fieldName] = fee;
  170. };
  171. treeNode.changed = true;
  172. };
  173. },
  174. initFeeField: function (treeNode, fieldName){
  175. this.initFees(treeNode);
  176. if (!treeNode.data.feesIndex[fieldName]) {
  177. let fee = {
  178. 'fieldName': fieldName,
  179. 'unitFee': 0,
  180. 'totalFee': 0,
  181. 'tenderUnitFee': 0,
  182. 'tenderTotalFee': 0
  183. };
  184. treeNode.data.fees.push(fee);
  185. treeNode.data.feesIndex[fieldName] = fee;
  186. treeNode.changed = true;
  187. };
  188. },
  189. checkFeeField: function (treeNode, feeObj){
  190. if (!feeObj) return;
  191. if (feeObj.fieldName == '') return;
  192. // 初始化前先拦截末定义的情况
  193. if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
  194. if (feeObj.unitFee == 0 && feeObj.totalFee == 0) return;
  195. }
  196. this.initFeeField(treeNode, feeObj.fieldName);
  197. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  198. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  199. treeNode.changed = true;
  200. };
  201. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  202. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  203. treeNode.changed = true;
  204. };
  205. },
  206. initSummaryFee: function (treeNode){
  207. if (!treeNode.data.summaryFees){
  208. treeNode.data.summaryFees = {
  209. totalFee: 0,
  210. estimateFee: 0,
  211. safetyFee: 0,
  212. chargeFee: 0
  213. };
  214. treeNode.changed = true;
  215. };
  216. },
  217. // 参数fieldName值: 'common.totalFee'、'equipment.unitFee'
  218. getFee: function (treeNode, fieldName) {
  219. if (!treeNode) return 0;
  220. let ns = fieldName.split(".");
  221. if (ns.length != 2)
  222. return 0
  223. else if (treeNode.data.feesIndex && treeNode.data.feesIndex[ns[0]] && treeNode.data.feesIndex[ns[0]][ns[1]])
  224. return parseFloat(treeNode.data.feesIndex[ns[0]][ns[1]])
  225. else
  226. return 0;
  227. },
  228. rationBaseFee: function (treeNode, gljTypes, priceType){
  229. if (!treeNode.data.gljList) return 0;
  230. let me = this, result = 0;
  231. let price = 0, temp = 0, temp2 = 0;
  232. for (let glj of treeNode.data.gljList) {
  233. if (gljTypes.indexOf(glj.type) >= 0) {
  234. /* if (priceType == priceTypes.ptDiffPrice){
  235. let aprice = me.uiGLJPrice(glj["adjustPrice"]);
  236. let mprice = me.uiGLJPrice(glj["marketPrice"]);
  237. temp = (me.uiGLJQty(glj["quantity"]) * mprice).toDecimal(decimalObj.process) - (me.uiGLJQty(glj["quantity"]) * aprice).toDecimal(decimalObj.process);
  238. temp = temp.toDecimal(decimalObj.process);
  239. }*/
  240. if (priceType == priceTypes.ptDiffPrice){
  241. let aprice = me.uiGLJPrice(glj["adjustPrice"]);
  242. let mprice = me.uiGLJPrice(glj["marketPrice"]);
  243. if (aprice != mprice){
  244. temp = (temp + (me.uiGLJQty(glj["quantity"]) * mprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  245. temp2 = (temp2 + (me.uiGLJQty(glj["quantity"]) * aprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  246. }
  247. }
  248. else {
  249. if (priceType == priceTypes.ptBasePrice){ price = me.uiGLJPrice(glj["basePrice"]);}
  250. else if (priceType == priceTypes.ptAdjustPrice){price = me.uiGLJPrice(glj["adjustPrice"]);}
  251. else if (priceType == priceTypes.ptMarketPrice){price = me.uiGLJPrice(glj["marketPrice"]);}
  252. temp = (me.uiGLJQty(glj["quantity"]) * price).toDecimal(decimalObj.process);
  253. result = (result + temp).toDecimal(decimalObj.process);
  254. };
  255. };
  256. };
  257. if (priceType == priceTypes.ptDiffPrice){
  258. result = (temp.toDecimal(decimalObj.ration.unitPrice) - temp2.toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  259. }
  260. else{
  261. result = result.toDecimal(decimalObj.ration.unitPrice);
  262. };
  263. return result;
  264. },
  265. machineLabourFee: function (gljArr) {
  266. if (!gljArr) return 0;
  267. let result = 0;
  268. for (let glj of gljArr) {
  269. if (glj.type == gljType.GENERAL_MACHINE) {
  270. // 获取机械组成物
  271. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  272. if (!mds) mds = [];
  273. let mdSum = 0;
  274. for (let md of mds) {
  275. if (md.type == gljType.MACHINE_LABOUR) {
  276. let q = md["consumption"] ? md["consumption"] : 0;
  277. let p = md["basePrice"] ? md["basePrice"] : 0;
  278. mdSum = mdSum + (q * p).toDecimal(decimalObj.ration.unitPrice);
  279. mdSum = (mdSum).toDecimal(decimalObj.ration.unitPrice);
  280. }
  281. }
  282. result = result + (glj["quantity"] * mdSum).toDecimal(decimalObj.ration.unitPrice);
  283. result = (result).toDecimal(decimalObj.ration.unitPrice);
  284. }
  285. }
  286. return result;
  287. },
  288. // 总造价清单、叶子清单、定额的暂估费。父清单是汇总子清单的暂估费,走计算程序逻辑,不在这里。
  289. estimateFee: function (treeNode, isBase = false){
  290. let me = this, sumU = 0, sumT = 0;
  291. let nodeQ = me.uiNodeQty(treeNode);
  292. let isGather = (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial);
  293. // 先汇总数量,再乘市场价。如果是叶子清单,进入这里的gljList中的材料,已经是同类材料跨定额汇总过的了。
  294. function eTFee(){
  295. if (!treeNode.data.gljList) return 0;
  296. let GLJObjs = [];
  297. for (let glj of treeNode.data.gljList) {
  298. if (!allMaterialTypes.includes(glj.type)) continue;
  299. if (glj.isEstimate){
  300. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type,
  301. // quantity: (nodeQ * glj.quantity).toDecimal(decimalObj.process),
  302. quantity: me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.process),
  303. marketPrice: glj.marketPrice});
  304. }
  305. else{ // 组成物
  306. if (!compositionTypes.includes(glj.type)) continue;
  307. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  308. if (!mds) mds = [];
  309. for (let md of mds){
  310. if (md.isEstimate){
  311. let isExist = false;
  312. // let totalQ = (nodeQ * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.glj.quantity);
  313. let totalQ = me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.glj.quantity);
  314. let mdQ = (totalQ * me.uiGLJQty(md.consumption)).toDecimal(decimalObj.process);
  315. for (let obj of GLJObjs){
  316. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  317. isExist = true;
  318. obj.quantity = (obj.quantity + mdQ).toDecimal(decimalObj.glj.quantity);
  319. break;
  320. }
  321. };
  322. if (!isExist)
  323. GLJObjs.push({code: md.code, name: md.name, specs: md.specs, unit: md.unit, type: md.type,
  324. quantity: mdQ, marketPrice: md.marketPrice});
  325. }
  326. }
  327. }
  328. };
  329. let rst = 0;
  330. for (let obj of GLJObjs){
  331. let tp = (me.uiGLJQty(obj.quantity) * me.uiGLJPrice(obj.marketPrice)).toDecimal(decimalObj.bills.totalPrice);
  332. rst = (rst + tp).toDecimal(decimalObj.bills.totalPrice);
  333. };
  334. return rst;
  335. };
  336. // 汇总子结点的暂估合价
  337. function eTFeeByChildren(){
  338. let rst = 0;
  339. for (let node of treeNode.children){
  340. if (node.data.feesIndex && node.data.feesIndex['estimate']) {
  341. rst = (rst + parseFloatPlus(node.data.feesIndex['estimate'].totalFee)).toDecimal(decimalObj.process);
  342. };
  343. };
  344. rst = (rst).toDecimal(decimalObj.bills.totalPrice);
  345. return rst;
  346. };
  347. // 先数量乘市场价,再汇总
  348. function eUFee(){
  349. if (!treeNode.data.gljList) return 0;
  350. let rst = 0;
  351. for (let glj of treeNode.data.gljList) {
  352. if (!allMaterialTypes.includes(glj.type)) continue;
  353. if (glj.isEstimate){
  354. rst = rst + (me.uiGLJQty(glj.quantity) * me.uiGLJPrice(glj.marketPrice)).toDecimal(decimalObj.process);
  355. rst = rst.toDecimal(decimalObj.process);
  356. }
  357. else{ // 组成物
  358. if (!compositionTypes.includes(glj.type)) continue;
  359. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  360. if (!mds) mds = [];
  361. for (let md of mds){
  362. if (!md.isEstimate) continue;
  363. let mdU = (me.uiGLJQty(md.consumption) * me.uiGLJPrice(md.marketPrice)).toDecimal(decimalObj.glj.unitPrice);
  364. rst = rst + (mdU * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.process);
  365. rst = rst.toDecimal(decimalObj.process);
  366. }
  367. }
  368. };
  369. rst = rst.toDecimal(decimalObj.bills.unitPrice);
  370. return rst;
  371. };
  372. // 总造价暂估费
  373. if (me.isTotalCostBill(treeNode)){
  374. let nodes = projectObj.project.mainTree.roots;
  375. for (let node of nodes){
  376. if (me.isTotalCostBill(node)) break;
  377. let eU = 0, eT = 0;
  378. if (node.data.feesIndex && node.data.feesIndex.estimate){
  379. eU = node.data.feesIndex.estimate.unitFee;
  380. eT = node.data.feesIndex.estimate.totalFee;
  381. }
  382. else {
  383. eU = 0, eT = 0;
  384. };
  385. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  386. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  387. };
  388. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  389. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  390. }
  391. else if (me.isParentBill(treeNode)){ // 父清单不要汇总单价。
  392. sumT = eTFeeByChildren();
  393. sumU = undefined;
  394. }
  395. else if (me.isLeafBill(treeNode)){
  396. if (isGather)
  397. sumT = eTFee()
  398. else
  399. sumT = eTFeeByChildren();
  400. let q = nodeQ ? nodeQ : 1;
  401. sumU = (sumT / q).toDecimal(decimalObj.bills.totalPrice);
  402. }
  403. else if (me.isRationCategory(treeNode)){
  404. sumU = eUFee();
  405. if (isBase) return sumU;
  406. if (isGather)
  407. sumT = eTFee()
  408. else
  409. sumT = (nodeQ * sumU).toDecimal(decimalObj.ration.totalPrice);
  410. };
  411. me.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
  412. },
  413. marketPriceToBase: function (treeNode, baseName) {
  414. if (treeNode.data.type != rationType.volumePrice && treeNode.data.type != rationType.gljRation) return;
  415. let result = 0, me = this;
  416. if (
  417. (treeNode.data.subType === gljType.LABOUR && baseName === calcBaseNames.DEJJRGF) ||
  418. (baseMaterialTypes.includes(treeNode.data.subType) && baseName === calcBaseNames.DEJJCLF) ||
  419. (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.DEJJJXF) ||
  420. (treeNode.data.subType === gljType.MAIN_MATERIAL && baseName === calcBaseNames.ZCF) ||
  421. (treeNode.data.subType === gljType.EQUIPMENT && baseName === calcBaseNames.SBF)) {
  422. if (treeNode.data.type == rationType.volumePrice)
  423. result = treeNode.data.marketUnitFee ? parseFloat(treeNode.data.marketUnitFee).toDecimal(decimalObj.ration.unitPrice) : 0
  424. else if (treeNode.data.type == rationType.gljRation)
  425. // result = treeNode.data.basePrice ? parseFloat(treeNode.data.basePrice).toDecimal(decimalObj.ration.unitPrice) : 0;
  426. // 这里因为是算基数所以要取基价,但不能直接取basePrice,受限于项目属性的三个选项。
  427. result = gljOprObj.getBasePrice(treeNode);
  428. }
  429. else if (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.DEJJJSRGF) {
  430. let glj = {
  431. 'code': treeNode.data.code,
  432. 'name': treeNode.data.name,
  433. 'specs': treeNode.data.specs,
  434. 'unit': treeNode.data.unit,
  435. 'quantity': 1,
  436. 'type': treeNode.data.subType // 注意:这里要取subType
  437. };
  438. result = me.machineLabourFee([glj]);
  439. }
  440. else if (
  441. (treeNode.data.type == rationType.gljRation) &&
  442. ((treeNode.data.subType === gljType.LABOUR && baseName === calcBaseNames.RGFJC) ||
  443. (baseMaterialTypes.includes(treeNode.data.subType) && baseName === calcBaseNames.CLFJC) ||
  444. (treeNode.data.subType === gljType.GENERAL_MACHINE && baseName === calcBaseNames.JXFJC))
  445. ) {
  446. let aprice = me.uiGLJPrice(treeNode.data.adjustPrice);
  447. let mprice = me.uiGLJPrice(treeNode.data.marketUnitFee);
  448. result = (mprice - aprice).toDecimal(decimalObj.ration.unitPrice);
  449. }
  450. return result;
  451. },
  452. partASupplyFee: function (treeNode, baseName) {
  453. if (!treeNode.data.gljList) return 0;
  454. let projectGLJ = projectObj.project.projectGLJ;
  455. let supplyT = [];
  456. if (baseName.includes('甲供'))
  457. supplyT = [supplyType.BFJG, supplyType.WQJG] // 字段中存储的是汉字、数字混杂!
  458. else if (baseName.includes('甲定'))
  459. supplyT = [supplyType.JDYG];
  460. let gljT = [], compT = [];
  461. if (baseName == calcBaseNames.JGDEJJRGF || baseName == calcBaseNames.JDDEJJRGF){
  462. gljT = [gljType.LABOUR];
  463. }
  464. else if (baseName == calcBaseNames.JGDEJJCLF || baseName == calcBaseNames.JDDEJJCLF){
  465. gljT = baseMaterialTypes;
  466. compT = compositionTypes;
  467. }
  468. else if (baseName == calcBaseNames.JGDEJJJXF || baseName == calcBaseNames.JDDEJJJXF){
  469. gljT = baseMachineTypes;
  470. compT = [gljType.GENERAL_MACHINE];
  471. }
  472. else if (baseName == calcBaseNames.JGZCF || baseName == calcBaseNames.JDZCF){
  473. gljT = [gljType.MAIN_MATERIAL];
  474. compT = [gljType.MAIN_MATERIAL];
  475. }
  476. else if (baseName == calcBaseNames.JGSBF || baseName == calcBaseNames.JDSBF){
  477. gljT = [gljType.EQUIPMENT];
  478. };
  479. // alert(JSON.stringify(projectGLJ.testGLJs()));
  480. let supplyGLJs = projectGLJ.getGLJsBySupply(supplyT, gljT);
  481. if (supplyGLJs.length == 0) return 0;
  482. let supplyGLJsIdx = {};
  483. for (let sglj of supplyGLJs){supplyGLJsIdx[sglj.id] = sglj};
  484. function isSupply(composition, supplies) {
  485. for (let supply of supplies){
  486. if(supply.code == composition.code && supply.name == composition.name && supply.unit == composition.unit &&
  487. supply.specs == composition.specs && supply.type == composition.type ){
  488. composition.basePrice = supply.unit_price.base_price;
  489. composition.supply = supply.supply;
  490. composition.supplyX = 1;
  491. if (composition.supply == supplyType.BFJG){
  492. let Q = supply.quantity ? supply.quantity : 1;
  493. composition.supplyX = supply.supply_quantity / Q;
  494. }
  495. return true;
  496. }
  497. };
  498. return false;
  499. };
  500. let sum = 0;
  501. for (let glj of treeNode.data.gljList){
  502. let X = 1; // 部分甲供系数(默认1,即完全甲供)
  503. let tempSGLJ = supplyGLJsIdx[glj.projectGLJID];
  504. // 当前材料是甲供材料:①普通 ②商品硂等不计组成物的母体材料
  505. if (tempSGLJ) {
  506. // 处理部分甲供
  507. if (baseName.includes('甲供') && (tempSGLJ.supply == supplyType.BFJG)){
  508. let Q = tempSGLJ.quantity ? tempSGLJ.quantity : 1;
  509. X = tempSGLJ.supply_quantity / Q;
  510. }
  511. sum = (sum + glj.basePrice * glj.quantity * X).toDecimal(decimalObj.process);
  512. }
  513. else{ // 当前材料不是甲供材料
  514. if (compT.includes(glj.type)) { // 混凝土等。组成物的母体,母体如果有组成物,则母体无法作为甲供材料,无法设置,此时要看其组成物是否是甲供材料;母体如果没有组成物,则母体有可能成为甲供材料。
  515. let pGLJ = projectGLJ.getDataByID(glj.projectGLJID);
  516. let compositions = pGLJ.ratio_data; // 组成物明细
  517. if (compositions.length > 0){
  518. for (let c of compositions){
  519. if (isSupply(c, supplyGLJs)) {
  520. X = 1;
  521. if (baseName.includes('甲供') && (c.supply == supplyType.BFJG)){
  522. X = c.supplyX;
  523. };
  524. sum = (sum + c.basePrice * c.consumption * glj.quantity * X).toDecimal(decimalObj.process);
  525. }
  526. }
  527. };
  528. }
  529. };
  530. };
  531. sum = sum.toDecimal(decimalObj.ration.unitPrice);
  532. return sum;
  533. },
  534. getCalcType: function (treeNode) {
  535. if (this.isRationCategory(treeNode)){
  536. return treeNodeCalcType.ctRationCalcProgram;
  537. }
  538. else if (this.isNullBill(treeNode)){
  539. return treeNodeCalcType.ctNull;
  540. }
  541. else if (this.isLeafBill(treeNode)) {
  542. if (treeNode.children && treeNode.children.length > 0){
  543. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  544. if (projectObj.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  545. return treeNodeCalcType.ctBillCalcProgram;
  546. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  547. return treeNodeCalcType.ctGatherRationsFees;
  548. }
  549. else{ // 公式计算
  550. return treeNodeCalcType.ctCalcBaseValue;
  551. };
  552. }
  553. else if (this.isBill(treeNode)) { // 父清单:汇总子清单的费用类别
  554. return treeNodeCalcType.ctGatherBillsFees;
  555. }
  556. else {
  557. return treeNodeCalcType.ctRationCalcProgram;
  558. };
  559. },
  560. cutNodeForSave(treeNode){
  561. let me = this;
  562. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  563. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  564. let data = {
  565. projectID: projectObj.project.ID(),
  566. ID: treeNode.data.ID,
  567. unit: treeNode.data.unit, //对清单来说,改变单位,工程量精度会跟着改变从而影响计算。
  568. subType: treeNode.data.subType,
  569. quantity: treeNode.data.quantity,
  570. calcBase: treeNode.data.calcBase,
  571. calcBaseValue: treeNode.data.calcBaseValue,
  572. programID: treeNode.data.programID,
  573. marketUnitFee: treeNode.data.marketUnitFee,
  574. marketTotalFee: treeNode.data.marketTotalFee,
  575. fees: treeNode.data.fees,
  576. isFromDetail:treeNode.data.isFromDetail,
  577. feeRate: treeNode.data.feeRate,
  578. feeRateID: treeNode.data.feeRateID,
  579. contain: treeNode.data.contain,
  580. quantityEXP: treeNode.data.quantityEXP,
  581. summaryFees: treeNode.data.summaryFees
  582. };
  583. // 定额大类
  584. if (me.isRationCategory(treeNode)) {
  585. data.isSubcontract = treeNode.data.isSubcontract;
  586. //定额类型的工料机做特殊处理
  587. if(me.isGljRation(treeNode)){
  588. data.code = treeNode.data.code;
  589. data.projectGLJID = treeNode.data.projectGLJID;
  590. delete data.marketUnitFee;
  591. };
  592. };
  593. // 优化掉 undefined 属性
  594. data = JSON.parse(JSON.stringify(data));
  595. return data;
  596. },
  597. uiNodeQty: function (treeNode){
  598. return parseFloatPlus(treeNode.data.quantity).toDecimal(decimalObj.decimal("quantity", treeNode));
  599. },
  600. // 界面显示的工料机价格,包括定额价、市场价等。参数 price 传入一个普通的价格数值即可。
  601. uiGLJPrice: function (price){
  602. if (price)
  603. return parseFloat(price).toDecimal(decimalObj.glj.unitPrice)
  604. else return 0;
  605. },
  606. // 界面显示的工料机数量。参数 quantity 传入一个普通的数量数值即可。
  607. uiGLJQty: function (quantity){
  608. if (quantity)
  609. return parseFloat(quantity).toDecimal(decimalObj.glj.quantity)
  610. else return 0;
  611. },
  612. getRationsByProjectGLJ(PGLJID){
  613. let rationIDs = [];
  614. let RGs = projectObj.project.ration_glj.datas;
  615. for (let rg of RGs){
  616. if (rg.projectGLJID == PGLJID){
  617. rationIDs.push(rg.rationID);
  618. }
  619. };
  620. let rationNodes = [];
  621. let nodes = projectObj.project.mainTree.nodes;
  622. for (let rID of rationIDs){
  623. rationNodes.push(nodes['id_' + rID]);
  624. };
  625. // 工料机形式的定额
  626. let items = projectObj.project.mainTree.items;
  627. for (let item of items){
  628. if (item.data.projectGLJID == PGLJID)
  629. rationNodes.push(item);
  630. };
  631. return rationNodes;
  632. },
  633. getNodesByProgramID(programID){
  634. let discreteNodes = [];
  635. let nodes = projectObj.project.mainTree.items;
  636. for (let node of nodes){
  637. if (node.data.programID == programID)
  638. discreteNodes.push(node);
  639. };
  640. return discreteNodes;
  641. }
  642. };
  643. const calcBaseNames = {
  644. DEJJRGF: '定额基价人工费',
  645. DEJJCLF: '定额基价材料费',
  646. DEJJJXF: '定额基价机械费',
  647. DEJJJSRGF: '定额基价机上人工费',
  648. RGFJC: '人工费价差',
  649. CLFJC: '材料费价差',
  650. JXFJC: '机械费价差',
  651. ZCF: '主材费',
  652. SBF: '设备费',
  653. RGGR: '人工工日',
  654. JGDEJJRGF: '甲供定额基价人工费',
  655. JGDEJJCLF: '甲供定额基价材料费',
  656. JGDEJJJXF: '甲供定额基价机械费',
  657. JGZCF: '甲供主材费',
  658. JGSBF: '甲供设备费',
  659. JDDEJJRGF: '甲定定额基价人工费',
  660. JDDEJJCLF: '甲定定额基价材料费',
  661. JDDEJJJXF: '甲定定额基价机械费',
  662. JDZCF: '甲定主材费',
  663. JDSBF: '甲定设备费',
  664. ZGCLF: '暂估材料费',
  665. FBDEJJRGF: '分包定额基价人工费',
  666. FBDEJJCLF: '分包定额基价材料费',
  667. FBDEJJJXF: '分包定额基价机械费',
  668. FBZCF: '分包主材费',
  669. FBSBF: '分包设备费',
  670. FBRGGR: '分包人工工日'
  671. };
  672. const rationCalcBases = {
  673. '定额基价人工费': function (node) {
  674. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice);
  675. },
  676. '定额基价材料费': function (node) {
  677. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice);
  678. },
  679. '定额基价机械费': function (node) {
  680. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice);
  681. },
  682. '定额基价机上人工费': function (node) {
  683. return calcTools.machineLabourFee(node.data.gljList);
  684. },
  685. '人工费价差': function (node) {
  686. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptDiffPrice);
  687. },
  688. '材料费价差': function (node) {
  689. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptDiffPrice);
  690. },
  691. '机械费价差': function (node) {
  692. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptDiffPrice);
  693. },
  694. '主材费': function (node) {
  695. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptMarketPrice);
  696. },
  697. '设备费': function (node) {
  698. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptMarketPrice);
  699. },
  700. '人工工日': function (node) {
  701. if (!node.data.gljList) return 0;
  702. let rst = 0;
  703. for (let glj of node.data.gljList) {
  704. if (glj.type == gljType.LABOUR) {
  705. rst = rst + (calcTools.uiGLJQty(glj["quantity"]) * calcTools.uiNodeQty(node)).toDecimal(decimalObj.process);
  706. rst = rst.toDecimal(decimalObj.process);
  707. }
  708. };
  709. return rst.toDecimal(decimalObj.glj.quantity);
  710. },
  711. '甲供定额基价人工费': function (node) {
  712. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJRGF);
  713. },
  714. '甲供定额基价材料费': function (node) {
  715. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJCLF);
  716. },
  717. '甲供定额基价机械费': function (node) {
  718. return calcTools.partASupplyFee(node, calcBaseNames.JGDEJJJXF);
  719. },
  720. '甲供主材费': function (node) {
  721. return calcTools.partASupplyFee(node, calcBaseNames.JGZCF);
  722. },
  723. '甲供设备费': function (node) {
  724. return calcTools.partASupplyFee(node, calcBaseNames.JGSBF);
  725. },
  726. '甲定定额基价人工费': function (node) {
  727. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJRGF);
  728. },
  729. '甲定定额基价材料费': function (node) {
  730. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJCLF);
  731. },
  732. '甲定定额基价机械费': function (node) {
  733. return calcTools.partASupplyFee(node, calcBaseNames.JDDEJJJXF);
  734. },
  735. '甲定主材费': function (node) {
  736. return calcTools.partASupplyFee(node, calcBaseNames.JDZCF);
  737. },
  738. '甲定设备费': function (node) {
  739. return calcTools.partASupplyFee(node, calcBaseNames.JDSBF);
  740. },
  741. '暂估材料费': function (node) {
  742. return calcTools.estimateFee(node, true);
  743. },
  744. '分包定额基价人工费': function (node) {
  745. if (node.data.isSubcontract) return this.定额基价人工费(node)
  746. else return 0;
  747. },
  748. '分包定额基价材料费': function (node) {
  749. if (node.data.isSubcontract) return this.定额基价材料费(node)
  750. else return 0;
  751. },
  752. '分包定额基价机械费': function (node) {
  753. if (node.data.isSubcontract) return this.定额基价机械费(node)
  754. else return 0;
  755. },
  756. '分包主材费': function (node) {
  757. if (node.data.isSubcontract) return this.主材费(node)
  758. else return 0;
  759. },
  760. '分包设备费': function (node) {
  761. if (node.data.isSubcontract) return this.设备费(node)
  762. else return 0;
  763. },
  764. '分包人工工日': function (node) {
  765. if (node.data.isSubcontract) return this.人工工日(node)
  766. else return 0;
  767. }
  768. };
  769. let analyzer = {
  770. standard: function(expr){
  771. let str = expr;
  772. str = str.replace(/\s/g, ""); // 去空格、去中文空格
  773. str = str.replace(/(/g, "("); // 中文括号"("换成英文括号"("
  774. str = str.replace(/)/g, ")"); // 中文括号")"换成英文括号")"
  775. str = str.replace(/f/g, "F"); // f换成F
  776. str = str.replace(/l/g, "L"); // l换成L
  777. return str;
  778. },
  779. getFArr: function (expr) {
  780. let pattF = new RegExp(/F\d+/gi);
  781. let arrF = expr.match(pattF);
  782. return arrF ? arrF : [];
  783. },
  784. getAtIDArr: function (expr) { // ['@1','@2']
  785. let patt = new RegExp(/@\d+/gi);
  786. let arr = expr.match(patt);
  787. return arr ? arr : [];
  788. },
  789. getBaseArr: function (expr) {
  790. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  791. let arrBase = expr.match(pattBase);
  792. return arrBase ? arrBase : [];
  793. },
  794. getID: function (FName, template) { // F13 → 4
  795. let idx = FName.slice(1) - 1;
  796. let id = template.calcItems[idx] ? template.calcItems[idx].ID : null;
  797. return id;
  798. },
  799. getFName: function (atID, template) { // @3 → F7
  800. for (var i = 0; i < template.calcItems.length; i++) {
  801. if (template.calcItems[i].ID == atID.slice(1)) {
  802. return 'F'+ (i + 1);
  803. }
  804. }
  805. return null;
  806. },
  807. getItem: function (FName, template){ // F3 → calcItems[2] → {Object}
  808. let idx = FName.slice(1) - 1;
  809. return template.calcItems[idx];
  810. },
  811. isCycleCalc: function (expression, ID, template) { // 这里判断expression,是ID引用: @5+@6
  812. if (expression.includes(`@${ID}`))
  813. return true;
  814. let atIDs = analyzer.getAtIDArr(expression);
  815. for (let atID of atIDs) {
  816. let item = template.compiledCalcItems[atID.slice(1)];
  817. if (analyzer.isCycleCalc(item.expression, ID, template))
  818. return true;
  819. }
  820. return false;
  821. },
  822. isLegal: function (dispExpr, itemID, template) { // 检测包括:无效字符、基数是否中括号、基数是否定义、行引用、循环计算
  823. function testValue(expr){
  824. try {
  825. expr = expr.replace(/\[[\u4E00-\u9FA5]+\]/gi, '0');
  826. expr = expr.replace(/@\d+/gi, '0');
  827. if (expr.includes('00'))
  828. return false;
  829. eval(expr);
  830. return true;
  831. }
  832. catch (err) {
  833. return false;
  834. }
  835. };
  836. let me = analyzer;
  837. let expr = me.standard(dispExpr);
  838. let invalidChars = /[^0-9\u4e00-\u9fa5\+\-\*\/\(\)\.\[\]FL%]/g;
  839. if (invalidChars.test(expr)){
  840. hintBox.infoBox('错误提示',`表达式中含有${hintBox.font('无效字符')}!`,1);
  841. return false;
  842. };
  843. let i = expr.search(/\df/ig); // 4F3
  844. let j = expr.search(/\d\[/ig); // 4[定额基价人工费]
  845. if (i > -1 || j > -1){
  846. hintBox.infoBox('错误提示', `表达式中${hintBox.font('缺少运算符')}!`, 1);
  847. return false;
  848. }
  849. let pattCn = new RegExp(/[\u4E00-\u9FA5]+/gi);
  850. let arrCn = expr.match(pattCn);
  851. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  852. let arrBase = expr.match(pattBase);
  853. if (arrCn && !arrBase){
  854. hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  855. return false;
  856. };
  857. if (arrCn && arrBase && (arrCn.length != arrBase.length)){
  858. for (let cn of arrCn){
  859. let tempBase = `[${cn}]`;
  860. if (!arrBase.includes(tempBase)){
  861. hintBox.infoBox('错误提示', `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`, 1);
  862. return false;
  863. }
  864. };
  865. // 这里要加一个保险。因为上面的 for 循环在“主材费 + [主材费]” 情况下有Bug
  866. hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  867. return false;
  868. };
  869. if (arrBase){
  870. for (let base of arrBase){
  871. let baseName = base.slice(1, -1);
  872. if (!rationCalcBases[baseName]){
  873. hintBox.infoBox('错误提示', `定额基数${hintBox.font('[' +baseName + ']')}末定义!`, 1);
  874. return false;
  875. }
  876. };
  877. };
  878. let arrF = me.getFArr(expr);
  879. for (let F of arrF){
  880. let num = F.slice(1);
  881. if (num > template.calcItems.length){
  882. let s = hintBox.font('F'+num);
  883. hintBox.infoBox('错误提示', `表达式中 “${hintBox.font(s)}” 行号引用错误!`, 1);
  884. return false;
  885. };
  886. };
  887. let expression = me.getExpression(expr, template);
  888. if (me.isCycleCalc(expression, itemID, template)){
  889. hintBox.infoBox('错误提示', `表达式中有${hintBox.font('循环计算')}!`, 1);
  890. return false;
  891. };
  892. if (!testValue(expression)){
  893. hintBox.infoBox('错误提示', `表达式中有${hintBox.font('语法错误')}!`, 1);
  894. return false;
  895. };
  896. return true; // 表达式合法
  897. },
  898. refIDToLines: function (expression, template) { // @2、@3 → F7、F8
  899. let rst = expression;
  900. let atIDArr = analyzer.getAtIDArr(rst);
  901. let fArr = [];
  902. for (let atID of atIDArr) {
  903. let FN = analyzer.getFName(atID, template);
  904. fArr.push(FN);
  905. };
  906. for (let i = 0; i < atIDArr.length; i++) {
  907. let patt = new RegExp(atIDArr[i]);
  908. let val = fArr[i];
  909. rst = rst.replace(patt, val);
  910. };
  911. return rst;
  912. },
  913. refLineToIDs: function (dispExpr, template) { // F7、F8 → @2、@3
  914. let rst = analyzer.standard(dispExpr);
  915. let fArr = analyzer.getFArr(rst);
  916. let IDArr = [];
  917. for (let F of fArr) {
  918. let ID = analyzer.getID(F, template);
  919. IDArr.push(ID);
  920. };
  921. for (let i = 0; i < fArr.length; i++) {
  922. let patt = new RegExp(fArr[i]);
  923. let val = `@${IDArr[i]}`;
  924. rst = rst.replace(patt, val);
  925. };
  926. return rst;
  927. },
  928. getDispExpr: function (expression, template) {
  929. return analyzer.refIDToLines(expression, template);
  930. },
  931. getExpression: function (dispExpr, template) {
  932. return analyzer.refLineToIDs(dispExpr, template);
  933. },
  934. getDispExprUser: function (dispExpr, labourCoe) { // labourCoe 是 str 类型
  935. let rst = analyzer.standard(dispExpr);
  936. rst = rst.replace(/L/g, labourCoe);
  937. return rst;
  938. },
  939. getCompiledExpr: function (expression, labourCoe) { // labourCoe 是 str 类型
  940. let rst = expression;
  941. let atIDArr = analyzer.getAtIDArr(rst);
  942. let IDArr = atIDArr.map(function (atID) {
  943. return atID.slice(1);
  944. });
  945. for (var i = 0; i < atIDArr.length; i++) {
  946. let patt = new RegExp(atIDArr[i]);
  947. let val = `$CE.at(${IDArr[i]})`;
  948. rst = rst.replace(patt, val);
  949. };
  950. rst = rst.replace(/\[/g, "$CE.base('");
  951. rst = rst.replace(/\]/g, "')");
  952. rst = rst.replace(/L/g, labourCoe);
  953. return rst;
  954. },
  955. getStatement: function (expression, template) {
  956. let rst = expression;
  957. let atIDArr = analyzer.getAtIDArr(rst);
  958. let IDArr = atIDArr.map(function (atID) {
  959. return atID.slice(1);
  960. });
  961. for (var i = 0; i < atIDArr.length; i++) {
  962. let patt = new RegExp(atIDArr[i]);
  963. let val = projectObj.project.calcProgram.compiledTemplates[template.ID].compiledCalcItems[IDArr[i]].name;
  964. rst = rst.replace(patt, val);
  965. };
  966. rst = rst.replace(/\[/g, "");
  967. rst = rst.replace(/\]/g, "");
  968. rst = rst.replace(/L/g, '人工系数');
  969. return rst;
  970. },
  971. calcItemMaxID: function(template){
  972. let MaxID = 0;
  973. for (let item of template.calcItems){
  974. if (item.ID > MaxID)
  975. MaxID = item.ID;
  976. };
  977. return MaxID;
  978. },
  979. calcItemIsUsed: function(template, calcItem){
  980. let atID = '@' + calcItem.ID;
  981. for (var i = 0; i < template.calcItems.length; i++) {
  982. let item = template.calcItems[i];
  983. let atIDArr = analyzer.getAtIDArr(item.expression);
  984. if (atIDArr.indexOf(atID) >= 0){
  985. calcItem.tempUsed = i;
  986. return true;
  987. }
  988. }
  989. return false;
  990. },
  991. calcItemLabourCoe: function(calcItem){
  992. let lc = 0;
  993. if (calcItem.labourCoeID)
  994. lc = projectObj.project.calcProgram.compiledLabourCoes[calcItem.labourCoeID].coe.toString();
  995. return lc;
  996. },
  997. templateRefresh: function(template){
  998. for (let item of template){
  999. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1000. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, me.calcItemLabourCoe(item));
  1001. };
  1002. },
  1003. templateMaxID: function(){
  1004. let ts = projectObj.project.calcProgram.templates;
  1005. let MaxID = 0;
  1006. for (let t of ts){
  1007. if (t.ID > MaxID)
  1008. MaxID = t.ID;
  1009. };
  1010. return MaxID;
  1011. },
  1012. templateNewName: function (name) {
  1013. let i = 2;
  1014. while (projectObj.project.calcProgram.compiledTemplateMaps[name + i]) {
  1015. i++;
  1016. };
  1017. return name + i;
  1018. },
  1019. templateNameIsExist: function (name) {
  1020. if (projectObj.project.calcProgram.compiledTemplateMaps[name])
  1021. return true
  1022. else return false;
  1023. },
  1024. templateIsUsed: function (ID){
  1025. let nodes = projectObj.project.mainTree.items;
  1026. for (let node of nodes){
  1027. if (node.data && node.data.programID && node.data.programID == ID) {
  1028. return true;
  1029. }
  1030. };
  1031. return false;
  1032. },
  1033. fieldNameIsUsed: function(template, fieldName){
  1034. let fieldNameEn = projectObj.project.calcProgram.compiledFeeTypeMaps[fieldName];
  1035. for (var i = 0; i < template.calcItems.length; i++) {
  1036. if (template.calcItems[i].fieldName == fieldNameEn){
  1037. template.fieldNameTempUsed = i;
  1038. return true;
  1039. }
  1040. }
  1041. return false;
  1042. }
  1043. };
  1044. let executeObj = {
  1045. treeNode: null,
  1046. template: null,
  1047. tempCalcItem: null,
  1048. at: function(ID) {
  1049. let me = executeObj;
  1050. let rst = me.template.compiledCalcItems[ID].unitFee;
  1051. rst = parseFloat(rst);
  1052. return rst;
  1053. },
  1054. base: function(baseName) {
  1055. let me = executeObj;
  1056. // 量价、工料机形式的定额, 要把自己的市场单价用于计算程序中的基数。
  1057. if (calcTools.isVolumePrice(me.treeNode) || calcTools.isGljRation(me.treeNode))
  1058. return calcTools.marketPriceToBase(me.treeNode, baseName)
  1059. else{
  1060. if (!rationCalcBases[baseName]){
  1061. hintBox.infoBox('系统提示', '定额基数“' + baseName + '”末定义,计算错误。 (模板 ' + me.template.ID + ',规则 ' + me.tempCalcItem.ID +')', 1);
  1062. return 0;
  1063. }
  1064. else
  1065. return rationCalcBases[baseName](me.treeNode);
  1066. }
  1067. },
  1068. HJ: function () {
  1069. let me = this;
  1070. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  1071. let q = calcTools.uiNodeQty(me.treeNode) ? calcTools.uiNodeQty(me.treeNode) : 1;
  1072. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  1073. return u;
  1074. }
  1075. };
  1076. class CalcProgram {
  1077. constructor(project){
  1078. let me = this;
  1079. me.project = project;
  1080. me.datas = [];
  1081. project.registerModule(ModuleNames.calc_program, me);
  1082. };
  1083. // 兼容Project框架方法
  1084. getSourceType () {
  1085. return ModuleNames.calc_program;
  1086. };
  1087. // 兼容Project框架方法
  1088. loadData (datas) {
  1089. this.datas = datas;
  1090. this.compileAllTemps();
  1091. };
  1092. // 兼容Project框架方法
  1093. doAfterUpdate (err, data) {
  1094. if(!err){
  1095. $.bootstrapLoading.end();
  1096. }
  1097. };
  1098. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  1099. compileAllTemps(){
  1100. let me = this;
  1101. me.compiledFeeRates = {};
  1102. me.compiledLabourCoes = {};
  1103. me.compiledTemplates = {};
  1104. me.compiledTemplateMaps = {};
  1105. me.compiledTemplateNames = [];
  1106. me.compiledFeeTypeMaps = {};
  1107. me.compiledFeeTypeNames = [];
  1108. me.compiledCalcBases = {};
  1109. me.saveForReports = [];
  1110. me.feeRates = this.project.FeeRate.datas.rates;
  1111. me.labourCoes = this.project.labourCoe.datas.coes;
  1112. me.feeTypes = cpFeeTypes;
  1113. // me.calcBases = rationCalcBase;
  1114. me.templates = this.project.calcProgram.datas.templates;
  1115. // me.templates.push(defaultBillTemplate);
  1116. // 先编译公用的基础数据
  1117. me.compilePublics();
  1118. me.compileTemplateMaps();
  1119. for (let t of me.templates){
  1120. me.compileTemplate(t);
  1121. };
  1122. // 存储费率临时数据,报表用。
  1123. if (me.saveForReports.length > 0){
  1124. let saveDatas = {};
  1125. saveDatas.projectID = projectInfoObj.projectInfo.ID;
  1126. saveDatas.calcItems = me.saveForReports;
  1127. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (result) {
  1128. if (result){
  1129. me.saveForReports = [];
  1130. };
  1131. });
  1132. };
  1133. };
  1134. compilePublics(){
  1135. let me = this;
  1136. for (let rate of me.feeRates) {
  1137. me.compiledFeeRates[rate.ID] = rate;
  1138. }
  1139. for (let coe of me.labourCoes) {
  1140. me.compiledLabourCoes[coe.ID] = coe;
  1141. }
  1142. for (let ft of me.feeTypes) {
  1143. me.compiledFeeTypeMaps[ft.type] = ft.name;
  1144. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  1145. me.compiledFeeTypeNames.push(ft.name);
  1146. }
  1147. /* for (let cb of me.calcBases) {
  1148. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  1149. }*/
  1150. };
  1151. compileTemplateMaps(){
  1152. let me = this;
  1153. function clearObj(obj){
  1154. for (let key in obj) {
  1155. delete obj[key];
  1156. }
  1157. };
  1158. clearObj(me.compiledTemplates);
  1159. clearObj(me.compiledTemplateMaps);
  1160. me.compiledTemplateNames.splice(0, me.compiledTemplateNames.length);
  1161. for (let t of me.templates){
  1162. me.compiledTemplates[t.ID] = t;
  1163. me.compiledTemplateMaps[t.ID] = t.name;
  1164. me.compiledTemplateMaps[t.name] = t.ID;
  1165. me.compiledTemplateNames.push(t.name);
  1166. };
  1167. };
  1168. compileTemplate(template){
  1169. let me = this;
  1170. // me.compiledTemplates[template.ID] = template;
  1171. // me.compiledTemplateMaps[template.ID] = template.name;
  1172. // me.compiledTemplateMaps[template.name] = template.ID;
  1173. // me.compiledTemplateNames.push(template.name);
  1174. template.hasCompiled = false;
  1175. template.errs = [];
  1176. let private_extract_ID = function(str, idx){
  1177. let subStr = str.slice(idx);
  1178. let patt = new RegExp(/@\d+/);
  1179. let atID = subStr.match(patt);
  1180. let ID = atID ? atID[0].slice(1) : null;
  1181. return ID;
  1182. };
  1183. let private_parse_ref = function(item, itemIdx){
  1184. let idx = item.expression.indexOf('@', 0);
  1185. while (idx >= 0) {
  1186. let ID = private_extract_ID(item.expression, idx);
  1187. let len = ID ? ID.toString().length : 0;
  1188. if (len) {
  1189. let subItem = template.compiledCalcItems[ID];
  1190. if (subItem) {
  1191. if (subItem.ID !== item.ID) {
  1192. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  1193. } else {
  1194. template.errs.push("循环引用ID: " + ID);
  1195. }
  1196. } else {
  1197. template.errs.push("找不到ID: " + ID);
  1198. console.log('找不到ID: ' + ID);
  1199. }
  1200. }
  1201. idx = item.expression.indexOf('@', idx + len + 1);
  1202. }
  1203. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  1204. template.compiledSeq.push(itemIdx);
  1205. }
  1206. };
  1207. let private_compile_items = function() {
  1208. for (let idx of template.compiledSeq) {
  1209. let item = template.calcItems[idx];
  1210. let lc = analyzer.calcItemLabourCoe(item);
  1211. // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  1212. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, lc);
  1213. if (item.expression == 'HJ')
  1214. item.compiledExpr = '$CE.HJ()'
  1215. else
  1216. item.compiledExpr = analyzer.getCompiledExpr(item.expression, lc);
  1217. if (item.feeRateID) {
  1218. let orgFeeRate = item.feeRate;
  1219. let cfr = me.compiledFeeRates[item.feeRateID];
  1220. item.feeRate = cfr ? cfr.rate : 100;
  1221. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  1222. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  1223. }
  1224. };
  1225. // 字段名映射
  1226. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  1227. }
  1228. };
  1229. if (template && template.calcItems && template.calcItems.length > 0) {
  1230. template.compiledSeq = [];
  1231. template.compiledCalcItems = {};
  1232. for (let i = 0; i < template.calcItems.length; i++) {
  1233. let item = template.calcItems[i];
  1234. // item.expression = analyzer.getExpression(item.dispExpr, template);
  1235. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1236. template.compiledCalcItems[item.ID] = item;
  1237. template.compiledCalcItems[item.ID + "_idx"] = i;
  1238. }
  1239. for (let i = 0; i < template.calcItems.length; i++) {
  1240. let item = template.calcItems[i];
  1241. if (template.compiledSeq.indexOf(i) < 0) {
  1242. private_parse_ref(item, i);
  1243. }
  1244. }
  1245. if (template.errs.length == 0) {
  1246. private_compile_items();
  1247. template.hasCompiled = true;
  1248. } else {
  1249. console.log('errors: ' + template.errs.toString());
  1250. }
  1251. };
  1252. };
  1253. // 存储、刷新零散的多个结点。
  1254. saveNodes(treeNodes, callback){
  1255. if (treeNodes.length < 1) {
  1256. $.bootstrapLoading.end();
  1257. return;
  1258. }
  1259. let me = this;
  1260. /* me.project.beginUpdate('');
  1261. for (let node of treeNodes){
  1262. if (node.changed){
  1263. let data = calcTools.cutNodeForSave(node);
  1264. let newData = {'updateType': 'ut_update', 'updateData': data};
  1265. me.project.push(node.sourceType, [newData]);
  1266. }
  1267. };
  1268. me.project.endUpdate();*/
  1269. let dataArr = [];
  1270. for (let node of treeNodes){
  1271. if (node.changed){
  1272. let data = calcTools.cutNodeForSave(node);
  1273. let newData = {'type': node.sourceType, 'data': data};
  1274. dataArr.push(newData);
  1275. }
  1276. };
  1277. if (dataArr.length < 1) {
  1278. $.bootstrapLoading.end();
  1279. return;
  1280. };
  1281. $.bootstrapLoading.start();
  1282. let startTime = +new Date();
  1283. me.project.updateNodes(dataArr, function (data) {
  1284. let endShowTime = +new Date();
  1285. console.log(`保存所需时间——${endShowTime - startTime}`);
  1286. if(callback){
  1287. callback(data);
  1288. };
  1289. for (let node of treeNodes){delete node.changed};
  1290. projectObj.mainController.refreshTreeNode(treeNodes);
  1291. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。
  1292. if (activeSubSheetIs(subSheetIndex.ssiCalcProgram)) {
  1293. calcProgramObj.refreshCurNodeCalcItems(me.project.mainTree.selected);
  1294. };
  1295. $.bootstrapLoading.end();
  1296. });
  1297. };
  1298. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  1299. innerCalc(treeNode, changedArr){
  1300. let me = this;
  1301. // 仅用作树节点显示的工料机不能参与计算。
  1302. if (treeNode.sourceType === ModuleNames.ration_glj) return;
  1303. treeNode.calcType = calcTools.getCalcType(treeNode);
  1304. function isBaseFeeType(type){
  1305. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  1306. };
  1307. /*删掉多余的费用。例如:从其它计算方式(有很多费)切换到公式计算方式(只需要common费),多出来的费要删除。
  1308. fieldNameArr 值取自:遍历treeNode的计算规则,取有绑定的字段名。这些字段名以外的fee是因旧计算多出来的,需要删除。 */
  1309. function deleteUselessFees(treeNode, fieldNameArr){
  1310. if (fieldNameArr){ // 用于计算程序没有绑定的费用类别,不要同步到清单,而清单因为以前计算过该类别又有值,需删除。如切换取费类别,旧费要清掉。
  1311. for (var i = 0; i < treeNode.data.fees.length; i++) {
  1312. let fee = treeNode.data.fees[i];
  1313. if ((fee.fieldName != 'estimate') && !fieldNameArr.includes(fee.fieldName)){
  1314. treeNode.data.fees.splice(i, 1);
  1315. delete treeNode.data.feesIndex[fee.fieldName];
  1316. treeNode.changed = true;
  1317. }
  1318. }
  1319. }
  1320. else{ // 总造价清单只留common, estimate两个费用类别。其它公式清单只留common。
  1321. let reserveArr = calcTools.isTotalCostBill(treeNode)? ['common', 'estimate']:['common'];
  1322. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1323. let feesArr = treeNode.data.fees;
  1324. for (let i = 0; i < feesArr.length; i++) {
  1325. if (!reserveArr.includes(feesArr[i].fieldName)) {
  1326. delete treeNode.data.feesIndex[feesArr[i].fieldName];
  1327. feesArr.splice(i, 1);
  1328. treeNode.changed = true;
  1329. }
  1330. }
  1331. };
  1332. };
  1333. };
  1334. // 父清单汇总子项(子清单或定额)的费用类别
  1335. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees || treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1336. treeNode.data.programID = null;
  1337. calcTools.initFees(treeNode);
  1338. let nodes = [];
  1339. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1340. calcTools.getGLJList(treeNode, true);
  1341. nodes = me.project.Ration.getRationNodes(treeNode);
  1342. }
  1343. else nodes = treeNode.children;
  1344. let rst = [];
  1345. for (let ft of cpFeeTypes) {
  1346. let ftObj = {};
  1347. ftObj.fieldName = ft.type;
  1348. ftObj.name = ft.name;
  1349. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  1350. let bq = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 1;
  1351. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  1352. for (let node of nodes) {
  1353. if (node.data.feesIndex && node.data.feesIndex[ft.type]) { // 父清单不要汇总综合单价。
  1354. btf = (btf + parseFloatPlus(node.data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  1355. bttf = (bttf + parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  1356. };
  1357. };
  1358. }
  1359. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  1360. let sum_rtf = 0, sum_rttf = 0;
  1361. for (let node of nodes) {
  1362. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  1363. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1364. ruf = parseFloatPlus(node.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  1365. rtuf = parseFloatPlus(node.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  1366. rtf = parseFloatPlus(node.data.feesIndex[ft.type].totalFee).toDecimal(decimalObj.bills.totalPrice);
  1367. rttf = parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee).toDecimal(decimalObj.bills.totalPrice);
  1368. };
  1369. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  1370. buf = (buf + (ruf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1371. btuf = (btuf + (rtuf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1372. };
  1373. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  1374. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  1375. };
  1376. if (me.project.property.billsCalcMode == leafBillGetFeeType.rationPriceConverse ||
  1377. me.project.property.billsCalcMode == leafBillGetFeeType.rationPrice) {
  1378. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  1379. btuf = (sum_rttf / bq).toDecimal(decimalObj.process);
  1380. };
  1381. if (isBaseFeeType(ft.type) ||
  1382. (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  1383. btf = sum_rtf;
  1384. bttf = sum_rttf;
  1385. }
  1386. else{
  1387. btf = (buf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1388. bttf = (btuf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1389. };
  1390. };
  1391. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  1392. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  1393. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  1394. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  1395. calcTools.checkFeeField(treeNode, ftObj);
  1396. rst.push(ftObj);
  1397. };
  1398. treeNode.data.calcTemplate = {"calcItems": rst};
  1399. }
  1400. // 叶子清单无子结点、无公式计算(啥都没有时)
  1401. else if (treeNode.calcType == treeNodeCalcType.ctNull){
  1402. delete treeNode.data.gljList;
  1403. // 不能直接删除该属性,否则无法冲掉库中已存储的值。下同。
  1404. if (treeNode.data.calcBase){
  1405. treeNode.data.calcBase = null;
  1406. treeNode.changed = true;
  1407. }
  1408. if (treeNode.data.calcBaseValue){
  1409. treeNode.data.calcBaseValue = null;
  1410. treeNode.changed = true;
  1411. }
  1412. if (treeNode.data.programID) {
  1413. treeNode.data.programID = null;
  1414. treeNode.changed = true;
  1415. }
  1416. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1417. treeNode.data.fees = null;
  1418. treeNode.data.feesIndex = null;
  1419. treeNode.changed = true;
  1420. }
  1421. treeNode.data.calcTemplate = {"calcItems": []};
  1422. }
  1423. // 叶子清单公式计算
  1424. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  1425. delete treeNode.data.gljList;
  1426. if (treeNode.data.programID) {
  1427. treeNode.data.programID = null;
  1428. treeNode.changed = true;
  1429. }
  1430. let f = treeNode.data.feeRate ? treeNode.data.feeRate : 100;
  1431. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  1432. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 1;
  1433. let uf = (b * f * 0.01 / q).toDecimal(decimalObj.bills.unitPrice);
  1434. let tuf = uf;
  1435. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100) : (uf * q);
  1436. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  1437. let ttf = tf;
  1438. deleteUselessFees(treeNode);
  1439. calcTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf});
  1440. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  1441. if (calcTools.isTotalCostBill(treeNode)){
  1442. // 公式叶子清单没有暂估费,但总造价清单除外。
  1443. calcTools.estimateFee(treeNode);
  1444. calcTools.initSummaryFee(treeNode);
  1445. treeNode.data.summaryFees.totalFee = tf;
  1446. treeNode.data.summaryFees.estimateFee = calcTools.getFee(treeNode, 'estimate.totalFee');
  1447. treeNode.data.summaryFees.safetyFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION), 'common.totalFee');
  1448. treeNode.data.summaryFees.chargeFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.CHARGE), 'common.totalFee');
  1449. }
  1450. treeNode.data.calcTemplate = {"calcItems": []};
  1451. }
  1452. // 定额或叶子清单自己的计算程序计算
  1453. else{
  1454. let fnArr = [];
  1455. calcTools.getGLJList(treeNode, true);
  1456. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1457. // 量价、工料机类型的定额要求市场合价
  1458. if (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation){
  1459. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1460. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 0;
  1461. let mtf = (muf * q).toDecimal(decimalObj.ration.totalPrice);
  1462. if (treeNode.data.marketTotalFee != mtf){
  1463. treeNode.data.marketTotalFee = mtf;
  1464. treeNode.changed = true;
  1465. } ;
  1466. };
  1467. };
  1468. if (treeNode.data.programID == undefined) treeNode.data.programID = projectInfoObj.projectInfo.property.engineering;
  1469. let template = me.compiledTemplates[treeNode.data.programID];
  1470. treeNode.data.calcTemplate = template;
  1471. if (treeNode && template.hasCompiled) {
  1472. let $CE = executeObj;
  1473. $CE.treeNode = treeNode;
  1474. $CE.template = template;
  1475. calcTools.initFees(treeNode);
  1476. for (let idx of template.compiledSeq) {
  1477. let calcItem = template.calcItems[idx];
  1478. $CE.tempCalcItem = calcItem;
  1479. let feeRate = 100; // 100%
  1480. if (calcItem.feeRate != undefined)
  1481. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  1482. // console.log(`[${calcItem.ID}]: ${calcItem.compiledExpr}`); // for test.
  1483. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode)); // 如果eval()对清单树有影响,就换成小麦的Expression对象再试
  1484. let q = calcTools.uiNodeQty(treeNode) ? calcTools.uiNodeQty(treeNode) : 0;
  1485. calcItem.totalFee = (calcItem.unitFee * q).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1486. if (calcItem.fieldName) {
  1487. fnArr.push(calcItem.fieldName);
  1488. calcTools.checkFeeField(treeNode, calcItem);
  1489. };
  1490. };
  1491. deleteUselessFees(treeNode, fnArr);
  1492. };
  1493. };
  1494. if (!calcTools.isTotalCostBill(treeNode)){ // 已在上面的分支中计算过
  1495. calcTools.getGLJList(treeNode, false);
  1496. calcTools.estimateFee(treeNode);
  1497. }
  1498. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  1499. };
  1500. // 计算本节点、所有父节点(默认,可选)、公式引用节点(默认,可选)。
  1501. calculate(treeNode, calcParents = true, calcFormulas = true){
  1502. let me = this;
  1503. let changedNodes = [];
  1504. me.innerCalc(treeNode, changedNodes);
  1505. if (treeNode.changed) {
  1506. // 计算父结点
  1507. if (calcParents){
  1508. let curNode = treeNode.parent;
  1509. while (curNode){
  1510. me.innerCalc(curNode, changedNodes);
  1511. curNode = curNode.parent;
  1512. };
  1513. };
  1514. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1515. if (calcFormulas) {
  1516. me.calcFormulaNodes(changedNodes);
  1517. };
  1518. };
  1519. return changedNodes;
  1520. };
  1521. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1522. calcAllType.catAll 计算所有树结点 (不指定参数时的默认值)
  1523. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1524. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到) 不要用
  1525. 缺陷:calcAllType.catRations 参数情况不会计算父结点。(calcAllType.catBills 可以,因为清单的父结点也是清单会计算)
  1526. */
  1527. calcAllNodes(calcType = calcAllType.catAll){
  1528. let me = this;
  1529. let changedNodes = [];
  1530. function calcNodes(nodes) {
  1531. for (let node of nodes) {
  1532. if (node.children.length > 0) {
  1533. calcNodes(node.children);
  1534. };
  1535. if ((calcType == calcAllType.catAll || calcType == node.sourceType) && node.calcType != treeNodeCalcType.ctCalcBaseValue) {
  1536. me.innerCalc(node, changedNodes);
  1537. };
  1538. }
  1539. };
  1540. calcNodes(me.project.mainTree.roots);
  1541. me.calcFormulaNodes(changedNodes);
  1542. return changedNodes;
  1543. };
  1544. // 计算全部公式项。 (参数意义:将通过本方法后发生改变的节点存入changedArr中)
  1545. calcFormulaNodes(changedArr){
  1546. let me = this;
  1547. let formulaNodes = cbTools.getFormulaNodes(true);
  1548. if (formulaNodes.length == 0) return;
  1549. for (let formulaNode of formulaNodes){
  1550. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  1551. projectObj.project.calcBase.calculate(formulaNode, true);
  1552. if (projectObj.project.calcBase.success){
  1553. // 计算公式结点
  1554. me.innerCalc(formulaNode, changedArr);
  1555. // 计算父结点
  1556. if (formulaNode.changed){
  1557. let curNode = formulaNode.parent;
  1558. while (curNode){
  1559. me.innerCalc(curNode, changedArr);
  1560. curNode = curNode.parent;
  1561. };
  1562. };
  1563. };
  1564. };
  1565. };
  1566. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  1567. calcLeafAndSave(treeNode){
  1568. let me = this;
  1569. if(!calcTools.isLeafBill(treeNode)) return;
  1570. if (treeNode.children && treeNode.children.length > 0) {
  1571. let changedNodes = [];
  1572. for (let child of treeNode.children){
  1573. me.innerCalc(child, changedNodes);
  1574. };
  1575. let curChangeds = me.calculate(treeNode);
  1576. mergeArr(changedNodes, curChangeds);
  1577. me.saveNodes(changedNodes);
  1578. };
  1579. };
  1580. // 计算并保存指定的一个树节点。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1581. // 这个方法实际上封装了calculate()和saveNodes()两个方法,主要目的是为了外部调用方便,少写一点累赘代码。
  1582. calcAndSave(treeNode, callback){
  1583. let changedNodes = this.calculate(treeNode);
  1584. this.saveNodes(changedNodes, callback);
  1585. };
  1586. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  1587. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  1588. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1589. calcNodesAndSave(nodes, callback){
  1590. let me = this, rationNodes = [], billNodes = [], leafBills = [], allChangedNodes = [];
  1591. for (let node of nodes) {
  1592. if (node.sourceType == ModuleNames.ration)
  1593. rationNodes.push(node)
  1594. else
  1595. billNodes.push(node);
  1596. };
  1597. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1598. for (let ration of rationNodes) {
  1599. me.innerCalc(ration, allChangedNodes);
  1600. let leafBill = ration.parent;
  1601. if (leafBill && leafBills.indexOf(leafBill) < 0)
  1602. leafBills.push(leafBill);
  1603. };
  1604. mergeArr(billNodes, leafBills);
  1605. for (let bill of billNodes){
  1606. let changeBills = me.calculate(bill, true, false);
  1607. mergeArr(allChangedNodes, changeBills);
  1608. };
  1609. me.calcFormulaNodes(allChangedNodes);
  1610. me.saveNodes(allChangedNodes, callback);
  1611. };
  1612. calcRationsAndSave(rationNodes, callback){
  1613. // let me = this, leafBills = [], allChangedNodes = [];
  1614. // for (let node of rationNodes) {
  1615. // me.innerCalc(node, allChangedNodes);
  1616. // let leafBill = node.parent;
  1617. // // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1618. // if (leafBill && leafBills.indexOf(leafBill) < 0) leafBills.push(leafBill);
  1619. // };
  1620. //
  1621. // for (let node of leafBills){
  1622. // let curChangeds = me.calculate(node);
  1623. // mergeArr(allChangedNodes, curChangeds);
  1624. // };
  1625. // let endShowTime = +new Date();
  1626. // me.saveNodes(allChangedNodes, callback);
  1627. this.calcNodesAndSave(rationNodes, callback);
  1628. };
  1629. calcBillsAndSave(billNodes,callback){
  1630. // let me = this, allChangedNodes = [];
  1631. // for (let node of billNodes) {
  1632. // let curChangeds = me.calculate(node, true, false);
  1633. // mergeArr(allChangedNodes, curChangeds);
  1634. // };
  1635. // me.calcFormulaNodes(allChangedNodes);
  1636. // me.saveNodes(allChangedNodes,callback);
  1637. this.calcNodesAndSave(billNodes, callback);
  1638. };
  1639. calcAllNodesAndSave(calcType = calcAllType.catAll, callback){
  1640. let changedNodes = this.calcAllNodes(calcType);
  1641. this.saveNodes(changedNodes, callback);
  1642. };
  1643. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  1644. getTotalFee(baseNodes, excludeNodes){
  1645. let rst = 0;
  1646. function calcNodes(nodes) {
  1647. for (let node of nodes) {
  1648. if (!excludeNodes.includes(node)){
  1649. if (node.source && node.source.children && node.source.children.length > 0) {
  1650. calcNodes(node.children);
  1651. }
  1652. else{
  1653. if (node.sourceType == ModuleNames.bills) {
  1654. rst = (rst + calcTools.getFee(node, 'common.totalFee')).toDecimal(decimalObj.decimal("totalPrice", node));
  1655. };
  1656. }
  1657. }
  1658. }
  1659. };
  1660. calcNodes(baseNodes);
  1661. return rst;
  1662. };
  1663. // 税前工程造价
  1664. getBeforeTaxTotalFee(excludeNodes){
  1665. let baseNodes = [], me = this;
  1666. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  1667. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  1668. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  1669. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  1670. return me.getTotalFee(baseNodes, excludeNodes);
  1671. };
  1672. };
  1673. // export default analyzer;