calc_program.js 128 KB

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