project_glj.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /**
  53. * 获取对应工料机数据
  54. *
  55. * @param {String} code
  56. * @return {Object}
  57. */
  58. ProjectGLJ.prototype.getDataByCode = function (code) {
  59. let result = {};
  60. if (this.datas === null) {
  61. return result;
  62. }
  63. let gljList = this.datas.gljList;
  64. if (gljList === undefined) {
  65. return result;
  66. }
  67. for(let tmp of gljList) {
  68. if (tmp.code === code) {
  69. result = tmp;
  70. break;
  71. }
  72. }
  73. return result;
  74. };
  75. /**
  76. * 修改工料机数据
  77. *
  78. * @param {Number} id
  79. * @param {Object} data
  80. * @return {boolean}
  81. */
  82. ProjectGLJ.prototype.updateData = function(id, data) {
  83. let result = false;
  84. if (this.datas === null) {
  85. return result;
  86. }
  87. let gljList = this.datas.gljList;
  88. if (gljList === undefined) {
  89. return result;
  90. }
  91. // 查找对应的index
  92. let index = -1;
  93. for(let tmp in gljList) {
  94. if (gljList[tmp].id === id) {
  95. index = tmp;
  96. break;
  97. }
  98. }
  99. if (index < 0) {
  100. return result;
  101. }
  102. // 修改数据
  103. for(let tmpIndex in data) {
  104. if(tmpIndex.indexOf('_price') >= 0) {
  105. // 修改unit_price中的对象
  106. this.datas.gljList[index]['unit_price'][tmpIndex] = data[tmpIndex];
  107. } else {
  108. this.datas.gljList[index][tmpIndex] = data[tmpIndex];
  109. }
  110. }
  111. };
  112. /**
  113. * 加载缓存数据到spread
  114. *
  115. * @return {void}
  116. */
  117. ProjectGLJ.prototype.loadCacheData = function() {
  118. // 加载工料机数据
  119. let data = this.datas === null ? null : this.datas;
  120. if (data === null) {
  121. return;
  122. }
  123. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  124. jsonData=filterProjectGLJ(jsonData);
  125. projectGLJSheet.setData(jsonData);
  126. projectGLJSpread.specialColumn(jsonData);
  127. };
  128. ProjectGLJ.prototype.updatePriceFromRG=function(recode,updateField,newval){
  129. if(updateField=='marketPrice'){
  130. this.updateBasePriceFromRG(recode,"market_price",newval);
  131. }
  132. if(updateField=='basePrice'){
  133. this.updateBasePriceFromRG(recode,"base_price",newval);
  134. }
  135. };
  136. ProjectGLJ.prototype.updateBasePriceFromRG=function(recode,updateField,newval){
  137. let me = this;
  138. let projectGljs = this.datas.gljList;
  139. let glj = _.find(projectGljs,{'id':recode.projectGLJID});
  140. if(glj){
  141. let data = {id:glj.unit_price.id,field:updateField,newval:newval};
  142. let callback =function (data) {
  143. if(updateField=='base_price'){
  144. glj.unit_price.base_price=newval;
  145. me.setAdjustPrice(glj);
  146. }else {
  147. glj.unit_price.market_price=newval;
  148. }
  149. //更新项目工料机价格
  150. me.refreshProjectGLJPrice(data);
  151. me.refreshRationGLJPrice(glj);
  152. gljOprObj.showRationGLJSheetData();
  153. $.bootstrapLoading.end();
  154. }
  155. $.bootstrapLoading.start();
  156. CommonAjax.post("/glj/updatePrice",data,callback,function (err) {
  157. $.bootstrapLoading.end();
  158. });
  159. }else {
  160. gljOprObj.showRationGLJSheetData();
  161. }
  162. }
  163. ProjectGLJ.prototype.refreshRationGLJPrice=function (glj) {
  164. for(let ration_glj of gljOprObj.sheetData){
  165. if(ration_glj.projectGLJID ==glj.id){
  166. ration_glj.basePrice=glj.unit_price.base_price;
  167. ration_glj.marketPrice=glj.unit_price.market_price;
  168. ration_glj.adjustPrice=glj.adjust_price;
  169. }
  170. }
  171. }
  172. ProjectGLJ.prototype.refreshProjectGLJPrice=function(data){
  173. let projectGljs = this.datas.gljList;
  174. let indexList = ['code','name','specs','unit','type'];
  175. for(let d of data){
  176. if(d){
  177. let condition = {};
  178. for(let index of indexList){
  179. if(d[index]!=null&&d[index]!=undefined&&d[index]!=''){
  180. condition[index]=d[index]
  181. }
  182. }
  183. let glj = _.find(projectGljs,condition);
  184. if(glj){
  185. glj.unit_price.base_price = d.base_price;
  186. glj.unit_price.market_price = d.market_price;
  187. this.setAdjustPrice(glj);
  188. this.refreshRationGLJPrice(glj);
  189. }
  190. }
  191. }
  192. }
  193. ProjectGLJ.prototype.setAdjustPrice=function(glj){
  194. switch (glj.unit_price.type + '') {
  195. // 人工: 调整基价=基价单价*调整系数
  196. case GLJTypeConst.LABOUR:
  197. glj.adjust_price = scMathUtil.roundTo(parseFloat(glj.adjustment * glj.unit_price.base_price), -2);
  198. break;
  199. // 机械类型的算法
  200. case GLJTypeConst.MACHINE:
  201. console.log('机械');
  202. break;
  203. // 材料、主材、设备
  204. default:
  205. glj.adjust_price = glj.unit_price.base_price;
  206. }
  207. }