yl
2023-04-12 01b5ef474e1a5d8dc682b64312f23bdbb0fdf3a7
VueWebApi/DLL/DAL/AppPurchManagementDAL.cs
@@ -36,7 +36,7 @@
                //查询未关闭且未全部到货的采购订单
                sql = @"select distinct A.ordercode  from T_PurchaseOrder A
                        inner join T_PurchaseOrder_sub B on A.ordercode=B.purchaseorder_code
                        where B.countarrivalquantity<quantity and A.status<>'CLOSED'" + search;
                        where (isnull(B.quantity,0)-isnull(B.countarrivalquantity,0)+isnull(B.countreturnquantity,0))>0 and A.status<>'CLOSED'" + search;
                dynamicParams.Add("@purchordercode", purchordercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
@@ -80,7 +80,7 @@
                        left  join TUom U on M.uom_code=U.code
                        left  join TCustomer C on B.customercode=C.code
                        left  join TOrganization T on A.departmentcode=T.org_code
                        where B.quantity>B.countarrivalquantity and A.ordercode=@purchordercode";
                        where (isnull(B.quantity,0)-isnull(B.countarrivalquantity,0)+isnull(B.countreturnquantity,0))>0 and A.ordercode=@purchordercode";
                dynamicParams.Add("@purchordercode", purchordercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
@@ -116,7 +116,7 @@
        #endregion
        #region[采购到货:提交]
        public static ToMessage MesAppPurchOrderSave(string purordercode, string orderstaus, string username, List<AppPurchOrderSave> json)
        public static ToMessage MesAppPurchOrderSave(string purordercode, string username, List<AppPurchOrderSave> json)
        {
            var sql = "";
            string djwo = "", numvalue = "";
@@ -159,7 +159,7 @@
                        hbillno = djwo,
                        hdate = DateTime.Now.ToString(),
                        hacceptdate = hacceptdate == "" ? null : hacceptdate,
                        hbillstaus = orderstaus,
                        hbillstaus = "NEW",
                        hdepartmentcode = dpartment,
                        hcustomercode = customercode,
                        hsourcebillno = purordercode,
@@ -234,17 +234,6 @@
                        }
                    });
                }
                //更新采购订单主表:订单状态
                sql = @"update T_PurchaseOrder set status=@status where ordercode=@ordercode";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        status = orderstaus,
                        ordercode = purordercode
                    }
                });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
