Skip to content
UpdateKuCun.aspx.cs 16 KiB
Newer Older
Jack Dan's avatar
Jack Dan committed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Sqcy.Model.Model;
using Sqcy.Service.Impl;
using Sqcy.Service;
using Hxj.Data;
using System.Data.SqlClient;
using Sqcy.Page;
public partial class UpdateKuCun : MyPage
{
    Kenanfans.KDO.SqlDataBaseOperator _Sdbo = null;
    private ServiceGoods sg = ServiceHelper.GetGoodsService();
    private ServiceGoodsType st = ServiceHelper.GetGoodsTypeService();
    protected void Page_Load(object sender, EventArgs e)
    {
        _Sdbo = DataService.Sdbo;
        if (!IsPostBack)
        {
            string goodsid = Request.QueryString["GoodsID"].ToString();

            DataTable dt = sg.GetGoods(goodsid);
            MdlGoodsType mdl1 = new MdlGoodsType();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    mdl1 = st.GetGoodsType(dt.Rows[i]["SmallTypeID"].ToString());
                }
                
            }
          //  string xgkucun = this.txtNum.Value;//修改后的库存
            string pandianriKC = Request.QueryString["PandianriKC"].ToString();//盘点日库存
            if (mdl1 != null)
            {
                if (mdl1.LossRate.ToString() != "" && Convert.ToDecimal(pandianriKC) != 0)
                {
                    //decimal num = (((Convert.ToDecimal(xgkucun) - Convert.ToDecimal(pandianriKC)) / Convert.ToDecimal(pandianriKC)) * 100) - Convert.ToDecimal(mdl1.LossRate);
                    hfUserType.Value = pandianriKC;
                    hfUserType1.Value = mdl1.LossRate.ToString();
                    
                }
            }                     

        }
        
        String act = Request.QueryString["Action"];
        String msg = "";
        JSONObject jo = new JSONObject();
        jo.Add("Action", act);
        string strAction = Action.Value;

        if (strAction == "save")
        {
            string goodsid = Request.QueryString["GoodsID"].ToString();
            string deliveryID = Request.QueryString["DeliveryID"].ToString();
            string pandianDate = Request.QueryString["PandianDate"].ToString();//盘点日期
            string xgkucun = this.txtNum.Value;//修改后的库存
            string pandianriKC = Request.QueryString["PandianriKC"].ToString();//盘点日库存

            //decimal kcsl = Convert.ToDecimal(GetGoodsKC(goodsid, pandianDate, deliveryID));//获取到选中盘点日期该商品的库存总量
            decimal kcsl = Convert.ToDecimal(pandianriKC);//获取到选中盘点日期该商品的库存总量
            //变动数量
            decimal bdsl;
            bdsl = kcsl - Convert.ToDecimal(xgkucun);



            SqlConnection conn = _Sdbo.GetConnection();
            conn.Open();
            //SqlTransaction st = conn.BeginTransaction();
            try
            {
                SqlCommand cmd = conn.CreateCommand();
                //cmd.Transaction = st;
                cmd.CommandType = CommandType.Text;

                String sql1 = "";

                decimal left = bdsl;

                if (left > 0)//领料(当明细中的库存总量大于要修改为的库存数量时如:100 => 80 则说明要从总的明细中 领掉20  属于 领料行为)
                {


                    //保存领料信息
                    Guid InventoryID = Guid.NewGuid();

                    string date = DateTime.Now.ToString("yyyyMMdd");

                    sql1 = String.Format("INSERT INTO Warehouse.Inventory VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',{11},'{12}')", InventoryID, MySession.UserGroupID, MySession.UserID, StorageType.OUT, DateTime.Now, "库存调整", Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), "", date, deliveryID);
                    cmd.CommandText = sql1;
                    int i1 = cmd.ExecuteNonQuery();

                    decimal totalPrice = 0;


                    //***********************找出该配送点该商品库存数量List*******************************//
                    string sqlkc1 = @"select a.InventoryID,InvertoryLast,InvertoryPrice from  Warehouse.Inventory a left join Warehouse.InventoryDetail b on a.InventoryID=b.InventoryID
                                        where GoodsID='{0}' and DeliveryID='{1}' and InvertoryLast>0 and [Type]='入库' order by  InventoryTime asc";//按照时间正序,查出入库的产品及其明细,返回不同时期产品对应的库存及其单价
                    DataTable dt = _Sdbo.ExecuteDataTable(sqlkc1, goodsid, deliveryID);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (left > Convert.ToDecimal(dt.Rows[i][1]))//当领料数量大于第一条入库记录的库存数量时,则按照此条记录中产品的报价计算
                        {
                            left = left - Convert.ToDecimal(dt.Rows[i][1]);//计算出剩余要领料的数量
                            totalPrice = totalPrice + Convert.ToDecimal(dt.Rows[i][1]) * Convert.ToDecimal(dt.Rows[i][2]);
                            sql1 = String.Format("update Warehouse.InventoryDetail set InvertoryLast='{0}' where InventoryID='{1}' and GoodsID='{2}'", 0, dt.Rows[i][0], goodsid);
                            cmd.CommandText = sql1;
                            int j = cmd.ExecuteNonQuery();
                        }
                        else
                        {
                            totalPrice = totalPrice + Convert.ToDecimal(dt.Rows[i][2]) * left;//剩余的数量按照对应的第‘i’条入库记录 的价格 计算
                            sql1 = String.Format("update Warehouse.InventoryDetail set InvertoryLast='{0}' where InventoryID='{1}' and GoodsID='{2}'", Convert.ToDecimal(dt.Rows[i][1]) - left, dt.Rows[i][0], goodsid);
                            cmd.CommandText = sql1;
                            int t = cmd.ExecuteNonQuery();
                            break;
                        }
                    }

                    //领料时产生一条领料明细记录插入明细表中,其中明细中的InventoryPrice 为总价(即:数量*单价 的最后总和)
                    sql1 = String.Format("INSERT INTO Warehouse.InventoryDetail VALUES ('{0}','{1}',{2},'{3}','{4}',{5},'{6}')", InventoryID, goodsid, left, DateTime.Now, totalPrice, 0, 0);
                    cmd.CommandText = sql1;
                    int j1 = cmd.ExecuteNonQuery();

                    //修改库存
                    //库存中减去领料数量
                    //***********************找出该配送点该商品库存明细中入库的总库存量*******************************//
                    string sqlkc = @"select sum(InvertoryLast) as ZongLast  from  Warehouse.Inventory a left join Warehouse.InventoryDetail b on a.InventoryID=b.InventoryID
                                        where GoodsID='{0}' and DeliveryID='{1}' and InvertoryLast>0 and [Type]='入库'";
                    DataRow dr = _Sdbo.ExecuteDataRow(sqlkc, goodsid, deliveryID);
                    Decimal il;
                    if (dr != null && dr[0].ToString() != "")
                        il = Convert.ToDecimal(dr[0].ToString());
                    else
                        il = 0;
                    Decimal xgkc = Convert.ToDecimal(xgkucun);

                    DataRow dr1 = _Sdbo.ExecuteDataRow("select * from dbo.StorageInfo where GoodsID='{0}' and DeliveryID='{1}' ", goodsid, deliveryID);
                    if (dr1 != null)
                    {
                        sql1 = String.Format("update dbo.StorageInfo set ACount={0} where GoodsID='{1}' and DeliveryID='{2}' ", il, goodsid, deliveryID);
                        cmd.CommandText = sql1;
                        int t1 = cmd.ExecuteNonQuery();
                    }


                    jo.Add("status", "success");

                    decimal xghzkc = 0;//修改后库存总量
                    sql1 = "select ACount from dbo.StorageInfo where GoodsID='{0}' and DeliveryID='{1}' ";
                    DataRow dr2 = _Sdbo.ExecuteDataRow(sql1, goodsid, deliveryID);//修改后查出总库存和 盘点日库存
                    if (dr2 != null && dr2["ACount"].ToString() != "")
                    {
                        xghzkc = Convert.ToDecimal(dr2["ACount"]);
                    }

                    //往调整日志表里查数据
                    sql1 = "INSERT INTO dbo.KCTZDetail VALUES ('{0}','{1}','{2}',{3},{4},{5},'{6}','{7}','{8}')";
                    _Sdbo.ExecuteNonQuery(sql1, Guid.NewGuid().ToString(), MySession.UserID, DateTime.Now, kcsl, xgkucun, Convert.ToDecimal(xgkucun) - kcsl, pandianDate,this.txtMemo.Value,InventoryID);
                    string price = GetGoodsZJE(goodsid, deliveryID);
                    msg = goodsid + "@" + xghzkc + "/" + kcsl + '$' + price;//返回变动数值 格式为 goodsid@数值
                    jo.Add("msg", msg);
                    //Response.Write(JSONConvert.SerializeObject(jo));
                    ClientScript.RegisterStartupScript(this.GetType(), "保存成功!", "<script language='javascript'>window.parent.CBClose('" + goodsid + "','" + xgkucun + "');</script>");
                }
                else if (left < 0)//入库:(当明细中的库存总量小于要修改为的库存数量时如:80  => 100 则说明要向总的明细中 入库20 属于 入库行为) 
                {
                    left = -left;

                    //分两种情况:判断是否为特殊采购:
                    //                    
                    //                   
                    string sqlprice = @"select Price  from Supplier.PriceSheet  A inner join Supplier.PriceSheetDetail B on  A.PriceSheetID=B.PriceSheetID  
                                where GoodsID='{0}' order by EndTime desc";
                    DataRow dt = _Sdbo.ExecuteDataRow(sqlprice, goodsid);
                    Guid InventoryID = Guid.NewGuid();
                    string date = DateTime.Now.ToString("yyyyMMdd");
                    Decimal dc = 0;
                    if (dt == null)//如果报价表与报价明细表中该商品的记录为空 则为特殊采购的商品
                    {
                        string sqlts = @"select InvertoryPrice from  Warehouse.Inventory a left join Warehouse.InventoryDetail b on a.InventoryID=b.InventoryID
                                       where GoodsID='{0}' and DeliveryID='{1}' and [Type]='入库' order by  InventoryTime desc";//按照时间正序,查出入库的产品及其明细,返回不同时期产品对应的库存及其单价
                        DataRow dt1 = _Sdbo.ExecuteDataRow(sqlts, goodsid, deliveryID);
                        dc = Convert.ToDecimal(dt1[0].ToString());
                    }
                    else
                    {
                        dc = Convert.ToDecimal(dt[0].ToString());

                    }

                    //入库时产生一条入库记录插入明细表及产品表中,其中InvertoryPrice 为最近一次的最新报价(即:dc)
                    sql1 = String.Format("INSERT INTO Warehouse.Inventory VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')", InventoryID, MySession.UserGroupID, MySession.UserID, StorageType.IN, DateTime.Now, "库存调整", Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), "", date, deliveryID);
                    cmd.CommandText = sql1;
                    int i1 = cmd.ExecuteNonQuery();
                    sql1 = String.Format("INSERT INTO Warehouse.InventoryDetail VALUES ('{0}','{1}','{2}','{3}','{4}',{5},'{6}')", InventoryID, goodsid, left, DateTime.Now, dc, 0, left);
                    cmd.CommandText = sql1;
                    int j1 = cmd.ExecuteNonQuery();




                    //***********************找出该配送点该商品库存明细中入库的总库存量*******************************//
                    string sqlkc = @"select sum(InvertoryLast) as ZongLast  from  Warehouse.Inventory a left join Warehouse.InventoryDetail b on a.InventoryID=b.InventoryID
                                where GoodsID='{0}' and DeliveryID='{1}' and InvertoryLast>0 and [Type]='入库'";
                    DataRow dr = _Sdbo.ExecuteDataRow(sqlkc, goodsid, deliveryID);
                    Decimal il;
                    if (dr != null && dr[0].ToString() != "")
                        il = Convert.ToDecimal(dr[0].ToString());
                    else
                        il = 0;
                    Decimal xgkc = Convert.ToDecimal(xgkucun);

                    //修改总库存
                    DataRow dr1 = _Sdbo.ExecuteDataRow("select * from dbo.StorageInfo where GoodsID='{0}' and DeliveryID='{1}' ", goodsid, deliveryID);
                    if (dr1 != null)
                    {
                        sql1 = String.Format("update dbo.StorageInfo set ACount={0} where GoodsID='{1}' and DeliveryID='{2}' ", il, goodsid, deliveryID);
                        cmd.CommandText = sql1;
                        int t1 = cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        sql1 = String.Format("insert into  dbo.StorageInfo values('{0}','{1}','{2}','{3}') ", goodsid, deliveryID, il - bdsl, null);
                        cmd.CommandText = sql1;
                        int t1 = cmd.ExecuteNonQuery();
                    }

                    jo.Add("status", "success");

                    decimal xghzkc = 0;//修改后库存总量
                    sql1 = "select ACount from dbo.StorageInfo where GoodsID='{0}' and DeliveryID='{1}' ";
                    DataRow dr2 = _Sdbo.ExecuteDataRow(sql1, goodsid, deliveryID);//修改后查出总库存和 盘点日库存
                    if (dr2 != null && dr2["ACount"].ToString() != "")
                    {
                        xghzkc = Convert.ToDecimal(dr2["ACount"]);
                    }

                    //往调整日志表里查数据
                    sql1 = "INSERT INTO dbo.KCTZDetail VALUES ('{0}','{1}','{2}',{3},{4},{5},'{6}','{7}','{8}')";
                    _Sdbo.ExecuteNonQuery(sql1, Guid.NewGuid().ToString(), MySession.UserID, DateTime.Now, kcsl, xgkucun, Convert.ToDecimal(xgkucun) - kcsl, pandianDate, this.txtMemo.Value, InventoryID);
                    string price = GetGoodsZJE(goodsid, deliveryID);
                    msg = goodsid + "@" + xghzkc + "/" + kcsl + "$" + price;//返回变动数值 格式为 goodsid@数值
                    jo.Add("msg", msg);
                    //Response.Write(JSONConvert.SerializeObject(jo));
                    ClientScript.RegisterStartupScript(this.GetType(), "保存成功!", "<script language='javascript'>window.parent.CBClose('" + goodsid + "','" + xgkucun + "');</script>");
                }
                else
                {
                    jo.Add("status", "falie");
                    msg = "请重新输入,要修改的库存数不能与原库存数重复!";
                    jo.Add("msg", msg);
                    Response.Write(JSONConvert.SerializeObject(jo));
                   
                }

            }
            catch { }

        }


    }
    /// <summary>
    /// 获取 总金额
    /// </summary>
    /// <param name="goodsID"></param>
    /// <param name="deliveryID"></param>
    /// <param name="totalLast"></param>
    /// <returns></returns>
    private string GetGoodsZJE(string goodsID, string deliveryID)
    {
        decimal Price = 0;
        string sql = @"select a.InventoryID,InvertoryLast*InvertoryPrice as Price from Warehouse.InventoryDetail a left join Warehouse.Inventory b on a.InventoryID=b.InventoryID
                        where GoodsID='{0}' and DeliveryID='{1}' and Type='入库' and InvertoryLast>0 order by  InventoryTime desc ";
        DataTable dt = _Sdbo.ExecuteDataTable(sql, goodsID, deliveryID);
        foreach (DataRow dr in dt.Rows)
        {
            Price += Convert.ToDecimal(dr["Price"]);
        }

        return String.Format("{0:F}", Price);
    }
}