예제 #1
0
 protected void btnSetSales_Click(object sender, EventArgs e)
 {
     if (Page.IsPostBack)
     {
         string AccountId           = ddlSalesAccount.SelectedValue;
         Classes.CDefaultAccount cd = new Classes.CDefaultAccount();
         Models.MDefaultAccounts md = new Models.MDefaultAccounts();
         md.DefaultSaleAccountId = AccountId;
         string WareHouseId = Session["WareHouse"].ToString();
         md.WareHouseId = Convert.ToInt32(WareHouseId);
         if (cd.SaveDefaultAccounts(md, "Sale") < 0)
         {
             ShowFailMessage();
         }
         else
         {
             ShowSuccessMessage();
         }
     }
 }
        public int SaveDefaultAccounts(Models.MDefaultAccounts model, string AccountType)
        {
            try
            {
                var query = from o in obj.DefaultCashAccounts
                            where o.WareHouseId == Convert.ToInt32(model.WareHouseId)
                            select o;

                switch (AccountType)
                {
                case "Purchase":
                {
                    if (query.Count() == 0)
                    {
                        DB.DefaultCashAccount df = new DB.DefaultCashAccount();
                        df.DefaultPurchaseAccountId = Convert.ToInt32(model.DefaultPurchaseAccountId);
                        df.DefaultSalesAccountId    = Convert.ToInt32(model.DefaultSaleAccountId);
                        df.WareHouseId = Convert.ToInt32(model.WareHouseId);
                        obj.DefaultCashAccounts.InsertOnSubmit(df);
                    }
                    else
                    {
                        foreach (var item in query)
                        {
                            item.DefaultPurchaseAccountId = Convert.ToInt32(model.DefaultPurchaseAccountId);
                        }
                    }

                    obj.SubmitChanges();

                    return(1);
                }

                case "Sale":
                {
                    if (query.Count() == 0)
                    {
                        DB.DefaultCashAccount df = new DB.DefaultCashAccount();
                        df.DefaultPurchaseAccountId = Convert.ToInt32(model.DefaultPurchaseAccountId);
                        df.DefaultSalesAccountId    = Convert.ToInt32(model.DefaultSaleAccountId);
                        df.WareHouseId = Convert.ToInt32(model.WareHouseId);
                        obj.DefaultCashAccounts.InsertOnSubmit(df);
                    }
                    else
                    {
                        foreach (var item in query)
                        {
                            item.DefaultSalesAccountId = Convert.ToInt32(model.DefaultSaleAccountId);
                        }
                    }

                    obj.SubmitChanges();
                    return(1);
                }

                default:
                {
                    return(-1);
                }
                }
            }
            catch
            {
                return(-1);
            }
        }