public object sendEmail(int supplierOrderID) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { Supplier_Order order = db.Supplier_Order.Where(z => z.SupplierOrderID == supplierOrderID).FirstOrDefault(); if (order != null) { Supplier supplier = db.Suppliers.Where(x => x.SupplierID == order.SupplierID).FirstOrDefault(); if (supplier != null) { List <Supplier_Order_Product> products = db.Supplier_Order_Product.Where(x => x.SupplierOrderID == supplierOrderID).ToList(); if (products.Count != 0) { using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress("*****@*****.**"); mail.To.Add(supplier.SupEmail); mail.Subject = "Ordra Products Order"; mail.Body = PrepareHtmlContent(products); mail.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)) { toReturn.Message = "Supplier Order Email Sent"; smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Ordra@444"); smtp.EnableSsl = true; smtp.Send(mail); } } } else { toReturn.Error = "No Products In Order"; } } else { toReturn.Error = "Supplier Not Found"; } } else { toReturn.Error = "Supplier Order Not Found"; } } catch { toReturn.Error = "Mail unsuccessfully sent"; } return(toReturn); }
// PUT: api/SupplierOrder/5 public string Put(int id, HttpRequestMessage value) { try { Supplier_Order so = new Supplier_Order(); so = (from p in db.Supplier_Order where p.Supplier_Order_ID == id select p).First(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); so.Supplier_Order_Status_ID = (int)json["Supplier_Order_Status_ID"]; if (so.Supplier_Order_Status_ID == 7) { int rawCount = (from t in db.Supplier_Order_Detail_Raw_Material where t.Supplier_Order_ID == id && t.Quantity_Received > 0 select t).Count(); int compCount = (from t in db.Supplier_Order_Component where t.Supplier_Order_ID == id && t.Quantity_Received > 0 select t).Count(); int partCount = (from t in db.Supplier_Order_Detail_Part where t.Supplier_Order_ID == id && t.Quantity_Received > 0 select t).Count(); if (rawCount > 0 || compCount > 0 || partCount > 0) { return("false|The Purchase Order has already received some of its items, thus it cannot be cancelled."); } else { db.SaveChanges(); } } else { db.SaveChanges(); } return("true|Supplier Order successfully updated."); } catch (Exception e) { ExceptionLog.LogException(e, "SupplierOrderController PUT"); return("false|An error has occured updating the Supplier Order on the system."); } }
public object deleteSupplier(int id) { db.Configuration.ProxyCreationEnabled = false; Supplier objectSupplier = new Supplier(); dynamic toReturn = new ExpandoObject(); try { objectSupplier = db.Suppliers.Find(id); if (objectSupplier == null) { toReturn.Message = "Supplier Record Not Found"; } else { Supplier_Order order = db.Supplier_Order.Where(x => x.SupplierID == id).FirstOrDefault(); Product product = db.Products.Where(x => x.SupplierID == id).FirstOrDefault(); if (product == null || order == null) { db.Suppliers.Remove(objectSupplier); db.SaveChanges(); toReturn.Message = "Delete Successful"; } else { toReturn.Message = "Delete Restricted"; } } } catch { toReturn.Message = "Delete Resticted"; } return(toReturn); }
// POST: api/ReturnSupplier/5 public string Put(int id, HttpRequestMessage value) { try { string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JArray returnDetails = (JArray)json["sr"]; string action = (string)json["action"]; int key = db.Supplier_Return.Count() == 0 ? 1 : (from t in db.Supplier_Return orderby t.Supplier_Return_ID descending select t.Supplier_Return_ID).First() + 1; Supplier_Return sr = new Supplier_Return(); sr.Supplier_Order_ID = id; sr.Supplier_Return_ID = key; sr.Invoice_Number = (string)json["Invoice_Number"]; sr.Delivery_Note_Number = (string)json["Delivery_Note_Number"]; sr.Comment = (string)json["Comment"]; sr.Date_of_Return = (DateTime)json["Date_of_Return"]; int item_key = db.Supplier_Return_Item.Count() == 0 ? 1 : (from t in db.Supplier_Return_Item orderby t.Return_Item_ID descending select t.Return_Item_ID).First() + 1; foreach (JObject ret in returnDetails) { Supplier_Return_Item sri = new Supplier_Return_Item(); sri.Return_Item_ID = item_key; item_key++; sri.Supplier_Return_ID = key; sri.Type_of_Inventory = (string)ret["Type_of_Inventory"]; sri.Inventory_ID = (int)ret["Inventory_ID"]; sri.Units_Returned = (int)ret["Units_Returned"]; sri.Item_Name = (string)ret["Item_Name"]; if (sri.Units_Returned > 0) { db.Supplier_Return_Item.Add(sri); } } db.Supplier_Return.Add(sr); db.SaveChanges(); if (action == "email") { Supplier_Order so = new Supplier_Order(); so = (from p in db.Supplier_Order where p.Supplier_Order_ID == id select p).First(); string to = so.Supplier.Email; string subject = "WME Supplier Return Note #" + key; String orderDate = sr.Date_of_Return.ToShortDateString(); string body = "Walter Meano Engineering Supplier Return Note #" + key + "\nThe return note was generated on " + orderDate + "\n\nItems in return note:\n"; foreach (JObject ret in returnDetails) { Supplier_Return_Item sri = new Supplier_Return_Item(); sri.Return_Item_ID = item_key; item_key++; sri.Supplier_Return_ID = key; sri.Type_of_Inventory = (string)ret["Type_of_Inventory"]; sri.Inventory_ID = (int)ret["Inventory_ID"]; sri.Units_Returned = (int)ret["Units_Returned"]; sri.Item_Name = (string)ret["Item_Name"]; if (sri.Units_Returned > 0) { db.Supplier_Return_Item.Add(sri); } } foreach (JObject ret in returnDetails) { if ((int)ret["Units_Returned"] > 0) { body += (string)ret["Item_Name"] + "\t\tx" + (int)ret["Units_Returned"] + "\n"; } } Email.SendEmail(to, subject, body); } return("true|Supplier Return Note #" + key + " successfully generated."); } catch (Exception e) { ExceptionLog.LogException(e, "ReturnSupplierController PUT"); return("false|An error has occured updating the Supplier Purchase Order on the system."); } }
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 cancelSupplierOrder(int id) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.supplierOrder = new ExpandoObject(); try { Supplier_Order order = db.Supplier_Order.Where(x => x.SupplierOrderID == id).FirstOrDefault(); if (order != null) { if (order.SupplierOrderStatusID == 2) { toReturn.Message = "Supplier Order Was Already Cancelled"; return(toReturn); } if (order.SupplierOrderStatusID == 3 || order.SupplierOrderStatusID == 4) { toReturn.Message = "Delivered BackOrderd Orders Cannot Be Cancel"; } else { Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 2).FirstOrDefault(); order.SupplierOrderStatusID = status.SupplierOrderStatusID; order.Supplier_Order_Status = status; db.SaveChanges(); List <Supplier_Order_Product> order_Products = db.Supplier_Order_Product.Where(x => x.SupplierOrderID == id).ToList(); foreach (Supplier_Order_Product prod in order_Products) { //get product details Product product = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault(); //get the backlog details of the product so we can add back the quantity Product_Backlog backlog = db.Product_Backlog.Where(x => x.ProductID == product.ProductID && x.ContainerID == order.ContainerID).FirstOrDefault(); if (backlog != null) { //add back the quantity backlog.QuantityToOrder = backlog.QuantityToOrder + prod.SOPQuantityOrdered; backlog.DateModified = DateTime.Now; db.SaveChanges(); } else { //if no backlog record exits, create a new Product_Backlog backlog1 = new Product_Backlog(); backlog1.ProductID = product.ProductID; backlog1.QuantityToOrder = prod.SOPQuantityOrdered; backlog1.DateModified = DateTime.Now; backlog1.ContainerID = order.ContainerID; db.Product_Backlog.Add(backlog1); db.SaveChanges(); } } } toReturn.Message = "Supplier Order Successfuly Cancelled"; } else { toReturn.Error = "Supplier Order Not Found"; } } catch { toReturn.Error = "Search Interrupted. Retry"; } 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 placeSupplierOrder(int id) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.supplierOrder = new ExpandoObject(); try { Supplier_Order order = db.Supplier_Order.Where(x => x.SupplierOrderID == id).FirstOrDefault(); if (order != null) { Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 1).FirstOrDefault(); order.SupplierOrderStatusID = status.SupplierOrderStatusID; db.SaveChanges(); List <Supplier_Order_Product> order_Products = db.Supplier_Order_Product.Where(x => x.SupplierOrderID == id).ToList(); foreach (Supplier_Order_Product prod in order_Products) { //get product details Product product = db.Products.Where(x => x.ProductID == prod.ProductID).FirstOrDefault(); //get the backlog details of the product so we can add back the quantity Product_Backlog backlog = db.Product_Backlog.Where(x => x.ProductID == product.ProductID && x.ContainerID == order.ContainerID).FirstOrDefault(); if (backlog != null) { //remove back the quantity if (backlog.QuantityToOrder < prod.SOPQuantityOrdered) { backlog.QuantityToOrder = 0; backlog.DateModified = DateTime.Now; db.SaveChanges(); } else { backlog.QuantityToOrder = backlog.QuantityToOrder - prod.SOPQuantityOrdered; backlog.DateModified = DateTime.Now; db.SaveChanges(); } } } toReturn.Message = this.sendEmail(id); } else { toReturn.Error = "Supplier Order Details Not Found"; } } catch { toReturn.Error = "Search Interrupted. Retry"; } return(toReturn); }
public object getSupplierOrdersByID(int id) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.supplierOrder = new ExpandoObject(); try { Supplier_Order order = db.Supplier_Order.Where(x => x.SupplierOrderID == id).FirstOrDefault(); if (order != null) { //get supplier linked to Supplier supplier = db.Suppliers.Where(x => x.SupplierID == order.SupplierID).FirstOrDefault(); Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == order.SupplierOrderStatusID).FirstOrDefault(); //set dynamic object for the supplier order details dynamic Order = new ExpandoObject(); Order.SupplierOrderID = order.SupplierOrderID; Order.SupplierID = order.SupplierID; Order.SupName = supplier.SupName; Order.ContainerID = order.ContainerID; Order.SupEmail = supplier.SupEmail; Order.SODate = order.SODate; Order.Status = status.SOSDescription; toReturn.supplierOrder = Order; //set the list of products List <Supplier_Order_Product> order_Products = db.Supplier_Order_Product.Where(x => x.SupplierOrderID == order.SupplierOrderID).ToList(); List <dynamic> products = new List <dynamic>(); if (order_Products != null) { foreach (Supplier_Order_Product product1 in order_Products) { Product prod = db.Products.Where(x => x.ProductID == product1.ProductID).FirstOrDefault(); if (prod != null) { dynamic product = new ExpandoObject(); product.ProductID = product1.ProductID; product.SupplierID = prod.SupplierID; product.ProdName = prod.ProdName; product.ProdDescription = prod.ProdDesciption; product.SOPQuantityOrdered = product1.SOPQuantityOrdered; product.SOPQuantityRecieved = product1.SOPQuantityRecieved; products.Add(product); } } toReturn.products = products; } else { toReturn.Error = "No Prodcuts In Supplier Order"; } } else { toReturn.Error = "Supplier Order Not Found"; } } catch { toReturn = "Search Interrupted. Retry"; } return(toReturn); }
public object addProductToOrder(int containerID, int supplierID, int productID, int quantity) { 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 (supplier != null && product != null) { DateTime date = DateTime.Now; string date_ = date.ToString("yyyy-MM-dd"); date = Convert.ToDateTime(date_); //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.SupplierID == supplierID && x.SODate == date && x.SupplierOrderStatusID == 1 && x.ContainerID == containerID).FirstOrDefault(); if (supplier_Order == null) { //get the "Placed" order status Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 1).FirstOrDefault(); //create new supplier order Supplier_Order newOrder = new Supplier_Order(); newOrder.SupplierID = supplierID; newOrder.SODate = DateTime.Now; newOrder.ContainerID = containerID; newOrder.SupplierOrderStatusID = status.SupplierOrderStatusID; db.Supplier_Order.Add(newOrder); db.SaveChanges(); //retrive the placed order Supplier_Order order = db.Supplier_Order.ToList().LastOrDefault(); if (order != null) { //add the product to the created order Supplier_Order_Product addProd = new Supplier_Order_Product(); addProd.ProductID = productID; addProd.SupplierOrderID = order.SupplierOrderID; addProd.SOPQuantityOrdered = quantity; addProd.SOPQuantityRecieved = 0; db.Supplier_Order_Product.Add(addProd); db.SaveChanges(); //returning the product so you can see it in the console toReturn.product = db.Supplier_Order_Product.Where(x => x.ProductID == productID && x.SupplierOrderID == order.SupplierOrderID).FirstOrDefault(); } } else { //add product to existing Order Supplier_Order_Product addProd = new Supplier_Order_Product(); addProd.ProductID = productID; addProd.SupplierOrderID = supplier_Order.SupplierOrderID; addProd.SOPQuantityOrdered = quantity; addProd.SOPQuantityRecieved = 0; db.Supplier_Order_Product.Add(addProd); 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 Added To Order"; } else { toReturn.Error = "Supplier Or Product Details Not Found"; } } catch { toReturn.Error = "Product Already Ordered. Awaiting Supplier Order Deliery"; } return(toReturn); }
public object sendBackOrderEmail(int supplierOrderID) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { Supplier_Order order = db.Supplier_Order.Where(z => z.SupplierOrderID == supplierOrderID).FirstOrDefault(); Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 4).FirstOrDefault(); if (order != null) { Supplier supplier = db.Suppliers.Where(x => x.SupplierID == order.SupplierID).FirstOrDefault(); if (supplier != null) { List <Supplier_Order_Product> products = db.Supplier_Order_Product.Where(x => x.SupplierOrderID == supplierOrderID).ToList(); if (products.Count != 0) { List <Supplier_Order_Product> backProducts = new List <Supplier_Order_Product>(); foreach (Supplier_Order_Product back in products) { if (back.SOPQuantityOrdered > back.SOPQuantityRecieved) { backProducts.Add(back); } } if (backProducts.Count != 0) { if (order != null && status != null) { order.SupplierOrderStatusID = status.SupplierOrderStatusID; order.Supplier_Order_Status = status; db.SaveChanges(); } using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress("*****@*****.**"); mail.To.Add(supplier.SupEmail); mail.Subject = "Ordra Products Back Order"; mail.Body = PrepareHtml(backProducts); mail.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)) { smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Ordra@444"); smtp.EnableSsl = true; smtp.Send(mail); toReturn.Message = "Supplier BackOrder Email Sent"; } } } else { toReturn.Error = "No Products In Order For BackOrder"; } } else { toReturn.Error = "No Products In Order"; } } else { toReturn.Error = "Supplier Not Found"; } } else { toReturn.Error = "Supplier Order Not Found"; } } catch { toReturn.Error = "Mail unsuccessfully sent"; } 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); }