| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 | 'use strict';/** * 变更令详细页js * * @author EllisRan. * @date 2018/11/22 * @version */function getPasteHint (str, row = '') {    let returnObj = str;    if (row) {        returnObj.msg = '清单第' + (row+1) + '行' + str.msg;    }    return returnObj;}$(document).ready(() => {    const changeSpreadSetting = {        cols: [            {title: '计价', colSpan: '1', rowSpan: '2', field: 'is_valuation', hAlign: 1, width: 50, cellType: 'checkbox', readOnly: true},            {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true},            {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 120, formatter: '@', readOnly: true},            {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true},            {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 60, type: 'Number', getValue: 'getValue.unit_price', readOnly: true},            {title: '变更部位', colSpan: '1', rowSpan: '2', field: 'bwmx', hAlign: 0, width: 120, formatter: '@', readOnly: true, getValue: 'getValue.bwmx'},            {title: '变更详情', colSpan: '1', rowSpan: '2', field: 'detail', hAlign: 0, width: 120, formatter: '@', readOnly: true},            {title: '计量上限(%)', colSpan: '1', rowSpan: '2', field: 'delimit', hAlign: 2, width: 60, formatter: '@', readOnly: true, visible: openChangeState},            {title: '原设计|数量', colSpan: '2|1', rowSpan: '1|1', field: 'oamount2', hAlign: 2, width: 60, type: 'Number', getValue: 'getValue.oamount2', readOnly: 'readOnly.isSettle'},            {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'oa_tp', hAlign: 2, width: 80, type: 'Number', getValue: 'getValue.oa_tp', readOnly: true},            {title: '申请变更增(+)减(-)|数量', colSpan: '2|1', rowSpan: '1|1', field: 'camount', hAlign: 2, width: 60, type: 'Number', getValue: 'getValue.camount', readOnly: true},            {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'ca_tp', hAlign: 2, width: 80, type: 'Number', getValue: 'getValue.ca_tp', readOnly: true},        ],        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(changeLedgerList, { id: data.gcl_id }) !== -1 || _.findIndex(changePosList, { id: data.mx_id }) !== -1) {                            return '#dc3545';                        }                        if(data.lid != 0) return;                        return '#007bff';                    }                },            },        ],        localCache: {            key: 'changes-spread',            colWidth: true,        }    };    for (const aid of aidList) {        // const userinfo = _.find(auditList2, { 'uid': aid });        const newColcount = {            title: (auditors2[aid - 1] && auditors2[aid - 1].length > 1 ? (aid + '审') : auditors2[aid - 1][0].name) + ' 审批|数量',            colSpan: '2|1', rowSpan: '1|1',            field: 'audit_amount_' + aid,            hAlign: 2, width: 60, type: 'Number', readOnly: _.findIndex(auditors2[aid - 1], { uid: parseInt(accountId) }) === -1 ? true : 'readOnly.isSettle' ,        };        const newColTp = {            title: '|金额',            colSpan: '|1', rowSpan: '|1',            field: 'sa_tp_' + aid,            hAlign: 2, width: 80, type: 'Number',            readOnly: true        };        changeSpreadSetting.cols.push(newColcount);        changeSpreadSetting.cols.push(newColTp);    }    changeSpreadSetting.cols.push({title: '变更后|数量', colSpan: '2|1', rowSpan: '1|1', field: 'samount', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.changed_amount'});    changeSpreadSetting.cols.push({title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'sa_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.changed_tp'});    changeSpreadSetting.getColor = function (sheet, data, row, col, defaultColor) {        return checkIsSettle(data) ? spreadColor.stage.settle : defaultColor;    }    const changeCol = {        getValue: {            unit_price: function(data) {                return ZhCalc.round(data.unit_price, unitPriceUnit);            },            oa_tp: function (data) {                return ZhCalc.round(ZhCalc.mul(ZhCalc.round(data.unit_price, unitPriceUnit), ZhCalc.round(data.oamount2, findDecimal(data.unit))), totalPriceUnit);            },            ca_tp: function (data) {                return ZhCalc.round(ZhCalc.mul(ZhCalc.round(data.unit_price, unitPriceUnit), ZhCalc.round(data.camount, findDecimal(data.unit))), totalPriceUnit);            },            oamount2: function (data) {                return ZhCalc.round(data.oamount2, findDecimal(data.unit));            },            camount: function (data) {                return ZhCalc.round(data.camount, findDecimal(data.unit));            },            changed_amount: function (data) {                return ZhCalc.round(ZhCalc.add(data.oamount2, data.spamount), findDecimal(data.unit));            },            changed_tp: function (data) {                return ZhCalc.add(ZhCalc.round(ZhCalc.mul(ZhCalc.round(data.unit_price, unitPriceUnit), ZhCalc.round(data.oamount2, findDecimal(data.unit))), totalPriceUnit),                    ZhCalc.round(ZhCalc.mul(ZhCalc.round(data.unit_price, unitPriceUnit), ZhCalc.round(data.spamount, findDecimal(data.unit))), totalPriceUnit));            },            bwmx: function (data) {                return data.bwmx ? data.bwmx : (data.xmj_jldy ? data.xmj_jldy : '');            }        },        readOnly: {            isSettle: function (data) {                return checkIsSettle(data);            }        }    };    const changeSpreadObj = {        makeSjsFooter: function () {            // 增加汇总行并设为锁定禁止编辑状态            changeSpreadSheet.addRows(changeSpreadSheet.getRowCount(), 1);            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, 0, '合计');            const cellType1 = new GC.Spread.Sheets.CellTypes.Text();            changeSpreadSheet.getCell(changeSpreadSheet.getRowCount() - 1, 0).cellType(cellType1);            changeSpreadSheet.setStyle(changeSpreadSheet.getRowCount() - 1, -1, style1);            changeSpreadObj.countSum();        },        setAuditValue: function () {            for (const c  of changeList) {                for (const j of aidList) {                    c['audit_amount_' + j] = ZhCalc.round(c['audit_amount_' + j], findDecimal(c.unit));                    c['sa_tp_' + j] = ZhCalc.round(ZhCalc.mul(c['audit_amount_' + j], ZhCalc.round(c.unit_price, unitPriceUnit)), totalPriceUnit);                }            }            // const rowCount = changeSpreadSheet.getRowCount();            // // 用户的数据合计            // for (const j in aidList) {            //     for(let i = 0; i <= rowCount - 1; i++){            //         const data = {            //             unit_price: changeSpreadSheet.getValue(i, 5),            //             amount: parseFloat(changeSpreadSheet.getValue(i, 10 + parseInt(j)*2)),            //         };            //         const sum = ZhCalc.round(ZhCalc.mul(data.unit_price, data.amount), totalPriceUnit);            //         changeSpreadSheet.setValue(i, 11 + j*2, sum !== 0 ? sum : null);            //     }            // }        },        resetXmjSpread: function(data = null) {            const xmj = [];            if (data && data.lid != 0 && data.xmj_code !== '' && data.xmj_code !== null) {                if (data.bwmx === data.xmj_jldy) {                    data.bwmx = '';                }                xmj.push(data);            }            SpreadJsObj.loadSheetData(xmjSpread.getActiveSheet(), SpreadJsObj.DataType.Data, xmj);        },        selectionChanged: function (e, info) {            const sel = info.sheet.getSelections()[0];            const col = info.sheet.zh_setting.cols[sel.col];            const data = SpreadJsObj.getSelectObject(info.sheet);            if (col && col.field === 'camount' && data) {                $('#camount-expr').val(data.camount_expr ? data.camount_expr : data.camount);            } else {                $('#camount-expr').val('');            }            changeSpreadObj.resetXmjSpread(data);        },        setRowValueAndSum: function (data, row, col) {            const oneSum = ZhCalc.round(ZhCalc.mul(data.unit_price, parseFloat(changeSpreadSheet.getValue(row, startLimit - 1))), totalPriceUnit);            changeSpreadSheet.setValue(row, startLimit, oneSum !== 0 ? oneSum : null);            for (const j in aidList) {                const sum = ZhCalc.round(ZhCalc.mul(data.unit_price, parseFloat(changeSpreadSheet.getValue(row, startLimit + 3 + parseInt(j)*2))), totalPriceUnit);                changeSpreadSheet.setValue(row, startLimit + 4 + j*2, sum !== 0 ? sum : null);            }            // const sum = ZhCalc.round(ZhCalc.mul(data.unit_price, data.spamount), totalPriceUnit);            // changeSpreadSheet.setValue(row, col+1, sum !== 0 ? sum : null);            const rowCount = changeSpreadSheet.getRowCount();            // 用户的数据合计            // 变更后数据合计            let audit_sum = 0;            let oSum = 0;            let changed_sum = 0;            for(let i = 0; i < rowCount - 1; i++){                audit_sum = ZhCalc.add(audit_sum, changeSpreadSheet.getValue(i, col+1));                oSum = ZhCalc.add(oSum, changeSpreadSheet.getValue(i, startLimit));                changed_sum = ZhCalc.add(changed_sum, changeSpreadSheet.getValue(i, (startLimit + 4 + aidList.length*2)));            }            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, col+1, audit_sum !== 0 ? audit_sum : null);            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, startLimit, oSum !== 0 ? oSum : null);            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, (startLimit + 4 + aidList.length*2), changed_sum !== 0 ? changed_sum : null);        },        countSum: function() {            const rowCount = changeSpreadSheet.getRowCount();            let oSum = 0,                cSum = 0,                cdSum = 0;            for(let i = 0; i < rowCount - 1; i++){                oSum = ZhCalc.add(oSum, changeSpreadSheet.getValue(i, startLimit));                cSum = ZhCalc.add(cSum, changeSpreadSheet.getValue(i, startLimit + 2));                cdSum = ZhCalc.add(cdSum, changeSpreadSheet.getValue(i, (startLimit + 4 + aidList.length*2)));            }            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, startLimit, oSum !== 0 ? oSum : null);            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, startLimit + 2, cSum !== 0 ? cSum : null);            changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, (startLimit + 4 + aidList.length*2), cdSum !== 0 ? cdSum : null);            // 用户的数据合计            for (const j in aidList) {                let audit_sum = 0;                for(let i = 0; i < rowCount - 1; i++){                    audit_sum = ZhCalc.add(audit_sum, changeSpreadSheet.getValue(i, startLimit + 4 + j*2));                }                changeSpreadSheet.setValue(changeSpreadSheet.getRowCount() - 1, startLimit + 4 + j*2, audit_sum !== 0 ? audit_sum : null);            }        },        deletePress: function (sheet) {            return;        },        editEnded: function (e, info) {            if (info.sheet.zh_setting) {                const select = SpreadJsObj.getSelectObject(info.sheet);                const col = info.sheet.zh_setting.cols[info.col];                // 未改变值则不提交                let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : '');                const orgValue = select[col.field];                if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {                    SpreadJsObj.reLoadRowData(info.sheet, info.row);                    changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                    return;                }                // 判断部分值是否输入的是数字判断和数据计算                if (col.type === 'Number') {                    if (isNaN(validText)) {                        toastr.error('不能输入其它非数字类型字符');                        SpreadJsObj.reLoadRowData(info.sheet, info.row);                        changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                        return;                    }                    validText = ZhCalc.round(validText, findDecimal(select.unit)) || 0;                    if (col.field !== 'oamount2') {                        // 判断是否 正数必须大于等于限制值,负数必须小于等于限制值,否则无法更改                        const usedInfo = _.find(changeUsedData, {cbid: select.id});                        if (usedInfo && validText >= 0 && validText < usedInfo.qty) {                            toastr.error('清单变更数值必须大于等于已调用值 ' + usedInfo.qty);                            SpreadJsObj.reLoadRowData(info.sheet, info.row);                            changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                            return;                        } else if (usedInfo && validText < 0 && validText > usedInfo.qty) {                            toastr.error('清单变更数值必须小于等于已调用值 ' + usedInfo.qty);                            SpreadJsObj.reLoadRowData(info.sheet, info.row);                            changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                            return;                        }                    }                }                select[col.field] = validText;                if (col.field !== 'oamount2') select.spamount = ZhCalc.round(validText, findDecimal(select.unit)) || 0;                const data = {                    id: select.id,                };                if (col.field === 'oamount2') {                    data[col.field] = select[col.field];                } else {                    data.spamount = select.spamount;                }                console.log(data, select);                // 更新至服务器                postData(window.location.pathname + '/save', { type:'update', updateData: data }, function (result) {                    changeList.splice(info.row, 1, select);                    SpreadJsObj.reLoadRowData(info.sheet, info.row);                    changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                }, function () {                    select[col.field] = orgValue;                    if (col.field !== 'oamount2') select.spamount = orgValue;                    SpreadJsObj.reLoadRowData(info.sheet, info.row);                    changeSpreadObj.setRowValueAndSum(select, info.row, info.col);                });            }        },        clipboardPasted(e, info) {            const hint = {                cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},                numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},                qtyError: {type: 'error', msg: '变更数值必须大于等于已调用值'},            };            const range = info.cellRange;            const sortData = info.sheet.zh_data || [];            // console.log(info, range);            // if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {            //     toastMessageUniq(hint.cellError);            //     // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);            //     SpreadJsObj.reLoadSheetHeader(changeSpreadSheet);            //     SpreadJsObj.reLoadSheetData(changeSpreadSheet);            //     changeSpreadObj.setAuditValue();            //     changeSpreadObj.makeSjsFooter();            //     return;            // }            // if (sortData.length > 0 && range.col + range.colCount > 10) {            //     toastMessageUniq(hint.cellError);            //     SpreadJsObj.reLoadSheetHeader(changeSpreadSheet);            //     SpreadJsObj.reLoadSheetData(changeSpreadSheet);            //     changeSpreadObj.setAuditValue();            //     changeSpreadObj.makeSjsFooter();            //     return;            // }            const data = [];            // const rowData = [];            for (let iRow = 0; iRow < range.rowCount; iRow++) {                let bPaste = true;                const curRow = range.row + iRow;                // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));                const cLData = { id: sortData[curRow].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 orgValue = sortData[curRow][colSetting.field];                    if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {                        sameCol++;                        if (range.colCount === sameCol)  {                            bPaste = false;                        }                        continue;                    }                    if (colSetting.type === 'Number') {                        if (isNaN(validText)) {                            toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));                            bPaste = false;                            continue;                        }                        validText = ZhCalc.round(validText, findDecimal(sortData[curRow].unit)) || 0;                        if (colSetting.field !== 'oamount2') {                            // 判断是否 正数必须大于等于限制值,负数必须小于等于限制值,否则无法更改                            const usedInfo = _.find(changeUsedData, {id: sortData[curRow].id});                            if (usedInfo && usedInfo.qty >= 0 && validText < usedInfo.qty) {                                toastr.error(hintRow ? '清单' + (hintRow + 1) + '行变更数值必须大于等于已调用值 ' + usedInfo.qty : '清单变更数值必须大于等于已调用值 ' + usedInfo.qty);                                SpreadJsObj.reLoadRowData(info.sheet, curRow);                                changeSpreadObj.setRowValueAndSum(sortData[curRow], curRow, curCol);                                bPaste = false;                                continue;                            } else if (usedInfo && usedInfo.qty < 0 && validText > usedInfo.qty) {                                toastr.error(hintRow ? '清单' + (hintRow + 1) + '行变更数值必须小于等于已调用值 ' + usedInfo.qty : '清单变更数值必须小于等于已调用值 ' + usedInfo.qty);                                SpreadJsObj.reLoadRowData(info.sheet, curRow);                                changeSpreadObj.setRowValueAndSum(sortData[curRow], curRow, curCol);                                bPaste = false;                                continue;                            }                        }                    }                    // cLData[colSetting.field] = validText;                    sortData[curRow][colSetting.field] = validText;                    if (colSetting.field === 'oamount2') {                        cLData[colSetting.field] = validText;                    } else {                        cLData.spamount = validText;                    }                }                if (bPaste) {                    data.push(cLData);                    // 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_amount_rows', updateData: data }, function (result) {                changeList = result;                changeSpreadObj.setAuditValue();                SpreadJsObj.loadSheetData(changeSpreadSheet, SpreadJsObj.DataType.Data, changeList);                changeSpreadObj.makeSjsFooter();                changeSpreadObj.resetXmjSpread(SpreadJsObj.getSelectObject(changeSpreadSheet));            }, function () {                changeSpreadObj.setAuditValue();                SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);                return;            });        },    };    SpreadJsObj.initSpreadSettingEvents(changeSpreadSetting, changeCol);    SpreadJsObj.initSheet(changeSpreadSheet, changeSpreadSetting);    const preUrl = window.location.pathname.split('/').slice(0, 4).join('/');    let changeListData;    let gclGatherData;    postData(preUrl + '/defaultBills', {}, function (result) {        gclGatherModel.loadLedgerData(result.bills);        gclGatherModel.loadPosData(result.pos);        gclGatherData = gclGatherModel.gatherGclData();        gclGatherData = _.filter(gclGatherData, function (item) {            return item.leafXmjs && item.leafXmjs.length !== 0;        });        for (const ggd in gclGatherData) {            if (gclGatherData[ggd].leafXmjs && gclGatherData[ggd].leafXmjs.length === 0) {                gclGatherData.splice(ggd, 1);            }            gclGatherData[ggd].code = gclGatherData[ggd].b_code;            let hadcid = 0;            for (const xmj of gclGatherData[ggd].leafXmjs) {                const changeLedger = _.find(changeLedgerList, { id: xmj.gcl_id });                const changePos = _.find(changePosList, { id: xmj.mx_id, lid: xmj.gcl_id });                if (changeLedger || changePos) {                    xmj.cid = 1;                    xmj.ccid = changeLedger ? changeLedger.ccid : changePos.ccid;                    hadcid = 1;                }            }            if (hadcid !== 0) gclGatherData[ggd].cid = 1;        }        console.log(gclGatherData);        // 数组去重        const dealBillList = result.dealBills;        for (const db of gclGatherData) {            const exist_index = dealBillList.findIndex(function (item) {                return item.code === db.code && item.name === db.name && item.unit === db.unit && item.unit_price === db.unit_price;            });            if (exist_index !== -1) {                dealBillList.splice(exist_index, 1);            }        }        changeListData = gclGatherData.concat(dealBillList).sort(sortByCode);        checkAndChange(changeListData);        changeSpreadObj.setAuditValue();        SpreadJsObj.loadSheetData(changeSpreadSheet, SpreadJsObj.DataType.Data, changeList);        console.log(changeList);        changeSpreadObj.makeSjsFooter();        changeSpreadObj.resetXmjSpread(SpreadJsObj.getSelectObject(changeSpreadSheet));    });    const userIndex = aidList.indexOf(parseInt(accountId));    changeSpread.bind(spreadNS.Events.EditEnded, changeSpreadObj.editEnded);    changeSpread.bind(spreadNS.Events.ClipboardPasted, changeSpreadObj.clipboardPasted);    changeSpread.bind(spreadNS.Events.SelectionChanged, changeSpreadObj.selectionChanged);    SpreadJsObj.addDeleteBind(changeSpread, changeSpreadObj.deletePress);    // 审批提交与判断    $('.approval-btn').on('click', function () {        // 判断审批状态        let returnflag = true;        const pData = {            change_id: $('#changeId').val(),            w_code: $.trim($('#w_code').val()),        }        if ($(this).hasClass('btn-success')) {            const sdesc = $('#success-approval').find('textarea').val();            if (sdesc === '') {                toastr.error('审批意见不能为空!');                returnflag = false;            }            if ($('input[name="p_code"]').val() === '') {                toastr.error('变更令号(批复编号)不能为空!');                returnflag = false;            } else if ($('input[name="p_code"]').val() !== undefined) {                $('input[name="p_code"]').val($.trim($('input[name="p_code"]').val()));                const postData2 = {                    p_code: $('input[name="p_code"]').val(),                };                postDataWithAsync('/tender/' + $('#tenderId').val() + '/change/' + $('#changeId').val() + '/check/codeRepeat',postData2, function (result) {                }, function (data) {                    returnflag = false;                });                pData.p_code = $('input[name="p_code"]').val();            }            if(returnflag) {                $('input[name="w_code"]').val($.trim($('#w_code').val()));                $('#success-approval').find('textarea').val(sdesc.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' '));                if ($('#warning-text').length) $('#warning-text').remove();                // $('#success-approval').submit();                pData.status = auditConst.status.checked;                pData.sdesc = sdesc.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');            }        } else {            const sdesc = $('#fail-approval').find('textarea').val();            if (sdesc === '') {                toastr.error('审批意见不能为空!');                returnflag = false;            }            const type = $('#fail-approval').find('input[name="status"]:checked').val();            if (type === undefined) {                // toastr.error('请选择退回类型!');                if (!$('#warning-text').length) {                    $('#change-back-content').append('<p id="warning-text" style="color: red; margin: 0;">请选择退回流程</p>');                }                returnflag = false;            }            if(returnflag) {                $('#fail-approval').find('textarea').val(sdesc.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' '));                $('input[name="w_code"]').val($.trim($('#w_code').val()));                // $('#fail-approval').submit();                pData.status = type;                pData.sdesc = sdesc.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');            }        }        if (pData.status) {            console.log(pData);            postData('/tender/' + $('#tenderId').val() + '/change/approval', pData, function (result) {                window.location.reload();            });        }    })});const postDataWithAsync = function (url, data, successCallback, errorCallBack, showWaiting = true) {    if (showWaiting) showWaitingView();    $.ajax({        type:"POST",        url: url,        data: {'data': JSON.stringify(data)},        dataType: 'json',        cache: false,        async: false,        timeout: 60000,        beforeSend: function(xhr) {            let csrfToken = Cookies.get('csrfToken_j');            xhr.setRequestHeader('x-csrf-token', csrfToken);        },        success: function(result){            if (result.err === 0) {                if (successCallback) {                    successCallback(result.data);                }            } else {                toastr.error(result.msg);                if (errorCallBack) {                    errorCallBack(result.msg);                }            }            if (showWaiting) closeWaitingView();        },        error: function(jqXHR, textStatus, errorThrown){            toastr.error('error: ' + textStatus + " " + errorThrown);            if (errorCallBack) {                errorCallBack();            }            if (showWaiting) closeWaitingView();        }    });};function auditCheck(i) {    const inlineRadio1 = $('#change-back:checked').val()    const inlineRadio2 = $('#chagne-backnew:checked').val()    const opinion = $('textarea[name="sdesc"]').eq(i).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');    $('textarea[name="sdesc"]').eq(i).val(opinion);    if (i === 1) {        if (!inlineRadio1 && !inlineRadio2) {            if (!$('#warning-text').length) {                $('#reject-process').prepend('<p id="warning-text" style="color: red; margin: 0;">请选择退回流程</p>');            }            return false;        }        if ($('#warning-text').length) $('#warning-text').remove()    }    return true;}function checkAndChange(changeListData) {    // 根据已添加的清单显示    if (changeList.length > 0 && changeList[0]) {        const removeList = [];        const updateList = [];        const updateGclIdList = [];        for (const [index,clinfo] of changeList.entries()) {            if (clinfo.lid != 0) {                let listinfo = changeListData.find(function (item) {                    return (item.id !== undefined && item.id == clinfo.lid) || (item.id === undefined && item.leafXmjs !== undefined && item.leafXmjs.length !== 0 && item.leafXmjs[0].gcl_id == clinfo.lid);                });                if (listinfo === undefined || (clinfo.lid && clinfo.gcl_id && clinfo.lid !== clinfo.gcl_id)) {                    // 有可能这部分台账发生变化,此时要更新清单lid信息,防止数据丢失                    const newlistinfo = changeListData.find(function (item) {                        return (item.id !== undefined && item.id == clinfo.gcl_id) || (item.id === undefined && item.leafXmjs !== undefined && item.leafXmjs.length !== 0 && _.find(item.leafXmjs, {gcl_id: clinfo.gcl_id }));                    });                    if ((listinfo === undefined && newlistinfo) || (listinfo && newlistinfo && !isObjEqual(listinfo, newlistinfo))) {                        listinfo = newlistinfo;                        updateList.push({id: clinfo.id, lid: newlistinfo.leafXmjs[0].gcl_id});                        // 更新lid                        changeList[index].lid = newlistinfo.leafXmjs[0].gcl_id;                    }                }                if (listinfo === undefined) {                    // 针对旧数据获取清单信息                    listinfo = changeListData[clinfo.lid - 1];                    if (listinfo === undefined) {                        toastr.warning('台账清单列表已不存在'+ clinfo.code +',已更新变更清单列表');                        if (changeStatus !== auditConst.status.revise) {                            removeList.push(clinfo);                        } else {                            updateList.push(makeWhiteList(clinfo));                        }                        continue;                    }                }                const info = makePushBwmx(clinfo, listinfo, removeList, updateList);                if (info && _.findIndex(changeLedgerList, { id: clinfo.gcl_id }) !== -1) {                    // 可能因为升降级关系:细目,分部分项等会发生变化,更新清单                    const updateInfo = {};                    if (info.code !== clinfo.xmj_code) {                        updateInfo.xmj_code = info.code;                        changeList[index].xmj_code = info.code;                    }                    if (info.jldy !== clinfo.xmj_jldy) {                        updateInfo.xmj_jldy = info.jldy;                        changeList[index].xmj_jldy = info.jldy;                    }                    if (info.dwgc !== clinfo.xmj_dwgc) {                        updateInfo.xmj_dwgc = info.dwgc;                        changeList[index].xmj_dwgc = info.dwgc;                    }                    if (info.fbgc !== clinfo.xmj_fbgc) {                        updateInfo.xmj_fbgc = info.fbgc;                        changeList[index].xmj_fbgc = info.fbgc;                    }                    if (info.fxgc !== clinfo.xmj_fxgc) {                        updateInfo.xmj_fxgc = info.fxgc;                        changeList[index].xmj_fxgc = info.fxgc;                    }                    if (!_.isEmpty(updateInfo) && _.indexOf(updateGclIdList, clinfo.gcl_id) === -1) {                        updateGclIdList.push(clinfo.gcl_id);                        // updateInfo.gcl_id = info.id;                        updateList.push({ row: updateInfo, where: { tid: tenderId, gcl_id: clinfo.gcl_id } });                    }                }            }        }        if(updateList.length > 0) {            console.log(updateList);            postData(window.location.pathname + '/save', { type:'update_list', updateData: updateList }, function (result) {            }, function () {            });        }        if(removeList.length > 0) {            _.pullAll(changeList, removeList);            postData(window.location.pathname + '/save', { type:'remove_list', updateData: removeList }, function (result) {            }, function () {            });        }    }}function makePushBwmx(clinfo, listinfo, removeList, updateList) {    let info = '';    const checkKey = ['name', 'code', 'unit', 'unit_price'];    const checkLeafKey = ['oamount', 'bwmx', 'code', 'dwgc', 'fbgc', 'fxgc', 'jldy'];    if (listinfo.leafXmjs !== undefined) {        const leafInfo = listinfo.leafXmjs.find(function (item) {            // const flag = (item.bwmx === undefined || item.bwmx === clinfo.bwmx || item.jldy === clinfo.bwmx) && item.gcl_id === clinfo.gcl_id && (!clinfo.mx_id || (item.mx_id && clinfo.mx_id && item.mx_id === clinfo.mx_id)) && (item.quantity !== null ? item.quantity === parseFloat(clinfo.oamount) : 0 === parseFloat(clinfo.oamount));            const flag = item.gcl_id === clinfo.gcl_id && (!clinfo.mx_id || (item.mx_id && clinfo.mx_id && item.mx_id === clinfo.mx_id));            if (flag && item.code === clinfo.xmj_code) {                return flag && item.code === clinfo.xmj_code;            }            return flag;        });        if (leafInfo) {            const oneUpdate = { id: clinfo.id };            let needUpdate = false;            // 判断要不要更新名称,单位,原数量,单价            checkKey.forEach(function (key) {                if (listinfo[key] !== clinfo[key]) {                    oneUpdate[key] = listinfo[key];                    clinfo[key] = listinfo[key];                    if (key === 'unit') {                        const changeKey = ['oamount', 'oamount2', 'camount'];                        changeKey.forEach(function (key) {                            const value = ZhCalc.round(clinfo[key], findDecimal(listinfo[key]));                            if (value !== clinfo[key]) {                                oneUpdate[key] = value;                                clinfo[key] = value;                            }                        });                    }                    // else if (key === 'unit_price') {                    //     // 可能要更新总金额了                    //     oneUpdate[key] = ZhCalc.round(listinfo[key], unitPriceUnit);                    //     clinfo[key] = ZhCalc.round(listinfo[key], unitPriceUnit);                    // }                    needUpdate = true;                }            });            checkLeafKey.forEach(function (key) {                // 只有数量是对比leafInfo,其它对比listinfo,且有些值需要重新计算                if (key === 'oamount') {                    if (leafInfo.quantity !== clinfo[key]) {                        oneUpdate[key] = leafInfo.quantity;                        clinfo[key] = leafInfo.quantity;                        needUpdate = true;                    }                } else if (key === 'bwmx') {                    if (leafInfo[key] !== undefined && leafInfo[key] !== clinfo[key]) {                        oneUpdate[key] = leafInfo[key];                        clinfo[key] = leafInfo[key];                        needUpdate = true;                    } else if (leafInfo[key] === undefined && leafInfo.jldy !== clinfo[key]) {                        oneUpdate[key] = leafInfo.jldy;                        clinfo[key] = leafInfo.jldy;                        needUpdate = true;                    }                } else {                    if (leafInfo[key] !== clinfo['xmj_' + key]) {                        oneUpdate['xmj_' + key] = leafInfo[key];                        clinfo['xmj_' + key] = leafInfo[key];                        needUpdate = true;                    }                }            });            if (needUpdate) {                updateList.push(oneUpdate);            }            info = leafInfo;        } else {            toastr.warning('台账清单列表已不存在'+ clinfo.code +',已更新变更清单列表');            if (changeStatus !== auditConst.status.revise) {                removeList.push(clinfo);            } else {                updateList.push(makeWhiteList(clinfo));            }            return false;        }    }    return info;}function isObjEqual(o1,o2){    var props1 = Object.getOwnPropertyNames(o1);    var props2 = Object.getOwnPropertyNames(o2);    if (props1.length != props2.length) {        return false;    }    for (var i = 0,max = props1.length; i < max; i++) {        var propName = props1[i];        if (o1[propName] !== o2[propName]) {            return false;        }    }    return true;}
 |