Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            StockToProducts = _context.StockToProducts.ToList();
            //Product = _context.Products.FirstOrDefault(m => m.Product_ID == Product.Product_ID);
            AllCategories = _context.Categories.ToList();
            StockItems    = _context.Stock.ToList();
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            try
            {
                string imageurl = Product.ImageUrl;
                if (UploadImage != null)
                {
                    var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "img", UploadImage.FileName);
                    using (var fileStream = new FileStream(fileName, FileMode.Create))
                    {
                        await UploadImage.CopyToAsync(fileStream);
                    }
                    Product.ImageUrl = "\\img\\" + UploadImage.FileName; // Set the file name
                }
                else
                {
                    Product.ImageUrl = imageurl;
                }
                Product.Cat_ID = Convert.ToInt32(SelectedTag);
                decimal stockPrice = 0;
                if (StockItemChecked != null || StockItemChecked.Count > 0)
                {
                    List <decimal> ssqNums = new List <decimal>();
                    foreach (string ssq in SelectedStockQuantities)
                    {
                        if (ssq != null)
                        {
                            decimal quan = 0m;
                            quan = decimal.Parse(ssq, CultureInfo.InvariantCulture);
                            ssqNums.Add(quan);
                        }
                    }
                    for (int s = 0; s < StockItemChecked.Count; s++)
                    {
                        StockItem stock = new StockItem();
                        stock = StockItems.FirstOrDefault(st => st.StockName == StockItemChecked[s]);
                        decimal qty = ssqNums[s];
                        decimal productStockPrice = stock.Price * qty;
                        stockPrice = stockPrice + productStockPrice;
                        StockToProduct stp = new StockToProduct();
                        stp = _context.StockToProducts.FirstOrDefault(st => st.StockId == stock.Id && st.ProductId == Product.Product_ID);
                        if (stp == null)
                        {
                            StockToProduct newStockToProduct = new StockToProduct
                            {
                                StockId     = stock.Id,
                                ProductId   = Product.Product_ID,
                                QuantityUse = ssqNums[s]
                            };
                            _context.StockToProducts.Add(newStockToProduct);
                            await _context.SaveChangesAsync();
                        }
                        else
                        {
                            stp.QuantityUse = ssqNums[s];
                            _context.StockToProducts.Update(stp);
                            await _context.SaveChangesAsync();
                        }
                    }
                }
                Product.CostPrice    = stockPrice;
                Product.ProfitMargin = Product.SellingPrice - stockPrice;
                _context.Products.Update(Product);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(Product.Product_ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            TempData["StatusMessage"] = "Product " + Product.ProductName + " successfully edited.";

            return(RedirectToPage("./ProductIndex"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            Categories = _context.Categories.Select(n => new SelectListItem
            {
                Value = n.Cat_ID.ToString(),
                Text  = n.CategoryName
            }).ToList();
            StockItems = _context.Stock.ToList();

            //if (!ModelState.IsValid)
            //{
            //    return Page();
            //}

            if (UploadImage != null)
            {
                var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "img", UploadImage.FileName);
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    await UploadImage.CopyToAsync(fileStream);
                }
                Product.ImageUrl = "\\img\\" + UploadImage.FileName; // Set the file name
            }
            Product.Cat_ID = Convert.ToInt32(SelectedTag);
            decimal stockPrice = 0;

            if (StockItemChecked != null)
            {
                List <decimal> ssqNums = new List <decimal>();
                foreach (string ssq in SelectedStockQuantities)
                {
                    if (ssq != null)
                    {
                        decimal quan = 0m;
                        quan = decimal.Parse(ssq, CultureInfo.InvariantCulture);
                        ssqNums.Add(quan);
                    }
                }
                for (int s = 0; s < StockItemChecked.Count; s++)
                {
                    StockItem stock = new StockItem();
                    stock = StockItems.FirstOrDefault(st => st.StockName == StockItemChecked[s]);
                    decimal productStockPrice = stock.Price * ssqNums[s];
                    stockPrice = stockPrice + productStockPrice;

                    StockToProduct newStockToProduct = new StockToProduct
                    {
                        StockId     = stock.Id,
                        ProductId   = Product.Product_ID,
                        QuantityUse = ssqNums[s]
                    };
                    _context.StockToProducts.Add(newStockToProduct);
                    await _context.SaveChangesAsync();
                }
            }
            Product.CostPrice    = stockPrice;
            Product.ProfitMargin = Product.SellingPrice - stockPrice;
            _context.Products.Update(Product);
            await _context.SaveChangesAsync();

            TempData["StatusMessage"] = "Product " + Product.ProductName + " successfully created.";
            return(RedirectToPage("./ProductIndex"));
        }
Exemplo n.º 3
0
        public IActionResult OnGetCompleteOrder(int id)
        {
            Order lastOrder = _context.Orders.FirstOrDefault(o => o.OrderId == id);

            StockItems      = _context.Stock.ToList();
            OrderItemss     = _context.OrderItems.Where(oi => oi.OrderId == id).ToList();
            AllProducts     = _context.Products.ToList();
            StockToProducts = _context.StockToProducts.ToList();
            DbContextOptions <ApplicationDbContext> options = new DbContextOptions <ApplicationDbContext>();

            using (var context = new ApplicationDbContext(options))
            {
                Order currentOrder = new Order
                {
                    Id                = lastOrder.Id,
                    OrderId           = lastOrder.OrderId,
                    StoreId           = 1,
                    CustomerId        = 1,
                    OrderStatus       = "Completed",
                    PaymentStatus     = "Paid",
                    PaymentMethod     = PaymentMethod,
                    OrderDiscount     = 0,
                    OrderTotal        = lastOrder.OrderTotal,
                    OrderDate         = lastOrder.OrderDate,
                    Paid              = lastOrder.Paid,
                    Change            = lastOrder.Change,
                    VatTotal          = lastOrder.VatTotal,
                    GrandTotal        = lastOrder.GrandTotal,
                    PreparationStatus = "Incomplete"
                };

                context.Orders.Update(currentOrder);
                context.SaveChanges();

                List <StockItem> updateStockItems = new List <StockItem>();
                Dictionary <StockToProduct, int> updateStockToProducts = new Dictionary <StockToProduct, int>();
                foreach (OrderItems orderItem in OrderItemss)
                {
                    StockToProduct stp = new StockToProduct();
                    stp = StockToProducts.FirstOrDefault(s => s.ProductId == orderItem.Product_ID);
                    if (updateStockToProducts.ContainsKey(stp))
                    {
                        updateStockToProducts[stp] = orderItem.Quantity;
                    }
                    else
                    {
                        updateStockToProducts.Add(stp, orderItem.Quantity);
                    }
                }
                foreach (KeyValuePair <StockToProduct, int> keyValuePair in updateStockToProducts)
                {
                    StockItem stock = new StockItem();
                    stock = StockItems.FirstOrDefault(si => si.Id == keyValuePair.Key.StockId);
                    decimal minusStock = keyValuePair.Key.QuantityUse * keyValuePair.Value;
                    stock.InStock = stock.InStock - minusStock;
                    updateStockItems.Add(stock);
                }
                _context.Stock.UpdateRange(updateStockItems);
                _context.SaveChanges();
            }

            return(RedirectToPage("/POS"));
        }