project_glj_spread.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**
  2. * 项目工料机相关spread
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/19
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @return {void}
  12. */
  13. function ProjectGLJSpread() {
  14. this.isChanging = false;
  15. this.sheetObj = null;
  16. this.firstMachineRow = -1;
  17. this.firstMixRatioRow = -1;
  18. this.successCallback = null;
  19. this.supplyType = ['自行采购', '部分甲供', '完全甲供', '甲定乙供'];
  20. // 工料机类型是混凝土、砂浆、配合比、机械(不包括机械组成物)时,供货方式列只读。
  21. this.supplyReadonlyType = [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO, GLJTypeConst.GENERAL_MACHINE];
  22. }
  23. /**
  24. * 初始化
  25. *
  26. * @return {object}
  27. */
  28. ProjectGLJSpread.prototype.init = function () {
  29. // 供货方式类型
  30. let supplySelect = [];
  31. for(let index in this.supplyType) {
  32. supplySelect.push({
  33. text: this.supplyType[index],
  34. value: index
  35. });
  36. }
  37. let selectBox = new GC.Spread.Sheets.CellTypes.ComboBox();
  38. selectBox.items(supplySelect);
  39. selectBox.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  40. let header = [
  41. {name: '编码', field: 'code', visible: true},
  42. {name: '名称', field: 'name', visible: true},
  43. {name: '规格型号', field: 'specs', visible: true},
  44. {name: '单位', field: 'unit', visible: true},
  45. {name: '类型', field: 'unit_price.short_name', visible: true},
  46. {name: 'ID', field: 'id', visible: false},
  47. {name: '类型', field: 'unit_price.type', visible: false},
  48. {name: '总消耗量', field: 'quantity', visible: true},
  49. {name: '基价单价', field: "unit_price.base_price", visible: true},
  50. {name: '调整基价', field: 'adjust_price', visible: true},
  51. {name: '市场单价', field: "unit_price.market_price", visible: true, validator: 'number'},
  52. {
  53. name: '是否暂估',
  54. field: 'is_evaluate',
  55. visible: true,
  56. cellType: new GC.Spread.Sheets.CellTypes.CheckBox(),
  57. validator: 'boolean'
  58. },
  59. {name: '供货方式', field: 'supply', visible: true, cellType: selectBox},
  60. {name: '甲供数量', field: 'supply_quantity', visible: true},
  61. {name: '交货方式', field: 'delivery', visible: true},
  62. {name: '送达地点', field: 'delivery_address', visible: true},
  63. {
  64. name: '不调价',
  65. field: 'is_adjust_price',
  66. visible: true,
  67. cellType: new GC.Spread.Sheets.CellTypes.CheckBox(),
  68. validator: 'boolean'
  69. },
  70. {name: 'UID', field: 'unit_price.id', visible: false},
  71. {name: '工料机ID', field: 'glj_id', visible: false},
  72. {name: '组成物消耗量', field: 'consumption', visible: false},
  73. {name: '父级关联编码', field: 'connect_code', visible: false},
  74. {name: '消耗量', field: 'ratio_data', visible: false},
  75. ];
  76. let sourceData = jsonData;
  77. this.sheetObj = new CommonSpreadJs(header);
  78. this.sheetObj.init('project-glj');
  79. // 获取列号
  80. let isEvaluateColumn = this.sheetObj.getFieldColumn('is_evaluate');
  81. let isAdjustPriceColumn = this.sheetObj.getFieldColumn('is_adjust_price');
  82. let unitColumn = this.sheetObj.getFieldColumn('unit');
  83. let quantityColumn = this.sheetObj.getFieldColumn('quantity');
  84. let basePriceColumn = this.sheetObj.getFieldColumn('unit_price.base_price');
  85. let adjustPriceColumn = this.sheetObj.getFieldColumn('adjust_price');
  86. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  87. let supplyColumn = this.sheetObj.getFieldColumn('supply');
  88. let shortNameColumn = this.sheetObj.getFieldColumn('unit_price.short_name');
  89. // 居中样式
  90. let centerStyleSetting = {hAlign: 1};
  91. this.sheetObj.setStyle(-1, isEvaluateColumn, centerStyleSetting);
  92. this.sheetObj.setStyle(-1, isAdjustPriceColumn, centerStyleSetting);
  93. this.sheetObj.setStyle(-1, unitColumn, centerStyleSetting);
  94. this.sheetObj.setStyle(-1, shortNameColumn, centerStyleSetting);
  95. // 向右对齐样式
  96. let rightStyleSetting = {hAlign: GC.Spread.Sheets.HorizontalAlign.right};
  97. this.sheetObj.setStyle(-1, quantityColumn, rightStyleSetting);
  98. this.sheetObj.setStyle(-1, basePriceColumn, rightStyleSetting);
  99. this.sheetObj.setStyle(-1, adjustPriceColumn, rightStyleSetting);
  100. this.sheetObj.setStyle(-1, marketPriceColumn, rightStyleSetting);
  101. // 设置可编辑列
  102. this.sheetObj.setColumnEditable(marketPriceColumn);
  103. this.sheetObj.setColumnEditable(isEvaluateColumn);
  104. this.sheetObj.setColumnEditable(isAdjustPriceColumn);
  105. this.sheetObj.setColumnEditable(supplyColumn);
  106. this.sheetObj.setData(sourceData);
  107. // 取消正在加载字符提示
  108. $("#project-glj > p").hide();
  109. this.specialColumn(sourceData);
  110. // 绑定修改事件
  111. let self = this;
  112. this.sheetObj.bind(GC.Spread.Sheets.Events.ValueChanged, function(element, info) {
  113. self.updateProjectGLJField(info, self.successCallback);
  114. });
  115. // 绑定双击事件
  116. this.sheetObj.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (element, info) {
  117. let column = info.col;
  118. let row = info.row;
  119. let field = self.sheetObj.getColumnField(column);
  120. let activeSheet = self.sheetObj.getSheet();
  121. // 获取类型
  122. let typeColumn = self.sheetObj.getFieldColumn('unit_price.type');
  123. let type = activeSheet.getValue(row, typeColumn);
  124. // 如果类型为混凝土、砂浆、配合比、机械,则提示
  125. if (field === 'unit_price.market_price' && canNotChangeTypeId.indexOf(type) >= 0) {
  126. alert('当前工料机的市场单价由组成物计算得出,不可直接修改');
  127. }
  128. });
  129. return this.sheetObj;
  130. };
  131. /**
  132. * 更新项目工料机中的数据字段
  133. *
  134. * @param {object} info
  135. * @param {function} callback
  136. * @return {void|boolean}
  137. */
  138. ProjectGLJSpread.prototype.updateProjectGLJField = function(info, callback) {
  139. // 获取修改的数据
  140. let column = info.col;
  141. let row = info.row;
  142. let idString = 'id';
  143. let field = projectGLJSheet.getColumnField(column);
  144. if (field === '') {
  145. return false;
  146. }
  147. let activeSheet = projectGLJSheet.getSheet();
  148. // 切割字段
  149. let fieldArray = field.split('.');
  150. idString = fieldArray.length > 1 ? 'unit_price.id' : idString;
  151. // 防止快速同时提交
  152. if (isChanging) {
  153. return false;
  154. }
  155. // 校验数据
  156. let value = info.newValue;
  157. if (!projectGLJSheet.checkData(column, value)) {
  158. alert('数据格式错误,请重新输入!');
  159. activeSheet.setValue(row, column, info.oldValue);
  160. return false;
  161. }
  162. // 获取id
  163. let idColumn = projectGLJSheet.getFieldColumn(idString);
  164. if (idColumn < 0) {
  165. return false;
  166. }
  167. let id = activeSheet.getValue(row, idColumn);
  168. // 直接在前端计算后传值后台改
  169. let extend = {};
  170. let parentMarketPrice = projectGLJSpread.compositionCalculate(row);
  171. if (parentMarketPrice !== null && Object.keys(parentMarketPrice).length > 0) {
  172. for (let activeCode in parentMarketPrice) {
  173. let tmpObject = {
  174. market_price: parentMarketPrice[activeCode],
  175. };
  176. extend[activeCode] = tmpObject;
  177. }
  178. }
  179. // 如果是供货方式则需要处理数据
  180. if (field === 'supply') {
  181. value = this.supplyType.indexOf(value);
  182. extend.supply_quantity = this.getSupplyQuantity(value, activeSheet, info);
  183. }
  184. extend = Object.keys(extend).length > 0 ? JSON.stringify(extend) : '';
  185. $.ajax({
  186. url: '/glj/update',
  187. type: 'post',
  188. data: {id: id, field: field, value: value, extend: extend},
  189. dataType: 'json',
  190. error: function() {
  191. alert('数据传输有误!');
  192. isChanging = false;
  193. },
  194. beforeSend: function() {
  195. isChanging = true;
  196. },
  197. success: function(response) {
  198. isChanging = false;
  199. // 修改失败则恢复原值
  200. if (response.err !== 0) {
  201. activeSheet.setValue(row, column, info.oldValue);
  202. alert('更改数据失败!');
  203. } else {
  204. // 成功则触发相应事件
  205. if (parentMarketPrice !== null) {
  206. info.parentMarketPrice = parentMarketPrice;
  207. }
  208. callback(field, info);
  209. }
  210. }
  211. });
  212. };
  213. /**
  214. * 设置特殊单元格数据
  215. *
  216. * @param {object} sourceData
  217. * @return {void}
  218. */
  219. ProjectGLJSpread.prototype.specialColumn = function (sourceData) {
  220. let rowCounter = 0;
  221. // 获取列号
  222. let isEvaluateColumn = this.sheetObj.getFieldColumn('is_evaluate');
  223. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  224. let connectCodeColumn = this.sheetObj.getFieldColumn('connect_code');
  225. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  226. let supplyColumn = this.sheetObj.getFieldColumn('supply');
  227. let supplyQuantity = this.sheetObj.getFieldColumn('supply_quantity');
  228. let activeSheet = this.sheetObj.getSheet();
  229. for (let data of sourceData) {
  230. // 只有材料才显示是否暂估
  231. if (materialIdList.indexOf(data.unit_price.type) < 0) {
  232. let string = new GC.Spread.Sheets.CellTypes.Text();
  233. activeSheet.setCellType(rowCounter, isEvaluateColumn, string, GC.Spread.Sheets.SheetArea.viewport);
  234. // 锁定该单元格
  235. activeSheet.getCell(rowCounter, isEvaluateColumn, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  236. activeSheet.setValue(rowCounter, isEvaluateColumn, '');
  237. }
  238. // 设置供货方式列是否可选
  239. if (this.supplyReadonlyType.indexOf(data.unit_price.type) >= 0) {
  240. // 锁定该单元格
  241. activeSheet.getCell(rowCounter, supplyColumn, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  242. }
  243. // 如果为部分甲供或者为全部甲供则甲供数量需要可编辑
  244. if (data.supply === 1 || data.supply === 2) {
  245. activeSheet.getCell(rowCounter, supplyQuantity, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  246. }
  247. // 供货方式数据
  248. let supplyIndex = parseInt(data.supply);
  249. supplyIndex = isNaN(supplyIndex) ? 0 : supplyIndex;
  250. let supplyText = this.supplyType[supplyIndex] !== undefined ? this.supplyType[supplyIndex] : '自行采购';
  251. activeSheet.setValue(rowCounter, supplyColumn, supplyText);
  252. // 如果类型为混凝土、砂浆、配合比、机械,则市场单价和供货方式不能修改
  253. if (canNotChangeTypeId.indexOf(data.unit_price.type) >= 0) {
  254. this.firstMixRatioRow = this.firstMixRatioRow === -1 && data.unit_price.type !== GLJTypeConst.GENERAL_MACHINE ?
  255. rowCounter : this.firstMixRatioRow;
  256. this.firstMachineRow = this.firstMachineRow === -1 && data.unit_price.type === GLJTypeConst.GENERAL_MACHINE ?
  257. rowCounter : this.firstMachineRow;
  258. // 锁定该单元格
  259. activeSheet.getCell(rowCounter, marketPriceColumn, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  260. activeSheet.getCell(rowCounter, supplyColumn, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  261. }
  262. // 处理数据
  263. if (data.ratio_data !== undefined && data.ratio_data.length > 0) {
  264. let connectCode = [];
  265. let consumption = [];
  266. for (let tmp of data.ratio_data) {
  267. connectCode.push(tmp.connect_code);
  268. consumption.push(tmp.consumption);
  269. }
  270. let connectCodeString = connectCode.join(',');
  271. let consumptionString = consumption.join(',');
  272. activeSheet.setValue(rowCounter, connectCodeColumn, connectCodeString);
  273. activeSheet.setValue(rowCounter, consumptionColumn, consumptionString);
  274. }
  275. rowCounter++;
  276. }
  277. };
  278. /**
  279. * 计算当前行对应组成物的市场以及基价单价价格
  280. *
  281. * @param {Number} row
  282. * @return {Object}
  283. */
  284. ProjectGLJSpread.prototype.compositionCalculate = function(row) {
  285. let activeSheet = this.sheetObj.getSheet();
  286. // 获取相关列号
  287. let connectCodeColumn = this.sheetObj.getFieldColumn('connect_code');
  288. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  289. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  290. let activeConnectCode = activeSheet.getValue(row, connectCodeColumn);
  291. // 不属于组成物则忽略
  292. if (activeConnectCode === '' || activeConnectCode === null) {
  293. return null;
  294. }
  295. activeConnectCode = activeConnectCode.split(',');
  296. // 计算同级组成物的价格
  297. // 遍历所有记录
  298. let maxRow = activeSheet.getRowCount();
  299. let parentMarketPrice = {};
  300. for (let i = 0; i < maxRow; i++) {
  301. let connectCode = activeSheet.getValue(i, connectCodeColumn);
  302. if (connectCode === null) {
  303. continue;
  304. }
  305. connectCode = connectCode.split(',');
  306. // 消耗量
  307. let consumption = activeSheet.getValue(i, consumptionColumn);
  308. consumption = consumption.split(',');
  309. // 获取市场价
  310. let marketPrice = activeSheet.getValue(i, marketPriceColumn);
  311. for (let active of activeConnectCode) {
  312. let index = connectCode.indexOf(active);
  313. if (index < 0) {
  314. continue;
  315. }
  316. let rowConsumption = consumption[index] === undefined ? 0 : consumption[index];
  317. // 计算价格
  318. let rowMarketPrice = (marketPrice * rowConsumption).toDecimal(2);
  319. parentMarketPrice[active] = parentMarketPrice[active] === undefined ?
  320. rowMarketPrice : (parentMarketPrice[active] + rowMarketPrice).toDecimal(2);
  321. }
  322. }
  323. return parentMarketPrice;
  324. };
  325. /**
  326. * 组成物父类数据更新
  327. *
  328. * @param {Object} parentMarketPrice
  329. * @return {void}
  330. */
  331. ProjectGLJSpread.prototype.compositionParentUpdate = function(parentMarketPrice) {
  332. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  333. let activeSheet = this.sheetObj.getSheet();
  334. // 定位到父节点,然后更新
  335. for (let code in parentMarketPrice) {
  336. let changeRow = this.sheetObj.searchKeyword(code);
  337. activeSheet.setValue(changeRow, marketPriceColumn, parentMarketPrice[code]);
  338. }
  339. };
  340. /**
  341. * 价格计算
  342. *
  343. * @param {Object} info
  344. * @return {void}
  345. */
  346. ProjectGLJSpread.prototype.priceCalculate = function(info) {
  347. let row = info.row;
  348. let typeColumn = this.sheetObj.getFieldColumn('unit_price.type');
  349. let basePriceColumn = this.sheetObj.getFieldColumn('unit_price.base_price');
  350. let adjustPriceColumn = projectGLJSheet.getFieldColumn('adjust_price');
  351. let activeSheet = this.sheetObj.getSheet();
  352. // 获取类型
  353. let type = activeSheet.getValue(row, typeColumn);
  354. // 基价单价计算
  355. switch (type) {
  356. // 主材、设备自动赋值基价单价=市场单价
  357. case GLJTypeConst.MAIN_MATERIAL:
  358. case GLJTypeConst.EQUIPMENT:
  359. activeSheet.setValue(info.row, basePriceColumn, info.newValue);
  360. break;
  361. }
  362. // 调整基价计算
  363. switch (type) {
  364. // 材料、主材、设备 调整基价=基价单价
  365. case GLJTypeConst.MAIN_MATERIAL:
  366. case GLJTypeConst.EQUIPMENT:
  367. let basePrice = activeSheet.getValue(info.row, basePriceColumn);
  368. activeSheet.setValue(info.row, adjustPriceColumn, basePrice);
  369. break;
  370. }
  371. // 市场单价计算
  372. switch (type) {
  373. // 人工、材料(普通材料)触发 需计算混凝土、砂浆、配合比、机械的市场单价
  374. case GLJTypeConst.LABOUR:
  375. case GLJTypeConst.GENERAL_MATERIAL:
  376. // 计算
  377. this.compositionParentUpdate(info.parentMarketPrice);
  378. break;
  379. }
  380. };
  381. /**
  382. * 更改供货方式
  383. *
  384. * @param {Object} info
  385. * @return {void}
  386. */
  387. ProjectGLJSpread.prototype.changeSupplyType = function(info) {
  388. let supply = info.newValue;
  389. let supplyNumber = this.supplyType.indexOf(supply) > -1 ? this.supplyType.indexOf(supply) : 0;
  390. let supplyQuantityColumn = this.sheetObj.getFieldColumn('supply_quantity');
  391. let activeSheet = this.sheetObj.getSheet();
  392. // 部分甲供时可更改甲供数量数据,其余则只读
  393. let locked = supplyNumber === 1 ? false : true;
  394. activeSheet.getCell(info.row, supplyQuantityColumn, GC.Spread.Sheets.SheetArea.viewport).locked(locked);
  395. let supplyQuantity = this.getSupplyQuantity(supplyNumber, activeSheet, info);
  396. activeSheet.setValue(info.row, supplyQuantityColumn, supplyQuantity);
  397. };
  398. /**
  399. * 根据供货方式获取甲供数量
  400. *
  401. * @param {Number} supplyType
  402. * @param {Object} activeSheet
  403. * @param {Object} info
  404. * @return {Number}
  405. */
  406. ProjectGLJSpread.prototype.getSupplyQuantity = function(supplyType, activeSheet, info) {
  407. let quantityColumn = this.sheetObj.getFieldColumn('quantity');
  408. // 获取总消耗量
  409. let quantity = activeSheet.getValue(info.row, quantityColumn);
  410. // 自行采购和甲定乙供则把甲供数量设置为0,其余情况则设置为当前总消耗量
  411. let supplyQuantity = supplyType === 0 || supplyType === 3 ? 0 : quantity;
  412. return supplyQuantity;
  413. };