project_glj_spread.js 19 KB

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