|  | @@ -8,8 +8,35 @@
 | 
	
		
			
				|  |  |  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},
 | 
	
	
		
			
				|  | @@ -18,15 +45,18 @@ $(document).ready(function() {
 | 
	
		
			
				|  |  |          {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, validator: 'number'},
 | 
	
		
			
				|  |  | -        {name: '总消耗量', field: 'quantity', 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);
 | 
	
	
		
			
				|  | @@ -35,31 +65,183 @@ $(document).ready(function() {
 | 
	
		
			
				|  |  |      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;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    // 切换tab触发refresh
 | 
	
		
			
				|  |  | -    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
 | 
	
		
			
				|  |  | -        let tagString = $(e.target).text();
 | 
	
		
			
				|  |  | -        if (tagString === "配合比表" && mixRatioSpread !== null) {
 | 
	
		
			
				|  |  | -            // 筛选数据显示(显示混凝土、砂浆、配合比)
 | 
	
		
			
				|  |  | -            filterDataByType([GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO]);
 | 
	
		
			
				|  |  | +    // 设置可编辑列
 | 
	
		
			
				|  |  | +    mixRatioSheet.getRange(-1, consumptionColumn, -1, 1).locked(false);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            // 获取工料机当前选中的行号
 | 
	
		
			
				|  |  | -            console.log(gljSheet.getActiveRowIndex());
 | 
	
		
			
				|  |  | -            mixRatioSpread.refresh();
 | 
	
		
			
				|  |  | +    // 绑定数据格式
 | 
	
		
			
				|  |  | +    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);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  | - * 根据id获取对应的配合比数据
 | 
	
		
			
				|  |  | + * 成功事件
 | 
	
		
			
				|  |  |   *
 | 
	
		
			
				|  |  | - * @param {Number} id
 | 
	
		
			
				|  |  | + * @param {string} field
 | 
	
		
			
				|  |  | + * @param {object} info
 | 
	
		
			
				|  |  |   * @return {void}
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -function getMixRatioData(id) {
 | 
	
		
			
				|  |  | +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);
 | 
	
		
			
				|  |  |  }
 |