project_glj_spread.js 19 KB

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