project_glj.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. }
  12. /**
  13. * 加载数据
  14. *
  15. * @param {function} callback
  16. * @return {boolean}
  17. */
  18. ProjectGLJ.prototype.loadData = function (callback = null) {
  19. let self = this;
  20. if (self.isLoading) {
  21. return false;
  22. }
  23. // 加载工料机数据
  24. $.ajax({
  25. url: '/glj/getData',
  26. type: 'post',
  27. dataType: 'json',
  28. data: {project_id: scUrlUtil.GetQueryString('project')},
  29. error: function () {
  30. // alert('数据传输错误');
  31. },
  32. beforeSend: function () {
  33. self.isLoading = true;
  34. },
  35. success: function (response) {
  36. self.isLoading = false;
  37. if (response.err === 1) {
  38. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '读取工料机数据失败!';
  39. alert(msg);
  40. return false;
  41. }
  42. self.datas = response.data;
  43. self.calcQuantity();
  44. // 回调函数
  45. if (callback !== null) {
  46. callback(response.data);
  47. }
  48. // 存入缓存
  49. projectObj.project.projectGLJ = self;
  50. }
  51. });
  52. };
  53. ProjectGLJ.prototype.loadToCache = function (data) {
  54. this.datas = data;
  55. projectObj.project.projectGLJ = this;
  56. }
  57. /**
  58. * 获取对应工料机数据
  59. *
  60. * @param {String} code
  61. * @return {Object}
  62. */
  63. ProjectGLJ.prototype.getDataByID = function (ID) {//根据项目工料机ID取工料机信息
  64. return _.find(this.datas.gljList, {'id': ID});
  65. };
  66. // CSL, 2018-02-08 甲供、甲定。
  67. ProjectGLJ.prototype.getGLJsBySupply = function (supplyTypeArr, gljTypeArr) {
  68. return _.filter(this.datas.gljList, function (glj) {
  69. return supplyTypeArr.includes(glj.supply) && gljTypeArr.includes(glj.type);
  70. });
  71. };
  72. /**
  73. * 修改工料机数据
  74. *
  75. * @param {Number} id
  76. * @param {Object} data
  77. * @return {boolean}
  78. */
  79. ProjectGLJ.prototype.updateData = function (id, data) {
  80. let result = false;
  81. if (this.datas === null) {
  82. return result;
  83. }
  84. let gljList = this.datas.gljList;
  85. if (gljList === undefined) {
  86. return result;
  87. }
  88. // 查找对应的index
  89. let index = -1;
  90. for (let tmp in gljList) {
  91. if (gljList[tmp].id === id) {
  92. index = tmp;
  93. break;
  94. }
  95. }
  96. if (index < 0) {
  97. return result;
  98. }
  99. // 修改数据
  100. for (let tmpIndex in data) {
  101. if (tmpIndex.indexOf('_price') >= 0) {
  102. // 修改unit_price中的对象
  103. this.datas.gljList[index]['unit_price'][tmpIndex] = data[tmpIndex];
  104. } else {
  105. this.datas.gljList[index][tmpIndex] = data[tmpIndex];
  106. }
  107. }
  108. };
  109. /**
  110. * 加载缓存数据到spread
  111. *
  112. * @return {void}
  113. */
  114. ProjectGLJ.prototype.loadCacheData = function (resort) {
  115. // 加载工料机数据
  116. let data = this.datas === null ? null : this.datas;
  117. if (data === null) {
  118. return;
  119. }
  120. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  121. console.log("filter start");
  122. jsonData = filterProjectGLJ(jsonData);
  123. console.log("filter end");
  124. jsonData = sortProjectGLJ(jsonData);
  125. console.log("sort end");
  126. if(projectGLJSheet&&projectGLJSpread){
  127. setTimeout(spreadInit, 1);
  128. /*projectGLJSheet.setData(jsonData);
  129. projectGLJSpread.specialColumn(jsonData);*/
  130. }
  131. };
  132. ProjectGLJ.prototype.updatePriceFromRG = function (recode, updateField, newval) {
  133. if (updateField == 'marketPrice') {
  134. this.updatePrice(recode, "market_price", newval,"rg");
  135. }
  136. if (updateField == 'basePrice') {
  137. this.updatePrice(recode, "base_price", newval,"rg");
  138. }
  139. };
  140. ProjectGLJ.prototype.updatePropertyFromMainSpread = function (node, updateField, newval) {
  141. if (updateField == "contain") {//更新含量和工程量时,要走定额更新的逻辑
  142. projectObj.project.Ration.updateContain(newval,node);
  143. }if(updateField == "quantity"){
  144. projectObj.project.quantity_detail.editMainTreeNodeQuantity(newval,node,updateField);
  145. } else {
  146. this.updateGLJProperty(node, updateField, newval);
  147. }
  148. };
  149. ProjectGLJ.prototype.updateGLJProperty = function (node, updateField, newval) {
  150. let rationTypeGLJ = node.data;
  151. let postData = {};
  152. if (rationTypeGLJ[updateField] == newval) {
  153. return;
  154. }
  155. let data = {
  156. glj_id: rationTypeGLJ.GLJID,
  157. project_id: rationTypeGLJ.projectID,
  158. code: rationTypeGLJ.code,
  159. original_code: rationTypeGLJ.original_code,
  160. name: rationTypeGLJ.name,
  161. shortName: rationTypeGLJ.shortName,
  162. specs: rationTypeGLJ.specs,
  163. unit: rationTypeGLJ.unit,
  164. type: rationTypeGLJ.subType,
  165. type_of_work: rationTypeGLJ.subType,
  166. base_price: rationTypeGLJ.basePrice,
  167. market_price: rationTypeGLJ.basePrice,
  168. repositoryId: rationTypeGLJ.repositoryId,
  169. adjCoe: rationTypeGLJ.adjCoe,
  170. from: rationTypeGLJ.from ? rationTypeGLJ.from : 'std'//std:标准工料机库, cpt:补充工料机库
  171. };
  172. if (updateField == 'subType') {
  173. data.type = newval;
  174. data.type_of_work = newval;
  175. data.shortName = this.getShortNameByID(newval);
  176. } else {
  177. data[updateField] = newval;
  178. }
  179. postData.ration = {
  180. ID: rationTypeGLJ.ID,
  181. projectID: rationTypeGLJ.projectID
  182. };
  183. postData.updateData = data;
  184. $.bootstrapLoading.start();
  185. CommonAjax.post("/glj/modifyKeyValue", postData, function (result) {
  186. console.log(result); //更新节点信息
  187. rationTypeGLJ[updateField] = newval;
  188. rationTypeGLJ.projectGLJID = result.id;
  189. rationTypeGLJ.code = result.code;
  190. rationTypeGLJ.basePrice = result.unit_price.base_price;
  191. rationTypeGLJ.marketUnitFee = result.unit_price.market_price;
  192. rationTypeGLJ.isAdd = result.unit_price.is_add;
  193. rationTypeGLJ.isEstimate = result.is_evaluate;
  194. rationTypeGLJ.shortName = result.unit_price.short_name;
  195. //触发计算并更新节点信息
  196. node.changed = true;
  197. projectObj.project.projectGLJ.loadData(function () {
  198. projectObj.project.calcProgram.calcAndSave(node);
  199. $.bootstrapLoading.end();
  200. });//重新加载项目工料机数据
  201. //上面两步都是异步操作,这句应该是要等上面两步做完了再执行的
  202. }, function (err) {
  203. $.bootstrapLoading.end();
  204. });
  205. }
  206. ProjectGLJ.prototype.updatePrice = function (recode, updateField, newval,from) {
  207. let me = this;
  208. let projectGljs = this.datas.gljList;
  209. let pgljID = from=="rg"?recode.projectGLJID:recode.id;//和定额工料机统一接口,项目工料机ID取值不一样
  210. let glj = _.find(projectGljs, {'id': pgljID});
  211. if (glj) {
  212. let data = {id: glj.unit_price.id, field: updateField, newval: newval,project_id:glj.project_id};
  213. let callback = function (data) {
  214. if (updateField == 'base_price') {
  215. glj.unit_price.base_price = newval;
  216. me.setAdjustPrice(glj);
  217. } else {
  218. glj.unit_price.market_price = newval;
  219. }
  220. //更新回传的父节点项目工料机价格
  221. let gljs = me.getProjectGLJs(data);
  222. me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  223. projectObj.project.projectGLJ.loadCacheData();//更新工料机汇总缓存和显示
  224. gljOprObj.showRationGLJSheetData();
  225. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  226. gljs.push(glj);
  227. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  228. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  229. socket.emit('unitFileChangeNotify', JSON.stringify(data));
  230. projectObj.project.markUpdateProject({projectID:projectObj.project.ID(),'unitFileID':socketObject.getUnitFileRoomID()},"unitFile");
  231. $.bootstrapLoading.end();
  232. }
  233. $.bootstrapLoading.start();
  234. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  235. $.bootstrapLoading.end();
  236. });
  237. } else {
  238. gljOprObj.showRationGLJSheetData();
  239. }
  240. }
  241. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  242. if ((data.unit_price.type == gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  243. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  244. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  245. tem.data.marketUnitFee = data.unit_price.market_price;
  246. return true;
  247. }
  248. })
  249. projectObj.mainController.refreshTreeNode(nodes);
  250. }
  251. }
  252. //根据工料机,取得所有受影响的定额节点
  253. ProjectGLJ.prototype.getImpactRationNodes = function (gljs) {
  254. let nodes = [];
  255. let rationMap = {};
  256. let idArray = _.map(gljs,'id');
  257. let priceArray = _.map(gljs,'unit_price');
  258. //先根据项目工料机ID,找到受影响定额的ID
  259. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  260. for (let rg of ration_glj_list) {
  261. if (_.indexOf(idArray,rg.projectGLJID)!=-1) {
  262. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  263. }
  264. }
  265. for (let item of projectObj.project.mainTree.items) {
  266. if (item.sourceType == ModuleNames.ration) {
  267. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  268. let idx = _.indexOf(idArray,item.data.projectGLJID);
  269. if (idx != -1) {
  270. item.data.marketUnitFee = priceArray[idx].market_price; //更新市场单价
  271. nodes.push(item);
  272. }
  273. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  274. nodes.push(item)
  275. }
  276. }
  277. }
  278. return nodes;
  279. };
  280. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  281. for (let ration_glj of gljOprObj.sheetData) {
  282. if (ration_glj.projectGLJID == glj.id) {
  283. ration_glj.basePrice = glj.unit_price.base_price;
  284. ration_glj.marketPrice = glj.unit_price.market_price;
  285. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  286. }
  287. }
  288. }
  289. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  290. }
  291. ProjectGLJ.prototype.getProjectGLJs = function (data,refreshPrice=true) {
  292. let parentGlj = [];
  293. //
  294. let projectGljs = this.datas.gljList;
  295. let indexList = gljKeyArray;
  296. for (let d of data) {
  297. if (d) {
  298. let condition = {};
  299. for (let index of indexList) {
  300. if (d[index] != null && d[index] != undefined && d[index] != '') {
  301. condition[index] = d[index]
  302. }
  303. }
  304. let glj = _.find(projectGljs, condition);
  305. if (glj) {
  306. if(refreshPrice==true){
  307. glj.unit_price.base_price = d.base_price;
  308. glj.unit_price.market_price = d.market_price;
  309. this.setAdjustPrice(glj);
  310. this.refreshRationGLJPrice(glj);
  311. this.refreshTreeNodePriceIfNeed(glj);
  312. }
  313. parentGlj.push(glj);
  314. }
  315. }
  316. }
  317. return parentGlj;
  318. }
  319. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  320. switch (glj.unit_price.type + '') {
  321. // 人工: 调整基价=基价单价*调整系数
  322. case GLJTypeConst.LABOUR:
  323. case GLJTypeConst.MACHINE_LABOUR:
  324. glj.adjust_price = this.getAdjustPrice(glj);
  325. break;
  326. // 机械类型的算法
  327. case GLJTypeConst.MACHINE:
  328. console.log('机械');
  329. break;
  330. // 材料、主材、设备
  331. default:
  332. glj.adjust_price = glj.unit_price.base_price;
  333. }
  334. }
  335. ProjectGLJ.prototype.getAdjustPrice = function (glj) {
  336. GLJTypeConst = this.datas.constData.GLJTypeConst !== undefined ? JSON.parse(this.datas.constData.GLJTypeConst) : GLJTypeConst;
  337. let decimal = getDecimal("glj.unitPrice");
  338. let quantity_decimal = getDecimal("glj.quantity");
  339. if (glj.unit_price.type == GLJTypeConst.LABOUR || glj.unit_price.type == GLJTypeConst.MACHINE_LABOUR) {//人工、机上人工,调整价根据定额价*调整系数计算得出。
  340. let labour = projectObj.project.calcProgram.compiledLabourCoes[glj.adjCoe];
  341. //let labour=1;
  342. let coe = labour && labour.coe ? labour.coe : 1;
  343. return scMathUtil.roundTo(parseFloat(coe * scMathUtil.roundForObj(glj.unit_price.base_price,decimal)), -decimal);
  344. } else if (notEditType.indexOf(glj.unit_price.type)>0&&glj.ratio_data.length>0) {//对于混凝土、配合比、砂浆、机械台班,调整价根据组成物计算得出。
  345. let p =0;
  346. for(let ratio of glj.ratio_data){
  347. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  348. 'code': ratio.code,
  349. 'name': ratio.name,
  350. 'specs':ratio.specs,
  351. 'type': ratio.type,
  352. 'unit': ratio.unit
  353. })
  354. if(tem){
  355. p+=scMathUtil.roundForObj(this.getAdjustPrice(tem)*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),decimal);
  356. }
  357. }
  358. return scMathUtil.roundForObj(p,decimal);
  359. } else {//对于其他普通材料等,无调整系数,调整价=定额价。
  360. return glj.unit_price.base_price
  361. }
  362. };
  363. ProjectGLJ.prototype.getBasePrice = function(glj){
  364. let price_decimal = getDecimal("glj.unitPrice");
  365. let quantity_decimal = getDecimal("glj.quantity");
  366. if (notEditType.indexOf(glj.unit_price.type)>0&&glj.ratio_data.length>0) {//对于混凝土、配合比、砂浆、机械台班等有组成物的材料,价格需根据组成物计算得出。
  367. let p =0;
  368. for(let ratio of glj.ratio_data){
  369. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  370. 'code': ratio.code,
  371. 'name': ratio.name,
  372. 'specs':ratio.specs,
  373. 'type': ratio.type,
  374. 'unit': ratio.unit
  375. });
  376. if(tem){
  377. p+=scMathUtil.roundForObj(tem.unit_price.base_price*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),price_decimal);
  378. }
  379. }
  380. return scMathUtil.roundForObj(p,price_decimal);
  381. }else {
  382. return scMathUtil.roundForObj(glj.unit_price.base_price,price_decimal);
  383. }
  384. };
  385. ProjectGLJ.prototype.getMarketPrice = function (glj) {
  386. let price_decimal = getDecimal("glj.unitPrice");
  387. let quantity_decimal = getDecimal("glj.quantity");
  388. if (notEditType.indexOf(glj.unit_price.type)>0&&glj.ratio_data.length>0) {//对于混凝土、配合比、砂浆、机械台班等有组成物的材料,价格需根据组成物计算得出。
  389. let p =0;
  390. for(let ratio of glj.ratio_data){
  391. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  392. 'code': ratio.code,
  393. 'name': ratio.name,
  394. 'specs':ratio.specs,
  395. 'type': ratio.type,
  396. 'unit': ratio.unit
  397. });
  398. if(tem){
  399. p+=scMathUtil.roundForObj(tem.unit_price.market_price*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),price_decimal);
  400. }
  401. }
  402. return scMathUtil.roundForObj(p,price_decimal);
  403. }else {
  404. return scMathUtil.roundForObj(glj.unit_price.market_price,price_decimal);
  405. }
  406. }
  407. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  408. let gljTypeMap = this.datas.constData.gljTypeMap;
  409. return gljTypeMap["typeId" + ID].shortName;
  410. };
  411. ProjectGLJ.prototype.calcQuantity = function (){
  412. let project_gljs = this.datas.gljList;
  413. let mixRatioConnectData = this.datas.mixRatioConnectData;
  414. let mixRatioSubdivisionMap = {};
  415. let mixRatioTechMap={};
  416. for(let pglj of project_gljs ){
  417. if(pglj.quantity !== 0 && pglj.quantity !== '0'){
  418. let result = this.getQuantityPerGLJ(pglj,mixRatioSubdivisionMap,mixRatioTechMap);
  419. pglj.subdivisionQuantity = result.subdivisionQuantity;
  420. pglj.techQuantity = result.techQuantity;
  421. }
  422. }
  423. //计算做为组成物的消耗量
  424. for(let pg of project_gljs ){
  425. if(pg.quantity !== 0 && pg.quantity !== '0'){
  426. let pg_index = gljOprObj.getIndex(pg,gljKeyArray);
  427. if(mixRatioConnectData[pg_index]){
  428. if(mixRatioSubdivisionMap[pg_index]){
  429. pg.subdivisionQuantity = scMathUtil.roundForObj(mixRatioSubdivisionMap[pg_index]+pg.subdivisionQuantity,getDecimal("glj.quantity"));
  430. }
  431. if(mixRatioTechMap[pg_index]){
  432. pg.techQuantity = scMathUtil.roundForObj(mixRatioTechMap[pg_index]+pg.techQuantity,getDecimal("glj.quantity"));
  433. }
  434. }
  435. }
  436. }
  437. }
  438. ProjectGLJ.prototype.getQuantityPerGLJ =function (pglj,mixRatioSubdivisionMap,mixRatioTechMap) {
  439. let billIDs = projectObj.project.Bills.getSubdivisionProjectLeavesID();//取分部分项上的所有叶子清单ID
  440. let tech_billIDS = projectObj.project.Bills.getTechLeavesID();//取所有技术措施项目叶子清单ID
  441. let ration_glj_list = projectObj.project.ration_glj.datas;
  442. let mixRatioMap = this.datas.mixRatioMap;
  443. let rations = projectObj.project.Ration.datas;
  444. let q_decimal = getDecimal("glj.quantity");
  445. let result={};
  446. let sum = 0;
  447. let tech_sum = 0;
  448. for(let rg of ration_glj_list){
  449. if(rg.projectGLJID==pglj.id){
  450. if(_.includes(billIDs,rg.billsItemID)){//计算分部分项
  451. let total = calcQuantity(rg,mixRatioSubdivisionMap);
  452. sum = scMathUtil.roundForObj(sum+total,q_decimal);
  453. }
  454. if(_.includes(tech_billIDS,rg.billsItemID)){//计算技术措施项目消耗量
  455. let tech_total = calcQuantity(rg,mixRatioTechMap);
  456. tech_sum = scMathUtil.roundForObj(tech_sum+tech_total,q_decimal);
  457. }
  458. }
  459. }
  460. for(let ra of rations){//计算定额类型工料机的消耗量
  461. if(ra.type == rationType.gljRation&&ra.projectGLJID===pglj.id){
  462. let r_quantity = scMathUtil.roundForObj(ra.quantity,q_decimal);
  463. r_quantity = r_quantity?r_quantity:0;
  464. if(_.includes(billIDs,ra.billsItemID)){//计算分部分项
  465. sum = scMathUtil.roundForObj(sum+r_quantity,q_decimal);
  466. }
  467. if(_.includes(tech_billIDS,ra.billsItemID)){//计算技术措施项目消耗量
  468. tech_sum = scMathUtil.roundForObj(tech_sum+r_quantity,q_decimal);
  469. }
  470. }
  471. }
  472. result.subdivisionQuantity = sum;
  473. result.techQuantity = tech_sum;
  474. return result;
  475. function calcQuantity(rg,quantityMap) { //计算工料机在所属定额下的总消耗量
  476. let tem_ration = _.find(rations,{"ID":rg.rationID});
  477. let total = parseFloat(gljOprObj.getTotalQuantity(rg,tem_ration));
  478. let r_index = gljOprObj.getIndex(rg,gljKeyArray);
  479. if(mixRatioMap.hasOwnProperty(r_index)){
  480. let mixRatioList = mixRatioMap[r_index];
  481. for(let m of mixRatioList){
  482. let m_index = gljOprObj.getIndex(m,gljKeyArray);
  483. let m_quantity = scMathUtil.roundForObj(total*m.consumption,q_decimal);
  484. if(quantityMap[m_index]){
  485. quantityMap[m_index] = scMathUtil.roundForObj(quantityMap[m_index]+m_quantity,q_decimal);
  486. }else {
  487. quantityMap[m_index] = m_quantity;
  488. }
  489. }
  490. }
  491. return total;
  492. }
  493. }