示例#1
0
        // GET: Bills/Create
        public ActionResult Create()
        {
            ViewBill viewBillCreat = new ViewBill();

            viewBillCreat.bill = new Bill();

            return(View(viewBillCreat));
        }
示例#2
0
 private void btnCashout_Click(object sender, EventArgs e)
 {
     if (lChitiet.Count > 0)
     {
         ViewBill f = new ViewBill();
         f.parent = this;
         f.ShowDialog();
     }
     else
     {
         MessageBox.Show("Giỏ hàng trống không thể tạo đơn !", "Cảnh báo");
     }
 }
示例#3
0
 public ActionResult Create(ViewBill viewBillCreat, string CreatTime)
 {
     try
     {
         if (ModelState.IsValid)
         {
             viewBillCreat.bill.Status        = statusY;
             viewBillCreat.bill.CreatPersonID = userID;
             viewBillCreat.bill.CreatTime     = Convert.ToDateTime(CreatTime);
             string billTypeid = viewBillCreat.typeID;
             viewBillCreat.bill.BillTypeID = Convert.ToInt32(billTypeid);
             db.Bills.Add(viewBillCreat.bill);
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex) {
         Log.Error("BillsController-Create-Post", ex);
         return(View());
     }
     return(View());
 }
示例#4
0
        public void ProcessRequest(HttpContext objHttpContext)
        {
            string strJSON   = new StreamReader(objHttpContext.Request.InputStream).ReadToEnd();
            string strAction = objHttpContext.Request.QueryString["Action"];

            if (strAction.Equals("Create"))
            {
                try
                {
                    ViewBill objViewBill = JsonConvert.DeserializeObject <ViewBill>(strJSON);

                    ApplicationDBContext objApplicationDBContext = new ApplicationDBContext();

                    int nCurrentUserID = SessionManager.GetCurrentUser().UserID;

                    Models.User objUser = objApplicationDBContext
                                          .Users
                                          .Include("Bills")
                                          .Where(x => x.UserID == nCurrentUserID).FirstOrDefault();

                    List <ProductCategory> listProductCategory = objApplicationDBContext
                                                                 .ProductCategories
                                                                 .Where(x => x.UserOwnerId == nCurrentUserID)
                                                                 .ToList();

                    List <BillEntry> listBillEntry = new List <BillEntry>();

                    foreach (ViewProduct objViewProduct in objViewBill.Products)
                    {
                        listBillEntry.Add(new BillEntry()
                        {
                            Category         = listProductCategory.Find(x => x.ProductCategoryID == objViewProduct.ProductCategoryID),
                            Price            = objViewProduct.Price,
                            ProductName      = objViewProduct.ProductName,
                            Quantity         = objViewProduct.Quantity,
                            LastModifiedDate = DateTime.Now
                        });
                    }

                    Bill objBill = new Bill()
                    {
                        PurchaseDate     = objViewBill.PurchaseDate,
                        Entries          = listBillEntry,
                        LastModifiedDate = DateTime.Now
                    };

                    AssignShopToBill(objApplicationDBContext, ref objBill, objViewBill, nCurrentUserID);

                    objUser.Bills.Add(objBill);
                    objApplicationDBContext.SaveChanges();

                    // update cache
                    User objUserSession = SessionManager.GetCurrentUser();
                    objUserSession.Bills.Add(objBill);
                    SessionManager.SetCurrentUser(objUserSession);

                    // nulls to avoid json recursion exception
                    // this data will be not used anyway
                    objBill.Shop.UserOwner = null;
                    for (int i = 0; i < objBill.Entries.Count; i++)
                    {
                        if (objBill.Entries[i].Category != null)
                        {
                            objBill.Entries[i].Category.UserOwner = null;
                        }
                    }

                    objHttpContext.Response.ContentType = "application/json";
                    objHttpContext.Response.Write(JsonConvert.SerializeObject(
                                                      new Response()
                    {
                        Success           = true,
                        Message           = "OK",
                        Bill              = objBill,
                        NewPriceFormatted = objBill.Entries.Sum(x => x.Price).ToString("C", new System.Globalization.CultureInfo("pl-PL")),
                        NewProductsCount  = objBill.Entries.Count,
                        NewShopName       = objBill.Shop.ShopName
                    }));
                }
                catch (Exception ex)
                {
                    objHttpContext.Response.ContentType = "application/json";
                    objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                    {
                        Success = false, Message = ex.ToString()
                    }));
                }
            }
            else if (strAction.Equals("Edit"))
            {
                try
                {
                    ViewBill objViewBill = JsonConvert.DeserializeObject <ViewBill>(strJSON);

                    ApplicationDBContext objApplicationDBContext = new ApplicationDBContext();

                    Bill objBill = objApplicationDBContext
                                   .Bills
                                   .Include("Entries")
                                   .Include("Shop")
                                   .Include("Entries.Category")
                                   .Where(x => x.BillID == objViewBill.BillID)
                                   .FirstOrDefault();

                    if (objBill != null)
                    {
                        int nCurrentUserID = SessionManager.GetCurrentUser().UserID;

                        List <ProductCategory> listProductCategory = objApplicationDBContext
                                                                     .ProductCategories
                                                                     .Where(x => x.UserOwnerId == nCurrentUserID)
                                                                     .ToList();

                        objBill.PurchaseDate     = objViewBill.PurchaseDate;
                        objBill.LastModifiedDate = DateTime.Now;
                        AssignShopToBill(objApplicationDBContext, ref objBill, objViewBill, nCurrentUserID);

                        // remove deleted entries
                        objBill.Entries.RemoveAll(x => !objViewBill.Products.Select(y => y.ProductID).Contains(x.BillEntryID));

                        for (int i = 0; i < objViewBill.Products.Count; i++)
                        {
                            BillEntry objBillEntryToUpdate = objBill.Entries.Find(x => x.BillEntryID == objViewBill.Products[i].ProductID);

                            if (objBillEntryToUpdate == null)
                            {
                                objBill.Entries.Add(new BillEntry()
                                {
                                    Category         = listProductCategory.Find(x => x.ProductCategoryID == objViewBill.Products[i].ProductCategoryID),
                                    ProductName      = objViewBill.Products[i].ProductName,
                                    Quantity         = objViewBill.Products[i].Quantity,
                                    Price            = objViewBill.Products[i].Price,
                                    LastModifiedDate = DateTime.Now
                                });
                            }
                            else
                            {
                                objBillEntryToUpdate.Category         = listProductCategory.Find(x => x.ProductCategoryID == objViewBill.Products[i].ProductCategoryID);
                                objBillEntryToUpdate.ProductName      = objViewBill.Products[i].ProductName;
                                objBillEntryToUpdate.Quantity         = objViewBill.Products[i].Quantity;
                                objBillEntryToUpdate.Price            = objViewBill.Products[i].Price;
                                objBillEntryToUpdate.LastModifiedDate = DateTime.Now;
                            }
                        }

                        objApplicationDBContext.SaveChanges();

                        // update cache
                        User objUser = SessionManager.GetCurrentUser();
                        int  nIndex  = objUser.Bills.FindIndex(x => x.BillID == objBill.BillID);
                        objUser.Bills[nIndex] = objBill;
                        SessionManager.SetCurrentUser(objUser);
                    }

                    // nulls to avoid json recursion exception
                    // this data will be not used anyway
                    objBill.Shop.UserOwner = null;
                    for (int i = 0; i < objBill.Entries.Count; i++)
                    {
                        if (objBill.Entries[i].Category != null)
                        {
                            objBill.Entries[i].Category.UserOwner = null;
                        }
                    }

                    objHttpContext.Response.ContentType = "application/json";
                    objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                    {
                        Success           = true,
                        Message           = "OK",
                        Bill              = objBill,
                        NewPriceFormatted = objBill.Entries.Sum(x => x.Price).ToString("C", new System.Globalization.CultureInfo("pl-PL")),
                        NewProductsCount  = objBill.Entries.Count,
                        NewShopName       = objBill.Shop.ShopName
                    }));
                }
                catch (Exception ex)
                {
                    objHttpContext.Response.ContentType = "application/json";
                    objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                    {
                        Success = false, Message = ex.ToString()
                    }));
                }
            }
            else if (strAction.Equals("Delete"))
            {
                ViewBill objViewBill = JsonConvert.DeserializeObject <ViewBill>(strJSON);

                User objUser = SessionManager.GetCurrentUser();
                int  nIndex  = objUser.Bills.FindIndex(x => x.BillID == objViewBill.BillID);

                objHttpContext.Response.ContentType = "application/json";

                if (nIndex != -1)
                {
                    ApplicationDBContext objApplicationDBContext = new ApplicationDBContext();

                    Bill objBillToDelete = objApplicationDBContext
                                           .Bills
                                           .Include("Entries")
                                           .Where(x => x.BillID == objViewBill.BillID)
                                           .FirstOrDefault();

                    if (objBillToDelete != null)
                    {
                        objApplicationDBContext.BillEntries.RemoveRange(objBillToDelete.Entries);
                        objApplicationDBContext.Bills.Remove(objBillToDelete);
                        objApplicationDBContext.SaveChanges();
                    }

                    objUser.Bills.RemoveAt(nIndex);
                    SessionManager.SetCurrentUser(objUser);

                    objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                    {
                        Success = true
                    }));
                }
                else
                {
                    objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                    {
                        Success = false
                    }));
                }
            }
            else if (strAction.Equals("Get"))
            {
                User objUser = SessionManager.GetCurrentUser();
                int  nBillID = Convert.ToInt32(objHttpContext.Request.QueryString["BillID"]);
                Bill objBill = objUser.Bills.Where(x => x.BillID == nBillID).FirstOrDefault();
                objHttpContext.Response.ContentType = "application/json";

                // nulls to avoid json recursion exception
                // this data will be not used anyway
                objBill.Shop.UserOwner = null;
                for (int i = 0; i < objBill.Entries.Count; i++)
                {
                    if (objBill.Entries[i].Category != null)
                    {
                        objBill.Entries[i].Category.UserOwner = null;
                    }
                }

                objHttpContext.Response.Write(JsonConvert.SerializeObject(new Response()
                {
                    Success = objBill != null,
                    Bill    = objBill
                }
                                                                          ));
            }
        }
示例#5
0
        public void AssignShopToBill(ApplicationDBContext objApplicationDBContext, ref Bill objBill, ViewBill objViewBill, int nCurrentUserID)
        {
            Shop objShop = objApplicationDBContext
                           .Shops
                           .Include("UserOwner")
                           .Where(x => x.UserOwner.UserID == nCurrentUserID &&
                                  x.ShopID == objViewBill.Store.StoreID)
                           .FirstOrDefault();

            if (objShop == null)
            {
                objShop = new Shop()
                {
                    ShopName         = objViewBill.Store.StoreName,
                    UserOwner        = objApplicationDBContext.Users.Where(x => x.UserID == nCurrentUserID).FirstOrDefault(),
                    LastModifiedDate = DateTime.Now
                };

                objApplicationDBContext.Shops.Add(objShop);
                objApplicationDBContext.SaveChanges();
            }

            objBill.Shop = objShop;
        }