Beispiel #1
0
        public int UpdateCart(int IDBill, int productID, int quantity)
        {
            Product p      = productRepo.GetProduct(productID);
            int     result = 0;

            try
            {
                result = CheckProductIsAvailableWithQuantity(p, quantity);
                if (result == 1)
                {
                    ProductsInBill prodInBill = productsInBillRepo.GetDetailsAProductInBill(IDBill, productID);
                    prodInBill.Quantity = quantity;
                    prodInBill.Total    = quantity * prodInBill.Price;
                    productsInBillRepo.UpdateProductInBill(prodInBill);
                    UpdateBillDetails(IDBill);
                    LoadCartDetailsModel(IDBill);
                }
            }
            catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at UpdateCart: " + ex.ToString());
                result = -1;
            }
            return(result);
        }
Beispiel #2
0
        private bool CheckCartIsChangeInStock(CartDetailsModel cart)
        {
            bool result = false;

            List <ProductInCartDetailsModel> productsInCart = cart.ProductsInCart;

            foreach (ProductInCartDetailsModel p in productsInCart)
            {
                ProductsInBill prodInBill = new ProductsInBill();
                prodInBill.BillID      = cart.BillID;
                prodInBill.ProductID   = p.ProductID;
                prodInBill.ProductName = productRepo.GetProductName(p.ProductID);
                prodInBill.Quantity    = p.Quantity;
                if (p.Price != productRepo.GetUnitPrice(p.ProductID))
                {
                    // double oldPrice = p.Price;
                    prodInBill.Price = productRepo.GetUnitPrice(p.ProductID);
                    p.Price          = prodInBill.Price;
                    prodInBill.Total = p.Quantity * prodInBill.Price;
                    p.Total          = prodInBill.Total;
                }
                else
                {
                    prodInBill.Price = p.Price;
                    prodInBill.Total = p.Total;
                }
                result = productsInBillRepo.UpdateProductInBill(prodInBill);
            }
            UpdateBillDetails(cart.BillID);

            return(result);
        }
        public bool RemoveAProductInBill(int idBill, int idProduct)
        {
            ProductsInBill prod = ProductsInBills.FirstOrDefault(p => p.BillID == idBill && p.ProductID == idProduct);

            if (prod != null)
            {
                context.ProductsInBills.Remove(prod);
                context.SaveChanges();
            }
            return(true);
        }
Beispiel #4
0
        public ActionResult AddToCart(int productID, int quantity = 1)
        {
            CartDetailsModel cartDetailsModel = null;
            Bill             userBill;

            try
            {
                Product product  = GetProduct(productID);
                string  username = HttpContext.User.Identity.Name;
                userBill = billRepo.GetUsersLastBillIsNotPay(username);
                if (userBill == null)
                {
                    userBill = CreateBillUser(username);
                }
                int totalQuantity = quantity;
                if (productsInBillRepo.CheckBillContainProduct(userBill.BillID, productID))
                {
                    totalQuantity = totalQuantity + productsInBillRepo.GetQuantityAProductInBill(userBill.BillID, productID);
                }
                int statusProdInStock = CheckProductIsAvailableWithQuantity(product, totalQuantity);
                if (statusProdInStock == 1)
                {
                    ProductsInBill productsInBill = new ProductsInBill();
                    productsInBill.BillID      = userBill.BillID;
                    productsInBill.ProductID   = productID;
                    productsInBill.ProductName = product.ProductName;
                    productsInBill.Quantity    = quantity;
                    productsInBill.Price       = product.Price;
                    productsInBill.Total       = product.Price * quantity;
                    bool result = productsInBillRepo.InsertProductToBill(productsInBill);
                    if (result)
                    {
                        UpdateBillDetails(userBill.BillID);
                        TempData["MessageAddToCart"] = "Add to cart success!!";
                    }
                    else
                    {
                        TempData["MessageAddToCart"] = "Error to Add to cart!!";
                    }
                }
                else
                {
                    TempData["ErrorAddToCart"] = "Error to Add to cart!!";
                }
                cartDetailsModel = LoadCartDetailsModel(userBill.BillID);
            }catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at AddToCart: " + ex.ToString());
            }
            return(Json(cartDetailsModel, JsonRequestBehavior.AllowGet));
        }
 public bool InsertProductToBill(ProductsInBill prod)
 {
     if (CheckBillContainProduct(prod.BillID, prod.ProductID))
     {
         ProductsInBill productsInBill = GetDetailsAProductInBill(prod.BillID, prod.ProductID);
         int            newQuantity    = GetQuantityAProductInBill(prod.BillID, prod.ProductID) + prod.Quantity;
         productsInBill.Quantity = newQuantity;
         productsInBill.Total    = newQuantity * GetUnitPriceAProductInBill(prod.BillID, prod.ProductID);
     }
     else
     {
         context.ProductsInBills.Add(prod);
     }
     context.SaveChanges();
     return(true);
 }
        public bool UpdateProductInBill(ProductsInBill pr)
        {
            bool result = false;

            if (ProductsInBills.Any())
            {
                ProductsInBill prod = ProductsInBills.FirstOrDefault(p => p.BillID == pr.BillID && p.ProductID == pr.ProductID);
                if (prod != null)
                {
                    prod.ProductName = pr.ProductName;
                    prod.Quantity    = pr.Quantity;
                    prod.Price       = pr.Price;
                    prod.Total       = pr.Total;
                    context.SaveChanges();
                }
            }
            return(result);
        }