project_glj.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 () {
  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. projectGLJSheet.setData(jsonData);
  131. projectGLJSpread.specialColumn(jsonData);
  132. };
  133. ProjectGLJ.prototype.updatePriceFromRG = function (recode, updateField, newval) {
  134. if (updateField == 'marketPrice') {
  135. this.updateBasePriceFromRG(recode, "market_price", newval);
  136. }
  137. if (updateField == 'basePrice') {
  138. this.updateBasePriceFromRG(recode, "base_price", newval);
  139. }
  140. };
  141. ProjectGLJ.prototype.updatePropertyFromMainSpread = function (node, updateField, newval) {
  142. if (updateField == "contain") {
  143. } else {
  144. this.updateGLJProperty(node, updateField, newval);
  145. }
  146. };
  147. ProjectGLJ.prototype.updateGLJProperty = function (node, updateField, newval) {
  148. let rationTypeGLJ = node.data;
  149. let postData = {};
  150. if (rationTypeGLJ[updateField] == newval) {
  151. return;
  152. }
  153. let data = {
  154. glj_id: rationTypeGLJ.GLJID,
  155. project_id: rationTypeGLJ.projectID,
  156. code: rationTypeGLJ.code,
  157. original_code: rationTypeGLJ.original_code,
  158. name: rationTypeGLJ.name,
  159. shortName: rationTypeGLJ.shortName,
  160. specs: rationTypeGLJ.specs,
  161. unit: rationTypeGLJ.unit,
  162. type: rationTypeGLJ.subType,
  163. type_of_work: rationTypeGLJ.subType,
  164. base_price: rationTypeGLJ.basePrice,
  165. market_price: rationTypeGLJ.basePrice,
  166. repositoryId: rationTypeGLJ.repositoryId,
  167. adjCoe: rationTypeGLJ.adjCoe,
  168. from: rationTypeGLJ.from ? rationTypeGLJ.from : 'std'//std:标准工料机库, cpt:补充工料机库
  169. };
  170. if (updateField == 'subType') {
  171. data.type = newval;
  172. data.type_of_work = newval;
  173. data.shortName = this.getShortNameByID(newval);
  174. } else {
  175. data[updateField] = newval;
  176. }
  177. postData.ration = {
  178. ID: rationTypeGLJ.ID,
  179. projectID: rationTypeGLJ.projectID
  180. };
  181. postData.updateData = data;
  182. $.bootstrapLoading.start();
  183. CommonAjax.post("/glj/modifyKeyValue", postData, function (result) {
  184. console.log(result); //更新节点信息
  185. rationTypeGLJ[updateField] = newval;
  186. rationTypeGLJ.projectGLJID = result.id;
  187. rationTypeGLJ.code = result.code;
  188. rationTypeGLJ.basePrice = result.unit_price.base_price;
  189. rationTypeGLJ.marketUnitFee = result.unit_price.market_price;
  190. rationTypeGLJ.isAdd = result.unit_price.is_add;
  191. rationTypeGLJ.isEstimate = result.is_evaluate;
  192. rationTypeGLJ.shortName = result.unit_price.short_name;
  193. //触发计算并更新节点信息
  194. node.changed = true;
  195. projectObj.project.projectGLJ.loadData(function () {
  196. projectObj.project.calcProgram.calculate(node);
  197. projectObj.project.calcProgram.saveNode(node);
  198. $.bootstrapLoading.end();
  199. });//重新加载项目工料机数据
  200. //上面两步都是异步操作,这句应该是要等上面两步做完了再执行的
  201. }, function (err) {
  202. $.bootstrapLoading.end();
  203. });
  204. }
  205. ProjectGLJ.prototype.updateBasePriceFromRG = function (recode, updateField, newval) {
  206. let me = this;
  207. let projectGljs = this.datas.gljList;
  208. let glj = _.find(projectGljs, {'id': recode.projectGLJID});
  209. console.log(glj);
  210. if (glj) {
  211. let data = {id: glj.unit_price.id, field: updateField, newval: newval};
  212. let callback = function (data) {
  213. if (updateField == 'base_price') {
  214. glj.unit_price.base_price = newval;
  215. me.setAdjustPrice(glj);
  216. } else {
  217. glj.unit_price.market_price = newval;
  218. }
  219. //更新项目工料机价格
  220. me.refreshProjectGLJPrice(data);
  221. me.refreshRationGLJPrice(glj);//刷新定额工料机列表的记录
  222. gljOprObj.showRationGLJSheetData();
  223. me.refreshTreeNodePriceIfNeed(glj);//刷新造价书中主树上的定额工料机;
  224. let nodes = me.getImpactRationNodes(glj);//取到因为改变工料机价格而受影响的定额
  225. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  226. $.bootstrapLoading.end();
  227. }
  228. $.bootstrapLoading.start();
  229. CommonAjax.post("/glj/updatePrice", data, callback, function (err) {
  230. $.bootstrapLoading.end();
  231. });
  232. } else {
  233. gljOprObj.showRationGLJSheetData();
  234. }
  235. }
  236. ProjectGLJ.prototype.refreshTreeNodePriceIfNeed = function (data) {
  237. if ((data.unit_price.type = gljType.MAIN_MATERIAL || data.unit_price.type == gljType.EQUIPMENT) && projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial == true) {
  238. var nodes = _.filter(projectObj.project.mainTree.items, function (tem) {
  239. if (tem.sourceType == ModuleNames.ration_glj && tem.data.projectGLJID == data.id) {
  240. tem.data.marketUnitFee = data.unit_price.market_price;
  241. return true;
  242. }
  243. })
  244. projectObj.mainController.refreshTreeNode(nodes);
  245. }
  246. }
  247. //根据工料机,取得所有受影响的定额节点
  248. ProjectGLJ.prototype.getImpactRationNodes = function (glj) {
  249. let nodes = [];
  250. let rationMap = {};
  251. //先根据项目工料机ID,找到受影响定额的ID
  252. let ration_glj_list = projectObj.project.ration_glj.datas; //取定额工料机数据
  253. for (let rg of ration_glj_list) {
  254. if (rg.projectGLJID == glj.id) {
  255. rationMap[rg.rationID] = true; //取所有定额ID,用MAP方式去重
  256. }
  257. }
  258. for (let item of projectObj.project.mainTree.items) {
  259. if (item.sourceType == ModuleNames.ration) {
  260. if (item.data.type == rationType.gljRation) {//取定额类型的工料机
  261. if (item.data.projectGLJID == glj.id) {
  262. item.data.marketUnitFee = glj.unit_price.market_price; //更新市场单价
  263. nodes.push(item);
  264. }
  265. } else if (rationMap[item.data.ID] == true) { //受影响的定额
  266. nodes.push(item)
  267. }
  268. }
  269. }
  270. return nodes;
  271. };
  272. ProjectGLJ.prototype.refreshRationGLJPrice = function (glj) {
  273. for (let ration_glj of gljOprObj.sheetData) {
  274. if (ration_glj.projectGLJID == glj.id) {
  275. ration_glj.basePrice = glj.unit_price.base_price;
  276. ration_glj.marketPrice = glj.unit_price.market_price;
  277. ration_glj.adjustPrice = this.getAdjustPrice(glj);
  278. }
  279. }
  280. }
  281. ProjectGLJ.prototype.refreshRationTypeGLJ = function (glj) {
  282. }
  283. ProjectGLJ.prototype.refreshProjectGLJPrice = function (data) {
  284. let projectGljs = this.datas.gljList;
  285. let indexList = ['code', 'name', 'specs', 'unit', 'type'];
  286. for (let d of data) {
  287. if (d) {
  288. let condition = {};
  289. for (let index of indexList) {
  290. if (d[index] != null && d[index] != undefined && d[index] != '') {
  291. condition[index] = d[index]
  292. }
  293. }
  294. let glj = _.find(projectGljs, condition);
  295. if (glj) {
  296. glj.unit_price.base_price = d.base_price;
  297. glj.unit_price.market_price = d.market_price;
  298. this.setAdjustPrice(glj);
  299. this.refreshRationGLJPrice(glj);
  300. this.refreshTreeNodePriceIfNeed(glj);
  301. }
  302. }
  303. }
  304. }
  305. ProjectGLJ.prototype.setAdjustPrice = function (glj) {
  306. switch (glj.unit_price.type + '') {
  307. // 人工: 调整基价=基价单价*调整系数
  308. case GLJTypeConst.LABOUR:
  309. case GLJTypeConst.MACHINE_LABOUR:
  310. glj.adjust_price = this.getAdjustPrice(glj);
  311. break;
  312. // 机械类型的算法
  313. case GLJTypeConst.MACHINE:
  314. console.log('机械');
  315. break;
  316. // 材料、主材、设备
  317. default:
  318. glj.adjust_price = glj.unit_price.base_price;
  319. }
  320. }
  321. ProjectGLJ.prototype.getAdjustPrice = function (glj) {
  322. GLJTypeConst = this.datas.constData.GLJTypeConst !== undefined ? JSON.parse(this.datas.constData.GLJTypeConst) : GLJTypeConst;
  323. let decimal = getDecimal("glj.unitPrice");
  324. let quantity_decimal = getDecimal("glj.quantity")
  325. if (glj.unit_price.type == GLJTypeConst.LABOUR || glj.unit_price.type == GLJTypeConst.MACHINE_LABOUR) {//人工、机上人工,调整价根据定额价*调整系数计算得出。
  326. let labour = projectObj.project.calcProgram.compiledLabourCoes[glj.adjCoe];
  327. //let labour=1;
  328. let coe = labour && labour.coe ? labour.coe : 1;
  329. return scMathUtil.roundTo(parseFloat(coe * scMathUtil.roundForObj(glj.unit_price.base_price,decimal)), -decimal);
  330. } else if (notEditType.indexOf(glj.unit_price.type)>0) {//对于混凝土、配合比、砂浆、机械台班,调整价根据组成物计算得出。
  331. let p =0;
  332. for(let ratio of glj.ratio_data){
  333. let tem = _.find( projectObj.project.projectGLJ.datas.gljList,{
  334. 'code': ratio.code,
  335. 'name': ratio.name,
  336. 'specs':ratio.specs,
  337. 'type': ratio.type,
  338. 'unit': ratio.unit
  339. })
  340. if(tem){
  341. p+=scMathUtil.roundForObj(this.getAdjustPrice(tem)*scMathUtil.roundForObj(ratio.consumption,quantity_decimal),decimal);
  342. }
  343. }
  344. return scMathUtil.roundForObj(p,decimal);
  345. } else {//对于其他普通材料等,无调整系数,调整价=定额价。
  346. return glj.unit_price.base_price
  347. }
  348. }
  349. ProjectGLJ.prototype.getShortNameByID = function (ID) {
  350. let gljTypeMap = this.datas.constData.gljTypeMap;
  351. return gljTypeMap["typeId" + ID].shortName;
  352. }