Esempio n. 1
0
        public IActionResult OnGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            StockToProducts = _context.StockToProducts.ToList();
            Product         = _context.Products.FirstOrDefault(m => m.Product_ID == id);
            Categories      = _context.Categories.Select(n => new SelectListItem
            {
                Value = n.Cat_ID.ToString(),
                Text  = n.CategoryName
            }).ToList();
            AllCategories = _context.Categories.ToList();
            SelectedTag   = AllCategories.FirstOrDefault(c => c.Cat_ID == Product.Cat_ID).Cat_ID.ToString();
            StockItems    = _context.Stock.ToList();

            List <string> stc = new List <string>();
            List <string> ssq = new List <string>();

            for (int stp = 0; stp < StockToProducts.Count; stp++)
            {
                if (StockToProducts[stp].ProductId == Product.Product_ID)
                {
                    stc.Add(StockItems[stp].StockName);
                    string qty = StockToProducts.FirstOrDefault(s => s.Id == StockToProducts[stp].Id).QuantityUse.ToString();
                    ssq.Add(qty);
                }
            }

            StockItemChecked        = stc;
            SelectedStockQuantities = ssq;

            if (Product == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostExport(int currentPage, int pageSize, string sortOrder, string searchString)
        {
            Products = await InitiateView(currentPage, pageSize, sortOrder, searchString);

            string   sWebRootFolder = _hostingEnvironment.WebRootPath;
            string   sFileName      = @"Product Data.xlsx";
            string   URL            = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
            FileInfo file           = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
            var      memory         = new MemoryStream();

            using (var fs = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Create, FileAccess.Write))
            {
                ExcelPackage package = new ExcelPackage();
                package.Workbook.Worksheets.Add("Products");
                ExcelWorksheet productsSheet = package.Workbook.Worksheets["Products"];
                using (var range = productsSheet.Cells["A1:I1"])
                {
                    range.Style.Font.Bold        = true;
                    range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(23, 121, 186));
                    range.Style.Font.Color.SetColor(Color.White);
                }
                productsSheet.Cells[1, 1].Value = "Product Id";
                productsSheet.Cells[1, 2].Value = "Product Name";
                productsSheet.Cells[1, 3].Value = "Product Short Description";
                productsSheet.Cells[1, 4].Value = "Product Detailed Description";
                productsSheet.Cells[1, 5].Value = "Selling Price";
                productsSheet.Cells[1, 6].Value = "Cost Price";
                productsSheet.Cells[1, 7].Value = "Profit Margin";
                productsSheet.Cells[1, 8].Value = "Category";
                productsSheet.Cells[1, 9].Value = "Stock Used";
                for (int r = 0; r < Products.Count(); r++)
                {
                    productsSheet.Cells[r + 2, 1].Value = Products.ToList()[r].Product_ID;
                    productsSheet.Cells[r + 2, 2].Value = Products.ToList()[r].ProductName;
                    productsSheet.Cells[r + 2, 3].Value = Products.ToList()[r].ProductShortDescription;
                    productsSheet.Cells[r + 2, 4].Value = Products.ToList()[r].ProductDetailedDescription;
                    productsSheet.Cells[r + 2, 5].Value = Products.ToList()[r].SellingPrice;
                    productsSheet.Cells[r + 2, 6].Value = Products.ToList()[r].CostPrice;
                    productsSheet.Cells[r + 2, 7].Value = Products.ToList()[r].ProfitMargin;
                    productsSheet.Cells[r + 2, 8].Value = AllCategories.FirstOrDefault(c => c.Cat_ID == Products.ToList()[r].Cat_ID).CategoryName;
                    string stock = string.Empty;
                    StockToProducts = _context.StockToProducts.ToList();
                    foreach (var stp in StockToProducts.Where(s => s.ProductId == Products.ToList()[r].Product_ID))
                    {
                        if (stock == "")
                        {
                            stock += _context.Stock.FirstOrDefault(st => st.Id == stp.StockId).StockName;
                        }
                        else
                        {
                            stock += ", " + _context.Stock.FirstOrDefault(st => st.Id == stp.StockId).StockName;
                        }
                    }
                    productsSheet.Cells[r + 2, 9].Value = stock;  // List All stock for this product
                    productsSheet.Cells[1, 1, 1, 9].AutoFitColumns();
                }

                package.SaveAs(fs);
                package.Dispose();
            }
            using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position           = 0;
            TempData["StatusMessage"] = "Products successfully exported to file " + sFileName + ".";
            return(File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName));
        }
        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"));
        }