@@ -274,16 +263,25 @@
        #region[采购入库选择有到货采购订单]
        public static ToMessage MesAppPurchInStorSelectCGOrder()
        public static ToMessage MesAppPurchInStorSelectCGOrder(string purchordercode)
        {
            string sql = "";
            string search = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                if (purchordercode != "" && purchordercode != null) //订单号不为空
                {
                    search += " and AA.ordercode like '%'+@purchordercode+'%' ";
                }
                //有到货或者部分入库的采购订单
                sql = @"select distinct A.ordercode   from T_PurchaseOrder A
                sql = @"select distinct AA.ordercode  from(
                        select A.ordercode,B.hbillstaus,S.hpartcode,MAX(S.hplanqty) as hplanqty,SUM(S.hqty) as hqty,SUM(S.hinqty) as hinqty,SUM(S.hreturnqty) as hreturnqty
                        from T_PurchaseOrder A
                        inner join T_PurchaseArrivalOrder B on A.ordercode=B.hsourcebillno
                        where B.hbillstaus in('BINARIVE','INARIVE','BINSTOG')";
                        inner join T_PurchaseArrivalOrder_sub S on B.hbillno=S.hbillno
                        group by A.ordercode,B.hbillstaus,S.hpartcode
                        ) as AA where  (isnull(AA.hplanqty,0)-isnull(AA.hqty,0)+isnull(AA.hreturnqty,0))>0 and AA.hbillstaus='NEW' " + search;
                var data = DapperHelper.selecttable(sql);
                if (data.Rows.Count > 0)
                {
@@ -322,7 +320,7 @@
                        inner join T_PurchaseArrivalOrder B on A.ordercode=B.hsourcebillno
                        inner join T_PurchaseArrivalOrder_sub S on B.hbillno=S.hbillno
                        left  join TMateriel_Info M on S.hpartcode=M.partcode
                        where B.hbillstaus in('BINARIVE','INARIVE','BINSTOG') and A.ordercode=@ordercode
                        where B.hbillstaus='NEW' and A.ordercode=@ordercode
                        group by A.ordercode,M.partcode,M.partname,M.partspec,A.customercode,A.departmentcode";
                dynamicParams.Add("@ordercode", ordercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
@@ -472,7 +470,19 @@
                    //查询汇总标签信息
                    for (int j = 0; j < arra[i]["labcont"].Count(); j++)//循环获取标签信息
                    {
                        //更新条码表仓库、库位信息
                        sql = @"update T_BarCodeBill set storehouse_code=@storehouse_code,location_code=@location_code where hsourcebillno=@hsourcebillno and hbarcode=@hbarcode";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                storehouse_code = arra[i]["storehousecode"].ToString(),
                                location_code = arra[i]["warehousecode"].ToString(),
                                hsourcebillno = ordercode,
                                hbarcode = arra[i]["labcont"][j]["labcode"].ToString()
                            }
                        });
                        //更新到货单子表入库数量
                        sql = @"update T_PurchaseArrivalOrder_sub set hinqty=isnull(hinqty,0)+@inqty where hbillno=@hbillno and hpartcode=@hpartcode";
                        list.Add(new
@@ -567,27 +577,6 @@
                     * **********************
                    **********************/
                }
                //更新采购定单状态(部分入库、已入库)
                if ((countInquantity + partlabqty) < countarrivalquantity) //当前采购订单累计已入库+本次总入库<当前采购订单累计到货=部分入库
                {
                    storg = "BINSTOG";
                }
                else
                {
                    storg = "INSTOG";
                }
                //更新采购订单主表单据状态
                sql = @"update T_PurchaseOrder set status=@storg
                            where ordercode=@ordercode";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        ordercode = ordercode,
                        storg = storg
                    }
                });
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
@@ -630,9 +619,9 @@
                    search += " and A.ordercode like '%'+@purchordercode+'%' ";
                }
                //查询未关闭且有到货的采购订单
                sql = @"select distinct A.ordercode  from T_PurchaseOrder A
                sql = @"select distinct A.id,A.ordercode  from T_PurchaseOrder A
                        inner join T_PurchaseOrder_sub B on A.ordercode=B.purchaseorder_code
                        where B.countarrivalquantity<=quantity and A.status<>'CLOSED'" + search;
                        where (isnull(B.countarrivalquantity,0)-isnull(B.countreturnquantity,0))>0 and A.status<>'CLOSED'" + search;
                dynamicParams.Add("@purchordercode", purchordercode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
@@ -700,5 +689,221 @@
        }
        #endregion
        #region[采购退货单:扫描标签]
        public static ToMessage MesAppPurchReturnInStorScanLabCode(string ordercode, string partcode, string labcode)
        {
            string sql = "";
            string search = "";
            var dynamicParams = new DynamicParameters();
            try
            {
                //根据采购订单号、物料编码、标签条码查找标签信息
                sql = @"select A.hbarcode,A.hcustomercode,C.name as hcustomercode,
                       A.hbatchno,A.qualitystatus,A.location_code,L.name as location_name,A.hqty
                       from T_BarCodeBill A
                       left join TCustomer C on A.hcustomercode=C.code
                       left join T_Sec_Loca L on A.location_code=L.code
                       where A.rightcode=@rightcode and A.hbilltype=@hbilltype and A.hbarcodestatus='Y'
                       and  A.hsourcebillno=@hsourcebillno and A.hmaterialcode=@hmaterialcode and A.hbarcode=@labcode";
                dynamicParams.Add("@rightcode", "2060");
                dynamicParams.Add("@hbilltype", "1103");
                dynamicParams.Add("@hsourcebillno", ordercode);
                dynamicParams.Add("@hmaterialcode", partcode);
                dynamicParams.Add("@labcode", labcode);
                var data = DapperHelper.selectdata(sql, dynamicParams);
                if (data.Rows.Count > 0)
                {
                    mes.code = "200";
                    mes.Message = "查询成功!";
                    mes.data = data;
                }
                else
                {
                    mes.code = "300";
                    mes.Message = "当前标签不符合要求!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
        #region[采购退货单:提交]
        public static ToMessage MesAppPurchReturnInStorSave(string orderid, string ordercode, string username, string incontent)
        {
            var sql = "";
            string djwo = "", numvalue = "";
            List<object> list = new List<object>();
            List<ObjectData> obj = new List<ObjectData>();
            var dynamicParams = new DynamicParameters();
            try
            {
                string datetime = DateTime.Now.ToString(); //获取系统时间
                mes = SeachEncode.EncodingSeach("2074");  //APP端采购退货生成单号
                if (mes.code == "300")
                {
                    return mes;
                }
                else
                {
                    List<string> wo = (List<string>)mes.data;
                    djwo = wo[0].ToString(); //获取单号
                    numvalue = wo[1].ToString(); //获取流水号
                }
                //获取提交信息
                JArray arra = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(incontent);
                list.Clear();
                //写入采购退货单主表
                sql = @"insert into  T_PurchaseReturnOrder(orderdate,ordercode,status,departmentcode,customercode,saleorderid,saleordercode,lm_user,lm_date)
                                values(@orderdate,@ordercode,@status,@departmentcode,@customercode,@saleorderid,@saleordercode,@lm_user,@lm_date)";
                list.Add(new
                {
                    str = sql,
                    parm = new
                    {
                        orderdate= datetime,
                        ordercode= djwo,
                        status="NEW",
                        departmentcode= arra[0]["departmentcode"].ToString(),
                        customercode= arra[0]["customercode"].ToString(),
                        saleorderid= orderid,
                        saleordercode=ordercode,
                        lm_user= username,
                        lm_date= datetime
                    }
                });
                //根据源单+物料查询采购订单到货总数、入库总数
                sql = @"select sum(B.quantity) as quantity,sum(B.countarrivalquantity) as countarrivalquantity,
                        sum(B.countInquantity) as countInquantity,sum(B.countreturnquantity) as countreturnquantity
                        from T_PurchaseOrder A
                        inner join T_PurchaseOrder_sub B on A.ordercode=B.purchaseorder_code
                        where  ordercode=@ordercode";
                dynamicParams.Add("@ordercode", ordercode);
                var data0 = DapperHelper.selectdata(sql, dynamicParams);
                decimal quantity = decimal.Parse(data0.Rows[0]["quantity"].ToString()); //采购订单累计任务数量
                decimal countarrivalquantity = decimal.Parse(data0.Rows[0]["countarrivalquantity"].ToString()); //采购订单累计到货数量
                decimal countInquantity = decimal.Parse(data0.Rows[0]["countInquantity"].ToString()); //采购订单累计入库数量
                decimal countreturnquantity = decimal.Parse(data0.Rows[0]["countreturnquantity"].ToString()); //采购订单累计退货数量
                string storg = "";//采购订单入库状态
                decimal partlabqty = 0; //当前标签累计数量
                for (int i = 1; i <= arra.Count; i++)//循环获取物料信息
                {
                    string labcode = "";//标签信息
                    decimal suminqty = 0; //单标签入库总数量
                    string batchno = ""; //批次号
                    //查询汇总标签信息
                    for (int j = 0; j < arra[i]["labcont"].Count(); j++)//循环获取标签信息
                    {
                        //更新到货单子表退货数量
                        sql = @"update T_PurchaseArrivalOrder_sub set hreturnqty=isnull(hreturnqty,0)+@inqty where hbillno=@hbillno and hpartcode=@hpartcode";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                hbillno = arra[i]["labcont"][j]["arrivelorder"].ToString(),
                                hpartcode = arra[i]["partcode"].ToString(),
                                hinqty = decimal.Parse(arra[i]["labcont"][j]["qty"].ToString())
                            }
                        });
                        labcode = labcode + arra[i]["labcont"][j]["labcode"].ToString() + ",";
                        suminqty = suminqty + decimal.Parse(arra[i]["labcont"][j]["qty"].ToString());
                        partlabqty = partlabqty + decimal.Parse(arra[i]["labcont"][j]["qty"].ToString());
                        batchno = arra[i]["labcont"][j]["batch"].ToString();
                    }
                    //更新采购订单子表退货数量
                    sql = @"update T_PurchaseOrder_sub set countreturnquantity=isnull(countreturnquantity,0)+@countreturnquantity
                            where purchaseorder_code=@purchaseorder_code and materiel_code=@materiel_code";
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            purchaseorder_code = ordercode,
                            materiel_code = arra[i]["partcode"].ToString(),
                            countreturnquantity = partlabqty
                        }
                    });
                    //写入采购退货单子表
                    sql = @"insert into  T_PurchaseReturnOrder_sub(purchaseorder_code,labcode,rownumber,materiel_code,customercode,quantity,batchno)
                                values(@purchaseorder_code,@labcode,@rownumber,@materiel_code,@customercode,@quantity,@batchno)";
                    labcode = string.Format("'{0}'", labcode.Substring(0, labcode.Length - 1).Replace(",", "','"));
                    list.Add(new
                    {
                        str = sql,
                        parm = new
                        {
                            purchaseorder_code = djwo,
                            labcode = labcode,
                            rownumber = i,
                            materiel_code = arra[i]["partcode"].ToString(),
                            customercode = arra[0]["customercode"].ToString(),
                            quantity = suminqty,
                            batchno = batchno
                        }
                    });
                    //写入库存表前查询当前物料在库存表中是否存在,写入或更新库存表
                    sql = @"select *  from TK_WMS_Stock where materiel_code=@materiel_code";
                    dynamicParams.Add("@materiel_code", arra[i]["partcode"].ToString());
                    var data1 = DapperHelper.selectdata(sql, dynamicParams);
                    if (data1.Rows.Count > 0)
                    {
                        sql = @"update TK_WMS_Stock set qty=qty-@suminqty
                                where materiel_code=@materiel_code";
                        list.Add(new
                        {
                            str = sql,
                            parm = new
                            {
                                materiel_code = arra[i]["partcode"].ToString(),
                                suminqty = suminqty
                            }
                        });
                    }
                    /*写入ERP采购退货单************
                     * ***********************
                     * **********************
                    **********************/
                }
                bool aa = DapperHelper.DoTransaction(list);
                if (aa)
                {
                    mes.code = "200";
                    mes.count = 0;
                    mes.Message = "操作成功!";
                    mes.data = null;
                }
                else
                {
                    mes.code = "300";
                    mes.count = 0;
                    mes.Message = "操作失败!";
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.count = 0;
                mes.Message = e.Message;
                mes.data = null;
            }
            return mes;
        }
        #endregion
    }
}