1
duhe
2023-09-06 2ac90163499626a2e2c4b4a46219b944dc6c9b17
WebAPI/Controllers/CJGL/Cj_StationOutBillController.cs
@@ -7,9 +7,11 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Http;
using System.Windows.Forms;
using WebAPI.Models;
namespace WebAPI.Controllers
@@ -19,7 +21,7 @@
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        public DAL.ClsSc_ICMOBill BillOld = new DAL.ClsSc_ICMOBill();
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
@@ -39,6 +41,8 @@
                //反序列化
                string msg2 = "[" + sArray[0].ToString() + "]";
                string user = sArray[1].ToString();//用户名
                string OperationType = sArray[2].ToString();//类型
                string HComputerName = SystemInformation.ComputerName; //设备名称
                //判断是否有编辑权限
                if (!DBUtility.ClsPub.Security_Log("Cj_StationOutBill_Edit", 1, false, user))
@@ -50,6 +54,8 @@
                    return objJsonResult;
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();//开始计时
                List<StationBill> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StationBill>>(msg2);
                long HMainInterID = 0;
@@ -62,8 +68,8 @@
                int HBillStatus = 0;
                string HMaker = user;//制单人
                string HMouldNum = list[0].HMouldNum;//模穴号
                int HYear = 2021;
                double HPeriod = 1;
                int HYear = DateTime.Now.Year;
                double HPeriod = DateTime.Now.Month;
                string HRemark = list[0].HRemark;//备注
                string HSourceName = list[0].HSourceName;//生产资源名称
                double HPieceQty = list[0].HPieceQty;//进站PCS数
