project_glj.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /**
  2. * 工料机汇总相关数据
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/9/14
  6. * @version
  7. */
  8. function ProjectGLJ() {
  9. this.datas = null;
  10. this.isLoading = false;
  11. this.quantityChangeMap=null;
  12. this.getRatioId = null;
  13. }
  14. /**
  15. * 加载数据
  16. *
  17. * @param {function} callback
  18. * @return {boolean}
  19. */
  20. ProjectGLJ.prototype.loadData = function (callback = null,error=null) {
  21. let self = this;
  22. if (self.isLoading) {
  23. return false;
  24. }
  25. // 加载工料机数据
  26. $.ajax({
  27. url: '/glj/getData',
  28. type: 'post',
  29. dataType: 'json',
  30. data: {project_id: scUrlUtil.GetQueryString('project')},
  31. error: function () {
  32. // alert('数据传输错误');
  33. if(error) error();
  34. },
  35. beforeSend: function () {
  36. self.isLoading = true;
  37. },
  38. success: function (response) {
  39. self.isLoading = false;
  40. if (response.err === 1) {
  41. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '读取人材机数据失败!';
  42. alert(msg);
  43. if(error) error();
  44. return false;
  45. }
  46. self.refreshByDatas(response.data);
  47. // 回调函数
  48. if (callback !== null) {
  49. callback(response.data);
  50. }
  51. // 存入缓存
  52. projectObj.project.projectGLJ = self;
  53. }
  54. });
  55. };
  56. //更新项目工料机数据和缓存
  57. ProjectGLJ.prototype.refreshByDatas = function(datas){
  58. this.loadToCache(datas);
  59. this.calcQuantity();
  60. };
  61. ProjectGLJ.prototype.synLoadData = function () {
  62. return new Promise(function (resolve, reject) {
  63. projectObj.project.projectGLJ.loadData(function (data) {
  64. resolve(data)
  65. },function () {
  66. reject("获取工料机数据失败")
  67. })
  68. })
  69. }
  70. ProjectGLJ.prototype.loadToCache = function (data) {
  71. this.datas = data;
  72. this.datas.gljMap = {};
  73. for(let g of this.datas.gljList){
  74. this.datas.gljMap[gljUtil.getIndex(g)]=g;
  75. }
  76. }
  77. /**
  78. * 获取对应工料机数据
  79. *
  80. * @param {String} code
  81. * @return {Object}
  82. */
  83. ProjectGLJ.prototype.getDataByID = function (ID) {//根据项目工料机ID取工料机信息
  84. return _.find(this.datas.gljList, {'id': ID});
  85. };
  86. // CSL, 2018-02-08 甲供、甲定。
  87. ProjectGLJ.prototype.getGLJsBySupply = function (supplyTypeArr, gljTypeArr) {
  88. // 项目工料机采用了内部绑定数据源方式,能够双向同步,但同时带来难干预控制问题。supply值存在混杂情况,如:“2”和“部分甲供”同时存在。
  89. // 所以这里要合并处理。
  90. let mixSupply = [];
  91. for (let s of supplyTypeArr){
  92. switch (s) {
  93. case 1:
  94. mixSupply.push('部分甲供');
  95. break;
  96. case 2:
  97. mixSupply.push('完全甲供');
  98. break;
  99. case 3:
  100. mixSupply.push('甲定乙供');
  101. break;
  102. default:
  103. mixSupply.push('自行采购');
  104. }
  105. };
  106. mixSupply = mixSupply.concat(supplyTypeArr);
  107. return _.filter(this.datas.gljList, function (glj) {
  108. return mixSupply.includes(glj.supply) && gljTypeArr.includes(glj.type);
  109. });
  110. };
  111. ProjectGLJ.prototype.testGLJs = function () {
  112. let gljs = [];
  113. for (let glj of this.datas.gljList){
  114. let o = new Object();
  115. o.name = glj.name;
  116. o.supply = glj.supply;
  117. o.quantity = glj.quantity;
  118. o.supply_quantity = glj.supply_quantity;
  119. gljs.push(o);
  120. };
  121. return gljs;
  122. };
  123. /**
  124. * 修改工料机数据
  125. *
  126. * @param {Number} id
  127. * @param {Object} data
  128. * @return {boolean}
  129. */
  130. ProjectGLJ.prototype.updateData = function (id, data) {
  131. let result = false;
  132. if (this.datas === null) {
  133. return result;
  134. }
  135. let gljList = this.datas.gljList;
  136. if (gljList === undefined) {
  137. return result;
  138. }
  139. // 查找对应的index
  140. let index = -1;
  141. for (let tmp in gljList) {
  142. if (gljList[tmp].id === id) {
  143. index = tmp;
  144. break;
  145. }
  146. }
  147. if (index < 0) {
  148. return result;
  149. }
  150. // 修改数据
  151. for (let tmpIndex in data) {
  152. if (tmpIndex.indexOf('_price') >= 0) {
  153. // 修改unit_price中的对象
  154. this.datas.gljList[index]['unit_price'][tmpIndex] = data[tmpIndex];
  155. } else {
  156. this.datas.gljList[index][tmpIndex] = data[tmpIndex];
  157. }
  158. }
  159. };
  160. /**
  161. * 加载缓存数据到spread
  162. *
  163. * @return {void}
  164. */
  165. ProjectGLJ.prototype.loadCacheData = function (resort) {
  166. // 加载工料机数据
  167. let data = this.datas === null ? null : this.datas;
  168. if (data === null) {
  169. return;
  170. }
  171. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  172. console.log("filter start");
  173. jsonData = filterProjectGLJ(jsonData);
  174. console.log("filter end");
  175. jsonData = sortProjectGLJ(jsonData);
  176. console.log("sort end");
  177. if(projectGLJSheet&&projectGLJSpread){
  178. setTimeout(spreadInit, 1);
  179. /*projectGLJSheet.setData(jsonData);
  180. projectGLJSpread.specialColumn(jsonData);*/
  181. }
  182. };
  183. ProjectGLJ.prototype.updatePriceFromRG = function (recode, updateField, newval,callback) {
  184. if (updateField == 'marketPrice') {
  185. this.updatePrice(recode, "market_price", newval,"rg",callback);
  186. }
  187. if (updateField == 'basePrice') {
  188. this.updatePrice(recode, "base_price", newval,"rg",callback);
  189. }
  190. };
  191. ProjectGLJ.prototype.updatePropertyFromMainSpread = function (node, updateField, newval,editingText) {
  192. if (updateField == "contain") {//更新含量和工程量时,要走定额更新的逻辑
  193. projectObj.project.Ration.updateContain(newval,node);
  194. }if(updateField == "quantity"){
  195. projectObj.project.quantity_detail.editMainTreeNodeQuantity(newval,node,updateField,editingText);
  196. } else {
  197. this.updateGLJProperty(node, updateField, newval);
  198. }
  199. };
  200. ProjectGLJ.prototype.updateGLJProperty = function (node, updateField, newval) {
  201. let rationTypeGLJ = node.data;
  202. let postData = {};
  203. if (rationTypeGLJ[updateField] == newval) {
  204. projectObj.mainController.refreshTreeNode([node]);
  205. return;
  206. }
  207. let data = {
  208. glj_id: rationTypeGLJ.GLJID,
  209. project_id: rationTypeGLJ.projectID,
  210. code: rationTypeGLJ.code,
  211. original_code: rationTypeGLJ.original_code,
  212. name: rationTypeGLJ.name,
  213. shortName: rationTypeGLJ.shortName,
  214. specs: rationTypeGLJ.specs,
  215. unit: rationTypeGLJ.unit,
  216. type: rationTypeGLJ.subType,
  217. type_of_work: rationTypeGLJ.subType,
  218. base_price: rationTypeGLJ.basePrice,
  219. market_price: rationTypeGLJ.basePrice,
  220. repositoryId: rationTypeGLJ.repositoryId,
  221. adjCoe: rationTypeGLJ.adjCoe,
  222. from: rationTypeGLJ.from ? rationTypeGLJ.from : 'std'//std:标准工料机库, cpt:补充工料机库
  223. };
  224. if (updateField == 'subType') {
  225. data.type = newval;
  226. data.type_of_work = newval;
  227. data.shortName = this.getShortNameByID(newval);
  228. } else {
  229. data[updateField] = newval;
  230. }
  231. postData.ration = {
  232. ID: rationTypeGLJ.ID,
  233. projectID: rationTypeGLJ.projectID
  234. };
  235. postData.updateData = data;
  236. $.bootstrapLoading.start();
  237. CommonAjax.post("/glj/modifyKeyValue", postData, function (result) {
  238. console.log(result); //更新节点信息
  239. node.updateData[updateField] = newval;
  240. node.updateData.projectGLJID = result.id;
  241. node.updateData.code = result.code;
  242. node.updateData.basePrice = result.unit_price.base_price;
  243. node.updateData.marketUnitFee = result.unit_price.market_price;
  244. node.updateData.isAdd = result.unit_price.is_add;
  245. node.updateData.isEstimate = result.is_evaluate;
  246. node.updateData.shortName = result.unit_price.short_name;
  247. //触发计算并更新节点信息
  248. node.changed = true;
  249. projectObj.project.projectGLJ.loadData(function () {
  250. projectObj.project.calcProgram.calcAndSave(node);
  251. $.bootstrapLoading.end();
  252. });//重新加载项目工料机数据
  253. //上面两步都是异步操作,这句应该是要等上面两步做完了再执行的
  254. }, function (err) {
  255. $.bootstrapLoading.end();
  256. });
  257. }
  258. ProjectGLJ.prototype.getPriceDecimal = function (glj) {//价格的小数位数,有无组成物,取值不同
  259. if(gljUtil.notEditType.indexOf(glj.unit_price.type)!=-1&&glj.ratio_data.length>0){
  260. return getDecimal("glj.unitPriceHasMix");
  261. }else {
  262. return getDecimal('glj.unitPrice');
  263. }
  264. };
  265. ProjectGLJ.prototype.getCalcMaterialUpdateData = function(projectGLJ,updateField,value,ext){//只有添加、删除走这个方法
  266. let glj = _.find(this.datas.gljList,{'id':projectGLJ.id});
  267. if(!glj) return null;
  268. if(glj.unit_price[updateField] == value){
  269. return null;
  270. }
  271. if(updateField == 'calcMaterial'){
  272. ext=ext?ext:{};
  273. if(value == 1){//添加材料计算操作,把单位毛重/采购保管费率等数据复制一份
  274. ext['grossWeightCoe_n'] = glj.unit_price.grossWeightCoe;
  275. ext['purchaseStorageRate_n'] = glj.unit_price.purchaseStorageRate;
  276. ext['offSiteTransportLossRate_n'] = glj.unit_price.offSiteTransportLossRate;
  277. ext['handlingLossRate_n'] = glj.unit_price.handlingLossRate;
  278. ext['totalLoadingTimes'] = '1';
  279. }else if(value == 0){//删除操作,恢复单位毛重/采购保管费率等数据
  280. ext['supplyLocation'] = '';
  281. ext['originalPrice'] = '0';
  282. ext['unitFreight']='0';
  283. ext['totalLoadingTimes']='0';
  284. ext['offSiteTransportLoss']='0';
  285. ext['purchaseStorage']='0';
  286. ext['packageRecoverValue']='0';
  287. }
  288. }
  289. let data = {id: glj.unit_price.id,updateField:updateField ,value: value,unit_price_file_id:glj.unit_price.unit_price_file_id,ext:ext};
  290. return {data:data,glj:glj}
  291. }
  292. ProjectGLJ.prototype.updateCalcMaterials = async function(projectGLJs,value) {//只有添加、删除走这个方法
  293. let updateField = 'calcMaterial';
  294. let datas = [];
  295. let dataMaps = [];
  296. for(let p of projectGLJs){
  297. let map = this.getCalcMaterialUpdateData(p,updateField,value);
  298. if(map){
  299. datas.push(map.data);
  300. dataMaps.push(map);
  301. }
  302. }
  303. if(datas.length == 0) return;
  304. $.bootstrapLoading.start();
  305. try {
  306. let result = await ajaxPost('/glj/updateCalcMaterial',datas);
  307. let calcQ = false;
  308. for(let dm of dataMaps){
  309. let glj = dm.glj;
  310. glj.unit_price[updateField] = value;
  311. if(dm.data.ext) gljUtil.setProperty(glj.unit_price,dm.data.ext);
  312. if(updateField == 'calcMaterial' && value ==0){///标记为0即删除材料计算标记,要删除其下挂的原价计算,运费计算,定额计算,消耗量重新计算
  313. calcQ = true;
  314. let connect_key = gljUtil.getIndex(glj);
  315. _.remove(this.datas.originalList,{'connect_key':connect_key});
  316. _.remove(this.datas.freightList,{'connect_key':connect_key});
  317. }
  318. }
  319. if(calcQ) this.calcQuantity();
  320. projectGljObject.refreshDataSheet();
  321. materialCalcObj.showDatas();
  322. } catch (e){
  323. console.log(e)
  324. }finally {
  325. $.bootstrapLoading.end();
  326. }
  327. };
  328. ProjectGLJ.prototype.updateCalcMaterial =async function (projectGLJ,value) {//只有添加、删除走这个方法
  329. this.updateCalcMaterials([projectGLJ],value);
  330. };
  331. ProjectGLJ.prototype.calcAllMaterial = function (unitPriecs,needReCalc) {//当材料发生改变时重算所有计算材料的价格
  332. let priceMap = {};
  333. for(let unitPrice of unitPriecs){
  334. if(unitPrice && gljUtil.isDef(unitPrice.doc.market_price)) priceMap[unitPrice.projectGLJID] = unitPrice.doc.market_price;
  335. }
  336. let freightListMap = _.groupBy(this.datas.freightList,'connect_key');//取运费计算项映射表
  337. let originalListMap = _.groupBy(this.datas.originalList,'connect_key');//取原价计算项映射表
  338. let sumFreightUpdateMap = {}; //存放所有材料下的运费更新信息;
  339. let sumOriginalUpdateMap = {};//存放所有材料下的原价更新信息;
  340. for(let g of this.datas.gljList){
  341. if(g.quantity !== 0 && g.quantity !== '0' && g.unit_price.calcMaterial == 1&&!priceMap[g.id]){//工料机本身不包含在要计算的列表内
  342. //先判断要不要重算,只有包含相关工料机的定额的材料,才需要重新计算
  343. let fOrPDataMap = {};
  344. let tIndex = gljUtil.getIndex(g);
  345. //运费和原价中的工料机只要有一个包含该材料,就要重算
  346. if (freightListMap[tIndex]) { //运费重算
  347. let freightUpdateMap = {};
  348. for(let f of freightListMap[tIndex]){
  349. if(f.conveyance != "自办运输") continue;//不是自办运输的不用管
  350. let calf = ifNeedReCalc(f,priceMap,needReCalc);
  351. if(calf){
  352. let f_task = this.calcEachFreightOrPrice(f,"freight",priceMap);
  353. if(f_task){
  354. freightUpdateMap[f_task.ID] = f_task.doc;
  355. sumFreightUpdateMap[f_task.ID] = f_task.doc;
  356. }
  357. }
  358. }
  359. if(!_.isEmpty(freightUpdateMap)) fOrPDataMap["freight"] = {update:freightUpdateMap};
  360. }
  361. //原价重算
  362. if(originalListMap[tIndex]){
  363. let originalUpdateMap = {};
  364. for(let o of originalListMap[tIndex]){
  365. let calo = ifNeedReCalc(o,priceMap,needReCalc);
  366. if(calo){
  367. let o_task = this.calcEachFreightOrPrice(o,"price",priceMap);
  368. if(o_task){
  369. originalUpdateMap[o_task.ID] = o_task.doc;
  370. sumOriginalUpdateMap[o_task.ID] = o_task.doc;
  371. }
  372. }
  373. }
  374. if(!_.isEmpty(originalUpdateMap)) fOrPDataMap["price"] = {update:originalUpdateMap};
  375. }
  376. //判断映射表是否为空,如果为空则说明不用计算
  377. if(!_.isEmpty(fOrPDataMap)){
  378. let t_unipPrice = this.materialCal(g.id,fOrPDataMap,g,freightListMap[tIndex],originalListMap[tIndex]);
  379. if(t_unipPrice && !_.isEmpty(t_unipPrice.doc)) unitPriecs.push(t_unipPrice);
  380. }
  381. }
  382. }
  383. let sumUpdateMap = {};
  384. if(!_.isEmpty(sumFreightUpdateMap))sumUpdateMap["freight"] = {update:sumFreightUpdateMap};
  385. if(!_.isEmpty(sumOriginalUpdateMap))sumUpdateMap["price"] = {update:sumOriginalUpdateMap};
  386. let ext = {};
  387. for(let u of unitPriecs){//计算受影响的综合电价
  388. ext[u.projectGLJID] = {marketPrice:u.doc.market_price}; //priceMap[unitPrice.projectGLJID] = unitPrice.doc.market_price;
  389. }
  390. let elecu = electrovalenceObj.clacNewElecPrice(ext);
  391. if(elecu) unitPriecs.push(elecu);
  392. return [unitPriecs,sumUpdateMap];
  393. function ifNeedReCalc(obj,IDMap,needReCalc = false) {
  394. let calcf = false;
  395. if(needReCalc == true) return true;//如果是通过修改费率等地方触发的,默认自动计算
  396. if(obj.ration_gljs){
  397. for(let rg of obj.ration_gljs){
  398. if(IDMap[rg.projectGLJID]){//判断是否关联了该项目工料机
  399. calcf = true;
  400. break;
  401. }
  402. }
  403. }
  404. return calcf;
  405. }
  406. };
  407. ProjectGLJ.prototype.materialCal = function (id,dataMap,tglj,tfreightList,tpriceList) {
  408. let unitPriceHasMix = getDecimal("glj.unitPriceHasMix");
  409. let unitPirceDecimal = getDecimal("glj.unitPrice");
  410. let process = getDecimal("process");
  411. let glj = tglj?tglj: _.find(this.datas.gljList,{'id':id});
  412. let doc = dataMap['material']&&dataMap['material']['update']?dataMap['material']['update']:{};//材料的update结构不同,不用ID做key
  413. if(!glj) return;
  414. let unitPrice = {projectGLJID:glj.id,id:glj.unit_price.id,'unit_price_file_id':glj.unit_price.unit_price_file_id,doc:{}};
  415. //先计算原价
  416. let [originalPrice,supplyLocation,priceHeightFee] = this.priceCalc(glj,dataMap,tpriceList);
  417. doc['originalPrice'] = originalPrice+'';
  418. doc['supplyLocation'] = supplyLocation;
  419. doc['priceHeightFee'] = priceHeightFee;
  420. //再计算运费
  421. let grossWeightCoe_n = doc['grossWeightCoe_n']?doc['grossWeightCoe_n']:glj.unit_price.grossWeightCoe_n;
  422. grossWeightCoe_n = scMathUtil.roundForObj(grossWeightCoe_n,process);
  423. let [unitFreight,freightHeightFee] = this.freightCalc(glj,grossWeightCoe_n,dataMap,tfreightList);
  424. doc['unitFreight'] = unitFreight+'';
  425. doc['freightHeightFee'] = freightHeightFee+'';
  426. //计算场外运输损耗
  427. let sum_o_f = scMathUtil.roundForObj(originalPrice+unitFreight,process);//(原价+单位运费)
  428. let totalLoadingTimes = doc["totalLoadingTimes"]?doc["totalLoadingTimes"]:glj.unit_price.totalLoadingTimes;//装卸总次数
  429. let handlingLossRate_n = doc["handlingLossRate_n"]?doc["handlingLossRate_n"]:glj.unit_price.handlingLossRate_n;//每增加一次装卸损耗率%
  430. let offSiteTransportLossRate_n = doc["offSiteTransportLossRate_n"]?doc["offSiteTransportLossRate_n"]:glj.unit_price.offSiteTransportLossRate_n;//场外运输损耗率
  431. handlingLossRate_n = scMathUtil.roundForObj(handlingLossRate_n,unitPirceDecimal)/100;
  432. offSiteTransportLossRate_n = scMathUtil.roundForObj(offSiteTransportLossRate_n,unitPirceDecimal)/100;
  433. totalLoadingTimes = scMathUtil.roundForObj(totalLoadingTimes,unitPirceDecimal) - 1;//(装卸总次数-1)
  434. totalLoadingTimes = totalLoadingTimes < 0 ?0:totalLoadingTimes;//(装卸总次数-1) 结果小于0的时候,值设为0
  435. totalLoadingTimes = scMathUtil.roundForObj(totalLoadingTimes * handlingLossRate_n,process);//(装卸总次数-1)*每增加一次装卸损耗率%
  436. totalLoadingTimes = scMathUtil.roundForObj(totalLoadingTimes + offSiteTransportLossRate_n,process);//(装卸总次数-1)*每增加一次装卸损耗率%+场外运输损耗率%
  437. let offSiteTransportLoss = scMathUtil.roundForObj(sum_o_f*totalLoadingTimes,unitPirceDecimal); //场外运输损耗=(原价+单位运费)*((装卸总次数-1)*每增加一次装卸损耗率%+场外运输损耗率%)。
  438. doc['offSiteTransportLoss'] = offSiteTransportLoss+'';
  439. //计算采购及保管费
  440. let purchaseStorageRate_n = doc["purchaseStorageRate_n"]?doc["purchaseStorageRate_n"]:glj.unit_price.purchaseStorageRate_n;//采购及保管费率%
  441. purchaseStorageRate_n = scMathUtil.roundForObj(purchaseStorageRate_n,unitPirceDecimal)/100;
  442. let purchaseStorage = scMathUtil.roundForObj(sum_o_f + offSiteTransportLoss,process)//(原价+单位运费+场外运输损耗)
  443. purchaseStorage = scMathUtil.roundForObj(purchaseStorage*purchaseStorageRate_n,unitPirceDecimal)//采购及保管费=(原价+单位运费+场外运输损耗)* 采购及保管费率%。
  444. doc['purchaseStorage'] = purchaseStorage+'';
  445. //再计算预算价
  446. let packageRecoverValue = doc['packageRecoverValue']?doc['packageRecoverValue']:glj.unit_price.packageRecoverValue;//包装品回收价值。
  447. packageRecoverValue = scMathUtil.roundForObj(packageRecoverValue,unitPirceDecimal);
  448. let market_price = scMathUtil.roundForObj(sum_o_f+offSiteTransportLoss,process);//原价+单位运费+场外运输损耗
  449. market_price = scMathUtil.roundForObj(market_price+purchaseStorage,process);//原价+单位运费+场外运输损耗+采购及保管费
  450. market_price = scMathUtil.roundForObj(market_price - packageRecoverValue,unitPriceHasMix)//预算价=原价+单位运费+场外运输损耗+采购及保管费-包装品回收价值。
  451. if(market_price>0) doc['market_price'] = market_price+'';//单机版预算价小于等于0的时候不更新
  452. let o_unit = glj.unit_price;
  453. for(let key in doc){
  454. if(o_unit[key] == doc[key]) continue; //忽略一样的值
  455. unitPrice.doc[key] = doc[key];
  456. }
  457. return unitPrice
  458. };
  459. ProjectGLJ.prototype.freightCalc= function (glj,grossWeightCoe_n,dataMap,tfreightList) {
  460. let process = getDecimal("process");
  461. let unitPirce = getDecimal("glj.unitPrice");
  462. let unitPriceHasMix = getDecimal("glj.unitPriceHasMix");//这里暂时没用到
  463. let sum = 0;
  464. let height_sum=0;
  465. let freightList = tfreightList?tfreightList:_.filter(this.datas.freightList,{"connect_key":gljUtil.getIndex(glj)});
  466. if(dataMap["freight"] && dataMap["freight"]["add"]) freightList = freightList.concat(dataMap["freight"]["add"]); //把添加的加进去
  467. for(let f of freightList){
  468. let unitFreight = f.unitFreight,kmDistance = f.kmDistance,freightIncreaseRate = f.freightIncreaseRate,unitLoadingFee= f.unitLoadingFee,loadingTimes = f.loadingTimes,otherFee = f.otherFee,weightCoe = f.weightCoe,conveyance = f.conveyance,calcType=f.calcType,materialType=f.materialType;
  469. let heightFee= f.heightFee?f.heightFee:0;
  470. if(dataMap["freight"] && dataMap["freight"]["update"]){//覆盖更新的数据
  471. let t = dataMap["freight"]["update"][f.ID];
  472. if(t){
  473. if(t['unitFreight']) unitFreight = t['unitFreight'];
  474. if(t['kmDistance']) kmDistance = t['kmDistance'];
  475. if(t['freightIncreaseRate']) freightIncreaseRate = t['freightIncreaseRate'];
  476. if(t['unitLoadingFee']) unitLoadingFee = t['unitLoadingFee'];
  477. if(t['loadingTimes']) loadingTimes = t['loadingTimes'];
  478. if(t['otherFee']) otherFee = t['otherFee'];
  479. if(t['weightCoe']) weightCoe = t['weightCoe'];
  480. if(t['conveyance']) conveyance = t['conveyance'];
  481. if(t['calcType']) calcType = t['calcType'];
  482. if(t['materialType']) materialType = t['materialType'];
  483. if(t['heightFee']) heightFee = t['heightFee'];
  484. }
  485. }
  486. if(dataMap["freight"] && dataMap["freight"]["delete"]){//忽略删除的数据
  487. if(dataMap["freight"]["delete"][f.ID]) continue;
  488. }
  489. //开始计算
  490. freightIncreaseRate = scMathUtil.roundForObj(freightIncreaseRate,unitPirce);
  491. freightIncreaseRate = scMathUtil.roundForObj(1+freightIncreaseRate/100,process);//(1+运距增加率%)
  492. unitFreight = scMathUtil.roundForObj(unitFreight,unitPirce);
  493. kmDistance = scMathUtil.roundForObj(kmDistance,unitPirce);
  494. heightFee = scMathUtil.roundForObj(heightFee,unitPirce);
  495. let t = getPrice(unitFreight,kmDistance,conveyance,calcType,materialType,process);//单位运价×km运距
  496. t = scMathUtil.roundForObj(t*freightIncreaseRate,process);//单位运价×km运距×(1+运距增加率%)
  497. unitLoadingFee = scMathUtil.roundForObj(unitLoadingFee,unitPirce);
  498. loadingTimes = scMathUtil.roundForObj(loadingTimes,unitPirce);
  499. let s = scMathUtil.roundForObj(unitLoadingFee*loadingTimes,process);//装卸费单价×装卸次数
  500. t = scMathUtil.roundForObj(t + s,process);//单位运价×km运距×(1+运距增加率%)+装卸费单价×装卸次数
  501. if(gljUtil.isDef(materialType)&&materialType != "") t = scMathUtil.roundForObj(t/1.09,process); //内蒙古要除以税率1.09; 当材料类型有值时,说明是内蒙古计算
  502. otherFee = scMathUtil.roundForObj(otherFee,unitPirce);
  503. t = scMathUtil.roundForObj(t+otherFee,process);//单位运价×km运距×(1+运距增加率%)+装卸费单价×装卸次数+其它费用
  504. if(conveyance !== "自办运输") t = scMathUtil.roundForObj(t*grossWeightCoe_n,process); //(单位运价×km运距×(1+运距增加率%)+装卸费单价×装卸次数+其它费用)×单位毛重”。 单位毛重:毛重系数
  505. weightCoe = scMathUtil.roundForObj(weightCoe,unitPirce);
  506. t = scMathUtil.roundForObj(t*weightCoe,process);//(单位运价×km运距×(1+运距增加率%)+装卸费单价×装卸次数+其它费用)×单位毛重×加权系数
  507. sum = scMathUtil.roundForObj(sum + t,process);
  508. heightFee = scMathUtil.roundForObj(heightFee*weightCoe,process);
  509. height_sum = scMathUtil.roundForObj(height_sum + heightFee,process);
  510. }
  511. if(freightList.length == 0){//如果运费计算表没有数据,则读取输入的或单价文件的值
  512. let doc = dataMap['material']&&dataMap['material']['update']?dataMap['material']['update']:{};
  513. sum = doc['unitFreight']?doc['unitFreight']:glj.unit_price.unitFreight;
  514. }
  515. return [scMathUtil.roundForObj(sum,unitPirce),scMathUtil.roundForObj(height_sum,unitPirce)];
  516. function getPrice(unitFreight,kmDistance,conveyance,calcType,materialType,process){
  517. if(gljUtil.isDef(materialType)&&materialType != "") return materialCalcObj.calNeiMengTotalFreight(kmDistance,materialType);
  518. //如果运输方式为自办运输,则直接返回单位运价即可;
  519. let price = conveyance == "自办运输" ? scMathUtil.roundForObj(unitFreight,process):scMathUtil.roundForObj(unitFreight * kmDistance,process);//单位运价×km运距
  520. return price
  521. }
  522. };
  523. ProjectGLJ.prototype.priceCalc = function (glj,dataMap,tpriceList) {
  524. let process = getDecimal("process");
  525. let unitPirce = getDecimal("glj.unitPrice");
  526. let unitPriceHasMix = getDecimal("glj.unitPriceHasMix");
  527. let original_price = 0;
  528. let sumFeightFee = 0;
  529. let supplyList = [];
  530. let supplyLocation = "";
  531. let priceList = tpriceList?tpriceList:_.filter(this.datas.originalList,{"connect_key":gljUtil.getIndex(glj)});
  532. if(dataMap["price"] && dataMap["price"]["add"]) priceList = priceList.concat(dataMap["price"]["add"]); //把添加的加进去
  533. for(let p of priceList){
  534. let supplyPrice = p.supplyPrice ,coe = p.coe,supplyLocation=p.supplyLocation;
  535. let heightFee = p.heightFee?p.heightFee:0;
  536. if(dataMap["price"] && dataMap["price"]["update"]){//覆盖更新的数据
  537. let t = dataMap["price"]["update"][p.ID];
  538. if(t && t["supplyPrice"]) supplyPrice =t["supplyPrice"];
  539. if(t && t["coe"]) coe =t["coe"];
  540. if(t && t["supplyLocation"]) supplyLocation = t["supplyLocation"];
  541. if(t && t["heightFee"]) heightFee = t["heightFee"];
  542. }
  543. if(dataMap["price"] && dataMap["price"]["delete"]){//忽略删除的数据
  544. if(dataMap["price"]["delete"][p.ID]) continue;
  545. }
  546. supplyPrice = scMathUtil.roundForObj(supplyPrice,unitPirce);
  547. coe = scMathUtil.roundForObj(coe,unitPirce);
  548. heightFee = scMathUtil.roundForObj(heightFee,unitPirce);
  549. let t_p = scMathUtil.roundForObj(supplyPrice * coe,process);
  550. let t_h = scMathUtil.roundForObj(heightFee * coe,process);
  551. original_price=scMathUtil.roundForObj(original_price+t_p,process);
  552. sumFeightFee = scMathUtil.roundForObj(sumFeightFee+t_h,process);
  553. supplyList.push(supplyLocation);
  554. }
  555. supplyLocation = supplyList.join(',');
  556. if(priceList.length == 0){//如果原价计算表没有数据,则读取输入的或单价文件的值
  557. let doc = dataMap['material']&&dataMap['material']['update']?dataMap['material']['update']:{};
  558. original_price = doc['originalPrice']!==undefined?doc['originalPrice']:glj.unit_price.originalPrice;
  559. supplyLocation = doc['supplyLocation']!==undefined?doc['supplyLocation']:glj.unit_price.supplyLocation;
  560. }
  561. return [scMathUtil.roundForObj(original_price,unitPriceHasMix),supplyLocation,scMathUtil.roundForObj(sumFeightFee,unitPirce)];
  562. };
  563. ProjectGLJ.prototype.m_updateUnitPrice = function (datas,isFromFeeRate=false) {//批量更新
  564. let me = this;
  565. let gljList = [];
  566. for(let d of datas){
  567. let g = updateUnit(d.projectGLJID,d);
  568. if(g) gljList.push(g);
  569. }
  570. if(isFromFeeRate == false)this.calcQuantity();
  571. //刷新项目工料机表显示
  572. projectGljObject.refreshDataSheet();
  573. //重新计算相关节点
  574. /* 2019-10-29 这里改成修改材料计算后先不自动计算,先在项目属性上做标记,切换到其它标签页时才做自动计算
  575. let nodes = me.getImpactRationNodes(gljList);//取到因为改变工料机价格而受影响的定额
  576. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  577. */
  578. //刷新定额工料机
  579. gljOprObj.refreshView();
  580. //socket推送更新信息
  581. projectGljObject.onUnitFileChange(!isFromFeeRate);//如果是从更新费率过来的,不用在项目属性上做标记,费率那里已经自动计算所有节点了
  582. return gljList
  583. function updateUnit(id,unitPrice) {
  584. let glj = _.find(me.datas.gljList,{'id':id});
  585. if(glj && !_.isEmpty(unitPrice.doc)){
  586. for(let key in unitPrice.doc){
  587. glj.unit_price[key] = unitPrice.doc[key];
  588. }
  589. }
  590. return glj;
  591. }
  592. };
  593. ProjectGLJ.prototype.updateUnitPrice = function(id,unitPrice){
  594. this.m_updateUnitPrice([unitPrice]);
  595. };
  596. ProjectGLJ.prototype.addPriceCalc = async function (datas,pgljID) {
  597. await this.updateMaterialCalcTasks(pgljID,datas,"add","price");
  598. };
  599. ProjectGLJ.prototype.updateMaterialCalc = async function (datas) {
  600. let updateDatas=[];
  601. try {
  602. for(let d of datas){
  603. let unitPrice = this.materialCal(d.id,{material:{update:d.doc}});
  604. if(_.isEmpty(unitPrice.doc)) continue;
  605. updateDatas.push(unitPrice);
  606. }
  607. if(updateDatas.length ==0 ) return;
  608. let [unitPrices,sumMap] = this.calcAllMaterial(updateDatas, true);
  609. sumMap = {"unitPrice":unitPrices};
  610. $.bootstrapLoading.start();
  611. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  612. this.refreshMaterialCalcCache(sumMap);
  613. materialCalcObj.showDatas();
  614. // 这里要重新设置所有运费计算的计算式(BUG #3388)
  615. if (materialCalcObj.freightDatas && materialCalcObj.freightDatas.length > 0) {
  616. let actionMap = {update: {}};
  617. for (const reCode of materialCalcObj.freightDatas) {
  618. let doc = {kmDistance: reCode.kmDistance};
  619. doc.exp = materialCalcObj.getFreightEXP(reCode, doc, true);
  620. actionMap.update[reCode.ID] = doc;
  621. }
  622. let sumMapAfter = {freight: actionMap};
  623. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMapAfter);
  624. this.refreshMaterialCalcCache(sumMapAfter);
  625. materialCalcObj.showDatas();
  626. }
  627. }catch (e){
  628. console.log(e)
  629. }finally {
  630. $.bootstrapLoading.end();
  631. }
  632. };
  633. ProjectGLJ.prototype.updateMaterialCalcTasks =async function(pgljID,dataMap,actionType,model){
  634. //为了统一接口, 参数dataMap 在添加操作时是一个数组,在修改的时候是一个ID - doc 映射表;
  635. try {
  636. let actionMap={};
  637. let calMap = {};
  638. actionMap[actionType] = dataMap;
  639. calMap[model] = actionMap;//{freight:{update:updateMap}
  640. let unitPrice = this.materialCal(pgljID,calMap);//计算当前这一条材料
  641. let [unitPrices,sumMap] = this.calcAllMaterial([unitPrice]);//计算完一条材料后,还要计算因为这条材料而受影响的其它所有材料
  642. if(actionType == "update") {//更新情况
  643. if (sumMap[model] && sumMap[model][actionType]) {//这一步,合并需要提交后台修改的数据
  644. let tt = sumMap[model][actionType];
  645. for(let key in dataMap) {
  646. tt[key] = dataMap[key];
  647. }
  648. } else {
  649. sumMap[model] = actionMap;
  650. }
  651. }else {
  652. sumMap[model] = actionMap;
  653. }
  654. sumMap["unitPrice"] = unitPrices;
  655. $.bootstrapLoading.start();
  656. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  657. this.refreshMaterialCalcCache(sumMap);
  658. materialCalcObj.showDatas();
  659. }catch (e){
  660. console.log(e)
  661. }finally {
  662. $.bootstrapLoading.end();
  663. }
  664. };
  665. ProjectGLJ.prototype.updateUserFreight =async function (data) {
  666. try {
  667. $.bootstrapLoading.start();
  668. let result = await ajaxPost('/glj/updateUserFreight',data);
  669. if(data.action == "add"){
  670. for(let f of data.freights){
  671. materialCalcObj.userFreightList.push(f);
  672. }
  673. }
  674. if(data.action == "update"){
  675. for(let u of materialCalcObj.userFreightList){
  676. if(u.ID == data.ID){
  677. gljUtil.updateProperty(u,data.doc);
  678. }
  679. }
  680. }
  681. materialCalcObj.showFreightDatas();
  682. }catch (e){
  683. console.log(e)
  684. }finally {
  685. $.bootstrapLoading.end();
  686. }
  687. };
  688. ProjectGLJ.prototype.getUserFreights = async function () {//初始化车船税选择
  689. try {
  690. let result = await ajaxGet("/glj/getUserFreights");
  691. return result;
  692. }catch (e){
  693. console.log(e)
  694. }
  695. };
  696. ProjectGLJ.prototype.refreshEctrovalenceCache = function (updateMap) {
  697. let elec = updateMap["electrovalence"];
  698. if(elec){
  699. let actionType = elec.actionType;
  700. if(actionType == "add"){
  701. projectObj.project.projectGLJ.datas.com_electrovalence = elec.doc;
  702. }else if(actionType == "update"){
  703. projectObj.project.projectGLJ.datas.com_electrovalence.gljList = elec.doc.gljList;
  704. }
  705. }
  706. if(updateMap["unitPrice"]) {
  707. let gljList = [];
  708. if(Array.isArray(updateMap["unitPrice"])){
  709. gljList= this.m_updateUnitPrice(updateMap["unitPrice"]);
  710. }else {
  711. gljList = this.m_updateUnitPrice([updateMap["unitPrice"]]);
  712. }
  713. const conditions = [];
  714. for(const glj of gljList){
  715. const keys = this.datas.mixRatioConnectData[gljUtil.getIndex(glj)]||[];
  716. for(const key of keys){
  717. conditions.push(gljOprObj.getConditionByKey(key));
  718. }
  719. }
  720. const parentGljs = this.getProjectGLJs(conditions,false)
  721. this.getImpactRationNodes([...gljList,...parentGljs]);//更新工料机类型的定额价格
  722. }
  723. };
  724. ProjectGLJ.prototype.refreshMaterialCalcCache = function (updateMap,isFromFeeRate = false){//从费率计算过来的时候,费率那里已经做了自动计算所有节点了
  725. let gljList = [];
  726. if(updateMap["freight"]) updateList(this.datas.freightList,updateMap["freight"]);
  727. if(updateMap["price"]) updateList(this.datas.originalList,updateMap["price"]);
  728. if(updateMap["unitPrice"]) {
  729. if(Array.isArray(updateMap["unitPrice"])){
  730. gljList = this.m_updateUnitPrice(updateMap["unitPrice"],isFromFeeRate);
  731. }else {
  732. gljList = this.m_updateUnitPrice([updateMap["unitPrice"]],isFromFeeRate);
  733. }
  734. const conditions = [];
  735. for(const glj of gljList){
  736. const keys = this.datas.mixRatioConnectData[gljUtil.getIndex(glj)]||[];
  737. for(const key of keys){
  738. conditions.push(gljOprObj.getConditionByKey(key));
  739. }
  740. }
  741. const parentGljs = this.getProjectGLJs(conditions,false)
  742. this.getImpactRationNodes([...gljList,...parentGljs]);//更新工料机类型的定额价格
  743. }
  744. return gljList;
  745. function updateList(list,updateMap) {
  746. if(updateMap["update"]){
  747. let map = updateMap["update"];
  748. for(let o of list){
  749. if(map[o.ID]){
  750. gljUtil.updateProperty(o,map[o.ID]);
  751. }
  752. }
  753. }
  754. if(updateMap["add"]){
  755. for(let a of updateMap["add"]){
  756. list.push(a);
  757. }
  758. }
  759. if(updateMap["delete"]){
  760. for(let ID in updateMap["delete"]){
  761. _.remove(list,{'ID':ID});
  762. }
  763. }
  764. }
  765. };
  766. ProjectGLJ.prototype.updatePriceCalc = async function (datas,pgljID) {
  767. let updateMap = {};
  768. for(let d of datas){
  769. updateMap[d.ID] = d.doc;
  770. }
  771. await this.updateMaterialCalcTasks(pgljID,updateMap,"update","price");
  772. };
  773. ProjectGLJ.prototype.addFreightCalc = async function (datas,pgljID) {
  774. await this.updateMaterialCalcTasks(pgljID,datas,"add","freight");
  775. };
  776. ProjectGLJ.prototype.updateFreightCalc = async function (datas,pgljID){
  777. let updateMap = {};
  778. for(let d of datas){
  779. updateMap[d.ID] = d.doc;
  780. }
  781. await this.updateMaterialCalcTasks(pgljID,updateMap,"update","freight");
  782. };
  783. ProjectGLJ.prototype.addMaterialRation = async function (code,type,parentID,connect_key){
  784. let libIDs = rationLibObj.getStdRationLibIDs();
  785. let defaultLibID = rationLibObj.getDefaultStdRationLibID();
  786. let selectedLib = sessionStorage.getItem("stdRationLib");
  787. selectedLib&&selectedLib!='undefined'?libIDs.unshift(selectedLib):libIDs.unshift(defaultLibID);
  788. let datas = {userID: userID,defaultLibID:defaultLibID, rationRepIds: libIDs, code: code,type:type,parentID:parentID,quantityDecimal:getDecimal("ration.quantity"),connect_key:connect_key,projectID:projectObj.project.ID()};
  789. datas.actionType="add";
  790. await this.updateMaterialRation(datas);
  791. };
  792. ProjectGLJ.prototype.changeAssistProductionFeeRate = async function (updateData) {//newFeeRate 机械不变费用系数 和 辅助生产间接费费率(%) 计算放一起
  793. $.bootstrapLoading.start();
  794. try {
  795. await ajaxPost("/glj/changeAssistProductionFeeRate",{unitFileID:projectObj.project.property.unitPriceFile.id,updateData:updateData});
  796. gljUtil.setProperty(this.datas.constData,updateData) //this.datas.constData.assistProductionFeeRate = newFeeRate;
  797. //todo 调用材料计算方法,所有材料重算一遍
  798. let [unitPrices,sumMap] = this.calcAllMaterial([],true);
  799. sumMap["unitPrice"] = unitPrices;
  800. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  801. this.refreshMaterialCalcCache(sumMap);
  802. materialCalcObj.showDatas();
  803. }catch (err){
  804. console.log(err);
  805. }finally {
  806. $.bootstrapLoading.end();
  807. }
  808. };
  809. ProjectGLJ.prototype.calcAllMaterialWhenChange = async function(){//当费率单价等改变时,重新计算所有材料 返回价格有变化的项目工料机
  810. let gljList = [];
  811. try {
  812. let [unitPrices,sumMap] = this.calcAllMaterial([],true);
  813. if(unitPrices.length > 0){
  814. sumMap["unitPrice"] = unitPrices;
  815. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  816. gljList = this.refreshMaterialCalcCache(sumMap,true);
  817. materialCalcObj.showDatas();
  818. }
  819. }catch (err){
  820. console.log(err);
  821. }
  822. return gljList
  823. }
  824. ProjectGLJ.prototype.loadFreightAndOriginalData = function(originalList,freightList){
  825. if(freightList) this.datas.freightList = freightList;
  826. if(originalList) this.datas.originalList = originalList;
  827. };
  828. ProjectGLJ.prototype.updateMaterialRation = async function (datas){
  829. try {
  830. let lists = datas.type == "freight"?this.datas.freightList:this.datas.originalList;
  831. $.bootstrapLoading.start();
  832. let result = await ajaxPost('/ration/updateMaterialRation',datas);
  833. if(datas.actionType=="add" && result.projectGLJList){
  834. this.loadNewProjectGLJToCaches(result.projectGLJList,true);
  835. this.loadFreightAndOriginalData(result.originalList,result.freightList); //这一步已经更新了运费和原价数据
  836. }
  837. if(datas.actionType == "delete"){
  838. let parent = _.find(lists,{ID:datas.parentID}) ;
  839. if(parent){
  840. _.remove(parent.rations,{ID:datas.rationID});
  841. _.remove(parent.ration_gljs,{rationID:datas.rationID});
  842. }
  843. }
  844. if(datas.actionType == "update"){
  845. let parent = _.find(lists,{ID:datas.parentID}) ;
  846. let ration = parent?_.find(parent.rations,{ID:datas.rationID}):null;
  847. if(ration){
  848. if(datas.field == "code" && result.projectGLJList){
  849. this.loadNewProjectGLJToCaches(result.projectGLJList,true);
  850. this.loadFreightAndOriginalData(result.originalList,result.freightList);
  851. }else {
  852. ration[datas.field] = datas.value;
  853. if(datas.ext){
  854. for(let key in datas.ext){
  855. ration[key] = datas.ext[key];
  856. }
  857. }
  858. }
  859. if(datas.ration_gljs){//更新定额工料机数量
  860. let gljMap = _.indexBy(parent.ration_gljs,"ID");
  861. for(let g of datas.ration_gljs){
  862. if(gljMap[g.ID]) gljMap[g.ID].quantity = g.quantity;
  863. }
  864. }
  865. }
  866. }
  867. }catch (e){
  868. console.log(e)
  869. }finally {
  870. //materialCalcObj.showDatas();
  871. this.calcMaterialRation(datas.parentID,datas.type);
  872. $.bootstrapLoading.end();
  873. }
  874. };
  875. ProjectGLJ.prototype.getHighlandFee = function (feeType) {
  876. let rates = projectObj.project.FeeRate.getActivateFeeRate().rates;
  877. let matchType = false;
  878. let rate = 0;
  879. //获取高原取费类别,是先按feeType取顶层的费率,然后取子项中的“高原地区施工增加费”
  880. if(!feeType||feeType=="") return 0;//空时为0
  881. let hightlandRate = null;
  882. let topParentID = null;
  883. for(let r of rates){//费率是按顺序排的,所以先取到顶层,之后的就是目标了。
  884. if(matchType && r.ParentID == topParentID) break; //如果feeType已经匹配过了,又经过了下一个顶层节点,那说明该feeType下没有高原费率
  885. if(r.name == feeType){
  886. matchType = true;
  887. topParentID = r.ParentID;
  888. }
  889. if(matchType && r.name == "高原地区施工增加费"){
  890. //顶层先匹配上了,第一个就是要找的费率
  891. hightlandRate = r;
  892. break;
  893. }
  894. }
  895. if(hightlandRate){
  896. rate = scMathUtil.roundForObj(hightlandRate.rate,getDecimal("feeRate"));
  897. }
  898. return rate*0.01
  899. };
  900. ProjectGLJ.prototype.calcEachFreightOrPrice = function (temp,type,priceMap,needUpdate=false) {//
  901. if(temp){
  902. if(type == "freight" && temp.conveyance !="自办运输") return null;
  903. let sum =0;
  904. let heightFee = 0;
  905. let gljMap = {};
  906. let expList = [];
  907. let decimal = type == "freight"?getDecimal("glj.unitPrice"):getDecimal("glj.unitPriceHasMix");
  908. //因为材料计算的数据是保存在单价文件里的,有可能存在共享的情况,这样的话就不能用单价文件里的项目工料机ID来匹配,要用5大项匹配
  909. let pgljMap = {};
  910. if(needUpdate==false && (!temp.rations || temp.rations.length == 0))return null;//没有定额时不计算
  911. for(let pg of this.datas.gljList){
  912. pgljMap[gljUtil.getIndex(pg)] = pg
  913. }
  914. if(temp.ration_gljs){
  915. for(let g of temp.ration_gljs){
  916. gljMap[g.rationID]?gljMap[g.rationID].push(g):gljMap[g.rationID]=[g];
  917. }
  918. }
  919. if(temp.rations) {
  920. for(let r of temp.rations){
  921. let [tsum,thei,exp] = calcRation(r,gljMap[r.ID],pgljMap,priceMap);
  922. heightFee = scMathUtil.roundForObj(heightFee+thei,6);
  923. sum = scMathUtil.roundForObj(sum + tsum,6);
  924. expList.push(exp);
  925. }
  926. }
  927. sum = scMathUtil.roundForObj(sum,decimal)+"";
  928. if(type == "freight"){
  929. let exp = expList.length == 0?`${temp.otherFee}×${temp.weightCoe}`:`((${expList.join("+")})+${temp.otherFee})×${temp.weightCoe}`
  930. let ndoc = {};
  931. if(temp.unitFreight != sum) ndoc['unitFreight'] = sum;
  932. ndoc['exp'] = exp;
  933. if(temp.heightFee != heightFee) ndoc['heightFee'] = heightFee;
  934. if(!_.isEmpty(ndoc)) return {ID:temp.ID,doc:ndoc};
  935. }
  936. if(type == "price" && temp.supplyPrice !=sum) return {ID:temp.ID,doc:{supplyPrice:sum,heightFee:heightFee}}
  937. }
  938. return null;
  939. function calcRation(ration,gljs,pMap,priceMap){
  940. let result = 0;
  941. let heightFee = 0;
  942. let exp = "";
  943. if(gljs){
  944. let processDecimal = getDecimal("process");
  945. let hightFeeRate = projectObj.project.projectGLJ.getHighlandFee(ration.feeType);//高原取费类别费率
  946. let assFeeRate = scMathUtil.roundForObj(projectObj.project.projectGLJ.datas.constData.assistProductionFeeRate,getDecimal("feeRate")) * 0.01;//辅助生产间接费费率
  947. let laberSum = 0;
  948. let machineSum = 0;
  949. let assList=[];
  950. let expList = [];
  951. let rationQuantity = scMathUtil.roundForObj(ration.quantity,getDecimal("ration.quantity"));
  952. for(let g of gljs){
  953. gljOprObj.setGLJPrice(g,pMap[gljUtil.getIndex(g)]);
  954. if(priceMap && priceMap[g.projectGLJID]) g.marketPrice = scMathUtil.roundForObj(priceMap[g.projectGLJID],getDecimal("glj.unitPrice"));
  955. let quantity = scMathUtil.roundForObj(g.quantity,getDecimal("glj.quantity"));
  956. let t = scMathUtil.roundForObj(quantity * g.marketPrice * rationQuantity,processDecimal);//市场价
  957. let r_price = gljUtil.getAssUsedPrice(g);//浙江定额时特殊处理,这里取市场价
  958. let rt = scMathUtil.roundForObj(quantity * r_price * rationQuantity,processDecimal);//定额价
  959. let calString = `${quantity}×${r_price}×${rationQuantity}`;
  960. if(g.type == gljUtil.gljType.LABOUR){
  961. laberSum = scMathUtil.roundForObj(laberSum+rt,processDecimal);
  962. assList.push(`${calString}×${assFeeRate*100}%`)
  963. }
  964. if(gljUtil.getMainType(g.type) == 3) machineSum = scMathUtil.roundForObj(machineSum+rt,processDecimal);
  965. expList.push(`${quantity}×${g.marketPrice}×${rationQuantity}`);
  966. result = scMathUtil.roundForObj(result + t,processDecimal);
  967. }
  968. let as = scMathUtil.roundForObj(laberSum * assFeeRate,processDecimal);//人工消耗量*定额价*定额工程量*辅助生产间接费费率
  969. let tt = scMathUtil.roundForObj(laberSum + machineSum,processDecimal);//人工定额消耗量*定额价*定额工程量+机械定额消耗量*定额价*定额工程量
  970. let hs = scMathUtil.roundForObj(tt*hightFeeRate,processDecimal);//(人工定额消耗量*定额价*定额工程量+机械定额消耗量*定额价*定额工程量)*高原取费类别费率
  971. exp = expList.join("+");
  972. if(hightFeeRate!=0) exp += `+${tt}×${hightFeeRate*100}%`;
  973. if(assFeeRate!=0 && assList.length > 0) exp += '+'+assList.join("+");
  974. result = scMathUtil.roundForObj(as + result,processDecimal);
  975. result = scMathUtil.roundForObj(hs + result,processDecimal);
  976. heightFee = hs;
  977. }
  978. return [result,heightFee,exp];
  979. }
  980. };
  981. ProjectGLJ.prototype.calcMaterialAssQuantity = function (parent,ration,newAssList) {
  982. let updateList = [];
  983. let assList = calcAssList(newAssList);
  984. for(let glj of parent.ration_gljs){
  985. if(glj.rationID == ration.ID){
  986. updateList.push(calcAssGljQuantity(glj,assList));
  987. }
  988. }
  989. return updateList;
  990. function calcAssGljQuantity(g,assList) {
  991. g.quantity = scMathUtil.roundForObj(g.rationItemQuantity,getDecimal("glj.quantity"));
  992. for(let a of assList){
  993. let ass = a.ass;
  994. let matchGlj = null;
  995. for(let aglj of ass.rationGljList){
  996. if(aglj.gljId == g.GLJID){
  997. matchGlj = aglj;
  998. break;
  999. }
  1000. }
  1001. if(matchGlj){
  1002. let calQuantity =scMathUtil.roundForObj(matchGlj.consumeAmt*a.times,6);
  1003. g.quantity = scMathUtil.roundForObj(calQuantity + g.quantity,getDecimal("glj.quantity"))+"";
  1004. }
  1005. }
  1006. return {ID:g.ID,quantity:g.quantity}
  1007. }
  1008. function calcAssList(rationAssList){
  1009. let calAss = [];
  1010. for(let ass of rationAssList){
  1011. let times = calculateTimes(ass);
  1012. if(times!=0){
  1013. calAss.push({ass:ass.assRation,times:times});
  1014. }
  1015. }
  1016. return calAss;
  1017. }
  1018. function calculateTimes(ass){
  1019. if(ass.name.indexOf("运距") == -1) return 0;//只计算运距;
  1020. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  1021. let r = false;
  1022. if(times<0){
  1023. r=true;
  1024. times=times*-1;
  1025. }
  1026. if(ass.carryBit=='四舍五入'){
  1027. times = _.round(times,ass.decimal);
  1028. }else if (ass.carryBit=='进一'){
  1029. times =_.ceil(times,ass.decimal);
  1030. }else if(ass.carryBit == '舍一'){
  1031. times = _.floor(times,ass.decimal);
  1032. }
  1033. if(r){
  1034. times=times*-1;
  1035. }
  1036. return scMathUtil.roundTo(times,-6);
  1037. }
  1038. };
  1039. ProjectGLJ.prototype.calcMaterialRation = function(ID,type){//计算带定额的运费或者原价
  1040. let lists = type == "freight"?this.datas.freightList:this.datas.originalList;
  1041. let temp = _.find(lists,{ID:ID});
  1042. if(temp){
  1043. let task = this.calcEachFreightOrPrice(temp,type);
  1044. let material = materialCalcObj.getMaterialSelected();
  1045. if(task && material){
  1046. if(type == "freight") projectObj.project.projectGLJ.updateFreightCalc([task],material.id);
  1047. if(type == "price") projectObj.project.projectGLJ.updatePriceCalc([task],material.id);
  1048. }else {
  1049. materialCalcObj.showDatas();
  1050. }
  1051. }
  1052. };
  1053. ProjectGLJ.prototype.deletePriceCalc = async function (ID,pgljID) {
  1054. let map = {};
  1055. map[ID] = true;
  1056. await this.updateMaterialCalcTasks(pgljID,map,"delete","price");
  1057. };
  1058. ProjectGLJ.prototype.deleteFreightCalc = async function (ID,pgljID) {
  1059. let map = {};
  1060. map[ID] = true;
  1061. await this.updateMaterialCalcTasks(pgljID,map,"delete","freight");
  1062. };
  1063. ProjectGLJ.prototype.updatePrice = function (recode, updateField, newval,from,cb) {
  1064. let me = this;
  1065. let projectGljs = this.datas.gljList;
  1066. let pgljID = from=="rg"?recode.projectGLJID:recode.id;//和定额工料机统一接口,项目工料机ID取值不一样
  1067. let glj = _.find(projectGljs, {'id': pgljID});
  1068. if (glj) {
  1069. if(glj.unit_price[updateField] == newval){
  1070. return;
  1071. }
  1072. newval = scMathUtil.roundForObj(newval,this.getPriceDecimal(glj));
  1073. let data = {id: glj.unit_price.id, field: updateField, newval: newval,project_id:glj.project_id,unit_price_file_id:glj.unit_price.unit_price_file_id};
  1074. if(updateField == 'market_price' && (this.datas.mixRatioConnectData[gljUtil.getIndex(glj)] || glj.name == "电网电")){//计算是否有受影响的综合电价
  1075. let uobj = {};
  1076. uobj[glj.id] = {marketPrice:newval};
  1077. let ext =electrovalenceObj.clacNewElecPrice(uobj);
  1078. if(ext) data.ext = ext;
  1079. }
  1080. let callback =async function (result) {
  1081. if (updateField == 'base_price') {
  1082. glj.unit_price.base_price = newval;
  1083. me.setAdjustPrice(glj);
  1084. } else {
  1085. glj.unit_price.market_price = newval;
  1086. }
  1087. let elec_glj = null;
  1088. if(data.ext){
  1089. elec_glj = _.find(projectGljs, {'id': data.ext.projectGLJID});
  1090. if(elec_glj) elec_glj.unit_price.market_price = data.ext.doc.market_price;
  1091. }
  1092. //更新回传的父节点项目工料机价格
  1093. let gljs = me.getProjectGLJs(result);
  1094. // me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  1095. let materialGljList =await me.calcAllMaterialWhenChange()
  1096. gljs= gljs.concat(materialGljList);
  1097. projectGljObject.refreshDataSheet();//更新工料机汇总缓存和显示
  1098. //projectObj.project.projectGLJ.loadCacheData();
  1099. gljOprObj.showRationGLJSheetData();
  1100. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  1101. gljs.push(glj);
  1102. if(elec_glj) gljs.push(elec_glj);
  1103. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1104. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1105. if(materialGljList.length==0)projectGljObject.onUnitFileChange(result);
  1106. divideObj.calcDivideAfterGLJUpdate(gljs);
  1107. if(cb){
  1108. cb(gljs);
  1109. }
  1110. $.bootstrapLoading.end();
  1111. }
  1112. $.bootstrapLoading.start();
  1113. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  1114. $.bootstrapLoading.end();
  1115. });
  1116. } else {
  1117. gljOprObj.showRationGLJSheetData();
  1118. }
  1119. };
  1120. ProjectGLJ.prototype.batchUpdateGLJProperty = function (updateMap,callback) {
  1121. let me = this;
  1122. //更新是否暂估和供货方式时,要做特殊处理
  1123. $.bootstrapLoading.start();
  1124. CommonAjax.post("/glj/batchUpdateGLJProperty", updateMap,function (result) {
  1125. $.bootstrapLoading.end();
  1126. let supplyChangeIDs = [],evaluate_gljs = [],rationNodes = [];
  1127. for(let idKey in updateMap){
  1128. let gljID = parseInt(idKey);
  1129. let glj = me.getByID(gljID);
  1130. let doc = updateMap[idKey];
  1131. for(let property in doc){
  1132. glj[property] = doc[property];
  1133. }
  1134. if(doc.hasOwnProperty("supply")||doc.hasOwnProperty("supply_quantity")){
  1135. supplyChangeIDs.push(gljID);
  1136. }
  1137. if(doc.hasOwnProperty("is_evaluate")){
  1138. evaluate_gljs.push(glj);
  1139. }
  1140. }
  1141. if(supplyChangeIDs.length>0){
  1142. let temRationNodes = calcTools.getRationsByProjectGLJ(supplyChangeIDs);
  1143. if (temRationNodes.length > 0) rationNodes = rationNodes.concat(temRationNodes);
  1144. }
  1145. if(evaluate_gljs.length > 0){
  1146. let [impactRationNodes,impactGLJs] = me.batchChangeIsEvaluate(evaluate_gljs);
  1147. rationNodes = rationNodes.concat(impactRationNodes);
  1148. }
  1149. if(rationNodes.length > 0){
  1150. rationNodes = _.uniq(rationNodes,'data');
  1151. projectObj.project.calcProgram.calcNodesAndSave(rationNodes, function () {
  1152. projectObj.mainController.refreshTreeNode(projectObj.project.mainTree.roots);
  1153. });
  1154. }
  1155. if(callback){
  1156. callback();
  1157. }
  1158. })
  1159. };
  1160. ProjectGLJ.prototype.batchUpdatePrice = function (changeInfo,sheetName,callback) {
  1161. let me = this;
  1162. let projectGljs = me.datas.gljList;
  1163. let updateData = [];
  1164. let newValueMap = {};
  1165. let gljs=[];
  1166. let setting = sheetName =="materialTreeSheet"?projectGljObject.materialSetting:projectGljObject.projectGljSetting;
  1167. if(changeInfo.length<=0){
  1168. callback?callback():'';
  1169. return
  1170. }
  1171. for(let ci of changeInfo){
  1172. let dataCode = setting.header[ci.col].dataCode;
  1173. let recode = sheetName =="materialTreeSheet"?projectGljObject.materialTree.items[ci.row].data:projectGljObject.projectGljSheetData[ci.row];
  1174. if(dataCode=='basePrice'||dataCode=='marketPrice'){
  1175. let editField = dataCode === 'basePrice'?"base_price":"market_price";
  1176. let glj = _.find(projectGljs, {'id': recode.id});
  1177. let newValue = ci.value;
  1178. if(glj&&glj.unit_price[editField]!=newValue){
  1179. newValue= scMathUtil.roundForObj(ci.value,this.getPriceDecimal(glj));
  1180. updateData.push({unit_price: glj.unit_price, field: editField, newval: newValue,project_id:glj.project_id});
  1181. newValueMap[glj.id]={field:editField,value:newValue};
  1182. gljs.push(glj);
  1183. }
  1184. }
  1185. }
  1186. if(updateData.length > 0){
  1187. $.bootstrapLoading.start();
  1188. CommonAjax.post("/glj/batchUpdatePrices", updateData,async function (result) {
  1189. let parentData = [];
  1190. //更新缓存
  1191. for(let g of gljs){
  1192. g.unit_price[newValueMap[g.id].field] = newValueMap[g.id].value;
  1193. me.refreshTreeNodePriceIfNeed(g);//刷新造价书中主树上的定额工料机;
  1194. }
  1195. //更新父工料机价格
  1196. for(let r of result){
  1197. let pdata = r.updateOne.filter;
  1198. let set = r.updateOne.update.$set;
  1199. for(let skey in set){
  1200. pdata[skey] = set[skey];
  1201. }
  1202. parentData.push(pdata);
  1203. }
  1204. let pgljs = me.getProjectGLJs(parentData);
  1205. gljs = gljs.concat(pgljs);
  1206. let materialGljList = await me.calcAllMaterialWhenChange();
  1207. gljs = gljs.concat(materialGljList);
  1208. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1209. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1210. gljOprObj.showRationGLJSheetData();
  1211. if(materialGljList.length == 0) projectGljObject.onUnitFileChange(gljs);
  1212. if(callback){
  1213. callback(gljs);
  1214. }
  1215. $.bootstrapLoading.end();
  1216. }, function (err) {
  1217. $.bootstrapLoading.end();
  1218. });
  1219. }
  1220. };
  1221. ProjectGLJ.prototype.batchUpdateConsumption = function (updateData,updateMap,callback) {
  1222. let me = this;
  1223. $.bootstrapLoading.start();
  1224. CommonAjax.post("/glj/batchUpdateConsumption", updateData, function (result) {
  1225. let unitPriceMap = {}, mu=null, parentID = null;
  1226. for(let u of updateData){
  1227. if(u.type == 'mix_ratio'){
  1228. mu = u;
  1229. }else if(u.type == 'unitPrice'){
  1230. unitPriceMap[u.projectGLJID] = u.doc;
  1231. if(u.isParent) parentID = u.projectGLJID;
  1232. }
  1233. }
  1234. if(!_.isEmpty(unitPriceMap)){
  1235. for(let g of me.datas.gljList) {
  1236. if(unitPriceMap[g.id]){
  1237. gljUtil.setProperty(g.unit_price,unitPriceMap[g.id]);
  1238. }
  1239. if(parentID == g.id) updateMixRatio(g,mu,me);
  1240. }
  1241. }
  1242. me.calcQuantity();
  1243. if(callback){
  1244. callback();
  1245. }
  1246. $.bootstrapLoading.end();
  1247. },function () {
  1248. $.bootstrapLoading.end();
  1249. })
  1250. function updateMixRatio(parent,u,me) {
  1251. let tem = updateMap[u.query.id];
  1252. tem.record.consumption = u.doc.consumption;//更新组成物表格的缓存
  1253. let subData = _.find(parent.ratio_data,{"id":u.query.id});
  1254. if(subData){
  1255. subData.consumption = u.doc.consumption;
  1256. }
  1257. let m_list = me.datas.mixRatioMap[gljUtil.getIndex(parent)];
  1258. let m_ratioData = _.find(m_list,{"id":u.query.id});
  1259. if(m_ratioData){
  1260. m_ratioData.consumption = u.doc.consumption;
  1261. }
  1262. }
  1263. };
  1264. ProjectGLJ.prototype.pGljUpdate= function (data,callback) {
  1265. let me = this;
  1266. $.bootstrapLoading.start();
  1267. CommonAjax.specialPost( '/glj/update',data,function (result) {
  1268. let glj = me.getByID(data.id);//更新缓存
  1269. let impactList = [];
  1270. glj[data.field] = data.value;
  1271. if(data.extend&&data.extend!=""){
  1272. let extend = JSON.parse(data.extend);
  1273. for (let key in extend) {
  1274. glj[key] = extend[key];
  1275. }
  1276. }
  1277. if(data.field == 'is_evaluate'){
  1278. impactList = me.changeIsEvaluate(data.id);
  1279. }
  1280. if(callback){
  1281. callback(impactList);
  1282. }
  1283. $.bootstrapLoading.end();
  1284. });
  1285. };
  1286. ProjectGLJ.prototype.getRatioData=function(id,callback){
  1287. this.getRatioId = id;
  1288. if(id){
  1289. CommonAjax.specialPost( '/glj/get-ratio',{id: id, project_id: scUrlUtil.GetQueryString('project')},function (response) {
  1290. let ratios = JSON.parse(response.data);
  1291. if(callback){
  1292. callback(ratios);
  1293. }
  1294. },function () {//取不到组成物的情况
  1295. callback([]);
  1296. })
  1297. }else {
  1298. if(callback){
  1299. callback([]);
  1300. }
  1301. }
  1302. };
  1303. ProjectGLJ.prototype.changeFile = function (changeData,callback) {
  1304. $.bootstrapLoading.start();
  1305. CommonAjax.specialPost('/glj/change-file',changeData,function (response) {
  1306. projectObj.project.property.unitPriceFile = response.unitFile;
  1307. projectObj.project.projectGLJ.loadData(function () {
  1308. if(callback){
  1309. callback();
  1310. }
  1311. $.bootstrapLoading.end();
  1312. });
  1313. }, function (err) {
  1314. if($.bootstrapLoading.isLoading()){
  1315. $.bootstrapLoading.end();
  1316. }
  1317. alert(err.msg);
  1318. });
  1319. };
  1320. ProjectGLJ.prototype.changeVvTaxFile = async function (newVvTaxFileID,callback) {
  1321. $.bootstrapLoading.start();
  1322. try {
  1323. await ajaxPost("/glj/changeVvTaxFile",{unitFileID:projectObj.project.property.unitPriceFile.id,newVvTaxFileID:newVvTaxFileID});
  1324. await this.synLoadData();
  1325. projectGljObject.refreshDataSheet();
  1326. gljOprObj.refreshView();
  1327. projectObj.project.ration_glj.reCalcWhenVvTaxChange();
  1328. projectGljObject.onUnitFileChange();
  1329. }catch (err){
  1330. console.log(err);
  1331. }finally {
  1332. $.bootstrapLoading.end();
  1333. }
  1334. };
  1335. ProjectGLJ.prototype.checkUnitFileName = function(newVal,callback){
  1336. let property = projectObj.project.projectInfo.property;
  1337. let data = {
  1338. name:newVal,
  1339. rootProjectID:property.rootProjectID
  1340. }
  1341. CommonAjax.post('/glj/checkUnitFileName', data, function (data) {
  1342. callback(data);
  1343. });
  1344. };
  1345. ProjectGLJ.prototype.saveAs = function (saveData,callback) {
  1346. $.bootstrapLoading.start();
  1347. CommonAjax.specialPost('/glj/save-as',saveData,function () {
  1348. projectObj.project.projectGLJ.loadData(function () {
  1349. if(callback){
  1350. callback();
  1351. }
  1352. $.bootstrapLoading.end();
  1353. });
  1354. },function (response) {
  1355. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '另存为失败!';
  1356. $("#save-as-tips").text(msg).show();
  1357. $.bootstrapLoading.end();
  1358. });
  1359. };
  1360. //更新是否暂估
  1361. ProjectGLJ.prototype.changeIsEvaluate=function (id){
  1362. let projectGLJ = projectObj.project.projectGLJ;
  1363. let datas = projectGLJ.datas;
  1364. let gljList = datas.gljList;
  1365. let glj = _.find(gljList, {'id': id});
  1366. if(glj){//与批量更新调用相同的方法
  1367. /* let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  1368. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  1369. let conditions = [];
  1370. if(pratioM&&pratioM.length>0){
  1371. for(let p_key of pratioM ){
  1372. conditions.push(gljOprObj.getConditionByKey(p_key));
  1373. }
  1374. }
  1375. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  1376. gljs.push(glj);
  1377. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1378. //更新对应的工料机类型的定额
  1379. let rations =_.filter(projectObj.project.Ration.datas,{'type':rationType.gljRation,'projectGLJID':glj.id});
  1380. let ration_nodes = [];
  1381. for(r of rations){
  1382. if(r){
  1383. r.isEstimate =glj.is_evaluate?1:0;
  1384. let ration_node = projectObj.project.mainTree.getNodeByID(r.ID);
  1385. ration_node?ration_nodes.push(ration_node):'';
  1386. }
  1387. }
  1388. let ration_glj_nodes = projectGLJ.getMainAndEquGLJNodeByID(glj.id);//取显示在造价书界面上的主材和设备节点
  1389. for(rg of ration_glj_nodes){
  1390. rg.data.isEstimate =glj.is_evaluate?1:0;
  1391. ration_nodes.push(rg);
  1392. }
  1393. ration_nodes.length>0?projectObj.mainController.refreshTreeNode(ration_nodes):"";*/
  1394. let [nodes,gljs] = projectGLJ.batchChangeIsEvaluate([glj]);
  1395. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1396. return gljs;
  1397. }
  1398. };
  1399. /**
  1400. * 批量更新是否暂估
  1401. *传入项目工料机数组
  1402. */
  1403. ProjectGLJ.prototype.batchChangeIsEvaluate=function (gljs){
  1404. let impactGLJs = [], changeArray =_.map(gljs,'id'),changeMap = _.indexBy(gljs,'id');;
  1405. for(let glj of gljs){
  1406. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  1407. let pratioM =this.datas.mixRatioConnectData[con_key];//找到父key
  1408. let conditions = [];
  1409. if(pratioM&&pratioM.length>0){
  1410. for(let p_key of pratioM ){
  1411. conditions.push(gljOprObj.getConditionByKey(p_key));
  1412. }
  1413. }
  1414. let tem_gljs = this.getProjectGLJs(conditions,false);
  1415. if(tem_gljs.length > 0) impactGLJs = impactGLJs.concat(tem_gljs);
  1416. }
  1417. impactGLJs = impactGLJs.concat(gljs);
  1418. impactGLJs = _.uniq(impactGLJs,'id');//去重复
  1419. let impactRationNodes = this.getImpactRationNodes(impactGLJs);
  1420. let neeRefreshNode = [];
  1421. //更新对应的工料机类型的定额
  1422. for(let item of projectObj.project.Ration.datas){
  1423. if(item.type == rationType.gljRation && changeArray.indexOf(item.projectGLJID) != -1){
  1424. let tem_g = changeMap[item.projectGLJID];
  1425. item.isEstimate =tem_g.is_evaluate?1:0;
  1426. let ration_node = projectObj.project.mainTree.getNodeByID(item.ID);
  1427. neeRefreshNode?neeRefreshNode.push(ration_node):'';
  1428. }
  1429. }
  1430. let ration_glj_nodes = this.getMainAndEquGLJNodeByID(changeArray);//取显示在造价书界面上的主材和设备节点
  1431. for(let rg of ration_glj_nodes){
  1432. let tem_g = changeMap[rg.data.projectGLJID];
  1433. rg.data.isEstimate =tem_g.is_evaluate?1:0;
  1434. neeRefreshNode.push(rg);
  1435. }
  1436. neeRefreshNode.length>0?projectObj.mainController.refreshTreeNode(neeRefreshNode):"";
  1437. return [impactRationNodes,impactGLJs];
  1438. };
  1439. ProjectGLJ.prototype.getByID = function (ID) {
  1440. return _.find(this.datas.gljList,{'id':ID});
  1441. };
  1442. ProjectGLJ.prototype.getByConKey = function (conkey) {//根据5个连接属性取对应的工料机
  1443. return _.find(this.datas.gljList,function (item) {
  1444. let tem_key = gljOprObj.getIndex(item,gljKeyArray);
  1445. return tem_key == conkey
  1446. })
  1447. };
  1448. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  1449. if ((data.unit_price.type == gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  1450. let me = this;
  1451. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  1452. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  1453. tem.data.marketUnitFee = me.getMarketPrice(data);//data.unit_price.market_price;
  1454. tem.data.marketPrice = me.getMarketPrice(data);//data.unit_price.market_price;
  1455. return true;
  1456. }
  1457. })
  1458. projectObj.mainController.refreshTreeNode(nodes);
  1459. }
  1460. }
  1461. ProjectGLJ.prototype.getMainAndEquGLJNodeByID = function (id) {//通过ID取显示到主树上的主材和设备节点
  1462. let nodes = [];
  1463. let ids = [];
  1464. if(Array.isArray(id)){
  1465. ids = id;
  1466. }else {
  1467. ids = [id];
  1468. }
  1469. if(projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true){
  1470. nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  1471. return tem.sourceType == ModuleNames.ration_glj && ids.indexOf(tem.data.projectGLJID) !== -1
  1472. })
  1473. }
  1474. return nodes;
  1475. };
  1476. //根据工料机,取得所有受影响的定额节点
  1477. ProjectGLJ.prototype.getImpactRationNodes = function (gljs, skipEmptyQuantity) {
  1478. let nodes = [];
  1479. let rationMap = {};
  1480. let idArray = _.map(gljs,'id');
  1481. let gljMap = _.indexBy(gljs,'id');
  1482. //先根据项目工料机ID,找到受影响定额的ID
  1483. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  1484. for (let rg of ration_glj_list) {
  1485. if (_.indexOf(idArray,rg.projectGLJID)!=-1 && (!skipEmptyQuantity || (skipEmptyQuantity && +rg.quantity))) {
  1486. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  1487. }
  1488. }
  1489. for (let item of projectObj.project.mainTree.items) {
  1490. if (item.sourceType == ModuleNames.ration) {
  1491. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  1492. let tem_g = gljMap[item.data.projectGLJID];
  1493. if(tem_g){
  1494. item.data.marketUnitFee = this.getMarketPrice(tem_g);//这里要按计算的市场价为准,不能直接取
  1495. item.data.marketPrice = this.getMarketPrice(tem_g);
  1496. nodes.push(item);
  1497. }
  1498. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  1499. nodes.push(item)
  1500. }
  1501. }
  1502. }
  1503. return nodes;
  1504. };
  1505. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  1506. for (let ration_glj of gljOprObj.sheetData) {
  1507. if (ration_glj.projectGLJID == glj.id) {
  1508. ration_glj.basePrice = this.getBasePrice(glj);
  1509. ration_glj.marketPrice = this.getMarketPrice(glj);
  1510. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  1511. }
  1512. }
  1513. }
  1514. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  1515. }
  1516. ProjectGLJ.prototype.getProjectGLJs = function (data,refreshPrice=true) {
  1517. let parentGlj = [];
  1518. //
  1519. let projectGljs = this.datas.gljList;
  1520. let indexList = gljKeyArray;
  1521. for (let d of data) {
  1522. if (d) {
  1523. let glj = _.find(projectGljs, function (item) {
  1524. return gljOprObj.getIndex(item,indexList) == gljOprObj.getIndex(d,indexList);
  1525. });
  1526. if (glj) {
  1527. if(refreshPrice==true){
  1528. d.base_price?glj.unit_price.base_price = d.base_price:'';
  1529. d.market_price?glj.unit_price.market_price = d.market_price:'';
  1530. this.setAdjustPrice(glj);
  1531. this.refreshRationGLJPrice(glj);
  1532. this.refreshTreeNodePriceIfNeed(glj);
  1533. }
  1534. parentGlj.push(glj);
  1535. }
  1536. }
  1537. }
  1538. return parentGlj;
  1539. }
  1540. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  1541. switch (glj.unit_price.type + '') {
  1542. // 人工: 调整基价=基价单价*调整系数
  1543. case GLJTypeConst.LABOUR:
  1544. case GLJTypeConst.MACHINE_LABOUR:
  1545. glj.adjust_price = this.getAdjustPrice(glj);
  1546. break;
  1547. // 机械类型的算法
  1548. case GLJTypeConst.MACHINE:
  1549. console.log('机械');
  1550. break;
  1551. // 材料、主材、设备
  1552. default:
  1553. glj.adjust_price = glj.unit_price.base_price;
  1554. }
  1555. }
  1556. ProjectGLJ.prototype.getAdjustPrice = function (glj,isRadio) {
  1557. let proGLJ = projectObj.project.projectGLJ;
  1558. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1559. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1560. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1561. return gljUtil.getAdjustPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1562. };
  1563. ProjectGLJ.prototype.getBasePrice = function(glj,isRadio){
  1564. let proGLJ = projectObj.project.projectGLJ;
  1565. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1566. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1567. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1568. return gljUtil.getBasePrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1569. };
  1570. ProjectGLJ.prototype.getMarketPrice = function (glj,isRadio) {
  1571. let proGLJ = projectObj.project.projectGLJ;
  1572. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1573. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1574. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1575. return gljUtil.getMarketPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1576. };
  1577. ProjectGLJ.prototype.getTenderMarketPrice = function (glj,isRadio) {
  1578. let proGLJ = projectObj.project.projectGLJ;
  1579. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1580. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1581. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1582. let tenderCoe = this.getTenderPriceCoe(glj);
  1583. return gljUtil.getMarketPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil,tenderCoe);
  1584. };
  1585. ProjectGLJ.prototype.getTenderPriceCoe = function(glj,tproperty){
  1586. return gljUtil.getTenderPriceCoe(glj,tproperty);
  1587. };
  1588. ProjectGLJ.prototype.isEstimateType = function(type){
  1589. let typeString = type + "";
  1590. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料、主材、设备类型才显示是否暂估
  1591. return type;
  1592. }
  1593. return false;
  1594. };
  1595. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  1596. let gljTypeMap = this.datas.constData.gljTypeMap;
  1597. return gljTypeMap["typeId" + ID]?gljTypeMap["typeId" + ID].shortName:'';
  1598. };
  1599. ProjectGLJ.prototype.calcQuantity = function (init=false){
  1600. let project_gljs = this.datas.gljList;
  1601. let changeArray=[];
  1602. let rationGLJDatas = projectObj.project.ration_glj.datas;
  1603. let rationDatas = projectObj.project.Ration.datas;
  1604. let billsDatas = projectObj.project.Bills.datas;
  1605. gljUtil.calcProjectGLJQuantity(this.datas,rationGLJDatas,rationDatas,billsDatas,getDecimal("glj.quantity"),_,scMathUtil);
  1606. if(init == true || this.quantityChangeMap == null){//如果是初始化,建立一个映射表
  1607. this.quantityChangeMap = {};
  1608. for(let pglj of project_gljs){
  1609. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1610. }
  1611. }else if(this.quantityChangeMap != null){
  1612. for(let pglj of project_gljs){
  1613. if(this.quantityChangeMap[pglj.id] != undefined|| this.quantityChangeMap[pglj.id] != null){
  1614. if(this.quantityChangeMap[pglj.id] != pglj.quantity){
  1615. changeArray.push(pglj);
  1616. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1617. }
  1618. }else { //映射表没有,说明是新添加的项目工料机
  1619. changeArray.push(pglj);
  1620. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1621. }
  1622. }
  1623. }
  1624. changeArray.length > 0 && projectGljObject.calcPartASupplyFeeByProjectGLJs ?projectGljObject.calcPartASupplyFeeByProjectGLJs(changeArray):'';
  1625. };
  1626. ProjectGLJ.prototype.calcTenderQuantity = function (){
  1627. let rationGLJDatas = projectObj.project.ration_glj.datas;
  1628. let rationDatas = projectObj.project.Ration.datas;
  1629. let billsDatas = projectObj.project.Bills.datas;
  1630. gljUtil.calcProjectGLJQuantity(this.datas,rationGLJDatas,rationDatas,billsDatas,getDecimal("glj.quantity"),_,scMathUtil,true);
  1631. };
  1632. ProjectGLJ.prototype.loadNewProjectGLJToCaches = function (datas,calquantity = false) {
  1633. if(!datas) return;
  1634. let gljIDMap = {};
  1635. for (let d of datas){
  1636. if(d) this.loadNewProjectGLJToCache(d,gljIDMap);
  1637. }
  1638. if(datas.length > 0 && calquantity) this.calcQuantity();
  1639. };
  1640. ProjectGLJ.prototype.loadNewProjectGLJToCache = function (data,tIDMap) {//把新插入的项目工料机数据增加至缓存中
  1641. if(!data) return;
  1642. let project_gljs = this.datas.gljList;
  1643. let unitPriceMap = this.datas.unitPriceMap;
  1644. let mixRatioMap = this.datas.mixRatioMap;
  1645. let mixRatioConnectData = this.datas.mixRatioConnectData;
  1646. let IDMap = !_.isEmpty(tIDMap)?tIDMap:_.indexBy(project_gljs,'id');
  1647. let tem = IDMap[data.id];
  1648. if(tem) return tem; //判断该工料机是否已经存在,是的话不用再次添加
  1649. //查看是否有组成物,有组成物的话先添加组成物信息
  1650. data.ratio_data=[];
  1651. if(data.subList && data.subList.length){
  1652. for(let s of data.subList){
  1653. let ratio = s.ratio_data;
  1654. data.ratio_data.push(ratio);
  1655. mixRatioMap[ratio.connect_key]?mixRatioMap[ratio.connect_key].push(ratio):mixRatioMap[ratio.connect_key] = [ratio];
  1656. let rIndex = gljUtil.getIndex(ratio);
  1657. mixRatioConnectData[rIndex]?mixRatioConnectData[rIndex].push(ratio.connect_key):mixRatioConnectData[rIndex] = [ratio.connect_key];
  1658. this.loadNewProjectGLJToCache(s,IDMap);
  1659. }
  1660. delete data.subList;
  1661. }
  1662. //添加unitPriceMap
  1663. let uIndex = gljUtil.getIndex(data.unit_price);
  1664. if(!unitPriceMap[uIndex]) unitPriceMap[uIndex] = data.unit_price;
  1665. this.datas.gljList.push(data);
  1666. this.datas.gljMap[gljUtil.getIndex(data)]= data;
  1667. IDMap[data.id] = data;
  1668. return data;
  1669. };
  1670. class DivideSetting {
  1671. constructor (project) {
  1672. this.project = project;
  1673. this.datas = [];
  1674. project.registerModule(ModuleNames.divide_setting, this);
  1675. };
  1676. loadData (datas) {
  1677. this.datas = datas;
  1678. };
  1679. }
  1680. class EvaluateList {
  1681. constructor (project) {
  1682. this.project = project;
  1683. this.datas = [];
  1684. project.registerModule(ModuleNames.evaluate_list, this);
  1685. };
  1686. loadData (datas) {
  1687. this.datas = datas;
  1688. };
  1689. }
  1690. class BidEvaluationList {
  1691. constructor (project) {
  1692. this.project = project;
  1693. this.datas = [];
  1694. project.registerModule(ModuleNames.bid_evaluation_list, this);
  1695. };
  1696. loadData (datas) {
  1697. this.datas = datas;
  1698. };
  1699. }