public async Task <ActionResult> NewPurchase(int supplierId) { Supplier supplier = await db.Suppliers.FindAsync(supplierId); var model = new NewPurchaseVM(); model.ProductsDropdown = new SelectList(db.Products .Where(m => m.SupplierId.Equals(supplierId)).ToList(), "Id", "Name"); model.ProductList = await db.Products .Where(m => m.SupplierId.Equals(supplierId)).ToListAsync(); model.SupplierId = supplierId; model.Supplier = supplier.Name; model.StockCart = await db.StockCarts.Where(c => c.StockOrderId.Equals(null) && c.SupplierId.Equals(supplierId)).ToListAsync(); model.TaxTotal = model.StockCart.Select(c => c.VatAmount).Sum(); model.TotalPrice = model.StockCart.Select(c => c.Price).Sum(); model.SubTotal = model.TotalPrice - model.TaxTotal; ViewBag.SupplierId = supplierId; return(View(model)); }
public async Task <ActionResult> Reset(NewPurchaseVM model) { if (!ModelState.IsValid) { model.ProductsDropdown = new SelectList(db.Products.ToList(), "Id", "Name"); return(RedirectToAction("/Pharmacist/NewPurchase?supplierid=" + model.SupplierId)); } var itemsToRemove = await db.StockCarts.Where(c => c.StockOrderId == null).ToListAsync(); db.StockCarts.RemoveRange(itemsToRemove); await db.SaveChangesAsync(); return(RedirectToAction("NewPurchase", new { model.SupplierId })); }
public ActionResult AddToCart(NewPurchaseVM model) { if (ModelState.IsValid) { Product product = db.Products.Find(model.ProductId); float vatTotal = product.SupplierPrice * model.Quantity * TAX_const; // if product is already in list in list, display error if (db.StockCarts.Any(c => c.ProductId.Equals(model.ProductId) && c.StockOrderId.Equals(null))) { TempData["Error"] = "That product is already in cart"; return(Redirect("/Pharmacist/StockOrders/NewPurchase?supplierId=" + model.SupplierId)); } if (model.Quantity <= 0) { TempData["Error"] = "That is an invalid quantity amount"; return(Redirect("/Pharmacist/StockOrders/NewPurchase?supplierId=" + model.SupplierId)); } db.StockCarts.Add(new StockCart { ProductId = model.ProductId, SupplierId = model.SupplierId, VatAmount = vatTotal, Price = product.SupplierPrice * model.Quantity, Quantity = model.Quantity, Product = product }); db.SaveChanges(); return(Redirect("/Pharmacist/StockOrders/NewPurchase?supplierId=" + model.SupplierId)); } TempData["Error"] = "You did not select a product"; return(Redirect("/Pharmacist/StockOrders/NewPurchase?supplierId=" + model.SupplierId)); }
public async Task <ActionResult> CreateOrder(NewPurchaseVM model) { if (ModelState.IsValid) { Supplier supplier = db.Suppliers.Find(model.SupplierId); // Change order status OrderStatus defaultOrderStatus = await db.OrderStatuses.Where(os => os.ProcessNumber == 2) .FirstOrDefaultAsync(); // Cant buy stock if theres no items in the cart if (!db.StockCarts.Any(c => c.SupplierId == model.SupplierId)) { TempData["Error"] = "There is no items in the cart to place order."; return(Redirect("/Pharmacist/StockOrders/NewPurchase?supplierId=" + model.SupplierId)); } db.StockOrders.Add(new StockOrder { SupplierId = model.SupplierId, PaymentPeriod = model.PaymentPeriod, StockOrderDate = DateTime.Now, TotalCost = model.TotalPrice, Notes = model.Notes, SubTotal = model.SubTotal, TotalTax = model.TaxTotal, Supplier = supplier, OrderStatusId = defaultOrderStatus.Id, OrderStatus = defaultOrderStatus }); await db.SaveChangesAsync(); // Assign orderid to stockcart items var stockCart = await db.StockCarts.Where(c => c.StockOrderId.Equals(null)).ToListAsync(); var latestOrder = await db.StockOrders.OrderByDescending(o => o.Id).FirstAsync(); stockCart.ForEach(c => c.StockOrderId = latestOrder.Id); // Order Id Should Equal to the latest order // Add notification db.Notifications.Add(new Notification { CreatedDate = DateTime.Now, Message = "You have placed an order of R" + latestOrder.TotalCost, isRead = false, Icon = "fa-file-alt", BackgroundColorIcon = "bg-info" }); await db.SaveChangesAsync(); var callbackUrl = Url.Action("InvoiceToPdf", "StockOrders", new { area = "pharmacist", id = latestOrder.Id }, protocol: Request.Url.Scheme); //EmailExtensions.SendMail(supplier.Email, "Request For A Stock Order", // string.Format("Good day. <br><br>I would like to request a stock order of R{0}.<br><br>" + // "<a href=\"" + callbackUrl + "\">Review Order Details Here</a><br><br>" + // "<strong>Order Notes: <strong><br>" + // "{2}", latestOrder.TotalCost, latestOrder.Id, latestOrder.Notes)); EmailExtensions.SendSms(supplier.ContactNumber, string.Format("From Dr J Govender Practice: Good day. I would like to request a stock order of R{0}. " + "Order Notes: {1}", latestOrder.TotalCost, latestOrder.Notes)); return(RedirectToAction("Index")); } return(View(model)); }