project_glj.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. } 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.calculate(node);
  199. projectObj.project.calcProgram.saveNode(node);
  200. $.bootstrapLoading.end();
  201. });//重新加载项目工料机数据
  202. //上面两步都是异步操作,这句应该是要等上面两步做完了再执行的
  203. }, function (err) {
  204. $.bootstrapLoading.end();
  205. });
  206. }
  207. ProjectGLJ.prototype.updatePrice = function (recode, updateField, newval,from) {
  208. let me = this;
  209. let projectGljs = this.datas.gljList;
  210. let pgljID = from=="rg"?recode.projectGLJID:recode.id;//和定额工料机统一接口,项目工料机ID取值不一样
  211. let glj = _.find(projectGljs, {'id': pgljID});
  212. if (glj) {
  213. let data = {id: glj.unit_price.id, field: updateField, newval: newval};
  214. let callback = function (data) {
  215. if (updateField == 'base_price') {
  216. glj.unit_price.base_price = newval;
  217. me.setAdjustPrice(glj);
  218. } else {
  219. glj.unit_price.market_price = newval;
  220. }
  221. //更新回传的父节点项目工料机价格
  222. let gljs = me.getProjectGLJs(data);
  223. me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  224. projectObj.project.projectGLJ.loadCacheData();//更新工料机汇总缓存和显示
  225. gljOprObj.showRationGLJSheetData();
  226. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  227. gljs.push(glj);
  228. let nodes = me.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  229. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  230. $.bootstrapLoading.end();
  231. }
  232. $.bootstrapLoading.start();
  233. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  234. $.bootstrapLoading.end();
  235. });
  236. } else {
  237. gljOprObj.showRationGLJSheetData();
  238. }
  239. }
  240. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  241. if ((data.unit_price.type == gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  242. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  243. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  244. tem.data.marketUnitFee = data.unit_price.market_price;
  245. return true;
  246. }
  247. })
  248. projectObj.mainController.refreshTreeNode(nodes);
  249. }
  250. }
  251. //根据工料机,取得所有受影响的定额节点
  252. ProjectGLJ.prototype.getImpactRationNodes = function (gljs) {
  253. let nodes = [];
  254. let rationMap = {};
  255. let idArray = _.map(gljs,'id');
  256. let priceArray = _.map(gljs,'unit_price');
  257. //先根据项目工料机ID,找到受影响定额的ID
  258. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  259. for (let rg of ration_glj_list) {
  260. if (_.indexOf(idArray,rg.projectGLJID)!=-1) {
  261. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  262. }
  263. }
  264. for (let item of projectObj.project.mainTree.items) {
  265. if (item.sourceType == ModuleNames.ration) {
  266. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  267. let idx = _.indexOf(idArray,item.data.projectGLJID);
  268. if (idx != -1) {
  269. item.data.marketUnitFee = priceArray[idx].market_price; //更新市场单价
  270. nodes.push(item);
  271. }
  272. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  273. nodes.push(item)
  274. }
  275. }
  276. }
  277. return nodes;
  278. };
  279. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  280. for (let ration_glj of gljOprObj.sheetData) {
  281. if (ration_glj.projectGLJID == glj.id) {
  282. ration_glj.basePrice = glj.unit_price.base_price;
  283. ration_glj.marketPrice = glj.unit_price.market_price;
  284. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  285. }
  286. }
  287. }
  288. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  289. }
  290. ProjectGLJ.prototype.getProjectGLJs = function (data,refreshPrice=true) {
  291. let parentGlj = [];
  292. //
  293. let projectGljs = this.datas.gljList;
  294. let indexList = gljKeyArray;
  295. for (let d of data) {
  296. if (d) {
  297. let condition = {};
  298. for (let index of indexList) {
  299. if (d[index] != null && d[index] != undefined && d[index] != '') {
  300. condition[index] = d[index]
  301. }
  302. }
  303. let glj = _.find(projectGljs, condition);
  304. if (glj) {
  305. if(refreshPrice==true){
  306. glj.unit_price.base_price = d.base_price;
  307. glj.unit_price.market_price = d.market_price;
  308. this.setAdjustPrice(glj);
  309. this.refreshRationGLJPrice(glj);
  310. this.refreshTreeNodePriceIfNeed(glj);
  311. }
  312. parentGlj.push(glj);
  313. }
  314. }
  315. }
  316. return parentGlj;
  317. }
  318. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  319. switch (glj.unit_price.type + '') {
  320. // 人工: 调整基价=基价单价*调整系数
  321. case GLJTypeConst.LABOUR:
  322. case GLJTypeConst.MACHINE_LABOUR:
  323. glj.adjust_price = this.getAdjustPrice(glj);
  324. break;
  325. // 机械类型的算法
  326. case GLJTypeConst.MACHINE:
  327. console.log('机械');
  328. break;
  329. // 材料、主材、设备
  330. default:
  331. glj.adjust_price = glj.unit_price.base_price;
  332. }
  333. }
  334. ProjectGLJ.prototype.getAdjustPrice = function (glj) {
  335. GLJTypeConst = this.datas.constData.GLJTypeConst !== undefined ? JSON.parse(this.datas.constData.GLJTypeConst) : GLJTypeConst;
  336. let decimal = getDecimal("glj.unitPrice");
  337. let quantity_decimal = getDecimal("glj.quantity")
  338. if (glj.unit_price.type == GLJTypeConst.LABOUR || glj.unit_price.type == GLJTypeConst.MACHINE_LABOUR) {//人工、机上人工,调整价根据定额价*调整系数计算得出。
  339. let labour = projectObj.project.calcProgram.compiledLabourCoes[glj.adjCoe];
  340. //let labour=1;
  341. let coe = labour && labour.coe ? labour.coe : 1;
  342. return scMathUtil.roundTo(parseFloat(coe * scMathUtil.roundForObj(glj.unit_price.base_price,decimal)), -decimal);
  343. } else if (notEditType.indexOf(glj.unit_price.type)>0) {//对于混凝土、配合比、砂浆、机械台班,调整价根据组成物计算得出。
  344. let p =0;
  345. for(let ratio of glj.ratio_data){
  346. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  347. 'code': ratio.code,
  348. 'name': ratio.name,
  349. 'specs':ratio.specs,
  350. 'type': ratio.type,
  351. 'unit': ratio.unit
  352. })
  353. if(tem){
  354. p+=scMathUtil.roundForObj(this.getAdjustPrice(tem)*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),decimal);
  355. }
  356. }
  357. return scMathUtil.roundForObj(p,decimal);
  358. } else {//对于其他普通材料等,无调整系数,调整价=定额价。
  359. return glj.unit_price.base_price
  360. }
  361. }
  362. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  363. let gljTypeMap = this.datas.constData.gljTypeMap;
  364. return gljTypeMap["typeId" + ID].shortName;
  365. }