public object collectCusOrder(Customer_Order OrderUpdate) { db.Configuration.ProxyCreationEnabled = false; Customer_Order objectOrder = new Customer_Order(); Payment objectP = new Payment(); List <Payment> objectPayment = db.Payments.Include(x => x.Customer_Order).ToList(); dynamic toReturn = new ExpandoObject(); var id = OrderUpdate.CustomerOrderID; try { objectOrder = db.Customer_Order.Where(x => x.CustomerOrderID == id).FirstOrDefault(); objectP = db.Payments.Where(x => x.CustomerOrderID == id).FirstOrDefault(); List <Product_Order_Line> product_Orders = db.Product_Order_Line.Include(x => x.Customer_Order).Include(x => x.Product).Where(x => x.CustomerOrderID == objectOrder.CustomerOrderID).ToList(); if (objectP != null) { List <dynamic> products = new List <dynamic>(); foreach (var prod in product_Orders) { Container_Product container_Product = db.Container_Product.Where(x => x.ContainerID == objectOrder.ContainerID && x.ProductID == prod.ProductID).FirstOrDefault(); if (container_Product.CPQuantity < prod.PLQuantity) { toReturn.Message = "Oops. Not enough stock."; } else { container_Product.CPQuantity = container_Product.CPQuantity - prod.PLQuantity; db.SaveChanges(); } } if (objectOrder.CustomerOrderStatusID == 2) { objectOrder.CustomerOrderStatusID = 3; db.SaveChanges(); toReturn.Message = "The customer order has successfully been collected."; } else { toReturn.Message = "Only 'Fulfilled' customer orders can be collected."; } } else { toReturn.Message = "Order has not been paid for yet."; } } catch (Exception) { toReturn.Message = "Cancellation not successful."; } return(toReturn); }
public object removeSaleProduct(int productID, int saleID, int quantity) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault(); if (product != null) { Sale sale = db.Sales.Where(x => x.SaleID == saleID).FirstOrDefault(); if (sale != null) { Container_Product container_Product = db.Container_Product.Where(x => x.ContainerID == sale.ContainerID && x.ProductID == product.ProductID).FirstOrDefault(); if (container_Product != null) { container_Product.CPQuantity = (container_Product.CPQuantity + quantity); db.SaveChanges(); Product_Sale product_Sale = db.Product_Sale.Where(x => x.ProductID == product.ProductID && x.SaleID == sale.SaleID).FirstOrDefault(); if (product_Sale != null) { db.Product_Sale.Remove(product_Sale); db.SaveChanges(); toReturn.Product_Sale = product_Sale; } } else { toReturn.Error = "Container Not Found"; } } else { toReturn.Error = "Sale Not Found"; } } else { toReturn.Error = "Product Not Found"; } } catch { toReturn.Error = "Product Removal Unsuccessful"; } return(toReturn); }
public object addDonatedProduct(int prodID, int contID, int donID, int quantity) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { //check quantity of container Container_Product conProd = db.Container_Product.Where(z => (z.ProductID == prodID) && (z.ContainerID == contID)).FirstOrDefault(); Donated_Product donProdObj = db.Donated_Product.Where(z => (z.DonationID == donID) && (z.ProductID == prodID)).FirstOrDefault(); //to check if product has already been added if (conProd.CPQuantity >= quantity) { if (donProdObj == null) { Donated_Product donProd = new Donated_Product(); donProd.ProductID = prodID; donProd.DonationID = donID; donProd.DPQuantity = quantity; donProd.ContainerID = contID; db.Donated_Product.Add(donProd); conProd.CPQuantity = conProd.CPQuantity - quantity; db.SaveChanges(); toReturn.Message = "Donated Products Added Successfully"; } else { donProdObj.DPQuantity = donProdObj.DPQuantity + quantity; conProd.CPQuantity = conProd.CPQuantity - quantity; db.SaveChanges(); toReturn.Message = "Donated Products Added Successfully"; } toReturn.DonatedProducts = db.Donated_Product.Where(z => z.DonationID == donID).ToList(); } else { toReturn.Message = "Not enough stock on hand. Available qunatity = " + conProd.CPQuantity; } } catch (Exception) { toReturn.Message = "Failed to add the product"; } return(toReturn); }
public object updateCustomerOrder(int containerID, int supplierOrderID) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); bool fulfilled = false; try { Supplier_Order_Status delivered = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 3).FirstOrDefault(); Supplier_Order suporder = db.Supplier_Order.Where(x => x.SupplierOrderID == supplierOrderID).FirstOrDefault(); if (suporder != null && delivered != null) { suporder.SupplierOrderStatusID = delivered.SupplierOrderStatusID; suporder.Supplier_Order_Status = delivered; db.SaveChanges(); } Customer_Order_Status status = db.Customer_Order_Status.Where(x => x.CustomerOrderStatusID == 2).FirstOrDefault(); List <Customer_Order> orders = db.Customer_Order.Where(x => x.CustomerOrderStatusID == 1 && x.ContainerID == containerID).ToList(); if (orders.Count != 0) { foreach (Customer_Order order in orders) { List <Product_Order_Line> product_Orders = db.Product_Order_Line.Where(x => x.CustomerOrderID == order.CustomerOrderID).ToList(); if (product_Orders.Count != 0) { foreach (Product_Order_Line product in product_Orders) { Container_Product prod = db.Container_Product.Where(x => x.ProductID == product.ProductID && x.ContainerID == containerID).FirstOrDefault(); if (prod != null) { if (product.PLQuantity <= prod.CPQuantity) { fulfilled = true; } else { fulfilled = false; } } } } if (fulfilled == true) { order.CustomerOrderStatusID = status.CustomerOrderStatusID; order.Customer_Order_Status = status; db.SaveChanges(); } } } toReturn.Message = "Stock Received And Recorded Successfuly"; } catch { toReturn.Error = "No Orders Found"; } return(toReturn); }
public object receiveProductStock(int supplierOrderID, int productID, int quantity, int containerID) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.product = new ExpandoObject(); try { //search for the supplier and product in the database // Supplier supplier = db.Suppliers.Where(X => X.SupplierID == supplierID).FirstOrDefault(); Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault(); if (product != null) { //check to see if the supplier order was already created today in the current container Supplier_Order supplier_Order = db.Supplier_Order.Where(x => x.SupplierOrderID == supplierOrderID).FirstOrDefault(); if (supplier_Order != null) { //get Product Supplier_Order_Product prod = db.Supplier_Order_Product.Where(x => x.ProductID == product.ProductID && x.SupplierOrderID == supplier_Order.SupplierOrderID).FirstOrDefault(); if (prod != null) { prod.SOPQuantityRecieved = quantity; db.SaveChanges(); //returning the product so you can see it in the console if you want toReturn.product = db.Supplier_Order_Product.Where(x => x.ProductID == productID && x.SupplierOrderID == supplier_Order.SupplierOrderID).FirstOrDefault(); toReturn.Message = "Product Quantity Saved"; //adjust quantity on hand in container Container con = db.Containers.Where(x => x.ContainerID == containerID).FirstOrDefault(); if (con != null) { Container_Product conProd = db.Container_Product.Where(x => x.ContainerID == con.ContainerID && x.ProductID == productID).FirstOrDefault(); if (conProd != null) { conProd.CPQuantity = conProd.CPQuantity + quantity; db.SaveChanges(); toReturn.Message = "Container's Product Quantity Updated"; } } } else { toReturn.Error = "Product Not Found"; } } } } catch { toReturn.Error = "Receiving Stock Failed"; } return(toReturn); }
public object DeleteContainer(int id) { db.Configuration.ProxyCreationEnabled = false; Container objectContainer = new Container(); dynamic toReturn = new ExpandoObject(); try { objectContainer = db.Containers.Find(id); if (objectContainer == null) { toReturn.Message = "Record Not Found"; } else { User user = db.Users.Where(x => x.ContainerID == id).FirstOrDefault(); Container_Product con = db.Container_Product.Where(x => x.ContainerID == id).FirstOrDefault(); Sale sale = db.Sales.Where(x => x.ContainerID == id).FirstOrDefault(); Customer_Order order = db.Customer_Order.Where(x => x.ContainerID == id).FirstOrDefault(); Supplier_Order suporder = db.Supplier_Order.Where(x => x.ContainerID == id).FirstOrDefault(); Product_Backlog prod = db.Product_Backlog.Where(x => x.ContainerID == id).FirstOrDefault(); if (user == null && con == null && sale == null && order == null && suporder == null && prod == null) { List <Manager> managers = db.Managers.ToList(); if (managers.Count != 0) { foreach (Manager man in managers) { List <Container> containers = man.Containers.ToList(); foreach (Container container in containers) { if (container.ContainerID == id) { objectContainer.InActive = true; db.SaveChanges(); toReturn.Message = "Delete Restricted But Container Set to Inactive"; return(toReturn); } } } } db.Containers.Remove(objectContainer); db.SaveChanges(); toReturn.Message = "Delete Successful"; } else { objectContainer.InActive = true; db.SaveChanges(); toReturn.Message = "Delete Restricted But Container Set to Inactive"; return(toReturn); } } } catch { toReturn.Message = "Delete Unsuccesful"; } return(toReturn); }
public object cancelSale(int saleID) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { //get sale Sale newSale = db.Sales.Where(x => x.SaleID == saleID).FirstOrDefault(); if (newSale != null) { //get container Container container = db.Containers.Where(x => x.ContainerID == newSale.ContainerID).FirstOrDefault(); //get list of products in Sale List <Product_Sale> product_Sales = db.Product_Sale.Where(x => x.SaleID == newSale.SaleID).ToList(); if (container != null) { if (product_Sales != null) { foreach (var prod in product_Sales) { Product product = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault(); if (product != null) { Container_Product container_Product = db.Container_Product.Where(x => x.ContainerID == newSale.ContainerID && x.ProductID == product.ProductID).FirstOrDefault(); if (container_Product != null) { container_Product.CPQuantity = (container_Product.CPQuantity + prod.PSQuantity); db.SaveChanges(); Product_Sale product_Sale = db.Product_Sale.Where(x => x.ProductID == product.ProductID && x.SaleID == newSale.SaleID).FirstOrDefault(); if (product_Sale != null) { db.Product_Sale.Remove(product_Sale); db.SaveChanges(); } } } else { toReturn.Error = "Product Not Found"; } //get list of payment List <Payment> payments = db.Payments.Where(x => x.SaleID == newSale.SaleID).ToList(); if (payments.Count != 0) { foreach (Payment payment in payments) { db.Payments.Remove(payment); db.SaveChanges(); } } toReturn.Message = "Sale Cancelled"; } } else { toReturn.Message = "Cancelling Sale Failed"; } } else { toReturn.Error = "Container Not Found"; } } else { toReturn.Error = "Cancel Failed: Sale Not Found"; } } catch { toReturn.Error = "Sale Cancellation Unsuccessfully Completed"; } return(toReturn); }
public object addSaleProduct(int productID, int saleID, int quantity) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault(); if (product != null) { Sale sale = db.Sales.Where(x => x.SaleID == saleID).FirstOrDefault(); if (sale != null) { Container_Product container_Product = db.Container_Product.Where(x => x.ContainerID == sale.ContainerID && x.ProductID == product.ProductID).FirstOrDefault(); if (container_Product != null) { if (quantity > container_Product.CPQuantity) { toReturn.Error = "Only " + container_Product.CPQuantity + "Of " + product.ProdName + "In Stock"; return(toReturn); } else { container_Product.CPQuantity = (container_Product.CPQuantity - quantity); db.SaveChanges(); Product_Sale product_Sale = db.Product_Sale.Where(x => x.ProductID == product.ProductID && x.SaleID == sale.SaleID).FirstOrDefault(); if (product_Sale == null) { Product_Sale newProduct_Sale = new Product_Sale(); newProduct_Sale.ProductID = product.ProductID; newProduct_Sale.Product = product; newProduct_Sale.SaleID = sale.SaleID; newProduct_Sale.Sale = sale; newProduct_Sale.PSQuantity = quantity; db.Product_Sale.Add(newProduct_Sale); db.SaveChanges(); toReturn.Product_Sale = db.Product_Sale.ToList().LastOrDefault(); } else { product_Sale.PSQuantity = product_Sale.PSQuantity + quantity; db.SaveChanges(); toReturn.Product_Sale = product_Sale; } } } else { toReturn.Error = "Container Not Found"; } } else { toReturn.Error = "Sale Not Found"; } } else { toReturn.Error = "Product Not Found"; } } catch { toReturn.Error = "Product Add Unsuccessful"; } return(toReturn); }