llj
7 天以前 2bf6de5d4748d28f7f3a098af25503d6a0c619d1
WebAPI/Controllers/²Ö´æ¹ÜÀí/ÁìÁÏ·¢»õ/Kf_MateOutBillController.cs
@@ -1,4 +1,5 @@
using DBUtility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@@ -17,17 +18,19 @@
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        public DLL.ClsKf_MateOutBill BillNew0 = new DLL.ClsKf_MateOutBill();   //模具报废入库对应单据类
        public DLL.ClsKf_MateOutBill BillOld0 = new DLL.ClsKf_MateOutBill();   //模具报废入库对应单据类
        #region ç”Ÿäº§é¢†æ–™å•列表
        [Route("Kf_MateOutBill/GetMateOutBillList")]
        [HttpGet]
        public object GetMateOutBillList(string sWhere,string user)
        {
            try
            {
                List<object> columnNameList = new List<object>();  //定义声明变量 ï¼ŒæŠŠé€šè¿‡ new List<object>()创建的 å®žä¾‹ï¼Œèµ‹å€¼ç»™å˜é‡
                //判断是否有查询权限
                if (!DBUtility.ClsPub.Security_Log("Kf_MateOutBillQuery", 1, false, user))
                {
@@ -39,13 +42,19 @@
                }
                ds = oCn.RunProcReturn("select * from h_v_IF_MateOutBillList_Search " + sWhere + " order by æ—¥æœŸ desc, hmainid desc", "h_v_IF_MateOutBillList_Search");
                foreach (DataColumn col in ds.Tables[0].Columns)//遍历ds中第一个表(Tables[0])的所有列(Columns)每次循环中,col变量会持有当前列的引用
                {
                    Type dataType = col.DataType; //获取当前数据类型传入 è‡ªå®šä¹‰å˜é‡datadataType
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}"; //字符串拼接         // å°†åˆ—名和数据类型信息拼接成一个JSON格式的字符串
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                //if (ds.Tables[0].Rows.Count != 0 || ds != null)
                //{
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                objJsonResult.list = columnNameList;
                return objJsonResult;
                //}
                //else
@@ -126,6 +135,17 @@
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //进行 ä¼šè®¡æœŸé—´ ç»“è´¦ çš„判断和控制
                int sYear = 0;
                int sPeriod = 0;
                DateTime HDate = DateTime.Now;
                if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false)
                {
                    objJsonResult.Message = s;
                    return objJsonResult;
                }
                bool IsDete = oBill.DeleteBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo);
                if (IsDete)
                {
@@ -403,7 +423,7 @@
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (sqlWhere == null || sqlWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn(string.Format(@"select HMaterID ,物料代码 HMaterCode ,物料名称 HMaterName,物料公式 HMaterRuleType,规格型号 HMaterSpec,用量 HRate,HUnitID,计量单位代码 HUnitCode,计量单位 HUnitName,应收数量 HQtyMust,实收数量 HQty,单价 HPrice,金额 HMoney,HWHID,领料仓库代码 HWHCode,领料仓库 HWHName,表体备注 HRemark
                    ds = oCN.RunProcReturn(string.Format(@"select HMaterID ,物料代码 HMaterCode ,物料名称 HMaterName,物料公式 HMaterRuleType,规格型号 HMaterSpec,用量 HRate,HUnitID,计量单位代码 HUnitCode,计量单位 HUnitName,应收数量 HQtyMust,实收数量 HQty,单价 HPrice,金额 HMoney,HWHID,领料仓库代码 HWHCode,领料仓库 HWHName,表体备注 HRemark,实际称重 HWeight, ç¾Šæ¯›ç‰©æ–™å…¬å¼ HMaterRuleExternType, æ¯›å°–物料公式 HMaterRuleExternType1
from h_v_IF_MateOutBillList"), "H_V_IF_MATEOUTBILLLIST");
                }
@@ -411,7 +431,7 @@
                {
                    //string sql1 = "select HRepairCheckID,HRepairCheckContent,HManagerID,HRemark  from Sc_MouldRepairCheckBillSub where 1 = 1 ";
                    string sql1 = string.Format(@"select HMaterID ,物料代码 HMaterCode ,物料名称 HMaterName,物料公式 HMaterRuleType,规格型号 HMaterSpec,用量 HRate, HUnitID,计量单位代码 HUnitCode,计量单位 HUnitName,应收数量 HQtyMust,实收数量 HQty,单价 HPrice,金额 HMoney,HWHID,领料仓库代码 HWHCode,领料仓库 HWHName,表体备注 HRemark
                    string sql1 = string.Format(@"select HMaterID ,物料代码 HMaterCode ,物料名称 HMaterName,物料公式 HMaterRuleType,规格型号 HMaterSpec,用量 HRate, HUnitID,计量单位代码 HUnitCode,计量单位 HUnitName,应收数量 HQtyMust,实收数量 HQty,单价 HPrice,金额 HMoney,HWHID,领料仓库代码 HWHCode,领料仓库 HWHName,表体备注 HRemark, å®žé™…称重 HWeight, ç¾Šæ¯›ç‰©æ–™å…¬å¼ HMaterRuleExternType, æ¯›å°–物料公式 HMaterRuleExternType1
from h_v_IF_MateOutBillList WHERE 1=1");
                    string sql = sql1 + sqlWhere + " order by HSubID asc";
                    ds = oCN.RunProcReturn(sql, "H_V_IF_MATEOUTBILLLIST");
@@ -533,7 +553,7 @@
                    //oItem.HMaker = "";
                    UserName = oItem.HMaker;  //制单人
                    oItem.HBillType = "1204";
                    oItem.HBillSubType = "1204";
                    //oItem.HBillSubType = "1204";
                    //oItem.HBillNo = "";    //单据号
                    //oItem.HDate = DBUtility.ClsPub.isDate(DateTime.Now.ToString("yyyy-MM-dd"));//  --日期
                    //oItem.HInnerBillNo = "";  //  --内部单据号
@@ -597,6 +617,16 @@
                    oBill.DetailColl.Add(oItemSub);
                }
                //进行 ä¼šè®¡æœŸé—´ ç»“è´¦ çš„判断和控制
                int sYear = 0;
                int sPeriod = 0;
                DateTime HDate = DateTime.Now;
                if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false)
                {
                    objJsonResult.Message = s;
                    return objJsonResult;
                }
                //保存
                //保存完毕后处理
                bool bResult;
@@ -652,7 +682,7 @@
            SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
            string sql = "select top(1) " +
                "a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo,a.HInterID HProcExchInterID,a.HMaterID,m1.HName HMaterName,a.HMaterModel HModel,a.HMaterModel2 HModel2,a.HCusID,c1.HName HCusName,a.HWeight" +
                "a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo,a.HInterID HProcExchInterID,a.HMaterID,m1.HName HMaterName,a.HMaterModel HModel,a.HMaterModel2 HModel2,a.HCusID,c1.HName HCusName,a.HWeight,a.HQty" +
                " from Sc_ProcessExchangeBillMain AS a " +
                " LEFT OUTER JOIN Gy_Material AS m1 on a.HMaterID = m1.HItemID " +
                " LEFT OUTER JOIN Gy_Customer AS c1 on a.HCusID = c1.HItemID " +
@@ -675,7 +705,7 @@
        {
            SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
            string sql = "select a.HItemID HMaterID,a.HNumber HMaterNumber,a.HName HMaterName,a.HMaterRuleType,a.HModel HMaterModel,a.HUnitID, b.HNumber HUnitNumber, b.HName HUnitName" +
            string sql = "select a.HItemID HMaterID,a.HNumber HMaterNumber,a.HName HMaterName,a.HMaterRuleType,a.HModel HMaterModel,a.HUnitID, b.HNumber HUnitNumber, b.HName HUnitName, a.HMaterRuleExternType,  a.HMaterRuleExternType1" +
                " from Gy_Material AS a " +
                " LEFT OUTER JOIN Gy_Unit AS b on a.HUnitID = b.HItemID " +
                " where a.HItemID =" + HMaterID;
@@ -864,10 +894,20 @@
                    return objJsonResult;
                }
                //进行 ä¼šè®¡æœŸé—´ ç»“è´¦ çš„判断和控制
                string s = "";
                int sYear = 0;
                int sPeriod = 0;
                DateTime HDate = DateTime.Now;
                if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false)
                {
                    objJsonResult.Message = s;
                    return objJsonResult;
                }
                //进行需要进行的审核/反审核操作
                if (IsAudit == 0) //审核提交
                {
                {
                    //审核提交
                    if (oBill.CheckBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true)
                    {
@@ -887,7 +927,7 @@
                    }
                }
                if (IsAudit == 1) //反审核提交
                {
                {
                    //反审核提交AbandonCheck
                    if (oBill.AbandonCheck(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true)
                    {
@@ -1200,5 +1240,518 @@
            }
        }
        #endregion
        #region æ ¹æ®å•据号,同步生产领料单
        [Route("Kf_MateOutBillController/GetKf_MateOutBill_Sync_Json")]
        [HttpGet]
        public json GetKf_MateOutBill_Sync_Json(string HBillNo, string HBillType)
        {
            try
            {
                //获取系统参数
                string sql = string.Empty;
                string sErrMsg = "";
                if (oSystemParameter.ShowBill(ref sErrMsg) == true)
                {
                    //系统参数是否为私有云模式,N为公有云模式,Y为私有云模式
                    //私有云模式,通过调用存储过程进行同步
                    if (oSystemParameter.omodel.WMS_CloudMode.ToUpper() == "Y")
                    {
                        ds = oCn.RunProcReturn("exec h_p_IFCLD_ERPSourceBillToLocal_MateOut '" + HBillNo + "'", "h_p_IFCLD_ERPSourceBillToLocal_MateOut");
                        if (ds == null || ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "单据同步发生错误!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            if (DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HBack"]) == 2)
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 0;
                                objJsonResult.Message = DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HBackRemark"]);
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                            else
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 1;
                                objJsonResult.Message = "单据同步成功!";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
                    }
                    //公有云模式,通过调用WEBAPI方式进行同步
                    else if (oSystemParameter.omodel.WMS_CloudMode.ToUpper() == "N")
                    {
                        //判断金蝶云是否登录成功
                        var loginRet = InvokeHelper.Login();
                        var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                        //判断是否登录成功
                        if (isSuccess <= 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "同步失败,金蝶云登入不成功,请确认C盘配置文件所设置金蝶云对应网址、账套、登录用户、登录密码是否正确!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        var json = new
                        {
                            CreateOrgId = 0,
                            Number = HBillNo,
                            Id = ""
                        };
                        //查看 èŽ·å–æ•°æ®
                        var _result = InvokeHelper.View("PRD_PickMtrl", JsonConvert.SerializeObject(json));
                        var _saveObj = JObject.Parse(_result);
                        //判断金蝶云数据是否获取成功
                        if (_saveObj["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "金蝶云生产领料单获取失败:" + _result;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        oCn.BeginTran();
                        //查找本地生产领料单表中是否存在该单据,存在获取本单单据ID,不存在则获取最大单据ID
                        Int64 HNewInterID = 0;
                        ds = oCn.RunProcReturn("select HInterID from Kf_ICStockBillMain with(nolock) where HBillNo = '" + HBillNo + "' and HBillType='1204'", "Kf_ICStockBillMain");
                        if (ds == null || ds.Tables[0].Rows.Count == 0)
                        {
                            HNewInterID = DBUtility.ClsPub.CreateBillID_Prod(HBillType, ref sErrMsg);
                        }
                        else
                        {
                            HNewInterID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HInterID"]);
                        }
                        //根据单据号删除本地生产领料单中对应单据
                        oCn.RunProc("Delete b from Kf_ICStockBillMain a inner join Kf_ICStockBillSub b on a.HInterID=b.HInterID where a.HBillNo = '" + HBillNo + "' and a.HBillType='1204'", ref DBUtility.ClsPub.sExeReturnInfo);
                        oCn.RunProc("Delete from Kf_ICStockBillMain where HBillNo = '" + HBillNo + "' and HBillType='1204'", ref DBUtility.ClsPub.sExeReturnInfo);
                        //主表数据赋值
                        var MainData = new
                        {
                            HInterID = HNewInterID,
                            HBillNo = DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["BillNo"]),
                            HBillType = "1204",
                            HBillSubType = "1204",
                            HMainSourceBillType = "",
                            HDate = DBUtility.ClsPub.isDate(_saveObj["Result"]["Result"]["Date"]),
                            HSupID = 0,
                            HWHID = 0,
                            HSCWHID = 0,
                            HEmpID = 0,
                            HManagerID = 0,
                            HSecManagerID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["PickerId_Id"]),
                            HKeeperID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["FSTOCKERID_Id"]),
                            HDeptID = 0,
                            HCurID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["CurrId_Id"]),
                            HSeOrderBillNo = "",
                            HExplanation = "CLD导入",
                            HRemark = "",
                            HInnerBillNo = "",
                            HRedBlueFlag = 0,
                            HBillStatus = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["ApproverId_Id"]) == 0 ? 1 : 2,
                            HMaker = DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["CreatorId"]["Name"]),
                            HMakeDate = DBUtility.ClsPub.isDate(_saveObj["Result"]["Result"]["CreateDate"]),
                            HChecker = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["ApproverId_Id"]) == 0 ? "" : DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["ApproverId"]["Name"]),
                            HCheckDate = DBUtility.ClsPub.isDate(_saveObj["Result"]["Result"]["ApproveDate"]),
                            HUpDater = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["ModifierId_Id"]) == 0 ? "" : DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["ModifierId"]["Name"]),
                            HUpDateDate = DBUtility.ClsPub.isDate(_saveObj["Result"]["Result"]["ModifyDate"]),
                            HDeleteMan = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["CANCELER_Id"]) == 0 ? "" : DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["CANCELER"]["Name"]),
                            HDeleteDate = DBUtility.ClsPub.isDate(_saveObj["Result"]["Result"]["CancelDate"]),
                            HOWNERID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["OwnerId_Id"]),
                            HOWNERTYPEID = DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["OwnerTypeId"]),
                            HERPInterID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["Id"]),
                            HERPBillType = DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["BillType_Id"]),
                            HERPBillNo = DBUtility.ClsPub.isStrNull(_saveObj["Result"]["Result"]["BillNo"]),
                            HPRDORGID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["PrdOrgId_Id"]),
                            HSTOCKORGID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["StockOrgId_Id"]),
                        };
                        //写入主表
                        sql = $@"Insert into Kf_ICStockBillMain
                                    (HInterID,HYear,HPeriod,HBillNo,HBillType,HBillSubType,HMainSourceBillType,HDate
                                    ,HSupID,HWHID,HSCWHID,HEmpID,HManagerID,HSecManagerID,HKeeperID,HDeptID,HCurID
                                    ,HSeOrderBillNo,HExplanation,HRemark,HInnerBillNo,HRedBlueFlag,HBillStatus
                                    ,HMaker,HMakeDate
                                    ,HChecker,HCheckDate
                                    ,HUpDater,HUpDateDate
                                    ,HDeleteMan,HDeleteDate
                                    ,HOWNERID,HOWNERTYPEID,HERPInterID,HERPBillType,HERPBillNo
                                    ,HPRDORGID,HSTOCKORGID)
                                    Values({MainData.HInterID},year('{MainData.HDate}'),month('{MainData.HDate}'),'{MainData.HBillNo}','{MainData.HBillType}','{MainData.HBillSubType}','{MainData.HMainSourceBillType}','{MainData.HDate}'
                                    , {MainData.HSupID},{MainData.HWHID},{MainData.HSCWHID},{MainData.HEmpID},{MainData.HManagerID},{MainData.HSecManagerID},{MainData.HKeeperID},{MainData.HDeptID},{MainData.HCurID}
                                    ,'{MainData.HSeOrderBillNo}','{MainData.HExplanation}','{MainData.HRemark}','{MainData.HInnerBillNo}',{MainData.HRedBlueFlag},{MainData.HBillStatus}
                                    ,'{MainData.HMaker}','{MainData.HMakeDate}'
                                    ,'{MainData.HChecker}',case when '{MainData.HCheckDate}' = '1900/1/1 0:00:00' then NULL else '{MainData.HCheckDate}' end
                                    ,'{MainData.HUpDater}',case when '{MainData.HUpDateDate}' = '1900/1/1 0:00:00' then NULL else '{MainData.HUpDateDate}' end
                                    ,'{MainData.HDeleteMan}',case when '{MainData.HDeleteDate}' = '1900/1/1 0:00:00' then NULL else '{MainData.HDeleteDate}' end
                                    , {MainData.HOWNERID},'{MainData.HOWNERTYPEID}',{MainData.HERPInterID},'{MainData.HERPBillType}','{MainData.HERPBillNo}'
                                    , {MainData.HPRDORGID},{MainData.HSTOCKORGID})";
                        oCn.RunProc(sql.Replace("\r\n", ""));
                        //子表数据赋值
                        var dataArr = _saveObj["Result"]["Result"]["Entity"];
                        foreach (var oSub in dataArr)
                        {
                            var subData = new
                            {
                                HInterID = HNewInterID,
                                HEntryID = DBUtility.ClsPub.isLong(oSub["Id"]),
                                HMTONo = DBUtility.ClsPub.isStrNull(oSub["MTONO"]),
                                HPlanMode = 0,
                                HRemark = "",
                                HExpressNumber = "",
                                HMaterID = DBUtility.ClsPub.isLong(oSub["MaterialId_Id"]),
                                HUnitID = DBUtility.ClsPub.isLong(oSub["UnitId_Id"]),
                                HBatchNo = DBUtility.ClsPub.isStrNull(oSub["Lot_Text"]),
                                HPropertyID = DBUtility.ClsPub.isLong(oSub["AuxPropId_Id"]),
                                HWHID = DBUtility.ClsPub.isLong(oSub["StockId_Id"]),
                                HSPID = DBUtility.ClsPub.isLong(oSub["StockLocId_Id"]),
                                HSCWHID = 0,
                                HSCSPID = 0,
                                HQtyMust = DBUtility.ClsPub.isDoule(oSub["AppQty"]),
                                HQty = DBUtility.ClsPub.isDoule(oSub["ActualQty"]),
                                HPrice = DBUtility.ClsPub.isDoule(oSub["FPrice"]),
                                HMoney = DBUtility.ClsPub.isDoule(oSub["FAmount"]),
                                HTaxPrice = DBUtility.ClsPub.isDoule(oSub["FPrice"]),
                                HTaxRate = 1,
                                HTaxMoney = DBUtility.ClsPub.isDoule(oSub["FAmount"]),
                                HRelationQty = DBUtility.ClsPub.isDoule(oSub["SelPrcdReturnQty"]),
                                HRelationMoney = 0,
                                HSourceInterID = DBUtility.ClsPub.isLong(oSub["SrcInterId"]),
                                HSourceEntryID = DBUtility.ClsPub.isLong(oSub["SrcEnteryId"]),
                                HSourceBillNo = DBUtility.ClsPub.isStrNull(oSub["SrcBillNo"]),
                                HSourceBillType = DBUtility.ClsPub.isStrNull(oSub["SrcBillType"]),
                                HERPInterID = DBUtility.ClsPub.isLong(_saveObj["Result"]["Result"]["Id"]),
                                HERPEntryID = DBUtility.ClsPub.isLong(oSub["Id"]),
                                HERPBillNo_Sub = "",
                                HPOOrderInterID = 0,
                                HPOOrderEntryID = 0,
                                HPOOrderBillNo = "",
                                HSeOrderInterID = 0,
                                HSeOrderEntryID = 0,
                                HSeOrderBillNo = "",
                                HICMOInterID = DBUtility.ClsPub.isLong(oSub["MoId"]),
                                HICMOEntryID = DBUtility.ClsPub.isLong(oSub["MoEntryId"]),
                                HICMOBillNo = DBUtility.ClsPub.isStrNull(oSub["MoBillNo"]),
                                HWWOrderInterID = 0,
                                HWWOrderEntryID = 0,
                                HWWOrderBillNo = "",
                                HProduceDate = DBUtility.ClsPub.isDate(oSub["ProduceDate"]),
                                HExpiryDate = DBUtility.ClsPub.isDate(oSub["ExpiryDate"]),
                                HOWNERID = DBUtility.ClsPub.isLong(oSub["OwnerId_Id"]),
                                HOWNERTYPEID = DBUtility.ClsPub.isStrNull(oSub["OwnerTypeId"]),
                                HKEEPERID = DBUtility.ClsPub.isLong(oSub["KeeperId_Id"]),
                                HKEEPERTYPEID = DBUtility.ClsPub.isStrNull(oSub["KeeperTypeId"]),
                            };
                            //写入子表
                            sql = $@"Insert into Kf_ICStockBillSub
                                       (HInterID,HEntryID,HMTONo,HPlanMode,HRemark,HExpressNumber
                                       ,HMaterID,HUnitID,HBatchNo,HPropertyID,HWHID,HSPID,HSCWHID,HSCSPID
                                       ,HQtyMust,HQty,HPrice,HMoney,HTaxPrice,HTaxRate,HTaxMoney,HRelationQty,HRelationMoney
                                       ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HERPInterID,HERPEntryID,HERPBillNo_Sub
                                       ,HPOOrderInterID,HPOOrderEntryID,HPOOrderBillNo,HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo
                                       ,HICMOInterID,HICMOEntryID,HICMOBillNo,HWWOrderInterID,HWWOrderEntryID,HWWOrderBillNo
                                       ,HProduceDate
                                        ,HExpiryDate
                                        ,HOWNERID,HOWNERTYPEID,HKEEPERID,HKEEPERTYPEID)
                                        Values ({subData.HInterID},{subData.HEntryID},'{subData.HMTONo}',{subData.HPlanMode},'{subData.HRemark}','{subData.HExpressNumber}'
                                        ,{subData.HMaterID},{subData.HUnitID},'{subData.HBatchNo}',{subData.HPropertyID},{subData.HWHID},{subData.HSPID},{subData.HSCWHID},{subData.HSCSPID}
                                        ,{subData.HQtyMust},{subData.HQty},{subData.HPrice},{subData.HMoney},{subData.HTaxPrice},{subData.HTaxRate},{subData.HTaxMoney},{subData.HRelationQty},{subData.HRelationMoney}
                                        ,{subData.HSourceInterID},{subData.HSourceEntryID},'{subData.HSourceBillNo}','{subData.HSourceBillType}',{subData.HERPInterID},{subData.HERPEntryID},'{subData.HERPBillNo_Sub}'
                                        ,{subData.HPOOrderInterID},{subData.HPOOrderEntryID},'{subData.HPOOrderBillNo}',{subData.HSeOrderInterID},{subData.HSeOrderEntryID},'{subData.HSeOrderBillNo}'
                                        ,{subData.HICMOInterID},{subData.HICMOEntryID},'{subData.HICMOBillNo}',{subData.HWWOrderInterID},{subData.HWWOrderEntryID},'{subData.HWWOrderBillNo}'
                                        ,case when '{subData.HProduceDate}' = '1900/1/1 0:00:00' then NULL else '{subData.HProduceDate}' end
                                        ,case when '{subData.HExpiryDate}' = '1900/1/1 0:00:00' then NULL else '{subData.HExpiryDate}' end
                                        ,{subData.HOWNERID},'{subData.HOWNERTYPEID}',{subData.HKEEPERID},'{subData.HKEEPERTYPEID}')";
                            oCn.RunProc(sql.Replace("\r\n", ""));
                        }
                        oCn.Commit();
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "单据同步成功!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "不支持同步功能!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "获取系统参数失败! " + sErrMsg;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "单据同步失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region ç”Ÿäº§é¢†æ–™å• å®¡æ ¸åŒæ­¥æ’ä»¶ è°ƒç”¨æŽ¥å£
        [Route("Kf_MateOutBill/Kf_MateOutBillSaveApi")]
        [HttpPost]
        public object Kf_MateOutBillSaveApi([FromBody] JObject sMainSub)
        {
            try
            {
                oCn.BeginTran();
                LogService.Write("生产领料单同步,保存方法执行:" + sMainSub.ToString());
                sMainSub["model"]["hredblueflag"] = string.Equals(sMainSub["model"]["hredblueflag"].ToString(), "0") ? false : true;
                var model = sMainSub["model"].ToString();
                // foreach(JObject entryOne in sMainSub["model"]["HENTRY"])
                // {
                //     entryOne["hentryclosedate"] = string.IsNullOrWhiteSpace(entryOne["hentryclosedate"].ToString())
                //     ? new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToString() : entryOne["hentryclosedate"];
                //
                //     entryOne["hclosetype"] = string.Equals(entryOne["hclosetype"].ToString(), "0") ? false : true;
                // }
                var entry = sMainSub["model"]["HENTRY"].ToString();
                model = "[" + model.ToString() + "]";
                List<ClsKf_MateOutBillMain> mainList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsKf_MateOutBillMain>>(model);
                List<ClsKf_MateOutBillSub> subList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsKf_MateOutBillSub>>(entry);
                var HInterID_Old = mainList[0].HInterID;
                // åˆ¤æ–­ æ•°æ®åº“中是否存在id和单据号相等的打单据,存在,则重建,只有id号相等,则申请新的id号
                var ds1 = oCn.RunProcReturn($@"
                select hmainid HInterID, å•据号 HBillNO from h_v_IF_MateOutBillList_Search where hmainid = {mainList[0].HInterID}
                ", "h_v_IF_MateOutBillList_Search");
                if (ds1.Tables[0].Rows.Count > 0 && ds1.Tables[0].Rows[0]["HInterID"] != null) {
                    if(string.Equals(ds1.Tables[0].Rows[0]["HBillNo"].ToString(), mainList[0].HBillNo, StringComparison.OrdinalIgnoreCase)){
                        string sql = string.Empty;
                        sql = $"delete Kf_ICStockBillMain where HinterID = {mainList[0].HInterID}";
                        oCn.RunProc(sql);
                        sql = $"delete Kf_ICStockBillSub where HinterID = {mainList[0].HInterID}";
                        oCn.RunProc(sql);
                    }
                    else
                    {
                        // é‡æ–°ç”³è¯·HInterID
                        var HInterID_Exch =  DBUtility.ClsPub.CreateBillID("1204", ref DBUtility.ClsPub.sExeReturnInfo);
                        mainList[0].HInterID = HInterID_Exch;
                        foreach(var oSub in subList)
                        {
                            oSub.HInterID = HInterID_Exch;
                        }
                    }
                }
                // string sql = string.Empty;
                // sql = $"delete Kf_ICStockBillMain where HinterID = {mainList[0].HInterID}";
                // oCn.RunProc(sql);
                // sql = $"delete Kf_ICStockBillSub where HinterID = {mainList[0].HInterID}";
                // oCn.RunProc(sql);
                // ä¸»è¡¨
                oCn.RunProc($@"Insert Into Kf_ICStockBillMain
                (
                    HInterID, HYear, HPeriod, HBillNo, HBillType, HBillSubType, HMainSourceBillType, HDate,
                    HSupID, HWHID, HSCWHID, HEmpID, HManagerID, HSecManagerID, HKeeperID, HDeptID, HCurID,
                    HSeOrderBillNo, HExplanation, HRemark, HInnerBillNo, HRedBlueFlag, HBillStatus,
                    HMaker, HMakeDate, HChecker, HCheckDate,
                    HUpDater, HUpDateDate, HDeleteMan, HDeleteDate,
                    HOWNERID, HOWNERTYPEID, HERPInterID, HERPBillType, HERPBillNo,
                    HPRDORGID, HSTOCKORGID, HSTOCKERGROUPID, HPURCHASERGROUPID, HDEMANDORGID, HCORRESPONDORGID,
                    HPROVIDERCONTACTID, HSUPPLYID, HSETTLEID, HCHARGEID, HPURCHASEDEPTID, HBUSINESSTYPE,
                    HSUPPLYADDRESS, HPAYORGID, HSETTLEORGID, HSETTLETYPEID, HPAYCONDITIONID, HSETTLECURRID,
                    HEXCHANGETYPEID, HDISCOUNTLISTID, HEXCHANGERATE, HPRICETIMEPOINT, HLOCALCURRID, HISINCLUDEDTAX,
                    HISPRICEEXCLUDETAX
                )
                values(
                    '{mainList[0].HInterID}','{mainList[0].HYear}','{mainList[0].HPeriod}','{mainList[0].HBillNo}',
                    '{mainList[0].HBillType}','{mainList[0].HBillSubType}','{mainList[0].HMainSourceBillType}','{mainList[0].HDate}',
                    '{mainList[0].HSupID}','{mainList[0].HWHID}','{mainList[0].HSCWHID}','{mainList[0].HEmpID}','{mainList[0].HManagerID}',
                    '{mainList[0].HSecManagerID}','{mainList[0].HKeeperID}','{mainList[0].HDeptID}','{mainList[0].HCurID}',
                    '{mainList[0].HSeOrderBillNo}','{mainList[0].HExplanation}','{mainList[0].HRemark}','{mainList[0].HInnerBillNo}',
                    '{mainList[0].HRedBlueFlag}',{mainList[0].HBillStatus},'{mainList[0].HMaker}','{mainList[0].HMakeDate}',
                    '{mainList[0].HChecker}','{mainList[0].HCheckDate}','{mainList[0].HUpDater}','{mainList[0].HUpDateDate}',
                    '{mainList[0].HDeleteMan}','{mainList[0].HDeleteDate}','{mainList[0].HOWNERID}','{mainList[0].HOWNERTYPEID}',
                    '{mainList[0].HERPInterID}','{mainList[0].HERPBillType}','{mainList[0].HERPBillNo}','{mainList[0].HPRDORGID}',
                    '{mainList[0].HSTOCKORGID}','{mainList[0].HSTOCKERGROUPID}','{mainList[0].HPURCHASERGROUPID}','{mainList[0].HDEMANDORGID}',
                    '{mainList[0].HCORRESPONDORGID}','{mainList[0].HPROVIDERCONTACTID}','{mainList[0].HSUPPLYID}','{mainList[0].HSETTLEID}',
                    '{mainList[0].HCHARGEID}','{mainList[0].HPURCHASEDEPTID}','{mainList[0].HBUSINESSTYPE}','{mainList[0].HSUPPLYADDRESS}',
                    '{mainList[0].HPAYORGID}','{mainList[0].HSETTLEORGID}','{mainList[0].HSETTLETYPEID}','{mainList[0].HPAYCONDITIONID}',
                    '{mainList[0].HSETTLECURRID}','{mainList[0].HEXCHANGETYPEID}','{mainList[0].HDISCOUNTLISTID}','{mainList[0].HEXCHANGERATE}',
                    '{mainList[0].HPRICETIMEPOINT}','{mainList[0].HLOCALCURRID}','{mainList[0].HISINCLUDEDTAX}','{mainList[0].HISPRICEEXCLUDETAX}'
                )");
                // å­è¡¨
                foreach (var osub in subList)
                {
                    // var syncSorceBillRes = oCn.RunProcReturn($@"
                    //     exec h_p_IFCLD_ERPSourceBillToLocal_MateOutBill_GetSourceInfo
                    //     '{osub.HSourceBillType}', '{osub.HSourceBillNo}', {osub.HSourceInterID}, {osub.HSourceEntryID}
                    // ", "h_p_IFCLD_ERPSourceBillToLocal_MateOutBill_GetSourceInfo");
                    // LogService.Write($@"
                    //     exec h_p_IFCLD_ERPSourceBillToLocal_MateOutBill_GetSourceInfo
                    //     '{osub.HSourceBillType}', '{osub.HSourceBillNo}', {osub.HSourceInterID}, {osub.HSourceEntryID}
                    // ", "h_p_IFCLD_ERPSourceBillToLocal_MateOutBill_GetSourceInfo");
                    // string HERPInterID = "0";
                    // string HERPEntryID = "0";
                    // if (syncSorceBillRes != null && syncSorceBillRes.Tables.Count > 0)
                    // {
                    //     osub.HSourceEntryID = (long)syncSorceBillRes.Tables[0].Rows[0]["HEntryID"];
                    //     osub.HSourceInterID = (long)syncSorceBillRes.Tables[0].Rows[0]["HInterID"];
                    //     HERPInterID = syncSorceBillRes.Tables[0].Rows[0]["HERPInterID"].ToString();
                    //     HERPEntryID = syncSorceBillRes.Tables[0].Rows[0]["HERPInterID"].ToString();
                    // }
                    oCn.RunProc($@"Insert into Kf_ICStockBillSub
                    (
                        HInterID, HEntryID, HMTONo, HPlanMode, HRemark, HExpressNumber,
                        HMaterID, HUnitID, HBatchNo, HPropertyID, HWHID, HSPID, HSCWHID, HSCSPID,
                        HQtyMust, HQty, HPrice, HMoney, HTaxPrice, HTaxRate, HTaxMoney, HRelationQty, HRelationMoney,
                        HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HERPInterID, HERPEntryID, HERPBillNo_Sub,
                        HPOOrderInterID, HPOOrderEntryID, HPOOrderBillNo, HSeOrderInterID, HSeOrderEntryID, HSeOrderBillNo,
                        HICMOInterID, HICMOEntryID, HICMOBillNo, HWWOrderInterID, HWWOrderEntryID, HWWOrderBillNo,
                        HProduceDate, HExpiryDate, HOWNERID, HOWNERTYPEID, HKEEPERID, HKEEPERTYPEID,
                        HSUPPLIERLOT, HREQTRACENO, HPROJECTNO, HGIVEAWAY, HSYSPRICE, HPRICECOEFFICIENT,
                        HDISCOUNTRATE, HBASEUNITPRICE, HTAXCOMBINATION, HPRICELISTENTRY, HCOSTPRICE, HSEQ,
                        HROWTYPE, HPARENTMATID, HPRILSTENTRYID, HPURBASENUM, HSTOCKBASEDEN, HBFLOWID,
                        HSTOCKSTATUSID, HPRICEUNITID, HBASEUNITID, HSNUNITID, HREMAININSTOCKUNITID, HEXTAUXUNITID,
                        HTAXRATEID, HTAXRATE_TAX, HTAXAMOUNT, HCOSTPERCENT, HCOSTAMOUNT, HVAT,
                        HSELLERWITHHOLDING, HBUYERWITHHOLDING
                    )
                    values (
                        '{osub.HInterID}','{osub.HEntryID}','{osub.HMTONo}',{osub.HPlanMode},'{osub.HRemark}','{osub.HExpressNumber}',
                        '{osub.HMaterID}','{osub.HUnitID}','{osub.HBatchNo}','{osub.HPropertyID}','{osub.HWHID}',
                        '{osub.HSPID}','{osub.HSCWHID}','{osub.HSCSPID}', '{osub.HQtyMust}','{osub.HQty}','{osub.HPrice}',
                        '{osub.HMoney}', '{osub.HTaxPrice}', '{osub.HTaxRate}', '{osub.HTaxMoney}', '{osub.HRelationQty}',
                        '{osub.HRelationMoney}','{osub.HSourceInterID}','{osub.HSourceEntryID}','{osub.HSourceBillNo}',
                        '{osub.HSourceBillType}','{osub.HERPInterID}','{osub.HERPEntryID}','{osub.HERPBillNo_Sub}',
                        '{osub.HPOOrderInterID}','{osub.HPOOrderEntryID}','{osub.HPOOrderBillNo}','{osub.HSeOrderInterID}',
                        '{osub.HSeOrderEntryID}','{osub.HSeOrderBillNo}','{osub.HICMOInterID}','{osub.HICMOEntryID}',
                        '{osub.HICMOBillNo}','{osub.HWWOrderInterID}','{osub.HWWOrderEntryID}', '{osub.HWWOrderBillNo}',
                        '{osub.HProduceDate}','{osub.HExpiryDate}', '{osub.HOWNERID}','{osub.HOWNERTYPEID}','{osub.HKEEPERID}',
                        '{osub.HKEEPERTYPEID}', '{osub.HSUPPLIERLOT}', '{osub.HREQTRACENO}', '{osub.HPROJECTNO}','{osub.HGIVEAWAY}',
                        '{osub.HSYSPRICE}', '{osub.HPRICECOEFFICIENT}','{osub.HDISCOUNTRATE}','{osub.HBASEUNITPRICE}', '{osub.HTAXCOMBINATION}',
                        '{osub.HPRICELISTENTRY}','{osub.HCOSTPRICE}', '{osub.HSEQ}','{osub.HROWTYPE}','{osub.HPARENTMATID}', '{osub.HPRILSTENTRYID}',
                        '{osub.HPURBASENUM}','{osub.HSTOCKBASEDEN}', '{osub.HBFLOWID}','{osub.HSTOCKSTATUSID}','{osub.HPRICEUNITID}', '{osub.HBASEUNITID}',
                        '{osub.HSNUNITID}', '{osub.HREMAININSTOCKUNITID}','{osub.HEXTAUXUNITID}','{osub.HTAXRATEID}','{osub.HTAXRATE_TAX}','{osub.HTAXAMOUNT}',
                        '{osub.HCOSTPERCENT}','{osub.HCOSTAMOUNT}','{osub.HVAT}','{osub.HSELLERWITHHOLDING}','{osub.HBUYERWITHHOLDING}'
                    )");
                }
                oCn.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "单据保存成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch(Exception e)
            {
                LogService.Write("采购订单同步异常,保存方法执行完成异常:" + e.Message.ToString());
                oCn.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region æ¡ç æ˜Žç»†
        /// <summary>
        /// </summary>
        /// <param name="HInterID">单据ID</param>
        /// <returns></returns>
        [Route("Kf_MateOutBillController/BarCodeNote")]
        [HttpGet]
        public object SellOutBill_BarCodeNote(int HInterID)
        {
            try
            {
                List<object> columnNameListSum = new List<object>();
                List<object> columnNameList0 = new List<object>();
                List<object> columnNameList1 = new List<object>();
                List<object> columnNameList2 = new List<object>();
                string sql = "exec  h_p_IFCLD_ERPSourceBillToLocal_MateOut " + HInterID;
                ds = oCn.RunProcReturn(sql, " h_p_IFCLD_ERPSourceBillToLocal_MateOut");
                //添加 åŸºæœ¬ä¿¡æ¯ åˆ—名
                foreach (DataColumn col in ds.Tables[1].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList0.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                //添加 ç å•信息 åˆ—名
                foreach (DataColumn col in ds.Tables[2].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList1.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                //添加 æ¡ç æ˜Žç»† åˆ—名
                foreach (DataColumn col in ds.Tables[3].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList2.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                columnNameListSum.Add(columnNameList0);
                columnNameListSum.Add(columnNameList1);
                columnNameListSum.Add(columnNameList2);
                List<DataTable> tableList = new List<DataTable>();
                tableList.Add(ds.Tables[1]);
                tableList.Add(ds.Tables[2]);
                tableList.Add(ds.Tables[3]);
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = tableList;
                objJsonResult.list = columnNameListSum;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}