1
wtt
2025-12-08 81d1261b908d586b1d078a5d712fcbefddf1cfbf
WebTM/views/Éú²ú¹ÜÀí/Éú²ú¼Æ»®Æ½Ì¨/JIT_DayPlanPlatForm_TK.html
@@ -36,6 +36,7 @@
                            <button class="layui-btn layui-btn-normal" style="margin-left: 0px" type="button" lay-submit="" lay-filter="btnUnlockOrder" id="btnUnlockOrder">解锁工单</button>
                            <button class="layui-btn layui-btn-normal" style="margin-left: 0px" type="button" lay-submit="" lay-filter="btnGetCompletionDate" id="btnGetCompletionDate">获取完工日期</button>
                            <button class="layui-btn layui-btn-normal" style="margin-left: 0px" type="button" lay-submit="" lay-filter="btnGetEarlyDate" id="btnGetEarlyDate">获取最早开工日期</button>
                            <button class="layui-btn layui-btn-normal" style="margin-left: 0px" type="button" lay-submit="" lay-filter="btnProHJPC" id="btnProHJPC">焊接排产</button>
                        </div>
                        <div class="layui-collapse resizable-container">
                            <div class="layui-colla-item">
@@ -783,6 +784,29 @@
                }, 1000)
            }
        })
        //焊接排产
        form.on('submit(btnProHJPC)', function (data) {
            var HWorkShopID = $("#HWorkShopID").val();//生产车间
            if (HWorkShopID == 0) {
                return layer.msg("生产车间不能为空!");
            } else {
                var index = layer.open({
                    type: 1, // page å±‚类型
                    area: ['20%', '20%'],
                    shade: 0.6, // é®ç½©é€æ˜Žåº¦
                    shadeClose: false, // ç‚¹å‡»é®ç½©åŒºåŸŸï¼Œä¸å…³é—­å¼¹å±‚
                    maxmin: false, // ä¸å…è®¸å…¨å±æœ€å°åŒ–
                    anim: 0, // 0-6 çš„动画形式,-1 ä¸å¼€å¯
                    content: '<div style="padding: 12px;">正在运算,请耐心等待!!!</div>'
                });
                setTimeout(function () {
                    get_btnProHJPC(HWorkShopID);
                    layer.close(index);
                }, 1000)
            }
        })
        //工艺路线编辑
        form.on('submit(btnRoutEdit)', function (data) {
@@ -924,7 +948,7 @@
            var myDate = new Date();
            var oneday = 1000 * 60 * 60 * 24;
             columns.push({ type: 'checkbox', fixed: 'left', sort: true });
            columns.push({ type: 'checkbox', fixed: 'left', sort: true });
            columns.push({ type: 'numbers', field: '序号', title: '序号', sort: true, filter: true });
            columns.push({ field: '单据号', title: '单据号', hide: true, sort: true, filter: true});
            columns.push({ field: '销售订单号', title: '销售订单号', width: 150, sort: true, filter: true });
@@ -935,13 +959,13 @@
            columns.push({ field: '物料名称', title: '物料名称', width: 150, sort: true, filter: true });
            columns.push({ field: '规格型号', title: '规格型号', width: 150, sort: true, filter: true });
            columns.push({ field: '小时产能', title: '小时产能', width: 150, sort: true, edit: 'text', filter: true });
            columns.push({ field: '产线总产能', title: '产线总产能', width: 150, sort: true, edit: 'text', filter: true });
            columns.push({ field: '计划数量', title: '生产订单数量', width: 150, sort: true, filter: true });//0
            columns.push({ field: '日计划数量总量', title: '日计划数量总量', width: 150, sort: true, filter: true});//0
            columns.push({ field: '订单可排数量', title: '订单可排数量', width: 150, filter: true});
            columns.push({ field: '拆单数量', title: '拆单数量', width: 150, edit: 'text', filter: true});
            columns.push({ field: '昨日数量', title: '昨日数量', filter: true});
            columns.push({ field: '优先级', title: '优先级', width: 150, edit: 'text', sort: true, filter: true });
            columns.push({ field: '生产资源', title: '生产资源', width: 150, edit: 'text', event: "HSourceID", filter: true });
            columns.push({ field: '使用工装', title: '使用工装', width: 150, filter: true });
            columns.push({ field: '最早开工日期', title: '最早开工日期', width: 150, templet: "<div>{{d.最早开工日期 ==null ?'':layui.util.toDateString(d.最早开工日期, 'yyyy-MM-dd')}}</div>", event: "HEarlyDate", sort: true, filter: { type: 'date[yyyy-MM-dd HH:mm:ss]' } });
            columns.push({
@@ -1157,11 +1181,16 @@
            columns.push({ field: 'HEmpID', title: 'HEmpID', hide: true });
            columns.push({ field: '拆单汇报数量', title: '拆单汇报数量', width: 150, edit: 'text', sort: true });
            columns.push({ field: '销售订单数量', title: '销售订单数量', width: 150, sort: true, filter: true });//0
            columns.push({ field: '日计划数量总量', title: '日计划数量总量', width: 150, sort: true, filter: true });//0
            columns.push({ field: '拆单数量', title: '拆单数量', width: 150, edit: 'text', filter: true });
            columns.push({ field: '今日之前日计划数量', title: '今日之前日计划数量', width: 150, sort: true, filter: true });//0
            columns.push({ field: '计划开始日期', title: '预计开工日期', width: 150, templet: "<div>{{d.计划开始日期 ==null ?'':layui.util.toDateString(d.计划开始日期, 'yyyy-MM-dd')}}</div>", sort: true, filter: { type: 'date[yyyy-MM-dd HH:mm:ss]' } });
            columns.push({ field: '计划结束日期', title: '计划结束日期', width: 150, templet: "<div>{{d.计划结束日期 ==null ?'':layui.util.toDateString(d.计划结束日期, 'yyyy-MM-dd')}}</div>", event: "HPlanEndDate", sort: true, filter: { type: 'date[yyyy-MM-dd HH:mm:ss]' } });
            columns.push({ field: '销售订单审核日期', title: '销售订单审核日期', width: 150, templet: "<div>{{d.销售订单审核日期 ==null ?'':layui.util.toDateString(d.销售订单审核日期, 'yyyy-MM-dd')}}</div>",sort: true, filter: { type: 'date[yyyy-MM-dd HH:mm:ss]' } });
            columns.push({ field: '操作工', title: '操作工', width: 150, edit: 'text', event: "HEmpID" });
            columns.push({ field: 'HMouldID', title: 'HMouldID', width: 150, hide: true });
            columns.push({ field: 'HMouldPCTime', title: 'HMouldPCTime', width: 150, hide: true });
            columns.push({ field: '所需工位数', title: '所需工位数', width: 150 });
            columns.push({ fixed: 'right', title: '操作', toolbar: '#barDemo' });
            option_columns = columns;
            option = {
@@ -1269,7 +1298,7 @@
                        layer.close(ajaxLoad);
                        if (HinterID_S.length > 0) {
                            get_Display_1(HinterID_S);
                            get_Display_2(HinterID_S)
                            get_Display_2(HinterID_S);
                        }
                        if ($("#Comparator").val() == 0 && $("#ColContent").val() == "") {
@@ -2071,359 +2100,6 @@
            }
        }
        //排产
        function get_btnProPC(HWorkShopID) {
            var JDTLen = 0;
            var lMainID = 0;
            var sICMOBillNo = "";
            var dEndDate = "";
            var sProdTimes = 0;
            var sPlanQty = 0;
            var lHGroupID = 0;
            var sGroupName = "";
            var lMaterID = 0;
            var sMaterNumber = "";
            var sMaterName = "";
            var sMaterModel = "";
            var sWorkQty = 0;
            var sYX = 0;
            var iLeftQty = 0;
            var sLeftProdTimes = 0;
            var lSourceID = 0;
            var iRow = 0;
            var sSourceName = "";
            var sSourceNumber = "";
            var ds = [];
            var dsSub = [];
            var bFind = false;
            var HPlanDay = 0;
            var HDelData = 0;
            JDTLen = (100 / option.data.length).toFixed(2);
            var HJDLen = 0;
            var HReportQty = 0;
            //如果锁定计划,则不需要再排程
            //查询本车间是否有锁定计划的数据
            var HinterID_S = [];
            for (var i = 0; i < option.data.length; i++) {
                if (option.data[i]["小时产能"] == "0") {
                    return layer.msg("请先点击准备按钮,确保小时产能不为0!!!")
                }
                HinterID_S.push(option.data[i]["hmainid"]);
            }
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHinterIDList',
                type: "GET",
                async: false,
                data: { "sWhere": HinterID_S.toString(), "num": 1 },
                success: function (data1) {
                    if (data1.count == 1) {
                        HPlanDay = data1.data[0].DayNum;
                    } else {
                        layer.alert(data1.code + data1.Message, { icon: 5 });
                    }
                }, error: function () {
                    layer.alert("接口请求失败!", { icon: 5 });
                }
            });
            //根据车间删除没有锁定的数据
            var sql = "delete from Sc_WorkBillSortBillSub where HMasterDate>=convert(varchar(10) ,DATEADD(DAY," + HPlanDay + ",GETDATE()),20) and HInterID in(select  HInterID from Sc_WorkBillSortBillMain where HWorkShopID=" + HWorkShopID + " and HLockOrder != 1 )";
            $.ajax({
                url: GetWEBURL() + '/Gy_SOPBillList/UpDelSQL',
                type: "GET",
                async: false,
                data: { "sWhere": sql },
                success: function (data1) {
                    if (data1.count == 1) {
                    } else {
                        layer.alert(data1.code + data1.Message, { icon: 5 });
                    }
                }, error: function () {
                    layer.alert("接口请求失败!", { icon: 5 });
                }
            });
            get_HFData(0, 0);
            //初始化表格数据
            for (var i = 0; i < option.data.length; i++) {
                for (var j = (22 + HPlanDay); j < 53; j++) {
                    if (option.data[i][option.cols[0][j].field] != null) {
                        option.data[i][option.cols[0][j].field] = "";
                    }
                }
            }
            //get_FastQuery(2);
            //按任务单优先级 å¾ªçޝ
            for (var i = 0; i < option.data.length; i++) {
                //获取锁定的排产数量
                var SDSum = 0;
                if (HPlanDay > 0) {
                    for (var j = 22; j < (22 + HPlanDay); j++) {
                        SDSum += TableData[i][option.cols[0][j].field];
                    }
                }
                //找到本任务单 å¯ç”¨ç”Ÿäº§èµ„源
                lMainID = option.data[i]["HICMOInterID"];
                sICMOBillNo = option.data[i]["生产订单号"];
                dEndDate = Format(option.data[i]["最迟完工日期"], 'yyyy/MM/dd');
                sProdTimes = option.data[i]["生产周期"];
                sPlanQty = option.data[i]["拆单数量"];
                lHGroupID = option.data[i]["HGroupID"];
                sGroupName = option.data[i]["HGroupName"];
                lMaterID = option.data[i]["HMaterID"];
                sMaterNumber = option.data[i]["物料代码"];
                sMaterName = option.data[i]["物料名称"];
                sMaterModel = option.data[i]["规格型号"];
                sWorkQty = option.data[i]["小时产能"];
                sYX = option.data[i]["优先级"];
                HReportQty = option.data[i]["拆单汇报数量"];
                if (SDSum > 0) {
                    sPlanQty = sPlanQty - SDSum - HReportQty;
                }
                //剩余需求工时
                iLeftQty = sPlanQty - HReportQty;
                if (sWorkQty <= 0) {
                    sLeftProdTimes = 0;
                }
                else {
                    sLeftProdTimes = iLeftQty / sWorkQty;
                }
                //若本任务单已有安排资源(上次排程或者手工排程) åˆ™ä¸å†å¯»æ‰¾èµ„源,直接排程
                if (option.data[i]["HSourceID"] != 0 && option.data[i]["生产资源"] != "") {
                    //获取该生产资源的剩余工时进行排程。
                    lSourceID = option.data[i]["HSourceID"];
                    sSourceName = option.data[i]["生产资源"];
                    sSourceNumber = option.data[i]["生产资源代码"];
                    //查询生产资源的剩余工时进行排程。
                    var sql = "exec h_p_Sc_ICMOGetWorkSource2 " + lMaterID + ",'" + dEndDate + "'," + sProdTimes + "," + lSourceID;
                    $.ajax({
                        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                        type: "GET",
                        async: false,
                        data: { "sWhere": sql },
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
                        }, error: function () {
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                else //没有资源则 è‡ªåŠ¨æ‰¾åˆ°èµ„æº
                {
                    //查询生产资源的剩余工时进行排程。
                    var sql = "exec h_p_Sc_ICMOGetWorkSource " + lMaterID + ",'" + dEndDate + "'," + sProdTimes;
                    $.ajax({
                        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                        type: "GET",
                        async: false,
                        data: { "sWhere": sql },
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
                        }, error: function () {
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                if (ds == null || ds.length == 0) //没有可以排的资源
                {
                    get_HFData(1, i);
                    //跳到下一单  æœ¬å•红色显示
                    $("tr[data-index='" + i + "']").attr({ "style": "background:red;color:black;" });
                    continue;
                }
                else {
                    $("tr[data-index='" + i + "']").attr({ "style": "background:White;color:black;" });
                    //开始排单
                    bFind = false;
                    //超交货期  ä»ç„¶æŽ’程
                    for (var j = 0; j < ds.length; j++) {
                        //判断资源剩余工时,是否大于 ç”Ÿäº§å‘¨æœŸ
                        if (parseFloat(ds[j]["剩余工时"]) >= sLeftProdTimes) {
                            if (iLeftQty > 0) // ç”Ÿäº§è®¢å•剩余数量
                            {
                                //在本资源上生产
                                //获取该生产资源的剩余工时进行排程。
                                lSourceID = ds[j]["HitemID"];
                                sSourceName = ds[j]["资源名称"];
                                sSourceNumber = ds[j]["资源代码"];
                                option.data[i]["HSourceID"] = lSourceID;
                                option.data[i]["生产资源"] = sSourceName;
                                option.data[i]["生产资源代码"] = sSourceNumber;
                                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime " + lSourceID;
                                $.ajax({
                                    url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                                    type: "GET",
                                    async: false,
                                    data: { "sWhere": sql },
                                    success: function (data1) {
                                        if (data1.count == 1) {
                                            dsSub = data1.data
                                        } else {
                                            layer.alert(data1.code + data1.Message, { icon: 5 });
                                        }
                                    }, error: function () {
                                        layer.alert("接口请求失败!", { icon: 5 });
                                    }
                                });
                                if (dsSub == null || dsSub.length == 0) {
                                    get_HFData(1, i);
                                    continue;//如果没有剩余则 æ‰§è¡Œä¸‹ä¸€ä¸ªç”Ÿäº§è®¢å•
                                }
                                if (dsSub != null)//不加班排程
                                {
                                    iRow = i + 1;
                                    //将数据排到网格中
                                    SetInfoToGrid(option, dsSub, null, (iRow - 1), iLeftQty, sWorkQty, false, HPlanDay);
                                    set_AddNew1(option.data[i]);
                                }
                                bFind = true;
                            }
                        } else {
                            get_HFData(1, i);
                        }
                    }
                    //if (!bFind) {
                    //    for (var j = 0; j < ds.length; j++) {
                    //        //判断资源最大剩余工时,是否大于 ç”Ÿäº§å‘¨æœŸ
                    //        if (parseFloat(ds[j]["最大剩余工时"]) > sLeftProdTimes) {
                    //            if (iLeftQty > 0) {
                    //                //在本资源上生产
                    //                grdMain.RowCount = grdMain.RowCount + 1;
                    //                //获取该生产资源的剩余工时进行排程。
                    //                lSourceID = ds[j]["HitemID"];
                    //                sSourceName = ds[j]["资源名称"];
                    //                sSourceNumber = ds[j]["资源代码"];
                    //                option.data[i]["HSourceID"] = lSourceID;
                    //                option.data[i]["生产资源"] = sSourceName;
                    //                option.data[i]["生产资源代码"] = sSourceNumber;
                    //                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime " + lSourceID;
                    //                $.ajax({
                    //                    url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                    //                    type: "GET",
                    //                    async: false,
                    //                    data: { "sWhere": sql },
                    //                    success: function (data1) {
                    //                        if (data1.count == 1) {
                    //                            dsSub = data1.data
                    //                        } else {
                    //                            layer.alert(data1.code + data1.Message, { icon: 5 });
                    //                        }
                    //                    }, error: function () {
                    //                        layer.alert("接口请求失败!", { icon: 5 });
                    //                    }
                    //                });
                    //                if (dsSub == null || dsSub.length == 0) {
                    //                    continue;
                    //                }
                    //                //if (dsSub != null)//加班排程
                    //                //{
                    //                //    //将数据排到网格中
                    //                //    grdMain.RowCount = grdMain.RowCount + 1;  //  grdmain æ–°å¢žä¸€è¡Œ
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecICMOBillNoCol].Value = sICMOBillNo;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecICMOInterIDCol].Value = lMainID;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecLeftQtyCol].Value = iLeftQty;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecGroupIDCol].Value = lHGroupID;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecGroupNameCol].Value = sGroupName;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterIDCol].Value = lMaterID;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterNameCol].Value = sMaterName;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterNumberCol].Value = sMaterNumber;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterModelCol].Value = sMaterModel;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecPlanEndDateCol].Value = dEndDate;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecPlanQtyCol].Value = sPlanQty;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceIDCol].Value = lSourceID;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceNameCol].Value = sSourceName;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceNumberCol].Value = sSourceNumber;
                    //                //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecYXCol].Value = sYX;
                    //                //    Sc_ICMOSortBillFun.SetInfoToGrid(grdMain, dsSub, null, grdMain.RowCount - 1, ref iLeftQty, sWorkQty, true, oClsGridValues);
                    //                //}
                    //            }
                    //            bFind = true;
                    //        }
                    //    }
                    //}
                    //if (!bFind) {
                    //    //如果没有找到合适资源则,直接排最后一个剩余最大的资源。
                    //    //获取该生产资源的剩余工时进行排程。
                    //    lSourceID = ds[ds.length - 1]["HitemID"];
                    //    sSourceName = ds[ds.length - 1]["资源名称"];
                    //    sSourceNumber = ds[ds.length - 1]["资源代码"];
                    //    option.data[i]["HSourceID"] = lSourceID;
                    //    option.data[i]["生产资源"] = sSourceName;
                    //    option.data[i]["生产资源代码"] = sSourceNumber;
                    //    //获取该生产资源的剩余工时进行排程。
                    //    var sql = "exec h_p_Sc_ICMOGetWorkSourceTime " + lSourceID;
                    //    $.ajax({
                    //        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                    //        type: "GET",
                    //        async: false,
                    //        data: { "sWhere": sql },
                    //        success: function (data1) {
                    //            if (data1.count == 1) {
                    //                dsSub = data1.data
                    //            } else {
                    //                layer.alert(data1.code + data1.Message, { icon: 5 });
                    //            }
                    //        }, error: function () {
                    //            layer.alert("接口请求失败!", { icon: 5 });
                    //        }
                    //    });
                    //    if (dsSub == null || dsSub.length == 0) {
                    //        continue;
                    //    }
                    //    //if (dsSub != null)//加班排程
                    //    //{
                    //    //    //将数据排到网格中
                    //    //    grdMain.RowCount = grdMain.RowCount + 1;  //  grdmain æ–°å¢žä¸€è¡Œ
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecICMOBillNoCol].Value = sICMOBillNo;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecICMOInterIDCol].Value = lMainID;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecLeftQtyCol].Value = iLeftQty;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecGroupIDCol].Value = lHGroupID;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecGroupNameCol].Value = sGroupName;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterIDCol].Value = lMaterID;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterNameCol].Value = sMaterName;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterNumberCol].Value = sMaterNumber;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecMaterModelCol].Value = sMaterModel;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecPlanEndDateCol].Value = dEndDate;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecPlanQtyCol].Value = sPlanQty;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceIDCol].Value = lSourceID;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceNameCol].Value = sSourceName;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecSourceNumberCol].Value = sSourceNumber;
                    //    //    grdMain.Rows[grdMain.RowCount - 1].Cells[HSecYXCol].Value = sYX;
                    //    //    Sc_ICMOSortBillFun.SetInfoToGrid(grdMain, dsSub, null, grdMain.RowCount - 1, ref iLeftQty, sWorkQty, true, oClsGridValues);
                    //    //}
                    //}
                }
            }
            table.render(option);
        }
        //组装排产
        function get_btnProZZPC(HWorkShopID) {
            var JDTLen = 0;
@@ -2531,6 +2207,12 @@
                if (HCount > 50000) {
                    break;
                }
                if (option.data[i].锁定工单 == "是") {
                    HArrIndex_PC.push(i);
                    continue;
                }
                var old_date = new Date();
                //element.progress('demo', (i + 1) * HProgress + '%')
@@ -2541,7 +2223,386 @@
                        i = option.data.length - 1;
                    }
                }
                console.log("1.开始排产第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",当前排产日期:" + HPCDate + ",最早开工日期:" + option.data[i].最早开工日期);
                //console.log("1.开始排产第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",当前排产日期:" + HPCDate + ",最早开工日期:" + option.data[i].最早开工日期);
                //排产最后一笔订单(若本次为最后一笔订单排产,但当前排产日期还不满足最早开工日期,则当前日期进行累加,直到满足最早开工日期为止)
                if (HArrIndex_PC.length == option.data.length - 1 && HArrIndex.length > 0 && Format(option.data[HArrIndex[0]].最早开工日期, 'yyyy/MM/dd') > HPCDate) {
                    for (var k = 0; k < 100; k++) {
                        //排产日期往后推一天
                        var dateTime = new Date(HPCDate);
                        HPCDate = Format(new Date(dateTime.setDate(dateTime.getDate() + 1)), 'yyyy/MM/dd');
                        //当前排产日期满足最早开工日期,终止累加
                        if (Format(option.data[HArrIndex[0]].最早开工日期, 'yyyy/MM/dd') <= HPCDate) {
                            break;
                        }
                    }
                }
                //判断是否有未排产的订单,若有则补排
                if (HArrIndex.length > 0 && (HIsPriority == false || HArrIndex.length + HArrIndex_PC.length == option.data.length)) {//(HIsPriority == false || HArrIndex.length >= 2)
                    //对先前未能排产的订单进行排序
                    HArrIndex.sort(function (a, b) { return a - b });
                    //获取之前排产时未能排产的订单
                    i = HArrIndex[0];
                    var index = 0;
                    //如果存在多个之前未能排产的订单,则看哪个订单能够优先满足最早开工日期,优先排产
                    if (HArrIndex.length > 1) {
                        //排产日期往后推一天
                        var dateTime = new Date(HPCDate);
                        HPCDate = Format(new Date(dateTime.setDate(dateTime.getDate() + 1)), 'yyyy/MM/dd');
                        for (var j = 0; j < HArrIndex.length; j++) {
                            //匹配到满足最早开工日期的订单,把这个订单优先拿去排产,并终止循环匹配
                            if (Format(option.data[HArrIndex[j]].最早开工日期, 'yyyy/MM/dd') <= HPCDate) {
                                i = HArrIndex[j];
                                index = j;
                                break;
                            }
                        }
                    }
                    //移除未排产订单信息
                    HArrIndex.splice(index, 1)
                }
                //如果当前订单的最早开工日期不满足当前排产日期(最早开工日期 > å½“前排产日期),则跳出当前订单的排产,先排下一个订单
                if (Format(option.data[i].最早开工日期, 'yyyy/MM/dd') > HPCDate) {
                    if (HArrIndex.indexOf(i) == -1) {
                        HArrIndex.push(i);
                    }
                    HIsPriority = true;
                    //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                    if (HArrIndex.length > 0 && i == HDataLen - 1) {
                        HDataLen = HDataLen + 1;
                    }
                    continue;
                }
                //如果当前行订单已经排产过,则跳出当前订单排产,接着下一行订单的排产
                if (HArrIndex_PC.indexOf(i) != -1) {
                    continue;
                }
                //记录已经排产的订单下标
                HArrIndex_PC.push(i);
                //console.log("2.开始排产第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",当前排产日期:" + HPCDate + ",最早开工日期:" + option.data[i].最早开工日期);
                //如果本次最后一笔订单排产,但还存在因最早开工日期不满足的而排产不了的订单则加长循环长度
                if (i == HDataLen - 1 && HArrIndex.length > 0) {
                    HDataLen = HDataLen + 1;
                }
                //获取锁定的排产数量
                var SDSum = 0;
                if (HPlanDay > 0) {
                    for (var j = 22; j < (22 + HPlanDay); j++) {
                        SDSum += TableData[i][option.cols[0][j].field];
                    }
                }
                //找到本任务单 å¯ç”¨ç”Ÿäº§èµ„源
                lMainID = option.data[i]["HICMOInterID"];
                sICMOBillNo = option.data[i]["生产订单号"];
                dEndDate = Format(option.data[i]["最迟完工日期"], 'yyyy/MM/dd');
                sProdTimes = option.data[i]["生产周期"];
                sPlanQty = option.data[i]["拆单数量"];
                lHGroupID = option.data[i]["HGroupID"];
                sGroupName = option.data[i]["HGroupName"];
                lMaterID = option.data[i]["HMaterID"];
                sMaterNumber = option.data[i]["物料代码"];
                sMaterName = option.data[i]["物料名称"];
                sMaterModel = option.data[i]["规格型号"];
                sYX = option.data[i]["优先级"];
                HReportQty = option.data[i]["拆单汇报数量"];
                var source_date = new Date();
                var HSourceType = "";
                //若本任务单已有安排资源(上次排程或者手工排程) åˆ™ä¸å†å¯»æ‰¾èµ„源,直接排程
                if (option.data[i]["HSourceID"] != 0 && option.data[i]["生产资源"] != "") {
                    HSourceType = 1;
                    //获取该生产资源的剩余工时进行排程。
                    lSourceID = option.data[i]["HSourceID"];
                    sSourceName = option.data[i]["生产资源"];
                    sSourceNumber = option.data[i]["生产资源代码"];
                    //查询生产资源的剩余工时进行排程。
                    var sql = "exec h_p_Sc_ICMOGetWorkSource2 " + lMaterID + ",'" + dEndDate + "'," + sProdTimes + "," + lSourceID;
                    $.ajax({
                        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                        type: "GET",
                        async: false,
                        data: { "sWhere": sql },
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                                if (ds.length > 0) {
                                    option.data[i]["产线总产能"] = option.data[i]["小时产能"] * ds[0]["产线人数"]
                                }
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
                        }, error: function () {
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                else //没有资源则 è‡ªåŠ¨æ‰¾åˆ°èµ„æº
                {
                    HSourceType = 2;
                    //查询生产资源的剩余工时进行排程。
                    var sql = "exec h_p_Sc_ICMOGetWorkSource_TK " + lMaterID + ",'" + dEndDate + "'," + sProdTimes;
                    $.ajax({
                        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                        type: "GET",
                        async: false,
                        data: { "sWhere": sql },
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                                if (ds.length > 0) {
                                    option.data[i]["产线总产能"] = option.data[i]["小时产能"] * ds[0]["产线人数"]
                                }
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
                        }, error: function () {
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                var HTimeDifference_Source = TimeDifference(source_date);
                console.log("第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",生产资源匹配方式" + HSourceType + ",用时为" + HTimeDifference_Source);
                //获取小时产能
                sWorkQty = option.data[i]["产线总产能"];
                //获取更新计划数量
                if (SDSum > 0) {
                    sPlanQty = sPlanQty - SDSum - HReportQty;
                }
                //剩余需求工时
                iLeftQty = sPlanQty - HReportQty;
                if (sWorkQty <= 0) {
                    sLeftProdTimes = 0;
                }
                else {
                    sLeftProdTimes = iLeftQty / sWorkQty;
                }
                if (ds == null || ds.length == 0) //没有可以排的资源
                {
                    get_HFData(1, i);
                    //跳到下一单  æœ¬å•红色显示
                    $("tr[data-index='" + i + "']").attr({ "style": "background:red;color:black;" });
                    HBackgroundColorIndex.push(i);
                    continue;
                }
                else {
                    $("tr[data-index='" + i + "']").attr({ "style": "background:White;color:black;" });
                    //开始排单
                    bFind = false;
                    //超交货期  ä»ç„¶æŽ’程
                    for (var j = 0; j < ds.length; j++) {
                        //判断资源剩余工时,是否大于 ç”Ÿäº§å‘¨æœŸ
                        if (parseFloat(ds[j]["剩余工时"]) >= sLeftProdTimes) {
                            if (iLeftQty > 0) // ç”Ÿäº§è®¢å•剩余数量
                            {
                                //在本资源上生产
                                //获取该生产资源的剩余工时进行排程。
                                lSourceID = ds[j]["HitemID"];
                                sSourceName = ds[j]["资源名称"];
                                sSourceNumber = ds[j]["资源代码"];
                                option.data[i]["HSourceID"] = lSourceID;
                                option.data[i]["生产资源"] = sSourceName;
                                option.data[i]["生产资源代码"] = sSourceNumber;
                                //console.log("======" + "第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ", è‡ªåŠ¨åŒ¹é…" + ds.length + "个生产资源,最终选择:" + sSourceName);
                                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime " + lSourceID;
                                $.ajax({
                                    url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                                    type: "GET",
                                    async: false,
                                    data: { "sWhere": sql },
                                    success: function (data1) {
                                        if (data1.count == 1) {
                                            dsSub = data1.data
                                        } else {
                                            layer.alert(data1.code + data1.Message, { icon: 5 });
                                        }
                                    }, error: function () {
                                        layer.alert("接口请求失败!", { icon: 5 });
                                    }
                                });
                                if (dsSub == null || dsSub.length == 0) {
                                    get_HFData(1, i);
                                    continue;//如果没有剩余则 æ‰§è¡Œä¸‹ä¸€ä¸ªç”Ÿäº§è®¢å•
                                }
                                if (dsSub != null)//不加班排程
                                {
                                    iRow = i + 1;
                                    //将数据排到网格中
                                    SetInfoToGrid(option, dsSub, null, (iRow - 1), iLeftQty, sWorkQty, false, HPlanDay);
                                    set_AddNew1(option.data[i]);
                                }
                                bFind = true;
                                break;
                            }
                        } else {
                            get_HFData(1, i);
                        }
                    }
                }
                var HTimeDifference_PC = TimeDifference(old_date);
                console.log("排产第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",总用时为" + HTimeDifference_PC);
            }
            table.render(option);
            get_FastQuery(2);
        }
        //焊接排产
        function get_btnProHJPC(HWorkShopID) {
            var JDTLen = 0;
            var lMainID = 0;
            var sICMOBillNo = "";
            var dEndDate = "";
            var sProdTimes = 0;
            var sPlanQty = 0;
            var lHGroupID = 0;
            var sGroupName = "";
            var lMaterID = 0;
            var sMaterNumber = "";
            var sMaterName = "";
            var sMaterModel = "";
            var sWorkQty = 0;
            var sYX = 0;
            var iLeftQty = 0;
            var sLeftProdTimes = 0;
            var lSourceID = 0;
            var iRow = 0;
            var sSourceName = "";
            var sSourceNumber = "";
            var ds = [];
            var dsSub = [];
            var bFind = false;
            var HPlanDay = 0;
            var HDelData = 0;
            JDTLen = (100 / option.data.length).toFixed(2);
            var HJDLen = 0;
            var HReportQty = 0;
            var HProducedTogether = [];
            var HProducedTogetherIndex = [];
            var HChildSourceData = []; //子生产资源
            //如果锁定计划,则不需要再排程
            //查询本车间是否有锁定计划的数据
            var HinterID_S = [];
            for (var i = 0; i < option.data.length; i++) {
                //if (option.data[i]["小时产能"] == "0") {
                //    return layer.msg("请先点击准备按钮,确保小时产能不为0!!!")
                //}
                HinterID_S.push(option.data[i]["hmainid"]);
            }
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillHinterIDList',
                type: "GET",
                async: false,
                data: { "sWhere": HinterID_S.toString(), "num": 1 },
                success: function (data1) {
                    if (data1.count == 1) {
                        HPlanDay = data1.data[0].DayNum;
                    } else {
                        layer.alert(data1.code + data1.Message, { icon: 5 });
                    }
                }, error: function () {
                    layer.alert("接口请求失败!", { icon: 5 });
                }
            });
            //根据车间删除没有锁定的数据
            var sql = "delete from Sc_WorkBillSortBillSub where HMasterDate>=convert(varchar(10) ,DATEADD(DAY," + HPlanDay + ",GETDATE()),20) and HInterID in(select  HInterID from Sc_WorkBillSortBillMain where HWorkShopID=" + HWorkShopID + " and HLockOrder != 1 )";
            $.ajax({
                url: GetWEBURL() + '/Gy_SOPBillList/UpDelSQL',
                type: "GET",
                async: false,
                data: { "sWhere": sql },
                success: function (data1) {
                    if (data1.count == 1) {
                    } else {
                        layer.alert(data1.code + data1.Message, { icon: 5 });
                    }
                }, error: function () {
                    layer.alert("接口请求失败!", { icon: 5 });
                }
            });
            //根据车间删除器具占用时间
            var sql2 = "delete from Gy_MouldOccupancyTime where HICMOEntryID not in ( select HICMOEntryID from Sc_WorkBillSortBillMain where HInterID in (select HInterID from Sc_WorkBillSortBillSub where HMasterDate>=convert(varchar(10) ,DATEADD(DAY," + HPlanDay + ",GETDATE()),20)))";
            $.ajax({
                url: GetWEBURL() + '/Gy_SOPBillList/UpDelSQL',
                type: "GET",
                async: false,
                data: { "sWhere": sql2 },
                success: function (data1) {
                    if (data1.count == 1) {
                    } else {
                        layer.alert(data1.code + data1.Message, { icon: 5 });
                    }
                }, error: function () {
                    layer.alert("接口请求失败!", { icon: 5 });
                }
            });
            get_HFData(0, 0);
            //初始化表格数据
            for (var i = 0; i < option.data.length; i++) {
                for (var j = (22 + HPlanDay); j < 53; j++) {
                    if (option.data[i][option.cols[0][j].field] != null) {
                        option.data[i][option.cols[0][j].field] = "";
                    }
                }
            }
            //get_FastQuery(2);
            //为防止用户手动篡改优先级,重新根据优先级排序数组
            option.data.sort(function (a, b) {
                return a.优先级 - b.优先级
            });
            window.HPCDate = Format(new Date().toLocaleDateString().replaceAll('/', '-'), 'yyyy/MM/dd') //当前排产日期
            window.HArrIndex = []; //用于存储因为当前日期不满足最早开工日期而未能排产的订单下标
            window.HArrIndex_PC = [];//用于存储已经排产的订单下标
            window.HIsPriority = false;//是否优先排产
            var HDataLen = option.data.length;//总排产订单长度
            var HProgress = parseInt(100 / HDataLen);
            var HCount = 0;
            //按任务单优先级 å¾ªçޝ
            for (var i = 0; i < HDataLen; i++) {
                HCount++;
                if (HCount > 50000) {
                    break;
                }
                //element.progress('demo', (i + 1) * HProgress + '%')
                //判断数组长度是否发生变化,是则重置数组长度
                if (HDataLen != option.data.length) {
                    HDataLen = option.data.length;
                    if (i == option.data.length) {
                        i = option.data.length - 1;
                    }
                }
                //console.log("1.开始排产第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ",当前排产日期:" + HPCDate + ",最早开工日期:" + option.data[i].最早开工日期);
                //排产最后一笔订单(若本次为最后一笔订单排产,但当前排产日期还不满足最早开工日期,则当前日期进行累加,直到满足最早开工日期为止)
                if (HArrIndex_PC.length == option.data.length - 1 && HArrIndex.length > 0 && Format(option.data[HArrIndex[0]].最早开工日期, 'yyyy/MM/dd') > HPCDate) {
                    for (var k = 0; k < 100; k++) {
@@ -2647,6 +2708,10 @@
                else {
                    sLeftProdTimes = iLeftQty / sWorkQty;
                }
                var HIsContinue = false;    //是否能匹配到合适的产品器具清单,匹配不到则跳过当前订单
                //#region å·²åŒ¹é…äº†èµ„源,直接排产
                //若本任务单已有安排资源(上次排程或者手工排程) åˆ™ä¸å†å¯»æ‰¾èµ„源,直接排程
                if (option.data[i]["HSourceID"] != 0 && option.data[i]["生产资源"] != "") {
                    //获取该生产资源的剩余工时进行排程。
@@ -2654,7 +2719,7 @@
                    sSourceName = option.data[i]["生产资源"];
                    sSourceNumber = option.data[i]["生产资源代码"];
                    //查询生产资源的剩余工时进行排程。
                    var sql = "exec h_p_Sc_ICMOGetWorkSource2 " + lMaterID + ",'" + dEndDate + "'," + sProdTimes + "," + lSourceID;
                    var sql = "exec h_p_Sc_ICMOGetWorkSource2 " + lMaterID + ",'" + dEndDate + "'," + sProdTimes + "," + lSourceID;
                    $.ajax({
                        url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                        type: "GET",
@@ -2663,6 +2728,49 @@
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                                //看是否能匹配到满足生产条件的产品器具清单
                                var HMaterMouldData = get_MouldByMater(lMaterID, lSourceID, Format(HPCDate, 'yyyy-MM-dd'));
                                console.log("第" + (i + 1) + "行,生产订单:" + option.data[i].生产订单号 + " exec h_p_Gy_MaterialMouldByMater " + lMaterID + "," + lSourceID + ",'" + Format(HPCDate, 'yyyy-MM-dd') + "' ã€‚匹配器具清单数为:" + HMaterMouldData.length);
                                if (HMaterMouldData.length < 1) {
                                    //没有匹配到器具,生产不了,把订单挂到待排产列表中,先匹配下一优先级的订单
                                    if (HArrIndex.indexOf(i) == -1) {
                                        HArrIndex.push(i);
                                    }
                                    HIsPriority = true;
                                    //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                                    if (HArrIndex.length > 0 && i == HDataLen - 1) {
                                        HDataLen = HDataLen + 1;
                                    }
                                    HIsContinue = true;
                                } else {
                                    //var HSourceMouldData = [];
                                    ////判断匹配出的器具是否能在此资源上使用
                                    //for (var index = 0; index < HMaterMouldData.length; index++) {
                                    //    HSourceMouldData = get_MouldBySource(lSourceID, HMaterMouldData[index].HMouldID);
                                    //    if (HSourceMouldData.length > 0) {
                                    //        break;
                                    //    }
                                    //}
                                    //if (HSourceMouldData.length < 1) {
                                    //    //没有匹配到资源器具清单,生产不了,把订单挂到待排产列表中,先匹配下一优先级的订单
                                    //    if (HArrIndex.indexOf(i) == -1) {
                                    //        HArrIndex.push(i);
                                    //    }
                                    //    HIsPriority = true;
                                    //    //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                                    //    if (HArrIndex.length > 0 && i == HDataLen - 1) {
                                    //        HDataLen = HDataLen + 1;
                                    //    }
                                    //    HIsContinue = true;
                                    //}
                                    option.data[i].HMouldID = HMaterMouldData[0].HMouldID;
                                    option.data[i].HMouldPCTime = HMaterMouldData[0].HPCTime;
                                }
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
@@ -2670,7 +2778,10 @@
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                //#endregion
                //#region æ²¡æœ‰èµ„源则 è‡ªåŠ¨æ‰¾åˆ°èµ„æº
                else //没有资源则 è‡ªåŠ¨æ‰¾åˆ°èµ„æº
                {
                    //查询生产资源的剩余工时进行排程。
@@ -2683,14 +2794,135 @@
                        success: function (data1) {
                            if (data1.count == 1) {
                                ds = data1.data
                                if (ds.length > 0) {
                                    //#region èµ„源工位数等于需求工位数
                                    if (ds[0].资源工位数 == ds[0].所需工位数) {
                                        //看是否能匹配到满足生产条件的产品器具清单
                                        var HMaterMouldData = get_MouldByMater(lMaterID, ds[0]["HitemID"], Format(HPCDate, 'yyyy-MM-dd'));
                                        if (HMaterMouldData.length < 1) {
                                            //没有匹配到器具,生产不了,把订单挂到待排产列表中,先匹配下一优先级的订单
                                            if (HArrIndex.indexOf(i) == -1) {
                                                HArrIndex.push(i);
                                            }
                                            HIsPriority = true;
                                            //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                                            if (HArrIndex.length > 0 && i == HDataLen - 1) {
                                                HDataLen = HDataLen + 1;
                                            }
                                            HIsContinue = true;
                                        } else {
                                            ////判断匹配出的器具是否能在此资源上使用
                                            //var HSourceMouldData = get_MouldBySource(ds[0]["HitemID"], HMaterMouldData[0].HMouldID);
                                            //if (HSourceMouldData.length < 1) {
                                            //    //没有匹配到资源器具清单,生产不了,把订单挂到待排产列表中,先匹配下一优先级的订单
                                            //    if (HArrIndex.indexOf(i) == -1) {
                                            //        HArrIndex.push(i);
                                            //    }
                                            //    HIsPriority = true;
                                            //    //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                                            //    if (HArrIndex.length > 0 && i == HDataLen - 1) {
                                            //        HDataLen = HDataLen + 1;
                                            //    }
                                            //    HIsContinue = true;
                                            //}
                                            option.data[i].HMouldID = HMaterMouldData[0].HMouldID;
                                            option.data[i].HMouldPCTime = HMaterMouldData[0].HPCTime;
                                        }
                                    }
                                    //#endregion
                                    //#region èµ„源工位数大于需求工位数
                                    else if (ds[0].资源工位数 > ds[0].所需工位数) {
                                        //看是否能匹配到满足生产条件的产品器具清单
                                        var HMaterMouldData = get_MouldByMater(lMaterID, ds[0]["HitemID"], Format(HPCDate, 'yyyy-MM-dd'));
                                        if (HMaterMouldData.length < 1) {
                                            //没有匹配到器具,生产不了,把订单挂到待排产列表中,先匹配下一优先级的订单
                                            if (HArrIndex.indexOf(i) == -1) {
                                                HArrIndex.push(i);
                                            }
                                            HIsPriority = true;
                                            //为满足最后一笔匹配订单也能进行排产,改变总排产订单数组长度,继续循环匹配
                                            if (HArrIndex.length > 0 && i == HDataLen - 1) {
                                                HDataLen = HDataLen + 1;
                                            }
                                            HIsContinue = true;
                                        } else {
                                            option.data[i].HMouldID = HMaterMouldData[0].HMouldID;
                                            option.data[i].HMouldPCTime = HMaterMouldData[0].HPCTime;
                                        }
                                        //匹配此生产资源下的子生产资源
                                        HChildSourceData = get_ChildSource(ds[0]["HitemID"]);
                                        option.data[i].生产资源 = "";
                                        for (var k = 0; k < option.data[i].所需工位数; k++) {
                                            if (k == 0) {
                                                option.data[i].HSourceID = HChildSourceData[k].子资源ID;
                                            }
                                            option.data[i].生产资源 += HChildSourceData[k].子资源名称 + "&";
                                        }
                                        option.data[i].生产资源 = option.data[i].生产资源.substring(0, option.data[i].生产资源.length - 1)
                                        //判断资源是否有剩余工位,接着匹配其他订单同时生产
                                        var HRestCubicleQty = ds[0].资源工位数 - ds[0].所需工位数
                                        for (var x = i + 1; x < option.data.length; x++) {
                                            if (HRestCubicleQty >= option.data[x].所需工位数 && HArrIndex_PC.indexOf(x) == -1) {
                                                console.log("&&&&&第" + (i + 1) + "行有订单一起参与匹配," + option.data[x].HMaterID)
                                                //判断此订单(物料)是否也能放在此生产资源上生产
                                                var HMaterSourceData = get_MaterSource(option.data[x].HMaterID);
                                                for (var y = 0; y < HMaterSourceData.length; y++) {
                                                    if (ds[0]["HitemID"] == HMaterSourceData[y]["HitemID"]) {
                                                        //看是否能匹配到满足生产条件的产品器具清单
                                                        var HMaterMouldData_Level2 = get_MouldByMater(option.data[x].HMaterID, ds[0]["HitemID"], Format(HPCDate, 'yyyy-MM-dd'));
                                                        if (HMaterMouldData_Level2.length >= 1) {
                                                            option.data[x].HMouldID = HMaterMouldData_Level2[0].HMouldID;
                                                            option.data[x].HMouldPCTime = HMaterMouldData_Level2[0].HPCTime;
                                                            option.data[x].生产资源 = "";
                                                            for (var k = HChildSourceData.length - HRestCubicleQty; k < HChildSourceData.length; k++) {
                                                                if (k == HChildSourceData.length - HRestCubicleQty) {
                                                                    option.data[x].HSourceID = HChildSourceData[k].子资源ID;
                                                                }
                                                                option.data[x].生产资源 += HChildSourceData[k].子资源名称 + "&";
                                                            }
                                                            option.data[x].生产资源 = option.data[x].生产资源.substring(0, option.data[x].生产资源.length - 1)
                                                            HProducedTogether.push(HMaterSourceData[y]);
                                                            HProducedTogetherIndex.push(x);
                                                            console.log("生产订单:" + option.data[x].生产订单号 + ",可以与订单" + option.data[i].生产订单号 + "一起进行生产")
                                                            HArrIndex_PC.push(x);
                                                            break;
                                                        } else {
                                                            //没有匹配到产品器具清单,继续匹配下一优先级订单共同生产
                                                            continue;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    //#endregion
                                } else {
                                    console.log("第" + (i + 1) + "行,优先级" + option.data[i].优先级 + ",没有匹配到生产资源:" + sql);
                                }
                            } else {
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                                layer.alert(data1.code + data1.Message, { icon: 5 });
                            }
                        }, error: function () {
                            layer.alert("接口请求失败!", { icon: 5 });
                        }
                    });
                }
                //#endregion
                //是否能匹配到合适的产品器具清单,匹配不到则跳过当前订单
                if (HIsContinue) {
                    continue;
                }
                if (ds == null || ds.length == 0) //没有可以排的资源
                {
                    get_HFData(1, i);
@@ -2714,13 +2946,13 @@
                                lSourceID = ds[j]["HitemID"];
                                sSourceName = ds[j]["资源名称"];
                                sSourceNumber = ds[j]["资源代码"];
                                option.data[i]["HSourceID"] = lSourceID;
                                option.data[i]["生产资源"] = sSourceName;
                                option.data[i]["HSourceID"] = option.data[i]["HSourceID"] == null ? lSourceID : option.data[i]["HSourceID"];
                                option.data[i]["生产资源"] = option.data[i]["生产资源"] == null ? sSourceName : option.data[i]["生产资源"];
                                option.data[i]["生产资源代码"] = sSourceNumber;
                                console.log("======" + "第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ", è‡ªåŠ¨åŒ¹é…" + ds.length + "个生产资源,最终选择:" + sSourceName);
                                //console.log("======" + "第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ", è‡ªåŠ¨åŒ¹é…" + ds.length + "个生产资源,最终选择:" + sSourceName);
                                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime " + lSourceID;
                                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime_TK_HJPC " + option.data[i]["HSourceID"] + "," + option.data[i].HMouldID;
                                $.ajax({
                                    url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                                    type: "GET",
@@ -2747,7 +2979,14 @@
                                    //将数据排到网格中
                                    SetInfoToGrid(option, dsSub, null, (iRow - 1), iLeftQty, sWorkQty, false, HPlanDay);
                                    set_AddNew1(option.data[i]);
                                    for (var z = 0; z < option.data[i].所需工位数; z++) {
                                        if (z == 0) {
                                            set_AddNew_HJPC(option.data[i]);
                                        } else {
                                            //option.data[i].HSourceID = HChildSourceData[z].子资源ID;
                                            //set_AddNew_HJPC(option.data[i]);
                                        }
                                    }
                                }
                                bFind = true;
@@ -2758,14 +2997,84 @@
                        }
                    }
                }
                var new_date = new Date();
                var difftime = (new_date - old_date) / 1000; //计算时间差,并把毫秒转换成秒
                //共同生产订单排产
                if (HProducedTogether.length > 0) {
                    //开始排单
                    bFind = false;
                    //超交货期  ä»ç„¶æŽ’程
                    for (var j = 0; j < HProducedTogether.length; j++) {
                        $("tr[data-index='" + HProducedTogetherIndex[j] + "']").attr({ "style": "background:White;color:black;" });
                var days = parseInt(difftime / 86400); // å¤©  24*60*60*1000
                var hours = parseInt(difftime / 3600) - 24 * days;    // å°æ—¶ 60*60 æ€»å°æ—¶æ•°-过去的小时数=现在的小时数
                var minutes = parseInt(difftime % 3600 / 60); // åˆ†é’Ÿ -(day*24) ä»¥60秒为一整份 å–ä½™ å‰©ä¸‹ç§’æ•° ç§’æ•°/60 å°±æ˜¯åˆ†é’Ÿæ•°
                var seconds = parseInt(difftime % 60);  // ä»¥60秒为一整份 å–ä½™ å‰©ä¸‹ç§’æ•°
                console.log("时间差是: " + days + "天, " + hours + "小时, " + minutes + "分钟, " + seconds + "秒");
                        sPlanQty = option.data[HProducedTogetherIndex[j]]["拆单数量"];
                        HReportQty = option.data[HProducedTogetherIndex[j]]["拆单汇报数量"];
                        sWorkQty = option.data[HProducedTogetherIndex[j]]["小时产能"];
                        if (SDSum > 0) {
                            sPlanQty = sPlanQty - SDSum - HReportQty;
                        }
                        //剩余需求工时
                        iLeftQty = sPlanQty - HReportQty;
                        if (sWorkQty <= 0) {
                            sLeftProdTimes = 0;
                        }
                        else {
                            sLeftProdTimes = iLeftQty / sWorkQty;
                        }
                        //判断资源剩余工时,是否大于 ç”Ÿäº§å‘¨æœŸ
                        if (parseFloat(HProducedTogether[j]["剩余工时"]) >= sLeftProdTimes) {
                            if (iLeftQty > 0) // ç”Ÿäº§è®¢å•剩余数量
                            {
                                //在本资源上生产
                                //获取该生产资源的剩余工时进行排程。
                                lSourceID = HProducedTogether[j]["HitemID"];
                                sSourceName = HProducedTogether[j]["资源名称"];
                                sSourceNumber = HProducedTogether[j]["资源代码"];
                                option.data[HProducedTogetherIndex[j]]["HSourceID"] = option.data[HProducedTogetherIndex[j]]["HSourceID"] == null ? lSourceID : option.data[HProducedTogetherIndex[j]]["HSourceID"]
                                option.data[HProducedTogetherIndex[j]]["生产资源"] = option.data[HProducedTogetherIndex[j]]["生产资源"] == null ? sSourceName : option.data[HProducedTogetherIndex[j]]["生产资源"];
                                option.data[HProducedTogetherIndex[j]]["生产资源代码"] = sSourceNumber;
                                //console.log("======" + "第" + (i + 1) + "行订单:" + option.data[i].生产订单号 + ", è‡ªåŠ¨åŒ¹é…" + ds.length + "个生产资源,最终选择:" + sSourceName);
                                var sql = "exec h_p_Sc_ICMOGetWorkSourceTime_TK_HJPC " + option.data[HProducedTogetherIndex[j]]["HSourceID"] + "," + option.data[HProducedTogetherIndex[j]].HMouldID;
                                $.ajax({
                                    url: GetWEBURL() + '/Gy_SOPBillList/WindowPrintList',
                                    type: "GET",
                                    async: false,
                                    data: { "sWhere": sql },
                                    success: function (data1) {
                                        if (data1.count == 1) {
                                            dsSub = data1.data
                                        } else {
                                            layer.alert(data1.code + data1.Message, { icon: 5 });
                                        }
                                    }, error: function () {
                                        layer.alert("接口请求失败!", { icon: 5 });
                                    }
                                });
                                if (dsSub == null || dsSub.length == 0) {
                                    get_HFData(1, HProducedTogetherIndex[j]);
                                    continue;//如果没有剩余则 æ‰§è¡Œä¸‹ä¸€ä¸ªç”Ÿäº§è®¢å•
                                }
                                if (dsSub != null)//不加班排程
                                {
                                    iRow = HProducedTogetherIndex[j] + 1;
                                    //将数据排到网格中
                                    SetInfoToGrid(option, dsSub, null, (iRow - 1), iLeftQty, sWorkQty, false, HPlanDay);
                                    set_AddNew_HJPC(option.data[HProducedTogetherIndex[j]]);
                                }
                                bFind = true;
                                break;
                            }
                        } else {
                            get_HFData(1, HProducedTogetherIndex[j]);
                        }
                    }
                    HProducedTogether = [];
                }
            }
            table.render(option);
            get_FastQuery(2);
@@ -2785,15 +3094,30 @@
        //将数据排程到网格
        function SetInfoToGrid(grd, ds, dsPre, iRow, sLeftQty, sWorkQty, bAddFlag, HPlanDay) {
            var dsindex = 0;
            var HPCDate_Reset = "";
            //判断当前生产资源已经排产到哪个日期了,当前生产资源还有产能的最近日期与当前订单的排产日期不符,则重置当前排产日期
            if (Format(ds[0]["日期"], 'yyyy/MM/dd') != Format(HPCDate, 'yyyy/MM/dd')) {
                for (var i = 0; i < ds.length; i++) {
                    if (Format(ds[i]["日期"], 'yyyy/MM/dd') >= Format(grd.data[iRow]['最早开工日期'], 'yyyy/MM/dd') || HArrIndex_PC.length == option.data.length || HArrIndex.length + HArrIndex_PC.length == option.data.length) {
                        //用来重置当前排产日期(有些订单开工日期靠后,要先把后面日期的资源工时占用,排产结束后在把日期调回到没被排产的日期)
                        if (i != 0) {
                            HPCDate_Reset = ds[0]["日期"];
                        }
                        //匹配订单适合的排产日期
                        dsindex = i;
                        break;
                    }
                }
                //当前生产资源剩余产能最近的日期满足最早开工日期,则重置排产日期为当前生产资源剩余产能最近的日期
                if (Format(ds[0]["日期"], 'yyyy/MM/dd') >= Format(grd.data[iRow]['最早开工日期'], 'yyyy/MM/dd') || HArrIndex_PC.length == option.data.length || HArrIndex.length + HArrIndex_PC.length == option.data.length) {
                if (Format(ds[dsindex]["日期"], 'yyyy/MM/dd') >= Format(grd.data[iRow]['最早开工日期'], 'yyyy/MM/dd') || HArrIndex_PC.length == option.data.length || HArrIndex.length + HArrIndex_PC.length == option.data.length) {
                    if (HArrIndex.length + HArrIndex_PC.length == option.data.length) {
                        HPCDate = Format(grd.data[iRow]['最早开工日期'], 'yyyy/MM/dd');
                    } else {
                        HPCDate = Format(ds[0]["日期"], 'yyyy/MM/dd');
                        HPCDate = Format(ds[dsindex]["日期"], 'yyyy/MM/dd');
                    }
                    //如果是最后一笔订单在排产,则重置排产日期为最早开工日期(下方会判断,不会超产能排产)
@@ -2801,7 +3125,6 @@
                        HPCDate = Format(grd.data[iRow]['最早开工日期'], 'yyyy/MM/dd');
                    }
                } else { //当前日期不符合订单的最早开工日期,则把订单重新加入待排产列表
                    //将下标加入未排产订单数组
                    if (HArrIndex.indexOf(iRow) == -1) {
                        HArrIndex.push(iRow);
@@ -3021,7 +3344,12 @@
                                            //oSub.iRow = iRow;
                                            //oSub.iCol = j;
                                            //option.data[iRow]["HLeftTimes"] = ds[i]["最大剩余工时"];
                                            HPCDate = Format(grd.cols[0][j].field, 'yyyy/MM/dd');//赋值排产日期
                                            if (HPCDate_Reset != "") {
                                                HPCDate = HPCDate_Reset;
                                            } else {
                                                HPCDate = Format(grd.cols[0][j].field, 'yyyy/MM/dd');//赋值排产日期
                                            }
                                            HIsPriority = false;
                                            break;
@@ -3036,6 +3364,90 @@
                }
            }
        }
        //物料匹配产品器具清单
        function get_MouldByMater(HMaterID, HSourceID, HPCDate) {
            var HReturnData = [];
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/GetMouldByMater',
                type: "GET",
                async: false,
                data: { "HMaterID": HMaterID, "HSourceID": HSourceID, "HPCDate": HPCDate},
                success: function (data1) {
                    if (data1.count == 1) {
                        HReturnData = data1.data;
                    } else {
                        console.log(new Date() + ",匹配产品器具清单出错1");
                    }
                }, error: function () {
                    console.log(new Date() + ",匹配产品器具清单出错2");
                }
            });
            return HReturnData;
        }
        //资源匹配资源器具清单
        function get_MouldBySource(HSourceID, HMouldID) {
            var HReturnData = [];
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/GetMouldBySource',
                type: "GET",
                async: false,
                data: { "HSourceID": HSourceID, "HMouldID": HMouldID },
                success: function (data1) {
                    if (data1.count == 1) {
                        HReturnData = data1.data;
                    } else {
                        console.log(new Date() + ",匹配产品器具清单出错2");
                    }
                }, error: function () {
                    console.log(new Date() + ",匹配产品器具清单出错1");
                }
            });
            return HReturnData;
        }
        //判断多个订单是否能一起放到同一个生产资源进行生产
        function get_MaterSource(HMaterID) {
            var HMaterSourceData = [];
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/GetSourceByMater',
                type: "GET",
                async: false,
                data: { "HMaterID": HMaterID },
                success: function (data1) {
                    if (data1.count == 1) {
                        HMaterSourceData = data1.data;
                    } else {
                        console.log(new Date() + ",匹配物料资源出错1");
                    }
                }, error: function () {
                    console.log(new Date() + ",匹配物料资源出错2");
                }
            });
            return HMaterSourceData;
        }
        //通过主生产资源匹配出子生产资源
        function get_ChildSource(HSourceID) {
            var HChildSource = [];
            $.ajax({
                url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/GetChildSource',
                type: "GET",
                async: false,
                data: { "HSourceID": HSourceID },
                success: function (data1) {
                    if (data1.count == 1) {
                        HChildSource = data1.data;
                    } else {
                        console.log(new Date() + ",获取子生产资源2");
                    }
                }, error: function () {
                    console.log(new Date() + ",获取子生产资源1");
                }
            });
            return HChildSource;
        }
        //查询生产订单
@@ -3241,6 +3653,8 @@
        //优先级
        function set_Priority() {
            var old_date = new Date();
            var data = table.cache["mainTable"];
            if ($("#HWorkShopIDName").val().indexOf("组装") !== -1) {
@@ -3268,6 +3682,21 @@
            }
            option.data = data;
            table.render(option);
            var HTimeDifference = TimeDifference(old_date);
            console.log("优先级排序用时: " + HTimeDifference);
        }
        function TimeDifference(old_date) {
            var new_date = new Date();
            var difftime = (new_date - old_date) / 1000; //计算时间差,并把毫秒转换成秒
            var days = parseInt(difftime / 86400); // å¤©  24*60*60*1000
            var hours = parseInt(difftime / 3600) - 24 * days;    // å°æ—¶ 60*60 æ€»å°æ—¶æ•°-过去的小时数=现在的小时数
            var minutes = parseInt(difftime % 3600 / 60); // åˆ†é’Ÿ -(day*24) ä»¥60秒为一整份 å–ä½™ å‰©ä¸‹ç§’æ•° ç§’æ•°/60 å°±æ˜¯åˆ†é’Ÿæ•°
            var seconds = parseInt(difftime % 60);  // ä»¥60秒为一整份 å–ä½™ å‰©ä¸‹ç§’æ•°
            return days + "天, " + hours + "小时, " + minutes + "分钟, " + seconds + "秒"
        }
        //准备排序
@@ -3414,9 +3843,18 @@
        //保存
        function set_AddNew() {
            var PCWithoutSource_date = new Date();
            //删除子表数据 ä¼šå ç”¨æ•°ç»„的位置,需要重新排一下顺序
            var num = [];
            for (var i = 0; i < option.data.length; i++) {
                if (option.data[i].物料名称.indexOf("\"") != -1 || option.data[i].物料名称.indexOf(";") != -1) {
                    option.data[i].物料名称 = option.data[i].物料名称.replaceAll("\"", "”").replaceAll(";", ";")
                }
                if (option.data[i].规格型号.indexOf("\"") != -1 || option.data[i].规格型号.indexOf(";") != -1) {
                    option.data[i].规格型号 = option.data[i].规格型号.replaceAll("\"", "”").replaceAll(";", ";")
                }
                if (option.data[i] != "") {
                    if (parseFloat(option.data[i]["拆单数量"]) == 0) {
                        return layer.msg("第" + (i + 1) + "行,拆产数量有误,请重新输入!");
@@ -3429,46 +3867,57 @@
                }
            }
            //var sMainStr = JSON.stringify(data.field);
            var sSubStr = JSON.stringify(num);
            var sMainSub = sSubStr +"&和" + sessionStorage["HUserName"];
            var HDataSub = [];
            var index = layer.load();
            $.ajax({
                type: "POST",
                url: GetWEBURL() + "/JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave",
                async: HAsync,
                data: { "sMainSub": sMainSub },
                dataType: "json",
                success: function (data) {
                    if (data.count == 1) {
                        if ($("#HWorkShopID").val() == 0) {
                            get_FastQuery(1);
                            HAsync = true;
            //JIT导入计划,每次导入100行数据,可修改i的自增值决定每次循环导入的行数
            for (var i = 0; i < option.data.length; i += 100) {
                HDataSub = option.data.slice(i, i + 100)
                console.log("开始保存前" + i + "条订单");
                var sSubStr = JSON.stringify(HDataSub);
                var sMainSub = sSubStr + "&和" + sessionStorage["HUserName"];
                $.ajax({
                    type: "POST",
                    url: GetWEBURL() + "/JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave",
                    async: HAsync,
                    data: { "sMainSub": sMainSub },
                    dataType: "json",
                    success: function (data) {
                        if (data.count == 1) {
                            //layer.close(index);
                            //layer.msg("保存成功");
                        }
                        else {
                            get_FastQuery(2);
                            //layer.close(index);
                            layer.msg(data.Message, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                            HAsync = true;
                        }
                        layer.close(index);
                        layer.msg("保存成功");
                    }
                    else {
                        layer.close(index);
                        layer.msg(data.Message, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                    },
                    error: function (err) {
                        //layer.close(index);
                        layer.msg("错误:" + err, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                        HAsync = true;
                    }
                },
                error: function (err) {
                    layer.close(index);
                    layer.msg("错误:" + err, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                    HAsync = true;
                }
            });
                });
            }
            layer.close(index);
            if ($("#HWorkShopID").val() == 0) {
                get_FastQuery(1);
                HAsync = true;
            }
            else {
                get_FastQuery(2);
                HAsync = true;
            }
            var HTimeDifference_PCSave2 = TimeDifference(PCWithoutSource_date);
            console.log("!!!!!!!!!!!排产为匹配到生产资源保存,用时为" + HTimeDifference_PCSave2);
        }
        function set_AddNew1(num) {
            var PCSuccess_date = new Date();
            //var sMainStr = JSON.stringify(data.field);
            var sSubStr = "["+JSON.stringify(num)+"]";
            var sMainSub = sSubStr + "&和" + sessionStorage["HUserName"];
@@ -3484,6 +3933,53 @@
                    if (data.count == 1) {
                        //layer.msg("保存成功");
                        //layer.close(index);
                    }
                    else {
                        //layer.close(index);
                        layer.msg(data.Message, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                    }
                },
                error: function (err) {
                    //layer.close(index);
                    layer.msg("错误:" + err, { icon: 5, btn: ['确认'], time: 100000, offset: 't', skin: 'layui-layer-lan', title: "温馨提示" });
                }
            });
            var HTimeDifference_PCSave = TimeDifference(PCSuccess_date);
            console.log("排产完成保存用时为:" + HTimeDifference_PCSave);
        }
        function set_AddNew_HJPC(num) {
            //var sMainStr = JSON.stringify(data.field);
            var sSubStr = "[" + JSON.stringify(num) + "]";
            var sMainSub = sSubStr + "&和" + sessionStorage["HUserName"];
            //var index = layer.load();
            $.ajax({
                type: "POST",
                url: GetWEBURL() + "/JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave",
                async: false,
                data: { "sMainSub": sMainSub },
                dataType: "json",
                success: function (data) {
                    if (data.count == 1) {
                        $.ajax({
                            url: GetWEBURL() + '/JIT_DayPlanPlatFormBill/AddMouldOccupancyTime',
                            type: "GET",
                            async: false,
                            data: { "HMouldID": num.HMouldID, "HSourceID": num.HSourceID, "HICMOInterID": num.HICMOInterID, "HICMOEntryID": num.HICMOEntryID, "HICMOBillNo": num.生产订单号, "HMouldPCTime": num.HMouldPCTime },
                            success: function (data1) {
                                if (data1.count == 1) {
                                } else {
                                    layer.alert(data1.code + data1.Message, { icon: 5 });
                                }
                            }, error: function () {
                                layer.alert("接口请求失败!", { icon: 5 });
                            }
                        });
                    }
                    else {
                        //layer.close(index);
@@ -3993,7 +4489,17 @@
                    item.规格型号 = item.规格型号.replace(/[,]+/g, ',');  // å°†æ¢åˆ†å·æ›¿æ¢ä¸ºç©ºå­—符串
                }
            });
            for (let i = 19; i < option.cols[0].length - 42; i++)
            {
                option.cols[0][i].title = Format(option.cols[0][i].title, 'MM-dd').toString();
            }
            table.exportFile(ins.config.id, data, "xls");
            for (let i = 19; i < option.cols[0].length - 42; i++) {
                option.cols[0][i].title = Format(option.cols[0][i].title, 'MM.dd');
            }
        }
        //排产功能