duhe
2023-09-07 2fa45c3a43e907ad22dcfada5be88c0f31115e31
WebAPI/Controllers/Sc_ProcessMangementController.cs
@@ -6,10 +6,12 @@
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.Http;
using WebAPI.DLL;
using WebAPI.Models;
using WebAPI.Service;
using Kingdee.BOS.WebApi.Client;
namespace WebAPI.Controllers
{
@@ -18,8 +20,8 @@
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        public DAL.ClsSc_ICMOBill BillOld = new DAL.ClsSc_ICMOBill();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
@@ -98,6 +100,51 @@
            return objJsonResult;
        }
        #region 末道工序汇报入库
        /// <summary>
        /// 末道工序汇报入库列表
        /// </summary>
        /// <param name="sWhere"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [Route("Sc_ProcessMangement/Sc_ProcessReportList_Last")]
        [HttpGet]
        public object Sc_ProcessReportList_Last(string sWhere, string user)
        {
            try
            {
                //判断是否有查询权限
                if (!DBUtility.ClsPub.Security_Log("Cj_StationOutBill_Query", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查询权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                string sql1 = "select * from h_v_Sc_ProcessReportList_Last where 1 = 1  and HLastProc='是'";
                string sql = sql1 + sWhere + "  order by hmainid desc";
                ds = oCN.RunProcReturn(sql, "h_v_Sc_ProcessReportList_Last");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region 工序汇报入库
        /// <summary>
        /// 获取工序汇报入库单列表
@@ -121,16 +168,11 @@
                    return objJsonResult;
                }
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList_LastProc  order by hmainid desc", "h_v_MES_StationOutBillList_LastProc");
                }
                else
                {
                    string sql1 = "select * from h_v_MES_StationOutBillList_LastProc where 1 = 1 ";
                    string sql = sql1 + sWhere + "  order by hmainid desc";
                    ds = oCN.RunProcReturn(sql, "h_v_MES_StationOutBillList_LastProc");
                }
                string sql1 = "select * from h_v_Sc_ProcessReportList_Last where 1 = 1 and HFstProc='是' ";
                string sql = sql1 + sWhere + "  order by hmainid desc";
                ds = oCN.RunProcReturn(sql, "h_v_Sc_ProcessReportList_Last");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
@@ -148,6 +190,210 @@
            }
        }
        #endregion
        #region 斯莫尔 末道工序汇报列表  汇报 入库
        /// <summary>
        /// 生成 生产汇报单
        /// 同步金蝶云 工序汇报入库单 入库
        /// </summary>
        /// <param name="InterID"></param>
        /// <param name="user"></param>
        /// <param name="BillNo"></param>
        /// <param name="OrganizationID"></param>
        /// <returns></returns>
        //[Route("Sc_ProcessMangement/SRM_SaveICMOReportBill")]
        //[HttpGet]
        //public object SRM_SaveICMOReportBill(string InterID, string user, string BillNo, string OrganizationID)
        //{
        //    try
        //    {
        //        //获取生产汇报单最大InterID和单据号
        //        Int64 HInterID = DBUtility.ClsPub.CreateBillID("3711", ref DBUtility.ClsPub.sExeReturnInfo);
        //        string HBillNo = DBUtility.ClsPub.CreateBillCode("3711", ref DBUtility.ClsPub.sExeReturnInfo, true);
        //        //获取组织代码
        //        string OrganizationNUM = oCN.RunProcReturn("select HNumber from Xt_ORGANIZATIONS where HItemID=" + OrganizationID, "Xt_ORGANIZATIONS").Tables[0].Rows[0]["HNumber"].ToString();
        //        //根据工序汇报单主ID获取工序汇报入库单的数据
        //        DataSet ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList_LastProc where HInterID=" + InterID, "h_v_MES_StationOutBillList_LastProc");
        //        DataRow dr = ds.Tables[0].Rows[0];
        //        //保存
        //        oCN.BeginTran();
        //        DataSet DsTable = oCN.RunProcReturn($"select * from Sc_ICMOReportBillMain where HBillNo='{HBillNo}'", "Sc_ICMOReportBillMain");
        //        if (DsTable.Tables[0].Rows.Count > 0)
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "已入库,请不要重复入库";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        //生产汇报单主表
        //        oCN.RunProc("Insert Into Sc_ICMOReportBillMain   " +
        //    "(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
        //    ",HYear,HPeriod,HRemark,HEmpID,HEmpNumber" +
        //    ",HGroupID,HDeptID,HDeptNumber" +
        //    ",HMainSourceBillNo,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillType" +
        //    ") " +
        //    " values('3711','3711'," + HInterID.ToString() + ",'" + BillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
        //    ",DATENAME(YEAR,GETDATE()),0,'','" + dr["HEmpID"].ToString() + "','" + dr["操作员代码"].ToString() +
        //    "','" + dr["HGroupID"].ToString() + "',0,''" +
        //    ",'" + BillNo.ToString() + "'," + InterID.ToString() + ", 0,'3791'" +
        //    ") ");
        //        //生产汇报单子表
        //        oCN.RunProc("Insert into Sc_ICMOReportBillSub " +
        //              " (HInterID,HEntryID,HMaterID,HMaterNumber" +
        //              ",HQty,HUnitID,HUnitNumber,HTimes,HSourceID" +
        //              ",HQtyMust,HWorkerID,HWorkerNumber,HBadCount,HWasterQty," +
        //              "HCloseMan,HCloseType,HRemark," +
        //              "HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
        //              ",HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo" +
        //              ",HICMOInterID,HICMOBillNo,HBarCode" +
        //              ") values("
        //              + HInterID.ToString() + ",1," + dr["HMaterID"].ToString() + ",'" + dr["产品代码"].ToString() + "'" +
        //              "," + dr["合格数量"].ToString() + ",0,'',0,0" +
        //              "," + dr["接收数量"].ToString() + "," + dr["HEmpID"].ToString() + ",'" + dr["操作员代码"].ToString() + "'," + dr["不良数量"].ToString() + "," + dr["报废数量"].ToString() +
        //              ",'',0,''" +
        //              "," + InterID.ToString() + ",0,'" + BillNo.ToString() + "','3791',0,0" +
        //              ",0,0,''" +
        //              "," + dr["HICMOInterID"].ToString() + ",'" + dr["任务单"].ToString() + "',''" +
        //              ") ");
        //        //同步金蝶
        //        //访问金蝶
        //        var loginRet = InvokeHelper.Login();
        //        var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
        //        if (isSuccess == 0)
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "操作失败,金蝶账号登录异常。" + loginRet;
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        //根据任务单查找到金蝶的生产订单
        //        DataSet ds1 = oCN.RunProcReturn("select * from  h_v_TOERP_StationOutBillList_LastProc where HICMOEntryID=" + dr["HICMOEntryID"].ToString(), "h_v_TOERP_StationOutBillList_LastProc");
        //        DataRow dr1 = ds1.Tables[0].Rows[0];
        //        JObject model = new JObject();
        //        model.Add("FBillType", new JObject() { ["Fnumber"] = "SCHBD01_SYS" }); //单据类型生产汇报“SCHBD02_SYS” 入库汇报SCHBD01_SYS
        //        model.Add("FPrdOrgId", new JObject() { ["Fnumber"] = dr1["FPrdOrgNUMBER"].ToString() }); //生产组织1
        //        model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期1
        //        model.Add("FBillNo", BillNo);
        //        JArray Fentity = new JArray();
        //        foreach (DataRow item in ds.Tables[0].Rows)
        //        {
        //            JObject FentityModel = new JObject();
        //            FentityModel.Add("FIsNew", false);//  源单类型
        //            FentityModel.Add("FReportType", new JObject() { ["Fnumber"] = dr1["FREPORTTYPENUMBER"].ToString() });//生产汇报类型
        //            FentityModel.Add("FSrcBillType", "PRD_MO");//  源单类型
        //            FentityModel.Add("FProductType", "1");//  产品类型
        //            FentityModel.Add("FSrcBillNo", item["任务单"].ToString());//  源单编号
        //            FentityModel.Add("FSrcInterId", dr1["FMOID"].ToString());//  源单内码
        //            FentityModel.Add("FSrcEntryId", dr1["FMOENTRYID"].ToString());//  源单分录内码、
        //            FentityModel.Add("FSRCENTRYSEQ", dr1["FMOENTRYSEQ"].ToString());//  源单分录行号
        //            FentityModel.Add("FUNITID", new JObject() { ["Fnumber"] = dr1["FUNITNUMBER"].ToString() });//单位
        //            FentityModel.Add("FTimeUnitId", "1");//时间单位
        //            FentityModel.Add("FWorkshipId", new JObject() { ["Fnumber"] = dr1["FWorkShopNUM"].ToString() }); //  生产车间
        //            FentityModel.Add("FStandHourUnitId", "3600"); // 单位标准工时单位
        //            FentityModel.Add("FMaterialId", new JObject() { ["Fnumber"] = dr1["FMaterialNUM"].ToString() }); // 物料编码
        //            FentityModel.Add("FMoEntrySeq", dr1["FMOENTRYSEQ"].ToString());//生产订单行号
        //            FentityModel.Add("FMoId", dr1["HICMOInterID"].ToString());//生产订单内码
        //            FentityModel.Add("FFinishQty", item["接收数量"].ToString());//完成数量1
        //            FentityModel.Add("FQuaQty", item["合格数量"].ToString());//合格数量1FFailQty
        //            FentityModel.Add("FFailQty", item["不良数量"].ToString());//不合格数量
        //            FentityModel.Add("FStockInOrgId ", new JObject() { ["Fnumber"] = OrganizationNUM });// 入库组织
        //            FentityModel.Add("FStockId", new JObject() { ["Fnumber"] = dr1["FStockNUM"].ToString() }); // 仓库
        //            FentityModel.Add("FMOBILLNO", dr1["FMOBILLNO"].ToString());//
        //            FentityModel.Add("FMOENTRYID", dr1["FMOENTRYID"].ToString());//
        //            FentityModel.Add("FOwnerTypeId", dr1["FOWNERTYPEID"].ToString()); //货主类型:FOwnerTypeId(必填项)
        //            FentityModel.Add("FOwnerId", new JObject() { ["Fnumber"] = dr1["FOwnerNumber"].ToString() }); //货主:FOwnerId(必填项)
        //            FentityModel.Add("FBomId", new JObject() { ["F    number"] = dr1["FBOMNUM"].ToString() }); //BOM版本:FBomId(必填项)
        //            FentityModel.Add("FCostRate", dr1["FCostRate"].ToString());// 成本权重
        //            FentityModel.Add("FISBACKFLUSH", dr1["FISBACKFLUSH"].ToString() == "1" ? true : false);// 倒冲领料
        //            FentityModel.Add("FMOMAINENTRYID", dr1["FMOENTRYID"].ToString());//
        //            FentityModel.Add("FLot", new JObject() { ["FNumber"] = dr1["FBATCHNO"].ToString() }); //批号
        //            JArray Fentity2 = new JArray();
        //            JObject FentityModel2 = new JObject();
        //            FentityModel2.Add("FEntity_Link_FFlowId", "f6e6eec3-5267-4f02-8593-b633da508a72");
        //            FentityModel2.Add("FEntity_Link_FFlowLineId", "PRD_MO2MORPT");
        //            FentityModel2.Add("FEntity_Link_FRuleId", "3");
        //            FentityModel2.Add("FEntity_Link_FSTableId", "0");
        //            FentityModel2.Add("FEntity_Link_FSTableName", "T_PRD_MOENTRY");
        //            FentityModel2.Add("FEntity_Link_FSBillId", dr1["FMOID"].ToString());
        //            FentityModel2.Add("FEntity_Link_FSId", dr1["FMOENTRYID"].ToString());
        //            FentityModel2.Add("FEntity_Link_FBaseQuaQtyOld", item["合格数量"].ToString());
        //            FentityModel2.Add("FEntity_Link_FBaseQuaQty", item["合格数量"].ToString());
        //            Fentity2.Add(FentityModel2);
        //        }
        //        model.Add("FEntity", Fentity); //明细信息
        //        JObject jsonRoot = new JObject()
        //        {
        //            ["Creator"] = "",
        //            ["NeedUpDateFields"] = new JArray(),
        //            ["NeedReturnFields"] = new JArray(),
        //            ["IsDeleteEntry"] = "false",
        //            ["SubSystemId"] = "",
        //            ["IsVerifyBaseDataField"] = "false",
        //            //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
        //            ["Model"] = model
        //        };
        //        string result = InvokeHelper.Save("PRD_MORPT", JsonConvert.SerializeObject(jsonRoot));//保存
        //        //判断保存是否成功
        //        if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
        //        {
        //            LogService.Write("工序汇报单入库错误jsonRoot:" + jsonRoot);
        //            oCN.RollBack();
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = $"生产汇报入库单同步金蝶云失败!单号:{dr["单据号"].ToString()}" + result;
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        //提交审核
        //        string result1 = string.Empty;
        //        string result2 = string.Empty;
        //        var fID = JObject.Parse(result)["Result"]["Id"].ToString();
        //        var fBillNo = JObject.Parse(result)["Result"]["Number"].ToString();
        //        var json = new
        //        {
        //            Ids = fID,
        //        };
        //        result1 = InvokeHelper.Submit("PRD_MORPT", JsonConvert.SerializeObject(json));//提交
        //        result2 = InvokeHelper.Audit("PRD_MORPT", JsonConvert.SerializeObject(json));//提交
        //        if (JObject.Parse(result1)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
        //        {
        //            oCN.RollBack();
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = $"生产汇报单单号:{fBillNo},提交失败" + result;
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        oCN.RunProc("update Sc_StationOutBillMain set HRelationQty=1 where  HBillNo='" + BillNo + "'");
        //        oCN.Commit();
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 1;
        //        objJsonResult.Message = "保存成功!";
        //        objJsonResult.data = 1;
        //        return objJsonResult;
        //    }
        //    catch (Exception e)
        //    {
        //        oCN.RollBack();
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "Exception!" + e.ToString();
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //}
        #endregion
        /// <summary>
        /// 入库——生产汇报单
        /// </summary>
