black-goat-me
2023-05-19 46ca053239e9841f4880873494bd1d58b275b6ad
WebAPI/Controllers/CJGL/Sc_ProcessExchangeBillController.cs
@@ -18,6 +18,7 @@
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        #region[新增单据-保存按钮]
        /// <summary>
@@ -373,6 +374,112 @@
        //}
        #endregion
        #region[新增单据-保存按钮-批量复制]
        /// <summary>
        /// 新增单据-保存按钮
        ///参数:string sql。
        ///返回值:object。
        /// </summary>
        [Route("Sc_ProcessExchangeBill/AddBill_Copy_pl")]
        [HttpPost]
        public object AddBill_Copy_pl([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            int msg2 = int.Parse(sArray[0].ToString());
            string msg3 = sArray[1].ToString();
            string msg4 = sArray[2].ToString();
            string msg5 = sArray[3].ToString();
            string UserName = "";
            ListModels oListModels = new ListModels();
            string sReturn = "";
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Sc_ProcessExchangeBill_Edit", 1, false, msg4))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                DAL.ClsSc_ProcessExchangeBill oBill = new DAL.ClsSc_ProcessExchangeBill();
                DBUtility.ClsPub.HOrgID = long.Parse(msg5);
                DBUtility.ClsPub.CurUserName = msg4;
                oBill.ShowBill(long.Parse(msg3), ref sReturn);
                ds = oCN.RunProcReturn("select isnull(sum(HQty),0) from Sc_ProcessExchangeBillMain where HICMOBillNo='" + oBill.omodel.HICMOBillNo + "' and HInterID <>"+ oBill.omodel.HInterID, "Sc_ProcessExchangeBillMain");
                var HQty = double.Parse(ds.Tables[0].Rows[0][0].ToString());
                int num = 0;
                //单据数量 * 张数 + 已生成不包括本单据的数量 >生产订单数量
                if ((oBill.omodel.HQty * msg2) + HQty > oBill.omodel.HPlanQty)
                {
                    if (((oBill.omodel.HPlanQty - oBill.omodel.HQty - HQty) % oBill.omodel.HQty) == 0)
                    {
                        num = (int)((oBill.omodel.HPlanQty - oBill.omodel.HQty - HQty) / oBill.omodel.HQty);
                    } else
                    {
                        num = (int)((oBill.omodel.HPlanQty - oBill.omodel.HQty - HQty) / oBill.omodel.HQty);
                        num += 1;
                    }
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "最多可以复制" + num + "张!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (num == 0) {
                    double HPlanQty = oBill.omodel.HPlanQty - oBill.omodel.HQty - HQty;
                    for (int i = 0; i < msg2; i++)
                    {
                        oBill.omodel.HBillNo = DBUtility.ClsPub.CreateBillCode("3772", ref DBUtility.ClsPub.sExeReturnInfo, true);
                        oBill.omodel.HDate =DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
                        oBill.omodel.HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year);
                        oBill.omodel.HPeriod = DBUtility.ClsPub.isLong(DateTime.Now.Month);
                        oBill.omodel.HChecker = "";
                        oBill.omodel.HCheckDate = "";
                        oBill.omodel.HMaker = msg4;
                        if (HPlanQty < oBill.omodel.HQty)
                        {
                            oBill.omodel.HQty = HPlanQty;
                        }
                        //保存完毕后处理
                        bool bResult;
                        bResult = oBill.AddBill(ref sReturn);
                        if (bResult)
                        {
                            num++;
                            HPlanQty -= oBill.omodel.HQty;
                        }
                    }
                }
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "复制了" + num + "张单据!";
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = 1;
                return objJsonResult;
            }
        }
        #endregion
        #region[批量新增单据-保存按钮]
        /// <summary>
        /// 新增单据-保存按钮
