SOURCE

var 产品表 = dataOper.产品表;
//定义结果
var res = [];
var helper = {
    getPrdInfoBySupSpc: function () {
        return { clsId: 1, prdId: 2 }
    },
    getCustByName: function () {
        return { cusId: 1, cusName: "22" }
    }
}
function 设置产品MIS属性(产品, tag) {
    var 对方货号 = 产品.对方货号;
    var prdt = helper.getPrdInfoBySupSpc(对方货号);
    if (prdt == null) {
        var msg = tag + " 对应的产品编码[" + 对方货号 + "]在MIS系统中未对应设置!";
        throw msg;
    }
    产品.clsId = prdt.clsId;
    产品.prdId = prdt.prdId;
    产品.prdType = prdt.prdType;
    产品.ut = prdt.prdUt;
    产品.upType = prdt.upType;
    产品.qtyType = prdt.qtyType;
}

//添加新的明细
function 添加新明细行(roomName, part) {
    var dtl = {
        roomName: roomName,
        clsId: part.clsId,
        prdId: part.prdId,
        prdName: part.产品名称,
        prdSpc: part.产品型号,

        wth: Number(part.宽),
        hgt: Number(part.高),
        thk: Number(part.厚),
        dtls: [],
        qty_q: part.数量 == null ? 1 : Number(part.数量),
        prdType: part.prdType,
        upType: part.upType,
        qtyType: part.qtyType,
    };
    res.push(dtl);
    return dtl;
}

//油漆板的特殊处理
function checkIsYouQiBan(subDtl) {
    if (subDtl.matName != null && subDtl.matName.indexOf("YQ-") > -1) {
        return true;
    }
}
//添加油漆板
function dealWithYouQiban(parDtl, panel) {
    // 将subDtl写入res中通过addNewDtl方法
    panel.PartNumber = "YQGTB";
    setPartPrdId(panel, panel.Name);
    var dtl = {
        roomName: parDtl.roomName,
        clsId: panel.clsId,
        prdId: panel.prdId,
        prdName: panel.Name,
        prdSpc: "",
        rem: panel.CabinetPanelNo,
        //prdSpc:part.CabinetPanelNo,
        wth: Number(panel.Width),
        hgt: Number(panel.Length),
        thk: Number(panel.Thickness),
        barCode: panel.barCode,
        qty_q: 1,
        prdType: panel.prdType,
        upType: panel.upType,
        qtyType: panel.qtyType,
    };
    //材质的处理
    try {
        mat = getMat(dtl.clsId, panel.BasicMaterial);
        if (mat == null) {
            throw "材质不存在!"
        }
    } catch (ef) {
        var msg = dtl.prdName + ",编号:[" + panel.CabinetPanelNo + "]的材料[" + panel.BasicMaterial + "]在MIS系统中不存在";
        throw msg;
    }
    dtl.matId = mat.matId;
    dtl.matName = mat.matName;
    res.push(dtl);
}

function getMat(clsId, matName, thk) {

    var mat = helper.getFMatInfo(clsId, matName);
    if (mat == null) {
        var fmatName = thk + "mm" + matName;
        mat = helper.getFMatInfo(clsId, fmatName);
        if (mat == null) {
            throw "材质[" + matName + "]或材质[" + fmatName + "]在系统中没有建档案"
        }
    }
    return mat;
}
//根据基材,颜色取材质
function getMatByClr(clrName, baseMat) {
    var mats = helper.getMatListByClr(clrName);
    if (mats.size() == 0) {
        return null;
    } else if (mats.size() == 1) {
        return mats.get(0);
    } else {
        for (var i = 0; i < mats.size(); i++) {
            var matName = mats.get(i).matName;
            if (matName.indexOf(baseMat) > -1) {
                return mats.get(i);
            }
        }
        return null;
    }
}
//设置分解计价的单价
function 设置子表行单价(clsId, subDtl) {
    var mat = null;
    //颜色subDtl.clr
    mat = getMatByClr(subDtl.clr, subDtl.matName);

    if (mat == null) {
        var msg = subDtl.prdName + ",编号:[" + subDtl.ID + "]的颜色[" + subDtl.clr + "],在MIS系统中不存在,此板件材料为:[" + subDtl.matName + "]";
        throw msg;
    }
    subDtl.matId = mat.matId;
    //单价  mat.upAddDef;0-9:180;10-18:200;
    //throw mat.upAddDef;
    var up = helper.getStepByVal(mat.upAddDef, subDtl.thk);
    subDtl.up = Number(up);
    subDtl.amt = subDtl.qty_a * subDtl.up;
    subDtl.amt = Math.round(subDtl.amt * 10000) / 10000;
}
function setDtlMatUp(dtl, prdt, mat, mp) {
    dtl.up = mat.matUp;

}

