project_glj.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // 回调函数
  44. if (callback !== null) {
  45. callback(response.data);
  46. }
  47. // 存入缓存
  48. projectObj.project.projectGLJ = self;
  49. }
  50. });
  51. };
  52. ProjectGLJ.prototype.loadToCache = function (data) {
  53. this.datas = data;
  54. projectObj.project.projectGLJ = this;
  55. }
  56. /**
  57. * 获取对应工料机数据
  58. *
  59. * @param {String} code
  60. * @return {Object}
  61. */
  62. ProjectGLJ.prototype.getDataByCode = function (code) {
  63. let result = {};
  64. if (this.datas === null) {
  65. return result;
  66. }
  67. let gljList = this.datas.gljList;
  68. if (gljList === undefined) {
  69. return result;
  70. }
  71. for (let tmp of gljList) {
  72. if (tmp.code === code) {
  73. result = tmp;
  74. break;
  75. }
  76. }
  77. return result;
  78. };
  79. /**
  80. * 修改工料机数据
  81. *
  82. * @param {Number} id
  83. * @param {Object} data
  84. * @return {boolean}
  85. */
  86. ProjectGLJ.prototype.updateData = function (id, data) {
  87. let result = false;
  88. if (this.datas === null) {
  89. return result;
  90. }
  91. let gljList = this.datas.gljList;
  92. if (gljList === undefined) {
  93. return result;
  94. }
  95. // 查找对应的index
  96. let index = -1;
  97. for (let tmp in gljList) {
  98. if (gljList[tmp].id === id) {
  99. index = tmp;
  100. break;
  101. }
  102. }
  103. if (index < 0) {
  104. return result;
  105. }
  106. // 修改数据
  107. for (let tmpIndex in data) {
  108. if (tmpIndex.indexOf('_price') >= 0) {
  109. // 修改unit_price中的对象
  110. this.datas.gljList[index]['unit_price'][tmpIndex] = data[tmpIndex];
  111. } else {
  112. this.datas.gljList[index][tmpIndex] = data[tmpIndex];
  113. }
  114. }
  115. };
  116. /**
  117. * 加载缓存数据到spread
  118. *
  119. * @return {void}
  120. */
  121. ProjectGLJ.prototype.loadCacheData = function (resort) {
  122. // 加载工料机数据
  123. let data = this.datas === null ? null : this.datas;
  124. if (data === null) {
  125. return;
  126. }
  127. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  128. jsonData = filterProjectGLJ(jsonData);
  129. jsonData = sortProjectGLJ(jsonData);
  130. if(projectGLJSheet&&projectGLJSpread){
  131. projectGLJSheet.setData(jsonData);
  132. projectGLJSpread.specialColumn(jsonData);
  133. }
  134. };
  135. ProjectGLJ.prototype.updatePriceFromRG = function (recode, updateField, newval) {
  136. if (updateField == 'marketPrice') {
  137. this.updatePrice(recode, "market_price", newval,"rg");
  138. }
  139. if (updateField == 'basePrice') {
  140. this.updatePrice(recode, "base_price", newval,"rg");
  141. }
  142. };
  143. ProjectGLJ.prototype.updatePropertyFromMainSpread = function (node, updateField, newval) {
  144. if (updateField == "contain") {//更新含量和工程量时,要走定额更新的逻辑
  145. projectObj.project.Ration.updateContain(newval,node);
  146. }if(updateField == "quantity"){
  147. projectObj.project.quantity_detail.editMainTreeNodeQuantity(newval,node,updateField);
  148. } else {
  149. this.updateGLJProperty(node, updateField, newval);
  150. }
  151. };
  152. ProjectGLJ.prototype.updateGLJProperty = function (node, updateField, newval) {
  153. let rationTypeGLJ = node.data;
  154. let postData = {};
  155. if (rationTypeGLJ[updateField] == newval) {
  156. return;
  157. }
  158. let data = {
  159. glj_id: rationTypeGLJ.GLJID,
  160. project_id: rationTypeGLJ.projectID,
  161. code: rationTypeGLJ.code,
  162. original_code: rationTypeGLJ.original_code,
  163. name: rationTypeGLJ.name,
  164. shortName: rationTypeGLJ.shortName,
  165. specs: rationTypeGLJ.specs,
  166. unit: rationTypeGLJ.unit,
  167. type: rationTypeGLJ.subType,
  168. type_of_work: rationTypeGLJ.subType,
  169. base_price: rationTypeGLJ.basePrice,
  170. market_price: rationTypeGLJ.basePrice,
  171. repositoryId: rationTypeGLJ.repositoryId,
  172. adjCoe: rationTypeGLJ.adjCoe,
  173. from: rationTypeGLJ.from ? rationTypeGLJ.from : 'std'//std:标准工料机库, cpt:补充工料机库
  174. };
  175. if (updateField == 'subType') {
  176. data.type = newval;
  177. data.type_of_work = newval;
  178. data.shortName = this.getShortNameByID(newval);
  179. } else {
  180. data[updateField] = newval;
  181. }
  182. postData.ration = {
  183. ID: rationTypeGLJ.ID,
  184. projectID: rationTypeGLJ.projectID
  185. };
  186. postData.updateData = data;
  187. $.bootstrapLoading.start();
  188. CommonAjax.post("/glj/modifyKeyValue", postData, function (result) {
  189. console.log(result); //更新节点信息
  190. rationTypeGLJ[updateField] = newval;
  191. rationTypeGLJ.projectGLJID = result.id;
  192. rationTypeGLJ.code = result.code;
  193. rationTypeGLJ.basePrice = result.unit_price.base_price;
  194. rationTypeGLJ.marketUnitFee = result.unit_price.market_price;
  195. rationTypeGLJ.isAdd = result.unit_price.is_add;
  196. rationTypeGLJ.isEstimate = result.is_evaluate;
  197. rationTypeGLJ.shortName = result.unit_price.short_name;
  198. //触发计算并更新节点信息
  199. node.changed = true;
  200. projectObj.project.projectGLJ.loadData(function () {
  201. projectObj.project.calcProgram.calculate(node);
  202. projectObj.project.calcProgram.saveNode(node);
  203. $.bootstrapLoading.end();
  204. });//重新加载项目工料机数据
  205. //上面两步都是异步操作,这句应该是要等上面两步做完了再执行的
  206. }, function (err) {
  207. $.bootstrapLoading.end();
  208. });
  209. }
  210. ProjectGLJ.prototype.updatePrice = function (recode, updateField, newval,from) {
  211. let me = this;
  212. let projectGljs = this.datas.gljList;
  213. let pgljID = from=="rg"?recode.projectGLJID:recode.id;//和定额工料机统一接口,项目工料机ID取值不一样
  214. let glj = _.find(projectGljs, {'id': pgljID});
  215. if (glj) {
  216. let data = {id: glj.unit_price.id, field: updateField, newval: newval};
  217. let callback = function (data) {
  218. if (updateField == 'base_price') {
  219. glj.unit_price.base_price = newval;
  220. me.setAdjustPrice(glj);
  221. } else {
  222. glj.unit_price.market_price = newval;
  223. }
  224. //更新回传的父节点项目工料机价格
  225. let gljs = me.getProjectGLJs(data);
  226. me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  227. projectObj.project.projectGLJ.loadCacheData();//更新工料机汇总缓存和显示
  228. gljOprObj.showRationGLJSheetData();
  229. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  230. gljs.push(glj);
  231. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  232. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  233. $.bootstrapLoading.end();
  234. }
  235. $.bootstrapLoading.start();
  236. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  237. $.bootstrapLoading.end();
  238. });
  239. } else {
  240. gljOprObj.showRationGLJSheetData();
  241. }
  242. }
  243. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  244. if ((data.unit_price.type == gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  245. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  246. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  247. tem.data.marketUnitFee = data.unit_price.market_price;
  248. return true;
  249. }
  250. })
  251. projectObj.mainController.refreshTreeNode(nodes);
  252. }
  253. }
  254. //根据工料机,取得所有受影响的定额节点
  255. ProjectGLJ.prototype.getImpactRationNodes = function (gljs) {
  256. let nodes = [];
  257. let rationMap = {};
  258. let idArray = _.map(gljs,'id');
  259. let priceArray = _.map(gljs,'unit_price');
  260. //先根据项目工料机ID,找到受影响定额的ID
  261. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  262. for (let rg of ration_glj_list) {
  263. if (_.indexOf(idArray,rg.projectGLJID)!=-1) {
  264. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  265. }
  266. }
  267. for (let item of projectObj.project.mainTree.items) {
  268. if (item.sourceType == ModuleNames.ration) {
  269. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  270. let idx = _.indexOf(idArray,item.data.projectGLJID);
  271. if (idx != -1) {
  272. item.data.marketUnitFee = priceArray[idx].market_price; //更新市场单价
  273. nodes.push(item);
  274. }
  275. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  276. nodes.push(item)
  277. }
  278. }
  279. }
  280. return nodes;
  281. };
  282. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  283. for (let ration_glj of gljOprObj.sheetData) {
  284. if (ration_glj.projectGLJID == glj.id) {
  285. ration_glj.basePrice = glj.unit_price.base_price;
  286. ration_glj.marketPrice = glj.unit_price.market_price;
  287. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  288. }
  289. }
  290. }
  291. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  292. }
  293. ProjectGLJ.prototype.getProjectGLJs = function (data,refreshPrice=true) {
  294. let parentGlj = [];
  295. //
  296. let projectGljs = this.datas.gljList;
  297. let indexList = gljKeyArray;
  298. for (let d of data) {
  299. if (d) {
  300. let condition = {};
  301. for (let index of indexList) {
  302. if (d[index] != null && d[index] != undefined && d[index] != '') {
  303. condition[index] = d[index]
  304. }
  305. }
  306. let glj = _.find(projectGljs, condition);
  307. if (glj) {
  308. if(refreshPrice==true){
  309. glj.unit_price.base_price = d.base_price;
  310. glj.unit_price.market_price = d.market_price;
  311. this.setAdjustPrice(glj);
  312. this.refreshRationGLJPrice(glj);
  313. this.refreshTreeNodePriceIfNeed(glj);
  314. }
  315. parentGlj.push(glj);
  316. }
  317. }
  318. }
  319. return parentGlj;
  320. }
  321. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  322. switch (glj.unit_price.type + '') {
  323. // 人工: 调整基价=基价单价*调整系数
  324. case GLJTypeConst.LABOUR:
  325. case GLJTypeConst.MACHINE_LABOUR:
  326. glj.adjust_price = this.getAdjustPrice(glj);
  327. break;
  328. // 机械类型的算法
  329. case GLJTypeConst.MACHINE:
  330. console.log('机械');
  331. break;
  332. // 材料、主材、设备
  333. default:
  334. glj.adjust_price = glj.unit_price.base_price;
  335. }
  336. }
  337. ProjectGLJ.prototype.getAdjustPrice = function (glj) {
  338. GLJTypeConst = this.datas.constData.GLJTypeConst !== undefined ? JSON.parse(this.datas.constData.GLJTypeConst) : GLJTypeConst;
  339. let decimal = getDecimal("glj.unitPrice");
  340. let quantity_decimal = getDecimal("glj.quantity")
  341. if (glj.unit_price.type == GLJTypeConst.LABOUR || glj.unit_price.type == GLJTypeConst.MACHINE_LABOUR) {//人工、机上人工,调整价根据定额价*调整系数计算得出。
  342. let labour = projectObj.project.calcProgram.compiledLabourCoes[glj.adjCoe];
  343. //let labour=1;
  344. let coe = labour && labour.coe ? labour.coe : 1;
  345. return scMathUtil.roundTo(parseFloat(coe * scMathUtil.roundForObj(glj.unit_price.base_price,decimal)), -decimal);
  346. } else if (notEditType.indexOf(glj.unit_price.type)>0) {//对于混凝土、配合比、砂浆、机械台班,调整价根据组成物计算得出。
  347. let p =0;
  348. for(let ratio of glj.ratio_data){
  349. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  350. 'code': ratio.code,
  351. 'name': ratio.name,
  352. 'specs':ratio.specs,
  353. 'type': ratio.type,
  354. 'unit': ratio.unit
  355. })
  356. if(tem){
  357. p+=scMathUtil.roundForObj(this.getAdjustPrice(tem)*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),decimal);
  358. }
  359. }
  360. return scMathUtil.roundForObj(p,decimal);
  361. } else {//对于其他普通材料等,无调整系数,调整价=定额价。
  362. return glj.unit_price.base_price
  363. }
  364. }
  365. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  366. let gljTypeMap = this.datas.constData.gljTypeMap;
  367. return gljTypeMap["typeId" + ID].shortName;
  368. }