public async Task <IActionResult> OnGet(string reqNo)
        {
            Suppliers = _context.Suppliers.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.Name
            }).ToList();
            WarehouseStock = _context.WarehouseStock.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.StockName
            }).ToList();
            AllSuppliers      = _context.Suppliers.AsNoTracking().ToList();
            AllWarehouseStock = _context.WarehouseStock.AsNoTracking().ToList();
            ApplicationUser   = await _userManager.GetUserAsync(User);

            Requisitions     = _context.Requisitions.AsNoTracking().Where(r => r.ReqNo == reqNo).ToList();
            Supplier         = AllSuppliers.FirstOrDefault(s => s.Id == Requisitions.FirstOrDefault().SupplierId);
            SelectedSupplier = Supplier.Name;
            List <Warehouse> wStock = new List <Warehouse>();

            foreach (var req in Requisitions)
            {
                wStock.Add(AllWarehouseStock.FirstOrDefault(w => w.Id == req.StockId));
            }
            WStock = wStock;
            ReqNo  = reqNo;
            return(Page());
        }
        public async Task <IActionResult> OnPostEditReq()
        {
            Suppliers = _context.Suppliers.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.Name
            }).ToList();
            WarehouseStock = _context.WarehouseStock.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.StockName
            }).ToList();
            AllSuppliers      = _context.Suppliers.AsNoTracking().ToList();
            AllWarehouseStock = _context.WarehouseStock.AsNoTracking().ToList();
            ApplicationUser   = await _userManager.GetUserAsync(User);

            Supplier         = AllSuppliers.FirstOrDefault(s => s.Id == Requisitions.FirstOrDefault().SupplierId);
            SelectedSupplier = Supplier.Name;
            List <Warehouse> wStock = new List <Warehouse>();

            foreach (var req in Requisitions)
            {
                wStock.Add(AllWarehouseStock.FirstOrDefault(w => w.Id == req.StockId));
            }
            WStock = wStock;
            if (await _userManager.IsInRoleAsync(ApplicationUser, "Admin"))
            {
                try
                {
                    CreatePDF(AllSuppliers.FirstOrDefault(s => s.Id == Convert.ToInt32(SelectedSupplier)).Email, ReqNo);
                }
                catch (Exception)
                {
                    _logger.LogError("Error in creating PDF");
                }
                Requisitions = _context.Requisitions.AsNoTracking().Where(r => r.ReqNo == ReqNo).ToList();
                foreach (var req in Requisitions)
                {
                    req.Approved     = "Approved";
                    req.ApprovedDate = DateTime.Now;
                    req.Approver     = ApplicationUser.UserName;

                    _context.Requisitions.Update(req);
                }
                _context.SaveChanges();
                TempData["StatusMessage"] = "Requisition created, approved and sent to supplier.";
            }
            else
            {
                TempData["StatusMessage"] = "Requisition created and waiting approval.";
            }
            return(Redirect("~/WarehousePages/RequisitionIndex"));
        }
        public async Task <IActionResult> OnPostAddItems()
        {
            Suppliers = _context.Suppliers.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.Name
            }).ToList();
            WarehouseStock = _context.WarehouseStock.AsNoTracking().Select(n => new SelectListItem
            {
                Value = n.Id.ToString(),
                Text  = n.StockName
            }).ToList();
            AllSuppliers      = _context.Suppliers.AsNoTracking().ToList();
            AllWarehouseStock = _context.WarehouseStock.AsNoTracking().ToList();
            Supplier          = AllSuppliers.FirstOrDefault(s => s.Id == Requisitions.FirstOrDefault().SupplierId);
            SelectedSupplier  = Supplier.Name;
            List <Warehouse> wStock = new List <Warehouse>();

            foreach (var req in Requisitions)
            {
                wStock.Add(AllWarehouseStock.FirstOrDefault(w => w.Id == req.StockId));
            }
            WStock = wStock;
            Warehouse warehouseStock = new Warehouse();

            warehouseStock = _context.WarehouseStock.AsNoTracking().AsNoTracking().FirstOrDefault(s => s.Id == Convert.ToInt32(SelectedStock));

            ApplicationUser = await _userManager.GetUserAsync(User);

            Requisition requisition = new Requisition
            {
                StockId      = warehouseStock.Id,
                Quantity     = Quantity,
                Price        = warehouseStock.Price,
                Total        = warehouseStock.Price * Quantity,
                Requester    = ApplicationUser.UserName,
                ReqDate      = DateTime.Now,
                SupplierId   = Convert.ToInt32(SelectedSupplier),
                ReqNo        = ReqNo,
                Approved     = "Waiting Approval",
                ApprovedDate = DateTime.Now
            };
            await _context.Requisitions.AddAsync(requisition);

            await _context.SaveChangesAsync();

            Requisitions = _context.Requisitions.AsNoTracking().Where(r => r.ReqNo == ReqNo).ToList();
            return(Page());
        }
        public string GetHTMLString(string supplierEmail)
        {
            Requisitions = _context.Requisitions.AsNoTracking().ToList();
            var sb = new StringBuilder();

            sb.AppendFormat(@"
                        <html>
                            <head>
                            </head>
                            <body>
                                <div class='header'><h1>Requisition</h1></div>
                                <table class='table'>
                                    <tr>
                                        <td colspan='2' align='right'>{0}</td>
                                    </tr>            
                                    <tr>            
                                        <td>            
                                            <table class='table'>
                                                <tr>
                                                    <th>Requisitioner Info</th>
                                                </tr>
                                                <tr>
                                                    <th>Name</th>
                                                    <td>{1} {2}</td>
                                                </tr>
                                                <tr>
                                                    <th>Phone</th>
                                                    <td>{3}</td>
                                                </tr>
                                                <tr>
                                                    <th>Email</th>
                                                    <td>{4}</td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td>
                                            <table class='table'>
                                                <tr>
                                                    <th>Supplier Info</th>
                                                </tr>
                                                <tr>
                                                    <th>Name</th>
                                                    <td>{5}</td>
                                                </tr>
                                                <tr>
                                                    <th>Phone</th>
                                                    <td>{6}</td>
                                                </tr>
                                                <tr>
                                                    <th>Address</th>
                                                    <td>{7}</td>
                                                </tr>
                                                <tr>
                                                    <th>Email</th>
                                                    <td>{8}</td>
                                                </tr>
                                                <tr>
                                                    <th>Attention</th>
                                                    <td>{9}</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>                                    
                                    <tr>
                                        <td colspan='2'>
                                            <table class='table'>
                                                <thead>
                                                    <tr>
                                                        <th>Stock Item</th>
                                                        <th>Quantity</th>
                                                        <th>Unit Price</th>
                                                        <th>Total</th>
                                                    </tr>
                                                </thead>
                                                <tbody>", ReqNo, ApplicationUser.FirstName, ApplicationUser.LastName, ApplicationUser.PhoneNumber, ApplicationUser.Email,
                            AllSuppliers.FirstOrDefault(s => s.Email == supplierEmail).Name, AllSuppliers.FirstOrDefault(s => s.Email == supplierEmail).Phone,
                            AllSuppliers.FirstOrDefault(s => s.Email == supplierEmail).Address, AllSuppliers.FirstOrDefault(s => s.Email == supplierEmail).Email, AllSuppliers.FirstOrDefault(s => s.Email == supplierEmail).Contact);

            foreach (var item in Requisitions)
            {
                sb.AppendFormat(@"
                    <tr>
                        <td>{0}</td>
                        <td>{1}</td>
                        <td>{2}</td>
                        <td>{3}</td>
                    </tr>", AllWarehouseStock.FirstOrDefault(w => w.Id == item.StockId).StockName, item.Quantity, item.Price, item.Total);
            }
            sb.Append(@"
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </body>
                        </html>");

            return(sb.ToString());
        }