//处理门板的部分
function dealWithMbPanel(subDtl, panel, parDtl) {

    if (subDtl.partNumber == null) {
        throw subDtl.prdName + "的partNumber未设定"
    }
    if (subDtl.partNumber.indexOf("-") == -1) {
        throw subDtl.prdName + "的partNumber:" + subDtl.partNumber + "设定方式必须为[基础型号-门片型号]";
    }
    //var f=subDtl.partNumber.split("-");
    //var supSpc=f[0];
    //var mpSpc=f[1];
    //var prdt=helper.getFPrdInfoBySupSpc(0,supSpc);

    //L改动部分  A-B-C

    var s = subDtl.partNumber;
    var pos = s.lastIndexOf("-");
    var supSpc = s.substr(0, pos);
    var mpSpc = s.substr(pos + 1, s.length - pos - 1);
    var prdt = helper.getFPrdInfoBySupSpc(0, supSpc);



    if (prdt == null) {
        var msg = subDtl.prdName + "的partNumber[" + subDtl.partNumber + "]对应的基础型号[" +
            supSpc + "]在MIS系统中不存在";
        throw msg;
    }
    var mp = helper.getFMpInfoBySupSpc(prdt.clsId, mpSpc);
    if (mp == null) {
        var msg = subDtl.prdName + "的partNumber[" + subDtl.partNumber + "]对应的门片型号[" +
            mpSpc + "]在MIS系统中不存在";
        throw msg;
    }
    var mat = null;
    try {
        var mat = getMat(parDtl.clsId, "免漆柜体");
        // if(prdt.clsId==6 ){
        //     mat=getMat(prdt.clsId,subDtl.matName);
        // }else if(prdt.clsId==211 ){
        //     mat=getMat(prdt.clsId,subDtl.matName);
        // }else if (prdt.clsId==7){
        //     mat=helper.getMatByClr(subDtl.clr);
        // }else if(prdt.clsId==8){
        //     mat=getMatByClr(subDtl.clr,subDtl.matName);
        // }
        // if(mat==null){
        //     throw "材质不存在!"
        // }
    } catch (ef) {
        var msg = subDtl.prdName + ",编号:[" + subDtl.ID + "]的材料[" + subDtl.matName + "]在MIS系统中不存在";
        throw msg;
    }

    subDtl.kw = panel.DoorDirection;
    subDtl.rem = panel.CabinetPanelNo;
    subDtl.prdId = prdt.prdId;
    subDtl.mpId = mp.mpId;
    subDtl.matId = mat.matId;
    //subDtl.matName=mat.matName;
    subDtl.prdType = prdt.prdType;
    subDtl.upType = prdt.upType;
    subDtl.qtyType = prdt.qtyType;
    subDtl.roomName = parDtl.roomName;
    subDtl.clsId = prdt.clsId;
    //setDtlMatUp(subDtl,prdt,mat,mp);
    res.push(subDtl);
}


//if(panel.PartNumber=="gtb"){
//处理子表板件的部分
function 处理部件列表(明细行, 源子表行) {
    //柜体板写入到柜体主键
    var 新子表行 = {
        itm: 明细行.dtls.length + 1,
        prdName: 源子表行.产品名称,
        matName: 源子表行.基材,
        clr: 源子表行.颜色,
        wth: Number(源子表行.宽),
        hgt: Number(源子表行.高),
        thk: Number(源子表行.厚),
        partNumber: 源子表行.对方货号,
        barCode: 源子表行.条码,
        qty_q: 1,
        ut: "平方",
        ID: 源子表行.id,
        rem: 源子表行.备注
    }
    //console.log(新子表行);
    新子表行.qty_a = 新子表行.wth * 新子表行.hgt * 0.001 * 0.001;
    新子表行.qty_a = Math.round(新子表行.qty_a * 10000) / 10000;
    if (源子表行.类型 == "门板") {
        dealWithMbPanel(subDtl, panel, parDtl);
    } else if (源子表行.类型 == "五金") {

    } else {
        var 材质 = getMat(明细行.clsId, "免漆柜体");
        新子表行.matId = 材质.matId;
        if (明细行.upType == 5) {
            设置子表行单价(明细行.clsId, 新子表行);
            明细行.amt = 明细行.amt == null ? 新子表行.amt : (明细行.amt + 新子表行.amt);
        }
        明细行.dtls.push(新子表行);
    }
}

