project_glj.js 17 KB

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