mix_ratio.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * 配合比相关
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/10
  6. * @version
  7. */
  8. let mixRatioHeader = [];
  9. let mixRatioSheet = null;
  10. let mixRatioSpread = null;
  11. let currentTag = '';
  12. $(document).ready(function() {
  13. initMixRatioExcel();
  14. // 切换tab触发refresh
  15. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  16. currentTag = $(e.target).data('name');
  17. if (currentTag === "mix-ratio" && mixRatioSpread !== null) {
  18. // 筛选数据显示(显示混凝土、砂浆、配合比)
  19. filterDataByType([GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO]);
  20. // 获取工料机当前选中的行号
  21. let projectGLJId = getActiveProjectGLJId();
  22. // 获取数据
  23. getMixRatioData(projectGLJId);
  24. }
  25. });
  26. });
  27. /**
  28. * 初始化excel
  29. *
  30. * @return {void}
  31. */
  32. function initMixRatioExcel() {
  33. mixRatioHeader = [
  34. {name: '编码', field: 'code', visible: true},
  35. {name: '名称', field: 'name', visible: true},
  36. {name: '单位', field: 'unit_price.unit', visible: true},
  37. {name: 'ID', field: 'id', visible: false},
  38. {name: '类型', field: 'unit_price.type', visible: false},
  39. {name: '基价单价', field: "unit_price.base_price", visible: true},
  40. {name: '调整基价', field: 'adjust_price', visible: true},
  41. {name: '市场单价', field: "unit_price.market_price", visible: true},
  42. {name: '消耗量', field: 'consumption', visible: true, validator: 'number'},
  43. {name: 'CID', field: 'mix_ratio_id', visible: false},
  44. ];
  45. let setting = {
  46. header: []
  47. };
  48. let columnInfo = [];
  49. for(let tmp of mixRatioHeader) {
  50. setting.header.push({headerName: tmp.name, headerWidth: 120});
  51. columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120});
  52. }
  53. mixRatioSpread = sheetCommonObj.buildSheet(document.getElementById("mix-ratio"), setting, 3);
  54. mixRatioSpread.options.scrollbarShowMax = true;
  55. mixRatioSpread.options.scrollbarMaxAlign = true;
  56. mixRatioSpread.options.showHorizontalScrollbar = true;
  57. mixRatioSheet = mixRatioSpread.getActiveSheet();
  58. // 编码列号
  59. let codeColumn = getFieldColumn(mixRatioHeader, 'code');
  60. // 单位列号
  61. let unitColumn = getFieldColumn(mixRatioHeader, 'unit_price.unit');
  62. // 消耗量列号
  63. let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
  64. // 居中样式
  65. let centerStyleSetting = {hAlign: 1};
  66. mixRatioSheet.setStyle(-1, codeColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  67. mixRatioSheet.setStyle(-1, unitColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  68. // 设置表单不可编辑
  69. mixRatioSheet.options.isProtected = true;
  70. // 设置可编辑列
  71. mixRatioSheet.getRange(-1, consumptionColumn, -1, 1).locked(false);
  72. // 绑定数据格式
  73. mixRatioSheet.autoGenerateColumns = false;
  74. mixRatioSheet.bindColumns(columnInfo);
  75. // 绑定事件
  76. mixRatioSheet.bind(GC.Spread.Sheets.Events.ValueChanged, updateConsumption);
  77. }
  78. /**
  79. * 根据项目工料机id获取对应的配合比数据
  80. *
  81. * @param {Number} projectGLJid
  82. * @return {void}
  83. */
  84. function getMixRatioData(projectGLJid) {
  85. $.ajax({
  86. url: '/glj/get-ratio',
  87. type: 'post',
  88. data: {id: projectGLJid, project_id: 1},
  89. error: function() {
  90. alert('服务器有误');
  91. },
  92. beforeSend: function() {
  93. },
  94. success: function(response) {
  95. if (response.err === 0) {
  96. response.data = JSON.parse(response.data);
  97. // 设置数据
  98. mixRatioSheet.setDataSource(response.data);
  99. mixRatioSpread.refresh();
  100. } else {
  101. alert('获取失败');
  102. }
  103. }
  104. });
  105. }
  106. /**
  107. * 更新消耗量
  108. *
  109. * @param {object} element
  110. * @param {object} info
  111. * @return {void|boolean}
  112. */
  113. function updateConsumption(element, info) {
  114. // 获取修改的数据
  115. let column = info.col;
  116. let row = info.row;
  117. let field = mixRatioHeader[column] !== undefined && mixRatioHeader[column].field !== undefined ?
  118. mixRatioHeader[column].field : '';
  119. if (field === '') {
  120. return false;
  121. }
  122. // 防止快速同时提交
  123. if (isChanging) {
  124. return false;
  125. }
  126. // 校验数据
  127. let validator = mixRatioHeader[column].validator !== undefined ? mixRatioHeader[column].validator : null;
  128. let value = info.newValue;
  129. if (validator && !checkData(validator, value)) {
  130. alert('数据格式错误,请重新输入!');
  131. mixRatioSheet.setValue(row, column, info.oldValue);
  132. return false;
  133. }
  134. // 获取id
  135. let idColumn = getFieldColumn(mixRatioHeader, 'mix_ratio_id');
  136. if (idColumn < 0) {
  137. return false;
  138. }
  139. let id = mixRatioSheet.getValue(row, idColumn);
  140. // 计算父级3个价格
  141. let maxRow = mixRatioSheet.getRowCount();
  142. // 获取对应列的列号
  143. let marketPriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.market_price');
  144. let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
  145. let basePriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.base_price');
  146. let parentMarketPrice = 0;
  147. let parentBasePrice = 0;
  148. for(let i = 0; i < maxRow; i++) {
  149. // 获取市场单价
  150. let marketPrice = mixRatioSheet.getValue(i, marketPriceColumn);
  151. // 获取基价单价
  152. let basePrice = mixRatioSheet.getValue(i, basePriceColumn);
  153. // 获取消耗量(如果是当前修改的行则替换数据)
  154. let consumption = row === i ? info.newValue : mixRatioSheet.getValue(i, consumptionColumn);
  155. parentMarketPrice += consumption * marketPrice;
  156. parentBasePrice += consumption * basePrice;
  157. }
  158. parentMarketPrice = Number(parentMarketPrice.toFixed(2));
  159. parentBasePrice = Number(parentBasePrice.toFixed(2));
  160. $.ajax({
  161. url: '/glj/update',
  162. type: 'post',
  163. data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},
  164. dataType: 'json',
  165. error: function() {
  166. alert('数据传输有误!');
  167. isChanging = false;
  168. mixRatioSheet.setValue(row, column, info.oldValue);
  169. },
  170. beforeSend: function() {
  171. isChanging = true;
  172. },
  173. success: function(response) {
  174. isChanging = false;
  175. // 修改失败则恢复原值
  176. if (response.err !== 0) {
  177. mixRatioSheet.setValue(row, column, info.oldValue);
  178. alert('更改数据失败!');
  179. } else {
  180. mixRatioSuccess(field, info);
  181. }
  182. }
  183. });
  184. }
  185. /**
  186. * 成功事件
  187. *
  188. * @param {string} field
  189. * @param {object} info
  190. * @return {void}
  191. */
  192. function mixRatioSuccess(field, info) {
  193. // 成功则对相应的总消耗量进行设置
  194. let activeGLJRow = gljSheet.getActiveRowIndex();
  195. let change = Number((info.newValue - info.oldValue).toFixed(2));
  196. setGLJCellByField(activeGLJRow, 'quantity', change, true);
  197. // 计算父级3个价格
  198. let maxRow = mixRatioSheet.getRowCount();
  199. // 获取对应列的列号
  200. let marketPriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.market_price');
  201. let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
  202. let basePriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.base_price');
  203. let parentMarketPrice = 0;
  204. let parentBasePrice = 0;
  205. for(let i = 0; i < maxRow; i++) {
  206. // 获取市场单价
  207. let marketPrice = mixRatioSheet.getValue(i, marketPriceColumn);
  208. // 获取基价单价
  209. let basePrice = mixRatioSheet.getValue(i, basePriceColumn);
  210. // 获取消耗量
  211. let consumption = mixRatioSheet.getValue(i, consumptionColumn);
  212. parentMarketPrice += consumption * marketPrice;
  213. parentBasePrice += consumption * basePrice;
  214. }
  215. parentMarketPrice = Number(parentMarketPrice.toFixed(2));
  216. parentBasePrice = Number(parentBasePrice.toFixed(2));
  217. setGLJCellByField(activeGLJRow, 'unit_price.market_price', parentMarketPrice, false);
  218. setGLJCellByField(activeGLJRow, 'unit_price.base_price', parentBasePrice, false);
  219. setGLJCellByField(activeGLJRow, 'adjust_price', parentBasePrice, false);
  220. }