project_glj.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  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. }
  735. return gljList;
  736. function updateList(list,updateMap) {
  737. if(updateMap["update"]){
  738. let map = updateMap["update"];
  739. for(let o of list){
  740. if(map[o.ID]){
  741. gljUtil.updateProperty(o,map[o.ID]);
  742. }
  743. }
  744. }
  745. if(updateMap["add"]){
  746. for(let a of updateMap["add"]){
  747. list.push(a);
  748. }
  749. }
  750. if(updateMap["delete"]){
  751. for(let ID in updateMap["delete"]){
  752. _.remove(list,{'ID':ID});
  753. }
  754. }
  755. }
  756. };
  757. ProjectGLJ.prototype.updatePriceCalc = async function (datas,pgljID) {
  758. let updateMap = {};
  759. for(let d of datas){
  760. updateMap[d.ID] = d.doc;
  761. }
  762. await this.updateMaterialCalcTasks(pgljID,updateMap,"update","price");
  763. };
  764. ProjectGLJ.prototype.addFreightCalc = async function (datas,pgljID) {
  765. await this.updateMaterialCalcTasks(pgljID,datas,"add","freight");
  766. };
  767. ProjectGLJ.prototype.updateFreightCalc = async function (datas,pgljID){
  768. let updateMap = {};
  769. for(let d of datas){
  770. updateMap[d.ID] = d.doc;
  771. }
  772. await this.updateMaterialCalcTasks(pgljID,updateMap,"update","freight");
  773. };
  774. ProjectGLJ.prototype.addMaterialRation = async function (code,type,parentID,connect_key){
  775. let libIDs = rationLibObj.getStdRationLibIDs();
  776. let defaultLibID = rationLibObj.getDefaultStdRationLibID();
  777. let selectedLib = sessionStorage.getItem("stdRationLib");
  778. selectedLib&&selectedLib!='undefined'?libIDs.unshift(selectedLib):libIDs.unshift(defaultLibID);
  779. 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()};
  780. datas.actionType="add";
  781. await this.updateMaterialRation(datas);
  782. };
  783. ProjectGLJ.prototype.changeAssistProductionFeeRate = async function (updateData) {//newFeeRate 机械不变费用系数 和 辅助生产间接费费率(%) 计算放一起
  784. $.bootstrapLoading.start();
  785. try {
  786. await ajaxPost("/glj/changeAssistProductionFeeRate",{unitFileID:projectObj.project.property.unitPriceFile.id,updateData:updateData});
  787. gljUtil.setProperty(this.datas.constData,updateData) //this.datas.constData.assistProductionFeeRate = newFeeRate;
  788. //todo 调用材料计算方法,所有材料重算一遍
  789. let [unitPrices,sumMap] = this.calcAllMaterial([],true);
  790. sumMap["unitPrice"] = unitPrices;
  791. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  792. this.refreshMaterialCalcCache(sumMap);
  793. materialCalcObj.showDatas();
  794. }catch (err){
  795. console.log(err);
  796. }finally {
  797. $.bootstrapLoading.end();
  798. }
  799. };
  800. ProjectGLJ.prototype.calcAllMaterialWhenChange = async function(){//当费率单价等改变时,重新计算所有材料 返回价格有变化的项目工料机
  801. let gljList = [];
  802. try {
  803. let [unitPrices,sumMap] = this.calcAllMaterial([],true);
  804. if(unitPrices.length > 0){
  805. sumMap["unitPrice"] = unitPrices;
  806. let result = await ajaxPost('/glj/updateMaterialCalcTasks',sumMap);
  807. gljList = this.refreshMaterialCalcCache(sumMap,true);
  808. materialCalcObj.showDatas();
  809. }
  810. }catch (err){
  811. console.log(err);
  812. }
  813. return gljList
  814. }
  815. ProjectGLJ.prototype.loadFreightAndOriginalData = function(originalList,freightList){
  816. if(freightList) this.datas.freightList = freightList;
  817. if(originalList) this.datas.originalList = originalList;
  818. };
  819. ProjectGLJ.prototype.updateMaterialRation = async function (datas){
  820. try {
  821. let lists = datas.type == "freight"?this.datas.freightList:this.datas.originalList;
  822. $.bootstrapLoading.start();
  823. let result = await ajaxPost('/ration/updateMaterialRation',datas);
  824. if(datas.actionType=="add" && result.projectGLJList){
  825. this.loadNewProjectGLJToCaches(result.projectGLJList,true);
  826. this.loadFreightAndOriginalData(result.originalList,result.freightList); //这一步已经更新了运费和原价数据
  827. }
  828. if(datas.actionType == "delete"){
  829. let parent = _.find(lists,{ID:datas.parentID}) ;
  830. if(parent){
  831. _.remove(parent.rations,{ID:datas.rationID});
  832. _.remove(parent.ration_gljs,{rationID:datas.rationID});
  833. }
  834. }
  835. if(datas.actionType == "update"){
  836. let parent = _.find(lists,{ID:datas.parentID}) ;
  837. let ration = parent?_.find(parent.rations,{ID:datas.rationID}):null;
  838. if(ration){
  839. if(datas.field == "code" && result.projectGLJList){
  840. this.loadNewProjectGLJToCaches(result.projectGLJList,true);
  841. this.loadFreightAndOriginalData(result.originalList,result.freightList);
  842. }else {
  843. ration[datas.field] = datas.value;
  844. if(datas.ext){
  845. for(let key in datas.ext){
  846. ration[key] = datas.ext[key];
  847. }
  848. }
  849. }
  850. if(datas.ration_gljs){//更新定额工料机数量
  851. let gljMap = _.indexBy(parent.ration_gljs,"ID");
  852. for(let g of datas.ration_gljs){
  853. if(gljMap[g.ID]) gljMap[g.ID].quantity = g.quantity;
  854. }
  855. }
  856. }
  857. }
  858. }catch (e){
  859. console.log(e)
  860. }finally {
  861. //materialCalcObj.showDatas();
  862. this.calcMaterialRation(datas.parentID,datas.type);
  863. $.bootstrapLoading.end();
  864. }
  865. };
  866. ProjectGLJ.prototype.getHighlandFee = function (feeType) {
  867. let rates = projectObj.project.FeeRate.getActivateFeeRate().rates;
  868. let matchType = false;
  869. let rate = 0;
  870. //获取高原取费类别,是先按feeType取顶层的费率,然后取子项中的“高原地区施工增加费”
  871. if(!feeType||feeType=="") return 0;//空时为0
  872. let hightlandRate = null;
  873. let topParentID = null;
  874. for(let r of rates){//费率是按顺序排的,所以先取到顶层,之后的就是目标了。
  875. if(matchType && r.ParentID == topParentID) break; //如果feeType已经匹配过了,又经过了下一个顶层节点,那说明该feeType下没有高原费率
  876. if(r.name == feeType){
  877. matchType = true;
  878. topParentID = r.ParentID;
  879. }
  880. if(matchType && r.name == "高原地区施工增加费"){
  881. //顶层先匹配上了,第一个就是要找的费率
  882. hightlandRate = r;
  883. break;
  884. }
  885. }
  886. if(hightlandRate){
  887. rate = scMathUtil.roundForObj(hightlandRate.rate,getDecimal("feeRate"));
  888. }
  889. return rate*0.01
  890. };
  891. ProjectGLJ.prototype.calcEachFreightOrPrice = function (temp,type,priceMap,needUpdate=false) {//
  892. if(temp){
  893. if(type == "freight" && temp.conveyance !="自办运输") return null;
  894. let sum =0;
  895. let heightFee = 0;
  896. let gljMap = {};
  897. let expList = [];
  898. let decimal = type == "freight"?getDecimal("glj.unitPrice"):getDecimal("glj.unitPriceHasMix");
  899. //因为材料计算的数据是保存在单价文件里的,有可能存在共享的情况,这样的话就不能用单价文件里的项目工料机ID来匹配,要用5大项匹配
  900. let pgljMap = {};
  901. if(needUpdate==false && (!temp.rations || temp.rations.length == 0))return null;//没有定额时不计算
  902. for(let pg of this.datas.gljList){
  903. pgljMap[gljUtil.getIndex(pg)] = pg
  904. }
  905. if(temp.ration_gljs){
  906. for(let g of temp.ration_gljs){
  907. gljMap[g.rationID]?gljMap[g.rationID].push(g):gljMap[g.rationID]=[g];
  908. }
  909. }
  910. if(temp.rations) {
  911. for(let r of temp.rations){
  912. let [tsum,thei,exp] = calcRation(r,gljMap[r.ID],pgljMap,priceMap);
  913. heightFee = scMathUtil.roundForObj(heightFee+thei,6);
  914. sum = scMathUtil.roundForObj(sum + tsum,6);
  915. expList.push(exp);
  916. }
  917. }
  918. sum = scMathUtil.roundForObj(sum,decimal)+"";
  919. if(type == "freight"){
  920. let exp = expList.length == 0?`${temp.otherFee}×${temp.weightCoe}`:`((${expList.join("+")})+${temp.otherFee})×${temp.weightCoe}`
  921. let ndoc = {};
  922. if(temp.unitFreight != sum) ndoc['unitFreight'] = sum;
  923. ndoc['exp'] = exp;
  924. if(temp.heightFee != heightFee) ndoc['heightFee'] = heightFee;
  925. if(!_.isEmpty(ndoc)) return {ID:temp.ID,doc:ndoc};
  926. }
  927. if(type == "price" && temp.supplyPrice !=sum) return {ID:temp.ID,doc:{supplyPrice:sum,heightFee:heightFee}}
  928. }
  929. return null;
  930. function calcRation(ration,gljs,pMap,priceMap){
  931. let result = 0;
  932. let heightFee = 0;
  933. let exp = "";
  934. if(gljs){
  935. let processDecimal = getDecimal("process");
  936. let hightFeeRate = projectObj.project.projectGLJ.getHighlandFee(ration.feeType);//高原取费类别费率
  937. let assFeeRate = scMathUtil.roundForObj(projectObj.project.projectGLJ.datas.constData.assistProductionFeeRate,getDecimal("feeRate")) * 0.01;//辅助生产间接费费率
  938. let laberSum = 0;
  939. let machineSum = 0;
  940. let assList=[];
  941. let expList = [];
  942. let rationQuantity = scMathUtil.roundForObj(ration.quantity,getDecimal("ration.quantity"));
  943. for(let g of gljs){
  944. gljOprObj.setGLJPrice(g,pMap[gljUtil.getIndex(g)]);
  945. if(priceMap && priceMap[g.projectGLJID]) g.marketPrice = scMathUtil.roundForObj(priceMap[g.projectGLJID],getDecimal("glj.unitPrice"));
  946. let quantity = scMathUtil.roundForObj(g.quantity,getDecimal("glj.quantity"));
  947. let t = scMathUtil.roundForObj(quantity * g.marketPrice * rationQuantity,processDecimal);//市场价
  948. let r_price = gljUtil.getAssUsedPrice(g);//浙江定额时特殊处理,这里取市场价
  949. let rt = scMathUtil.roundForObj(quantity * r_price * rationQuantity,processDecimal);//定额价
  950. let calString = `${quantity}×${r_price}×${rationQuantity}`;
  951. if(g.type == gljUtil.gljType.LABOUR){
  952. laberSum = scMathUtil.roundForObj(laberSum+rt,processDecimal);
  953. assList.push(`${calString}×${assFeeRate*100}%`)
  954. }
  955. if(gljUtil.getMainType(g.type) == 3) machineSum = scMathUtil.roundForObj(machineSum+rt,processDecimal);
  956. expList.push(`${quantity}×${g.marketPrice}×${rationQuantity}`);
  957. result = scMathUtil.roundForObj(result + t,processDecimal);
  958. }
  959. let as = scMathUtil.roundForObj(laberSum * assFeeRate,processDecimal);//人工消耗量*定额价*定额工程量*辅助生产间接费费率
  960. let tt = scMathUtil.roundForObj(laberSum + machineSum,processDecimal);//人工定额消耗量*定额价*定额工程量+机械定额消耗量*定额价*定额工程量
  961. let hs = scMathUtil.roundForObj(tt*hightFeeRate,processDecimal);//(人工定额消耗量*定额价*定额工程量+机械定额消耗量*定额价*定额工程量)*高原取费类别费率
  962. exp = expList.join("+");
  963. if(hightFeeRate!=0) exp += `+${tt}×${hightFeeRate*100}%`;
  964. if(assFeeRate!=0 && assList.length > 0) exp += '+'+assList.join("+");
  965. result = scMathUtil.roundForObj(as + result,processDecimal);
  966. result = scMathUtil.roundForObj(hs + result,processDecimal);
  967. heightFee = hs;
  968. }
  969. return [result,heightFee,exp];
  970. }
  971. };
  972. ProjectGLJ.prototype.calcMaterialAssQuantity = function (parent,ration,newAssList) {
  973. let updateList = [];
  974. let assList = calcAssList(newAssList);
  975. for(let glj of parent.ration_gljs){
  976. if(glj.rationID == ration.ID){
  977. updateList.push(calcAssGljQuantity(glj,assList));
  978. }
  979. }
  980. return updateList;
  981. function calcAssGljQuantity(g,assList) {
  982. g.quantity = scMathUtil.roundForObj(g.rationItemQuantity,getDecimal("glj.quantity"));
  983. for(let a of assList){
  984. let ass = a.ass;
  985. let matchGlj = null;
  986. for(let aglj of ass.rationGljList){
  987. if(aglj.gljId == g.GLJID){
  988. matchGlj = aglj;
  989. break;
  990. }
  991. }
  992. if(matchGlj){
  993. let calQuantity =scMathUtil.roundForObj(matchGlj.consumeAmt*a.times,6);
  994. g.quantity = scMathUtil.roundForObj(calQuantity + g.quantity,getDecimal("glj.quantity"))+"";
  995. }
  996. }
  997. return {ID:g.ID,quantity:g.quantity}
  998. }
  999. function calcAssList(rationAssList){
  1000. let calAss = [];
  1001. for(let ass of rationAssList){
  1002. let times = calculateTimes(ass);
  1003. if(times!=0){
  1004. calAss.push({ass:ass.assRation,times:times});
  1005. }
  1006. }
  1007. return calAss;
  1008. }
  1009. function calculateTimes(ass){
  1010. if(ass.name.indexOf("运距") == -1) return 0;//只计算运距;
  1011. let times =(ass.actualValue-ass.stdValue)/ass.stepValue;
  1012. let r = false;
  1013. if(times<0){
  1014. r=true;
  1015. times=times*-1;
  1016. }
  1017. if(ass.carryBit=='四舍五入'){
  1018. times = _.round(times,ass.decimal);
  1019. }else if (ass.carryBit=='进一'){
  1020. times =_.ceil(times,ass.decimal);
  1021. }else if(ass.carryBit == '舍一'){
  1022. times = _.floor(times,ass.decimal);
  1023. }
  1024. if(r){
  1025. times=times*-1;
  1026. }
  1027. return scMathUtil.roundTo(times,-6);
  1028. }
  1029. };
  1030. ProjectGLJ.prototype.calcMaterialRation = function(ID,type){//计算带定额的运费或者原价
  1031. let lists = type == "freight"?this.datas.freightList:this.datas.originalList;
  1032. let temp = _.find(lists,{ID:ID});
  1033. if(temp){
  1034. let task = this.calcEachFreightOrPrice(temp,type);
  1035. let material = materialCalcObj.getMaterialSelected();
  1036. if(task && material){
  1037. if(type == "freight") projectObj.project.projectGLJ.updateFreightCalc([task],material.id);
  1038. if(type == "price") projectObj.project.projectGLJ.updatePriceCalc([task],material.id);
  1039. }else {
  1040. materialCalcObj.showDatas();
  1041. }
  1042. }
  1043. };
  1044. ProjectGLJ.prototype.deletePriceCalc = async function (ID,pgljID) {
  1045. let map = {};
  1046. map[ID] = true;
  1047. await this.updateMaterialCalcTasks(pgljID,map,"delete","price");
  1048. };
  1049. ProjectGLJ.prototype.deleteFreightCalc = async function (ID,pgljID) {
  1050. let map = {};
  1051. map[ID] = true;
  1052. await this.updateMaterialCalcTasks(pgljID,map,"delete","freight");
  1053. };
  1054. ProjectGLJ.prototype.updatePrice = function (recode, updateField, newval,from,cb) {
  1055. let me = this;
  1056. let projectGljs = this.datas.gljList;
  1057. let pgljID = from=="rg"?recode.projectGLJID:recode.id;//和定额工料机统一接口,项目工料机ID取值不一样
  1058. let glj = _.find(projectGljs, {'id': pgljID});
  1059. if (glj) {
  1060. if(glj.unit_price[updateField] == newval){
  1061. return;
  1062. }
  1063. newval = scMathUtil.roundForObj(newval,this.getPriceDecimal(glj));
  1064. 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};
  1065. if(updateField == 'market_price' && (this.datas.mixRatioConnectData[gljUtil.getIndex(glj)] || glj.name == "电网电")){//计算是否有受影响的综合电价
  1066. let uobj = {};
  1067. uobj[glj.id] = {marketPrice:newval};
  1068. let ext =electrovalenceObj.clacNewElecPrice(uobj);
  1069. if(ext) data.ext = ext;
  1070. }
  1071. let callback =async function (result) {
  1072. if (updateField == 'base_price') {
  1073. glj.unit_price.base_price = newval;
  1074. me.setAdjustPrice(glj);
  1075. } else {
  1076. glj.unit_price.market_price = newval;
  1077. }
  1078. let elec_glj = null;
  1079. if(data.ext){
  1080. elec_glj = _.find(projectGljs, {'id': data.ext.projectGLJID});
  1081. if(elec_glj) elec_glj.unit_price.market_price = data.ext.doc.market_price;
  1082. }
  1083. //更新回传的父节点项目工料机价格
  1084. let gljs = me.getProjectGLJs(result);
  1085. // me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  1086. let materialGljList =await me.calcAllMaterialWhenChange()
  1087. gljs= gljs.concat(materialGljList);
  1088. projectGljObject.refreshDataSheet();//更新工料机汇总缓存和显示
  1089. //projectObj.project.projectGLJ.loadCacheData();
  1090. gljOprObj.showRationGLJSheetData();
  1091. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  1092. gljs.push(glj);
  1093. if(elec_glj) gljs.push(elec_glj);
  1094. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1095. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1096. if(materialGljList.length==0)projectGljObject.onUnitFileChange(result);
  1097. divideObj.calcDivideAfterGLJUpdate(gljs);
  1098. if(cb){
  1099. cb(gljs);
  1100. }
  1101. $.bootstrapLoading.end();
  1102. }
  1103. $.bootstrapLoading.start();
  1104. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  1105. $.bootstrapLoading.end();
  1106. });
  1107. } else {
  1108. gljOprObj.showRationGLJSheetData();
  1109. }
  1110. };
  1111. ProjectGLJ.prototype.batchUpdateGLJProperty = function (updateMap,callback) {
  1112. let me = this;
  1113. //更新是否暂估和供货方式时,要做特殊处理
  1114. $.bootstrapLoading.start();
  1115. CommonAjax.post("/glj/batchUpdateGLJProperty", updateMap,function (result) {
  1116. $.bootstrapLoading.end();
  1117. let supplyChangeIDs = [],evaluate_gljs = [],rationNodes = [];
  1118. for(let idKey in updateMap){
  1119. let gljID = parseInt(idKey);
  1120. let glj = me.getByID(gljID);
  1121. let doc = updateMap[idKey];
  1122. for(let property in doc){
  1123. glj[property] = doc[property];
  1124. }
  1125. if(doc.hasOwnProperty("supply")||doc.hasOwnProperty("supply_quantity")){
  1126. supplyChangeIDs.push(gljID);
  1127. }
  1128. if(doc.hasOwnProperty("is_evaluate")){
  1129. evaluate_gljs.push(glj);
  1130. }
  1131. }
  1132. if(supplyChangeIDs.length>0){
  1133. let temRationNodes = calcTools.getRationsByProjectGLJ(supplyChangeIDs);
  1134. if (temRationNodes.length > 0) rationNodes = rationNodes.concat(temRationNodes);
  1135. }
  1136. if(evaluate_gljs.length > 0){
  1137. let [impactRationNodes,impactGLJs] = me.batchChangeIsEvaluate(evaluate_gljs);
  1138. rationNodes = rationNodes.concat(impactRationNodes);
  1139. }
  1140. if(rationNodes.length > 0){
  1141. rationNodes = _.uniq(rationNodes,'data');
  1142. projectObj.project.calcProgram.calcNodesAndSave(rationNodes, function () {
  1143. projectObj.mainController.refreshTreeNode(projectObj.project.mainTree.roots);
  1144. });
  1145. }
  1146. if(callback){
  1147. callback();
  1148. }
  1149. })
  1150. };
  1151. ProjectGLJ.prototype.batchUpdatePrice = function (changeInfo,sheetName,callback) {
  1152. let me = this;
  1153. let projectGljs = me.datas.gljList;
  1154. let updateData = [];
  1155. let newValueMap = {};
  1156. let gljs=[];
  1157. let setting = sheetName =="materialTreeSheet"?projectGljObject.materialSetting:projectGljObject.projectGljSetting;
  1158. if(changeInfo.length<=0){
  1159. callback?callback():'';
  1160. return
  1161. }
  1162. for(let ci of changeInfo){
  1163. let dataCode = setting.header[ci.col].dataCode;
  1164. let recode = sheetName =="materialTreeSheet"?projectGljObject.materialTree.items[ci.row].data:projectGljObject.projectGljSheetData[ci.row];
  1165. if(dataCode=='basePrice'||dataCode=='marketPrice'){
  1166. let editField = dataCode === 'basePrice'?"base_price":"market_price";
  1167. let glj = _.find(projectGljs, {'id': recode.id});
  1168. let newValue = ci.value;
  1169. if(glj&&glj.unit_price[editField]!=newValue){
  1170. newValue= scMathUtil.roundForObj(ci.value,this.getPriceDecimal(glj));
  1171. updateData.push({unit_price: glj.unit_price, field: editField, newval: newValue,project_id:glj.project_id});
  1172. newValueMap[glj.id]={field:editField,value:newValue};
  1173. gljs.push(glj);
  1174. }
  1175. }
  1176. }
  1177. if(updateData.length > 0){
  1178. $.bootstrapLoading.start();
  1179. CommonAjax.post("/glj/batchUpdatePrices", updateData,async function (result) {
  1180. let parentData = [];
  1181. //更新缓存
  1182. for(let g of gljs){
  1183. g.unit_price[newValueMap[g.id].field] = newValueMap[g.id].value;
  1184. me.refreshTreeNodePriceIfNeed(g);//刷新造价书中主树上的定额工料机;
  1185. }
  1186. //更新父工料机价格
  1187. for(let r of result){
  1188. let pdata = r.updateOne.filter;
  1189. let set = r.updateOne.update.$set;
  1190. for(let skey in set){
  1191. pdata[skey] = set[skey];
  1192. }
  1193. parentData.push(pdata);
  1194. }
  1195. let pgljs = me.getProjectGLJs(parentData);
  1196. gljs = gljs.concat(pgljs);
  1197. let materialGljList = await me.calcAllMaterialWhenChange();
  1198. gljs = gljs.concat(materialGljList);
  1199. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1200. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1201. gljOprObj.showRationGLJSheetData();
  1202. if(materialGljList.length == 0) projectGljObject.onUnitFileChange(gljs);
  1203. if(callback){
  1204. callback(gljs);
  1205. }
  1206. $.bootstrapLoading.end();
  1207. }, function (err) {
  1208. $.bootstrapLoading.end();
  1209. });
  1210. }
  1211. };
  1212. ProjectGLJ.prototype.batchUpdateConsumption = function (updateData,updateMap,callback) {
  1213. let me = this;
  1214. $.bootstrapLoading.start();
  1215. CommonAjax.post("/glj/batchUpdateConsumption", updateData, function (result) {
  1216. let unitPriceMap = {}, mu=null, parentID = null;
  1217. for(let u of updateData){
  1218. if(u.type == 'mix_ratio'){
  1219. mu = u;
  1220. }else if(u.type == 'unitPrice'){
  1221. unitPriceMap[u.projectGLJID] = u.doc;
  1222. if(u.isParent) parentID = u.projectGLJID;
  1223. }
  1224. }
  1225. if(!_.isEmpty(unitPriceMap)){
  1226. for(let g of me.datas.gljList) {
  1227. if(unitPriceMap[g.id]){
  1228. gljUtil.setProperty(g.unit_price,unitPriceMap[g.id]);
  1229. }
  1230. if(parentID == g.id) updateMixRatio(g,mu,me);
  1231. }
  1232. }
  1233. me.calcQuantity();
  1234. if(callback){
  1235. callback();
  1236. }
  1237. $.bootstrapLoading.end();
  1238. },function () {
  1239. $.bootstrapLoading.end();
  1240. })
  1241. function updateMixRatio(parent,u,me) {
  1242. let tem = updateMap[u.query.id];
  1243. tem.record.consumption = u.doc.consumption;//更新组成物表格的缓存
  1244. let subData = _.find(parent.ratio_data,{"id":u.query.id});
  1245. if(subData){
  1246. subData.consumption = u.doc.consumption;
  1247. }
  1248. let m_list = me.datas.mixRatioMap[gljUtil.getIndex(parent)];
  1249. let m_ratioData = _.find(m_list,{"id":u.query.id});
  1250. if(m_ratioData){
  1251. m_ratioData.consumption = u.doc.consumption;
  1252. }
  1253. }
  1254. };
  1255. ProjectGLJ.prototype.pGljUpdate= function (data,callback) {
  1256. let me = this;
  1257. $.bootstrapLoading.start();
  1258. CommonAjax.specialPost( '/glj/update',data,function (result) {
  1259. let glj = me.getByID(data.id);//更新缓存
  1260. let impactList = [];
  1261. glj[data.field] = data.value;
  1262. if(data.extend&&data.extend!=""){
  1263. let extend = JSON.parse(data.extend);
  1264. for (let key in extend) {
  1265. glj[key] = extend[key];
  1266. }
  1267. }
  1268. if(data.field == 'is_evaluate'){
  1269. impactList = me.changeIsEvaluate(data.id);
  1270. }
  1271. if(callback){
  1272. callback(impactList);
  1273. }
  1274. $.bootstrapLoading.end();
  1275. });
  1276. };
  1277. ProjectGLJ.prototype.getRatioData=function(id,callback){
  1278. this.getRatioId = id;
  1279. if(id){
  1280. CommonAjax.specialPost( '/glj/get-ratio',{id: id, project_id: scUrlUtil.GetQueryString('project')},function (response) {
  1281. let ratios = JSON.parse(response.data);
  1282. if(callback){
  1283. callback(ratios);
  1284. }
  1285. },function () {//取不到组成物的情况
  1286. callback([]);
  1287. })
  1288. }else {
  1289. if(callback){
  1290. callback([]);
  1291. }
  1292. }
  1293. };
  1294. ProjectGLJ.prototype.changeFile = function (changeData,callback) {
  1295. $.bootstrapLoading.start();
  1296. CommonAjax.specialPost('/glj/change-file',changeData,function (response) {
  1297. projectObj.project.property.unitPriceFile = response.unitFile;
  1298. projectObj.project.projectGLJ.loadData(function () {
  1299. if(callback){
  1300. callback();
  1301. }
  1302. $.bootstrapLoading.end();
  1303. });
  1304. }, function (err) {
  1305. if($.bootstrapLoading.isLoading()){
  1306. $.bootstrapLoading.end();
  1307. }
  1308. alert(err.msg);
  1309. });
  1310. };
  1311. ProjectGLJ.prototype.changeVvTaxFile = async function (newVvTaxFileID,callback) {
  1312. $.bootstrapLoading.start();
  1313. try {
  1314. await ajaxPost("/glj/changeVvTaxFile",{unitFileID:projectObj.project.property.unitPriceFile.id,newVvTaxFileID:newVvTaxFileID});
  1315. await this.synLoadData();
  1316. projectGljObject.refreshDataSheet();
  1317. gljOprObj.refreshView();
  1318. projectObj.project.ration_glj.reCalcWhenVvTaxChange();
  1319. projectGljObject.onUnitFileChange();
  1320. }catch (err){
  1321. console.log(err);
  1322. }finally {
  1323. $.bootstrapLoading.end();
  1324. }
  1325. };
  1326. ProjectGLJ.prototype.checkUnitFileName = function(newVal,callback){
  1327. let property = projectObj.project.projectInfo.property;
  1328. let data = {
  1329. name:newVal,
  1330. rootProjectID:property.rootProjectID
  1331. }
  1332. CommonAjax.post('/glj/checkUnitFileName', data, function (data) {
  1333. callback(data);
  1334. });
  1335. };
  1336. ProjectGLJ.prototype.saveAs = function (saveData,callback) {
  1337. $.bootstrapLoading.start();
  1338. CommonAjax.specialPost('/glj/save-as',saveData,function () {
  1339. projectObj.project.projectGLJ.loadData(function () {
  1340. if(callback){
  1341. callback();
  1342. }
  1343. $.bootstrapLoading.end();
  1344. });
  1345. },function (response) {
  1346. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '另存为失败!';
  1347. $("#save-as-tips").text(msg).show();
  1348. $.bootstrapLoading.end();
  1349. });
  1350. };
  1351. //更新是否暂估
  1352. ProjectGLJ.prototype.changeIsEvaluate=function (id){
  1353. let projectGLJ = projectObj.project.projectGLJ;
  1354. let datas = projectGLJ.datas;
  1355. let gljList = datas.gljList;
  1356. let glj = _.find(gljList, {'id': id});
  1357. if(glj){//与批量更新调用相同的方法
  1358. /* let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  1359. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  1360. let conditions = [];
  1361. if(pratioM&&pratioM.length>0){
  1362. for(let p_key of pratioM ){
  1363. conditions.push(gljOprObj.getConditionByKey(p_key));
  1364. }
  1365. }
  1366. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  1367. gljs.push(glj);
  1368. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  1369. //更新对应的工料机类型的定额
  1370. let rations =_.filter(projectObj.project.Ration.datas,{'type':rationType.gljRation,'projectGLJID':glj.id});
  1371. let ration_nodes = [];
  1372. for(r of rations){
  1373. if(r){
  1374. r.isEstimate =glj.is_evaluate?1:0;
  1375. let ration_node = projectObj.project.mainTree.getNodeByID(r.ID);
  1376. ration_node?ration_nodes.push(ration_node):'';
  1377. }
  1378. }
  1379. let ration_glj_nodes = projectGLJ.getMainAndEquGLJNodeByID(glj.id);//取显示在造价书界面上的主材和设备节点
  1380. for(rg of ration_glj_nodes){
  1381. rg.data.isEstimate =glj.is_evaluate?1:0;
  1382. ration_nodes.push(rg);
  1383. }
  1384. ration_nodes.length>0?projectObj.mainController.refreshTreeNode(ration_nodes):"";*/
  1385. let [nodes,gljs] = projectGLJ.batchChangeIsEvaluate([glj]);
  1386. projectObj.project.calcProgram.calcNodesAndSave(nodes);//触发计算程序
  1387. return gljs;
  1388. }
  1389. };
  1390. /**
  1391. * 批量更新是否暂估
  1392. *传入项目工料机数组
  1393. */
  1394. ProjectGLJ.prototype.batchChangeIsEvaluate=function (gljs){
  1395. let impactGLJs = [], changeArray =_.map(gljs,'id'),changeMap = _.indexBy(gljs,'id');;
  1396. for(let glj of gljs){
  1397. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  1398. let pratioM =this.datas.mixRatioConnectData[con_key];//找到父key
  1399. let conditions = [];
  1400. if(pratioM&&pratioM.length>0){
  1401. for(let p_key of pratioM ){
  1402. conditions.push(gljOprObj.getConditionByKey(p_key));
  1403. }
  1404. }
  1405. let tem_gljs = this.getProjectGLJs(conditions,false);
  1406. if(tem_gljs.length > 0) impactGLJs = impactGLJs.concat(tem_gljs);
  1407. }
  1408. impactGLJs = impactGLJs.concat(gljs);
  1409. impactGLJs = _.uniq(impactGLJs,'id');//去重复
  1410. let impactRationNodes = this.getImpactRationNodes(impactGLJs);
  1411. let neeRefreshNode = [];
  1412. //更新对应的工料机类型的定额
  1413. for(let item of projectObj.project.Ration.datas){
  1414. if(item.type == rationType.gljRation && changeArray.indexOf(item.projectGLJID) != -1){
  1415. let tem_g = changeMap[item.projectGLJID];
  1416. item.isEstimate =tem_g.is_evaluate?1:0;
  1417. let ration_node = projectObj.project.mainTree.getNodeByID(item.ID);
  1418. neeRefreshNode?neeRefreshNode.push(ration_node):'';
  1419. }
  1420. }
  1421. let ration_glj_nodes = this.getMainAndEquGLJNodeByID(changeArray);//取显示在造价书界面上的主材和设备节点
  1422. for(let rg of ration_glj_nodes){
  1423. let tem_g = changeMap[rg.data.projectGLJID];
  1424. rg.data.isEstimate =tem_g.is_evaluate?1:0;
  1425. neeRefreshNode.push(rg);
  1426. }
  1427. neeRefreshNode.length>0?projectObj.mainController.refreshTreeNode(neeRefreshNode):"";
  1428. return [impactRationNodes,impactGLJs];
  1429. };
  1430. ProjectGLJ.prototype.getByID = function (ID) {
  1431. return _.find(this.datas.gljList,{'id':ID});
  1432. };
  1433. ProjectGLJ.prototype.getByConKey = function (conkey) {//根据5个连接属性取对应的工料机
  1434. return _.find(this.datas.gljList,function (item) {
  1435. let tem_key = gljOprObj.getIndex(item,gljKeyArray);
  1436. return tem_key == conkey
  1437. })
  1438. };
  1439. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  1440. if ((data.unit_price.type == gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  1441. let me = this;
  1442. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  1443. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  1444. tem.data.marketUnitFee = me.getMarketPrice(data);//data.unit_price.market_price;
  1445. tem.data.marketPrice = me.getMarketPrice(data);//data.unit_price.market_price;
  1446. return true;
  1447. }
  1448. })
  1449. projectObj.mainController.refreshTreeNode(nodes);
  1450. }
  1451. }
  1452. ProjectGLJ.prototype.getMainAndEquGLJNodeByID = function (id) {//通过ID取显示到主树上的主材和设备节点
  1453. let nodes = [];
  1454. let ids = [];
  1455. if(Array.isArray(id)){
  1456. ids = id;
  1457. }else {
  1458. ids = [id];
  1459. }
  1460. if(projectObj.project.projectInfo.property.displaySetting.disPlayMainMaterial == true){
  1461. nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  1462. return tem.sourceType == ModuleNames.ration_glj && ids.indexOf(tem.data.projectGLJID) !== -1
  1463. })
  1464. }
  1465. return nodes;
  1466. };
  1467. //根据工料机,取得所有受影响的定额节点
  1468. ProjectGLJ.prototype.getImpactRationNodes = function (gljs, skipEmptyQuantity) {
  1469. let nodes = [];
  1470. let rationMap = {};
  1471. let idArray = _.map(gljs,'id');
  1472. let gljMap = _.indexBy(gljs,'id');
  1473. //先根据项目工料机ID,找到受影响定额的ID
  1474. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  1475. for (let rg of ration_glj_list) {
  1476. if (_.indexOf(idArray,rg.projectGLJID)!=-1 && (!skipEmptyQuantity || (skipEmptyQuantity && +rg.quantity))) {
  1477. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  1478. }
  1479. }
  1480. for (let item of projectObj.project.mainTree.items) {
  1481. if (item.sourceType == ModuleNames.ration) {
  1482. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  1483. let tem_g = gljMap[item.data.projectGLJID];
  1484. if(tem_g){
  1485. item.data.marketUnitFee = this.getMarketPrice(tem_g);//这里要按计算的市场价为准,不能直接取
  1486. item.data.marketPrice = this.getMarketPrice(tem_g);
  1487. nodes.push(item);
  1488. }
  1489. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  1490. nodes.push(item)
  1491. }
  1492. }
  1493. }
  1494. return nodes;
  1495. };
  1496. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  1497. for (let ration_glj of gljOprObj.sheetData) {
  1498. if (ration_glj.projectGLJID == glj.id) {
  1499. ration_glj.basePrice = this.getBasePrice(glj);
  1500. ration_glj.marketPrice = this.getMarketPrice(glj);
  1501. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  1502. }
  1503. }
  1504. }
  1505. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  1506. }
  1507. ProjectGLJ.prototype.getProjectGLJs = function (data,refreshPrice=true) {
  1508. let parentGlj = [];
  1509. //
  1510. let projectGljs = this.datas.gljList;
  1511. let indexList = gljKeyArray;
  1512. for (let d of data) {
  1513. if (d) {
  1514. let glj = _.find(projectGljs, function (item) {
  1515. return gljOprObj.getIndex(item,indexList) == gljOprObj.getIndex(d,indexList);
  1516. });
  1517. if (glj) {
  1518. if(refreshPrice==true){
  1519. d.base_price?glj.unit_price.base_price = d.base_price:'';
  1520. d.market_price?glj.unit_price.market_price = d.market_price:'';
  1521. this.setAdjustPrice(glj);
  1522. this.refreshRationGLJPrice(glj);
  1523. this.refreshTreeNodePriceIfNeed(glj);
  1524. }
  1525. parentGlj.push(glj);
  1526. }
  1527. }
  1528. }
  1529. return parentGlj;
  1530. }
  1531. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  1532. switch (glj.unit_price.type + '') {
  1533. // 人工: 调整基价=基价单价*调整系数
  1534. case GLJTypeConst.LABOUR:
  1535. case GLJTypeConst.MACHINE_LABOUR:
  1536. glj.adjust_price = this.getAdjustPrice(glj);
  1537. break;
  1538. // 机械类型的算法
  1539. case GLJTypeConst.MACHINE:
  1540. console.log('机械');
  1541. break;
  1542. // 材料、主材、设备
  1543. default:
  1544. glj.adjust_price = glj.unit_price.base_price;
  1545. }
  1546. }
  1547. ProjectGLJ.prototype.getAdjustPrice = function (glj,isRadio) {
  1548. let proGLJ = projectObj.project.projectGLJ;
  1549. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1550. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1551. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1552. return gljUtil.getAdjustPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1553. };
  1554. ProjectGLJ.prototype.getBasePrice = function(glj,isRadio){
  1555. let proGLJ = projectObj.project.projectGLJ;
  1556. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1557. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1558. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1559. return gljUtil.getBasePrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1560. };
  1561. ProjectGLJ.prototype.getMarketPrice = function (glj,isRadio) {
  1562. let proGLJ = projectObj.project.projectGLJ;
  1563. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1564. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1565. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1566. return gljUtil.getMarketPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil);
  1567. };
  1568. ProjectGLJ.prototype.getTenderMarketPrice = function (glj,isRadio) {
  1569. let proGLJ = projectObj.project.projectGLJ;
  1570. let calcOptions=projectObj.project.projectInfo.property.calcOptions;
  1571. let decimalObj = projectObj.project.projectInfo.property.decimal;
  1572. let labourCoeDatas = projectObj.project.labourCoe.datas;
  1573. let tenderCoe = this.getTenderPriceCoe(glj);
  1574. return gljUtil.getMarketPrice(glj,proGLJ.datas,calcOptions,labourCoeDatas,decimalObj,isRadio,_,scMathUtil,tenderCoe);
  1575. };
  1576. ProjectGLJ.prototype.getTenderPriceCoe = function(glj,tproperty){
  1577. return gljUtil.getTenderPriceCoe(glj,tproperty);
  1578. };
  1579. ProjectGLJ.prototype.isEstimateType = function(type){
  1580. let typeString = type + "";
  1581. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料、主材、设备类型才显示是否暂估
  1582. return type;
  1583. }
  1584. return false;
  1585. };
  1586. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  1587. let gljTypeMap = this.datas.constData.gljTypeMap;
  1588. return gljTypeMap["typeId" + ID]?gljTypeMap["typeId" + ID].shortName:'';
  1589. };
  1590. ProjectGLJ.prototype.calcQuantity = function (init=false){
  1591. let project_gljs = this.datas.gljList;
  1592. let changeArray=[];
  1593. let rationGLJDatas = projectObj.project.ration_glj.datas;
  1594. let rationDatas = projectObj.project.Ration.datas;
  1595. let billsDatas = projectObj.project.Bills.datas;
  1596. gljUtil.calcProjectGLJQuantity(this.datas,rationGLJDatas,rationDatas,billsDatas,getDecimal("glj.quantity"),_,scMathUtil);
  1597. if(init == true || this.quantityChangeMap == null){//如果是初始化,建立一个映射表
  1598. this.quantityChangeMap = {};
  1599. for(let pglj of project_gljs){
  1600. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1601. }
  1602. }else if(this.quantityChangeMap != null){
  1603. for(let pglj of project_gljs){
  1604. if(this.quantityChangeMap[pglj.id] != undefined|| this.quantityChangeMap[pglj.id] != null){
  1605. if(this.quantityChangeMap[pglj.id] != pglj.quantity){
  1606. changeArray.push(pglj);
  1607. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1608. }
  1609. }else { //映射表没有,说明是新添加的项目工料机
  1610. changeArray.push(pglj);
  1611. this.quantityChangeMap[pglj.id] = pglj.quantity;
  1612. }
  1613. }
  1614. }
  1615. changeArray.length > 0 && projectGljObject.calcPartASupplyFeeByProjectGLJs ?projectGljObject.calcPartASupplyFeeByProjectGLJs(changeArray):'';
  1616. };
  1617. ProjectGLJ.prototype.calcTenderQuantity = function (){
  1618. let rationGLJDatas = projectObj.project.ration_glj.datas;
  1619. let rationDatas = projectObj.project.Ration.datas;
  1620. let billsDatas = projectObj.project.Bills.datas;
  1621. gljUtil.calcProjectGLJQuantity(this.datas,rationGLJDatas,rationDatas,billsDatas,getDecimal("glj.quantity"),_,scMathUtil,true);
  1622. };
  1623. ProjectGLJ.prototype.loadNewProjectGLJToCaches = function (datas,calquantity = false) {
  1624. if(!datas) return;
  1625. let gljIDMap = {};
  1626. for (let d of datas){
  1627. if(d) this.loadNewProjectGLJToCache(d,gljIDMap);
  1628. }
  1629. if(datas.length > 0 && calquantity) this.calcQuantity();
  1630. };
  1631. ProjectGLJ.prototype.loadNewProjectGLJToCache = function (data,tIDMap) {//把新插入的项目工料机数据增加至缓存中
  1632. if(!data) return;
  1633. let project_gljs = this.datas.gljList;
  1634. let unitPriceMap = this.datas.unitPriceMap;
  1635. let mixRatioMap = this.datas.mixRatioMap;
  1636. let mixRatioConnectData = this.datas.mixRatioConnectData;
  1637. let IDMap = !_.isEmpty(tIDMap)?tIDMap:_.indexBy(project_gljs,'id');
  1638. let tem = IDMap[data.id];
  1639. if(tem) return tem; //判断该工料机是否已经存在,是的话不用再次添加
  1640. //查看是否有组成物,有组成物的话先添加组成物信息
  1641. data.ratio_data=[];
  1642. if(data.subList && data.subList.length){
  1643. for(let s of data.subList){
  1644. let ratio = s.ratio_data;
  1645. data.ratio_data.push(ratio);
  1646. mixRatioMap[ratio.connect_key]?mixRatioMap[ratio.connect_key].push(ratio):mixRatioMap[ratio.connect_key] = [ratio];
  1647. let rIndex = gljUtil.getIndex(ratio);
  1648. mixRatioConnectData[rIndex]?mixRatioConnectData[rIndex].push(ratio.connect_key):mixRatioConnectData[rIndex] = [ratio.connect_key];
  1649. this.loadNewProjectGLJToCache(s,IDMap);
  1650. }
  1651. delete data.subList;
  1652. }
  1653. //添加unitPriceMap
  1654. let uIndex = gljUtil.getIndex(data.unit_price);
  1655. if(!unitPriceMap[uIndex]) unitPriceMap[uIndex] = data.unit_price;
  1656. this.datas.gljList.push(data);
  1657. this.datas.gljMap[gljUtil.getIndex(data)]= data;
  1658. IDMap[data.id] = data;
  1659. return data;
  1660. };
  1661. class DivideSetting {
  1662. constructor (project) {
  1663. this.project = project;
  1664. this.datas = [];
  1665. project.registerModule(ModuleNames.divide_setting, this);
  1666. };
  1667. loadData (datas) {
  1668. this.datas = datas;
  1669. };
  1670. }
  1671. class EvaluateList {
  1672. constructor (project) {
  1673. this.project = project;
  1674. this.datas = [];
  1675. project.registerModule(ModuleNames.evaluate_list, this);
  1676. };
  1677. loadData (datas) {
  1678. this.datas = datas;
  1679. };
  1680. }
  1681. class BidEvaluationList {
  1682. constructor (project) {
  1683. this.project = project;
  1684. this.datas = [];
  1685. project.registerModule(ModuleNames.bid_evaluation_list, this);
  1686. };
  1687. loadData (datas) {
  1688. this.datas = datas;
  1689. };
  1690. }