calc_program.js 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  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. calcTools.checkFeeField(treeNode, ftObj);
  1770. }
  1771. } else{
  1772. if (treeNode.data.fees && treeNode.data.fees.length > 0){
  1773. treeNode.data.fees = null;
  1774. treeNode.data.feesIndex = null;
  1775. treeNode.changed = true;
  1776. }
  1777. };
  1778. treeNode.data.calcTemplate = {"calcItems": []};
  1779. }
  1780. // 叶子清单公式计算
  1781. else if (treeNode.calcType == treeNodeCalcType.ctCalcBaseValue){
  1782. delete treeNode.data.gljList;
  1783. if (treeNode.data.programID) {
  1784. treeNode.data.programID = null;
  1785. treeNode.changed = true;
  1786. }
  1787. let f = calcTools.getFeeRateByNode(treeNode);
  1788. let b = treeNode.data.calcBaseValue ? treeNode.data.calcBaseValue : 0;
  1789. let tb = treeNode.data.tenderCalcBaseValue ? treeNode.data.tenderCalcBaseValue : 0;
  1790. let q = nQ ? nQ : 1;
  1791. let tq = nTQ ? nTQ : 1;
  1792. let uf = (b * f * 0.01 / q).toDecimal(decimalObj.bills.unitPrice);
  1793. let tuf = (tb * f * 0.01 / tq).toDecimal(decimalObj.bills.unitPrice);
  1794. let tf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (b * f / 100) : (uf * q);
  1795. tf = tf.toDecimal(decimalObj.bills.totalPrice);
  1796. let ttf = (me.project.property.billsCalcMode === leafBillGetFeeType.rationPrice) ? (tb * f / 100) : (tuf * tq);
  1797. ttf = ttf.toDecimal(decimalObj.bills.totalPrice);
  1798. deleteUselessFees(treeNode);
  1799. calcTools.checkFeeField(treeNode, {'fieldName': 'common', 'unitFee': uf, 'totalFee': tf, 'tenderUnitFee': tuf, 'tenderTotalFee': ttf});
  1800. // 总造价清单还要做单项工程、建设项目的四大项金额汇总
  1801. if (calcTools.isTotalCostBill(treeNode)){
  1802. // 公式叶子清单没有暂估费,但总造价清单除外。
  1803. calcTools.estimateFee(treeNode);
  1804. calcTools.initSummaryFee(treeNode);
  1805. treeNode.data.summaryFees.totalFee = tf;
  1806. treeNode.data.summaryFees.estimateFee = calcTools.getFee(treeNode, 'estimate.totalFee');
  1807. treeNode.data.summaryFees.safetyFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.SAFETY_CONSTRUCTION), 'common.totalFee');
  1808. treeNode.data.summaryFees.chargeFee = calcTools.getFee(calcTools.getNodeByFlag(fixedFlag.CHARGE), 'common.totalFee');
  1809. }
  1810. treeNode.data.calcTemplate = {"calcItems": []};
  1811. }
  1812. // 定额或叶子清单自己的计算程序计算
  1813. else{
  1814. let fnArr = [];
  1815. calcTools.getGLJList(treeNode, true);
  1816. if (treeNode.calcType == treeNodeCalcType.ctRationCalcProgram) {
  1817. // 量价、工料机类型的定额要求市场合价
  1818. if (treeNode.data.type == rationType.volumePrice || treeNode.data.type == rationType.gljRation){
  1819. let muf = treeNode.data.marketUnitFee ? treeNode.data.marketUnitFee : 0;
  1820. let mtf = (muf * nQ).toDecimal(decimalObj.ration.totalPrice);
  1821. if (treeNode.data.marketTotalFee != mtf){
  1822. treeNode.data.marketTotalFee = mtf;
  1823. treeNode.changed = true;
  1824. };
  1825. };
  1826. };
  1827. // 2018-08-27 zhang 插入空定额的时候,取费专业也为空
  1828. // if (treeNode.data.programID == undefined) treeNode.data.programID = projectObj.project.projectInfo.property.engineering;
  1829. let template = me.compiledTemplates[treeNode.data.programID];
  1830. treeNode.data.calcTemplate = template;
  1831. if (treeNode && template && template.hasCompiled) {//2018-08-27 空行的时候,取费专业为空,template也为空,加入template非空判断
  1832. let $CE = executeObj;
  1833. $CE.treeNode = treeNode;
  1834. $CE.template = template;
  1835. calcTools.initFees(treeNode);
  1836. for (let idx of template.compiledSeq) {
  1837. let calcItem = template.calcItems[idx];
  1838. $CE.tempCalcItem = calcItem;
  1839. let feeRate = 100; // 100%
  1840. if (calcItem.feeRate != undefined)
  1841. feeRate = parseFloat(calcItem.feeRate).toDecimal(decimalObj.feeRate);
  1842. // console.log(`[${calcItem.ID}]: ${calcItem.compiledExpr}`); // for test.
  1843. calcItem.unitFee = (eval(calcItem.compiledExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1844. calcItem.totalFee = (calcItem.unitFee * calcTools.uiNodeQty(treeNode)).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1845. // if (tender == tenderTypes.ttCalc) {
  1846. let tExpr = analyzer.getCompiledTenderExpr(calcItem.compiledExpr);
  1847. calcItem.tenderUnitFee = (eval(tExpr) * feeRate * 0.01).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  1848. calcItem.tenderTotalFee = (calcItem.tenderUnitFee * treeNode.data.tenderQuantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  1849. // };
  1850. if (calcItem.fieldName) {
  1851. fnArr.push(calcItem.fieldName);
  1852. calcTools.checkFeeField(treeNode, calcItem);
  1853. };
  1854. };
  1855. if (tender == tenderTypes.ttReverseRation || tender == tenderTypes.ttReverseGLJ)
  1856. this.calcTenderReverse(treeNode, tender);
  1857. deleteUselessFees(treeNode, fnArr);
  1858. };
  1859. };
  1860. if (!calcTools.isTotalCostBill(treeNode)) // 已在上面的分支中计算过
  1861. calcTools.estimateFee(treeNode);
  1862. if (treeNode.changed && !changedArr.includes(treeNode)) changedArr.push(treeNode);
  1863. };
  1864. // 存储、刷新零散的多个结点。
  1865. saveNodes(treeNodes, callback){
  1866. if (treeNodes.length < 1) {
  1867. $.bootstrapLoading.end();
  1868. this.rationMap = null;
  1869. this.pgljMap = null;
  1870. return;
  1871. }
  1872. let me = this;
  1873. let dataArr = [];
  1874. for (let node of treeNodes){
  1875. if (node.changed){
  1876. let data = calcTools.cutNodeForSave(node);
  1877. let newData = {'type': node.sourceType, 'data': data};
  1878. dataArr.push(newData);
  1879. }
  1880. };
  1881. if (dataArr.length < 1) {
  1882. $.bootstrapLoading.end();
  1883. return;
  1884. };
  1885. $.bootstrapLoading.start();
  1886. let startTime = +new Date();
  1887. me.project.updateNodes(dataArr, function (data) {
  1888. let endShowTime = +new Date();
  1889. console.log(`保存所需时间——${endShowTime - startTime}`);
  1890. me.rationMap = null;
  1891. me.pgljMap = null;
  1892. if(callback){
  1893. callback(data);
  1894. };
  1895. for (let node of treeNodes) { delete node.changed };
  1896. projectObj.mainController.refreshTreeNode(treeNodes);
  1897. // 批量树结点计算后,计算程序早已物是人非,所以这里要重新计算一下。警告:第二个参数千万不能改成3,否则死循环!
  1898. if (activeSubSheetIsCalcProgram())
  1899. calcProgramObj.refreshCalcProgram(projectObj.project.mainTree.selected, 2);
  1900. $.bootstrapLoading.end();
  1901. });
  1902. };
  1903. // 计算本节点及所有会被影响到的节点,如:所有父节点(默认,可选)、公式引用节点(默认,可选)。修改一个树节点,实际上要计算和保存的是一批树结点:层层父结点、被其它结点(的公式)引用的公式结点。
  1904. calculate(treeNode, calcParents = true, calcFormulas = true, tender){
  1905. let me = this;
  1906. let changedNodes = [];
  1907. me.innerCalc(treeNode, changedNodes, tender);
  1908. if (treeNode.changed) {
  1909. // 计算父结点
  1910. if (calcParents){
  1911. let curNode = treeNode.parent;
  1912. while (curNode){
  1913. me.innerCalc(curNode, changedNodes, tender);
  1914. curNode = curNode.parent;
  1915. };
  1916. };
  1917. // 父结点算完,再计算所有的公式结点(必须先算完父结点,再算公式结点)
  1918. if (calcFormulas) {
  1919. me.calcFormulaNodes(changedNodes, tender);
  1920. };
  1921. };
  1922. return changedNodes;
  1923. };
  1924. // 计算并保存本节点及所有会被影响到的节点。
  1925. calcAndSave(treeNode, callback, tender){
  1926. let changedNodes = this.calculate(treeNode, true, true, tender);
  1927. this.saveNodes(changedNodes, callback);
  1928. };
  1929. /* 计算所有树结点(分3种情况),并返回发生变动的零散的多个树结点。参数取值如下:
  1930. calcAllType.catAll 计算所有树结点 (默认值)
  1931. calcAllType.catBills 计算所有清单 (改变项目属性中清单取费算法时会用到)
  1932. calcAllType.catRations 计算所有定额、工料机形式的定额、量价,因为它们都走自己的计算程序 (改变人工系数、费率值、工料机单价时会用到)
  1933. (calcAllType.catRations时程序中做了特殊处理,实际上是计算所有树结点!)
  1934. 调价相关参数:
  1935. tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  1936. */
  1937. calcAllNodes(calcType = calcAllType.catAll, tender){
  1938. let me = this;
  1939. let changedNodes = [];
  1940. function calcNodes(nodes) {
  1941. for (let node of nodes) {
  1942. if (node.children.length > 0) {
  1943. calcNodes(node.children);
  1944. };
  1945. if (calcType == calcAllType.catAll || calcType == node.sourceType) {
  1946. node.calcType = calcTools.getCalcType(node);
  1947. if (node.calcType != treeNodeCalcType.ctCalcBaseValue)
  1948. me.innerCalc(node, changedNodes, tender);
  1949. };
  1950. }
  1951. };
  1952. // calcAllType.catRations 参数情况不会计算父结点(因为父结点是清单),所以这里进行特殊处理。
  1953. if (calcType == calcAllType.catRations)
  1954. calcType == calcAllType.catAll;
  1955. calcNodes(me.project.mainTree.roots);
  1956. me.calcFormulaNodes(changedNodes, tender);
  1957. if (tender){ // 普通计算不执行,只有3种调价计算才清。
  1958. for(let node of projectObj.project.mainTree.items){
  1959. this.clearTenderCache(node);
  1960. };
  1961. };
  1962. return changedNodes;
  1963. };
  1964. // tender: null:不调价(普通计算)。 1: 正向调价 2:反向调价-调子目 3: 反向调价-调工料机
  1965. calcAllNodesAndSave(calcType = calcAllType.catAll, callback, tender){
  1966. let changedNodes = this.calcAllNodes(calcType, tender);
  1967. this.saveNodes(changedNodes, callback);
  1968. };
  1969. // 计算零散的、混杂的树节点:清单、定额混合等(如:用到某一计算程序的定额和清单)。
  1970. // 计算多条零散的定额,并计算他们所属的清单、父清单、引用清单。如:批量替换工料机后受影响的定额。
  1971. // 计算多条零散的清单,并计算他们的父清单、引用清单。如:花选删除树结点(如花选清单、定额等,不区分树结点类型)。
  1972. calcNodesAndSave(nodes, callback, tender){
  1973. let me = this, rationNodes = [], billNodes = [], leafBills = [], allChangedNodes = [];
  1974. for (let node of nodes) {
  1975. if (node.sourceType == ModuleNames.ration)
  1976. rationNodes.push(node)
  1977. else
  1978. billNodes.push(node);
  1979. };
  1980. // 多条定额同属一条叶子清单时,避免叶子清单重复计算
  1981. for (let ration of rationNodes) {
  1982. me.innerCalc(ration, allChangedNodes, tender);
  1983. let leafBill = ration.parent;
  1984. if (leafBill && leafBills.indexOf(leafBill) < 0)
  1985. leafBills.push(leafBill);
  1986. };
  1987. billNodes.merge(leafBills);
  1988. for (let bill of billNodes){
  1989. let changeBills = me.calculate(bill, true, false, tender);
  1990. allChangedNodes.merge(changeBills);
  1991. };
  1992. me.calcFormulaNodes(allChangedNodes, tender);
  1993. me.saveNodes(allChangedNodes, callback);
  1994. };
  1995. // 计算全部公式项。 (changedArr:将通过本方法后发生改变的节点存入changedArr中)
  1996. calcFormulaNodes(changedArr, tender){
  1997. let me = this;
  1998. let formulaNodes = cbTools.getFormulaNodes(true);
  1999. if (formulaNodes.length == 0) return;
  2000. for (let formulaNode of formulaNodes){
  2001. formulaNode.data.userCalcBase = formulaNode.data.calcBase; // 这句不该出现,projectObj.project.calcBase中要改进。
  2002. projectObj.project.calcBase.calculate(formulaNode, true);
  2003. if (projectObj.project.calcBase.success){
  2004. // 计算公式结点
  2005. me.innerCalc(formulaNode, changedArr, tender);
  2006. // 计算父结点
  2007. if (formulaNode.changed){
  2008. let curNode = formulaNode.parent;
  2009. while (curNode){
  2010. me.innerCalc(curNode, changedArr, tender);
  2011. curNode = curNode.parent;
  2012. };
  2013. };
  2014. };
  2015. };
  2016. };
  2017. // 计算叶子清单下的所有子结点、自身、所有父结点、公式引用结点(即跟该叶子清单相关的所有结点)。最后打包存储。
  2018. calcLeafAndSave(treeNode, tender){
  2019. let me = this;
  2020. if(!calcTools.isLeafBill(treeNode)) return;
  2021. if (treeNode.children && treeNode.children.length > 0) {
  2022. let changedNodes = [];
  2023. for (let child of treeNode.children){
  2024. me.innerCalc(child, changedNodes);
  2025. };
  2026. let curChangeds = me.calculate(treeNode, true, true, tender);
  2027. changedNodes.merge(curChangeds);
  2028. me.saveNodes(changedNodes);
  2029. };
  2030. };
  2031. // 排除指定项的综合合价计算(用于带循环计算的情况。这里的汇总只到清单级别即可:清单单价取费时,汇总到清单和汇总到定额两个值不一样)
  2032. getTotalFee(baseNodes, excludeNodes, tender){
  2033. let rst = 0;
  2034. const totalFeeType = tender ? 'common.tenderTotalFee' : 'common.totalFee';
  2035. function calcNodes(nodes) {
  2036. for (let node of nodes) {
  2037. if(!node){
  2038. continue;
  2039. }
  2040. if (!excludeNodes.includes(node)){
  2041. if (node.source && node.source.children && node.source.children.length > 0) {
  2042. calcNodes(node.children);
  2043. }
  2044. else{
  2045. if (node.sourceType == ModuleNames.bills) {
  2046. rst = (rst + calcTools.getFee(node, totalFeeType)).toDecimal(decimalObj.decimal("totalPrice", node));
  2047. };
  2048. }
  2049. }
  2050. }
  2051. };
  2052. calcNodes(baseNodes);
  2053. return rst;
  2054. };
  2055. // 税前工程造价 ,adj调价
  2056. getBeforeTaxTotalFee(excludeNodes, tender){
  2057. let baseNodes = [], me = this;
  2058. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.SUB_ENGINERRING));
  2059. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.MEASURE));
  2060. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.OTHER));
  2061. baseNodes.push(calcTools.getNodeByFlag(fixedFlag.CHARGE));
  2062. return me.getTotalFee(baseNodes, excludeNodes, tender);
  2063. };
  2064. // 反向调价
  2065. calcTenderReverse(treeNode, tender){
  2066. if (tender == tenderTypes.ttReverseRation) {
  2067. if (treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2068. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2069. treeNode.changed = true;
  2070. }
  2071. };
  2072. if (!treeNode.data.targetTotalFee){
  2073. if (treeNode.data.targetUnitFee){
  2074. treeNode.data.targetTotalFee = (treeNode.data.targetUnitFee * treeNode.data.quantity).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2075. treeNode.changed = true;
  2076. }
  2077. else{ // 既没有目标金额也没有目标单价,此时要初始化使调价合价=原始综合合价,调价单价=原始综合单价。
  2078. if (treeNode.data.feesIndex.common.tenderUnitFee != treeNode.data.feesIndex.common.unitFee) {
  2079. treeNode.data.feesIndex.common.tenderUnitFee = treeNode.data.feesIndex.common.unitFee;
  2080. treeNode.changed = true;
  2081. };
  2082. if (treeNode.data.feesIndex.common.tenderTotalFee != treeNode.data.feesIndex.common.totalFee) {
  2083. treeNode.data.feesIndex.common.tenderTotalFee = treeNode.data.feesIndex.common.totalFee;
  2084. treeNode.changed = true;
  2085. };
  2086. return;
  2087. }
  2088. };
  2089. if (!treeNode.data.targetUnitFee || (parseFloat(treeNode.data.targetUnitFee) == 0)){
  2090. if (calcTools.hasQuantity(treeNode))
  2091. treeNode.data.targetUnitFee = (treeNode.data.targetTotalFee / treeNode.data.quantity).toDecimal(decimalObj.decimal('unitPrice', treeNode));
  2092. }
  2093. let coe = 1;
  2094. if (treeNode.data.feesIndex.common.totalFee != 0)
  2095. coe = (treeNode.data.targetUnitFee / treeNode.data.feesIndex.common.unitFee).toDecimal(decimalObj.process);
  2096. if (tender == tenderTypes.ttReverseRation){
  2097. treeNode.data.tenderQuantity = (treeNode.data.quantity * coe).toDecimal(decimalObj.decimal("quantity", treeNode));
  2098. if (treeNode.data.rationQuantityCoe != coe){
  2099. treeNode.data.rationQuantityCoe = coe;
  2100. treeNode.changed = true;
  2101. };
  2102. let ttf = (treeNode.data.tenderQuantity * treeNode.data.feesIndex.common.tenderUnitFee).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2103. if (treeNode.data.feesIndex.common.tenderTotalFee != ttf){
  2104. treeNode.data.feesIndex.common.tenderTotalFee = ttf;
  2105. treeNode.changed = true;
  2106. };
  2107. }else if (tender == tenderTypes.ttReverseGLJ){
  2108. // treeNode.data.tenderQuantity = treeNode.data.quantity; // 这句好像多余,因为调用calculate()时里面会重新计算tenderQuantity,有问题再放开AAAAA
  2109. let qcObj = treeNode.data.quantityCoe;
  2110. if (!qcObj || calcTools.isEmptyObject(qcObj)){
  2111. treeNode.data.quantityCoe = {labour: coe, material: coe, machine: coe, main: coe, equipment: coe};
  2112. treeNode.changed = true;
  2113. }else{
  2114. for (let pn in qcObj){
  2115. if (qcObj[pn] != coe){
  2116. qcObj[pn] = coe;
  2117. treeNode.changed = true;
  2118. }
  2119. };
  2120. };
  2121. projectObj.project.calcProgram.calculate(treeNode, false, false, tenderTypes.ttCalc); // 再正向算
  2122. };
  2123. };
  2124. // 清理调价缓存数据
  2125. clearTenderCache(treeNode){
  2126. // 这些属性值为什么不定义在一个对象里?因为每次要判断对象是否存在,十分麻烦。不如直接写简单。分散书写,统一处理也很好用。
  2127. if (treeNode.data.tender_activeTotal) delete treeNode.data.tender_activeTotal;
  2128. if (treeNode.data.tender_activeTarget) delete treeNode.data.tender_activeTarget;
  2129. if (treeNode.data.tender_fullLoad) delete treeNode.data.tender_fullLoad;
  2130. if (treeNode.data.tender_distribute){
  2131. delete treeNode.data.targetTotalFee;
  2132. delete treeNode.data.targetUnitFee;
  2133. delete treeNode.data.tender_distribute;
  2134. };
  2135. };
  2136. // 调价:分摊前的准备工作。主要标记满载的结点。
  2137. prepareForDistribute(treeNode){
  2138. if (!treeNode) return;
  2139. if (treeNode.firstChild())
  2140. this.prepareForDistribute(treeNode.firstChild());
  2141. if (treeNode.children.length == 0){
  2142. // if (!treeNode.data.feesIndex['common'])
  2143. // treeNode.data.tender_fullLoad = true
  2144. // else
  2145. // treeNode.data.tender_fullLoad = false;
  2146. }
  2147. else{
  2148. let full = true;
  2149. for (let i = 0; i < treeNode.children.length; i++) {
  2150. let child = treeNode.children[i];
  2151. if (!child.data.tender_fullLoad && !calcTools.hasTargetTotalFee(child)){
  2152. full = false;
  2153. break;
  2154. };
  2155. }
  2156. if (full) {
  2157. let total = 0, target = 0;
  2158. for (let i = 0; i < treeNode.children.length; i++) {
  2159. let child = treeNode.children[i];
  2160. total = total + parseFloat(child.data.feesIndex['common'].totalFee);
  2161. target = target + parseFloat(child.data.targetTotalFee);
  2162. };
  2163. treeNode.data.tender_activeTotal = total;
  2164. treeNode.data.tender_activeTarget = target;
  2165. treeNode.data.targetTotalFee = target;
  2166. treeNode.data.tender_distribute = 2;
  2167. };
  2168. treeNode.data.tender_fullLoad = full;
  2169. };
  2170. /*if (calcTools.isBill(treeNode) && (treeNode.data.tender_fullLoad != undefined)){
  2171. console.log(treeNode.data.name + ',tender_fullLoad: ' + treeNode.data.tender_fullLoad + ', target: ' + calcTools.hasTargetTotalFee(treeNode));
  2172. };*/
  2173. /* if (calcTools.isRationItem(treeNode)){
  2174. console.log(treeNode.data);
  2175. }*/
  2176. if (treeNode.nextSibling)
  2177. this.prepareForDistribute(treeNode.nextSibling);
  2178. };
  2179. // 调价之分摊目标合价:从父到子往下分摊。
  2180. distributeTargetTotalFee(treeNode){
  2181. if (!treeNode) return;
  2182. if (treeNode.data.feesIndex['common']){ // 空清单忽略
  2183. // 默认能够执行到这里时每个节点已经被初始化,缓存已删除
  2184. treeNode.data.tender_activeTotal = treeNode.data.feesIndex['common'].totalFee;
  2185. // 开始分摊:只分摊自有目标金额、从父结点分摊到的金额(不分摊子结点摊上来的金额)
  2186. if (calcTools.hasTargetTotalFee(treeNode) && !(treeNode.data.tender_distribute && treeNode.data.tender_distribute == 2)){
  2187. treeNode.data.tender_activeTarget = treeNode.data.targetTotalFee;
  2188. // 先把会破坏金额比例关系的孩子排除:1.有目标金额的 2.满载的(孙子全满,没有分摊空间,所以实质上该孩子的金额已被锁死无法分摊)
  2189. for (let i = 0; i < treeNode.children.length; i++) {
  2190. let child = treeNode.children[i];
  2191. if (!child.data.feesIndex['common']) continue; // 无计算金额的(如空清单)当它不存在
  2192. child.data.tender_activeTotal = child.data.feesIndex['common'].totalFee;
  2193. if (calcTools.hasTargetTotalFee(child)){
  2194. child.data.tender_activeTarget = child.data.targetTotalFee;
  2195. console.log(treeNode.data.tender_activeTotal);
  2196. console.log(child.data.tender_activeTotal);
  2197. treeNode.data.tender_activeTotal = treeNode.data.tender_activeTotal - child.data.tender_activeTotal;
  2198. treeNode.data.tender_activeTarget = treeNode.data.tender_activeTarget - child.data.tender_activeTarget;
  2199. };
  2200. }
  2201. if (treeNode.data.tender_activeTotal != 0){
  2202. let coe = (treeNode.data.tender_activeTarget / treeNode.data.tender_activeTotal).toDecimal(decimalObj.process);
  2203. for (let i = 0; i < treeNode.children.length; i++) {
  2204. let child = treeNode.children[i];
  2205. if (!child.data.feesIndex['common']) continue;
  2206. if (!calcTools.hasTargetTotalFee(child)){
  2207. child.data.tender_activeTarget = (coe * child.data.tender_activeTotal).toDecimal(decimalObj.decimal('totalPrice', treeNode));
  2208. child.data.targetTotalFee = child.data.tender_activeTarget;
  2209. child.data.tender_distribute = 1; // 1表示分摊金额来自父结点。2表示分摊金额来自孩子结点。
  2210. }
  2211. }
  2212. };
  2213. };
  2214. }
  2215. if (treeNode.firstChild())
  2216. this.distributeTargetTotalFee(treeNode.firstChild());
  2217. if (treeNode.nextSibling)
  2218. this.distributeTargetTotalFee(treeNode.nextSibling);
  2219. };
  2220. setRationMap(){
  2221. if(this.rationMap == null){
  2222. this.rationMap = {};
  2223. for(let glj of projectObj.project.ration_glj.datas){
  2224. if(this.rationMap[glj.rationID]){
  2225. this.rationMap[glj.rationID].push(glj)
  2226. }else {
  2227. this.rationMap[glj.rationID] = [glj];
  2228. }
  2229. }
  2230. }
  2231. if(this.pgljMap == null ){
  2232. this.pgljMap = _.indexBy(projectObj.project.projectGLJ.datas.gljList, 'id');
  2233. }
  2234. };
  2235. getGljArrByRation(ration){
  2236. if (ration.type == rationType.gljRation){
  2237. let glj = JSON.parse(JSON.stringify(ration));
  2238. glj.type = glj.subType;
  2239. glj.quantity = 1;
  2240. glj.totalQuantity = parseFloatPlus(ration.quantity);
  2241. return [glj];
  2242. } else{
  2243. if(!this.rationMap) return [];
  2244. let result = this.rationMap[ration.ID];
  2245. if(!result) return [];
  2246. result = gljOprObj.combineWithProjectGlj(result,false,ration,this.pgljMap);
  2247. return result;
  2248. }
  2249. };
  2250. };
  2251. // export default analyzer;