//处理五金的部分
function dealWithWj(part, parDtl) {
    var dtl = {
        roomName: parDtl.roomName,
        prdName: part.Name,
        prdSpc: part.PartNumber,
        wth: Number(part.Length),
        hgt: Number(part.Height),
        thk: Number(part.Width),
        qty_q: Number(part.Num),
        qty_a: Number(part.Num),
        PartNumber: part.PartNumber,
        rem: part.FXFA,
        matName: part.relatedMaterial
    };
    dtl.PartNumber = "WJ";
    setPartPrdId(dtl, part.Name);
    dtl.clsId = parDtl.clsId;
    res.push(dtl);

}

//循环产品表
for (var i = 0; i < 产品表.length; i++) {
    var 产品项 = 产品表[i];
    设置产品MIS属性(产品项, 产品项.产品名称);
    if (产品项.类型 == "门板") {

    } else if (产品项.类型 == "五金") {

    } else if (产品项.类型 == "附件") {

    } else {
        var 明细行 = 添加新明细行(产品项.房间, 产品项, []);
        console.log(明细行);
        for (var j = 0; j < 产品项.部件列表.length; j++) {
            处理部件列表(明细行, 产品项.部件列表[j]);
        }
        if (明细行.upType == 5) {
            明细行.amt = Math.round(明细行.amt * 100) / 100
        }
        for (var j = 0; j < unit.Metals.length; j++) {
            dealWithWj(unit.Metals[j], dtl);
        }
    }
 
}

var groupDtl = [{
    clsId: 323,
    roomlist: [
        {
            roomName: "厨房",
            dtls: []
        },
        {
            roomName: "次卧",
            dtls: []
        }]
}];
groupDtl = [];
function setGroupDtl(dtl) {
    var clsId = dtl.clsId;
    var roomName = dtl.roomName;
    var clsObj = null;
    var roomObj = null;
    for (var i = 0; i < groupDtl.length; i++) {
        if (groupDtl[i].clsId == clsId) {
            clsObj = groupDtl[i];
            break;
        }
    }
    if (clsObj == null) {
        clsObj = { clsId: clsId, roomlist: [] };
        groupDtl.push(clsObj);
    }
    for (var i = 0; i < clsObj.roomlist.length; i++) {
        if (clsObj.roomlist[i].roomName == roomName) {
            roomObj = clsObj.roomlist[i];
            break;
        }
    }
    if (roomObj == null) {
        roomObj = { roomName: roomName, dtls: [] };
        clsObj.roomlist.push(roomObj);
    }
    roomObj.dtls.push(dtl);

}
//对结果分组
for (var i = 0; i < res.length; i++) {
    var dtl = res[i];
    setGroupDtl(dtl);
}
//索引订单号;

var cusName = dataOper.客户;
var cusrName = dataOper.终端客户;
var cust = helper.getCustByName(cusName);
if (cust == null) {
    throw "客户名称[" + cusName + "]在MIS系统中未建档!";
}

//设定已经导入的订单分类
for (var i = 0; i < groupDtl.length; i++) {
    var group = groupDtl[i];
    for (var j = 0; j < group.roomlist.length; j++) {
        var room = group.roomlist[j];
        //throw JSON.stringify(room);
        var salOrder = helper.getSalListByCusInfo(cust.cusId, group.clsId, cusrName, room.roomName);
        if (salOrder != null) {
            room.ordId = salOrder.ordId;
            room.pNo = salOrder.pNo;
            room.pDate = salOrder.pDate;
        }
        room.cusId = cust.cusId;
        room.cusName = cusName;
        room.cusrName = cusrName;
        room.clsId = group.clsId;
    }
}
//res;
groupDtl;

console 命令行工具 X clear

                    
>
console