calc_program.js 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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. // 是否是标题清单
  101. isTitleBills: function (node) {
  102. let flag = node.getFlag();
  103. return titleFlags.includes(flag);
  104. },
  105. isBill: function(treeNode){
  106. return treeNode.sourceType === ModuleNames.bills;
  107. },
  108. isParentBill: function (treeNode) {
  109. return this.isBill(treeNode) &&
  110. treeNode.source.children &&
  111. treeNode.source.children.length > 0;
  112. },
  113. isLeafBill: function(treeNode){ // 下面挂有定额的清单也是叶子清单
  114. return this.isBill(treeNode) &&
  115. treeNode.source.children &&
  116. treeNode.source.children.length === 0;
  117. },
  118. isLeafNode: function(treeNode){ // 最底层结点,如定额等。
  119. return treeNode.children.length === 0;
  120. },
  121. isBill_DXFY: function(treeNode){
  122. return this.isBill(treeNode) && treeNode.data.type == billType.DXFY;
  123. },
  124. isBill_FB: function(treeNode){
  125. return this.isBill(treeNode) && treeNode.data.type == billType.FB;
  126. },
  127. isBill_FX: function(treeNode){
  128. return this.isBill(treeNode) && treeNode.data.type == billType.FX;
  129. },
  130. isBill_BILL: function(treeNode){
  131. return this.isBill(treeNode) && treeNode.data.type == billType.BILL;
  132. },
  133. isBill_BX: function(treeNode){
  134. return this.isBill(treeNode) && treeNode.data.type == billType.BX;
  135. },
  136. isNullBill: function (treeNode) {
  137. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (!treeNode.data.calcBase);
  138. },
  139. isCalcBaseBill: function(treeNode){
  140. return this.isLeafBill(treeNode) && (treeNode.children.length === 0) && (treeNode.data.calcBase);
  141. },
  142. isTotalCostBill: function (treeNode) {
  143. return treeNode.data.flagsIndex && treeNode.data.flagsIndex.fixed && treeNode.data.flagsIndex.fixed.flag &&
  144. treeNode.data.flagsIndex.fixed.flag == fixedFlag.ENGINEERINGCOST;
  145. },
  146. isRationCategory: function(treeNode){
  147. return treeNode.sourceType === ModuleNames.ration;
  148. },
  149. isRationItem: function(treeNode){
  150. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.ration;
  151. },
  152. isVolumePrice: function (treeNode) {
  153. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.volumePrice;
  154. },
  155. isGljRation: function (treeNode) {
  156. return this.isRationCategory(treeNode) && treeNode.data.type === rationType.gljRation;
  157. },
  158. isSameTypeNode: function (node1, node2) {
  159. if (node1.parent && node2.parent && (node1.parent === node2.parent) && (node1.sourceType === node1.sourceType)
  160. && (node1.data && node2.data && node1.data.type === node2.data.type)){
  161. return true;
  162. }
  163. return false;
  164. },
  165. isInheritFrom: function (treeNode, flagsArr){
  166. let cur = treeNode;
  167. while (cur.parent) {
  168. cur = cur.parent;
  169. };
  170. let flag = -1;
  171. if (cur.data.flagsIndex && cur.data.flagsIndex.fixed && cur.data.flagsIndex.fixed.flag)
  172. flag = cur.data.flagsIndex.fixed.flag;
  173. return flagsArr.includes(flag);
  174. },
  175. isFBFX:function (treeNode) {
  176. return projectObj.project.Bills.isFBFX(treeNode);
  177. },
  178. isTechMeasure:function(treeNode){
  179. return projectObj.project.Bills.isTechMeasure(treeNode)
  180. },
  181. canCalcToTalFeeByOwn: function (treeNode) {
  182. return !projectObj.project.Bills.cantCalcToTalFeeByOwn(treeNode);
  183. },
  184. getChildrenFormulaNodes: function (self, allFormulaNodesArr, parentNodes){ // 获取结点parentNodes下有公式的子结点
  185. let nodes = [];
  186. for (let pn of parentNodes){
  187. for (let node of allFormulaNodesArr){
  188. let cur = node;
  189. while (cur) {
  190. if (cur == pn && node != self){
  191. nodes.push(node);
  192. break;
  193. }
  194. cur = cur.parent;
  195. };
  196. };
  197. };
  198. return nodes;
  199. },
  200. getGLJList: function (treeNode, needOneBill) {
  201. delete treeNode.data.gljList;
  202. if (this.isRationCategory(treeNode)) {
  203. if (treeNode.data.type != rationType.volumePrice) {
  204. treeNode.data.gljList = projectObj.project.calcProgram.getGljArrByRation(treeNode.data);
  205. }
  206. }
  207. else if (this.isBill(treeNode)){
  208. let nodeQ = this.uiNodeQty(treeNode);
  209. let q = nodeQ ? nodeQ : 1;
  210. let rNodes = projectObj.project.Ration.getRationNodes(treeNode);
  211. let rations = rNodes.map(function (node) {return node.data});
  212. treeNode.data.gljList = projectObj.project.ration_glj.getGatherGljArrByRations(rations, needOneBill, q);
  213. };
  214. },
  215. getLeafBills: function (treeNode){
  216. let leafBills = [];
  217. function getBill(node){
  218. if (!node) return;
  219. if (calcTools.isLeafBill(node))
  220. leafBills.push(node);
  221. if (node.firstChild()) getBill(node.firstChild());
  222. if (node.nextSibling) getBill(node.nextSibling);
  223. };
  224. let fc = treeNode.firstChild();
  225. if (fc) getBill(fc);
  226. return leafBills;
  227. },
  228. forceSelect: function (treeNode, rowsCount = 1, colsCount = 2){
  229. projectObj.mainController.tree.selected = treeNode;
  230. let idx = projectObj.project.mainTree.items.indexOf(treeNode);
  231. let sheet = projectObj.mainSpread.getActiveSheet();
  232. sheet.setSelection(idx, 0, rowsCount, colsCount);
  233. sheet.showRow(idx, GC.Spread.Sheets.VerticalPosition.center);
  234. },
  235. initFees: function (treeNode){
  236. if (!treeNode.data.fees) {
  237. treeNode.data.fees = [];
  238. treeNode.data.feesIndex = {};
  239. treeNode.changed = true;
  240. }
  241. else if (!treeNode.data.feesIndex){
  242. treeNode.data.feesIndex = {};
  243. for (let fee of treeNode.data.fees){
  244. treeNode.data.feesIndex[fee.fieldName] = fee;
  245. };
  246. treeNode.changed = true;
  247. };
  248. },
  249. initFeeField: function (treeNode, fieldName){
  250. this.initFees(treeNode);
  251. if (!treeNode.data.feesIndex[fieldName]) {
  252. let fee = {
  253. 'fieldName': fieldName,
  254. 'unitFee': 0,
  255. 'totalFee': 0,
  256. 'tenderUnitFee': 0,
  257. 'tenderTotalFee': 0
  258. };
  259. treeNode.data.fees.push(fee);
  260. treeNode.data.feesIndex[fieldName] = fee;
  261. treeNode.changed = true;
  262. };
  263. },
  264. checkFeeField: function (treeNode, feeObj){
  265. if (!feeObj) return;
  266. if (feeObj.fieldName == '') return;
  267. // 初始化前先拦截末定义的情况
  268. if (!treeNode.data.feesIndex || !treeNode.data.feesIndex[feeObj.fieldName]){
  269. if (feeObj.unitFee == 0 && feeObj.totalFee == 0) return;
  270. }
  271. this.initFeeField(treeNode, feeObj.fieldName);
  272. if (treeNode.data.feesIndex[feeObj.fieldName].unitFee != feeObj.unitFee){
  273. treeNode.data.feesIndex[feeObj.fieldName].unitFee = feeObj.unitFee;
  274. treeNode.changed = true;
  275. };
  276. if (treeNode.data.feesIndex[feeObj.fieldName].totalFee != feeObj.totalFee){
  277. treeNode.data.feesIndex[feeObj.fieldName].totalFee = feeObj.totalFee;
  278. treeNode.changed = true;
  279. };
  280. // 不知在何种情况下,tenderUnitFee、tenderTotalFee的值会变成NaN,这里提前处理一下
  281. if (isNaN(treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee))
  282. treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee = undefined;
  283. if (isNaN(treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee))
  284. treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee = undefined;
  285. if (treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee != feeObj.tenderUnitFee){
  286. treeNode.data.feesIndex[feeObj.fieldName].tenderUnitFee = feeObj.tenderUnitFee;
  287. treeNode.changed = true;
  288. };
  289. if (treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee != feeObj.tenderTotalFee){
  290. treeNode.data.feesIndex[feeObj.fieldName].tenderTotalFee = feeObj.tenderTotalFee;
  291. treeNode.changed = true;
  292. };
  293. },
  294. setFieldValue: function (treeNode, fieldName, value){
  295. if (fieldName.includes('feesIndex')){
  296. let arr = fieldName.split('.');
  297. this.initFeeField(treeNode, arr[1]);
  298. treeNode.data.feesIndex[arr[1]][arr[2]] = value;
  299. if(fieldName == 'feesIndex.common.unitFee'){
  300. let fee = _.find(treeNode.data.fees,{'fieldName':arr[1]});
  301. if(fee) fee[arr[2]] = value;
  302. }
  303. }
  304. else{
  305. treeNode.data[fieldName] = value;
  306. };
  307. treeNode.changed = true;
  308. },
  309. initSummaryFee: function (treeNode){
  310. if (!treeNode.data.summaryFees){
  311. treeNode.data.summaryFees = {
  312. totalFee: 0,
  313. estimateFee: 0,
  314. safetyFee: 0,
  315. chargeFee: 0
  316. };
  317. treeNode.changed = true;
  318. };
  319. },
  320. hasAdjustPrice: function(){ // 编办是否有材料的调整价发文
  321. if (projectObj.project.projSetting.glj_col && projectObj.project.projSetting.glj_col.showAdjustPrice == true)
  322. return true
  323. else
  324. return false;
  325. },
  326. // 参数fieldName值: 'common.totalFee'、'equipment.unitFee'
  327. getFee: function (treeNode, fieldName) {
  328. if (!treeNode) return 0;
  329. let ns = fieldName.split(".");
  330. if (ns.length != 2)
  331. return 0
  332. else if (treeNode.data.feesIndex && treeNode.data.feesIndex[ns[0]] && treeNode.data.feesIndex[ns[0]][ns[1]])
  333. return parseFloat(treeNode.data.feesIndex[ns[0]][ns[1]])
  334. else
  335. return 0;
  336. },
  337. rationBaseFee: function (treeNode, gljTypes, priceType, isTender){
  338. if (!treeNode.data.gljList) return 0;
  339. let me = this, result = 0;
  340. let price = 0, temp = 0, temp2 = 0;
  341. // 机械组成物价差: 机上人工、动力燃料
  342. if ((priceType == priceTypes.ptDiffPrice) && (gljTypes.includes(gljType.MACHINE_LABOUR) || gljTypes.includes(gljType.FUEL_POWER_FEE))){
  343. for (let glj of treeNode.data.gljList) {
  344. if ([gljType.GENERAL_MACHINE, gljType.INSTRUMENT].includes(glj.type)){
  345. if (isTender){
  346. calcTools.calcGLJTenderPrice(glj);
  347. calcTools.calcGLJTenderQty(treeNode, glj);
  348. };
  349. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  350. if (!mds) mds = [];
  351. for (let md of mds){
  352. if (gljTypes.includes(md.type)){
  353. let gljQ = me.uiGLJQty(glj["quantity"]);
  354. let mdMP = md["marketPrice"];
  355. if (isTender){
  356. calcTools.calcGLJTenderPrice(md);
  357. mdMP = md["tenderPrice"];
  358. calcTools.calcGLJTenderQty(treeNode, glj);
  359. gljQ = me.uiGLJQty(glj["tenderQuantity"]);
  360. };
  361. let mdQ = me.uiGLJQty(md.consumption);
  362. let mdAP = calcTools.hasAdjustPrice() ? md["adjustPrice"] : md["basePrice"];
  363. // if (aprice != mprice){
  364. temp = (temp + (gljQ * mdQ * mdMP).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  365. temp2 = (temp2 + (gljQ * mdQ * mdAP).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  366. // }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. // 普通基数计算(包括普通价差)
  373. else{
  374. for (let glj of treeNode.data.gljList) {
  375. if (gljTypes.indexOf(glj.type) >= 0) {
  376. if (isTender){
  377. calcTools.calcGLJTenderPrice(glj);
  378. calcTools.calcGLJTenderQty(treeNode, glj);
  379. };
  380. let qty = isTender ? me.uiGLJQty(glj["tenderQuantity"]) : me.uiGLJQty(glj["quantity"]);
  381. let mprice = isTender ? me.uiGLJPrice(glj["tenderPrice"], glj) : me.uiGLJPrice(glj["marketPrice"], glj);
  382. let aprice = calcTools.hasAdjustPrice() ? me.uiGLJPrice(glj["adjustPrice"], glj) : me.uiGLJPrice(glj["basePrice"], glj);
  383. if (priceType == priceTypes.ptDiffPrice){
  384. // if (aprice != mprice){
  385. temp = (temp + (qty * mprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  386. temp2 = (temp2 + (qty * aprice).toDecimal(decimalObj.process)).toDecimal(decimalObj.process);
  387. // }
  388. }
  389. else {
  390. if (priceType == priceTypes.ptBasePrice){ price = me.uiGLJPrice(glj["basePrice"], glj);}
  391. else if (priceType == priceTypes.ptAdjustPrice){price = aprice;}
  392. else if (priceType == priceTypes.ptMarketPrice){price = mprice;}
  393. if (projectObj.project.property.areaSetting && treeNode.data.areaIncreaseFee){
  394. let p;
  395. if ([gljType.LABOUR].includes(glj.type))
  396. p = projectObj.project.property.areaSetting.labour
  397. else if (baseMaterialTypes.includes(glj.type))
  398. p = projectObj.project.property.areaSetting.material
  399. else if ([gljType.GENERAL_MACHINE].includes(glj.type))
  400. p = projectObj.project.property.areaSetting.machine;
  401. qty = qty * (1 + p * 0.01).toDecimal(decimalObj.process);
  402. }
  403. temp = (qty * price).toDecimal(decimalObj.process);
  404. result = (result + temp).toDecimal(decimalObj.process);
  405. };
  406. };
  407. };
  408. };
  409. if (priceType == priceTypes.ptDiffPrice){
  410. if (typeof isCQ2018 != 'undefined'){
  411. // 如下这一句十分重要!JS计算误差导致379.08-331.695=47.38499999999999。如果直接取2位会变成47.38。所以先取6位47.385,再取2位47.39。
  412. result = (temp - temp2).toDecimal(decimalObj.process);
  413. result = result.toDecimal(decimalObj.ration.unitPrice) // 重庆2018所有都是先汇总相减后再取舍
  414. }
  415. else{
  416. if (gljTypes == baseMaterialTypes)
  417. result = (temp - temp2).toDecimal(decimalObj.ration.unitPrice)
  418. else
  419. result = (temp.toDecimal(decimalObj.ration.unitPrice) - temp2.toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  420. };
  421. }
  422. else{
  423. result = result.toDecimal(decimalObj.ration.unitPrice);
  424. };
  425. return result;
  426. },
  427. // masterTypeFilter 过滤机械机型:[]全部, [1,2]特大机械 [3,4]中小机械。 detailType 如机上人工费、机械折旧费等
  428. machineDetailFee: function (treeNode, gljArr, masterTypeFilter, detailType, isTender) {
  429. if (!gljArr) return 0;
  430. let result = 0;
  431. for (let glj of gljArr) {
  432. if (baseMachineMasterTypes.includes(glj.type)){
  433. // 机型不符
  434. if ((masterTypeFilter.length > 0) && (glj.model && !masterTypeFilter.includes(glj.model))) continue;
  435. let gljQ;
  436. if (isTender){
  437. calcTools.calcGLJTenderQty(treeNode, glj);
  438. gljQ = glj.tenderQuantity;
  439. }
  440. else
  441. gljQ = glj.quantity;
  442. // 获取机械组成物(调价不深入到组成物)
  443. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  444. if (!mds) mds = [];
  445. let mdSum = 0;
  446. for (let md of mds) {
  447. if (md.type == detailType) {
  448. let q = md["consumption"] ? md["consumption"] : 0;
  449. let p = md["basePrice"] ? md["basePrice"] : 0;
  450. mdSum = mdSum + (q * p).toDecimal(decimalObj.glj.unitPriceHasMix);
  451. mdSum = (mdSum).toDecimal(decimalObj.glj.unitPriceHasMix);
  452. }
  453. }
  454. if (typeof isCQ2018 != 'undefined')
  455. result = (result + (gljQ * mdSum).toDecimal(decimalObj.process)).toDecimal(decimalObj.process)
  456. else
  457. result = (result + (gljQ * mdSum).toDecimal(decimalObj.ration.unitPrice)).toDecimal(decimalObj.ration.unitPrice);
  458. }
  459. }
  460. result = (result).toDecimal(decimalObj.ration.unitPrice);
  461. return result;
  462. },
  463. // 总造价清单、叶子清单、定额的暂估费。父清单是汇总子清单的暂估费,走计算程序逻辑,不在这里。
  464. estimateFee: function (treeNode, isBase, isTender){
  465. let me = this, sumU = 0, sumT = 0;
  466. let nodeQ = me.uiNodeQty(treeNode, isTender);
  467. let isGather = (projectObj.project.property.zanguCalcMode == zanguCalcType.gatherMaterial);
  468. // 先汇总数量,再乘市场价。如果是叶子清单,进入这里的gljList中的材料,已经是同类材料跨定额汇总过的了。
  469. function eTFee(){
  470. if (!treeNode.data.gljList) return 0;
  471. let GLJObjs = [];
  472. for (let glj of treeNode.data.gljList) {
  473. if (!allMaterialTypes.includes(glj.type)) continue;
  474. if (glj.isEstimate){
  475. GLJObjs.push({code: glj.code, name: glj.name, specs: glj.specs, unit: glj.unit, type: glj.type,
  476. // quantity: (nodeQ * glj.quantity).toDecimal(decimalObj.process),
  477. quantity: me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.process),
  478. marketPrice: glj.marketPrice});
  479. }
  480. else{ // 组成物
  481. if (!compositionTypes.includes(glj.type)) continue;
  482. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  483. if (!mds) mds = [];
  484. for (let md of mds){
  485. if (md.isEstimate){
  486. let isExist = false;
  487. // let totalQ = (nodeQ * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.glj.quantity);
  488. let totalQ = me.uiGLJQty((glj.totalQuantity)).toDecimal(decimalObj.glj.quantity);
  489. let mdQ = (totalQ * me.uiGLJQty(md.consumption)).toDecimal(decimalObj.process);
  490. for (let obj of GLJObjs){
  491. if (gljOprObj.getIndex(md, gljKeyArray) == gljOprObj.getIndex(obj, gljKeyArray)){
  492. isExist = true;
  493. obj.quantity = (obj.quantity + mdQ).toDecimal(decimalObj.glj.quantity);
  494. break;
  495. }
  496. };
  497. if (!isExist)
  498. GLJObjs.push({code: md.code, name: md.name, specs: md.specs, unit: md.unit, type: md.type,
  499. quantity: mdQ, marketPrice: md.marketPrice});
  500. }
  501. }
  502. }
  503. };
  504. let rst = 0;
  505. for (let obj of GLJObjs){
  506. let tp = (me.uiGLJQty(obj.quantity) * me.uiGLJPrice(obj.marketPrice, obj)).toDecimal(decimalObj.bills.totalPrice);
  507. rst = (rst + tp).toDecimal(decimalObj.bills.totalPrice);
  508. };
  509. return rst;
  510. };
  511. // 汇总子结点的暂估合价
  512. function eTFeeByChildren(){
  513. let rst = 0;
  514. for (let node of treeNode.children){
  515. if (node.data.feesIndex && node.data.feesIndex['estimate']) {
  516. rst = (rst + parseFloatPlus(node.data.feesIndex['estimate'].totalFee)).toDecimal(decimalObj.process);
  517. };
  518. };
  519. rst = (rst).toDecimal(decimalObj.bills.totalPrice);
  520. return rst;
  521. };
  522. // 先数量乘市场价,再汇总
  523. function eUFee(){
  524. if (!treeNode.data.gljList) return 0;
  525. let rst = 0;
  526. for (let glj of treeNode.data.gljList) {
  527. if (!allMaterialTypes.includes(glj.type)) continue;
  528. if (glj.isEstimate){
  529. rst = rst + (me.uiGLJQty(glj.quantity) * me.uiGLJPrice(glj.marketPrice, glj)).toDecimal(decimalObj.process);
  530. rst = rst.toDecimal(decimalObj.process);
  531. }
  532. else{ // 组成物
  533. if (!compositionTypes.includes(glj.type)) continue;
  534. let mds = projectObj.project.composition.getCompositionByGLJ(glj);
  535. if (!mds) mds = [];
  536. for (let md of mds){
  537. if (!md.isEstimate) continue;
  538. let mdU = (me.uiGLJQty(md.consumption) * me.uiGLJPrice(md.marketPrice)).toDecimal(decimalObj.glj.unitPrice);
  539. rst = rst + (mdU * me.uiGLJQty(glj.quantity)).toDecimal(decimalObj.process);
  540. rst = rst.toDecimal(decimalObj.process);
  541. }
  542. }
  543. };
  544. rst = rst.toDecimal(decimalObj.bills.unitPrice);
  545. return rst;
  546. };
  547. // 总造价暂估费
  548. if (me.isTotalCostBill(treeNode)){
  549. let nodes = projectObj.project.mainTree.roots;
  550. for (let node of nodes){
  551. if (me.isTotalCostBill(node)) break;
  552. let eU = 0, eT = 0;
  553. if (node.data.feesIndex && node.data.feesIndex.estimate){
  554. eU = node.data.feesIndex.estimate.unitFee;
  555. eT = node.data.feesIndex.estimate.totalFee;
  556. }
  557. else {
  558. eU = 0, eT = 0;
  559. };
  560. sumU = (sumU + parseFloatPlus(eU)).toDecimal(decimalObj.process);
  561. sumT = (sumT + parseFloatPlus(eT)).toDecimal(decimalObj.process);
  562. };
  563. sumU = (sumU).toDecimal(decimalObj.bills.unitPrice);
  564. sumT = (sumT).toDecimal(decimalObj.bills.totalPrice);
  565. }
  566. else if (me.isParentBill(treeNode)){ // 父清单不要汇总单价。
  567. sumT = eTFeeByChildren();
  568. sumU = undefined;
  569. }
  570. else if (me.isLeafBill(treeNode)){
  571. if (projectObj.project.Bills.isEngineerEst(treeNode)){
  572. if (treeNode.data.feesIndex['common'] != undefined){
  573. sumT = treeNode.data.feesIndex['common'].totalFee;
  574. sumU = treeNode.data.feesIndex['common'].unitFee;
  575. }
  576. }
  577. else{
  578. if (isGather){
  579. me.getGLJList(treeNode, false);
  580. sumT = eTFee();
  581. }
  582. else
  583. sumT = eTFeeByChildren();
  584. let q = nodeQ ? nodeQ : 1;
  585. sumU = (sumT / q).toDecimal(decimalObj.bills.totalPrice);
  586. }
  587. }
  588. else if (me.isRationCategory(treeNode)){
  589. me.getGLJList(treeNode, false);
  590. sumU = eUFee();
  591. if (isBase) return sumU;
  592. if (isGather)
  593. sumT = eTFee()
  594. else
  595. sumT = (nodeQ * sumU).toDecimal(decimalObj.ration.totalPrice);
  596. };
  597. me.checkFeeField(treeNode, {'fieldName': 'estimate', 'unitFee': sumU, 'totalFee': sumT});
  598. },
  599. marketPriceToBase: function (treeNode, baseName, isTender) {
  600. if (treeNode.data.type != rationType.volumePrice && treeNode.data.type != rationType.gljRation) return;
  601. let result = 0, me = this;
  602. function isRCJZC(treeNode, baseName) { // 基数名称中是否包含人材机主设,且树结点类型要匹配一致
  603. let rst = (treeNode.data.subType === gljType.LABOUR && baseName.includes('人工')) || // 人工费、市场人工费
  604. (baseMaterialTypes.includes(treeNode.data.subType) && baseName.includes('材料')) ||
  605. (treeNode.data.subType === gljType.GENERAL_MACHINE && (baseName.includes('机械') || baseName.includes('机具'))) ||
  606. (treeNode.data.subType === gljType.MAIN_MATERIAL && baseName.includes('主材')) ||
  607. (treeNode.data.subType === gljType.EQUIPMENT && baseName.includes('设备'));
  608. return rst;
  609. }
  610. if (baseName.includes('甲供') || baseName.includes('甲定') || baseName.includes('分包')) {
  611. result = 0;
  612. }
  613. else if (baseName.includes('价差')) {
  614. if (treeNode.data.type == rationType.gljRation) {
  615. if (isRCJZC(treeNode, baseName)) {
  616. let aprice = me.uiGLJPrice(treeNode.data.basePrice); // 量价虚拟的工料机不可能有发文,这里直接取定额价。
  617. let mprice = me.uiGLJPrice(treeNode.data.marketUnitFee);
  618. result = (mprice - aprice).toDecimal(decimalObj.ration.unitPrice);
  619. }
  620. }
  621. }
  622. else if (baseName.includes('机上人工')) {
  623. if (treeNode.data.subType === gljType.GENERAL_MACHINE) {
  624. let glj = {
  625. 'code': treeNode.data.code,
  626. 'name': treeNode.data.name,
  627. 'specs': treeNode.data.specs,
  628. 'unit': treeNode.data.unit,
  629. 'quantity': 1,
  630. 'type': treeNode.data.subType // 注意:这里要取subType
  631. };
  632. result = me.machineDetailFee(treeNode, [glj], [], gljType.MACHINE_LABOUR, isTender);
  633. }
  634. }
  635. else {
  636. if (isRCJZC(treeNode, baseName)) {
  637. if (treeNode.data.type == rationType.volumePrice)
  638. result = treeNode.data.marketUnitFee ? parseFloat(treeNode.data.marketUnitFee).toDecimal(decimalObj.ration.unitPrice) : 0
  639. else if (treeNode.data.type == rationType.gljRation)
  640. // result = treeNode.data.basePrice ? parseFloat(treeNode.data.basePrice).toDecimal(decimalObj.ration.unitPrice) : 0;
  641. // 这里因为是算基数所以要取基价,但不能直接取basePrice,受限于项目属性的三个选项。
  642. result = gljOprObj.getBasePrice(treeNode);
  643. }
  644. };
  645. return result;
  646. },
  647. partASupplyFee: function (treeNode, baseName, isTender, isRationPirce = true) {
  648. if (!treeNode.data.gljList) return 0;
  649. let projectGLJ = projectObj.project.projectGLJ;
  650. let supplyT = [];
  651. if (baseName.includes('甲供'))
  652. supplyT = [supplyType.BFJG, supplyType.WQJG] // 字段中存储的是汉字、数字混杂!
  653. else if (baseName.includes('甲定'))
  654. supplyT = [supplyType.JDYG];
  655. let gljT = [], compT = [];
  656. if (baseName.includes('人工')) gljT = [gljType.LABOUR]; // 含人工的:如甲供人工、甲定人工
  657. else if (baseName.includes('材料')){
  658. gljT = baseMaterialTypes;
  659. compT = compositionTypes;
  660. }
  661. else if (baseName.includes('机械') || baseName.includes('机具')) {
  662. gljT = baseMachineTypes;
  663. compT = [gljType.GENERAL_MACHINE, gljType.INSTRUMENT, gljType.OTHER_MACHINE_USED]; // 取并集,兼容重庆2018新定额
  664. }
  665. else if (baseName.includes('主材')) {
  666. gljT = [gljType.MAIN_MATERIAL];
  667. compT = [gljType.MAIN_MATERIAL];
  668. }
  669. else if (baseName.includes('设备')) {
  670. gljT = [gljType.EQUIPMENT];
  671. };
  672. // alert(JSON.stringify(projectGLJ.testGLJs()));
  673. let supplyGLJs = projectGLJ.getGLJsBySupply(supplyT, gljT);
  674. if (supplyGLJs.length == 0) return 0;
  675. let supplyGLJsIdx = {};
  676. for (let sglj of supplyGLJs){supplyGLJsIdx[sglj.id] = sglj};
  677. function isSupply(composition, supplies) {
  678. for (let supply of supplies){
  679. if(supply.code == composition.code && supply.name == composition.name && supply.unit == composition.unit &&
  680. supply.specs == composition.specs && supply.type == composition.type ){
  681. composition.basePrice = supply.unit_price.base_price;
  682. composition.marketPrice = supply.unit_price.market_price;
  683. composition.supply = supply.supply;
  684. composition.supplyX = 1;
  685. if (composition.supply == supplyType.BFJG){
  686. let Q = supply.quantity ? supply.quantity : 1;
  687. composition.supplyX = supply.supply_quantity / Q;
  688. }
  689. return true;
  690. }
  691. };
  692. return false;
  693. };
  694. let sum = 0;
  695. for (let glj of treeNode.data.gljList){
  696. let gljQ, gljP;
  697. if (isTender){
  698. calcTools.calcGLJTenderQty(treeNode, glj);
  699. gljQ = glj.tenderQuantity;
  700. }
  701. else
  702. gljQ = glj.quantity;
  703. gljP = isRationPirce ? glj.basePrice : glj.marketPrice;
  704. let X = 1; // 部分甲供系数(默认1,即完全甲供)
  705. let tempSGLJ = supplyGLJsIdx[glj.projectGLJID];
  706. // 当前材料是甲供材料:①普通 ②商品硂等不计组成物的母体材料
  707. if (tempSGLJ) {
  708. // 处理部分甲供
  709. if (baseName.includes('甲供') && (tempSGLJ.supply == supplyType.BFJG)){
  710. // let Q = isTender ? tempSGLJ.tenderQuantity : tempSGLJ.quantity;
  711. let Q = tempSGLJ.quantity;
  712. Q = Q ? Q : 1;
  713. X = tempSGLJ.supply_quantity / Q;
  714. }
  715. sum = (sum + gljP * gljQ * X).toDecimal(decimalObj.process);
  716. }
  717. else{ // 当前材料不是甲供材料
  718. if (compT.includes(glj.type)) { // 混凝土等。组成物的母体,母体如果有组成物,则母体无法作为甲供材料,无法设置,此时要看其组成物是否是甲供材料;母体如果没有组成物,则母体有可能成为甲供材料。
  719. let pGLJ = projectGLJ.getDataByID(glj.projectGLJID);
  720. let compositions = pGLJ.ratio_data; // 组成物明细
  721. if (compositions.length > 0){
  722. for (let c of compositions){
  723. if (isSupply(c, supplyGLJs)) {
  724. X = 1;
  725. if (baseName.includes('甲供') && (c.supply == supplyType.BFJG)){
  726. X = c.supplyX;
  727. };
  728. let cP = isRationPirce ? c.basePrice : c.marketPrice;
  729. sum = (sum + cP * c.consumption * gljQ * X).toDecimal(decimalObj.process);
  730. }
  731. }
  732. };
  733. }
  734. };
  735. };
  736. sum = sum.toDecimal(decimalObj.ration.unitPrice);
  737. return sum;
  738. },
  739. getCalcType: function (treeNode) {
  740. if (this.isRationCategory(treeNode)){
  741. return treeNodeCalcType.ctRationCalcProgram;
  742. }
  743. else if (this.isNullBill(treeNode)){
  744. return treeNodeCalcType.ctNull;
  745. }
  746. else if (this.isLeafBill(treeNode)) {
  747. if (treeNode.children && treeNode.children.length > 0){
  748. // 清单单价计算模式下的叶子清单:取自己的计算程序ID,找到自己的计算程序计算。(汇总清单所有定额的工料机)
  749. if (projectObj.project.property.billsCalcMode === leafBillGetFeeType.billsPrice)
  750. return treeNodeCalcType.ctBillCalcProgram;
  751. else // 前三种计算模式下的叶子清单:汇总定额的计算程序的费用类别
  752. return treeNodeCalcType.ctGatherRationsFees;
  753. }
  754. else{ // 公式计算
  755. return treeNodeCalcType.ctCalcBaseValue;
  756. };
  757. }
  758. else if (this.isBill(treeNode)) { // 父清单:汇总子清单的费用类别
  759. return treeNodeCalcType.ctGatherBillsFees;
  760. }
  761. else {
  762. return treeNodeCalcType.ctRationCalcProgram;
  763. };
  764. },
  765. cutNodeForSave(treeNode){
  766. let me = this;
  767. /* subType、quantity、calcBase、programID、marketUnitFee等等字段较为特殊,它们的改变一定会触发计算并导致计算
  768. 结果的变化,从而引发保存动作。将这些字段放在该位置跟计算结果一起保存,可减少前端跟后端的通讯频率。 */
  769. let data = {
  770. projectID: projectObj.project.ID(),
  771. ID: treeNode.data.ID,
  772. unit: treeNode.data.unit, //对清单来说,改变单位,工程量精度会跟着改变从而影响计算。
  773. subType: treeNode.data.subType,
  774. quantity: treeNode.data.quantity,
  775. calcBase: treeNode.data.calcBase,
  776. calcBaseValue: treeNode.data.calcBaseValue,
  777. tenderCalcBaseValue: treeNode.data.tenderCalcBaseValue,
  778. programID: treeNode.data.programID,
  779. marketUnitFee: treeNode.data.marketUnitFee,
  780. marketTotalFee: treeNode.data.marketTotalFee,
  781. fees: treeNode.data.fees,
  782. isFromDetail:treeNode.data.isFromDetail,
  783. feeRate: treeNode.data.feeRate,
  784. feeRateID: treeNode.data.feeRateID,
  785. contain: treeNode.data.contain,
  786. quantityEXP: treeNode.data.quantityEXP,
  787. summaryFees: treeNode.data.summaryFees,
  788. name:treeNode.data.name,
  789. rationQuantityCoe: treeNode.data.rationQuantityCoe,
  790. quantityCoe: treeNode.data.quantityCoe == null ? {} : treeNode.data.quantityCoe,
  791. targetUnitFee: treeNode.data.targetUnitFee,
  792. targetTotalFee: treeNode.data.targetTotalFee
  793. };
  794. // 定额大类
  795. if (me.isRationCategory(treeNode)) {
  796. data.isSubcontract = treeNode.data.isSubcontract;
  797. data.evaluationProject = treeNode.data.evaluationProject;
  798. //定额类型的工料机做特殊处理
  799. if(me.isGljRation(treeNode)){
  800. data.code = treeNode.data.code;
  801. data.projectGLJID = treeNode.data.projectGLJID;
  802. delete data.marketUnitFee;
  803. };
  804. };
  805. // 优化掉 undefined 属性
  806. data = JSON.parse(JSON.stringify(data));
  807. return data;
  808. },
  809. uiNodeQty: function (treeNode){
  810. return parseFloatPlus(treeNode.data.quantity).toDecimal(decimalObj.decimal("quantity", treeNode));
  811. },
  812. uiNodeTenderQty: function (treeNode){
  813. return this.calcNodeTenderQty(treeNode);
  814. },
  815. calcNodeTenderQty: function (treeNode){
  816. if (this.isBill(treeNode)) // 清单只有一个工程量,没有调整后工程量。
  817. return this.uiNodeQty(treeNode)
  818. else{
  819. let qCoe = 1;
  820. /* 量价、工料机类型的定额,在反向调价之调整人材机消耗量系数时,因为他们没有工料机可调,调价结果没变,影响汇总后的父结点金额。
  821. 所以要特殊处理:此种情况下仍然要调量价的消耗量,即还是要像"子目工程量调整系数"方式那样调,但系数又不能在"子目工程量调整系数"
  822. 中显示出来,所以可以改变tenderQuantity达到同样的效果以瞒天过海。所以在取系数时,无论什么系数,只要能取到就算正确。 */
  823. if (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation){
  824. if (treeNode.data.rationQuantityCoe)
  825. qCoe = treeNode.data.rationQuantityCoe
  826. else if (treeNode.data.quantityCoe && treeNode.data.quantityCoe.labour)
  827. qCoe = treeNode.data.quantityCoe.labour;
  828. }
  829. else {
  830. if (treeNode.data.rationQuantityCoe)
  831. qCoe = treeNode.data.rationQuantityCoe;
  832. };
  833. treeNode.data.tenderQuantity = (this.uiNodeQty(treeNode) * qCoe).toDecimal(decimalObj.decimal("quantity", treeNode));
  834. return treeNode.data.tenderQuantity;
  835. }
  836. },
  837. calcGLJTenderQty: function (treeNode, glj){
  838. if (treeNode.data.quantityCoe == undefined){
  839. glj.tenderQuantity = glj.quantity;
  840. return;
  841. };
  842. let qCoe = 1;
  843. let projGLJ = calcTools.getProjectGLJ(glj);
  844. if (projGLJ.is_adjust_price != 1) { // 先检查项目工料机里,该工料机是否参与调价
  845. if (gljType.LABOUR == glj.type){
  846. if (treeNode.data.quantityCoe.labour)
  847. qCoe = treeNode.data.quantityCoe.labour;
  848. }
  849. else if (baseMaterialTypes.indexOf(glj.type)){
  850. if (treeNode.data.quantityCoe.material)
  851. qCoe = treeNode.data.quantityCoe.material;
  852. }
  853. else if (baseMachineTypes.indexOf(glj.type)){
  854. if (treeNode.data.quantityCoe.machine)
  855. qCoe = treeNode.data.quantityCoe.machine;
  856. }
  857. else if (gljType.MAIN_MATERIAL == glj.type){
  858. if (treeNode.data.quantityCoe.main)
  859. qCoe = treeNode.data.quantityCoe.main;
  860. }
  861. else if (gljType.EQUIPMENT == glj.type){
  862. if (treeNode.data.quantityCoe.equipment)
  863. qCoe = treeNode.data.quantityCoe.equipment;
  864. };
  865. }
  866. glj.tenderQuantity = (glj.quantity * qCoe).toDecimal(decimalObj.glj.quantity);
  867. },
  868. calcGLJTenderPrice: function (glj) {
  869. let projGLJ = calcTools.getProjectGLJ(glj);
  870. if (projGLJ == null){ // 量价定额虚拟出来的工料机,在项目工料机中查不到。
  871. glj.tenderPrice = projectObj.project.projectGLJ.getTenderMarketPrice(projGLJ);
  872. }else{
  873. let pCoe = 1;
  874. if (projGLJ.is_adjust_price != 1){ // 先检查项目工料机里,该工料机是否参与调价
  875. if (projectObj.project.property.tenderSetting && projectObj.project.property.tenderSetting.gljPriceTenderCoe)
  876. pCoe = projectObj.project.property.tenderSetting.gljPriceTenderCoe;
  877. };
  878. glj.tenderPrice = (glj.marketPrice * pCoe).toDecimal(decimalObj.glj.unitPrice);
  879. };
  880. },
  881. // 界面显示的工料机价格,包括定额价、市场价等。参数 price 传入一个普通的价格数值即可。
  882. uiGLJPrice: function (price, glj){
  883. if (price){
  884. let projGLJ = glj ? calcTools.getProjectGLJ(glj) : null;
  885. let d = (projGLJ && projGLJ.ratio_data.length > 0) ? decimalObj.glj.unitPriceHasMix : decimalObj.glj.unitPrice;
  886. return parseFloat(price).toDecimal(d);
  887. }
  888. else return 0;
  889. },
  890. // 界面显示的工料机数量。参数 quantity 传入一个普通的数量数值即可。
  891. uiGLJQty: function (quantity){
  892. if (quantity)
  893. return parseFloat(quantity).toDecimal(decimalObj.glj.quantity)
  894. else return 0;
  895. },
  896. hasTargetTotalFee: function (treeNode){ // targetTotalFee 有时为字符串“0”,此种情况会执行if 条件引起逻辑错误。所以这里封闭成方法直接调用。
  897. return (treeNode.data.targetTotalFee && parseFloat(treeNode.data.targetTotalFee));
  898. },
  899. hasQuantity: function (treeNode){
  900. return (treeNode.data.quantity && parseFloat(treeNode.data.quantity));
  901. },
  902. getRationsByProjectGLJ(PGLJID){
  903. let rationIDs = [];
  904. let RGs = projectObj.project.ration_glj.datas;
  905. let PGLJIDs = [];
  906. if(Array.isArray(PGLJID)){//为了提高效率,改成支持传入数组
  907. PGLJIDs = PGLJID
  908. }else {
  909. PGLJIDs = [PGLJID];
  910. }
  911. for (let rg of RGs){
  912. if (PGLJIDs.indexOf(rg.projectGLJID) !== -1){
  913. rationIDs.push(rg.rationID);
  914. }
  915. };
  916. let rationNodes = [];
  917. let nodes = projectObj.project.mainTree.nodes;
  918. for (let rID of rationIDs){
  919. rationNodes.push(nodes['id_' + rID]);
  920. };
  921. // 工料机形式的定额
  922. let items = projectObj.project.mainTree.items;
  923. for (let item of items){
  924. if (PGLJIDs.indexOf(item.data.projectGLJID) !== -1)
  925. rationNodes.push(item);
  926. };
  927. return rationNodes;
  928. },
  929. getNodesByProgramID(programID){
  930. let discreteNodes = [];
  931. let nodes = projectObj.project.mainTree.items;
  932. for (let node of nodes){
  933. if (node.data.programID == programID)
  934. discreteNodes.push(node);
  935. };
  936. return discreteNodes;
  937. },
  938. getProjectGLJ(glj){
  939. if (glj.projectGLJID){
  940. return projectObj.project.projectGLJ.getDataByID(glj.projectGLJID);
  941. }
  942. else return null;
  943. },
  944. labourDays(node, isTender){
  945. if (!node.data.gljList) return 0;
  946. let rst = 0;
  947. calcTools.uiNodeQty(node);
  948. for (let glj of node.data.gljList) {
  949. if (glj.type == gljType.LABOUR) {
  950. let gljQ;
  951. if (isTender){
  952. calcTools.calcGLJTenderQty(node, glj);
  953. gljQ = glj.tenderQuantity.toDecimal(decimalObj.glj.quantity);
  954. }
  955. else
  956. gljQ = glj.quantity.toDecimal(decimalObj.glj.quantity);
  957. // rst = rst + (gljQ * calcTools.uiNodeQty(node)).toDecimal(decimalObj.process);
  958. // rst = rst.toDecimal(decimalObj.process);
  959. rst = (rst + gljQ).toDecimal(decimalObj.process);
  960. }
  961. };
  962. return rst.toDecimal(decimalObj.glj.quantity);
  963. },
  964. getProjectFeatureProperty(propertyKey){
  965. for (let o of projectObj.project.property.projectFeature){
  966. if (o.key == propertyKey){
  967. return o.value;
  968. }
  969. };
  970. },
  971. getFeeRateByNode(node){
  972. let decimal = getDecimal("feeRate");
  973. if(node.data.feeRateID) {
  974. let r = projectObj.project.FeeRate.getFeeRateByID(node.data.feeRateID);
  975. if (r) return scMathUtil.roundForObj(r.rate, decimal);
  976. };
  977. if (node.data.feeRate || node.data.feeRate == 0)
  978. return scMathUtil.roundForObj(node.data.feeRate,decimal);
  979. return 100;
  980. },
  981. isEmptyObject(obj){
  982. let arr = Object.keys(obj);
  983. return arr.length == 0;
  984. },
  985. // 清单价格是否大于最高限价
  986. unitFeeGTMaxPrice: function (node, feeField) {
  987. if (!this.isBill(node)) {
  988. return false;
  989. }
  990. const totalFee = this.getFee(node, feeField);
  991. const maxPrice = node.data.maxPrice;
  992. // 最高限价有值才对比
  993. if (!commonUtil.isDef(maxPrice)) {
  994. return false;
  995. }
  996. return totalFee > maxPrice;
  997. }
  998. };
  999. let rationCalcBases = {
  1000. '定额基价人工费': function (node, isTender) {
  1001. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice, isTender);
  1002. },
  1003. '定额基价材料费': function (node, isTender) {
  1004. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice, isTender);
  1005. },
  1006. '定额基价机械费': function (node, isTender) {
  1007. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice, isTender);
  1008. },
  1009. '定额基价机上人工费': function (node, isTender) {
  1010. return calcTools.machineDetailFee(node, node.data.gljList, [], gljType.MACHINE_LABOUR, isTender);
  1011. },
  1012. '人工费价差': function (node, isTender) {
  1013. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptDiffPrice, isTender);
  1014. },
  1015. '材料费价差': function (node, isTender) {
  1016. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptDiffPrice, isTender);
  1017. },
  1018. '机械费价差': function (node, isTender) {
  1019. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptDiffPrice, isTender);
  1020. },
  1021. '主材费价差': function (node, isTender) {
  1022. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptDiffPrice, isTender);
  1023. },
  1024. '设备费价差': function (node, isTender) {
  1025. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptDiffPrice, isTender);
  1026. },
  1027. '主材费': function (node, isTender) {
  1028. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptBasePrice, isTender);
  1029. },
  1030. '设备费': function (node, isTender) {
  1031. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptBasePrice, isTender);
  1032. },
  1033. '人工工日': function (node, isTender) {
  1034. return calcTools.labourDays(node, isTender);
  1035. },
  1036. '甲供定额基价人工费': function (node, isTender) {
  1037. return calcTools.partASupplyFee(node, '甲供定额基价人工费', isTender);
  1038. },
  1039. '甲供定额基价材料费': function (node, isTender) {
  1040. return calcTools.partASupplyFee(node, '甲供定额基价材料费', isTender);
  1041. },
  1042. '甲供定额基价机械费': function (node, isTender) {
  1043. return calcTools.partASupplyFee(node, '甲供定额基价机械费', isTender);
  1044. },
  1045. '甲供主材费': function (node, isTender) {
  1046. return calcTools.partASupplyFee(node, '甲供主材费', isTender);
  1047. },
  1048. '甲供设备费': function (node, isTender) {
  1049. return calcTools.partASupplyFee(node, '甲供设备费', isTender);
  1050. },
  1051. '甲定定额基价人工费': function (node, isTender) {
  1052. return calcTools.partASupplyFee(node, '甲定定额基价人工费', isTender);
  1053. },
  1054. '甲定定额基价材料费': function (node, isTender) {
  1055. return calcTools.partASupplyFee(node, '甲定定额基价材料费', isTender);
  1056. },
  1057. '甲定定额基价机械费': function (node, isTender) {
  1058. return calcTools.partASupplyFee(node, '甲定定额基价机械费', isTender);
  1059. },
  1060. '甲定主材费': function (node, isTender) {
  1061. return calcTools.partASupplyFee(node, '甲定主材费', isTender);
  1062. },
  1063. '甲定设备费': function (node, isTender) {
  1064. return calcTools.partASupplyFee(node, '甲定设备费', isTender);
  1065. },
  1066. '暂估材料费': function (node, isTender) {
  1067. return calcTools.estimateFee(node, true, isTender);
  1068. },
  1069. '分包定额基价人工费': function (node, isTender) {
  1070. if (node.data.isSubcontract)
  1071. return calcTools.rationBaseFee(node, [gljType.LABOUR], priceTypes.ptBasePrice, isTender)
  1072. else
  1073. return 0;
  1074. },
  1075. '分包定额基价材料费': function (node, isTender) {
  1076. if (node.data.isSubcontract)
  1077. return calcTools.rationBaseFee(node, baseMaterialTypes, priceTypes.ptBasePrice, isTender)
  1078. else
  1079. return 0;
  1080. },
  1081. '分包定额基价机械费': function (node, isTender) {
  1082. if (node.data.isSubcontract)
  1083. return calcTools.rationBaseFee(node, [gljType.GENERAL_MACHINE], priceTypes.ptBasePrice, isTender)
  1084. else
  1085. return 0;
  1086. },
  1087. '分包主材费': function (node, isTender) {
  1088. if (node.data.isSubcontract)
  1089. return calcTools.rationBaseFee(node, [gljType.MAIN_MATERIAL], priceTypes.ptBasePrice, isTender)
  1090. else
  1091. return 0;
  1092. },
  1093. '分包设备费': function (node, isTender) {
  1094. if (node.data.isSubcontract)
  1095. return calcTools.rationBaseFee(node, [gljType.EQUIPMENT], priceTypes.ptBasePrice, isTender)
  1096. else
  1097. return 0;
  1098. },
  1099. '分包人工工日': function (node, isTender) {
  1100. if (node.data.isSubcontract)
  1101. return calcTools.labourDays(node, isTender)
  1102. else
  1103. return 0;
  1104. }
  1105. };
  1106. let analyzer = {
  1107. error: '',
  1108. standard: function(expr){
  1109. let str = expr;
  1110. str = str.replace(/\s/g, ""); // 去空格、去中文空格
  1111. str = str.replace(/(/g, "("); // 中文括号"("换成英文括号"("
  1112. str = str.replace(/)/g, ")"); // 中文括号")"换成英文括号")"
  1113. str = str.replace(/f/g, "F"); // f换成F
  1114. str = str.replace(/l/g, "L"); // l换成L
  1115. return str;
  1116. },
  1117. getFArr: function (expr) {
  1118. let pattF = new RegExp(/F\d+/gi);
  1119. let arrF = expr.match(pattF);
  1120. return arrF ? arrF : [];
  1121. },
  1122. getAtIDArr: function (expr) { // ['@1','@2']
  1123. let patt = new RegExp(/@\d+/gi);
  1124. let arr = expr.match(patt);
  1125. return arr ? arr : [];
  1126. },
  1127. getBaseArr: function (expr) {
  1128. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  1129. let arrBase = expr.match(pattBase);
  1130. return arrBase ? arrBase : [];
  1131. },
  1132. getID: function (FName, template) { // F13 → 4
  1133. let idx = FName.slice(1) - 1;
  1134. let id = template.calcItems[idx] ? template.calcItems[idx].ID : null;
  1135. return id;
  1136. },
  1137. getFName: function (atID, template) { // @3 → F7
  1138. for (var i = 0; i < template.calcItems.length; i++) {
  1139. if (template.calcItems[i].ID == atID.slice(1)) {
  1140. return 'F'+ (i + 1);
  1141. }
  1142. }
  1143. return null;
  1144. },
  1145. getItem: function (FName, template){ // F3 → calcItems[2] → {Object}
  1146. let idx = FName.slice(1) - 1;
  1147. return template.calcItems[idx];
  1148. },
  1149. isCycleCalc: function (expression, ID, template) { // 这里判断expression,是ID引用: @5+@6
  1150. let atID = `@${ID}`;
  1151. // 避免部分匹配,如:@10匹配@1
  1152. let idx = expression.indexOf(atID);
  1153. if (idx >= 0){
  1154. let nextPos = idx + atID.length;
  1155. if (nextPos >= expression.length)
  1156. return true;
  1157. let char = expression.charAt(nextPos);
  1158. if (!char.isNumberStr())
  1159. return true;
  1160. };
  1161. let atIDs = analyzer.getAtIDArr(expression);
  1162. for (let atID of atIDs) {
  1163. let item = template.compiledCalcItems[atID.slice(1)];
  1164. if (analyzer.isCycleCalc(item.expression, ID, template))
  1165. return true;
  1166. }
  1167. return false;
  1168. },
  1169. isLegal: function (dispExpr, itemID, template) { // 检测包括:无效字符、基数是否中括号、基数是否定义、行引用、循环计算
  1170. function testValue(expr){
  1171. try {
  1172. expr = expr.replace(/\[[\u4E00-\u9FA5]+\]/gi, '0');
  1173. expr = expr.replace(/@\d+/gi, '0');
  1174. expr = expr.replace(/L/gi, '0');
  1175. expr = expr.replace(/%/gi, '*0.01');
  1176. if (expr.includes('00'))
  1177. return false;
  1178. eval(expr);
  1179. return true;
  1180. }
  1181. catch (err) {
  1182. return false;
  1183. }
  1184. };
  1185. let me = analyzer;
  1186. let expr = me.standard(dispExpr);
  1187. let invalidChars = /[^0-9\u4e00-\u9fa5\+\-\*\/\(\)\.\[\]FL%]/g;
  1188. if (invalidChars.test(expr)){
  1189. analyzer.error = `表达式中含有${hintBox.font('无效字符')}!`;
  1190. // hintBox.infoBox('错误提示',`表达式中含有${hintBox.font('无效字符')}!`,1);
  1191. return false;
  1192. };
  1193. let i = expr.search(/\df/ig); // 4F3
  1194. let j = expr.search(/\d\[/ig); // 4[定额基价人工费]
  1195. if (i > -1 || j > -1){
  1196. analyzer.error = `表达式中${hintBox.font('缺少运算符')}!`;
  1197. // hintBox.infoBox('错误提示', `表达式中${hintBox.font('缺少运算符')}!`, 1);
  1198. return false;
  1199. }
  1200. let pattCn = new RegExp(/[\u4E00-\u9FA5]+/gi);
  1201. let arrCn = expr.match(pattCn);
  1202. let pattBase = new RegExp(/\[[\u4E00-\u9FA5]+\]/gi);
  1203. let arrBase = expr.match(pattBase);
  1204. if (arrCn && !arrBase){
  1205. analyzer.error = `定额基数必须用中括号${hintBox.font('[]')}括起来!`;
  1206. // hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1207. return false;
  1208. };
  1209. if (arrCn && arrBase && (arrCn.length != arrBase.length)){
  1210. for (let cn of arrCn){
  1211. let tempBase = `[${cn}]`;
  1212. if (!arrBase.includes(tempBase)){
  1213. analyzer.error = `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`;
  1214. // hintBox.infoBox('错误提示', `定额基数“${hintBox.font(cn)}”必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1215. return false;
  1216. }
  1217. };
  1218. // 这里要加一个保险。因为上面的 for 循环在“主材费 + [主材费]” 情况下有Bug
  1219. analyzer.error =`定额基数必须用中括号${hintBox.font('[]')}括起来!`;
  1220. // hintBox.infoBox('错误提示', `定额基数必须用中括号${hintBox.font('[]')}括起来!`, 1);
  1221. return false;
  1222. };
  1223. if (arrBase){
  1224. for (let base of arrBase){
  1225. let baseName = base.slice(1, -1);
  1226. if (!rationCalcBases[baseName]){
  1227. analyzer.error = `定额基数${hintBox.font('[' +baseName + ']')}末定义!`;
  1228. // hintBox.infoBox('错误提示', `定额基数${hintBox.font('[' +baseName + ']')}末定义!`, 1);
  1229. return false;
  1230. }
  1231. };
  1232. };
  1233. let arrF = me.getFArr(expr);
  1234. for (let F of arrF){
  1235. let num = F.slice(1);
  1236. if (num > template.calcItems.length){
  1237. let s = hintBox.font('F'+num);
  1238. analyzer.error = `表达式中 “${hintBox.font(s)}” 行号引用错误!`;
  1239. // hintBox.infoBox('错误提示', `表达式中 “${hintBox.font(s)}” 行号引用错误!`, 1);
  1240. return false;
  1241. };
  1242. };
  1243. let expression = me.getExpression(expr, template);
  1244. if (me.isCycleCalc(expression, itemID, template)){
  1245. analyzer.error = `表达式中有${hintBox.font('循环计算')}!`;
  1246. // hintBox.infoBox('错误提示', `表达式中有${hintBox.font('循环计算')}!`, 1);
  1247. return false;
  1248. };
  1249. if (!testValue(expression)){
  1250. analyzer.error = `表达式中有${hintBox.font('语法错误')}!`;
  1251. // hintBox.infoBox('错误提示', `表达式中有${hintBox.font('语法错误')}!`, 1);
  1252. return false;
  1253. };
  1254. return true; // 表达式合法
  1255. },
  1256. refIDToLines: function (expression, template) { // @2、@3 → F7、F8
  1257. let rst = expression;
  1258. let atIDArr = analyzer.getAtIDArr(rst);
  1259. let fArr = [];
  1260. for (let atID of atIDArr) {
  1261. let FN = analyzer.getFName(atID, template);
  1262. fArr.push(FN);
  1263. };
  1264. for (let i = 0; i < atIDArr.length; i++) {
  1265. let patt = new RegExp(atIDArr[i]);
  1266. let val = fArr[i];
  1267. rst = rst.replace(patt, val);
  1268. };
  1269. return rst;
  1270. },
  1271. refLineToIDs: function (dispExpr, template) { // F7、F8 → @2、@3
  1272. let rst = analyzer.standard(dispExpr);
  1273. let fArr = analyzer.getFArr(rst);
  1274. let IDArr = [];
  1275. for (let F of fArr) {
  1276. let ID = analyzer.getID(F, template);
  1277. IDArr.push(ID);
  1278. };
  1279. for (let i = 0; i < fArr.length; i++) {
  1280. let patt = new RegExp(fArr[i]);
  1281. let val = `@${IDArr[i]}`;
  1282. rst = rst.replace(patt, val);
  1283. };
  1284. return rst;
  1285. },
  1286. getDispExpr: function (expression, template) {
  1287. return analyzer.refIDToLines(expression, template);
  1288. },
  1289. getExpression: function (dispExpr, template) {
  1290. return analyzer.refLineToIDs(dispExpr, template);
  1291. },
  1292. getDispExprUser: function (dispExpr, labourCoe) { // labourCoe 是 str 类型
  1293. let rst = analyzer.standard(dispExpr);
  1294. rst = rst.replace(/L/g, labourCoe);
  1295. return rst;
  1296. },
  1297. getCompiledExpr: function (expression, labourCoe) { // labourCoe 是 str 类型
  1298. let rst = expression;
  1299. let atIDArr = analyzer.getAtIDArr(rst);
  1300. let IDArr = atIDArr.map(function (atID) {
  1301. return atID.slice(1);
  1302. });
  1303. for (var i = 0; i < atIDArr.length; i++) {
  1304. let patt = new RegExp(atIDArr[i]);
  1305. let val = `$CE.at(${IDArr[i]},false)`;
  1306. rst = rst.replace(patt, val);
  1307. };
  1308. rst = rst.replace(/%/g, "*0.01");
  1309. rst = rst.replace(/\[/g, "$CE.base('");
  1310. rst = rst.replace(/\]/g, "',false)");
  1311. rst = rst.replace(/L/g, labourCoe);
  1312. return rst;
  1313. },
  1314. getCompiledTenderExpr: function (compiledExpr) {
  1315. let rst = compiledExpr.replace(/false/g, "true");
  1316. return rst;
  1317. },
  1318. getStatement: function (expression, template) {
  1319. let rst = expression;
  1320. let atIDArr = analyzer.getAtIDArr(rst);
  1321. let IDArr = atIDArr.map(function (atID) {
  1322. return atID.slice(1);
  1323. });
  1324. for (var i = 0; i < atIDArr.length; i++) {
  1325. let patt = new RegExp(atIDArr[i]);
  1326. let val = projectObj.project.calcProgram.compiledTemplates[template.ID].compiledCalcItems[IDArr[i]].name;
  1327. rst = rst.replace(patt, val);
  1328. };
  1329. rst = rst.replace(/\[/g, "");
  1330. rst = rst.replace(/\]/g, "");
  1331. rst = rst.replace(/L/g, '人工系数');
  1332. return rst;
  1333. },
  1334. calcItemMaxID: function(template){
  1335. let MaxID = 0;
  1336. for (let item of template.calcItems){
  1337. if (item.ID > MaxID)
  1338. MaxID = item.ID;
  1339. };
  1340. return MaxID;
  1341. },
  1342. calcItemIsUsed: function(template, calcItem){
  1343. let atID = '@' + calcItem.ID;
  1344. for (var i = 0; i < template.calcItems.length; i++) {
  1345. let item = template.calcItems[i];
  1346. let atIDArr = analyzer.getAtIDArr(item.expression);
  1347. if (atIDArr.indexOf(atID) >= 0){
  1348. calcItem.tempUsed = i;
  1349. return true;
  1350. }
  1351. }
  1352. return false;
  1353. },
  1354. refreshUsedCalcItemsStatement: function(template, calcItem){
  1355. let atID = '@' + calcItem.ID;
  1356. for (var i = 0; i < template.calcItems.length; i++) {
  1357. let item = template.calcItems[i];
  1358. let atIDArr = analyzer.getAtIDArr(item.expression);
  1359. if (atIDArr.indexOf(atID) >= 0){
  1360. item.statement = analyzer.getStatement(item.expression, template);
  1361. };
  1362. }
  1363. },
  1364. calcItemLabourCoe: function(calcItem){
  1365. let lc = 0;
  1366. if (calcItem.labourCoeID)
  1367. lc = projectObj.project.calcProgram.compiledLabourCoes[calcItem.labourCoeID].coe.toString();
  1368. return lc;
  1369. },
  1370. templateRefresh: function(template){
  1371. for (let item of template){
  1372. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1373. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, me.calcItemLabourCoe(item));
  1374. };
  1375. },
  1376. templateMaxID: function(){
  1377. let ts = projectObj.project.calcProgram.templates;
  1378. let MaxID = 0;
  1379. for (let t of ts){
  1380. if (t.ID > MaxID)
  1381. MaxID = t.ID;
  1382. };
  1383. return MaxID;
  1384. },
  1385. templateNewName: function (name) {
  1386. let i = 1;
  1387. while (projectObj.project.calcProgram.compiledTemplateMaps[name + i]) {
  1388. i++;
  1389. };
  1390. return name + i;
  1391. },
  1392. templateNameIsExist: function (name) {
  1393. if (projectObj.project.calcProgram.compiledTemplateMaps[name])
  1394. return true
  1395. else return false;
  1396. },
  1397. templateIsUsed: function (ID){
  1398. let nodes = projectObj.project.mainTree.items;
  1399. for (let node of nodes){
  1400. if (node.data && node.data.programID && node.data.programID == ID) {
  1401. return true;
  1402. }
  1403. };
  1404. return false;
  1405. },
  1406. fieldNameIsUsed: function(template, fieldName){
  1407. let fieldNameEn = projectObj.project.calcProgram.compiledFeeTypeMaps[fieldName];
  1408. for (var i = 0; i < template.calcItems.length; i++) {
  1409. if (template.calcItems[i].fieldName == fieldNameEn){
  1410. template.fieldNameTempUsed = i;
  1411. return true;
  1412. }
  1413. }
  1414. return false;
  1415. }
  1416. };
  1417. let executeObj = {
  1418. treeNode: null,
  1419. template: null,
  1420. tempCalcItem: null,
  1421. at: function(ID, isTender) {
  1422. let item = executeObj.template.compiledCalcItems[ID];
  1423. let rst = isTender ? item.tenderUnitFee : item.unitFee;
  1424. rst = parseFloat(rst);
  1425. return rst;
  1426. },
  1427. base: function(baseName, isTender) {
  1428. let me = executeObj;
  1429. // 量价、工料机形式的定额, 要把自己的市场单价用于计算程序中的基数。
  1430. if (calcTools.isVolumePrice(me.treeNode) || calcTools.isGljRation(me.treeNode))
  1431. return calcTools.marketPriceToBase(me.treeNode, baseName, isTender)
  1432. else{
  1433. if (!rationCalcBases[baseName]){
  1434. hintBox.infoBox('系统提示', '定额基数“' + baseName + '”末定义,计算错误。 (模板 ' + me.template.ID + ',规则 ' + me.tempCalcItem.ID +')', 1);
  1435. return 0;
  1436. }
  1437. else
  1438. return rationCalcBases[baseName](me.treeNode, isTender);
  1439. }
  1440. },
  1441. HJ: function () {
  1442. let me = this;
  1443. let p = me.treeNode.data.calcBaseValue ? me.treeNode.data.calcBaseValue : 0;
  1444. let q = calcTools.uiNodeQty(me.treeNode) ? calcTools.uiNodeQty(me.treeNode) : 1;
  1445. let u = (p / q).toDecimal(decimalObj.decimal('unitPrice', me.treeNode));
  1446. return u;
  1447. }
  1448. };
  1449. class CalcProgram {
  1450. constructor(project){
  1451. let me = this;
  1452. me.project = project;
  1453. me.datas = [];
  1454. me.rationMap = null;//定额 - 工料机映射临时变量
  1455. me.pgljMap = null;
  1456. project.registerModule(ModuleNames.calc_program, me);
  1457. };
  1458. // 兼容Project框架方法
  1459. getSourceType () {
  1460. return ModuleNames.calc_program;
  1461. };
  1462. // 兼容Project框架方法
  1463. // isInit:是否初始化,进入单位工程为true,导出接口为false,不需要存储费率临时数据
  1464. loadData (datas, isInit) {
  1465. this.datas = datas;
  1466. this.compileAllTemps(isInit);
  1467. };
  1468. // 兼容Project框架方法
  1469. doAfterUpdate (err, data) {
  1470. if(!err){
  1471. $.bootstrapLoading.end();
  1472. }
  1473. };
  1474. // 经测试,全部编译一次耗时0.003~0.004秒。耗时基本忽略不计。
  1475. compileAllTemps(isInit = false){
  1476. let me = this;
  1477. me.compiledFeeRates = {};
  1478. me.compiledLabourCoes = {};
  1479. me.compiledTemplates = {};
  1480. me.compiledTemplateMaps = {};
  1481. me.compiledTemplateNames = [];
  1482. me.compiledFeeTypeMaps = {};
  1483. me.compiledFeeTypeNames = [];
  1484. me.compiledCalcBases = {};
  1485. me.saveForReports = [];
  1486. me.feeRates = this.project.FeeRate.datas.rates;
  1487. me.labourCoes = this.project.labourCoe.datas.coes;
  1488. me.feeTypes = cpFeeTypes;
  1489. // me.calcBases = rationCalcBase;
  1490. me.templates = this.project.calcProgram.datas.templates;
  1491. // me.templates.push(defaultBillTemplate);
  1492. // 先编译公用的基础数据
  1493. me.compilePublics();
  1494. me.compileTemplateMaps();
  1495. for (let t of me.templates){
  1496. me.compileTemplate(t);
  1497. };
  1498. // 存储费率临时数据,报表用。
  1499. if (isInit && me.saveForReports.length > 0){
  1500. let saveDatas = {};
  1501. saveDatas.projectID = projectObj.project.projectInfo.ID;
  1502. saveDatas.calcItems = me.saveForReports;
  1503. CommonAjax.post('/calcProgram/saveCalcItems', saveDatas, function (result) {
  1504. if (result){
  1505. me.saveForReports = [];
  1506. };
  1507. });
  1508. };
  1509. };
  1510. compilePublics(){
  1511. let me = this;
  1512. for (let rate of me.feeRates) {
  1513. me.compiledFeeRates[rate.ID] = rate;
  1514. }
  1515. for (let coe of me.labourCoes) {
  1516. me.compiledLabourCoes[coe.ID] = coe;
  1517. }
  1518. for (let ft of me.feeTypes) {
  1519. me.compiledFeeTypeMaps[ft.type] = ft.name;
  1520. me.compiledFeeTypeMaps[ft.name] = ft.type; // 中文预编译,可靠性有待验证
  1521. me.compiledFeeTypeNames.push(ft.name);
  1522. }
  1523. /* for (let cb of me.calcBases) {
  1524. me.compiledCalcBases[cb.dispName] = cb; // 中文预编译,可靠性有待验证
  1525. }*/
  1526. };
  1527. compileTemplateMaps(){
  1528. let me = this;
  1529. function clearObj(obj){
  1530. for (let key in obj) {
  1531. delete obj[key];
  1532. }
  1533. };
  1534. clearObj(me.compiledTemplates);
  1535. clearObj(me.compiledTemplateMaps);
  1536. me.compiledTemplateNames.splice(0, me.compiledTemplateNames.length);
  1537. for (let t of me.templates){
  1538. me.compiledTemplates[t.ID] = t;
  1539. me.compiledTemplateMaps[t.ID] = t.name;
  1540. me.compiledTemplateMaps[t.name] = t.ID;
  1541. me.compiledTemplateNames.push(t.name);
  1542. };
  1543. };
  1544. compileTemplate(template){
  1545. let me = this;
  1546. // me.compiledTemplates[template.ID] = template;
  1547. // me.compiledTemplateMaps[template.ID] = template.name;
  1548. // me.compiledTemplateMaps[template.name] = template.ID;
  1549. // me.compiledTemplateNames.push(template.name);
  1550. template.hasCompiled = false;
  1551. template.errs = [];
  1552. let private_extract_ID = function(str, idx){
  1553. let subStr = str.slice(idx);
  1554. let patt = new RegExp(/@\d+/);
  1555. let atID = subStr.match(patt);
  1556. let ID = atID ? atID[0].slice(1) : null;
  1557. return ID;
  1558. };
  1559. let private_parse_ref = function(item, itemIdx){
  1560. let idx = item.expression.indexOf('@', 0);
  1561. while (idx >= 0) {
  1562. let ID = private_extract_ID(item.expression, idx);
  1563. let len = ID ? ID.toString().length : 0;
  1564. if (len) {
  1565. let subItem = template.compiledCalcItems[ID];
  1566. if (subItem) {
  1567. if (subItem.ID !== item.ID) {
  1568. private_parse_ref(subItem, template.compiledCalcItems[ID + "_idx"]);
  1569. } else {
  1570. template.errs.push("循环引用ID: " + ID);
  1571. }
  1572. } else {
  1573. template.errs.push("找不到ID: " + ID);
  1574. console.log('找不到ID: ' + ID);
  1575. }
  1576. }
  1577. idx = item.expression.indexOf('@', idx + len + 1);
  1578. }
  1579. if (template.compiledSeq.indexOf(itemIdx) < 0) {
  1580. template.compiledSeq.push(itemIdx);
  1581. }
  1582. };
  1583. let private_compile_items = function() {
  1584. for (let idx of template.compiledSeq) {
  1585. let item = template.calcItems[idx];
  1586. let lc = analyzer.calcItemLabourCoe(item);
  1587. // 用于界面显示。disExpr是公式模板,不允许修改:人工系数占位符被修改后变成数值,第二次无法正确替换。
  1588. item.dispExprUser = analyzer.getDispExprUser(item.dispExpr, lc);
  1589. if (item.expression == 'HJ')
  1590. item.compiledExpr = '$CE.HJ()'
  1591. else
  1592. item.compiledExpr = analyzer.getCompiledExpr(item.expression, lc);
  1593. if (item.feeRateID) {
  1594. let orgFeeRate = item.feeRate;
  1595. let cfr = me.compiledFeeRates[item.feeRateID];
  1596. item.feeRate = cfr ? cfr.rate : 100;
  1597. if (!orgFeeRate || (orgFeeRate && orgFeeRate != item.feeRate)){
  1598. me.saveForReports.push({templatesID: template.ID, calcItem: item});
  1599. }
  1600. };
  1601. // 字段名映射
  1602. item.displayFieldName = me.compiledFeeTypeMaps[item.fieldName];
  1603. }
  1604. };
  1605. if (template && template.calcItems && template.calcItems.length > 0) {
  1606. template.compiledSeq = [];
  1607. template.compiledCalcItems = {};
  1608. for (let i = 0; i < template.calcItems.length; i++) {
  1609. let item = template.calcItems[i];
  1610. item.dispExpr = analyzer.getDispExpr(item.expression, template);
  1611. template.compiledCalcItems[item.ID] = item;
  1612. template.compiledCalcItems[item.ID + "_idx"] = i;
  1613. }
  1614. for (let i = 0; i < template.calcItems.length; i++) {
  1615. let item = template.calcItems[i];
  1616. if (template.compiledSeq.indexOf(i) < 0) {
  1617. private_parse_ref(item, i);
  1618. }
  1619. }
  1620. if (template.errs.length == 0) {
  1621. private_compile_items();
  1622. template.hasCompiled = true;
  1623. } else {
  1624. console.log('errors: ' + template.errs.toString());
  1625. }
  1626. };
  1627. };
  1628. // 只计算treeNode自身。changedArr: 外部传来的一个数组,专门存储发生变动的节点。
  1629. innerCalc(treeNode, changedArr, tender){
  1630. let me = this;
  1631. // 仅用作树节点显示的工料机不能参与计算。
  1632. if (treeNode.sourceType === ModuleNames.ration_glj) return;
  1633. //设置定额工料机映射表
  1634. me.setRationMap();
  1635. treeNode.calcType = calcTools.getCalcType(treeNode);
  1636. let nQ = calcTools.uiNodeQty(treeNode);
  1637. let nTQ = calcTools.uiNodeTenderQty(treeNode);
  1638. function isBaseFeeType(type){
  1639. return ['labour', 'material', 'machine', 'mainMaterial', 'equipment'].indexOf(type) > -1;
  1640. };
  1641. /*删掉多余的费用。例如:从其它计算方式(有很多费)切换到公式计算方式(只需要common费),多出来的费要删除。
  1642. fieldNameArr 值取自:遍历treeNode的计算规则,取有绑定的字段名。这些字段名以外的fee是因旧计算多出来的,需要删除。 */
  1643. function deleteUselessFees(treeNode, fieldNameArr){
  1644. if (fieldNameArr){ // 用于计算程序没有绑定的费用类别,不要同步到清单,而清单因为以前计算过该类别又有值,需删除。如切换取费类别,旧费要清掉。
  1645. for (var i = 0; i < treeNode.data.fees.length; i++) {
  1646. let fee = treeNode.data.fees[i];
  1647. if ((fee.fieldName != 'estimate') && !fieldNameArr.includes(fee.fieldName)){
  1648. treeNode.data.fees.splice(i, 1);
  1649. delete treeNode.data.feesIndex[fee.fieldName];
  1650. treeNode.changed = true;
  1651. }
  1652. }
  1653. }
  1654. else{ // 总造价清单只留common, estimate两个费用类别。其它公式清单只留common。
  1655. let reserveArr = calcTools.isTotalCostBill(treeNode)? ['common', 'estimate']:['common'];
  1656. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1657. let feesArr = treeNode.data.fees;
  1658. for (let i = 0; i < feesArr.length; i++) {
  1659. if (!reserveArr.includes(feesArr[i].fieldName)) {
  1660. delete treeNode.data.feesIndex[feesArr[i].fieldName];
  1661. feesArr.splice(i, 1);
  1662. treeNode.changed = true;
  1663. }
  1664. }
  1665. };
  1666. };
  1667. };
  1668. // 父清单汇总子项(子清单或定额)的费用类别
  1669. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees || treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1670. treeNode.data.programID = null;
  1671. calcTools.initFees(treeNode);
  1672. let nodes = [];
  1673. if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){
  1674. calcTools.getGLJList(treeNode, true);
  1675. nodes = me.project.Ration.getRationNodes(treeNode);
  1676. } else { //固定清单材料(工程设备)暂估价比较特殊,不进行父项汇总(需求)
  1677. nodes = me.project.Bills.getGatherNodes(treeNode);
  1678. }
  1679. //else nodes = treeNode.children;
  1680. let rst = [];
  1681. for (let ft of cpFeeTypes) {
  1682. let ftObj = {};
  1683. ftObj.fieldName = ft.type;
  1684. ftObj.name = ft.name;
  1685. let buf = 0, btf = 0, btuf = 0, bttf = 0;
  1686. let bq = nQ ? nQ : 1;
  1687. let btq = nTQ ? nTQ : 1;
  1688. if (treeNode.calcType == treeNodeCalcType.ctGatherBillsFees){
  1689. for (let node of nodes) {
  1690. if (node.data.feesIndex && node.data.feesIndex[ft.type]) { // 父清单不要汇总综合单价。
  1691. btf = (btf + parseFloatPlus(node.data.feesIndex[ft.type].totalFee)).toDecimal(decimalObj.process);
  1692. bttf = (bttf + parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee)).toDecimal(decimalObj.process);
  1693. };
  1694. };
  1695. }
  1696. else if (treeNode.calcType == treeNodeCalcType.ctGatherRationsFees){ // 这里的算法要配合冷姐姐的神图才能看懂^_^
  1697. let sum_rtf = 0, sum_rttf = 0;
  1698. for (let node of nodes) {
  1699. let ruf = 0, rtuf = 0, rtf = 0, rttf = 0;
  1700. if (node.data.feesIndex && node.data.feesIndex[ft.type]) {
  1701. ruf = parseFloatPlus(node.data.feesIndex[ft.type].unitFee).toDecimal(decimalObj.bills.unitPrice);
  1702. rtuf = parseFloatPlus(node.data.feesIndex[ft.type].tenderUnitFee).toDecimal(decimalObj.bills.unitPrice);
  1703. rtf = parseFloatPlus(node.data.feesIndex[ft.type].totalFee).toDecimal(decimalObj.bills.totalPrice);
  1704. rttf = parseFloatPlus(node.data.feesIndex[ft.type].tenderTotalFee).toDecimal(decimalObj.bills.totalPrice);
  1705. };
  1706. // 取费方式为子目含量,清单行/列的XX单价应 =ROUND( ∑ROUND(定额XX单价*含量,清单单价精度),清单单价精度)
  1707. if (me.project.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  1708. buf = (buf + (ruf * parseFloatPlus(node.data.contain)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1709. node.data.tenderContaion = (node.data.tenderQuantity / bq).toDecimal(decimalObj.process);
  1710. btuf = (btuf + (rtuf * parseFloatPlus(node.data.tenderContaion)).toDecimal(decimalObj.bills.unitPrice)).toDecimal(decimalObj.process);
  1711. };
  1712. sum_rtf = (sum_rtf + rtf).toDecimal(decimalObj.process);
  1713. sum_rttf = (sum_rttf + rttf).toDecimal(decimalObj.process);
  1714. };
  1715. if (me.project.property.billsCalcMode == leafBillGetFeeType.rationPriceConverse ||
  1716. me.project.property.billsCalcMode == leafBillGetFeeType.rationPrice) {
  1717. buf = (sum_rtf / bq).toDecimal(decimalObj.process);
  1718. btuf = (sum_rttf / btq).toDecimal(decimalObj.process);
  1719. };
  1720. if (isBaseFeeType(ft.type) ||
  1721. (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice && ft.type == "common")){
  1722. btf = sum_rtf;
  1723. bttf = sum_rttf;
  1724. }
  1725. else{
  1726. btf = (buf.toDecimal(decimalObj.bills.unitPrice) * bq).toDecimal(decimalObj.process);
  1727. bttf = (btuf.toDecimal(decimalObj.bills.unitPrice) * btq).toDecimal(decimalObj.process);
  1728. };
  1729. };
  1730. ftObj.totalFee = btf.toDecimal(decimalObj.bills.totalPrice);
  1731. ftObj.tenderTotalFee = bttf.toDecimal(decimalObj.bills.totalPrice);
  1732. ftObj.unitFee = buf.toDecimal(decimalObj.bills.unitPrice);
  1733. ftObj.tenderUnitFee = btuf.toDecimal(decimalObj.bills.unitPrice);
  1734. calcTools.checkFeeField(treeNode, ftObj);
  1735. rst.push(ftObj);
  1736. };
  1737. treeNode.data.calcTemplate = {"calcItems": rst};
  1738. }
  1739. // 叶子清单无子结点、无公式计算(啥都没有时)
  1740. else if (treeNode.calcType == treeNodeCalcType.ctNull){
  1741. delete treeNode.data.gljList;
  1742. // 不能直接删除该属性,否则无法冲掉库中已存储的值。下同。
  1743. if (treeNode.data.calcBase){
  1744. treeNode.data.calcBase = null;
  1745. treeNode.changed = true;
  1746. }
  1747. if (treeNode.data.calcBaseValue){
  1748. treeNode.data.calcBaseValue = null;
  1749. treeNode.changed = true;
  1750. }
  1751. if (treeNode.data.tenderCalcBaseValue){
  1752. treeNode.data.tenderCalcBaseValue = null;
  1753. treeNode.changed = true;
  1754. }
  1755. if (treeNode.data.programID) {
  1756. treeNode.data.programID = null;
  1757. treeNode.changed = true;
  1758. };
  1759. // 第1、2部分以外的叶子清单在没有公式的情况下可以手工修改综合单价并参与计算。
  1760. // 2017-09-27 需求改了,除了第 1 、 2.2部分以外,都可以手工修改综合单价、综合合价并参与计算
  1761. //if(!calcTools.isFBFX(treeNode) && !calcTools.isTechMeasure(treeNode)){ // if(!calcTools.isInheritFrom(treeNode, [fixedFlag.SUB_ENGINERRING, fixedFlag.MEASURE]))
  1762. // 在没有公式的情况下可以手工修改综合单价并参与计算
  1763. if(calcTools.canCalcToTalFeeByOwn(treeNode)){
  1764. if (treeNode.data.feesIndex && treeNode.data.feesIndex.common){
  1765. let ftObj = {};
  1766. ftObj.fieldName = 'common';
  1767. ftObj.unitFee = parseFloatPlus(treeNode.data.feesIndex.common.unitFee);
  1768. ftObj.totalFee = (ftObj.unitFee * nQ).toDecimal(decimalObj.bills.totalPrice);
  1769. ftObj.tenderUnitFee = ftObj.unitFee;
  1770. ftObj.tenderTotalFee = (ftObj.tenderUnitFee * nTQ).toDecimal(decimalObj.bills.totalPrice);
  1771. calcTools.checkFeeField(treeNode, ftObj);
  1772. }
  1773. } else{
  1774. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1775. treeNode.data.fees = null;
  1776. treeNode.data.feesIndex = null;
  1777. treeNode.changed = true;
  1778. }
  1779. };
  1780. treeNode.data.calcTemplate = {"calcItems": []};
  1781. }
  1782. // 叶子清单公式计算
  1783. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  1784. delete treeNode.data.gljList;
  1785. if (treeNode.data.programID) {
  1786. treeNode.data.programID = null;
  1787. treeNode.changed = true;
  1788. }
  1789. let f = calcTools.getFeeRateByNode(treeNode);
  1790. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  1791. let tb = treeNode.data.tenderCalcBaseValue ? treeNode.data.tenderCalcBaseValue : 0;
  1792. let q = nQ ? nQ : 1;
  1793. let tq = nTQ ? nTQ : 1;
  1794. let uf = (b * f * 0.01 / q).toDecimal(decimalObj.bills.unitPrice);
  1795. let tuf = (tb * f * 0.01 / tq).toDecimal(decimalObj.bills.unitPrice);
  1796. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100) : (uf * q);
  1797. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  1798. let ttf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (tb * f / 100) : (tuf * tq);
  1799. ttf = ttf.toDecimal(decimalObj.bills.totalPrice);
  1800. deleteUselessFees(treeNode);
  1801. calcTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf, 'tenderUnitFee': tuf, 'tenderTotalFee': ttf});
  1802. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  1803. if (calcTools.isTotalCostBill(treeNode)){
  1804. // 公式叶子清单没有暂估费,但总造价清单除外。
  1805. calcTools.estimateFee(treeNode);
  1806. calcTools.initSummaryFee(treeNode);
  1807. treeNode.data.summaryFees.totalFee = tf;
  1808. treeNode.data.summaryFees.estimateFee = calcTools.getFee(treeNode, 'estimate.totalFee');
  1809. treeNode.data.summaryFees.safetyFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION), 'common.totalFee');
  1810. treeNode.data.summaryFees.chargeFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.CHARGE), 'common.totalFee');
  1811. }
  1812. treeNode.data.calcTemplate = {"calcItems": []};
  1813. }
  1814. // 定额或叶子清单自己的计算程序计算
  1815. else{
  1816. let fnArr = [];
  1817. calcTools.getGLJList(treeNode, true);
  1818. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1819. // 量价、工料机类型的定额要求市场合价
  1820. if (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation){
  1821. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1822. let mtf = (muf * nQ).toDecimal(decimalObj.ration.totalPrice);
  1823. if (treeNode.data.marketTotalFee != mtf){
  1824. treeNode.data.marketTotalFee = mtf;
  1825. treeNode.changed = true;
  1826. };
  1827. };
  1828. };
  1829. // 2018-08-27 zhang 插入空定额的时候,取费专业也为空
  1830. // if (treeNode.data.programID == undefined) treeNode.data.programID = projectObj.project.projectInfo.property.engineering;
  1831. let template = me.compiledTemplates[treeNode.data.programID];
  1832. treeNode.data.calcTemplate = template;
  1833. if (treeNode && template && template.hasCompiled) {//2018-08-27 空行的时候,取费专业为空,template也为空,加入template非空判断
  1834. let $CE = executeObj;
  1835. $CE.treeNode = treeNode;
  1836. $CE.template = template;
  1837. calcTools.initFees(treeNode);
  1838. for (let idx of template.compiledSeq) {
  1839. let calcItem = template.calcItems[idx];
  1840. $CE.tempCalcItem = calcItem;
  1841. let feeRate = 100; // 100%
  1842. if (calcItem.feeRate != undefined)
  1843. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  1844. // console.log(`[${calcItem.ID}]: ${calcItem.compiledExpr}`); // for test.
  1845. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1846. calcItem.totalFee = (calcItem.unitFee * calcTools.uiNodeQty(treeNode)).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1847. // if (tender == tenderTypes.ttCalc) {
  1848. let tExpr = analyzer.getCompiledTenderExpr(calcItem.compiledExpr);
  1849. calcItem.tenderUnitFee = (eval(tExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1850. calcItem.tenderTotalFee = (calcItem.tenderUnitFee * treeNode.data.tenderQuantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1851. // };
  1852. if (calcItem.fieldName) {
  1853. fnArr.push(calcItem.fieldName);
  1854. calcTools.checkFeeField(treeNode, calcItem);
  1855. };
  1856. };
  1857. if (tender == tenderTypes.ttReverseRation || tender == tenderTypes.ttReverseGLJ)
  1858. this.calcTenderReverse(treeNode, tender);
  1859. deleteUselessFees(treeNode, fnArr);
  1860. };
  1861. };
  1862. if (!calcTools.isTotalCostBill(treeNode)) // 已在上面的分支中计算过
  1863. calcTools.estimateFee(treeNode);
  1864. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  1865. };
  1866. // 存储、刷新零散的多个结点。
  1867. saveNodes(treeNodes, callback){
  1868. if (treeNodes.length < 1) {
  1869. $.bootstrapLoading.end();
  1870. this.rationMap = null;
  1871. this.pgljMap = null;
  1872. return;
  1873. }
  1874. let me = this;
  1875. let dataArr = [];
  1876. for (let node of treeNodes){
  1877. if (node.changed){
  1878. let data = calcTools.cutNodeForSave(node);
  1879. let newData = {'type': node.sourceType, 'data': data};
  1880. dataArr.push(newData);
  1881. }
  1882. };
  1883. if (dataArr.length < 1) {
  1884. $.bootstrapLoading.end();
  1885. return;
  1886. };
  1887. $.bootstrapLoading.start();
  1888. let startTime = +new Date();
  1889. me.project.updateNodes(dataArr, function (data) {
  1890. let endShowTime = +new Date();
  1891. console.log(`保存所需时间——${endShowTime - startTime}`);
  1892. me.rationMap = null;
  1893. me.pgljMap = null;
  1894. if(callback){
  1895. callback(data);
  1896. };
  1897. for (let node of treeNodes) { delete node.changed };
  1898. projectObj.mainController.refreshTreeNode(treeNodes);
  1899. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。警告:第二个参数千万不能改成3,否则死循环!
  1900. if (activeSubSheetIsCalcProgram())
  1901. calcProgramObj.refreshCalcProgram(projectObj.project.mainTree.selected, 2);
  1902. $.bootstrapLoading.end();
  1903. });
  1904. };
  1905. // 计算本节点及所有会被影响到的节点,如:所有父节点(默认,可选)、公式引用节点(默认,可选)。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1906. calculate(treeNode, calcParents = true, calcFormulas = true, tender){
  1907. let me = this;
  1908. let changedNodes = [];
  1909. me.innerCalc(treeNode, changedNodes, tender);
  1910. if (treeNode.changed) {
  1911. // 计算父结点
  1912. if (calcParents){
  1913. let curNode = treeNode.parent;
  1914. while (curNode){
  1915. me.innerCalc(curNode, changedNodes, tender);
  1916. curNode = curNode.parent;
  1917. };
  1918. };
  1919. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1920. if (calcFormulas) {
  1921. me.calcFormulaNodes(changedNodes, tender);
  1922. };
  1923. };
  1924. return changedNodes;
  1925. };
  1926. // 计算并保存本节点及所有会被影响到的节点。
  1927. calcAndSave(treeNode, callback, tender){
  1928. let changedNodes = this.calculate(treeNode, true, true, tender);
  1929. this.saveNodes(changedNodes, callback);
  1930. };
  1931. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1932. calcAllType.catAll 计算所有树结点 (默认值)
  1933. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1934. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  1935. (calcAllType.catRations时程序中做了特殊处理,实际上是计算所有树结点!)
  1936. 调价相关参数:
  1937. tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  1938. */
  1939. calcAllNodes(calcType = calcAllType.catAll, tender){
  1940. let me = this;
  1941. let changedNodes = [];
  1942. function calcNodes(nodes) {
  1943. for (let node of nodes) {
  1944. if (node.children.length > 0) {
  1945. calcNodes(node.children);
  1946. };
  1947. if (calcType == calcAllType.catAll || calcType == node.sourceType) {
  1948. node.calcType = calcTools.getCalcType(node);
  1949. if (node.calcType != treeNodeCalcType.ctCalcBaseValue)
  1950. me.innerCalc(node, changedNodes, tender);
  1951. };
  1952. }
  1953. };
  1954. // calcAllType.catRations 参数情况不会计算父结点(因为父结点是清单),所以这里进行特殊处理。
  1955. if (calcType == calcAllType.catRations)
  1956. calcType == calcAllType.catAll;
  1957. calcNodes(me.project.mainTree.roots);
  1958. me.calcFormulaNodes(changedNodes, tender);
  1959. if (tender){ // 普通计算不执行,只有3种调价计算才清。
  1960. for(let node of projectObj.project.mainTree.items){
  1961. this.clearTenderCache(node);
  1962. };
  1963. };
  1964. return changedNodes;
  1965. };
  1966. // tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  1967. calcAllNodesAndSave(calcType = calcAllType.catAll, callback, tender){
  1968. let changedNodes = this.calcAllNodes(calcType, tender);
  1969. this.saveNodes(changedNodes, callback);
  1970. };
  1971. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  1972. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  1973. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1974. calcNodesAndSave(nodes, callback, tender){
  1975. let me = this, rationNodes = [], billNodes = [], leafBills = [], allChangedNodes = [];
  1976. for (let node of nodes) {
  1977. if (node.sourceType == ModuleNames.ration)
  1978. rationNodes.push(node)
  1979. else
  1980. billNodes.push(node);
  1981. };
  1982. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1983. for (let ration of rationNodes) {
  1984. me.innerCalc(ration, allChangedNodes, tender);
  1985. let leafBill = ration.parent;
  1986. if (leafBill && leafBills.indexOf(leafBill) < 0)
  1987. leafBills.push(leafBill);
  1988. };
  1989. billNodes.merge(leafBills);
  1990. for (let bill of billNodes){
  1991. let changeBills = me.calculate(bill, true, false, tender);
  1992. allChangedNodes.merge(changeBills);
  1993. };
  1994. me.calcFormulaNodes(allChangedNodes, tender);
  1995. me.saveNodes(allChangedNodes, callback);
  1996. };
  1997. // 计算全部公式项。 (changedArr:将通过本方法后发生改变的节点存入changedArr中)
  1998. calcFormulaNodes(changedArr, tender){
  1999. let me = this;
  2000. let formulaNodes = cbTools.getFormulaNodes(true);
  2001. if (formulaNodes.length == 0) return;
  2002. for (let formulaNode of formulaNodes){
  2003. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  2004. projectObj.project.calcBase.calculate(formulaNode, true);
  2005. if (projectObj.project.calcBase.success){
  2006. // 计算公式结点
  2007. me.innerCalc(formulaNode, changedArr, tender);
  2008. // 计算父结点
  2009. if (formulaNode.changed){
  2010. let curNode = formulaNode.parent;
  2011. while (curNode){
  2012. me.innerCalc(curNode, changedArr, tender);
  2013. curNode = curNode.parent;
  2014. };
  2015. };
  2016. };
  2017. };
  2018. };
  2019. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  2020. calcLeafAndSave(treeNode, tender){
  2021. let me = this;
  2022. if(!calcTools.isLeafBill(treeNode)) return;
  2023. if (treeNode.children && treeNode.children.length > 0) {
  2024. let changedNodes = [];
  2025. for (let child of treeNode.children){
  2026. me.innerCalc(child, changedNodes);
  2027. };
  2028. let curChangeds = me.calculate(treeNode, true, true, tender);
  2029. changedNodes.merge(curChangeds);
  2030. me.saveNodes(changedNodes);
  2031. };
  2032. };
  2033. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  2034. getTotalFee(baseNodes, excludeNodes, tender){
  2035. let rst = 0;
  2036. const totalFeeType = tender ? 'common.tenderTotalFee' : 'common.totalFee';
  2037. function calcNodes(nodes) {
  2038. for (let node of nodes) {
  2039. if(!node){
  2040. continue;
  2041. }
  2042. if (!excludeNodes.includes(node)){
  2043. if (node.source && node.source.children && node.source.children.length > 0) {
  2044. calcNodes(node.children);
  2045. }
  2046. else{
  2047. if (node.sourceType == ModuleNames.bills) {
  2048. rst = (rst + calcTools.getFee(node, totalFeeType)).toDecimal(decimalObj.decimal("totalPrice", node));
  2049. };
  2050. }
  2051. }
  2052. }
  2053. };
  2054. calcNodes(baseNodes);
  2055. return rst;
  2056. };
  2057. // 税前工程造价 ,adj调价
  2058. getBeforeTaxTotalFee(excludeNodes, tender){
  2059. let baseNodes = [], me = this;
  2060. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  2061. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  2062. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  2063. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  2064. return me.getTotalFee(baseNodes, excludeNodes, tender);
  2065. };
  2066. // 反向调价
  2067. calcTenderReverse(treeNode, tender){
  2068. if (tender == tenderTypes.ttReverseRation) {
  2069. if (treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2070. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2071. treeNode.changed = true;
  2072. }
  2073. };
  2074. if (!treeNode.data.targetTotalFee){
  2075. if (treeNode.data.targetUnitFee){
  2076. treeNode.data.targetTotalFee = (treeNode.data.targetUnitFee * treeNode.data.quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2077. treeNode.changed = true;
  2078. }
  2079. else{ // 既没有目标金额也没有目标单价,此时要初始化使调价合价=原始综合合价,调价单价=原始综合单价。
  2080. if (treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2081. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2082. treeNode.changed = true;
  2083. };
  2084. if (treeNode.data.feesIndex.common.tenderTotalFee != treeNode.data.feesIndex.common.totalFee) {
  2085. treeNode.data.feesIndex.common.tenderTotalFee = treeNode.data.feesIndex.common.totalFee;
  2086. treeNode.changed = true;
  2087. };
  2088. return;
  2089. }
  2090. };
  2091. if (!treeNode.data.targetUnitFee || (parseFloat(treeNode.data.targetUnitFee) == 0)){
  2092. if (calcTools.hasQuantity(treeNode))
  2093. treeNode.data.targetUnitFee = (treeNode.data.targetTotalFee / treeNode.data.quantity).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  2094. }
  2095. let coe = 1;
  2096. if (treeNode.data.feesIndex.common.totalFee != 0)
  2097. coe = (treeNode.data.targetUnitFee / treeNode.data.feesIndex.common.unitFee).toDecimal(decimalObj.process);
  2098. if (tender == tenderTypes.ttReverseRation){
  2099. treeNode.data.tenderQuantity = (treeNode.data.quantity * coe).toDecimal(decimalObj.decimal("quantity", treeNode));
  2100. if (treeNode.data.rationQuantityCoe != coe){
  2101. treeNode.data.rationQuantityCoe = coe;
  2102. treeNode.changed = true;
  2103. };
  2104. let ttf = (treeNode.data.tenderQuantity * treeNode.data.feesIndex.common.tenderUnitFee).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2105. if (treeNode.data.feesIndex.common.tenderTotalFee != ttf){
  2106. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  2107. treeNode.changed = true;
  2108. };
  2109. }else if (tender == tenderTypes.ttReverseGLJ){
  2110. // treeNode.data.tenderQuantity = treeNode.data.quantity; // 这句好像多余,因为调用calculate()时里面会重新计算tenderQuantity,有问题再放开AAAAA
  2111. let qcObj = treeNode.data.quantityCoe;
  2112. if (!qcObj || calcTools.isEmptyObject(qcObj)){
  2113. treeNode.data.quantityCoe = {labour: coe, material: coe, machine: coe, main: coe, equipment: coe};
  2114. treeNode.changed = true;
  2115. }else{
  2116. for (let pn in qcObj){
  2117. if (qcObj[pn] != coe){
  2118. qcObj[pn] = coe;
  2119. treeNode.changed = true;
  2120. }
  2121. };
  2122. };
  2123. projectObj.project.calcProgram.calculate(treeNode, false, false, tenderTypes.ttCalc); // 再正向算
  2124. };
  2125. };
  2126. // 清理调价缓存数据
  2127. clearTenderCache(treeNode){
  2128. // 这些属性值为什么不定义在一个对象里?因为每次要判断对象是否存在,十分麻烦。不如直接写简单。分散书写,统一处理也很好用。
  2129. if (treeNode.data.tender_activeTotal) delete treeNode.data.tender_activeTotal;
  2130. if (treeNode.data.tender_activeTarget) delete treeNode.data.tender_activeTarget;
  2131. if (treeNode.data.tender_fullLoad) delete treeNode.data.tender_fullLoad;
  2132. if (treeNode.data.tender_distribute){
  2133. delete treeNode.data.targetTotalFee;
  2134. delete treeNode.data.targetUnitFee;
  2135. delete treeNode.data.tender_distribute;
  2136. };
  2137. };
  2138. // 调价:分摊前的准备工作。主要标记满载的结点。
  2139. prepareForDistribute(treeNode){
  2140. if (!treeNode) return;
  2141. if (treeNode.firstChild())
  2142. this.prepareForDistribute(treeNode.firstChild());
  2143. if (treeNode.children.length == 0){
  2144. // if (!treeNode.data.feesIndex['common'])
  2145. // treeNode.data.tender_fullLoad = true
  2146. // else
  2147. // treeNode.data.tender_fullLoad = false;
  2148. }
  2149. else{
  2150. let full = true;
  2151. for (let i = 0; i < treeNode.children.length; i++) {
  2152. let child = treeNode.children[i];
  2153. if (!child.data.tender_fullLoad && !calcTools.hasTargetTotalFee(child)){
  2154. full = false;
  2155. break;
  2156. };
  2157. }
  2158. if (full) {
  2159. let total = 0, target = 0;
  2160. for (let i = 0; i < treeNode.children.length; i++) {
  2161. let child = treeNode.children[i];
  2162. total = total + parseFloat(child.data.feesIndex['common'].totalFee);
  2163. target = target + parseFloat(child.data.targetTotalFee);
  2164. };
  2165. treeNode.data.tender_activeTotal = total;
  2166. treeNode.data.tender_activeTarget = target;
  2167. treeNode.data.targetTotalFee = target;
  2168. treeNode.data.tender_distribute = 2;
  2169. };
  2170. treeNode.data.tender_fullLoad = full;
  2171. };
  2172. /*if (calcTools.isBill(treeNode) && (treeNode.data.tender_fullLoad != undefined)){
  2173. console.log(treeNode.data.name + ',tender_fullLoad: ' + treeNode.data.tender_fullLoad + ', target: ' + calcTools.hasTargetTotalFee(treeNode));
  2174. };*/
  2175. /* if (calcTools.isRationItem(treeNode)){
  2176. console.log(treeNode.data);
  2177. }*/
  2178. if (treeNode.nextSibling)
  2179. this.prepareForDistribute(treeNode.nextSibling);
  2180. };
  2181. // 调价之分摊目标合价:从父到子往下分摊。
  2182. distributeTargetTotalFee(treeNode){
  2183. if (!treeNode) return;
  2184. if (treeNode.data.feesIndex['common']){ // 空清单忽略
  2185. // 默认能够执行到这里时每个节点已经被初始化,缓存已删除
  2186. treeNode.data.tender_activeTotal = treeNode.data.feesIndex['common'].totalFee;
  2187. // 开始分摊:只分摊自有目标金额、从父结点分摊到的金额(不分摊子结点摊上来的金额)
  2188. if (calcTools.hasTargetTotalFee(treeNode) && !(treeNode.data.tender_distribute && treeNode.data.tender_distribute == 2)){
  2189. treeNode.data.tender_activeTarget = treeNode.data.targetTotalFee;
  2190. // 先把会破坏金额比例关系的孩子排除:1.有目标金额的 2.满载的(孙子全满,没有分摊空间,所以实质上该孩子的金额已被锁死无法分摊)
  2191. for (let i = 0; i < treeNode.children.length; i++) {
  2192. let child = treeNode.children[i];
  2193. if (!child.data.feesIndex['common']) continue; // 无计算金额的(如空清单)当它不存在
  2194. child.data.tender_activeTotal = child.data.feesIndex['common'].totalFee;
  2195. if (calcTools.hasTargetTotalFee(child)){
  2196. child.data.tender_activeTarget = child.data.targetTotalFee;
  2197. console.log(treeNode.data.tender_activeTotal);
  2198. console.log(child.data.tender_activeTotal);
  2199. treeNode.data.tender_activeTotal = treeNode.data.tender_activeTotal - child.data.tender_activeTotal;
  2200. treeNode.data.tender_activeTarget = treeNode.data.tender_activeTarget - child.data.tender_activeTarget;
  2201. };
  2202. }
  2203. if (treeNode.data.tender_activeTotal != 0){
  2204. let coe = (treeNode.data.tender_activeTarget / treeNode.data.tender_activeTotal).toDecimal(decimalObj.process);
  2205. for (let i = 0; i < treeNode.children.length; i++) {
  2206. let child = treeNode.children[i];
  2207. if (!child.data.feesIndex['common']) continue;
  2208. if (!calcTools.hasTargetTotalFee(child)){
  2209. child.data.tender_activeTarget = (coe * child.data.tender_activeTotal).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2210. child.data.targetTotalFee = child.data.tender_activeTarget;
  2211. child.data.tender_distribute = 1; // 1表示分摊金额来自父结点。2表示分摊金额来自孩子结点。
  2212. }
  2213. }
  2214. };
  2215. };
  2216. }
  2217. if (treeNode.firstChild())
  2218. this.distributeTargetTotalFee(treeNode.firstChild());
  2219. if (treeNode.nextSibling)
  2220. this.distributeTargetTotalFee(treeNode.nextSibling);
  2221. };
  2222. setRationMap(){
  2223. if(this.rationMap == null){
  2224. this.rationMap = {};
  2225. for(let glj of projectObj.project.ration_glj.datas){
  2226. if(this.rationMap[glj.rationID]){
  2227. this.rationMap[glj.rationID].push(glj)
  2228. }else {
  2229. this.rationMap[glj.rationID] = [glj];
  2230. }
  2231. }
  2232. }
  2233. if(this.pgljMap == null ){
  2234. this.pgljMap = _.indexBy(projectObj.project.projectGLJ.datas.gljList, 'id');
  2235. }
  2236. };
  2237. getGljArrByRation(ration){
  2238. if (ration.type == rationType.gljRation){
  2239. let glj = JSON.parse(JSON.stringify(ration));
  2240. glj.type = glj.subType;
  2241. glj.quantity = 1;
  2242. glj.totalQuantity = parseFloatPlus(ration.quantity);
  2243. return [glj];
  2244. } else{
  2245. if(!this.rationMap) return [];
  2246. let result = this.rationMap[ration.ID];
  2247. if(!result) return [];
  2248. result = gljOprObj.combineWithProjectGlj(result,false,ration,this.pgljMap);
  2249. return result;
  2250. }
  2251. };
  2252. };
  2253. // export default analyzer;