123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /**
- * 配合比相关
- *
- * @author CaiAoLin
- * @date 2017/7/10
- * @version
- */
- let mixRatioHeader = [];
- let mixRatioSheet = null;
- let mixRatioSpread = null;
- let currentTag = '';
- $(document).ready(function() {
- initMixRatioExcel();
- // 切换tab触发refresh
- $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
- currentTag = $(e.target).data('name');
- if (currentTag === "mix-ratio" && mixRatioSpread !== null) {
- // 筛选数据显示(显示混凝土、砂浆、配合比)
- filterDataByType([GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO]);
- // 获取工料机当前选中的行号
- let projectGLJId = getActiveProjectGLJId();
- // 获取数据
- getMixRatioData(projectGLJId);
- }
- });
- });
- /**
- * 初始化excel
- *
- * @return {void}
- */
- function initMixRatioExcel() {
- mixRatioHeader = [
- {name: '编码', field: 'code', visible: true},
- {name: '名称', field: 'name', visible: true},
- {name: '单位', field: 'unit_price.unit', visible: true},
- {name: 'ID', field: 'id', visible: false},
- {name: '类型', field: 'unit_price.type', visible: false},
- {name: '基价单价', field: "unit_price.base_price", visible: true},
- {name: '调整基价', field: 'adjust_price', visible: true},
- {name: '市场单价', field: "unit_price.market_price", visible: true},
- {name: '消耗量', field: 'consumption', visible: true, validator: 'number'},
- {name: 'CID', field: 'mix_ratio_id', visible: false},
- ];
- let setting = {
- header: []
- };
- let columnInfo = [];
- for(let tmp of mixRatioHeader) {
- setting.header.push({headerName: tmp.name, headerWidth: 120});
- columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120});
- }
- mixRatioSpread = sheetCommonObj.buildSheet(document.getElementById("mix-ratio"), setting, 3);
- mixRatioSpread.options.scrollbarShowMax = true;
- mixRatioSpread.options.scrollbarMaxAlign = true;
- mixRatioSpread.options.showHorizontalScrollbar = true;
- mixRatioSheet = mixRatioSpread.getActiveSheet();
- // 编码列号
- let codeColumn = getFieldColumn(mixRatioHeader, 'code');
- // 单位列号
- let unitColumn = getFieldColumn(mixRatioHeader, 'unit_price.unit');
- // 消耗量列号
- let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
- // 居中样式
- let centerStyleSetting = {hAlign: 1};
- mixRatioSheet.setStyle(-1, codeColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
- mixRatioSheet.setStyle(-1, unitColumn, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
- // 设置表单不可编辑
- mixRatioSheet.options.isProtected = true;
- // 设置可编辑列
- mixRatioSheet.getRange(-1, consumptionColumn, -1, 1).locked(false);
- // 绑定数据格式
- mixRatioSheet.autoGenerateColumns = false;
- mixRatioSheet.bindColumns(columnInfo);
- // 绑定事件
- mixRatioSheet.bind(GC.Spread.Sheets.Events.ValueChanged, updateConsumption);
- }
- /**
- * 根据项目工料机id获取对应的配合比数据
- *
- * @param {Number} projectGLJid
- * @return {void}
- */
- function getMixRatioData(projectGLJid) {
- $.ajax({
- url: '/glj/get-ratio',
- type: 'post',
- data: {id: projectGLJid, project_id: 1},
- error: function() {
- alert('服务器有误');
- },
- beforeSend: function() {
- },
- success: function(response) {
- if (response.err === 0) {
- response.data = JSON.parse(response.data);
- // 设置数据
- mixRatioSheet.setDataSource(response.data);
- mixRatioSpread.refresh();
- } else {
- alert('获取失败');
- }
- }
- });
- }
- /**
- * 更新消耗量
- *
- * @param {object} element
- * @param {object} info
- * @return {void|boolean}
- */
- function updateConsumption(element, info) {
- // 获取修改的数据
- let column = info.col;
- let row = info.row;
- let field = mixRatioHeader[column] !== undefined && mixRatioHeader[column].field !== undefined ?
- mixRatioHeader[column].field : '';
- if (field === '') {
- return false;
- }
- // 防止快速同时提交
- if (isChanging) {
- return false;
- }
- // 校验数据
- let validator = mixRatioHeader[column].validator !== undefined ? mixRatioHeader[column].validator : null;
- let value = info.newValue;
- if (validator && !checkData(validator, value)) {
- alert('数据格式错误,请重新输入!');
- mixRatioSheet.setValue(row, column, info.oldValue);
- return false;
- }
- // 获取id
- let idColumn = getFieldColumn(mixRatioHeader, 'mix_ratio_id');
- if (idColumn < 0) {
- return false;
- }
- let id = mixRatioSheet.getValue(row, idColumn);
- // 计算父级3个价格
- let maxRow = mixRatioSheet.getRowCount();
- // 获取对应列的列号
- let marketPriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.market_price');
- let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
- let basePriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.base_price');
- let parentMarketPrice = 0;
- let parentBasePrice = 0;
- for(let i = 0; i < maxRow; i++) {
- // 获取市场单价
- let marketPrice = mixRatioSheet.getValue(i, marketPriceColumn);
- // 获取基价单价
- let basePrice = mixRatioSheet.getValue(i, basePriceColumn);
- // 获取消耗量(如果是当前修改的行则替换数据)
- let consumption = row === i ? info.newValue : mixRatioSheet.getValue(i, consumptionColumn);
- parentMarketPrice += consumption * marketPrice;
- parentBasePrice += consumption * basePrice;
- }
- parentMarketPrice = Number(parentMarketPrice.toFixed(2));
- parentBasePrice = Number(parentBasePrice.toFixed(2));
- $.ajax({
- url: '/glj/update',
- type: 'post',
- data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},
- dataType: 'json',
- error: function() {
- alert('数据传输有误!');
- isChanging = false;
- mixRatioSheet.setValue(row, column, info.oldValue);
- },
- beforeSend: function() {
- isChanging = true;
- },
- success: function(response) {
- isChanging = false;
- // 修改失败则恢复原值
- if (response.err !== 0) {
- mixRatioSheet.setValue(row, column, info.oldValue);
- alert('更改数据失败!');
- } else {
- mixRatioSuccess(field, info);
- }
- }
- });
- }
- /**
- * 成功事件
- *
- * @param {string} field
- * @param {object} info
- * @return {void}
- */
- function mixRatioSuccess(field, info) {
- // 成功则对相应的总消耗量进行设置
- let activeGLJRow = gljSheet.getActiveRowIndex();
- let change = Number((info.newValue - info.oldValue).toFixed(2));
- setGLJCellByField(activeGLJRow, 'quantity', change, true);
- // 计算父级3个价格
- let maxRow = mixRatioSheet.getRowCount();
- // 获取对应列的列号
- let marketPriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.market_price');
- let consumptionColumn = getFieldColumn(mixRatioHeader, 'consumption');
- let basePriceColumn = getFieldColumn(mixRatioHeader, 'unit_price.base_price');
- let parentMarketPrice = 0;
- let parentBasePrice = 0;
- for(let i = 0; i < maxRow; i++) {
- // 获取市场单价
- let marketPrice = mixRatioSheet.getValue(i, marketPriceColumn);
- // 获取基价单价
- let basePrice = mixRatioSheet.getValue(i, basePriceColumn);
- // 获取消耗量
- let consumption = mixRatioSheet.getValue(i, consumptionColumn);
- parentMarketPrice += consumption * marketPrice;
- parentBasePrice += consumption * basePrice;
- }
- parentMarketPrice = Number(parentMarketPrice.toFixed(2));
- parentBasePrice = Number(parentBasePrice.toFixed(2));
- setGLJCellByField(activeGLJRow, 'unit_price.market_price', parentMarketPrice, false);
- setGLJCellByField(activeGLJRow, 'unit_price.base_price', parentBasePrice, false);
- setGLJCellByField(activeGLJRow, 'adjust_price', parentBasePrice, false);
- }
|