@@ -118,6 +124,12 @@
                double HWorkTimes = list[0].HWorkTimes;//工时
                long HQCCheckID = list[0].HQCCheckID;//检验员ID
                long HPRDOrgID = list[0].HPRDOrgID;//组织ID
                double HmaterOutqty = list[0].HmaterOutqty;//白坯发布
                double HProcPriceRate = list[0].HProcPriceRate;//工价系数
                int HTemporaryAreaID = list[0].HTemporaryAreaID;//暂放区
                LogService.Write($"1.字段赋值,用时" + sw.Elapsed );
                //单据完整性判断
                if (HProcExchInterID <= 0)
                {
@@ -190,7 +202,7 @@
                if (oSystemParameter.ShowBill(ref Ret))
                {
                    //判断客户不为龙山汽配
                    if (oSystemParameter.omodel.WMS_CampanyName != "龙山汽配") //系统参数
                    if (oSystemParameter.omodel.WMS_CampanyName != "龙山汽配"&& oSystemParameter.omodel.WMS_CampanyName != "瑞与祺") //系统参数
                    {
                        if (HSourceID == 0)
                        {
@@ -216,6 +228,18 @@
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
                    }
                    //判断客户为瑞与祺
                    else if (oSystemParameter.omodel.WMS_CampanyName == "瑞与祺")
                    {
                        if (HEmpID == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "操作员没有选择!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
@@ -277,7 +301,11 @@
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                //LogService.Write($"2.条件判断,用时" + sw.Elapsed);
                var num = 0;
                //LogService.Write($"3.事务开始,用时" + sw.Elapsed);
                //若MAINDI重复则重新获取
                oCN.BeginTran();
@@ -293,6 +321,7 @@
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    LogService.Write($"3.1,用时" + sw.Elapsed);
                    oCN.RunProc("Insert Into Sc_StationOutBillMain " +
                    "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate,HMouldNum" +
                    ",HYear,HPeriod,HRemark,HSourceName,HPieceQty,HWasterQty,HPlanPieceQty,HBadPNL" +
@@ -302,8 +331,9 @@
                    ",HSupID,HQty,HPrice,HMoney,HBadCount,HCenterID,HProcNo,HOrderProcNO,HSourceNameList" +
                    ",HMainSourceInterID,HMainSourceBillNo,HMainSourceBillType,HLastSubProc" +
                    ",HEmpID2,HEmpID3,HEmpID4,HEmpID5,HDSQty,HChongQty,HPriceRate,HWorkTimes,HQCCheckID,HMainInterID,HPRDOrgID" +
                        ",HmaterOutqty,HProcPriceRate,HTemporaryAreaID" +
                    ") " +
                    " values('" + BillType + "','" + HBillSubType + "'," + HInterID + ",'" + HBillNo + "'," + HBillStatus + ",getdate(),'" + HMaker + "',getdate(),'" + HMouldNum + "'" +
                    " values('" + BillType + "','" + (OperationType == "5" ? "SUB" : HBillSubType)  + "'," + HInterID + ",'" + HBillNo + "'," + HBillStatus + ",getdate(),'" + HMaker + "',getdate(),'" + HMouldNum + "'" +
                    "," + HYear + "," + HPeriod + ",'" + HRemark + "','" + HSourceName + "'," + HPieceQty + "," + HWasterQty + "," + HPlanPieceQty + "," + HBadPNL +
                    "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HProcPlanInterID + "," + HProcPlanEntryID + ",'" + HProcPlanBillNo + "'," + HProcExchInterID + "," + HProcExchEntryID +
                    ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + "," + HICMOQty + "," + HPlanQty + ",getdate()," + HSourceID + "," + HPayProcID +
@@ -311,7 +341,11 @@
                    "," + HSupID + "," + HQty + "," + HPrice + "," + HMoney + "," + HBadCount + "," + HCenterID + "," + HProcNo + ",'" + HOrderProcNO + "'" + ",'" + HSourceNameList + "'" +
                    "," + HMainSourceInterID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'," + Convert.ToString(HLastSubProc ? 1 : 0) +
                    "," + HEmpID2 + "," + HEmpID3 + "," + HEmpID4 + "," + HEmpID5 + "," + HDSQty + "," + HChongQty + "," + HPriceRate + "," + HWorkTimes + "," + HQCCheckID + "," + HMainInterID + "," + HPRDOrgID +
                    ") ");
                  ","+ HmaterOutqty + "," + HProcPriceRate + ","+ HTemporaryAreaID + ") ");
                    LogService.Write($"4.新增结束,用时" + sw.Elapsed);
                    LogService.Write("用户:" + user + ",日期:" + DateTime.Now + ",新增工序出站单据:" + HBillNo);
                    oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + user + "','" + HComputerName + "','" + "新增工序出站单据:" + HBillNo + "','LMES-工序出站模块','" + DBUtility.ClsPub.IPAddress + "','新增单据'", ref DBUtility.ClsPub.sExeReturnInfo);
                }
                else if ("Modify".Equals(eventType))
                {
@@ -339,11 +373,15 @@
                    ",HQty=" + HQty +
                    ",HDSQty=" + HDSQty +
                    ",HChongQty=" + HChongQty +
                    ",HTemporaryAreaID=" + HTemporaryAreaID +
                    ",HMouldNum='" + HMouldNum + "'" +
                    ",HSourceNameList='" + HSourceNameList + "'" +
                    ",HWasterQty=" + HWasterQty +
                    ",HLastSubProc=" + Convert.ToString(HLastSubProc ? 1 : 0) +
                    " where HInterID=" + HInterID);
                    LogService.Write("用户:" + user + ",日期:" + DateTime.Now + ",修改工序出站单据:" + HBillNo);
                    oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + user + "','" + HComputerName + "','" + "修改工序出站单据:" + HBillNo + "','LMES-工序出站模块','" + DBUtility.ClsPub.IPAddress + "','修改单据'", ref DBUtility.ClsPub.sExeReturnInfo);
                }
                else
                {
@@ -356,6 +394,7 @@
                }
                //主表
                //LogService.Write($"5.系统控制查询判断,用时" + sw.Elapsed);
                ds = oCN.RunProcReturn("exec h_p_MES_StationOutBill_QtyCtrl " + HInterID + "", "h_p_MES_StationOutBill_QtyCtrl");
                if (ds == null)
                {
@@ -366,6 +405,7 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //LogService.Write($"5.1,用时" + sw.Elapsed);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    oCN.RollBack();
@@ -375,6 +415,7 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //LogService.Write($"5.2,用时" + sw.Elapsed);
                if (ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBack"]) == "2")
                {
                    oCN.RollBack();
@@ -384,9 +425,13 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //LogService.Write($"6.判断结束,用时" + sw.Elapsed);
                oCN.RunProc("exec h_p_Mes_ProcessExchangeRelationQty_Out " + HInterID + ","+ num + "");
                //LogService.Write($"6.1,用时" + sw.Elapsed);
                oCN.RunProc("exec h_p_Mes_ProcessExchangeChangeStatus  " + HInterID + " ");
                //LogService.Write($"6.2,用时" + sw.Elapsed);
                oCN.RunProc("exec h_p_Mes_StationOutBillBackBarCode  " + HInterID + " ");
                //LogService.Write($"7.存储过程结束,用时" + sw.Elapsed);
                //判断是否是 末道工序 
                ds1 = oCN.RunProcReturn(" select HLastProc,HFstProc from Sc_ProcessExchangeBillMain a  " +
                                    " inner join Sc_ProcessExchangeBillSub b on a.HInterID=b.HInterID  " +
@@ -404,8 +449,10 @@
                            //判断末道 生产汇报汇报 是否勾选
                            if (oSystemParameter.omodel.MES_StationOutBill_LastAutoICMOReport == "Y") //系统参数   
                            {
                                //LogService.Write($"7.1,用时" + sw.Elapsed);
                                //同步生产汇报单
                                objJsonResult= (json)SaveICMOReportBill(HInterID.ToString(), user, HBillNo, HPRDOrgID.ToString());
                                objJsonResult = (json)SaveICMOReportBill(HInterID.ToString(), user, HBillNo, HPRDOrgID.ToString());
                                //LogService.Write($"7.2,用时" + sw.Elapsed);
                                if (objJsonResult.count == 0)
                                {
                                    oCN.RollBack();
@@ -415,12 +462,15 @@
                                    objJsonResult.data = null;
                                    return objJsonResult;
                                }
                                //LogService.Write($"8.生产汇报结束,用时" + sw.Elapsed);
                            }
                            //判断末道 生产入库 是否勾选
                            else if (oSystemParameter.omodel.MES_StationOutBill_AutoStockIn == "Y") //系统参数   
                            {
                                //LogService.Write($"8.1,用时" + sw.Elapsed);
                                //同步生产入库单
                                objJsonResult=(json)SaveProcdutInBill(HBillNo);
                                objJsonResult =(json)SaveProcdutInBill(HBillNo);
                                //LogService.Write($"8.2,用时" + sw.Elapsed);
                                if (objJsonResult.count == 0)
                                {
                                    oCN.RollBack();
@@ -430,14 +480,274 @@
                                    objJsonResult.data = null;
                                    return objJsonResult;
                                }
                                //LogService.Write($"9.生产入库结束,用时" + sw.Elapsed);
                            }
                           
                        }
                    }
                }
                //LogService.Write($"10.事务结束,用时" + sw.Elapsed);
                oCN.Commit();
                string sReturn = "";
                if (oSystemParameter.ShowBill(ref sReturn) == true)
                {
                    if (oSystemParameter.omodel.MES_StationOutBill_SaveAutoAddnew == "Y") //系统参数  页面保存自动变成新增状态
                    {
                        objJsonResult.Verify = "Y";
                    }
                    else
                    {
                        objJsonResult.Verify = "N";
                    }
                }
                sw.Stop();//结束计时
                LogService.Write($"11.参数查询结束,新增结束,用时" + sw.Elapsed);
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "新增单据成功!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #region 批量新增单据-保存按钮
        [Route("Cj_StationOutBill/BatchAddBill")]
        [HttpPost]
        public object BatchAddBill([FromBody] JObject oMain)
        {
            var _value = oMain["oMain"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                //反序列化
                string msg2 = "[" + sArray[0].ToString() + "]"; //表头
                string user = sArray[1].ToString();//用户名
                string msg3 = sArray[2].ToString(); //表体
                Int64 OperationType = Convert.ToInt64(sArray[3]); //操作类型
                //判断是否有编辑权限
                if (!DBUtility.ClsPub.Security_Log("Cj_StationOutBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无编辑权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                List<StationBill> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StationBill>>(msg2);
                List<StationBill> listSub = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StationBill>>(msg3);
                oCN.BeginTran();
                for (int i = 0;i < listSub.Count; i++)
                {
                    long HMainInterID = 0;
                    HMainInterID = list[0].HMainInterID;
                    string eventType = list[0].eventType;
                    string BillType = "3791";
                    string HBillSubType = "3791";
                    long HInterID = listSub[i].HInterID;//递入type得到的单据ID
                    string HBillNo = listSub[i].HBillNo;//递入type得到的单据号
                    int HBillStatus = 0;
                    string HMaker = user;//制单人
                    string HMouldNum = list[0].HMouldNum;//模穴号
                    int HYear = DateTime.Now.Year;
                    double HPeriod = DateTime.Now.Month;
                    string HRemark = list[0].HRemark;//备注
                    string HSourceName = listSub[i].HSourceName;//生产资源名称
                    double HPieceQty = list[0].HPieceQty;//进站PCS数
                    double HWasterQty = listSub[i].HWasterQty;//报废数量
                    double HPlanPieceQty = list[0].HPlanPieceQty;//进站PNL数
                    double HBadPNL = list[0].HBadPNL;//报废PNL数
                    long HICMOInterID = listSub[i].HICMOInterID;//任务单ID
                    string HICMOBillNo = listSub[i].HICMOBillNo;//任务单
                    int HProcPlanInterID = 0;
                    int HProcPlanEntryID = 0;
                    string HProcPlanBillNo = "";
                    long HProcExchInterID = listSub[i].lngBillKey;
                    long HProcExchEntryID = listSub[i].lngBillSubKey;
                    string HProcExchBillNo = listSub[i].HProcExchBillNo;//流转卡
                    long HMaterID = listSub[i].HMaterID;//产品ID
                    long HProcID = listSub[i].HProcID;//当前工序ID
                    double HICMOQty = listSub[i].HICMOQty;//任务单数量
                    double HPlanQty = list[0].HPlanQty;//移交PNL数
                    DateTime HStationOutTime = list[0].HStationOutTime;//汇报时间
                    long HSourceID = listSub[i].HSourceID;//生产资源ID
                    long HPayProcID = list[0].HPayProcID;//核算工序ID
                    long HGroupID = list[0].HGroupID;//班组ID
                    long HDeptID = 0;
                    long HEmpID = listSub[i].HEmpID;//操作员ID
                    string HBarCode = list[0].HBarCode;//条形码
                    string HAddr = "";
                    string HBarCodeMaker = "";
                    long HSourceID2 = list[0].HSourceID2;//生产资源2ID
                    long HSourceID3 = list[0].HSourceID3;//生产资源3ID
                    long HSourceID4 = list[0].HSourceID4;//生产资源4ID
                    long HSourceID5 = list[0].HSourceID5;//生产资源5ID
                    long HSupID = 0;
                    double HQty = listSub[i].HQty;//合格数量
                    double HPrice = 0;
                    double HMoney = 0;
                    double HBadCount = list[0].HBadCount;//不良数量
                    long HCenterID = listSub[0].HCenterID;//工作中心ID
                    string HProcNo = listSub[i].HProcNo;//流水号
                    string HOrderProcNO = list[0].HOrderProcNO;//订单跟踪号
                    string HSourceNameList = list[0].HSourceNameList;//设备清单
                    long HMainSourceInterID = list[0].HInterID;//递入type得到的单据ID
                    string HMainSourceBillNo = list[0].HBillNo;//递入type得到的单据号
                    string HMainSourceBillType = "3790";
                    bool HLastSubProc = list[0].HLastSubProc;//转下工序
                    long HEmpID2 = list[0].HEmpID2;//操作员2ID
                    long HEmpID3 = list[0].HEmpID3;//操作员3ID
                    long HEmpID4 = list[0].HEmpID4;//操作员4ID
                    long HEmpID5 = list[0].HEmpID5;//操作员5ID
                    double HDSQty = list[0].HDSQty;//折弯刀数
                    double HChongQty = list[0].HChongQty;//NCT冲次数
                    double HPriceRate = list[0].HPriceRate;//系数
                    double HWorkTimes = list[0].HWorkTimes;//工时
                    long HQCCheckID = list[0].HQCCheckID;//检验员ID
                    long HPRDOrgID = list[0].HPRDOrgID;//组织ID
                    var num = 0;
                    //若MAINDI重复则重新获取
                    if (OperationType == 1)//新增
                    {
                        num = 1;
                        if (!AllowbillNo(HBillNo))
                        {
                            oCN.RollBack();
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "单据号重复!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        oCN.RunProc("Insert Into Sc_StationOutBillMain " +
                        "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate,HMouldNum" +
                        ",HYear,HPeriod,HRemark,HSourceName,HPieceQty,HWasterQty,HPlanPieceQty,HBadPNL" +
                        ",HICMOInterID,HICMOBillNo,HProcPlanInterID,HProcPlanEntryID,HProcPlanBillNo,HProcExchInterID,HProcExchEntryID" +
                        ",HProcExchBillNo,HMaterID,HProcID,HICMOQty,HPlanQty,HStationOutTime,HSourceID,HPayProcID" +
                        ",HGroupID,HDeptID,HEmpID,HBarCode,HAddr,HBarCodeMaker,HBarCodeMakeDate,HSourceID2,HSourceID3,HSourceID4,HSourceID5" +
                        ",HSupID,HQty,HPrice,HMoney,HBadCount,HCenterID,HProcNo,HOrderProcNO,HSourceNameList" +
                        ",HMainSourceInterID,HMainSourceBillNo,HMainSourceBillType,HLastSubProc" +
                        ",HEmpID2,HEmpID3,HEmpID4,HEmpID5,HDSQty,HChongQty,HPriceRate,HWorkTimes,HQCCheckID,HMainInterID,HPRDOrgID" +
                        ") " +
                        " values('" + BillType + "','" + HBillSubType + "'," + HInterID + ",'" + HBillNo + "'," + HBillStatus + ",getdate(),'" + HMaker + "',getdate(),'" + HMouldNum + "'" +
                        "," + HYear + "," + HPeriod + ",'" + HRemark + "','" + HSourceName + "'," + HPieceQty + "," + HWasterQty + "," + HPlanPieceQty + "," + HBadPNL +
                        "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HProcPlanInterID + "," + HProcPlanEntryID + ",'" + HProcPlanBillNo + "'," + HProcExchInterID + "," + HProcExchEntryID +
                        ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + "," + HICMOQty + "," + HPlanQty + ",getdate()," + HSourceID + "," + HPayProcID +
                        "," + HGroupID + "," + HDeptID + "," + HEmpID + ",'" + HBarCode + "','" + HAddr + "','" + HBarCodeMaker + "',getdate()" + "," + HSourceID2 + "," + HSourceID3 + "," + HSourceID4 + "," + HSourceID5 +
                        "," + HSupID + "," + HQty + "," + HPrice + "," + HMoney + "," + HBadCount + "," + HCenterID + "," + HProcNo + ",'" + HOrderProcNO + "'" + ",'" + HSourceNameList + "'" +
                        "," + HMainSourceInterID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'," + Convert.ToString(HLastSubProc ? 1 : 0) +
                        "," + HEmpID2 + "," + HEmpID3 + "," + HEmpID4 + "," + HEmpID5 + "," + HDSQty + "," + HChongQty + "," + HPriceRate + "," + HWorkTimes + "," + HQCCheckID + "," + HMainInterID + "," + HPRDOrgID +
                        ") ");
                    }
                    else
                    {
                        oCN.RollBack();
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "操作类型无效!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //主表
                    ds = oCN.RunProcReturn("exec h_p_MES_StationOutBill_QtyCtrl " + HInterID + "", "h_p_MES_StationOutBill_QtyCtrl");
                    if (ds == null)
                    {
                        oCN.RollBack();
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "判断数量失败!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        oCN.RollBack();
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "判断数量失败!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBack"]) == "2")
                    {
                        oCN.RollBack();
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "数量控制," + ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBackRemark"]);
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    oCN.RunProc("exec h_p_Mes_ProcessExchangeRelationQty_Out " + HInterID + "," + num + "");
                    oCN.RunProc("exec h_p_Mes_ProcessExchangeChangeStatus  " + HInterID + " ");
                    oCN.RunProc("exec h_p_Mes_StationOutBillBackBarCode  " + HInterID + " ");
                    //判断是否是 末道工序
                    ds1 = oCN.RunProcReturn(" select HLastProc,HFstProc from Sc_ProcessExchangeBillMain a  " +
                                        " inner join Sc_ProcessExchangeBillSub b on a.HInterID=b.HInterID  " +
                                        " Where a.HInterID=" + HProcExchInterID + " and b.HEntryID=" + HProcExchEntryID + " ", "Sc_ProcessExchangeBillMain");
                    //判断是否是新增
                    if (OperationType == 1)
                    {
                        //判断是否是末道工序
                        if (ClsPub.isStrNull(ds1.Tables[0].Rows[0]["HLastProc"]) == "是")
                        {
                            //获取系统参数
                            string Return = "";
                            if (oSystemParameter.ShowBill(ref Return))
                            {
                                //判断末道 生产汇报汇报 是否勾选
                                if (oSystemParameter.omodel.MES_StationOutBill_LastAutoICMOReport == "Y") //系统参数
                                {
                                    //同步生产汇报单
                                    objJsonResult = (json)SaveICMOReportBill(HInterID.ToString(), user, HBillNo, HPRDOrgID.ToString());
                                    if (objJsonResult.count == 0)
                                    {
                                        oCN.RollBack();
                                        objJsonResult.code = "0";
                                        objJsonResult.count = 0;
                                        objJsonResult.Message = objJsonResult.Message;
                                        objJsonResult.data = null;
                                        return objJsonResult;
                                    }
                                }
                                //判断末道 生产入库 是否勾选
                                else if (oSystemParameter.omodel.MES_StationOutBill_AutoStockIn == "Y") //系统参数
                                {
                                    //同步生产入库单
                                    objJsonResult = (json)SaveProcdutInBill(HBillNo);
                                    if (objJsonResult.count == 0)
                                    {
                                        oCN.RollBack();
                                        objJsonResult.code = "0";
                                        objJsonResult.count = 0;
                                        objJsonResult.Message = objJsonResult.Message;
                                        objJsonResult.data = null;
                                        return objJsonResult;
                                    }
                                }
                            }
                        }
                    }
                }
                oCN.Commit();
                string sReturn = "";
                if (oSystemParameter.ShowBill(ref sReturn) == true)
@@ -467,6 +777,7 @@
                return objJsonResult;
            }
        }
        #endregion
        #region 末道工序 生产汇报
        public object SaveICMOReportBill(string InterID, string user, string BillNo, string OrganizationID)
@@ -1204,9 +1515,34 @@
                    return objJsonResult;
                }
                //获取系统参数
                string Ret = "";
                if (oSystemParameter.ShowBill(ref Ret))
                {
                    //判断客户为龙山汽配
                    if (oSystemParameter.omodel.WMS_CampanyName == "龙山汽配")
                    {
                        //获取需要拼接的字符串
                        string sql_splice = DBUtility.ClsPub.SpliceSQL(user, "工序汇报单列表");
                        if (sql_splice == "没有查询到相关用户信息")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "没有查询到相关用户信息!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            sWhere += sql_splice;
                        }
                    }
                }
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList  order by hmainid desc", "h_v_MES_StationOutBillList");
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList where 1 = 1" + sWhere + "order by hmainid desc", "h_v_MES_StationOutBillList");
                }
                else
                {
@@ -1610,7 +1946,7 @@
        {
            try
            {
                string HComputerName = SystemInformation.ComputerName; //设备名称
                //编辑权限
                if (!DBUtility.ClsPub.Security_Log("Cj_StationOutBill_Drop", 1, false, HDeleteMan))
                {
@@ -1623,14 +1959,22 @@
                DAL.ClsSc_StationOutBill oBill = new DAL.ClsSc_StationOutBill();
                if (oBill.ShowBill(HInterID, ref DBUtility.ClsPub.sExeReturnInfo))
                {
                    if (oBill.omodel.HMaker != HDeleteMan && (HDeleteMan != "admin" && HDeleteMan != "Admin"))
                    string sReturn = "";
                    if (oSystemParameter.ShowBill(ref sReturn))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "只能删除本人的单据!";
                        objJsonResult.data = null;
                        return objJsonResult;
                        if (oSystemParameter.omodel.MES_StationOutBill_DeleteCtl == "Y")
                        {
                            if (oBill.omodel.HMaker != HDeleteMan && (HDeleteMan != "admin" && HDeleteMan != "Admin"))
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 0;
                                objJsonResult.Message = "只能删除本人的单据!";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
                    }
                    SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                    DataSet ds;
@@ -1672,8 +2016,6 @@
                        return objJsonResult;
                    }
                    //写入删除日志
                    string WorkList = "删除出站单:" + oBill.omodel.HBillNo;
                    if (!oBill.DeleteBill(oBill.omodel.HInterID, oBill.omodel.HProcExchInterID, oBill.omodel.HProcExchEntryID, ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
@@ -1684,6 +2026,10 @@
                    }
                    else
                    {
                        //写入删除日志
                        LogService.Write("用户:" + HDeleteMan + ",日期:" + DateTime.Now + ",删除工序出站单据:" + oBill.omodel.HBillNo);
                        oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + HDeleteMan + "','" + HComputerName + "','" + "删除工序出站单据:" + oBill.omodel.HBillNo + "','LMES-工序出站模块','" + DBUtility.ClsPub.IPAddress + "','删除单据'", ref DBUtility.ClsPub.sExeReturnInfo);
                        objJsonResult.code = "1";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "删除成功!";
@@ -1825,7 +2171,7 @@
        #region 审核
        [Route("Cj_StationOutBill/set_CheckBill")]
        [HttpGet]
        public object set_CheckBill(int HInterID, string CurUserName)
        public object set_CheckBill(string HInterID, string CurUserName)
        {
            try
            {
@@ -1839,54 +2185,64 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (HInterID == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据ID为0,不能审核!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                var HBillNo = HInterID.Split(',');
                DAL.ClsSc_StationOutBill oBill = new DAL.ClsSc_StationOutBill();
                //查看是否已审核,关闭,作废
                if (oBill.ShowBill(HInterID, ref DBUtility.ClsPub.sExeReturnInfo))
                for (int i = 0; i < HBillNo.Length; i++)
                {
                    if (oBill.omodel.HChecker.Trim() != "")
                    if (HBillNo[i] == "0")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据已审核!不能再次审核";
                        objJsonResult.Message = "第"+(i+1)+"行:单据ID为0,不能审核!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //查看是否已审核,关闭,作废
                    if (oBill.ShowBill(long.Parse(HBillNo[i]), ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        if (oBill.omodel.HChecker.Trim() != "")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "第" + (i + 1) + "行:单据已审核!不能再次审核";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "第" + (i + 1) + "行:单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                int j = 0;
                for (int i = 0; i < HBillNo.Length; i++)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //审核
                if (Cj_StationOutBillCheckBill(HInterID, CurUserName, oBill.omodel.HLastSubProc, ref DBUtility.ClsPub.sExeReturnInfo))
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "审核成功!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                    //审核
                    if (Cj_StationOutBillCheckBill(long.Parse(HBillNo[i]), CurUserName, oBill.omodel.HLastSubProc, ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        j += 1;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "成功:" + j + "行,第" + (i + 1) + "行:审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "全部执行成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
@@ -1943,7 +2299,7 @@
        #region 反审核
        [Route("Cj_StationOutBill/set_AbandonCheck")]
        [HttpGet]
        public object set_AbandonCheck(int HInterID, string CurUserName)
        public object set_AbandonCheck(string HInterID, string CurUserName)
        {
            try
            {
@@ -1958,52 +2314,82 @@
                    return objJsonResult;
                }
                if (HInterID == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据ID为0,不能反审核!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                var HBillNo = HInterID.Split(',');
                DAL.ClsSc_StationOutBill oBill = new DAL.ClsSc_StationOutBill();
                //查看是否已审核,关闭,作废
                if (oBill.ShowBill(HInterID, ref DBUtility.ClsPub.sExeReturnInfo))
                //获取系统参数
                string Ret = "";
                oSystemParameter.ShowBill(ref Ret);
                    for (int i = 0; i < HBillNo.Length; i++)
                {
                    if (oBill.omodel.HChecker.Trim() == "")
                    if (long.Parse(HBillNo[i]) == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据未审核!不需要反审核!";
                        objJsonResult.Message = "第" + (i + 1) + "行:单据ID为0,不能反审核!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //查看是否已审核,关闭,作废
                    if (oBill.ShowBill(long.Parse(HBillNo[i]), ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        if (oBill.omodel.HChecker.Trim() == "")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "第" + (i + 1) + "行:单据未审核!不需要反审核!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        //审核人必须和反审核人保持一致  乔一
                        if (oSystemParameter.omodel.WMS_CampanyName == "乔一")
                        {
                            if (oBill.omodel.HChecker.Trim() != CurUserName)
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 0;
                                objJsonResult.Message = "第" + (i + 1) + "行:审核人和反审核人不一致!";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "第" + (i + 1) + "行:单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                int j = 0;
                for (int i = 0; i < HBillNo.Length; i++)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                    //反审核
                    if (Cj_StationOutBillAbandonCheckBill(long.Parse(HBillNo[i]), CurUserName, oBill.omodel.HLastSubProc, ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        j += 1;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "成功:" + j + "行,第" + (i + 1) + "行:反审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                //反审核
                if (Cj_StationOutBillAbandonCheckBill(HInterID, CurUserName, oBill.omodel.HLastSubProc, ref DBUtility.ClsPub.sExeReturnInfo))
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "反审核成功!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "反审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "全部执行成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
@@ -2061,6 +2447,115 @@
            }
        }
        #region 工序出站关闭/反关闭功能
        [Route("Cj_StationOutBill/CloseCj_StationOutBill")]
        [HttpGet]
        public object CloseCj_StationOutBill(string HInterID, int Type, string user)
        {
            try
            {
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("Cj_StationOutBill_Close", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限关闭!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (string.IsNullOrWhiteSpace(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HInterID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                ClsPub.CurUserName = user;
                BillOld.MvarItemKey = "Sc_StationOutBillMain";
                oCN.BeginTran();//开始事务
                //Type 1 关闭  2  反关闭
                if (Type == 1)
                {
                    //判断单据是否已经关闭
                    DataSet ds;
                    string sql = "select * from " + BillOld.MvarItemKey + " where HinterID = " + HInterID;
                    ds = oCN.RunProcReturn(sql, BillOld.MvarItemKey);
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["HCloseMan"] != null && ds.Tables[0].Rows[0]["HCloseMan"].ToString() != "")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "单据已关闭!不需要再关闭!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                    //关闭单据
                    if (!BillOld.CloseBill(Int64.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "关闭失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    //判断单据是否已经反关闭
                    DataSet ds;
                    string sql = "select * from " + BillOld.MvarItemKey + " where HinterID = " + HInterID;
                    ds = oCN.RunProcReturn(sql, BillOld.MvarItemKey);
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["HCloseMan"] == null || ds.Tables[0].Rows[0]["HCloseMan"].ToString() == "")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "单据已反关闭!不需要再反关闭!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                    //反关闭单据
                    if (!BillOld.CancelClose(Int64.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "反关闭失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                oCN.Commit();//提交事务
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "执行成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "执行失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        //是否被关联
        public bool Cj_StationInBillisUse(Int64 lngBillKey, ref string sReturn)
        {