project_glj.js 19 KB

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