'use strict'; /** * 材料调差 - 调差清单 * * @author EllisRan * @date 2019/10/25 * @version */ function getStageId() { return window.location.pathname.split('/')[5]; } function findNotJoinLeafXmj(x, type = '') { if (type === 'index') { return notJoinList.findIndex(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } return notJoinList.find(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } function findNotChangeLeafXmj(x, type = '') { if (type === 'index') { return notChangeList.findIndex(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } return notChangeList.find(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } function findSelfLeafXmj(x, type = '') { if (type === 'index') { return selfList.findIndex(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } return selfList.find(function (item) { return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id)); }); } function getMpSpreadByMBData(id) { const info = materialBillsData.find(function (item) { return item.id === parseInt(id); }); return info ? info.m_spread : 0; } function getMaterialListByLeafXmj(gcl_id, xmj_id, mx_id = '') { const list = []; const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; for (const ml of materialListData) { if (gcl_id === ml.gcl_id && xmj_id === ml.xmj_id && (mx_id === '' || mx_id === ml.mx_id) && (ms_id === null || ms_id === ml.ms_id)) { list.push(ml); } } return list; } function calcOneBQJC(xmj) { let jiacha = 0; const notx = findNotJoinLeafXmj(xmj); if (notx === undefined) { const list = xmj.mx_id !== undefined ? getMaterialListByLeafXmj(xmj.gcl_id, xmj.id, xmj.mx_id) : getMaterialListByLeafXmj(xmj.gcl_id, xmj.id); const notx2 = findNotChangeLeafXmj(xmj); if (notx2 !== undefined) { for (const l of list) { jiacha = ZhCalc.add(jiacha, ZhCalc.mul(ZhCalc.mul(xmj.contract_qty, l.quantity), getMpSpreadByMBData(l.mb_id))); } } else { for (const l of list) { jiacha = ZhCalc.add(jiacha, ZhCalc.mul(ZhCalc.mul(xmj.gather_qty, l.quantity), getMpSpreadByMBData(l.mb_id))); } } } return ZhCalc.round(jiacha, materialDecimal.tp); } function getPasteHint (str, row = '') { let returnObj = str; if (row) { returnObj.msg = '清单第' + (row+1) + '行' + (str.msg ? str.msg : str); } return returnObj; } // 重新计算列表的价差 function calculateJiaCha(data, index) { // 计算单条的 if (index) { const gcld = data[index] let total_jiacha = 0; for (const [index, xmj] of gcld.leafXmjs.entries()) { const jiacha = calcOneBQJC(xmj); gcld.leafXmjs[index].jiacha = jiacha !== 0 ? jiacha : null; total_jiacha += jiacha; } gcld.total_jiacha = ZhCalc.round(total_jiacha, materialDecimal.tp) } else { for(const gcld of data) { let total_jiacha = 0; for (const [index, xmj] of gcld.leafXmjs.entries()) { const jiacha = calcOneBQJC(xmj); gcld.leafXmjs[index].jiacha = jiacha !== 0 ? jiacha : null; total_jiacha += jiacha; } gcld.total_jiacha = ZhCalc.round(total_jiacha, materialDecimal.tp) } } } const is_numeric = (value) => { if (typeof(value) === 'object') { return false; } else { return !Number.isNaN(Number(value)) && value.toString().trim() !== ''; } }; $(document).ready(() => { function TipCellType() { } TipCellType.prototype = new GC.Spread.Sheets.CellTypes.ColumnHeader(); TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) { return { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea, sheet: context.sheet }; }; TipCellType.prototype.processMouseEnter = function (hitInfo){ if (!this._toolTipElement) { var div = document.createElement("div"); $(div).css("position", "absolute") .css("border", "1px #C0C0C0 solid") .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)") .css("font", "9pt Arial") .css("background", "#fff") // .css("color", "#fff") .css("z-index", "1000") .css("padding", 5); this._toolTipElement = div; } $(this._toolTipElement).text("单位数量:每一单位清单下所需工料消耗量。") .css("top", hitInfo.y + 15) .css("left", hitInfo.x - 15); $(this._toolTipElement).hide(); // document.body.insertBefore(this._toolTipElement, null); if (hitInfo.sheet.yn === 4) { // 用于区分上下两个工料关联表 $('#material-self-spread-div').append(this._toolTipElement, null); } else { $('#material-spread-div').append(this._toolTipElement, null); } $(this._toolTipElement).show("fast"); }; TipCellType.prototype.processMouseLeave = function (hitInfo) { if (this._toolTipElement) { // document.body.removeChild(this._toolTipElement); // $('#material-spread-div').removeChild(this._toolTipElement); this._toolTipElement.remove(); this._toolTipElement = null; } }; autoFlashHeight(); // 清单table const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]); const ledgerCols = [ {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 90, formatter: '@'}, {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 220, formatter: '@'}, {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 80, formatter: '@'}, {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 110, type: 'Number'}, ]; if (materialQtySource === 1) { ledgerCols.push({title: '本期计量数量|合同', colSpan: '3|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 110, type: 'Number'}); } else if (materialQtySource === 2) { ledgerCols.push({title: '本期计量数量|合同', colSpan: '1|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 110, type: 'Number'}); } else if (materialQtySource === 3) { ledgerCols.push({title: '本期计量数量|合同', colSpan: '4|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '|不计价', colSpan: '|1', rowSpan: '|1', field: 'qc_minus_qty', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 110, type: 'Number', getValue: 'getValue.gather_qty'}); } ledgerCols.push({title: '本期完成金额', colSpan: '1', rowSpan: '2', field: 'gather_tp', hAlign: 2, width: 110, type: 'Number'}); ledgerCols.push({title: '本期价差', colSpan: '1', rowSpan: '2', field: 'total_jiacha', hAlign:3, width: 110, type: 'Number'}); const ledgerSpreadSetting = { // cols: [ // {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 90, formatter: '@'}, // {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 220, formatter: '@'}, // {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 80, formatter: '@'}, // {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 110, type: 'Number'}, // {title: '本期计量数量|合同', colSpan: '4|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 110, type: 'Number'}, // {title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 110, type: 'Number'}, // {title: '|不计价', colSpan: '|1', rowSpan: '|1', field: 'qc_minus_qty', hAlign: 2, width: 110, type: 'Number'}, // {title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 110, type: 'Number'}, // {title: '本期完成金额', colSpan: '1', rowSpan: '2', field: 'gather_tp', hAlign: 2, width: 110, type: 'Number'}, // {title: '本期价差', colSpan: '1', rowSpan: '2', field: 'total_jiacha', hAlign:3, width: 110, type: 'Number'} // ], emptyRows: 0, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', readOnly: true, }; const ledgerCol = { getValue: { gather_qty: function (data) { if (materialQtySource === 3) { return ZhCalc.add(data.gather_qty, data.qc_minus_qty); } }, } } ledgerSpreadSetting.cols = ledgerCols; // let gclGatherData = gclGatherModel.gatherGclData() // 获取项目节数据 function loadLeafXmjData(iGclRow) { const gcl = gclGatherData[iGclRow]; if (gcl) { for (const [index, xmj] of gcl.leafXmjs.entries()) { const jiacha = calcOneBQJC(xmj); gcl.leafXmjs[index].jiacha = jiacha !== 0 ? ZhCalc.round(jiacha, materialDecimal.tp) : null; } const leafXmjs = gcl.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty }); console.log(leafXmjs); SpreadJsObj.loadSheetData(leafXmjSpread.getActiveSheet(), SpreadJsObj.DataType.Data, leafXmjs); // 对清单调差工料table的单位数量进行改变 // materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + '每' + gcl.unit + '数量 �'; // SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting); } else { SpreadJsObj.loadSheetData(leafXmjSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []); } SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting); } SpreadJsObj.initSpreadSettingEvents(ledgerSpreadSetting, ledgerCol); SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting); // 项目明细table const leafXmjSpread = SpreadJsObj.createNewSpread($('#leaf-xmj-spread')[0]); const leafXmjCols = [ {title: '项目节|编号', colSpan: '2|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@'}, {title: '|项目节名称', colSpan: '|1', rowSpan: '|1', field: 'jldy', hAlign: 0, width: 100, formatter: '@'}, {title: '计量单元|计量单元', colSpan: '2|1', rowSpan: '1|1', field: 'bwmx', hAlign: 0, width: 100, formatter: '@'}, {title: '|复核数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 0, width: 80, type: 'Number'}, {title: '部位信息|单位工程', colSpan: '3|1', rowSpan: '1|1', field: 'dwgc', hAlign: 0, width: 100, formatter: '@'}, {title: '|分部工程', colSpan: '|1', rowSpan: '|1', field: 'fbgc', hAlign: 0, width: 100, formatter: '@'}, {title: '|分项工程', colSpan: '|1', rowSpan: '|1', field: 'fxgc', hAlign: 0, width: 180, formatter: '@'}, ]; if (materialQtySource === 1) { leafXmjCols.push({title: '本期计量数量|合同', colSpan: '3|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 60, type: 'Number'}); leafXmjCols.push({title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 60, type: 'Number'}); leafXmjCols.push({title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 60, type: 'Number'}); } else if (materialQtySource === 2) { leafXmjCols.push({title: '本期计量数量|合同', colSpan: '1|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 100, type: 'Number'}); } else if (materialQtySource === 3) { leafXmjCols.push({title: '本期计量数量|合同', colSpan: '4|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 60, type: 'Number'}); leafXmjCols.push({title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 60, type: 'Number'}); leafXmjCols.push({title: '|不计价', colSpan: '|1', rowSpan: '|1', field: 'qc_minus_qty', hAlign: 2, width: 60, type: 'Number'}); leafXmjCols.push({title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 60, type: 'Number', getValue: 'getValue.gather_qty'}); } leafXmjCols.push({title: '本期价差', colSpan: '1', rowSpan: '2', field: 'jiacha', hAlign: 2, width: 80, type: 'Number'}); const leafXmjSpreadSetting = { // cols: [ // {title: '项目节|编号', colSpan: '2|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@'}, // {title: '|项目节名称', colSpan: '|1', rowSpan: '|1', field: 'jldy', hAlign: 0, width: 100, formatter: '@'}, // {title: '计量单元|计量单元', colSpan: '2|1', rowSpan: '1|1', field: 'bwmx', hAlign: 0, width: 100, formatter: '@'}, // {title: '|复核数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 0, width: 80, type: 'Number'}, // {title: '部位信息|单位工程', colSpan: '3|1', rowSpan: '1|1', field: 'dwgc', hAlign: 0, width: 100, formatter: '@'}, // {title: '|分部工程', colSpan: '|1', rowSpan: '|1', field: 'fbgc', hAlign: 0, width: 100, formatter: '@'}, // {title: '|分项工程', colSpan: '|1', rowSpan: '|1', field: 'fxgc', hAlign: 0, width: 180, formatter: '@'}, // {title: '本期计量数量|合同', colSpan: '4|1', rowSpan: '1|1', field: 'contract_qty', hAlign: 2, width: 60, type: 'Number'}, // {title: '|数量变更', colSpan: '|1', rowSpan: '|1', field: 'qc_qty', hAlign: 2, width: 80, type: 'Number'}, // {title: '|不计价', colSpan: '|1', rowSpan: '|1', field: 'qc_minus_qty', hAlign: 2, width: 80, type: 'Number'}, // {title: '|小计', colSpan: '|1', rowSpan: '|1', field: 'gather_qty', hAlign: 2, width: 60, type: 'Number'}, // {title: '本期价差', colSpan: '1', rowSpan: '2', field: 'jiacha', hAlign: 2, width: 80, type: 'Number'}, // ], emptyRows: 0, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', readOnly: true, rowHeader:[ { rowHeaderType: 'circle', setting: { size: 5, indent: 16, getColor: function (index, data) { if (!data) return; if (_.findIndex(selfList, { gcl_id: data.gcl_id, xmj_id: data.id, mx_id: (data.mx_id ? data.mx_id : '') }) !== -1) { return '#dc3545'; } } }, }, ], }; leafXmjSpreadSetting.cols = leafXmjCols; const leafXmjCol = { getValue: { gather_qty: function (data) { if (materialQtySource === 3) { return ZhCalc.add(data.gather_qty, data.qc_minus_qty); } }, } } const needUpdateArray = ['quantity', 'msg_tp', 'msg_times', 'msg_spread', 'm_spread', 'm_tp', 'm_tax_tp', 'is_summary', 'remark']; function getGclList() { if (gclGatherListData) { return gclGatherListData; } else { const newGclGatherListData = []; for (const [index,s] of ledgerListData.entries()) { console.log(index, s); gclGatherModel.loadLedgerData(ledger, s); gclGatherModel.loadPosData(pos, posListData[index]); const oneGclGatherData = gclGatherModel.gatherGclData().filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); newGclGatherListData.push(oneGclGatherData); } return newGclGatherListData; } } let first = true; function setListsData(sid = null, ms_id = null) { if (first) { // 加载清单数据 - 暂时统一加载,如有需要,切换成动态加载并缓存 postData(window.location.pathname + '/load', {sid}, async function (result) { ledger = result.ledger; curLedgerData = result.curLedgerData; pos = result.pos; curPosData = result.curPosData; materialListData = result.materialListData; gclList = result.gclList; notJoinList = result.materialNotJoinListData; notChangeList = result.materialNotChangeListData; selfList = result.materialSelfListData; materialChecklistData = result.materialChecklistData; if (isStageSelf) { updateBillsData(ms_id); const newGclGatherListData = []; for (const [index, s] of result.ledgerListData.entries()) { gclGatherModel.loadLedgerData(_.cloneDeep(ledger), s); gclGatherModel.loadPosData(_.cloneDeep(pos), result.posListData[index]); const oneGclGatherData = gclGatherModel.gatherGclData().filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); newGclGatherListData.push(oneGclGatherData); } gclGatherListData = newGclGatherListData; } // 解析清单汇总数据 gclGatherModel.loadLedgerData(_.cloneDeep(ledger), curLedgerData); gclGatherModel.loadPosData(_.cloneDeep(pos), curPosData); gclGatherData = gclGatherModel.gatherGclData(); console.log(gclGatherData); if (openMaterialChecklist) { const hadBillsidList = _.uniq(_.map(gclList, 'gcl_id')); console.log(hadBillsidList); // 判断是否有修订影响到本次数据,并有几率修改清单设置页的值 const pushData = []; const updateData = []; for (const hb of hadBillsidList) { const gcl = _.find(gclGatherData, function (item) { return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: hb}) !== -1; }); if (gcl) { const mc = _.find(materialChecklistData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price }); // const newOrder = _.indexOf(gclGatherData, gcl); // console.log(newOrder); if (!mc && _.findIndex(pushData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price }) === -1) { pushData.push({ b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price, quantity: (gcl.quantity ? gcl.quantity : null), total_price: (gcl.total_price ? gcl.total_price : null), had_bills: 1 }); } } } const removeData = []; for (const mc of materialChecklistData) { const gcl = _.find(gclGatherData, { b_code: mc.b_code, name: mc.name, unit: mc.unit, unit_price: mc.unit_price }); // 判断是否已不存在工料清单,台账修改过后删除之 if (!gcl) { removeData.push(mc.id); } else { // 更新had_bills值 const updateObj = {id: mc.id}; const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : []; const jiaoji = _.intersection(gcl_ids, hadBillsidList); if (mc.had_bills === 1) { if (jiaoji.length === 0) { updateObj.mid = materialID; updateObj.had_bills = 0; // updateData.push({ id: mc.id, mid: materialID, had_bills: 0 }); } } else if (mc.had_bills === 0) { if (jiaoji.length !== 0) { updateObj.had_bills = 1; } } // 更新工程量及台账金额 if (mc.quantity !== (gcl.quantity ? gcl.quantity : null)) { updateObj.quantity = gcl.quantity ? gcl.quantity : null; updateObj.total_price = gcl.total_price ? gcl.total_price : null; } if (!_.isEqual(updateObj, {id: mc.id})) updateData.push(updateObj); } } console.log(pushData, removeData, updateData); if (pushData.length > 0 || removeData.length > 0 || updateData.length > 0) { // materialChecklistData = await postDataAsync('/tender/'+ tenderID +'/measure/material/'+ stage_order +'/checklist/save', { type: 'resetChecklist', pushData, removeData, updateData }) } gclGatherData = gclGatherData.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); // 取交集 const selfListGcl = _.uniq(_.map(selfList, 'gcl_id')); gclGatherData = _.filter(gclGatherData, function (item) { if (_.find(materialChecklistData, { b_code: item.b_code, name: item.name, unit: item.unit, unit_price: item.unit_price })) { return true; } else { const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : []; const jiaoji = _.intersection(gcl_ids, selfListGcl); if (jiaoji.length > 0) { return true; } } }); } else { gclGatherData = gclGatherData.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); } calculateJiaCha(gclGatherData); SpreadJsObj.initSpreadSettingEvents(leafXmjSpreadSetting, leafXmjCol); SpreadJsObj.initSheet(leafXmjSpread.getActiveSheet(), leafXmjSpreadSetting); // 加载清单数据 SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData); loadLeafXmjData(0); loadMaterialData(0); loadXmjMaterialData(0, 0); const sheet = materialSpread.getActiveSheet(); sheet.suspendPaint(); sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader); sheet.resumePaint(); const selfSheet = materialSelfSpread.getActiveSheet(); selfSheet.suspendPaint(); selfSheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader); selfSheet.resumePaint(); checkNotJoinMaterialData(); first = false; // 判断是否需要更新contract_qty、qc_qty和qc_minus_qty if (!materialIsNewQty && !readOnly) { let needUpdateList = []; if (materialListData.length > 0) { if (isStageSelf) { for (const msIndex in materialStageData) { const materialStageList = _.filter(materialListData, { ms_id: materialStageData[msIndex].id }); const gclIdList = _.uniq(_.map(materialListData, 'gcl_id')); let leafXmjList = []; for (const id of gclIdList) { const gcl = _.find(gclGatherListData[msIndex], function (item) { return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1; }); if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs]; } for (const ml of materialStageList) { const leafXmjInfo = _.find(leafXmjList, function (item) { return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id)); }); if (leafXmjInfo) { needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty }); } } } } else { // 找出所有gcl相同的,方便搜索gclGatherData值 const gclIdList = _.uniq(_.map(materialListData, 'gcl_id')); let leafXmjList = []; for (const id of gclIdList) { const gcl = _.find(gclGatherData, function (item) { return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1; }); if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs]; } for (const ml of materialListData) { const leafXmjInfo = _.find(leafXmjList, function (item) { return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id)); }); if (leafXmjInfo) { needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty }); } } } } console.log(needUpdateList); // postData(window.location.pathname + '/save', needUpdateList, function (result) { // materialListData2 = result.materialListData; // }); } }); } else { // 解析清单汇总数据 // const ms_id = $('#myTab').find('.active').data('msid'); updateBillsData(ms_id); const i = _.findIndex(materialStageData, { sid }); gclGatherData = gclGatherListData[i]; if (openMaterialChecklist) { gclGatherData = gclGatherData.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); // 取交集 const selfListGcl = _.uniq(_.map(selfList, 'gcl_id')); gclGatherData = _.filter(gclGatherData, function (item) { if (_.find(materialChecklistData, { b_code: item.b_code, name: item.name, unit: item.unit, unit_price: item.unit_price })) { return true; } else { const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : []; const jiaoji = _.intersection(gcl_ids, selfListGcl); if (jiaoji.length > 0) { return true; } } }); } else { gclGatherData = gclGatherData.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); } if ($('#show_material_gcl').is(':checked')) { const hadMaterialGclGatherData = []; const hadGclIdList = []; for (const ml of materialListData) { if (hadGclIdList.indexOf(ml.gcl_id) === -1) { hadGclIdList.push(ml.gcl_id); } } for (const gcl of gclGatherData) { for (const index in gcl.leafXmjs) { const gcl_id = gcl.leafXmjs[index].gcl_id; if (hadGclIdList.indexOf(gcl_id) !== -1) { hadMaterialGclGatherData.push(gcl); break; } } } gclGatherData = hadMaterialGclGatherData; } console.log(gclGatherData); calculateJiaCha(gclGatherData); SpreadJsObj.initSpreadSettingEvents(leafXmjSpreadSetting, leafXmjCol); SpreadJsObj.initSheet(leafXmjSpread.getActiveSheet(), leafXmjSpreadSetting); // 加载清单数据 SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData); loadLeafXmjData(0); loadMaterialData(0); loadXmjMaterialData(0, 0); const sheet = materialSpread.getActiveSheet(); sheet.suspendPaint(); sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader); sheet.resumePaint(); const selfSheet = materialSelfSpread.getActiveSheet(); selfSheet.suspendPaint(); selfSheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader); selfSheet.resumePaint(); ledgerSpread.getActiveSheet().setSelection(0, 0, 1, 1); checkNotJoinMaterialData(); } } function updateBillsData(ms_id) { const msbList = _.filter(materialStageBillsData, { ms_id }); for (const mb of materialBillsData) { mb.ms_id = ms_id; const msbInfo = _.find(msbList, { mb_id: mb.id }); for (const nu of needUpdateArray) { mb[nu] = msbInfo[nu]; } } } setListsData(isStageSelf ? materialStageData[0].sid : null, isStageSelf ? materialStageData[0].id : null); // 期切换 $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) { e.preventDefault(); const ms_id = $('#myTab').find('.active').data('msid'); const msInfo = _.find(materialStageData, { id: ms_id }); showWaitingView(); setTimeout(function () { setListsData(msInfo.sid, ms_id); closeWaitingView(); }, 500); }); // const leafXmjCol = { // getValue: { // jiacha: function (data) { // let sum = 0; // const sheet = leafXmjSpread.getActiveSheet(); // const select = SpreadJsObj.getSelectObject(sheet); // const notx = findNotJoinLeafXmj(select); // if (notx === undefined) { // for(const ml of materialList) { // sum = ZhCalc.round(ZhCalc.add(sum, ZhCalc.mul(ZhCalc.mul(data.gather_qty, ml.quantity), getMpSpreadByMBData(ml.mb_id))), 2); // } // } // return sum !== 0 ? sum : null; // } // } // }; // SpreadJsObj.initSpreadSettingEvents(leafXmjSpreadSetting, leafXmjCol); // 调差清单工料table const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]); const materialSpreadSetting = { cols: [ {title: '清单工料含量|编号', colSpan: '5|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true}, {title: '|名称', colSpan: '|1', rowSpan: '|1', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true}, {title: '|单位', colSpan: '|1', rowSpan: '|1', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true}, {title: '|数量 �', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 80, type: 'Number', readOnly: 'readOnly.isEdit'}, {title: '|计算式', colSpan: '1', rowSpan: '|1', field: 'expr', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isEdit'}, ], emptyRows: 0, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', }; const materialBase = { isEdit: function (data, type = 'normal') { // 是否本期添加的工料 // return data.order === stage_order && !openMaterialChecklist; let flag = true; if (type === 'del' || !editListPermission) { flag = data.order === stage_order; } return flag && !openMaterialChecklist; } }; const materialCol = { readOnly: { isEdit: function (data) { // const sheet = leafXmjSpread.getActiveSheet(); // const select = SpreadJsObj.getSelectObject(sheet); // const notx = findNotJoinLeafXmj(select); // return !(!readOnly && notx === undefined && materialBase.isEdit(data)); return !(!readOnly && materialBase.isEdit(data)); }, }, }; SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol); // 获取项目节数据 let materialList = []; function loadMaterialData(iGclRow) { const gcl = gclGatherData[iGclRow]; if (gcl && gcl.leafXmjs) { const gcl_ids = gcl.leafXmjs ? _.uniq(_.map(gcl.leafXmjs, 'gcl_id')) : []; materialList = []; const newMaterialList = _.uniqBy(_.filter(gclList, function (m) { return _.indexOf(gcl_ids, m.gcl_id) !== -1; }), 'mb_id'); for(const m of newMaterialList) { const bills = _.find(materialBillsData, { id: m.mb_id }); if(bills) { m.code = bills.code; m.name = bills.name; m.unit = bills.unit; } } console.log(newMaterialList); materialList = newMaterialList; // 对清单调差工料table的单位数量进行改变 materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + '每' + gcl.unit + '数量 �'; SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting); SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialList); } else { materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '数量 �'; SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting); SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []); } SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet()); } // 调差清单工料table const materialSelfSpread = SpreadJsObj.createNewSpread($('#material-self-spread')[0]); const materialSelfSpreadSetting = { cols: [ {title: '明细清单工料含量|编号', colSpan: '5|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true}, {title: '|名称', colSpan: '|1', rowSpan: '|1', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true}, {title: '|单位', colSpan: '|1', rowSpan: '|1', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true}, {title: '|数量 �', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 80, type: 'Number', readOnly: 'readOnly.isEdit'}, {title: '|计算式', colSpan: '1', rowSpan: '|1', field: 'expr', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isEdit'}, ], emptyRows: 0, headRows: 2, headRowHeight: [25, 25], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', }; const materialSelfBase = { isEdit: function (data, type = 'normal') { // 是否本期添加的工料 // return data.order === stage_order && !openMaterialChecklist; let flag = true; if (type === 'del' || !editListPermission) { flag = data.order === stage_order; } return flag; } }; const materialSelfCol = { readOnly: { isEdit: function (data) { // const sheet = leafXmjSpread.getActiveSheet(); // const select = SpreadJsObj.getSelectObject(sheet); // const notx = findNotJoinLeafXmj(select); // return !(!readOnly && notx === undefined && materialBase.isEdit(data)); return openMaterialSelf && !(!readOnly && materialSelfBase.isEdit(data)); }, }, }; SpreadJsObj.initSpreadSettingEvents(materialSelfSpreadSetting, materialSelfCol); let materialListSelf = []; function loadXmjMaterialData(iGclRow, iLXmjRow) { // 存在单独设置的才展示,不然隐藏 const gcl = gclGatherData[iGclRow]; const leafXmjs = gcl && gcl.leafXmjs ? gcl.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }) : null; if (leafXmjs) { const xmj = leafXmjs[iLXmjRow]; console.log(xmj, iLXmjRow); materialListSelf = []; if (_.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.id, mx_id: (xmj.mx_id ? xmj.mx_id : '') }) != -1) { const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; for (const m of materialListData) { if (m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined) && (ms_id === null || ms_id === m.ms_id)) { materialListSelf.push(m); } } $('#material-self-spread-div').addClass('active'); showSelfSideTools(true); } else { $('#material-self-spread-div').removeClass('active'); showSelfSideTools(false); } // 对清单调差工料table的单位数量进行改变 materialSelfSpreadSetting.cols[materialSelfSpreadSetting.cols.length - 2].title = '|' + '每' + gcl.unit + '数量 �'; SpreadJsObj.initSheet(materialSelfSpread.getActiveSheet(), materialSelfSpreadSetting); SpreadJsObj.loadSheetData(materialSelfSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialListSelf); } else { $('#material-self-spread-div').removeClass('active'); showSelfSideTools(false); materialSpreadSetting.cols[materialSelfSpreadSetting.cols.length - 2].title = '数量 �'; SpreadJsObj.initSheet(materialSelfSpread.getActiveSheet(), materialSelfSpreadSetting); SpreadJsObj.loadSheetData(materialSelfSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []); } SpreadJsObj.resetTopAndSelect(materialSelfSpread.getActiveSheet()); leafXmjSpread.refresh(); materialSelfSpread.refresh(); } // SpreadJsObj.locateTreeNode(ledgerSpread.getActiveSheet(), ) // loadLeafXmjData(0); // loadMaterialData(0, 0); // const sheet = materialSpread.getActiveSheet(); // sheet.suspendPaint(); // sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader); // sheet.resumePaint(); // 不参与调差数据值变灰, 数量变更不参与调差数据值变黄 function checkNotJoinMaterialData() { const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); if (index !== -1) { const xmj = gclGatherData[index].leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); const leafXmjSheet = leafXmjSpread.getActiveSheet(); for (const [iRow,x] of xmj.entries()) { const notx = findNotJoinLeafXmj(x); const color = notx === undefined ? '' : '#d6d8db'; // leafXmjSheet.getRange(iRow, -1, 1, -1).backColor(color); const notx2 = findNotChangeLeafXmj(x); const color2 = notx2 === undefined ? color : '#FFE699'; leafXmjSheet.getRange(iRow, -1, 1, -1).backColor(color2); } } } // checkNotJoinMaterialData(); // 对添加工料表格赋值 function changeMaterialTable(from = 'all') { $('#materialBills tr').removeClass('table-secondary'); $('#materialBills').find('input').removeAttr('disabled'); $('#materialBills').find('input').prop('checked', false); const searchList = from === 'self' ? materialListSelf : materialList; console.log(searchList); for (const [index, ml] of searchList.entries()) { const mbIndex = materialBillsData.findIndex(function (item) { return item.id === ml.mb_id; }); if (mbIndex !== -1) { $('#materialBills tr').eq(mbIndex).addClass('table-secondary'); $('#materialBills').find('input').eq(mbIndex).attr('disabled', true); $('#materialBills').find('input').eq(mbIndex).prop('checked', true); } } } // 添加调差工料 $('#add_material_bill').click(function () { // 获取已选工料 $('#materialBills').find('input:disabled').prop('checked', false); const selectList = $('#materialBills').find('input:checked'); if (selectList.length === 0) { toastr.warning('请选择调差工料'); $('#materialBills').find('input:disabled').prop('checked', true); return false; } const mb_id = []; for (let s = 0; s < selectList.length; s++) { mb_id.push($('#materialBills').find('input:checked').eq(s).val()); } const type = $('#add_type').val(); if (type === 'all') { // 获取当前项目节或部位明细id const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); const gcl = gclGatherData[index].leafXmjs; const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; const datas = []; for (const xmj of gcl) { const notx = findNotJoinLeafXmj(xmj); const notx2 = findNotChangeLeafXmj(xmj); const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', contract_qty: xmj.contract_qty, qc_qty: xmj.qc_qty, qc_minus_qty: xmj.qc_minus_qty, gather_qty: xmj.gather_qty, is_join: notx === undefined ? 1 : 0, }; if (ms_id) data.ms_id = ms_id; datas.push(data); } if (isStageSelf) { // 取所有的gclGatherData才行,然后获取下的值 const gclData = gclGatherData[index]; for (const [index, ms] of materialStageData.entries()) { if (ms.id !== ms_id) { const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price }); if (gclOther) { const leafXmjs = gclOther.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); // gclOther.leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined); for (const xmj of leafXmjs) { const notx = findNotJoinLeafXmj(xmj); const notx2 = findNotChangeLeafXmj(xmj); const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', contract_qty: xmj.contract_qty, qc_qty: xmj.qc_qty, qc_minus_qty: xmj.qc_minus_qty, gather_qty: xmj.gather_qty, is_join: notx === undefined ? 1 : 0, ms_id: ms.id, }; datas.push(data); } } } } } console.log(datas, gcl, mb_id); postData(window.location.pathname + '/save', {type: 'adds', postData: {xmjs: datas, mbIds: mb_id} }, function (result) { materialListData = result.materialListData; gclList = result.gclList; // toastr.success('已成功应用'); calculateJiaCha(gclGatherData); // const index = gclGatherData.indexOf(ledgerSelect); loadLeafXmjData(index); // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gclGatherData[index].leafXmjs.indexOf(xmjSelect); loadMaterialData(index); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index); $('#addgl').modal('hide'); }); } else if (type === 'self') { // 获取当前项目节或部位明细id const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); const leafXmjSheet = leafXmjSpread.getActiveSheet(); const leafXmjSelect = SpreadJsObj.getSelectObject(leafXmjSheet); const gcl = gclGatherData[index].leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); const leafXmjIndex = gcl.indexOf(leafXmjSelect); const xmj = gcl[leafXmjIndex]; const notx = findNotJoinLeafXmj(xmj); const notx2 = findNotChangeLeafXmj(xmj); const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', mb_id: mb_id, contract_qty: xmj.contract_qty, qc_qty: xmj.qc_qty, qc_minus_qty: xmj.qc_minus_qty, gather_qty: xmj.gather_qty, is_join: notx === undefined ? 1 : 0, }; console.log(data); postData(window.location.pathname + '/save', {type: 'add', postData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { // 添加到materialList里 materialListData = result; loadXmjMaterialData(index, leafXmjIndex); $('#addgl').modal('hide'); }); } $('#materialBills').find('input:disabled').prop('checked', true); }); const leafXmjSpreadObj = { getSelect : function () { const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); const leafXmjSheet = leafXmjSpread.getActiveSheet(); const leafXmjSelect = SpreadJsObj.getSelectObject(leafXmjSheet); const iRow = gclGatherData[index].leafXmjs.indexOf(leafXmjSelect); const leafXmjs = gclGatherData[index].leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); const nRow = leafXmjs.indexOf(leafXmjSelect); const leafXmjColor = findNotJoinLeafXmj(leafXmjSelect) ? '#d6d8db' : (findNotChangeLeafXmj(leafXmjSelect) ? '#FFE699' : ''); return [index, iRow, nRow, leafXmjSheet, leafXmjSelect, leafXmjColor]; }, checkJoinMaterial: function (type) { const [iGclRow, iRow, nRow, sheet, select] = leafXmjSpreadObj.getSelect(); const color = type === 'join' ? '' : '#d6d8db'; const data = { type: type, select: type === 'join' ? findNotJoinLeafXmj(select) : select, gather_qty: select.gather_qty, ms_id: $('#myTab').find('.active').data('msid') || null, }; // 添加到 postData(window.location.pathname + '/save', data, function (result) { if (type === 'join') { const index = findNotJoinLeafXmj(select, 'index'); notJoinList.splice(index, 1); } else { notJoinList.push(result); const index = findNotChangeLeafXmj(select, 'index'); notChangeList.splice(index, 1); } gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(select); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(sheet, nRow); sheet.getRange(nRow, -1, 1, -1).backColor(color); loadMaterialData(iGclRow); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); }); }, checkChangeMaterial: function (type) { const [iGclRow, iRow, nRow, sheet, select] = leafXmjSpreadObj.getSelect(); const color = type === 'change' ? '' : '#FFE699'; const data = { type: type, select: type === 'change' ? findNotChangeLeafXmj(select) : select, gather_qty: type === 'change' ? select.gather_qty : select.contract_qty, ms_id: $('#myTab').find('.active').data('msid') || null, }; // 添加到 postData(window.location.pathname + '/save', data, function (result) { if (type === 'change') { const index = findNotChangeLeafXmj(select, 'index'); notChangeList.splice(index, 1); } else { notChangeList.push(result); } gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(select); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(sheet, nRow); sheet.getRange(nRow, -1, 1, -1).backColor(color); loadMaterialData(iGclRow); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); }); }, checkSelfMaterial: function (type) { const [iGclRow, iRow, nRow, sheet, select, color] = leafXmjSpreadObj.getSelect(); // const color = type === 'self' ? '' : '#d6d8db'; const data = { type: type, select: type === 'noself' ? findSelfLeafXmj(select) : select, ms_id: $('#myTab').find('.active').data('msid') || null, }; if (type === 'noself') { if (isStageSelf) { data.select.gather_qty = {}; const ledgerSheet = ledgerSpread.getActiveSheet(); const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet); const index = gclGatherData.indexOf(ledgerSelect); // 取所有的gclGatherData才行,然后获取下的值 const gclData = gclGatherData[index]; for (const [index, ms] of materialStageData.entries()) { const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price }); let gather_qty = null; if (gclOther) { const leafXmjs = gclOther.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); const oneXmj = _.find(leafXmjs, function (item) { return item.gcl_id === select.gcl_id && item.id === select.id && (select.mx_id === undefined || item.mx_id === select.mx_id); }); if (oneXmj) gather_qty = oneXmj.gather_qty; } data.select.gather_qty['ms_id_' + ms.id] = gather_qty; } } else { data.select.gather_qty = select.gather_qty ? select.gather_qty : null; } } console.log(data); // 添加到 postData(window.location.pathname + '/save', data, function (result) { if (type === 'noself') { const index = findSelfLeafXmj(select, 'index'); selfList.splice(index, 1); materialListData = result; $('#cancel-self').modal('hide'); } else { selfList.push(result.info); materialListData = result.materialListData; } gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(select); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(sheet, nRow); sheet.getRange(nRow, -1, 1, -1).backColor(color); loadXmjMaterialData(iGclRow, nRow); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); }); }, } if (!readOnly) { // leafXmj右键功能 if (!editForAudit) { $.contextMenu({ selector: '#leaf-xmj-spread', build: function ($trigger, e) { const target = SpreadJsObj.safeRightClickSelection($trigger, e, leafXmjSpread); return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader; }, items: { 'stop': { name: '当前节点不参与调差', icon: 'fa-remove', callback: function (key, opt) { leafXmjSpreadObj.checkJoinMaterial('notjoin'); }, visible: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findNotJoinLeafXmj(select); if (!readOnly && select && notx === undefined) { return true; } else { return false; } } }, 'start': { name: '当前节点参与调差', icon: 'fa-sign-in', callback: function (key, opt) { leafXmjSpreadObj.checkJoinMaterial('join'); }, visible: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findNotJoinLeafXmj(select); if (!readOnly && select && notx === undefined) { return false; } else { return true; } }, }, 'stop2': { name: '数量变更不参与调差', icon: 'fa-remove', callback: function (key, opt) { leafXmjSpreadObj.checkChangeMaterial('notchange'); }, visible: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findNotJoinLeafXmj(select); if (!(!readOnly && select && notx === undefined)) { return false; } const notx2 = findNotChangeLeafXmj(select); if (!readOnly && select && notx2 === undefined && materialQtySource !== qtySourceValueConst.contract_qty) { return true; } else { return false; } } }, 'start2': { name: '数量变更参与调差', icon: 'fa-sign-in', callback: function (key, opt) { leafXmjSpreadObj.checkChangeMaterial('change'); }, visible: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findNotJoinLeafXmj(select); if (!(!readOnly && select && notx === undefined)) { return false; } const notx2 = findNotChangeLeafXmj(select); if (!readOnly && select && notx2 === undefined) { return false; } else { return true; } }, }, 'self': { name: '单独添加工料', icon: 'fa-sign-in', callback: function (key, opt) { leafXmjSpreadObj.checkSelfMaterial('self'); }, visible: function (key, opt) { if (!openMaterialSelf) { return false; } const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findSelfLeafXmj(select); if (!readOnly && select && notx === undefined) { return true; } else { return false; } } }, 'noself': { name: '取消单独添加工料', icon: 'fa-remove', callback: function (key, opt) { $('#cancel-self').modal('show'); // leafXmjSpreadObj.checkSelfMaterial('noself'); }, visible: function (key, opt) { if (!openMaterialSelf) { return false; } const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const sel = sheet.getSelections()[0]; if (!select || sel.rowCount !== 1) { return false; } const notx = findSelfLeafXmj(select); if (!readOnly && select && notx === undefined) { return false; } else { return true; } }, disabled: function (key, opt) { // const sheet = ledgerSpread.getActiveSheet(); // const select = SpreadJsObj.getSelectObject(sheet); // const index = gclGatherData.indexOf(select); // const leafXmjSheet = leafXmjSpread.getActiveSheet(); // const leafXmjSelect = SpreadJsObj.getSelectObject(leafXmjSheet); // const iRow = gclGatherData[index].leafXmjs.indexOf(leafXmjSelect); // const leafXmjs = gclGatherData[index].leafXmjs.filter(item => { // return item.qc_qty || item.contract_qty // }); // const nRow = leafXmjs.indexOf(leafXmjSelect); // const leafXmjColor = findNotJoinLeafXmj(leafXmjSelect) ? '#d6d8db' : ''; // return [index, iRow, nRow, leafXmjSheet, leafXmjSelect, leafXmjColor]; const [iGclRow, iRow, nRow, sheet, select, color] = leafXmjSpreadObj.getSelect(); const gcl = gclGatherData[iGclRow]; const leafXmjs = gcl && gcl.leafXmjs ? gcl.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }) : null; let flag = false; if (leafXmjs) { const xmj = leafXmjs[nRow]; if (_.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.id, mx_id: (xmj.mx_id ? xmj.mx_id : '') }) != -1) { for (const m of materialListData) { if (m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined) && m.order !== stage_order) { flag = true; break; } } } } return flag; }, }, } }); $('#cancelSelfBtn').click(function () { leafXmjSpreadObj.checkSelfMaterial('noself'); }); } // material-spread右键功能 const materialSpreadObj = { del: function () { const materialSheet = materialSpread.getActiveSheet(); const materialSelect = SpreadJsObj.getSelectObject(materialSheet); const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); // const leafXmjSheet = leafXmjSpread.getActiveSheet(); // const leafXmjSelect = SpreadJsObj.getSelectObject(leafXmjSheet); const gcl = gclGatherData[index].leafXmjs; // const leafXmjIndex = gcl.indexOf(leafXmjSelect); const datas = []; for (const xmj of gcl) { const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; datas.push(data); } if (isStageSelf) { const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; // 取所有的gclGatherData才行,然后获取下的值 const gclData = gclGatherData[index]; for (const [index, ms] of materialStageData.entries()) { if (ms.id !== ms_id) { const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price }); if (gclOther) { const leafXmjs = gclOther.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); for (const xmj of leafXmjs) { const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; if (_.indexOf(datas, data) === -1) { datas.push(data); } } } } } } console.log(datas, materialSelect.mb_id); postData(window.location.pathname + '/save', {type: 'dels', postData: { xmjs: datas, mb_id: materialSelect.mb_id }, ms_id: $('#myTab').find('.active').data('msid') || null}, function (result) { materialListData = result.materialListData; gclList = result.gclList; calculateJiaCha(gclGatherData); // const index = gclGatherData.indexOf(ledgerSelect); loadLeafXmjData(index); // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gclGatherData[index].leafXmjs.indexOf(xmjSelect); loadMaterialData(index); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index); }); // postData(window.location.pathname + '/save', {type: 'del', id: select.id, mb_id: select.mb_id}, function (result) { // const index = materialList.indexOf(select); // materialList.splice(index, 1); // sheet.deleteRows(index, 1); // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet()); // const sel = sheet.getSelections(); // sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1); // const materialListIndex = materialListData.indexOf(select); // materialListData.splice(materialListIndex, 1); // const [iGclRow, iRow, lsheet, lselect] = leafXmjSpreadObj.getSelect(); // gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); // calculateJiaCha(gclGatherData, iGclRow); // SpreadJsObj.reLoadRowData(lsheet, iRow); // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); // }); }, deletePress: function (sheet) { return; }, editStarting: function (e, info) { const col = info.sheet.zh_setting.cols[info.col]; const select = SpreadJsObj.getSelectObject(info.sheet); if (col.field === 'quantity') { if (select.expr && select.expr !== '') { info.sheet.getCell(info.row, info.col).text(select.expr); } } }, editEnded: function (e, info) { if (info.sheet.zh_setting) { const select = SpreadJsObj.getSelectObject(info.sheet); const col = info.sheet.zh_setting.cols[info.col]; // 未改变值则不提交 // const validText = info.editingText ? (typeof(info.editingText) === 'String' ? info.editingText.replace('\n', '') : info.editingText) : null; // const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null); // let orgValue = select[col.field]; const validText = info.editingText ? info.editingText.replace('\n', '') : null; let orgValue; if (col.field === 'quantity') { orgValue = validText && validText !== '' ? _.toNumber(validText) ? select.quantity : select.expr : (select.expr && select.expr !== '') ? select.expr : select.quantity; } else { orgValue = select[col.field]; } if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) { SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } const exprQuantity = { expr: '', quantity: 0, }; const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity); if (!valid) { toastr.error(msg); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } if (isNaN(exprQuantity.quantity)) { toastr.error('不能输入其它非数字类型字符'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } const num = parseFloat(exprQuantity.quantity); if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) { // toastr.error('数量值必须大于0并且小于6位小数的浮点数'); // SpreadJsObj.reLoadRowData(info.sheet, info.row); // return; toastr.warning('已保留6位小数'); exprQuantity.quantity = ZhCalc.round(num, 6); } // 更新至服务器 const ledgerSheet = ledgerSpread.getActiveSheet(); const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet); const index = gclGatherData.indexOf(ledgerSelect); const gcl = gclGatherData[index].leafXmjs; // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gcl.indexOf(xmjSelect); const datas = []; for (const xmj of gcl) { const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; datas.push(data); } if (isStageSelf) { const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; // 取所有的gclGatherData才行,然后获取下的值 const gclData = gclGatherData[index]; for (const [index, ms] of materialStageData.entries()) { if (ms.id !== ms_id) { const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price }); if (gclOther) { const leafXmjs = gclOther.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); for (const xmj of leafXmjs) { const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; if (_.indexOf(datas, data) === -1) { datas.push(data); } } } } } } console.log(exprQuantity, datas, select.mb_id); postData(window.location.pathname + '/save', { type:'updates', updateData: { xmjs: datas, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id }, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { materialListData = result.materialListData; gclList = result.gclList; calculateJiaCha(gclGatherData); // const index = gclGatherData.indexOf(ledgerSelect); loadLeafXmjData(index); // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gclGatherData[index].leafXmjs.indexOf(xmjSelect); loadMaterialData(index); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index); materialSpread.getActiveSheet().setSelection(info.row + 1, info.col, 1, 1); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.row); }); // postData(window.location.pathname + '/save', { type:'update', updateData: { id:select.id, quantity: validText, mb_id: select.mb_id } }, function (result) { // const materialListIndex = materialListData.indexOf(select); // const index = materialList.indexOf(select); // select[col.field] = validText; // materialListData.splice(materialListIndex, 1, select); // materialList.indexOf(index, 1, select); // SpreadJsObj.reLoadRowData(info.sheet, info.row); // const [iGclRow, iRow, sheet, lselect] = leafXmjSpreadObj.getSelect(); // gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); // calculateJiaCha(gclGatherData, iGclRow) // SpreadJsObj.reLoadRowData(sheet, iRow); // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); // }, function () { // SpreadJsObj.reLoadRowData(info.sheet, info.row); // }); } }, clipboardPasted(e, info) { const hint = { cellError: {type: 'error', msg: '粘贴内容超出了表格范围'}, numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'}, numberCan: {type: 'warning', msg: '已保留6位小数'}, }; const range = info.cellRange; const sortData = info.sheet.zh_data || []; if (range.row + range.rowCount > sortData.length) { toastMessageUniq(hint.cellError); SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet()); return; } if (sortData.length > 0 && range.col + range.colCount > 5) { toastMessageUniq(hint.cellError); SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet()); return; } const data = []; for (let iRow = 0; iRow < range.rowCount; iRow++) { let bPaste = true; const curRow = range.row + iRow; const materialData = { id: sortData[curRow].id, mb_id: sortData[curRow].mb_id }; const hintRow = range.rowCount > 1 ? curRow : ''; let sameCol = 0; for (let iCol = 0; iCol < range.colCount; iCol++) { const curCol = range.col + iCol; const colSetting = info.sheet.zh_setting.cols[curCol]; if (!colSetting) continue; // let validText = info.sheet.getText(curRow, curCol); // validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null); const validText = info.sheet.getText(curRow, curCol).replace('\n', ''); const orgValue = sortData[curRow][colSetting.field]; if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) { sameCol++; if (range.colCount === sameCol) { bPaste = false; } continue; } const exprQuantity = { expr: '', quantity: 0, }; const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity); if (!valid) { toastMessageUniq(getPasteHint(msg, hintRow)); bPaste = false; continue; } if (isNaN(exprQuantity.quantity)) { toastMessageUniq(getPasteHint(hint.numberExpr, hintRow)); bPaste = false; continue; } const num = parseFloat(exprQuantity.quantity); if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) { toastMessageUniq(getPasteHint(hint.numberCan, hintRow)); // bPaste = false; // continue; exprQuantity.quantity = ZhCalc.round(num, 6); } // materialData[colSetting.field] = validText; materialData.expr = exprQuantity.expr; materialData.quantity = exprQuantity.quantity; } if (bPaste) { data.push(materialData); // rowData.push(curRow); } else { SpreadJsObj.reLoadRowData(info.sheet, curRow); } } if (data.length === 0) { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); return; } const ledgerSheet = ledgerSpread.getActiveSheet(); const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet); const index = gclGatherData.indexOf(ledgerSelect); const gcl = gclGatherData[index].leafXmjs; // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gcl.indexOf(xmjSelect); const datas = []; for (const xmj of gcl) { const data2 = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; datas.push(data2); } if (isStageSelf) { const ms_id = isStageSelf ? parseInt($('#myTab').find('.active').data('msid')) : null; // 取所有的gclGatherData才行,然后获取下的值 const gclData = gclGatherData[index]; for (const [index, ms] of materialStageData.entries()) { if (ms.id !== ms_id) { const gclOther = _.find(gclGatherListData[index], { b_code: gclData.b_code, name: gclData.name, unit: gclData.unit, unit_price: gclData.unit_price }); if (gclOther) { const leafXmjs = gclOther.leafXmjs.filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); for (const xmj of leafXmjs) { const data = { xmj_id: xmj.id, gcl_id: xmj.gcl_id, mx_id: xmj.mx_id ? xmj.mx_id : '', }; if (_.indexOf(datas, data) === -1) { datas.push(data); } } } } } } console.log(data, datas); // 更新至服务器 postData(window.location.pathname + '/save', { type:'pastes', updateData: { xmjs: datas, pasteData: data }, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { materialListData = result.materialListData; gclList = result.gclList; calculateJiaCha(gclGatherData); // const index = gclGatherData.indexOf(ledgerSelect); loadLeafXmjData(index); // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gclGatherData[index].leafXmjs.indexOf(xmjSelect); loadMaterialData(index); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index); materialSpread.getActiveSheet().setSelection(info.cellRange.row, info.cellRange.col, info.cellRange.rowCount, info.cellRange.colCount); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.row); }); // postData(window.location.pathname + '/save', { type:'paste', updateData: data }, function (result) { // materialListData = result; // const [iGclRow, iRow, sheet, lselect] = leafXmjSpreadObj.getSelect(); // gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); // calculateJiaCha(gclGatherData, iGclRow); // SpreadJsObj.reLoadRowData(sheet, iRow); // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); // }, function () { // SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); // }); }, _checkExprValid(expr) { if (!expr) return [true, null]; const param = []; let num = '', base = ''; for (let i = 0, iLen = expr.length; i < iLen; i++) { if (/^[\d\.%]+/.test(expr[i])) { if (base !== '') { param.push({type: 'base', value: base}); base = ''; } num = num + expr[i]; } else if (expr[i] === '(') { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'left', value: '('}); } else if (expr[i] === ')') { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'right', value: ')'}); } else if (/^[\+\-*\/]/.test(expr[i])) { if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } param.push({type: 'calc', value: expr[i]}); } else { return [false, '输入的表达式含有非法字符: ' + expr[i]]; } } if (num !== '') { param.push({type: 'num', value: num}); num = ''; } if (base !== '') { param.push({type: 'base', value: base}); base = ''; } if (param.length === 0) return true; if (param.length > 1) { if (param[0].value === '-') { param[1].value = '-' + param[1]; } param.unshift(); } const iLen = param.length; let iLeftCount = 0, iRightCount = 0; for (const [i, p] of param.entries()) { if (p.type === 'calc') { if (i === 0 || i === iLen - 1) return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字']; } if (p.type === 'num') { num = p.value.replace('%', ''); if (p.value.length - num.length > 1) return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字']; num = _.toNumber(num); if (num === undefined || num === null || _.isNaN(num)) return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字']; if (i > 0) { if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') { return [false, '输入的表达式非法:' + p.value + '前应有运算符']; } else if (param[i - 1].value === '/' && num === 0) { return [false, '输入的表达式非法:请勿除0']; } } } if (p.type === 'base') { if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right')) return [false, '输入的表达式非法:' + p.value + '前应有运算符']; } if (p.type === 'left') { iLeftCount += 1; if (i !== 0 && param[i-1].type !== 'calc') return [false, '输入的表达式非法:(前应有运算符']; } if (p.type === 'right') { iRightCount += 1; if (i !== iLen - 1 && param[i+1].type !== 'calc') return [false, '输入的表达式非法:)后应有运算符']; if (iRightCount > iLeftCount) return [false, '输入的表达式非法:")"前无对应的"("']; } } if (iLeftCount > iRightCount) return [false, '输入的表达式非法:"("后无对应的")"']; return [true, '']; }, _checkExpr: function (text, data) { if (text) { const num = _.toNumber(text); if (num) { data.quantity = num; data.expr = ''; } else { const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase(); const [valid, msg] = this._checkExprValid(expr); if (!valid) return [valid, msg]; data.expr = expr; data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr.replace('%', '/100')); // const ce = new CalcEvalMin(); // data.quantity = ce.eval(expr); // console.log(data.quantity); } } else { data.quantity = 0; data.expr = ''; } return [true, '']; }, }; materialSpread.bind(spreadNS.Events.EditStarting, materialSpreadObj.editStarting); materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded); materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted); SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress); // material-spread右键功能 if (openMaterialSelf) { const materialSelfSpreadObj = { del: function () { const sheet = materialSelfSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); console.log(select); postData(window.location.pathname + '/save', { type: 'del', id: select.id, mb_id: select.mb_id, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { const index = materialListSelf.indexOf(select); materialListSelf.splice(index, 1); sheet.deleteRows(index, 1); SpreadJsObj.reLoadSheetData(materialSelfSpread.getActiveSheet()); const sel = sheet.getSelections(); sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1); const materialListIndex = materialListData.indexOf(select); materialListData.splice(materialListIndex, 1); const [iGclRow, iRow, nRow, lsheet, lselect, color] = leafXmjSpreadObj.getSelect(); gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(lsheet, nRow); lsheet.getRange(nRow, -1, 1, -1).backColor(color); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); }); }, deletePress: function (sheet) { return; }, editStarting: function (e, info) { const col = info.sheet.zh_setting.cols[info.col]; const select = SpreadJsObj.getSelectObject(info.sheet); if (col.field === 'quantity') { if (select.expr && select.expr !== '') { info.sheet.getCell(info.row, info.col).text(select.expr); } } }, editEnded: function (e, info) { if (info.sheet.zh_setting) { const select = SpreadJsObj.getSelectObject(info.sheet); const col = info.sheet.zh_setting.cols[info.col]; // 未改变值则不提交 // const validText = info.editingText ? (typeof(info.editingText) === 'String' ? info.editingText.replace('\n', '') : info.editingText) : null; // const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null); // let orgValue = select[col.field]; const validText = info.editingText ? info.editingText.replace('\n', '') : null; let orgValue; if (col.field === 'quantity') { orgValue = validText && validText !== '' ? _.toNumber(validText) ? select.quantity : select.expr : (select.expr && select.expr !== '') ? select.expr : select.quantity; } else { orgValue = select[col.field]; } if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) { SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } const exprQuantity = { expr: '', quantity: 0, }; const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity); if (!valid) { toastr.error(msg); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } if (isNaN(exprQuantity.quantity)) { toastr.error('不能输入其它非数字类型字符'); SpreadJsObj.reLoadRowData(info.sheet, info.row); return; } const num = parseFloat(exprQuantity.quantity); if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) { // toastr.error('数量值必须大于0并且小于6位小数的浮点数'); // SpreadJsObj.reLoadRowData(info.sheet, info.row); // return; toastr.warning('已保留6位小数'); exprQuantity.quantity = ZhCalc.round(num, 6); } // 更新至服务器 console.log(exprQuantity, select.mb_id); postData(window.location.pathname + '/save', { type: 'update', updateData: { id: select.id, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id }, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { const materialListIndex = materialListData.indexOf(select); const index = materialList.indexOf(select); select.quantity = exprQuantity.quantity; select.expr = exprQuantity.expr; materialListData.splice(materialListIndex, 1, select); materialList.indexOf(index, 1, select); SpreadJsObj.reLoadRowData(info.sheet, info.row); const [iGclRow, iRow, nRow, sheet, lselect, color] = leafXmjSpreadObj.getSelect(); gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(sheet, nRow); console.log(lselect, color); sheet.getRange(nRow, -1, 1, -1).backColor(color); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.row); }); } }, clipboardPasted(e, info) { const hint = { cellError: {type: 'error', msg: '粘贴内容超出了表格范围'}, numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'}, numberCan: {type: 'warning', msg: '已保留6位小数'}, }; const range = info.cellRange; const sortData = info.sheet.zh_data || []; if (range.row + range.rowCount > sortData.length) { toastMessageUniq(hint.cellError); SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet()); return; } if (sortData.length > 0 && range.col + range.colCount > 5) { toastMessageUniq(hint.cellError); SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet()); SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet()); return; } const data = []; for (let iRow = 0; iRow < range.rowCount; iRow++) { let bPaste = true; const curRow = range.row + iRow; const materialData = {id: sortData[curRow].id, mb_id: sortData[curRow].mb_id}; const hintRow = range.rowCount > 1 ? curRow : ''; let sameCol = 0; for (let iCol = 0; iCol < range.colCount; iCol++) { const curCol = range.col + iCol; const colSetting = info.sheet.zh_setting.cols[curCol]; if (!colSetting) continue; // let validText = info.sheet.getText(curRow, curCol); // validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null); const validText = info.sheet.getText(curRow, curCol).replace('\n', ''); const orgValue = sortData[curRow][colSetting.field]; if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) { sameCol++; if (range.colCount === sameCol) { bPaste = false; } continue; } const exprQuantity = { expr: '', quantity: 0, }; const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity); if (!valid) { toastMessageUniq(getPasteHint(msg, hintRow)); bPaste = false; continue; } if (isNaN(exprQuantity.quantity)) { toastMessageUniq(getPasteHint(hint.numberExpr, hintRow)); bPaste = false; continue; } const num = parseFloat(exprQuantity.quantity); if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) { toastMessageUniq(getPasteHint(hint.numberCan, hintRow)); // bPaste = false; // continue; exprQuantity.quantity = ZhCalc.round(num, 6); } // materialData[colSetting.field] = validText; materialData.expr = exprQuantity.expr; materialData.quantity = exprQuantity.quantity; } if (bPaste) { data.push(materialData); // rowData.push(curRow); } else { SpreadJsObj.reLoadRowData(info.sheet, curRow); } } if (data.length === 0) { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); return; } console.log(data); postData(window.location.pathname + '/save', { type: 'paste', updateData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) { materialListData = result; const [iGclRow, iRow, nRow, sheet, lselect] = leafXmjSpreadObj.getSelect(); gclGatherData[iGclRow].leafXmjs[iRow].jiacha = calcOneBQJC(lselect); calculateJiaCha(gclGatherData, iGclRow); SpreadJsObj.reLoadRowData(sheet, nRow); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), iGclRow); loadXmjMaterialData(iGclRow, nRow); }, function () { SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount); }); }, }; materialSelfSpread.bind(spreadNS.Events.EditStarting, materialSelfSpreadObj.editStarting); materialSelfSpread.bind(spreadNS.Events.EditEnded, materialSelfSpreadObj.editEnded); materialSelfSpread.bind(spreadNS.Events.ClipboardPasted, materialSelfSpreadObj.clipboardPasted); SpreadJsObj.addDeleteBind(materialSelfSpread, materialSelfSpreadObj.deletePress); if (!editForAudit) { $.contextMenu({ selector: '#material-self-spread', build: function ($trigger, e) { const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSelfSpread); return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader; }, items: { 'create': { name: '添加工料', icon: 'fa-sign-in', callback: function (key, opt) { // 获取已选清单 changeMaterialTable('self'); $('#add_type').val('self'); $('#addgl').modal('show'); }, disabled: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); // const notx = findNotJoinLeafXmj(select); if (!select) { return true; } // if (!readOnly && notx === undefined) { // return false; // } else { // return true; // } return readOnly; }, }, 'delete': { name: '删除工料', icon: 'fa-remove', callback: function (key, opt) { materialSelfSpreadObj.del(materialSelfSpread.getActiveSheet()); }, disabled: function (key, opt) { const sheet = materialSelfSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); if (!select) { return true; } if (!readOnly && select && materialSelfBase.isEdit(select, 'del')) { return false; } else { return true; } } }, } }); } } if (!openMaterialChecklist && !editForAudit) { $.contextMenu({ selector: '#material-spread', build: function ($trigger, e) { const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread); return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader; }, items: { 'create': { name: '添加工料', icon: 'fa-sign-in', callback: function (key, opt) { // 获取已选清单 changeMaterialTable(); $('#add_type').val('all'); $('#addgl').modal('show'); }, disabled: function (key, opt) { const sheet = leafXmjSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); // const notx = findNotJoinLeafXmj(select); if (!select) { return true; } // if (!readOnly && notx === undefined) { // return false; // } else { // return true; // } return readOnly; } }, 'delete': { name: '删除工料', icon: 'fa-remove', callback: function (key, opt) { materialSpreadObj.del(materialSpread.getActiveSheet()); }, disabled: function (key, opt) { const sheet = materialSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); if (!select) { return true; } if (!readOnly && select && materialBase.isEdit(select, 'del')) { return false; } else { return true; } } }, } }); } } // 应用调差工料至其他清单明细 $('#user_all_material').click(function () { const ledgerSheet = ledgerSpread.getActiveSheet(); const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet); if (ledgerSelect.leafXmjs.length < 2) { toastr.warning('没有需要应用调差工料的其它清单明细'); return false; } const xmjSheet = leafXmjSpread.getActiveSheet(); const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); console.log(ledgerSelect.leafXmjs); // 判断需要应用调差工料的清单明细 const needAddList = []; for (const xmj of ledgerSelect.leafXmjs) { const notx = findNotJoinLeafXmj(xmj); if (notx === undefined && xmjSelect !== xmj && xmj.gather_qty && xmj.jiacha === null && xmjSelect.jiacha !== null) { needAddList.push(xmj); } } // if (needAddList.length === 0) { // toastr.warning('没有需要应用调差工料的其它清单明细'); // return false; // } console.log(needAddList, materialList); // 更新至服务器 postData(window.location.pathname + '/save', { type:'useOther', postData: { addXmj: needAddList, materialBills: materialList } }, function (result) { materialListData = result.materialListData; gclList = result.gclList; toastr.success('已成功应用'); calculateJiaCha(gclGatherData); const index = gclGatherData.indexOf(ledgerSelect); loadLeafXmjData(index); // const xmjSheet = leafXmjSpread.getActiveSheet(); // const xmjSelect = SpreadJsObj.getSelectObject(xmjSheet); // const xmjIndex = gclGatherData[index].leafXmjs.indexOf(xmjSelect); loadMaterialData(index); SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index); }); }); // 切换清单行,读取所属项目节数据 ledgerSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) { if (info.oldSelections !== undefined) { const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row; if (iNewRow !== iOldRow) { loadLeafXmjData(iNewRow); SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet()); loadMaterialData(iNewRow); loadXmjMaterialData(iNewRow, 0); checkNotJoinMaterialData(); } } }); // 切换项目节数据清单明细行,读取单独的清单工料数据 leafXmjSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) { if (info.oldSelections !== undefined) { const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row; if (iNewRow !== iOldRow) { const sheet = ledgerSpread.getActiveSheet(); const select = SpreadJsObj.getSelectObject(sheet); const index = gclGatherData.indexOf(select); loadXmjMaterialData(index, iNewRow); SpreadJsObj.resetTopAndSelect(materialSelfSpread.getActiveSheet()); } } }); // 显示有调差工料清单 $('#show_material_gcl').click(function () { if ($(this).is(':checked')) { const hadMaterialGclGatherData = []; const hadGclIdList = []; for (const ml of materialListData) { if (hadGclIdList.indexOf(ml.gcl_id) === -1) { hadGclIdList.push(ml.gcl_id); } } for (const gcl of gclGatherData) { for (const index in gcl.leafXmjs) { const gcl_id = gcl.leafXmjs[index].gcl_id; if (hadGclIdList.indexOf(gcl_id) !== -1) { hadMaterialGclGatherData.push(gcl); break; } } } gclGatherData = hadMaterialGclGatherData; } else { if (isStageSelf) { const ms_id = parseInt($('#myTab').find('.active').data('msid')); updateBillsData(ms_id); const i = _.findIndex(materialStageData, { id: ms_id }); gclGatherData = gclGatherListData[i]; } else { gclGatherModel.loadLedgerData(_.cloneDeep(ledger), curLedgerData); gclGatherModel.loadPosData(_.cloneDeep(pos), curPosData); gclGatherData = gclGatherModel.gatherGclData().filter(item => { return item.qc_qty || item.contract_qty || item.qc_minus_qty }); } if (openMaterialChecklist) { // 取交集 gclGatherData = _.filter(gclGatherData, function (item) { return _.find(materialChecklistData, { b_code: item.b_code, name: item.name, unit: item.unit, unit_price: item.unit_price }); }); } } calculateJiaCha(gclGatherData); SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData); loadLeafXmjData(0); loadMaterialData(0); loadXmjMaterialData(0, 0); SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet()); SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet()); SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet()); SpreadJsObj.resetTopAndSelect(materialSelfSpread.getActiveSheet()); checkNotJoinMaterialData(); }); $.subMenu({ menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list', toMenu: '#to-menu', toMiniMenu: '#to-mini-menu', key: 'menu.1.0.0', miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1', callback: function (info) { if (info.mini) { $('.panel-title').addClass('fluid'); $('#sub-menu').removeClass('panel-sidebar'); } else { $('.panel-title').removeClass('fluid'); $('#sub-menu').addClass('panel-sidebar'); } autoFlashHeight(); ledgerSpread.refresh(); leafXmjSpread.refresh(); materialSpread.refresh(); materialSelfSpread.refresh(); } }); $(window).resize(function () { ledgerSpread.refresh(); leafXmjSpread.refresh(); materialSpread.refresh(); materialSelfSpread.refresh(); }); $.divResizer({ select: '#right-spr', callback: function () { ledgerSpread.refresh(); // leafXmjSpread.refresh(); materialSpread.refresh(); const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed(); setLocalCache('material_list_' + materialID, width); } }); $.divResizer({ select: '#right-spr2', callback: function () { leafXmjSpread.refresh(); materialSelfSpread.refresh(); // const width = (($('#right-view2').width()/$('#right-view2').parent('div').width())*100).toFixed(); // setLocalCache('material_list3_' + materialID, width); } }); $.divResizer({ select: '#main-resize', callback: function () { let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0; const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0; $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight); $(".sp-wrap").height(bcontent-30); materialSpread.refresh(); ledgerSpread.refresh(); leafXmjSpread.refresh(); materialSelfSpread.refresh(); const height = $('.bcontent-wrap').height(); setLocalCache('material_list2_' + materialID, height); } }); // 展开收起月信息价并浏览器记住本期展开收起 $('a', '.right-nav').bind('click', function () { //const main = $('#main-view'), tool = $('#tools-view'); const tab = $(this), tabPanel = $(tab.attr('content')); if (!tab.hasClass('active')) { $('a', '.side-menu').removeClass('active'); $('.tab-content .tab-select-show').removeClass('active'); tab.addClass('active'); tabPanel.addClass('active'); showSideTools(tab.hasClass('active')); if (tab.attr('content') === '#material-tab') { const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed(); setLocalCache('material_list_' + materialID, width); } } else { removeLocalCache('material_list_' + materialID); tab.removeClass('active'); tabPanel.removeClass('active'); showSideTools(tab.hasClass('active')); } ledgerSpread.refresh(); // leafXmjSpread.refresh(); materialSpread.refresh(); }); // 根据浏览器记录展开收起 if (getLocalCache('material_list_' + materialID)) { const tab = $('.right-nav a[content="#material-tab"]'), tabPanel = $(tab.attr('content')); $('a', '.side-menu').removeClass('active'); $('.tab-content .tab-select-show').removeClass('active'); tab.addClass('active'); tabPanel.addClass('active'); $('#right-view').css('width', getLocalCache('material_list_' + materialID) + '%'); showSideTools(tab.hasClass('active')); ledgerSpread.refresh(); // leafXmjSpread.refresh(); materialSpread.refresh(); } const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0; if (getLocalCache('material_list2_' + materialID)) { $('.bcontent-wrap').height(getLocalCache('material_list2_' + materialID)); const cHeader = getObjHeight($(".c-header")); const bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0; $(".sp-wrap").height(bcontent-30); $('.sjs-height-1').height($(window).height()-cHeader-bcontent-90+53); $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight); materialSpread.refresh(); ledgerSpread.refresh(); leafXmjSpread.refresh(); materialSelfSpread.refresh(); } else { $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight); ledgerSpread.refresh(); } function getObjHeight(select) { return select.length > 0 ? select.height() : 0; } const showSelfSideTools = function (show) { const left = $('#left-view2'), right = $('#right-view2'), parent = left.parent(); if (show) { right.show(); autoFlashHeight(); /** * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px * 故需要通过最终的parent.width再计算一次left.width * * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width? * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width * */ //left.css('width', parent.width() - right.outerWidth()); //left.css('width', parent.width() - right.outerWidth()); const percent = 100 - right.outerWidth() /parent.width() * 100; left.css('width', percent + '%'); } else { // left.css('width', parent.width()); left.css('width', '100%'); right.hide(); } }; });