@@ -698,7 +805,7 @@
        /// <returns></returns>
        [Route("Sc_ProcessExchangeBill/DeltetProcessExchangeBillByID")]
        [HttpGet]
        public object DeltetProcessExchangeBillByID(string HInterID,int HPRDORGID, string user)
        public object DeltetProcessExchangeBillByID(string HInterID, int HPRDORGID, string user)
        {
            //编辑权限
            if (!DBUtility.ClsPub.Security_Log("Sc_ProcessExchangeBill_Drop", 1, false, user))
@@ -710,69 +817,91 @@
                return objJsonResult;
            }
            Int64 lngBillKey = 0;
            lngBillKey = DBUtility.ClsPub.isLong(HInterID);
            if (lngBillKey == 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "单据ID为空!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            //删除前判断关联数量
            SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
            DataSet ds;
            ds = oCn.RunProcReturn("select ct from (  " +
                          "  select 1 ct from Sc_StationInBillMain Where HProcExchInterID=" + lngBillKey + " " +
                          "  union all " +
                          "  select 1 from Sc_StationOutBillMain Where HProcExchInterID=" + lngBillKey + " ) as a  ", "Sc_ProcExchReportSub");
            if (ds.Tables[0].Rows.Count != 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "流转卡已经有下游进出站单后,不允许删除!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            DAL.ClsSc_ProcessExchangeBill oBill = new DAL.ClsSc_ProcessExchangeBill();
            DBUtility.ClsPub.HOrgID = HPRDORGID;
            if (oBill.ShowBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo))
            {
                if (oBill.omodel.HBillStatus > 1)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据当前处于不能删除状态,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (oBill.omodel.HChecker != "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已经审核,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                bool IsDete = oBill.DeleteBill(oBill.omodel.HInterID, ref DBUtility.ClsPub.sExeReturnInfo);
                if (IsDete)
            string[] HBillNo = HInterID.Split(',');
            foreach (var item in HBillNo)
            {
                Int64 lngBillKey = 0;
                lngBillKey = DBUtility.ClsPub.isLong(item);
                DBUtility.ClsPub.HOrgID = HPRDORGID;
                if (oBill.ShowBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 1;
                    objJsonResult.Message = DBUtility.ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                    if (lngBillKey == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据号:"+ oBill .omodel.HBillNo+ "单据ID为空!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //删除前判断关联数量
                    SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                    DataSet ds;
                    ds = oCn.RunProcReturn("select ct from (  " +
                                  "  select 1 ct from Sc_StationInBillMain Where HProcExchInterID=" + lngBillKey + " " +
                                  "  union all " +
                                  "  select 1 from Sc_StationOutBillMain Where HProcExchInterID=" + lngBillKey + " ) as a  ", "Sc_ProcExchReportSub");
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据号:" + oBill.omodel.HBillNo + "流转卡已经有下游进出站单后,不允许删除!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    string sReturn = "";
                    if (oSystemParameter.ShowBill(ref sReturn))
                    {
                        if (oSystemParameter.omodel.Sc_ProcessExchangeBill_DeleterAndMakerMustSame == "Y")
                        {
                            if (oBill.omodel.HMaker != user && (user != "admin" && user != "Admin"))
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 0;
                                objJsonResult.Message = "单据号:" + oBill.omodel.HBillNo + "只能删除本人的单据!";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
                    }
                    if (oBill.omodel.HBillStatus > 1)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据号:" + oBill.omodel.HBillNo + "单据当前处于不能删除状态,不能删除!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (oBill.omodel.HChecker != "")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据号:" + oBill.omodel.HBillNo + "单据已经审核,不能删除!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据未找到";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
            }
            foreach (var item in HBillNo)
            {
                bool IsDete = oBill.DeleteBill(long.Parse(item), ref DBUtility.ClsPub.sExeReturnInfo);
                if (!IsDete)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = DBUtility.ClsPub.sExeReturnInfo;
@@ -780,14 +909,11 @@
                    return objJsonResult;
                }
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "单据未找到";
                objJsonResult.data = null;
                return objJsonResult;
            }
            objJsonResult.code = "0";
            objJsonResult.count = 1;
            objJsonResult.Message = DBUtility.ClsPub.sExeReturnInfo;
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
@@ -903,12 +1029,26 @@
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                var ds1= oCN.RunProcReturn("select isnull(max(流水号),0) from h_v_Sc_ProcessExchangeBillQuerySub where hmainid=" + HInterID + " and 出站关联数量>0 ", "h_v_Sc_ProcessExchangeBillQuerySub");
                string sql = string.Format(@"select * from h_v_Sc_ProcessExchangeBillQuerySub");
                ds = oCN.RunProcReturn(sql + " where hmainid=" + HInterID, "h_v_Sc_ProcessExchangeBillQuerySub");
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取信息成功!";
                objJsonResult.data = ds.Tables[0];
                ds = oCN.RunProcReturn(sql + " where hmainid=" + HInterID + " and 出站关联数量=0  and  cast(流水号 as int)>" + ds1 .Tables[0].Rows[0][0].ToString(), "h_v_Sc_ProcessExchangeBillQuerySub");
                if (float.Parse(ds.Tables[0].Rows[0]["进站关联数量"].ToString()) > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "当前单据:"+ ds.Tables[0].Rows[0]["工序名称"].ToString() + "未出站不能拆分!";
                    objJsonResult.data = ds.Tables[0];
                }
                else {
                    objJsonResult.code = "0";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "获取信息成功!";
                    objJsonResult.data = ds.Tables[0];
                }
            }
            catch (Exception e)
            {
@@ -990,26 +1130,52 @@
                //msg2 = msg2.Replace("'", "’");
                List<Model.ClsSc_ProcessExchangeBillSub> ls = new List<Model.ClsSc_ProcessExchangeBillSub>();
                ls = oListModels.getObjectByJson_Sc_ProcessExchangeBillSub(msg3);
                int i = 0;
                foreach (Model.ClsSc_ProcessExchangeBillSub oItemSub in ls)
                {
                string HEntryID = "";
                    if (oItemSub.HSplitQty > oItemSub.HLeftQty)
                for (int i = 0; i < oBill.DetailColl.Count; i++)
                {
                    if (oBill.DetailColl[i].HRelationQty_Out > 0 || oBill.DetailColl[i].HRelationQty_In > 0)
                    {
                        HEntryID += i + ",";
                    }
                }
                if (HEntryID != "")
                {
                    HEntryID = HEntryID.Substring(0, HEntryID.Length - 1);
                    var num = HEntryID.Split(',');
                    for (int k = 0; k < num.Length; k++)
                    {
                        oBill.DetailColl.RemoveAt(int.Parse(num[k]) - k);
                    }
                }
                int j = 0 ;
                foreach (Model.ClsSc_ProcessExchangeBillSub oItemSub in oBill.DetailColl)
                {
                    if (ls[j].HSplitQty > ls[j].HLeftQty)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "第" + (i + 1) + "行,拆分数量不能大于可拆分数量!";
                        objJsonResult.Message = "第" + (j + 1) + "行,拆分数量不能大于可拆分数量!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    oBill.DetailColl[i].HQty = oItemSub.HQty;
                    oBill.DetailColl[i].HBackProc = oItemSub.HBackProc;
                    oBill.DetailColl[i].HPrevSourceInterID = oItemSub.HPrevSourceInterID;
                    oBill.DetailColl[i].HPrevSourceEntryID = oItemSub.HPrevSourceEntryID;
                    oBill.DetailColl[i].HRemark = oItemSub.HRemark;
                    oBill.DetailColl[i].HTechnologyParameter = oItemSub.HTechnologyParameter;
                    i++;
                    if (oBill.DetailColl[j].HProcNo == ls[j].HProcNo)
                    {
                        oBill.DetailColl[j].HEntryID = j+1;
                        oBill.DetailColl[j].HQty = ls[j].HQty;
                        oBill.DetailColl[j].HBackProc = ls[j].HBackProc;
                        oBill.DetailColl[j].HPrevSourceInterID = ls[j].HPrevSourceInterID;
                        oBill.DetailColl[j].HPrevSourceEntryID = ls[j].HPrevSourceEntryID;
                        oBill.DetailColl[j].HRemark = ls[j].HRemark;
                        oBill.DetailColl[j].HTechnologyParameter = ls[j].HTechnologyParameter;
                        j++;
                    }
                }
                //保存
                //保存完毕后处理
@@ -1022,6 +1188,13 @@
                //{
                //    bResult = oBill.ModifyBill(oBill.omodel.HInterID, ref DBUtility.ClsPub.sExeReturnInfo);
                //}
                oCN.BeginTran();
                oCN.RunProc("update Sc_ProcessExchangeBillSub set HQty=(select HQty from Sc_ProcessExchangeBillMain where HInterID="+ oBill.DetailColl[0].HPrevSourceInterID + ") where HInterID=" + oBill.DetailColl[0].HPrevSourceInterID );
                oCN.Commit();
                if (bResult)
                {
                    objJsonResult.code = "0";
@@ -1050,5 +1223,92 @@
        }
        #endregion
        #region 单据变更 页面初始化
        [Route("Sc_ProcessExchangeBill/GetProcessExchangeBillMain_Change_init")]
        [HttpGet]
        public object GetProcessExchangeBillMain_Change_init(string HInterID)
        {
            try
            {
                if (string.IsNullOrEmpty(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "Exception!HInterID不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("select * from h_v_Sc_ProcessExchangeBillQuery  where hmainid = " + HInterID + " ", "h_v_Sc_ProcessExchangeBillQuery");
                if (ds==null||ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "Exception!不存在工序流转卡";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    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 单据变更 保存
        [Route("Sc_ProcessExchangeBill/GetProcessExchangeBillMain_Change_save")]
        [HttpGet]
        public object GetProcessExchangeBillMain_Change_save(string HInterID,string HMaterModel2)
        {
            try
            {
                if (string.IsNullOrEmpty(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "Exception!HInterID不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                oCN.BeginTran();
                oCN.RunProc("update Sc_ProcessExchangeBillMain set HMaterModel2 = '" + HMaterModel2 + "' where HInterID = " + HInterID + "");
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}