@@ -159,6 +405,10 @@
        {
            try
            {
                string sErr = "";
                oSystemParameter.ShowBill(ref sErr);
                //获取生产汇报单最大InterID和单据号
                Int64 HInterID = DBUtility.ClsPub.CreateBillID("3711", ref DBUtility.ClsPub.sExeReturnInfo);
                string HBillNo = DBUtility.ClsPub.CreateBillCode("3711", ref DBUtility.ClsPub.sExeReturnInfo, true);
@@ -166,21 +416,39 @@
                string OrganizationNUM = oCN.RunProcReturn("select HNumber from Xt_ORGANIZATIONS where HItemID=" + OrganizationID, "Xt_ORGANIZATIONS").Tables[0].Rows[0]["HNumber"].ToString();
                //根据工序汇报单主ID获取工序汇报入库单的数据
                DataSet ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList_LastProc where HInterID=" + InterID, "h_v_MES_StationOutBillList_LastProc");
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "生成生产汇报单过程中没有查询到出站单";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                DataRow dr = ds.Tables[0].Rows[0];
                //保存
                oCN.BeginTran();
                DataSet DsTable = oCN.RunProcReturn($"select * from Sc_ICMOReportBillMain where HBillNo='{HBillNo}'", "Sc_ICMOReportBillMain");
                if (DsTable.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "已入库,请不要重复入库";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //生产汇报单主表
                oCN.RunProc("Insert Into Sc_ICMOReportBillMain   " +
                "(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
                ",HYear,HPeriod,HRemark,HEmpID,HEmpNumber" +
                ",HGroupID,HDeptID,HDeptNumber" +
                ",HMainSourceBillNo,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillType" +
                ") " +
                " values('3711','3711'," + HInterID.ToString() + ",'" + HBillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
                ",DATENAME(YEAR,GETDATE()),0,'','" + dr["HEmpID"].ToString() + "','" + dr["操作员代码"].ToString() +
                "','" + dr["HGroupID"].ToString() + "',0,''" +
                ",'" + BillNo.ToString() + "'," + InterID.ToString() + ", 0,'3791'" +
                ") ");
            "(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
            ",HYear,HPeriod,HRemark,HEmpID,HEmpNumber" +
            ",HGroupID,HDeptID,HDeptNumber" +
            ",HMainSourceBillNo,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillType" +
            ") " +
            " values('3711','3711'," + HInterID.ToString() + ",'" + BillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
            ",DATENAME(YEAR,GETDATE()),0,'','" + dr["HEmpID"].ToString() + "','" + dr["操作员代码"].ToString() +
            "','" + dr["HGroupID"].ToString() + "',0,''" +
            ",'" + BillNo.ToString() + "'," + InterID.ToString() + ", 0,'3791'" +
            ") ");
                //生产汇报单子表
                oCN.RunProc("Insert into Sc_ICMOReportBillSub " +
                      " (HInterID,HEntryID,HMaterID,HMaterNumber" +
@@ -220,8 +488,8 @@
                model.Add("FPrdOrgId", new JObject() { ["Fnumber"] = dr1["FPrdOrgNUMBER"].ToString() }); //生产组织1
                model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期1
                model.Add("FHZYMESFLAG", "是");//  是否为MES同步
                model.Add("FBillNo", HBillNo);
                model.Add("FBillNo", BillNo);
                JArray Fentity = new JArray();
                foreach (DataRow item in ds.Tables[0].Rows)
@@ -256,21 +524,13 @@
                    FentityModel.Add("FBomId", new JObject() { ["Fnumber"] = dr1["FBOMNUM"].ToString() }); //BOM版本:FBomId(必填项)
                    FentityModel.Add("FCostRate", dr1["FCostRate"].ToString());// 成本权重
                    FentityModel.Add("FISBACKFLUSH", dr1["FISBACKFLUSH"].ToString() == "1" ? true : false);// 倒冲领料
                    FentityModel.Add("F_bsv_Base1", new JObject() { ["Fnumber"] = dr1["FPREBDONENUMBER"].ToString() }); //包装标识
                    FentityModel.Add("FLot", new JObject() { ["FNumber"] = dr1["FBATCHNO"].ToString() }); //包装标识
                    //批号
                    //FFLOWID FFLOWLINEID FRULEID FSTABLENAME
                    //f6e6eec3 - 5267 - 4f02 - 8593 - b633da508a72    3   PRD_MO2MORPT T_PRD_MOENTRY
                    //业务流程图:FEntity_Link_FFlowId
                    //推进路线:FEntity_Link_FFlowLineId
                    //转换规则:FEntity_Link_FRuleId
                    //源单表内码:FEntity_Link_FSTableId
                    //源单表:FEntity_Link_FSTableName
                    //源单内码:FEntity_Link_FSBillId
                    //源单分录内码:FEntity_Link_FSId
                    //原始携带量:FEntity_Link_FBaseQuaQtyOld
                    //修改携带量:FEntity_Link_FBaseQuaQty
                    FentityModel.Add("FMOMAINENTRYID", dr1["FMOENTRYID"].ToString());//
                    if (oSystemParameter.omodel.WMS_CampanyName == "瑞与祺")
                    {
                        FentityModel.Add("F_bsv_Base1", new JObject() { ["Fnumber"] = dr1["FPREBDONENUMBER"].ToString() });//包装标识
                    }
                    FentityModel.Add("FLot", new JObject() { ["FNumber"] = dr1["FBATCHNO"].ToString() }); //批号
                    FentityModel.Add("F_bsv_Text", dr1["工序流转卡号"].ToString()); //流转卡号
                    JArray Fentity2 = new JArray();
                    JObject FentityModel2 = new JObject();
                    FentityModel2.Add("FEntity_Link_FFlowId", "f6e6eec3-5267-4f02-8593-b633da508a72");
@@ -299,7 +559,7 @@
                    //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
                    ["Model"] = model
                };
                string result = InvokeHelper.Save("PRD_MORPT", JsonConvert.SerializeObject(jsonRoot));//保存
                //判断保存是否成功
                if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
@@ -333,6 +593,8 @@
                    return objJsonResult;
                }
                oCN.RunProc("update Sc_StationOutBillMain set HRelationQty=1 where  HBillNo='" + BillNo + "'");
                oCN.Commit();
                objJsonResult.code = "0";
                objJsonResult.count = 1;
@@ -350,7 +612,1129 @@
                return objJsonResult;
            }
        }
        #endregion
        /// <summary>
        /// 入库——产品入库单
        /// </summary>
        /// <param name="InterID">工序汇报单主ID</param>
        /// <returns></returns>
        [Route("Sc_ProcessMangement/SaveProcdutInBill")]
        [HttpGet]
        public object SaveProcdutInBill(string BillNo)
        {
            try
            {
                //获取生产汇报单最大InterID和单据号
                Int64 HInterID = DBUtility.ClsPub.CreateBillID("1202", ref DBUtility.ClsPub.sExeReturnInfo);
                string HBillNo = DBUtility.ClsPub.CreateBillCode("1202", ref DBUtility.ClsPub.sExeReturnInfo, true);
                ////获取组织代码
                //string OrganizationNUM = oCN.RunProcReturn("select HNumber from Xt_ORGANIZATIONS where HItemID=" + OrganizationID, "Xt_ORGANIZATIONS").Tables[0].Rows[0]["HNumber"].ToString();
                ////根据工序汇报单主ID获取工序汇报入库单的数据
                //DataSet ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList_LastProc where HInterID=" + InterID, "h_v_MES_StationOutBillList_LastProc");
                //DataRow dr = ds.Tables[0].Rows[0];
                //判断入库的合格数量是否为0
                var DTable = oCN.RunProcReturn("select * from  Sc_StationOutBillMain where HBillNo='" + BillNo + "' ", "Sc_StationOutBillMain").Tables[0];
                if (double.Parse(DTable.Rows[0]["HQty"].ToString()) == 0)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 2;
                    objJsonResult.Message = "合格数量为0,不需要入库!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //保存
                oCN.BeginTran();
                //生产汇报单主表
                //oCN.RunProc("Insert Into Sc_ICMOReportBillMain   " +
                //"(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
                //",HYear,HPeriod,HRemark,HEmpID,HEmpNumber" +
                //",HGroupID,HDeptID,HDeptNumber" +
                //",HMainSourceBillNo,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillType" +
                //") " +
                //" values('3711','3711'," + HInterID.ToString() + ",'" + HBillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
                //",DATENAME(YEAR,GETDATE()),0,'','" + dr["HEmpID"].ToString() + "','" + dr["操作员代码"].ToString() +
                //"','" + dr["HGroupID"].ToString() + "',0,''" +
                //",'" + BillNo.ToString() + "'," + InterID.ToString() + ", 0,'3791'" +
                //") ");
                ////生产汇报单子表
                //oCN.RunProc("Insert into Sc_ICMOReportBillSub " +
                //      " (HInterID,HEntryID,HMaterID,HMaterNumber" +
                //      ",HQty,HUnitID,HUnitNumber,HTimes,HSourceID" +
                //      ",HQtyMust,HWorkerID,HWorkerNumber,HBadCount,HWasterQty," +
                //      "HCloseMan,HCloseType,HRemark," +
                //      "HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                //      ",HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo" +
                //      ",HICMOInterID,HICMOBillNo,HBarCode" +
                //      ") values("
                //      + HInterID.ToString() + ",1," + dr["HMaterID"].ToString() + ",'" + dr["产品代码"].ToString() + "'" +
                //      "," + dr["合格数量"].ToString() + ",0,'',0,0" +
                //      "," + dr["接收数量"].ToString() + "," + dr["HEmpID"].ToString() + ",'" + dr["操作员代码"].ToString() + "'," + dr["不良数量"].ToString() + "," + dr["报废数量"].ToString() +
                //      ",'',0,''" +
                //      "," + InterID.ToString() + ",0,'" + BillNo.ToString() + "','3791',0,0" +
                //      ",0,0,''" +
                //      "," + dr["HICMOInterID"].ToString() + ",'" + dr["任务单"].ToString() + "',''" +
                //      ") ");
                //同步金蝶
                //访问金蝶
                var loginRet = InvokeHelper.Login();
                var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                if (isSuccess == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "操作失败,金蝶账号登录异常。" + loginRet;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //
                //DataSet ds1 = oCN.RunProcReturn("select * from  h_v_TOERPProcduct_LastProc where HLastProc = '是' and  hbillno='" + BillNo.ToString() + "'", "h_v_TOERPProcduct_LastProc");
                DataSet ds1 = oCN.RunProcReturn("exec h_p_TOERPProcduct_LastProc '" + BillNo + "'", "h_p_TOERPProcduct_LastProc");
                if (ds1 == null || ds1.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "未找到对应的本地或金蝶云生产汇报单记录;1.未查询到对应的生产汇报单;2.请确保当前工序是末道工序3.汇报单对应的入库数量已满 请在金蝶云查看入库记录!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                DataRow dr1 = ds1.Tables[0].Rows[0];
                if (double.Parse(dr1[0].ToString()) <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "未找到对应的生产汇报单记录;1.未查询到对应的生产汇报单;2.请确保当前工序是末道工序3.汇报单对应的入库数量已满 请在金蝶云查看入库记录!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                string sErr = "";
                if (oSystemParameter.ShowBill(ref sErr))
                {
                    if (oSystemParameter.omodel.WMS_CampanyName == "瑞与祺")
                    {
                        //判断金蝶云是否已经有入库的数据
                        ds = oCN.RunProcReturn($@"select * from AIS20220308151944..T_PRD_INSTOCK where FBILLNO='{BillNo}'", "T_PRD_INSTOCK");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "当前出站单号已入库,请不要重新入库!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        //判断金蝶云生产汇报单 数量 是否 大于 末道出站数量
                        ds = oCN.RunProcReturn($@"select sum(FFinishQty)-sum(FSTOCKINQUASELQTY)-(select HQty from Sc_StationOutBillMain where HBillNo='{BillNo}')
                        from  AIS20220308151944..T_PRD_MORPT rpt3
                        left join AIS20220308151944..T_PRD_MORPTENTRY  rpt2 on rpt3.FID = rpt2.FID
                        left join AIS20220308151944..T_PRD_MORPTENTRY_A  rpt1 on rpt2.FID = rpt1.FID
                        where F_bsv_Text=(select HProcExchBillNo from Sc_StationOutBillMain where HBillNo='{BillNo}')", "SumCount");
                        if (double.Parse(ds.Tables[0].Rows[0][0].ToString()) < 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "金蝶云汇报数量小于本地出站数量!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
                JObject model = new JObject();
                model.Add("FBillType", new JObject() { ["FNumber"] = "SCRKD01_SYS" }); //单据类型
                model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期
                model.Add("FStockOrgId", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //库存组织代码
                model.Add("FPrdOrgId", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //生产组织代码
                model.Add("FOwnerTypeId0", "BD_OwnerOrg");
                model.Add("FOwnerId0", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //
                model.Add("FIsEntrust", "false");//
                model.Add("FCurrId", new JObject() { ["FNumber"] = "PRE001" }); //
                model.Add("FBillNo", BillNo);
                JArray Fentity = new JArray();
                foreach (DataRow item in ds1.Tables[0].Rows)
                {
                    JObject FentityModel = new JObject();
                    FentityModel.Add("FSrcEntryId", item["HSourceEntryID"].ToString());//  源单分录内码、
                    FentityModel.Add("FIsNew", "false");//  源单类型
                    FentityModel.Add("FMaterialId", new JObject() { ["FNumber"] = item["HMaterNumber"].ToString() }); // 物料编码
                    FentityModel.Add("FCheckProduct", "false");//
                    FentityModel.Add("FInStockType", "1");//
                    FentityModel.Add("FProductType", "1");//
                    FentityModel.Add("FUNITID", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//单位
                    FentityModel.Add("FMustQty", item["数量"].ToString());//
                    FentityModel.Add("FRealQty", item["数量"].ToString());//
                    FentityModel.Add("FCostRate", "100");//
                    FentityModel.Add("FBaseUnitId", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//单位
                    FentityModel.Add("FBaseMustQty", item["数量"].ToString());//
                    FentityModel.Add("FBaseRealQty", item["数量"].ToString());//
                    FentityModel.Add("FOwnerTypeId", "BD_OwnerOrg");//
                    FentityModel.Add("FOwnerId", new JObject() { ["FNumber"] = item["HPrdOrgNumber"].ToString() });//
                    if (oSystemParameter.ShowBill(ref sErr))
                    {
                        if (oSystemParameter.omodel.WMS_CampanyName == "瑞与祺")
                        {
                            if (oSystemParameter.omodel.MES_StationOutBill_InStockType == "工艺路线")
                            {
                                FentityModel.Add("FStockId", new JObject() { ["FNumber"] = item["HStockNumbers"].ToString() }); // 仓库
                            }
                            else if (oSystemParameter.omodel.MES_StationOutBill_InStockType == "工序")
                            {
                                FentityModel.Add("FStockId", new JObject() { ["FNumber"] = item["HStockNumber"].ToString() }); // 仓库
                            }
                        }
                    }
                    FentityModel.Add("FLot", new JObject() { ["FNumber"] = item["HBatchNo"].ToString() }); //批号
                    FentityModel.Add("FISBACKFLUSH", "true");//
                    FentityModel.Add("FWorkShopId1", new JObject() { ["FNumber"] = item["HWorkShopNumber"].ToString() }); //  生产车间
                    FentityModel.Add("FMOBILLNO", item["HMOBillNo"].ToString());//
                    FentityModel.Add("FMoId", item["HICMOInterID"].ToString());//生产订单内码
                    FentityModel.Add("FMoEntryId", item["HMOEntryID"].ToString());//
                    FentityModel.Add("FMoEntrySeq", item["HMOEntrySEQ"].ToString());//生产订单行号
                    FentityModel.Add("FStockUnitId", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//库存单位
                    FentityModel.Add("FStockRealQty", item["数量"].ToString());//
                    FentityModel.Add("FSrcBillType", "PRD_MORPT");//
                    FentityModel.Add("FSrcBillNo", item["HSourceBillNo"].ToString());//
                    FentityModel.Add("FSrcInterId", item["HSourceInterID"].ToString());//
                    FentityModel.Add("FBasePrdRealQty", item["数量"].ToString());//
                    FentityModel.Add("FIsFinished", "false");//
                    FentityModel.Add("FStockStatusId", new JObject() { ["FNumber"] = "KCZT01_SYS" }); //
                    FentityModel.Add("FSrcEntrySeq", item["HSourceSeQ"].ToString());//  源单分录行号
                    FentityModel.Add("FMOMAINENTRYID", item["HMOEntryID"].ToString());//
                    FentityModel.Add("FKeeperTypeId", "BD_KeeperOrg");
                    FentityModel.Add("FKeeperId", new JObject() { ["FNumber"] = item["HPrdOrgNumber"].ToString() });//
                    FentityModel.Add("FIsOverLegalOrg", "false");//
                    FentityModel.Add("F_bsv_Base1", new JObject() { ["FNumber"] = item["HBZBS"].ToString() });//
                    FentityModel.Add("F_BSV_TEXT", item["HLZKH"].ToString());//
                    FentityModel.Add("F_BSV_TEXT1", item["HWYID"].ToString());//
                                                                              //批号
                                                                              //FFLOWID FFLOWLINEID FRULEID FSTABLENAME
                                                                              //f6e6eec3 - 5267 - 4f02 - 8593 - b633da508a72    3   PRD_MO2MORPT T_PRD_MOENTRY
                                                                              //业务流程图:FEntity_Link_FFlowId
                                                                              //推进路线:FEntity_Link_FFlowLineId
                                                                              //转换规则:FEntity_Link_FRuleId
                                                                              //源单表内码:FEntity_Link_FSTableId
                                                                              //源单表:FEntity_Link_FSTableName
                                                                              //源单内码:FEntity_Link_FSBillId
                                                                              //源单分录内码:FEntity_Link_FSId
                                                                              //原始携带量:FEntity_Link_FBaseQuaQtyOld
                                                                              //修改携带量:FEntity_Link_FBaseQuaQty
                    JArray Fentity2 = new JArray();
                    JObject FentityModel2 = new JObject();
                    FentityModel2.Add("FEntity_Link_FFlowId", "f6e6eec3-5267-4f02-8593-b633da508a72");
                    FentityModel2.Add("FEntity_Link_FFlowLineId", "5");
                    FentityModel2.Add("FEntity_Link_FRuleId", "PRD_MORPT2INSTOCK");
                    FentityModel2.Add("FEntity_Link_FSTableName", "T_PRD_MORPTENTRY");
                    FentityModel2.Add("FEntity_Link_FSTableId", "0");
                    FentityModel2.Add("FEntity_Link_FSBillId", item["HSourceInterID"].ToString());
                    FentityModel2.Add("FEntity_Link_FSId", item["HSourceEntryID"].ToString());
                    FentityModel2.Add("FEntity_Link_FBasePrdRealQtyOld", item["关联数量"].ToString());
                    FentityModel2.Add("FEntity_Link_FBasePrdRealQty", item["数量"].ToString());
                    Fentity2.Add(FentityModel2);
                    FentityModel.Add("FEntity_Link", Fentity2);
                    FentityModel.Add("FBFLowId", new JObject() { ["FID"] = "f6e6eec3-5267-4f02-8593-b633da508a72" }); //
                    Fentity.Add(FentityModel);
                    //Fentity.Add(FentityModel);
                }
                model.Add("FEntity", Fentity); //明细信息
                JObject jsonRoot = new JObject()
                {
                    ["Creator"] = "",
                    ["NeedUpDateFields"] = new JArray(),
                    ["NeedReturnFields"] = new JArray(),
                    //["IsDeleteEntry"] = "true",
                    //["SubSystemId"] = "",
                    //["IsVerifyBaseDataField"] = "false",
                    ["IsDeleteEntry"] = "true",
                    ["SubSystemId"] = "",
                    ["IsVerifyBaseDataField"] = "true",
                    ["IsEntryBatchFill"] = "false",
                    ["ValidateFlag"] = "true",
                    ["NumberSearch"] = "true",
                    ["IsAutoAdjustField"] = "false",
                    ["InterationFlags"] = "",
                    ["IgnoreInterationFlag"] = "",
                    //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
                    ["Model"] = model
                };
                string result = InvokeHelper.Save("PRD_INSTOCK", JsonConvert.SerializeObject(jsonRoot));//保存
                //判断保存是否成功
                if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    LogService.Write("生产入库错误jsonRoot:" + jsonRoot);
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"生产入库单同步金蝶云失败!单号:{HBillNo.ToString()}" + result + jsonRoot;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //提交审核
                string result1 = string.Empty;
                string result2 = string.Empty;
                var fID = JObject.Parse(result)["Result"]["Id"].ToString();
                var fBillNo = JObject.Parse(result)["Result"]["Number"].ToString();
                var json = new
                {
                    Ids = fID,
                };
                result1 = InvokeHelper.Submit("PRD_INSTOCK", JsonConvert.SerializeObject(json));//提交
                //result2 = InvokeHelper.Audit("PRD_INSTOCK", JsonConvert.SerializeObject(json));//提交
                if (JObject.Parse(result1)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"生产入库单单号:{fBillNo},提交失败" + result;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.RunProc("update Sc_StationOutBillMain set HRelationQty=1 where  HBillNo='" + BillNo + "'");
                oCN.Commit();
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// <summary>
        /// 入库——产品入库单
        /// </summary>
        /// <param name="InterID">工序汇报单主ID</param>
        /// <returns></returns>
        [Route("Sc_ProcessMangement/SaveBFBill")]
        [HttpGet]
        public object SaveBFBill(string BillNo)
        {
            try
            {
                //获取生产汇报单最大InterID和单据号
                Int64 HInterID = DBUtility.ClsPub.CreateBillID("1202", ref DBUtility.ClsPub.sExeReturnInfo);
                string HBillNo = DBUtility.ClsPub.CreateBillCode("1202", ref DBUtility.ClsPub.sExeReturnInfo, true);
                ////获取组织代码
                //string OrganizationNUM = oCN.RunProcReturn("select HNumber from Xt_ORGANIZATIONS where HItemID=" + OrganizationID, "Xt_ORGANIZATIONS").Tables[0].Rows[0]["HNumber"].ToString();
                ////根据工序汇报单主ID获取工序汇报入库单的数据
                //DataSet ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList_LastProc where HInterID=" + InterID, "h_v_MES_StationOutBillList_LastProc");
                //DataRow dr = ds.Tables[0].Rows[0];
                //判断本次报废总数量是否为0
                var DTable = oCN.RunProcReturn("select isnull(sum(HWasterQty),0) HWasterQty from  Sc_StationOutBillMain where  HProcExchBillNo='" + BillNo + "' and HBFFlag=0 ", "Sc_StationOutBillMain").Tables[0];
                if (double.Parse(DTable.Rows[0]["HWasterQty"].ToString()) == 0)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 2;
                    objJsonResult.Message = "报废数量为0,不需要入库!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //保存
                oCN.BeginTran();
                //生产汇报单主表
                //oCN.RunProc("Insert Into Sc_ICMOReportBillMain   " +
                //"(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
                //",HYear,HPeriod,HRemark,HEmpID,HEmpNumber" +
                //",HGroupID,HDeptID,HDeptNumber" +
                //",HMainSourceBillNo,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillType" +
                //") " +
                //" values('3711','3711'," + HInterID.ToString() + ",'" + HBillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
                //",DATENAME(YEAR,GETDATE()),0,'','" + dr["HEmpID"].ToString() + "','" + dr["操作员代码"].ToString() +
                //"','" + dr["HGroupID"].ToString() + "',0,''" +
                //",'" + BillNo.ToString() + "'," + InterID.ToString() + ", 0,'3791'" +
                //") ");
                ////生产汇报单子表
                //oCN.RunProc("Insert into Sc_ICMOReportBillSub " +
                //      " (HInterID,HEntryID,HMaterID,HMaterNumber" +
                //      ",HQty,HUnitID,HUnitNumber,HTimes,HSourceID" +
                //      ",HQtyMust,HWorkerID,HWorkerNumber,HBadCount,HWasterQty," +
                //      "HCloseMan,HCloseType,HRemark," +
                //      "HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                //      ",HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo" +
                //      ",HICMOInterID,HICMOBillNo,HBarCode" +
                //      ") values("
                //      + HInterID.ToString() + ",1," + dr["HMaterID"].ToString() + ",'" + dr["产品代码"].ToString() + "'" +
                //      "," + dr["合格数量"].ToString() + ",0,'',0,0" +
                //      "," + dr["接收数量"].ToString() + "," + dr["HEmpID"].ToString() + ",'" + dr["操作员代码"].ToString() + "'," + dr["不良数量"].ToString() + "," + dr["报废数量"].ToString() +
                //      ",'',0,''" +
                //      "," + InterID.ToString() + ",0,'" + BillNo.ToString() + "','3791',0,0" +
                //      ",0,0,''" +
                //      "," + dr["HICMOInterID"].ToString() + ",'" + dr["任务单"].ToString() + "',''" +
                //      ") ");
                //同步金蝶
                //访问金蝶
                var loginRet = InvokeHelper.Login();
                var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                if (isSuccess == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "操作失败,金蝶账号登录异常。" + loginRet;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //
                //DataSet ds1 = oCN.RunProcReturn("select * from  h_v_TOERPProcduct_LastProc where HLastProc = '是' and  hbillno='" + BillNo.ToString() + "'", "h_v_TOERPProcduct_LastProc");
                DataSet ds1 = oCN.RunProcReturn("exec h_p_TOERPProcduct_LastProc_BF '" + BillNo + "'", "h_p_TOERPProcduct_LastProc_BF");
                DataRow dr1 = ds1.Tables[0].Rows[0];
                JObject model = new JObject();
                model.Add("FBillType", new JObject() { ["FNumber"] = "SCRKD01_SYS" }); //单据类型
                model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期
                model.Add("FStockOrgId", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //库存组织代码
                model.Add("FPrdOrgId", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //生产组织代码
                model.Add("FOwnerTypeId0", "BD_OwnerOrg");
                model.Add("FOwnerId0", new JObject() { ["FNumber"] = dr1["HPrdOrgNumber"].ToString() }); //
                model.Add("FIsEntrust", "false");//
                model.Add("FCurrId", new JObject() { ["FNumber"] = "PRE001" }); //
                model.Add("FBillNo", HBillNo);
                JArray Fentity = new JArray();
                foreach (DataRow item in ds1.Tables[0].Rows)
                {
                    JObject FentityModel = new JObject();
                    FentityModel.Add("FSrcEntryId", item["HSourceEntryID"].ToString());//  源单分录内码、
                    FentityModel.Add("FIsNew", "false");//  源单类型
                    FentityModel.Add("FMaterialId", new JObject() { ["FNumber"] = item["HMaterNumber"].ToString() }); // 物料编码
                    FentityModel.Add("FCheckProduct", "false");//
                    FentityModel.Add("FInStockType", "1");//
                    FentityModel.Add("FProductType", "1");//
                    FentityModel.Add("FUNITID", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//单位
                    FentityModel.Add("FMustQty", item["数量"].ToString());//
                    FentityModel.Add("FRealQty", item["数量"].ToString());//
                    FentityModel.Add("FCostRate", "100");//
                    FentityModel.Add("FBaseUnitId", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//单位
                    FentityModel.Add("FBaseMustQty", item["数量"].ToString());//
                    FentityModel.Add("FBaseRealQty", item["数量"].ToString());//
                    FentityModel.Add("FOwnerTypeId", "BD_OwnerOrg");//
                    FentityModel.Add("FOwnerId", new JObject() { ["FNumber"] = item["HPrdOrgNumber"].ToString() });//
                    string sErr = "";
                    if (oSystemParameter.ShowBill(ref sErr))
                    {
                        if (oSystemParameter.omodel.WMS_CampanyName == "瑞与祺")
                        {
                            if (oSystemParameter.omodel.MES_StationOutBill_InStockType == "工艺路线")
                            {
                                FentityModel.Add("FStockId", new JObject() { ["FNumber"] = item["HStockNumbers"].ToString() }); // 仓库
                            }
                            else if (oSystemParameter.omodel.MES_StationOutBill_InStockType == "工序")
                            {
                                FentityModel.Add("FStockId", new JObject() { ["FNumber"] = item["HStockNumber"].ToString() }); // 仓库
                            }
                        }
                    }
                    FentityModel.Add("FLot", new JObject() { ["FNumber"] = item["HBatchNo"].ToString() }); //批号
                    FentityModel.Add("FISBACKFLUSH", "true");//
                    FentityModel.Add("FWorkShopId1", new JObject() { ["FNumber"] = item["HWorkShopNumber"].ToString() }); //  生产车间
                    FentityModel.Add("FMOBILLNO", item["HMOBillNo"].ToString());//
                    FentityModel.Add("FMoId", item["HICMOInterID"].ToString());//生产订单内码
                    FentityModel.Add("FMoEntryId", item["HMOEntryID"].ToString());//
                    FentityModel.Add("FMoEntrySeq", item["HMOEntrySEQ"].ToString());//生产订单行号
                    FentityModel.Add("FStockUnitId", new JObject() { ["FNumber"] = item["HUnitNumber"].ToString() });//库存单位
                    FentityModel.Add("FStockRealQty", item["数量"].ToString());//
                    FentityModel.Add("FSrcBillType", "PRD_MORPT");//
                    FentityModel.Add("FSrcBillNo", item["HSourceBillNo"].ToString());//
                    FentityModel.Add("FSrcInterId", item["HSourceInterID"].ToString());//
                    FentityModel.Add("FBasePrdRealQty", item["数量"].ToString());//
                    FentityModel.Add("FIsFinished", "false");//
                    FentityModel.Add("FStockStatusId", new JObject() { ["FNumber"] = "KCZT001" }); //
                    FentityModel.Add("FSrcEntrySeq", item["HSourceSeQ"].ToString());//  源单分录行号
                    FentityModel.Add("FMOMAINENTRYID", item["HMOEntryID"].ToString());//
                    FentityModel.Add("FKeeperTypeId", "BD_KeeperOrg");
                    FentityModel.Add("FKeeperId", new JObject() { ["FNumber"] = item["HPrdOrgNumber"].ToString() });//
                    FentityModel.Add("FIsOverLegalOrg", "false");//
                    FentityModel.Add("F_bsv_Base1", new JObject() { ["FNumber"] = item["HBZBS"].ToString() });//
                    FentityModel.Add("F_BSV_TEXT", item["HLZKH"].ToString());//
                    FentityModel.Add("F_BSV_TEXT1", item["HWYID"].ToString());//
                                                                              //批号
                                                                              //FFLOWID FFLOWLINEID FRULEID FSTABLENAME
                                                                              //f6e6eec3 - 5267 - 4f02 - 8593 - b633da508a72    3   PRD_MO2MORPT T_PRD_MOENTRY
                                                                              //业务流程图:FEntity_Link_FFlowId
                                                                              //推进路线:FEntity_Link_FFlowLineId
                                                                              //转换规则:FEntity_Link_FRuleId
                                                                              //源单表内码:FEntity_Link_FSTableId
                                                                              //源单表:FEntity_Link_FSTableName
                                                                              //源单内码:FEntity_Link_FSBillId
                                                                              //源单分录内码:FEntity_Link_FSId
                                                                              //原始携带量:FEntity_Link_FBaseQuaQtyOld
                                                                              //修改携带量:FEntity_Link_FBaseQuaQty
                    JArray Fentity2 = new JArray();
                    JObject FentityModel2 = new JObject();
                    FentityModel2.Add("FEntity_Link_FFlowId", "f6e6eec3-5267-4f02-8593-b633da508a72");
                    FentityModel2.Add("FEntity_Link_FFlowLineId", "5");
                    FentityModel2.Add("FEntity_Link_FRuleId", "PRD_MORPT2INSTOCK");
                    FentityModel2.Add("FEntity_Link_FSTableName", "T_PRD_MORPTENTRY");
                    FentityModel2.Add("FEntity_Link_FSTableId", "0");
                    FentityModel2.Add("FEntity_Link_FSBillId", item["HSourceInterID"].ToString());
                    FentityModel2.Add("FEntity_Link_FSId", item["HSourceEntryID"].ToString());
                    FentityModel2.Add("FEntity_Link_FBasePrdRealQtyOld", item["关联数量"].ToString());
                    FentityModel2.Add("FEntity_Link_FBasePrdRealQty", item["数量"].ToString());
                    Fentity2.Add(FentityModel2);
                    FentityModel.Add("FEntity_Link", Fentity2);
                    FentityModel.Add("FBFLowId", new JObject() { ["FID"] = "f6e6eec3-5267-4f02-8593-b633da508a72" }); //
                    Fentity.Add(FentityModel);
                    //Fentity.Add(FentityModel);
                }
                model.Add("FEntity", Fentity); //明细信息
                JObject jsonRoot = new JObject()
                {
                    ["Creator"] = "",
                    ["NeedUpDateFields"] = new JArray(),
                    ["NeedReturnFields"] = new JArray(),
                    //["IsDeleteEntry"] = "true",
                    //["SubSystemId"] = "",
                    //["IsVerifyBaseDataField"] = "false",
                    ["IsDeleteEntry"] = "true",
                    ["SubSystemId"] = "",
                    ["IsVerifyBaseDataField"] = "true",
                    ["IsEntryBatchFill"] = "false",
                    ["ValidateFlag"] = "true",
                    ["NumberSearch"] = "true",
                    ["IsAutoAdjustField"] = "false",
                    ["InterationFlags"] = "",
                    ["IgnoreInterationFlag"] = "",
                    //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
                    ["Model"] = model
                };
                string result = InvokeHelper.Save("PRD_INSTOCK", JsonConvert.SerializeObject(jsonRoot));//保存
                //判断保存是否成功
                if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    LogService.Write("生产入库错误jsonRoot:" + jsonRoot);
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"生产入库单同步金蝶云失败!单号:{HBillNo.ToString()}" + result + jsonRoot;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //提交审核
                string result1 = string.Empty;
                string result2 = string.Empty;
                var fID = JObject.Parse(result)["Result"]["Id"].ToString();
                var fBillNo = JObject.Parse(result)["Result"]["Number"].ToString();
                var json = new
                {
                    Ids = fID,
                };
                result1 = InvokeHelper.Submit("PRD_INSTOCK", JsonConvert.SerializeObject(json));//提交
                //result2 = InvokeHelper.Audit("PRD_INSTOCK", JsonConvert.SerializeObject(json));//提交
                if (JObject.Parse(result1)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"生产入库单单号:{fBillNo},提交失败" + result;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //oCN.RunProc("update Sc_StationOutBillMain set HRelationQty=1 where  HBillNo='" + BillNo + "'");
                oCN.Commit();
                oCN.RunProc("update  sc_stationoutbillmain set HBFFlag =1 where HProcExchBillNo = '" + BillNo + "'");
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// <summary>
        /// 生成金蝶云来料检验单
        /// </summary>
        /// <param name="InterID">工序汇报单主ID</param>
        /// <returns></returns>
        [Route("QCStockInCheckBill/set_SaveQCStockInCheckBill_Json")]
        [HttpGet]
        public object set_SaveQCStockInCheckBill_Json(string HZJOrgNumber, string HMaterNumber, string HUnitNumber,
            double HCheckQty, double HRightQty, double HBadQty,
            string HCheckResult, string HSupNumber,
            string HUseResult, Int64 HSeQ, Int64 HSourceInterID,
            Int64 HSourceEntryID, string HSourceBillNo, string user,
            Int64 HWHID, Int64 HSPID, Int64 HSupID, Int64 HKeeperID,
            Int64 HMaterID, string HSourceBillType, Int64 HSLInterID,
            Int64 HSLEntryID, string HSLBillNo, Int64 HSLSeQ, string HBillNo, Int64 HInterID, string HBatchNo)
        {
            try
            {
                //获取生产汇报单最大InterID和单据号
                //Int64 HInterID = DBUtility.ClsPub.CreateBillID("7503", ref DBUtility.ClsPub.sExeReturnInfo);
                //string HBillNo = DBUtility.ClsPub.CreateBillCode("7503", ref DBUtility.ClsPub.sExeReturnInfo, true);
                DataSet ds1 = oCN.RunProcReturn("select   * from MES_AccessoriesList  where  HSourceBillNo = '" + HBillNo + "'", "MES_AccessoriesList");
                string HFileName = DBUtility.ClsPub.isStrNull(ds1.Tables[0].Rows[0]["HFileName"]);
                string HFilePath = DBUtility.ClsPub.isStrNull(ds1.Tables[0].Rows[0]["HFilePath"]);
                string path = HFilePath;
                FileInfo fi = new FileInfo(path);
                long len = fi.Length;
                byte[] buffer = new byte[len];
                FileStream fs = new FileStream(path, FileMode.Open);
                fs.Read(buffer, 0, (int)len);
                //文件IO流
                string a = Convert.ToBase64String(buffer);
                //保存
                oCN.BeginTran();
                //生产汇报单主表
                oCN.RunProc("Insert Into QC_POStockInCheckBillMain   " +
                "(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
                ",HYear,HPeriod,HRemark,HSupID,HMaterID" +
                ",HInstockQty,HCheckQty,HRightQty,HBadQty,HFirstCheckEmp" +
                ",HCheckerResult,HSteelStoveNo,HSteelCompReport,HAspect,HSize" +
                ") " +
                " values('7503','7503'," + HInterID.ToString() + ",'" + HBillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
                ",DATENAME(YEAR,GETDATE()),0,''," + HSupID.ToString() + "," + HMaterID.ToString() +
                ",'" + HCheckQty.ToString() + "','" + HCheckQty.ToString() + "','" + HRightQty.ToString() + "','" + HBadQty.ToString() + "','" + HKeeperID.ToString() + "'" +
                ",'" + HCheckResult.ToString() + "','', '','',''" +
                ") ");
                //生产汇报单子表
                oCN.RunProc("Insert into QC_POStockInCheckBillSub " +
                      " (HInterID,HEntryID,HCloseMan,HCloseType" +
                      ",HRemark,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType" +
                      ",HRelationQty,HRelationMoney,HQCCheckClassID,HQCCheckItemID,HQCStd," +
                      "HResult,HQCRelValue,HProcCheckEmp,HProcCheckTime" +
                      ") values("
                      + HInterID.ToString() + ",1,'',''" +
                      ",''," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo.ToString() + "','" + HSourceBillType.ToString() + "'" +
                      ",0,0,0,0,''" +
                      ",'" + HCheckResult.ToString() + "','" + HCheckResult.ToString() + "','" + HKeeperID.ToString() + "',getdate()" +
                      ") ");
                //同步金蝶
                //访问金蝶
                var loginRet = InvokeHelper.Login();
                var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                if (isSuccess == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "操作失败,金蝶账号登录异常。" + loginRet;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                JObject model = new JObject();
                model.Add("FBillTypeID", new JObject() { ["Fnumber"] = "JYD001_SYS" }); //单据类型 来料检验单JYD001_SYS
                model.Add("FBusinessType", "1"); //业务类型
                model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期
                model.Add("FInspectOrgId", new JObject() { ["Fnumber"] = HZJOrgNumber.ToString() }); //质检组织
                model.Add("FSourceOrgId", new JObject() { ["Fnumber"] = HZJOrgNumber.ToString() }); //来源组织
                model.Add("FISSYNCED", "false");//  是否已同步
                model.Add("F_PGKJ_Date", DateTime.Now.ToString("yyyy-MM-dd"));//  报检日期
                model.Add("FBillNo", HBillNo);
                JArray Fentity = new JArray();
                JObject FentityModel = new JObject();
                FentityModel.Add("FMaterialId", new JObject() { ["Fnumber"] = HMaterNumber.ToString() });//  物料内码
                FentityModel.Add("FUnitID", new JObject() { ["Fnumber"] = HUnitNumber.ToString() });//计量单位内码
                FentityModel.Add("FInspectQty", HCheckQty.ToString());//  检验数量
                FentityModel.Add("FQualifiedQty", HRightQty.ToString());//  合格数量
                FentityModel.Add("FUnqualifiedQty", HBadQty.ToString());//  不合格数量
                FentityModel.Add("FInspectResult", HCheckResult.ToString());//  检验结果
                FentityModel.Add("FQCStatus", "1");//  质检状态
                FentityModel.Add("FIsRelated", false);//  不良品关联标志
                FentityModel.Add("FSrcBillType0", "PUR_ReceiveBill");//  源单类型
                FentityModel.Add("FBaseUnitId", new JObject() { ["Fnumber"] = HUnitNumber.ToString() });//基本单位
                FentityModel.Add("FBaseInspectQty", HCheckQty.ToString());//基本单位检验数量
                FentityModel.Add("FSupplierId", new JObject() { ["Fnumber"] = HSupNumber.ToString() }); //  供应商
                                                                                                        //FentityModel.Add("FStockId", new JObject() { ["Fnumber"] = HWHNumber.ToString() }); // 仓库
                FentityModel.Add("FInspectTimes", "1"); // 检验次数
                FentityModel.Add("FTimeUnit", "24");//时间单位
                FentityModel.Add("FSAMPLEDAMAGEBEARER", "2");//样本破坏承担方
                FentityModel.Add("FISFIRSTINSPECT", false);//首检
                FentityModel.Add("FBaseQualifiedQty", HRightQty.ToString());//基本单位合格数
                FentityModel.Add("FBaseAcceptQty", HRightQty.ToString());//基本单位接收数
                FentityModel.Add("FCurrency", new JObject() { ["Fnumber"] = "PRE001" });//币别
                FentityModel.Add("FIsSplitRow ", false);// 是否拆分行
                FentityModel.Add("FLot", new JObject() { ["Fnumber"] = HBatchNo.ToString() });//批号
                JArray Fentity2 = new JArray();
                JObject FentityModel2 = new JObject();
                FentityModel2.Add("FPolicyMaterialId", new JObject() { ["Fnumber"] = HMaterNumber.ToString() });//  物料内码
                FentityModel2.Add("FPolicyStatus", "1");  //状态
                FentityModel2.Add("FPolicyQty", HRightQty.ToString());    //数量
                FentityModel2.Add("FBasePolicyQty", HRightQty.ToString()); //基本单位数量
                FentityModel2.Add("FUsePolicy", HUseResult.ToString());   //使用决策
                FentityModel2.Add("FIsCheck", false);   //是否抽检
                FentityModel2.Add("FIsDefectProcess", false);  //不良处理
                FentityModel2.Add("FCanSale", false);   //可销售
                FentityModel2.Add("FIsMRBReview", false);   //MRP评审
                FentityModel2.Add("FIsReturn", true);   //判退
                FentityModel2.Add("FIsRelatedDefect", false);   //不良品关联标志
                Fentity2.Add(FentityModel2);
                FentityModel.Add("FPolicyDetail", Fentity2);
                JArray Fentity3 = new JArray();
                JObject FentityModel3 = new JObject();
                FentityModel3.Add("FDetailID", "0");//
                FentityModel3.Add("FSrcBillType", "PUR_ReceiveBill");   //源单类型
                FentityModel3.Add("FSrcBillNo", HSLBillNo.ToString());    //收料通知单单号
                FentityModel3.Add("FSrcInterId", HSLInterID.ToString());      //收料通知单主ID
                FentityModel3.Add("FSrcEntryId", HSLEntryID.ToString());    //收料通知单子ID
                FentityModel3.Add("FSrcEntrySeq", HSLSeQ.ToString());    //源单行号
                FentityModel3.Add("FOrderType", new JObject() { ["FID"] = "PUR_PurchaseOrder" });  //  订单类型
                FentityModel3.Add("FOrderBillNo", HSourceBillNo.ToString());   //订单单号
                FentityModel3.Add("FOrderId", HSourceInterID.ToString());      //订单主ID
                FentityModel3.Add("FOrderEntryId", HSourceEntryID.ToString());  //订单子ID
                FentityModel3.Add("FOrderEntrySeq", HSeQ.ToString());  //订单行号
                Fentity3.Add(FentityModel3);
                FentityModel.Add("FReferDetail", Fentity3);
                JArray Fentity4 = new JArray();
                JObject FentityModel4 = new JObject();
                FentityModel4.Add("FEntity_Link_FRuleId", "QM_PURReceive2Inspect");   //单据转换规则
                FentityModel4.Add("FEntity_Link_FSTableName", "T_PUR_RECEIVEENTRY");    //收料通知单子表
                FentityModel4.Add("FEntity_Link_FSBillId", HSLInterID.ToString());      //收料通知单主内码
                FentityModel4.Add("FEntity_Link_FSId", HSLEntryID.ToString());    //收料通知单子内码
                FentityModel4.Add("FEntity_Link_FBaseAcceptQty", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FBaseAcceptQtyOld", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FBaseInspectQtyOld", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FInspectQtyOld", HRightQty.ToString());    //
                Fentity4.Add(FentityModel4);
                FentityModel.Add("FEntity_Link", Fentity4);
                Fentity.Add(FentityModel);
                model.Add("FEntity", Fentity); //明细信息
                JObject jsonRoot = new JObject()
                {
                    ["Creator"] = "",
                    ["NeedUpDateFields"] = new JArray(),
                    ["NeedReturnFields"] = new JArray(),
                    ["IsDeleteEntry"] = "false",
                    ["SubSystemId"] = "",
                    ["IsVerifyBaseDataField"] = "false",
                    //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
                    ["Model"] = model
                };
                string result = InvokeHelper.Save("QM_InspectBill", JsonConvert.SerializeObject(jsonRoot));//保存
                //判断保存是否成功
                if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    LogService.Write("来料检验单保存错误jsonRoot:" + jsonRoot);
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"来料检验单同步金蝶云失败!单号:{HBillNo.ToString()}" + jsonRoot;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //提交审核
                string result1 = string.Empty;
                string result2 = string.Empty;
                var fID = JObject.Parse(result)["Result"]["Id"].ToString();
                var fBillNo = JObject.Parse(result)["Result"]["Number"].ToString();
                var json = new
                {
                    Ids = fID,
                };
                K3CloudApiClient client = new K3CloudApiClient("http://47.96.97.237/k3cloud");
                //        K3CloudApiClient client = new K3CloudApiClient("http://192.168.80.90/k3cloud/");
                result1 = InvokeHelper.Submit("QM_InspectBill", JsonConvert.SerializeObject(json));//提交
                result2 = InvokeHelper.Audit("QM_InspectBill", JsonConvert.SerializeObject(json));//提交
                if (JObject.Parse(result1)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"来料检验单号:{fBillNo},提交失败" + result;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    string jsonStr = "{" +
                       " \"FileName\":\"" + HFileName + "\"," +
                           " \"FormId\":\"QM_InspectBill\"," +
                           " \"IsLast\":\"true\"," +
                           " \"InterId\":\"" + HInterID + "\"," +
                           " \"BillNO\":\"" + HBillNo + "\"," +
                           " \"AliasFileName\":\"test\"," +
                           " \"SendByte\":\"" + a + "\"," +
                       "}";
                    var ret = client.AttachmentUpload(jsonStr);
                }
                oCN.Commit();
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        ///// <summary>
        ///// 生成金蝶云来料检验单
        ///// </summary>
        ///// <param name="InterID">工序汇报单主ID</param>
        ///// <returns></returns>
        //[Route("QCStockInCheckBill/set_SaveQCStockInCheckBill_Json")]
        //[HttpGet]
        //public object set_SaveQCStockInCheckBill_Json(string HZJOrgNumber, string HMaterNumber, string HUnitNumber,
        //    double HCheckQty, double HRightQty, double HBadQty,
        //    string HCheckResult, string HSupNumber,
        //    string HUseResult, Int64 HSeQ, Int64 HSourceInterID,
        //    Int64 HSourceEntryID, string HSourceBillNo, string user,
        //    Int64 HWHID, Int64 HSPID, Int64 HSupID, Int64 HKeeperID,
        //    Int64 HMaterID, string HSourceBillType, Int64 HSLInterID,
        //    Int64 HSLEntryID, string HSLBillNo, Int64 HSLSeQ, string HBillNo, Int64 HInterID,ref string sErrMsg)
        //{
        //    WebS.WebService1 oWebs1 = new WebS.WebService1();
        //    //oWebs1.Url = "http://localhost:9099/WebService1.asmx";
        //    //K3CloudApiClient client = new K3CloudApiClient("http://192.168.80.90/k3cloud/");
        //    if (oWebs1.set_SaveQcStockInCheckBill_New(HZJOrgNumber, HMaterNumber, HUnitNumber, HCheckQty, HRightQty, HBadQty
        //        , HCheckResult, HSupNumber, HUseResult, HSeQ, HSourceInterID
        //        , HSourceEntryID, HSourceBillNo, user, HWHID, HSPID
        //        , HSupID, HKeeperID, HMaterID, HSourceBillType, HSLInterID
        //        , HSLEntryID, HSLBillNo, HSLSeQ, HBillNo, HInterID
        //        , ref sErrMsg))
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 1;
        //        objJsonResult.Message = "生成单据成功!";
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //    else
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "生成单据失败!";
        //        objJsonResult.data = sErrMsg;
        //        return objJsonResult;
        //    }
        //}
        /// <summary>
        /// 生成金蝶云产品检验单
        /// </summary>
        /// <param name="InterID">工序汇报单主ID</param>
        /// <returns></returns>
        [Route("SCStockInCheckBill/set_SaveSCStockInCheckBill_Json")]
        [HttpGet]
        public object set_SaveSCStockInCheckBill_Json(string HZJOrgNumber, string HMaterNumber, string HUnitNumber,
            double HCheckQty, double HRightQty, double HBadQty,
            string HCheckResult, string HSupNumber, string HWHNumber,
            string HUseResult, Int64 HSeQ, Int64 HSourceInterID,
            Int64 HSourceEntryID, string HSourceBillNo, string user,
            Int64 HWHID, Int64 HSPID, Int64 HSupID, Int64 HKeeperID,
            Int64 HMaterID, string HSourceBillType, Int64 HSLInterID,
            Int64 HSLEntryID, string HSLBillNo, Int64 HSLSeQ)
        {
            try
            {
                //获取生产汇报单最大InterID和单据号
                Int64 HInterID = DBUtility.ClsPub.CreateBillID("7501", ref DBUtility.ClsPub.sExeReturnInfo);
                string HBillNo = DBUtility.ClsPub.CreateBillCode("7501", ref DBUtility.ClsPub.sExeReturnInfo, true);
                //保存
                //oCN.BeginTran();
                //生成产品检验单
                //oCN.RunProc("Insert Into QC_POStockInCheckBillMain   " +
                //"(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMaker,HMakeDate,HBillStatus,HChecker,HCheckDate" +
                //",HYear,HPeriod,HRemark,HSupID,HMaterID" +
                //",HInstockQty,HCheckQty,HRightQty,HBadQty,HFirstCheckEmp" +
                //",HCheckerResult,HSteelStoveNo,HSteelCompReport,HAspect,HSize" +
                //") " +
                //" values('7503','7503'," + HInterID.ToString() + ",'" + HBillNo + "',getdate(),'" + user + "',getdate(),2,'" + user + "',getdate()" +
                //",DATENAME(YEAR,GETDATE()),0,''," + HSupID.ToString() + "," + HMaterID.ToString() +
                //",'" + HCheckQty.ToString() + "','" + HCheckQty.ToString() + "','" + HRightQty.ToString() + "','" + HBadQty.ToString() + "','" + HKeeperID.ToString() + "'" +
                //",'" + HCheckResult.ToString() + "','', '','',''" +
                //") ");
                ////生产汇报单子表
                //oCN.RunProc("Insert into QC_POStockInCheckBillSub " +
                //      " (HInterID,HEntryID,HCloseMan,HCloseType" +
                //      ",HRemark,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType" +
                //      ",HRelationQty,HRelationMoney,HQCCheckClassID,HQCCheckItemID,HQCStd," +
                //      "HResult,HQCRelValue,HProcCheckEmp,HProcCheckTime" +
                //      ") values("
                //      + HInterID.ToString() + ",1,'',''" +
                //      ",''," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo.ToString() + "','" + HSourceBillType.ToString() + "'" +
                //      ",0,0,0,0,''" +
                //      ",'" + HCheckResult.ToString() + "','" + HCheckResult.ToString() + "','" + HKeeperID.ToString() + "',getdate()" +
                //      ") ");
                //同步金蝶
                //访问金蝶
                var loginRet = InvokeHelper.Login();
                var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                if (isSuccess == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "操作失败,金蝶账号登录异常。" + loginRet;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                JObject model = new JObject();
                model.Add("FBillTypeID", new JObject() { ["Fnumber"] = "JYD002_SYS" }); //单据类型 产品检验单JYD002_SYS
                model.Add("FBusinessType", "3"); //业务类型
                model.Add("FDate", DateTime.Now.ToString("yyyy-MM-dd")); //单据日期
                model.Add("FSourceOrgId", new JObject() { ["Fnumber"] = HZJOrgNumber.ToString() }); //来源组织
                model.Add("FInspectOrgId", new JObject() { ["Fnumber"] = HZJOrgNumber.ToString() }); //质检组织
                model.Add("FISSYNCED", "false");//  是否已同步
                model.Add("FBillNo", HBillNo);
                JArray Fentity = new JArray();
                JObject FentityModel = new JObject();
                FentityModel.Add("FMaterialId", new JObject() { ["Fnumber"] = HMaterNumber.ToString() });//  物料内码
                FentityModel.Add("FUnitID", new JObject() { ["Fnumber"] = HUnitNumber.ToString() });//计量单位内码
                FentityModel.Add("FInspectQty", HCheckQty.ToString());//  检验数量
                FentityModel.Add("FQualifiedQty", HRightQty.ToString());//  合格数量
                FentityModel.Add("FUnqualifiedQty", HBadQty.ToString());//  不合格数量
                FentityModel.Add("FInspectResult", HCheckResult.ToString());//  检验结果
                FentityModel.Add("FQCStatus", "1");//  质检状态
                FentityModel.Add("FIsRelated", false);//  不良品关联标志
                FentityModel.Add("FSrcBillType0", "SFC_OperationReport");//  源单类型
                FentityModel.Add("FBaseUnitId", new JObject() { ["Fnumber"] = HUnitNumber.ToString() });//基本单位
                FentityModel.Add("FBaseInspectQty", HCheckQty.ToString());//基本单位检验数量
                //FentityModel.Add("FSupplierId", new JObject() { ["Fnumber"] = HSupNumber.ToString() }); //  供应商
                //FentityModel.Add("FStockId", new JObject() { ["Fnumber"] = HWHNumber.ToString() }); // 仓库
                FentityModel.Add("FInspectTimes", "1"); // 检验次数
                FentityModel.Add("FTimeUnit", "24");//时间单位
                FentityModel.Add("FSAMPLEDAMAGEBEARER", "2");//样本破坏承担方
                FentityModel.Add("FISFIRSTINSPECT", false);//首检
                FentityModel.Add("FBaseQualifiedQty", HRightQty.ToString());//基本单位合格数
                FentityModel.Add("FCurrency", new JObject() { ["Fnumber"] = "PRE001" });//币别
                FentityModel.Add("FIsSplitRow ", false);// 是否拆分行
                JArray Fentity2 = new JArray();
                JObject FentityModel2 = new JObject();
                FentityModel2.Add("FPolicyMaterialId", new JObject() { ["Fnumber"] = HMaterNumber.ToString() });//  物料内码
                FentityModel2.Add("FPolicyStatus", "1");  //状态
                FentityModel2.Add("FPolicyQty", HRightQty.ToString());    //数量
                FentityModel2.Add("FBasePolicyQty", HRightQty.ToString()); //基本单位数量
                FentityModel2.Add("FUsePolicy", HUseResult.ToString());   //使用决策
                FentityModel2.Add("FIsCheck", false);   //是否抽检
                FentityModel2.Add("FIsDefectProcess", false);  //不良处理
                FentityModel2.Add("FCanSale", false);   //可销售
                FentityModel2.Add("FIsMRBReview", false);   //MRP评审
                FentityModel2.Add("FIsReturn", true);   //判退
                FentityModel2.Add("FIsRelatedDefect", false);   //不良品关联标志
                Fentity2.Add(FentityModel2);
                FentityModel.Add("FPolicyDetail", Fentity2);
                JArray Fentity3 = new JArray();
                JObject FentityModel3 = new JObject();
                FentityModel3.Add("FDetailID", "0");//
                FentityModel3.Add("FSrcBillType", "SFC_OperationReport");   //源单类型
                FentityModel3.Add("FSrcBillNo", HSLBillNo.ToString());    //工序汇报单单号
                FentityModel3.Add("FSrcInterId", HSLInterID.ToString());      //工序汇报单主ID
                FentityModel3.Add("FSrcEntryId", HSLEntryID.ToString());    //工序汇报单子ID
                FentityModel3.Add("FSrcEntrySeq", HSLSeQ.ToString());    //工序汇报单行号
                FentityModel3.Add("FOrderType", new JObject() { ["FID"] = "PRD_MO" });  //  生产订单类型
                FentityModel3.Add("FOrderBillNo", HSourceBillNo.ToString());   //生产订单单号
                FentityModel3.Add("FOrderId", HSourceInterID.ToString());      //生产订单主ID
                FentityModel3.Add("FOrderEntryId", HSourceEntryID.ToString());  //生产订单子ID
                FentityModel3.Add("FOrderEntrySeq", HSeQ.ToString());  //生产订单行号
                Fentity3.Add(FentityModel3);
                FentityModel.Add("FReferDetail", Fentity3);
                JArray Fentity4 = new JArray();
                JObject FentityModel4 = new JObject();
                FentityModel4.Add("FEntity_Link_FRuleId", "QM_OperRpt2Inspect");   //单据转换规则
                FentityModel4.Add("FEntity_Link_FSTableName", "T_SFC_OPTRPTENTRY");    //工序汇报单子表
                FentityModel4.Add("FEntity_Link_FSBillId", HSLInterID.ToString());      //工序汇报单主ID
                FentityModel4.Add("FEntity_Link_FSId", HSLEntryID.ToString());    //工序汇报单子ID
                FentityModel4.Add("FEntity_Link_FBaseAcceptQty", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FBaseAcceptQtyOld", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FBaseInspectQtyOld", HRightQty.ToString());    //
                FentityModel4.Add("FEntity_Link_FInspectQtyOld", HRightQty.ToString());    //
                Fentity4.Add(FentityModel4);
                FentityModel.Add("FEntity_Link", Fentity4);
                //JArray Fentity3 = new JArray();
                //JObject FentityModel3 = new JObject();
                //FentityModel3.Add("FSrcBillType", "SFC_OperationReport");   //源单类型
                //FentityModel3.Add("FSrcBillNo", HSLBillNo.ToString());    //工序汇报单单号
                //FentityModel3.Add("FSrcInterId", HSLInterID.ToString());      //工序汇报单主ID
                //FentityModel3.Add("FSrcEntryId", HSLEntryID.ToString());    //工序汇报单子ID
                //FentityModel3.Add("FSrcEntrySeq", HSLSeQ.ToString());    //工序汇报单行号
                //FentityModel3.Add("FOrderType", new JObject() { ["FID"] = "PUR_PurchaseOrder" });  //  工序计划类型
                //FentityModel3.Add("FOrderBillNo", HSourceBillNo.ToString());   //工序计划单号
                //FentityModel3.Add("FOrderId", HSourceInterID.ToString());      //工序计划主ID
                //FentityModel3.Add("FOrderEntryId", HSourceEntryID.ToString());  //工序计划子ID
                //FentityModel3.Add("FOrderEntrySeq", HSeQ.ToString());  //工序计划行号
                //Fentity3.Add(FentityModel3);
                //FentityModel.Add("FReferDetail", Fentity3);
                Fentity.Add(FentityModel);
                model.Add("FEntity", Fentity); //明细信息
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                DataSet ds = oCN.RunProcReturn("select HCheckdate from Sc_ProcessReportMain where hbillno = '" + HSourceBillNo + "'", "Sc_ProcessReportMain");
                DateTime HCheckDate = DBUtility.ClsPub.isDate(ds.Tables[0].Rows[0]["HCheckdate"]);
                JObject jsonRoot = new JObject()
                {
                    ["Creator"] = "",
                    ["NeedUpDateFields"] = new JArray(),
                    ["NeedReturnFields"] = new JArray(),
                    ["IsDeleteEntry"] = "false",
                    ["SubSystemId"] = "",
                    ["IsVerifyBaseDataField"] = "false",
                    ["F_PGKJ_Date"] = HCheckDate.ToString(),
                    //["IsAutoSubmitAndAudit"] = true,//自动调用提交和审核功能
                    ["Model"] = model
                };
                string result = InvokeHelper.Save("QM_InspectBill", JsonConvert.SerializeObject(jsonRoot));//保存
                //判断保存是否成功
                if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    LogService.Write("来料检验单保存错误jsonRoot:" + jsonRoot);
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"来料检验单同步金蝶云失败!单号:{HBillNo.ToString()}" + result;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //提交审核
                string result1 = string.Empty;
                string result2 = string.Empty;
                var fID = JObject.Parse(result)["Result"]["Id"].ToString();
                var fBillNo = JObject.Parse(result)["Result"]["Number"].ToString();
                var json = new
                {
                    Ids = fID,
                };
                result1 = InvokeHelper.Submit("QM_InspectBill", JsonConvert.SerializeObject(json));//提交
                result2 = InvokeHelper.Audit("QM_InspectBill", JsonConvert.SerializeObject(json));//提交
                if (JObject.Parse(result1)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"来料检验单号:{fBillNo},提交失败" + result;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.Commit();
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// <summary>
        /// 返回生产汇报单列表
@@ -1624,6 +3008,115 @@
        }
        #endregion
        #region 不良品处理单关闭/反关闭功能
        [Route("Sc_ProcessMangement/CloseSc_ProcessMangement")]
        [HttpGet]
        public object CloseSc_ProcessMangement(string HInterID, int Type, string user)
        {
            try
            {
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("QC_NoPassProdCheckBill_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 = "QC_NoPassProdCheckBillMain";
                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
        /// <summary>
        /// PDA工序汇报单保存
        /// </summary>