// POST: api/JobCard public string Post(HttpRequestMessage value) { try { string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JArray partDetails = (JArray)json["details"]; Job_Card jc = new Job_Card(); int job_key = db.Job_Card.Count() == 0 ? 1 : (from t in db.Job_Card orderby t.Job_Card_ID descending select t.Job_Card_ID).First() + 1; jc.Job_Card_ID = job_key; jc.Job_Card_Date = (DateTime)json["Job_Card_Date"]; jc.Job_Card_Status_ID = (int)json["Job_Card_Status_ID"]; jc.Job_Card_Priority_ID = 2; db.Job_Card.Add(jc); db.SaveChanges(); int detail_key = db.Job_Card_Detail.Count() == 0 ? 1 : (from t in db.Job_Card_Detail orderby t.Job_Card_Details_ID descending select t.Job_Card_Details_ID).First() + 1; foreach (JObject part in partDetails) { Job_Card_Detail jcd = new Job_Card_Detail(); jcd.Job_Card_Details_ID = detail_key; detail_key++; jcd.Part_Type_ID = (int)part["Part_Type_ID"]; jcd.Job_Card_ID = job_key; jcd.Non_Manual = Convert.ToBoolean((string)part["Non_Manual"]); jcd.Quantity = (int)part["Quantity"]; db.Job_Card_Detail.Add(jcd); db.SaveChanges(); //2. Handle child dependencies checkForChildren(jcd.Part_Type_ID, jcd.Quantity, job_key, "Manufacturing"); generateUniqueParts(jcd.Quantity, jcd.Part_Type_ID, 0, detail_key - 1, "Manufacturing"); } return("true|Job Card #" + job_key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "JobCardController POST"); return("false|An error has occured adding the Job Card to the system."); } }
// POST: api/SubContractor public string Post(HttpRequestMessage value) { try { Model.Sub_Contractor sub = new Model.Sub_Contractor(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject subDetails = JObject.Parse(message); JArray contactDetails = (JArray)subDetails["contact_details"]; int key = db.Sub_Contractor.Count() == 0 ? 1 : (from t in db.Sub_Contractor orderby t.Sub_Contractor_ID descending select t.Sub_Contractor_ID).First() + 1; sub.Sub_Contractor_ID = key; sub.Name = (string)subDetails["Name"]; sub.Address = (string)subDetails["Address"]; sub.City = (string)subDetails["City"]; sub.Zip = (string)subDetails["Zip"]; sub.Status = Convert.ToBoolean(Convert.ToInt32(subDetails["Status"])); sub.Manual_Labour_Type_ID = (int)subDetails["Manual_Labour_Type_ID"]; sub.Province_ID = (int)subDetails["Province_ID"]; string errorString = "false|"; bool error = false; if ((from t in db.Sub_Contractor where t.Name == sub.Name select t).Count() != 0) { error = true; errorString += "The Sub-Contractor name entered already exists on the system. "; } if ((from t in db.Sub_Contractor where t.Address == sub.Address && t.City == sub.City && t.Zip == sub.Zip select t).Count() != 0) { error = true; errorString += "The Sub-Contractor address entered already exists on the system. "; } if (error) { return(errorString); } db.Sub_Contractor.Add(sub); int contact_id = db.Sub_Contractor_Contact_Detail.Count() == 0 ? 1 : (from t in db.Sub_Contractor_Contact_Detail orderby t.Contact_ID descending select t.Contact_ID).First() + 1; foreach (JObject contact in contactDetails) { Sub_Contractor_Contact_Detail ccpd = new Sub_Contractor_Contact_Detail(); ccpd.Contact_ID = contact_id; contact_id++; ccpd.Sub_Contractor_ID = key; ccpd.Number = (string)contact["Number"]; ccpd.Name = (string)contact["Name"]; ccpd.Email = (string)contact["Email"]; db.Sub_Contractor_Contact_Detail.Add(ccpd); } db.SaveChanges(); return("true|Sub-Contractor #" + key + " successfully added."); } catch (DbEntityValidationException dbEx) { // Retrieve the error messages as a list of strings. var errorMessages = dbEx.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(dbEx.Message, " The validation errors are: ", fullErrorMessage); ExceptionLog.LogException(dbEx, exceptionMessage); return("false|An error has occured adding the Sub-Contractor to the system.|" + exceptionMessage); } catch (Exception e) { ExceptionLog.LogException(e, "SubContractorController POST"); return("false|An error has occured adding the Sub-Contractor to the system."); } }
private void generateProductionSchedule() { TimeSpan day_start = new TimeSpan(8, 0, 0); TimeSpan day_end = new TimeSpan(17, 0, 0); TimeSpan day_duration = day_end.Subtract(day_start); List <Machine> machines = new List <Machine>(); List <Manual_Labour_Type> manual = new List <Manual_Labour_Type>(); List <Part> pp = new List <Part>(); List <Unique_Machine> unique = new List <Unique_Machine>(); //*************************************** Create Part Queue List <Job_Card> cards = (from p in db.Job_Card where p.Job_Card_Status_ID == 1 || p.Job_Card_Status_ID == 2 orderby p.Job_Card_Priority_ID, p.Job_Card_Date select p).ToList(); //Make an empty queue Queue <ProductionPart> parts = new Queue <ProductionPart>(); foreach (Job_Card jc in cards) { if (jc.Job_Card_Status_ID == 1) { //Armand : IF job card has a status of started then update to in-production Job_Card jcTemp = new Job_Card(); jcTemp = (from p in db.Job_Card where p.Job_Card_ID == jc.Job_Card_ID select p).First(); jcTemp.Job_Card_Status_ID = 2; db.SaveChanges(); } //Somehow get parts foreach (Job_Card_Detail jcd in jc.Job_Card_Detail) { List <Part> tmp = (from p in db.Parts where p.Job_Card_Detail.Where(y => y.Job_Card_Details_ID == jcd.Job_Card_Details_ID).FirstOrDefault().Job_Card_Details_ID == jcd.Job_Card_Details_ID && p.Part_Status_ID < 3 select p).ToList(); pp = pp.Concat(tmp).ToList(); foreach (Part p in tmp) { ProductionPart tmp2 = new ProductionPart(); tmp2.part = p; tmp2.job_card_ID = jcd.Job_Card_ID; List <Machine> mac = (from z in db.Machines join d in db.Machine_Part on z.Machine_ID equals d.Machine_ID where d.Part_Type_ID == p.Part_Type_ID select z).ToList(); List <Manual_Labour_Type> man = (from z in db.Manual_Labour_Type join d in db.Manual_Labour_Type_Part on z.Manual_Labour_Type_ID equals d.Manual_Labour_Type_ID where d.Part_Type_ID == p.Part_Type_ID select z).ToList();; tmp2.recipe = p.Part_Type.Recipes.ToList(); foreach (Machine m in mac) { ProductionPart.MachineRecipe mr = new ProductionPart.MachineRecipe(); mr.machine = m; mr.stage = (from z in db.Machine_Part where z.Machine_ID == m.Machine_ID && z.Part_Type_ID == p.Part_Type_ID select z.Stage_In_Manufacturing).First(); machines.Add(m); tmp2.machines.Add(mr); } foreach (Manual_Labour_Type m in man) { ProductionPart.ManualRecipe mr = new ProductionPart.ManualRecipe(); mr.manual = m; mr.stage = (from z in db.Manual_Labour_Type_Part where z.Manual_Labour_Type_ID == m.Manual_Labour_Type_ID && z.Part_Type_ID == p.Part_Type_ID select z.Stage_In_Manufacturing).First(); manual.Add(m); tmp2.manual.Add(mr); } parts.Enqueue(tmp2); } } } manual = manual.Distinct().ToList(); machines = machines.Distinct().ToList(); foreach (Machine m in machines) { unique = unique.Concat(m.Unique_Machine.Where(x => x.Machine_Status_ID == 1)).ToList(); } int i = 1440; //max amount of time is equal to 24 hours //Calculate the shortest amount of time. foreach (Machine m in machines) { if (i > m.Run_Time) { i = m.Run_Time; } } foreach (Manual_Labour_Type ml in manual) { if (i > ml.Duration) { i = ml.Duration; } } TimeSpan intervals = new TimeSpan(0, i, 0); int interval_count = Convert.ToInt32(day_duration.TotalMinutes / i); int num_of_resources = unique.Count() + manual.Count(); bool[,] grid = new bool[interval_count, num_of_resources]; Employee[,] gridEmployee = new Employee[interval_count, num_of_resources]; for (int aa = 0; aa < interval_count; aa++) { for (int bb = 0; bb < num_of_resources; bb++) { grid[aa, bb] = false; gridEmployee[aa, bb] = null; } } //*************************************** Create production schedule Production_Schedule ps = new Production_Schedule(); int key = db.Production_Schedule.Count() == 0 ? 1 : (from t in db.Production_Schedule orderby t.Production_Schedule_ID descending select t.Production_Schedule_ID).First() + 1; ps.Production_Schedule_ID = key; ps.Production_Schedule_Date = DateTime.Now; ps.intervals = Convert.ToInt32(intervals.TotalMinutes); db.Production_Schedule.Add(ps); db.SaveChanges(); bool scheduleFull = false; while (parts.Count != 0 && scheduleFull == false) { ProductionPart ProdPart = parts.Dequeue(); TimeSpan currentTime = day_start; int interval_stage = 0; for (int x = 1; x <= ProdPart.part.Part_Type.Number_Of_Stages; x++) { Machine tmp1; Manual_Labour_Type tmp2; try { tmp1 = ProdPart.machines.Find(y => y.stage == x).machine; } catch { tmp1 = null; } try { tmp2 = ProdPart.manual.Find(y => y.stage == x).manual; } catch { tmp2 = null; } Recipe tmp3 = ProdPart.recipe.Find(y => y.Stage_in_Manufacturing == x); int cellcount; if (tmp1 != null) { cellcount = RoundUp(tmp1.Run_Time, i) / i; bool foundEmployee = false; bool foundMachine = false; List <Unique_Machine> um = tmp1.Unique_Machine.ToList(); int k = manual.Count(); int l = 0; for (int y = 0; y < unique.Count(); y++) { if (um[0].Unique_Machine_ID == unique[y].Unique_Machine_ID) { k += y; break; } } while (!foundEmployee && !foundMachine) { if (interval_stage + cellcount >= interval_count) { l++; if (l == um.Count()) { break; } k = manual.Count(); for (int y = 0; y < unique.Count(); y++) { if (um[l].Unique_Machine_ID == unique[y].Unique_Machine_ID) { k += y; break; } } interval_stage = Convert.ToInt32((currentTime.TotalMinutes - day_start.TotalMinutes) / i); } foundMachine = false; foundEmployee = false; bool spotOpen = true; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { if (grid[g, k] == true) { spotOpen = false; } } if (!spotOpen) { interval_stage += 1; } else { foundMachine = true; Unique_Machine umO = um[l]; List <Employee> empList = (from p in db.Employees where p.Machines.Where(e => e.Machine_ID == tmp1.Machine_ID).FirstOrDefault().Machine_ID == tmp1.Machine_ID select p).ToList(); int z = 0; bool empOpen = true; Employee emp = null; while (empOpen) { if (z == empList.Count()) { emp = null; break; } emp = empList[z]; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { for (int h = 0; h < manual.Count() + unique.Count(); h++) { if (gridEmployee[g, h] == emp) { empOpen = false; } } } if (!empOpen) { z++; empOpen = true; } else { break; } } if (emp == null) { interval_stage += 1; } else { foundEmployee = true; Production_Task pt = new Production_Task(); int prod_task_key = db.Production_Task.Count() == 0 ? 1 : (from t in db.Production_Task orderby t.Production_Task_ID descending select t.Production_Task_ID).First() + 1; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { grid[g, k] = true; gridEmployee[g, k] = emp; } TimeSpan minutes = new TimeSpan(0, interval_stage * Convert.ToInt32(intervals.TotalMinutes), 0); TimeSpan start_time = currentTime.Add(minutes); minutes = new TimeSpan(0, RoundUp(tmp1.Run_Time, i), 0); TimeSpan end_time = start_time.Add(minutes); currentTime = end_time; pt.Production_Task_ID = prod_task_key; pt.Production_Task_Type = "Machine"; pt.start_time = start_time; pt.end_time = end_time; pt.Part_Stage = x; pt.Production_Schedule_ID = key; pt.Employee_ID = emp.Employee_ID; pt.complete = false; pt.Part_ID = ProdPart.part.Part_ID; pt.Resource_ID = umO.Unique_Machine_ID; pt.Job_Card_ID = ProdPart.job_card_ID; pt.duration = RoundUp(tmp1.Run_Time, i); db.Production_Task.Add(pt); db.SaveChanges(); } } } } else if (tmp2 != null) { cellcount = RoundUp(tmp2.Duration, i) / i; bool foundEmployee = false; bool foundManual = false; int k = 0; for (int y = 0; y < manual.Count(); y++) { if (tmp2.Manual_Labour_Type_ID == manual[y].Manual_Labour_Type_ID) { k += y; break; } } while (!foundEmployee && !foundManual) { if (interval_stage + cellcount >= interval_count) { break; } foundManual = false; foundEmployee = false; bool spotOpen = true; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { if (grid[g, k] == true) { spotOpen = false; } } if (!spotOpen) { interval_stage += 1; } else { foundManual = true; List <Employee> empList = (from p in db.Employees where p.Manual_Labour_Type.Where(e => e.Manual_Labour_Type_ID == tmp2.Manual_Labour_Type_ID).FirstOrDefault().Manual_Labour_Type_ID == tmp2.Manual_Labour_Type_ID select p).ToList(); int z = 0; bool empOpen = true; Employee emp = null; while (empOpen) { if (z == empList.Count()) { emp = null; break; } emp = empList[z]; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { for (int h = 0; h < manual.Count() + unique.Count(); h++) { if (gridEmployee[g, h] == emp) { empOpen = false; } } } if (!empOpen) { z++; empOpen = true; } else { break; } } if (emp == null) { interval_stage += 1; } else { foundEmployee = true; Production_Task pt = new Production_Task(); int prod_task_key = db.Production_Task.Count() == 0 ? 1 : (from t in db.Production_Task orderby t.Production_Task_ID descending select t.Production_Task_ID).First() + 1; for (int g = interval_stage; g < interval_stage + cellcount && g < interval_count; g++) { grid[g, k] = true; gridEmployee[g, k] = emp; } TimeSpan minutes = new TimeSpan(0, interval_stage * Convert.ToInt32(intervals.TotalMinutes), 0); TimeSpan start_time = currentTime.Add(minutes); minutes = new TimeSpan(0, RoundUp(tmp2.Duration, i), 0); TimeSpan end_time = start_time.Add(minutes); currentTime = end_time; pt.Production_Task_ID = prod_task_key; pt.Production_Task_Type = "Manual"; pt.start_time = start_time; pt.end_time = end_time; pt.Part_Stage = x; pt.Production_Schedule_ID = key; pt.Employee_ID = emp.Employee_ID; pt.complete = false; pt.Part_ID = ProdPart.part.Part_ID; pt.Resource_ID = tmp2.Manual_Labour_Type_ID; pt.Job_Card_ID = ProdPart.job_card_ID; pt.duration = RoundUp(tmp2.Duration, i); db.Production_Task.Add(pt); db.SaveChanges(); } } } } } } }
// 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."); } }
// POST: api/CreditNote public string Post(HttpRequestMessage value) { try { Model.Customer_Credit cc = new Model.Customer_Credit(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JArray returnDetails = (JArray)json["cni"]; string action = (string)json["action"]; int key = db.Customer_Credit.Count() == 0 ? 1 : (from t in db.Customer_Credit orderby t.Customer_Credit_ID descending select t.Customer_Credit_ID).First() + 1; cc.Customer_Credit_ID = key; cc.Date = (DateTime)json["Date"]; cc.Return_Comment = (string)json["Return_Comment"]; db.Customer_Credit.Add(cc); foreach (JObject part in returnDetails) { if ((bool)part["return_to"] == true) { Customer_Credit_Detail ccd = new Customer_Credit_Detail(); ccd.Customer_Credit_ID = key; ccd.Part_ID = (int)part["Part_ID"]; ccd.Client_Order_Detail_ID = (int)part["Client_Order_Detail_ID"]; ccd.Status = (string)part["status"]; Part p = new Part(); p = (from d in db.Parts where d.Part_ID == ccd.Part_ID select d).First(); p.Part_Status_ID = (int)part["status"]; db.Customer_Credit_Detail.Add(ccd); } } db.SaveChanges(); if (action == "email") { string to = (string)json["email"]; string subject = "WME Credit Note #" + key; String orderDate = cc.Date.ToShortDateString(); string body = "Walter Meano Engineering Credit Note #" + key + "\nThe credit note was generated on " + orderDate + "\n\nItems on Order:\n"; foreach (JObject part in returnDetails) { if ((bool)part["return_to"] == true) { int Part_ID = (int)part["Part_ID"]; Part p = new Part(); p = (from d in db.Parts where d.Part_ID == Part_ID select d).First(); body += p.Part_Type.Abbreviation + " - " + p.Part_Type.Name + " - " + p.Part_Serial + "\n"; } } Email.SendEmail(to, subject, body); } return("true|Customer Credit Note #" + key + " successfully generated.|" + key); } catch (Exception e) { ExceptionLog.LogException(e, "CreditNoteController"); return("false|An error has occured adding the Customer Credit Note to the system."); } }
// POST: api/Component public string Post(HttpRequestMessage value) { try { Model.Component component = new Model.Component(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject cmpDetails = (JObject)json["component"]; JArray supplierDetails = (JArray)json["suppliers"]; int key = db.Components.Count() == 0 ? 1 : (from t in db.Components orderby t.Component_ID descending select t.Component_ID).First() + 1; component.Component_ID = key; component.Quantity = (int)cmpDetails["Quantity"]; component.Unit_Price = (decimal)cmpDetails["Unit_Price"]; component.Description = (string)cmpDetails["Description"]; component.Dimension = (string)cmpDetails["Dimension"]; component.Name = (string)cmpDetails["Name"]; component.Min_Stock = (int)cmpDetails["Min_Stock"]; string errorString = "false|"; bool error = false; if ((from t in db.Components where t.Name == component.Name select t).Count() != 0) { error = true; errorString += "The Component name entered already exists on the system. "; } if (error) { return(errorString); } db.Components.Add(component); foreach (JObject supplier in supplierDetails) { Component_Supplier cs = new Component_Supplier(); cs.Component_ID = key; cs.Supplier_ID = (int)supplier["Supplier_ID"]; cs.is_preferred = Convert.ToBoolean(Convert.ToInt32(supplier["Is_Prefered"])); cs.unit_price = (decimal)supplier["unit_price"]; db.Component_Supplier.Add(cs); } db.SaveChanges(); return("true|Component #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "ComponentController POST"); return("false|An error has occured adding the Component to the system."); } }
// POST: api/Customer public string Post(HttpRequestMessage value) { try { Model.Client client = new Model.Client(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject clientDetails = (JObject)json["client"]; JArray partDiscounts = (JArray)json["discounts"]; JArray contactDetails = (JArray)json["contacts"]; int key = db.Clients.Count() == 0 ? 1 : (from t in db.Clients orderby t.Client_ID descending select t.Client_ID).First() + 1; client.Client_ID = key; client.Name = (string)clientDetails["Name"]; client.Address = (string)clientDetails["Address"]; client.City = (string)clientDetails["City"]; client.Zip = (string)clientDetails["Zip"]; client.Overdue_Payment = 0; client.Vat_Number = (string)clientDetails["Vat_Number"]; client.Account_Name = (string)clientDetails["Account_Name"]; client.Contract_Discount_Rate = (int)clientDetails["Contract_Discount_Rate"]; client.Client_Status = Convert.ToBoolean(Convert.ToInt32(clientDetails["Client_Status"])); client.Province_ID = (int)clientDetails["Province_ID"]; client.Settlement_Discount_Rate = (int)clientDetails["Settlement_Discount_Rate"]; string errorString = "false|"; bool error = false; if ((from t in db.Clients where t.Name == client.Name select t).Count() != 0) { error = true; errorString += "The Customer Name entered already exists on the system. "; } if ((from t in db.Clients where t.Name == client.Vat_Number select t).Count() != 0) { error = true; errorString += "The Customer VAT Number entered already exists on the system. "; } if ((from t in db.Clients where t.Address == client.Address && t.City == client.City && t.Zip == client.Zip select t).Count() != 0) { error = true; errorString += "The Customer Address entered already exists on the system. "; } if ((from t in db.Clients where t.Vat_Number == client.Vat_Number && client.Vat_Number != "" select t).Count() != 0) { error = true; errorString += "The Customer VAT Number entered already exists on the system. "; } if ((from t in db.Clients where t.Account_Name == client.Account_Name select t).Count() != 0) { error = true; errorString += "The Customer VAT Number entered already exists on the system. "; } if (error) { return(errorString); } db.Clients.Add(client); int contact_key = db.Client_Contact_Person_Detail.Count() == 0 ? 1 : (from t in db.Client_Contact_Person_Detail orderby t.Contact_ID descending select t.Contact_ID).First() + 1; foreach (JObject contact in contactDetails) { Client_Contact_Person_Detail ccpd = new Client_Contact_Person_Detail(); ccpd.Contact_ID = contact_key; contact_key++; ccpd.Client_ID = key; ccpd.Number = (string)contact["Number"]; ccpd.Name = (string)contact["Name"]; ccpd.Job_Description = (string)contact["Job_Description"]; ccpd.Email_Address = (string)contact["Email_Address"]; errorString = "false|"; error = false; if ((from t in db.Client_Contact_Person_Detail where t.Email_Address == ccpd.Email_Address select t).Count() != 0) { error = true; errorString += "The Customer Contact Email for " + ccpd.Name + " entered already exists on the system. "; } if ((from t in db.Client_Contact_Person_Detail where t.Number == ccpd.Number select t).Count() != 0) { error = true; errorString += "The Customer Contact Number for " + ccpd.Name + " entered already exists on the system. "; } if (error) { return(errorString); } db.Client_Contact_Person_Detail.Add(ccpd); } foreach (JObject part in partDiscounts) { Client_Discount_Rate cdr = new Client_Discount_Rate(); cdr.Client_ID = key; cdr.Part_Type_ID = (int)part["Part_Type_ID"]; cdr.Discount_Rate = (float)part["Discount_Rate"]; db.Client_Discount_Rate.Add(cdr); } db.SaveChanges(); return("true|Customer # " + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "CustomerController POST"); return("false|An error has occured adding the Customer to the system."); } }
// PUT: api/ReceiveSupplierOrder/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 cs = (JArray)json["cs"]; JArray ps = (JArray)json["ps"]; JArray rms = (JArray)json["rms"]; var so = (from p in db.Supplier_Order where p.Supplier_Order_ID == id select p).First(); bool rawAll = true; bool partAll = true; bool compAll = true; foreach (JObject comp in cs) { int component_id = (int)comp["Component_ID"]; Supplier_Order_Component soc = new Supplier_Order_Component(); soc = (from p in db.Supplier_Order_Component where p.Supplier_Order_ID == id && p.Component_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int newquantity = (int)comp["Quantity_Received"] - quantity; soc.Quantity_Received = (int)comp["Quantity_Received"]; soc.Supplier_Order = so; Component c = new Component(); c = (from p in db.Components where p.Component_ID == component_id select p).First(); c.Quantity += newquantity; if (soc.Quantity_Requested != soc.Quantity_Received) { compAll = false; } } foreach (JObject part in ps) { int component_id = (int)part["Part_Type_ID"]; Supplier_Order_Detail_Part soc = new Supplier_Order_Detail_Part(); soc = (from p in db.Supplier_Order_Detail_Part where p.Supplier_Order_ID == id && p.Part_Type_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int newquantity = (int)part["Quantity_Received"] - quantity; soc.Quantity_Received = (int)part["Quantity_Received"]; int key = db.Parts.Count() == 0 ? 1 : (from t in db.Parts orderby t.Part_ID descending select t.Part_ID).First() + 1; if (soc.Quantity != soc.Quantity_Received) { partAll = false; } for (int x = 0; x < newquantity; x++) { string abbreviation = soc.Part_Type.Abbreviation; //(Armand) Generate the new number; Random rand = new Random(); int num = 0; string serial = ""; while (true) { num = rand.Next(1, 999999999); serial = abbreviation + "-" + Convert.ToString(num); if ((from d in db.Parts where d.Part_Serial == serial select d).Count() == 0) { break; } } Part p = new Part(); p.Parent_ID = 0; p.Part_Serial = serial; p.Part_Status_ID = 1; p.Part_Type_ID = component_id; p.Part_ID = key; key++; p.Part_Stage = 0; p.Date_Added = DateTime.Now; p.Cost_Price = (decimal)part["Price"]; p.Supplier_Order.Add(so); db.Parts.Add(p); } } foreach (JObject raw in rms) { int component_id = (int)raw["Raw_Material_ID"]; Supplier_Order_Detail_Raw_Material soc = new Supplier_Order_Detail_Raw_Material(); soc = (from p in db.Supplier_Order_Detail_Raw_Material where p.Supplier_Order_ID == id && p.Raw_Material_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int newquantity = (int)raw["Quantity_Received"] - quantity; soc.Quantity_Received = (int)raw["Quantity_Received"]; int key = db.Unique_Raw_Material.Count() == 0 ? 1 : (from t in db.Unique_Raw_Material orderby t.Unique_Raw_Material_ID descending select t.Unique_Raw_Material_ID).First() + 1; if (soc.Quantity != soc.Quantity_Received) { rawAll = false; } for (int x = 0; x < newquantity; x++) { Unique_Raw_Material p = new Unique_Raw_Material(); p.Unique_Raw_Material_ID = key; key++; p.Cost_Price = (decimal)raw["Price"]; p.Date_Added = DateTime.Now; p.Raw_Material_ID = component_id; p.Dimension = (string)raw["Dimensions"]; p.Quality = ""; p.Supplier_Order_ID = id; db.Unique_Raw_Material.Add(p); } } if (rawAll && partAll && compAll) { so.Supplier_Order_Status_ID = 5; } else { so.Supplier_Order_Status_ID = 2; string to = so.Supplier.Email; string subject = "WME Supplier Order #" + so.Supplier_Order_ID + " Back Order"; String orderDate = DateTime.Now.ToShortDateString(); string body = "Walter Meano Engineering Supplier Order #" + so.Supplier_Order_ID + "\nThe order was partially received on " + orderDate + "\n\nThe following items still need to be delivered:\n"; foreach (JObject comp in cs) { int component_id = (int)comp["Component_ID"]; Supplier_Order_Component soc = new Supplier_Order_Component(); soc = (from p in db.Supplier_Order_Component where p.Supplier_Order_ID == id && p.Component_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int quantityLeft = soc.Quantity_Requested - quantity; if (quantityLeft != 0) { Component c = new Component(); c = (from p in db.Components where p.Component_ID == component_id select p).First(); body += c.Name + "\t\tx" + quantityLeft + "\n"; } } foreach (JObject part in ps) { int component_id = (int)part["Part_Type_ID"]; Supplier_Order_Detail_Part soc = new Supplier_Order_Detail_Part(); soc = (from p in db.Supplier_Order_Detail_Part where p.Supplier_Order_ID == id && p.Part_Type_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int quantityLeft = soc.Quantity - quantity; if (quantityLeft != 0) { Part_Type c = new Part_Type(); c = (from p in db.Part_Type where p.Part_Type_ID == component_id select p).First(); body += c.Name + "\t\tx" + quantityLeft + "\n"; } } foreach (JObject raw in rms) { int component_id = (int)raw["Raw_Material_ID"]; Supplier_Order_Detail_Raw_Material soc = new Supplier_Order_Detail_Raw_Material(); soc = (from p in db.Supplier_Order_Detail_Raw_Material where p.Supplier_Order_ID == id && p.Raw_Material_ID == component_id select p).First(); int quantity = soc.Quantity_Received; int quantityLeft = soc.Quantity - quantity; if (quantityLeft != 0) { Raw_Material c = new Raw_Material(); c = (from p in db.Raw_Material where p.Raw_Material_ID == component_id select p).First(); body += c.Name + "\t\tx" + quantityLeft + "\n"; } } Email.SendEmail(to, subject, body); } db.SaveChanges(); return("true|Supplier Purchase Order successfully received."); } catch (Exception e) { ExceptionLog.LogException(e, "ReceiveSupplierOrderController"); return("false|An error has occured receiving the Supplier Purchase Order on the system."); } }
// POST: api/Supplier public string Post(HttpRequestMessage value) { try { Model.Supplier supplier = new Model.Supplier(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject supplierDetails = (JObject)json["supplier"]; JArray componentDetails = (JArray)json["components"]; JArray partTypeDetails = (JArray)json["parts"]; JArray RawMaterialDetails = (JArray)json["raw"]; int key = db.Suppliers.Count() == 0 ? 1 : (from t in db.Suppliers orderby t.Supplier_ID descending select t.Supplier_ID).First() + 1; supplier.Supplier_ID = key; supplier.Name = (string)supplierDetails["Name"]; supplier.Address = (string)supplierDetails["Address"]; supplier.City = (string)supplierDetails["City"]; supplier.Zip = (string)supplierDetails["Zip"]; supplier.Bank_Account_Number = (string)supplierDetails["Bank_Account_Number"]; supplier.Bank_Branch = (string)supplierDetails["Bank_Branch"]; supplier.Bank_Name = (string)supplierDetails["Bank_Name"]; supplier.Email = (string)supplierDetails["Email"]; supplier.Contact_Number = (string)supplierDetails["Contact_Number"]; supplier.Status = Convert.ToBoolean(Convert.ToInt32(supplierDetails["Status"])); supplier.Province_ID = (int)supplierDetails["Province_ID"]; supplier.Bank_Reference = (string)supplierDetails["Bank_Reference"]; supplier.Contact_Name = (string)supplierDetails["Contact_Name"]; supplier.Foreign_Bank = Convert.ToBoolean(supplierDetails["Foreign_Bank"]); string errorString = "false|"; bool error = false; if ((from t in db.Suppliers where t.Name == supplier.Name select t).Count() != 0) { error = true; errorString += "The Supplier Name entered already exists on the system. "; } if ((from t in db.Suppliers where t.Email == supplier.Email select t).Count() != 0) { error = true; errorString += "The Contact Email entered already exists on the system. "; } if ((from t in db.Suppliers where t.Contact_Number == supplier.Contact_Number select t).Count() != 0) { error = true; errorString += "The Contact Number entered already exists on the system. "; } if ((from t in db.Suppliers where t.Address == supplier.Address && t.City == supplier.City && t.Zip == supplier.Zip select t).Count() != 0) { error = true; errorString += "The Supplier Address entered already exists on the system. "; } if ((from t in db.Suppliers where t.Bank_Account_Number == supplier.Bank_Account_Number && t.Bank_Branch == supplier.Bank_Branch select t).Count() != 0) { error = true; errorString += "The Supplier Banking details entered already exists on the system. "; } if (error) { return(errorString); } db.Suppliers.Add(supplier); if (componentDetails != null) { foreach (JObject comp in componentDetails) { Component_Supplier cs = new Component_Supplier(); cs.Component_ID = (int)comp["Component_ID"]; cs.Supplier_ID = key; cs.is_preferred = Convert.ToBoolean(Convert.ToInt32(comp["Is_Prefered"])); cs.unit_price = (decimal)comp["unit_price"]; db.Component_Supplier.Add(cs); } } if (partTypeDetails != null) { foreach (JObject part in partTypeDetails) { Part_Supplier ps = new Part_Supplier(); ps.Part_Type_ID = (int)part["Part_Type_ID"]; ps.Supplier_ID = key; ps.Is_Prefered = Convert.ToBoolean(Convert.ToInt32(part["Is_Prefered"])); ps.unit_price = (decimal)part["unit_price"]; db.Part_Supplier.Add(ps); } } if (RawMaterialDetails != null) { foreach (JObject raw in RawMaterialDetails) { Raw_Material_Supplier rms = new Raw_Material_Supplier(); rms.Raw_Material_ID = (int)raw["Raw_Material_ID"]; rms.Supplier_ID = key; rms.Is_Prefered = Convert.ToBoolean(Convert.ToInt32(raw["Is_Prefered"])); rms.unit_price = (decimal)raw["unit_price"]; db.Raw_Material_Supplier.Add(rms); } } db.SaveChanges(); return("true|Supplier #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "SupplierController POST"); return("false|An error has occured adding the Supplier to the system."); } }
// POST: api/Invoice public string Post(HttpRequestMessage value) { try { Model.Invoice inv = new Model.Invoice(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); string action = (string)json["action"]; int key = db.Invoices.Count() == 0 ? 1 : (from t in db.Invoices orderby t.Invoice_ID descending select t.Invoice_ID).First() + 1; inv.Invoice_ID = key; inv.Invoice_Date = (DateTime)json["Invoice_Date"]; inv.Invoice_Status_ID = 1; inv.Delivery_Note_ID = (int)json["Delivery_Note_ID"]; inv.amount_noVat = (decimal)json["amount_noVat"]; inv.amount_Vat = (decimal)json["amount_Vat"]; string errorString = "false|"; bool error = false; if ((from t in db.Invoices where t.Delivery_Note_ID == inv.Delivery_Note_ID select t).Count() != 0) { error = true; errorString += "The Delivery Note Number already has an Invoice on the system. "; } if (error) { return(errorString); } db.Invoices.Add(inv); db.SaveChanges(); if (action == "email") { string to = (string)json["email"]; string subject = "WME Invoice #" + key; String orderDate = inv.Invoice_Date.ToShortDateString(); string body = "Walter Meano Engineering Invoice #" + key + "\nThe invoice was generated on " + orderDate + "\n\nItems payable:\n"; List <Delivery_Note_Details> parts = (from p in db.Delivery_Note_Details where p.Delivery_Note_ID == inv.Delivery_Note_ID select p).ToList(); foreach (Delivery_Note_Details part in parts) { Part_Type partDetails = (from p in db.Delivery_Note_Details where p.Delivery_Note_ID == inv.Delivery_Note_ID select p.Client_Order_Detail.Part_Type).First(); body += partDetails.Name + "\t\tx" + part.Quantity_Delivered + "\t" + part.Client_Order_Detail.Part_Price + " per unit\n"; } body += "\nAmount payable VAT Excl.:\t" + inv.amount_noVat; body += "\nAmount payable VAT Incl.:\t" + inv.amount_Vat; Email.SendEmail(to, subject, body); } return("true|Invoice #" + key + " successfully added.|" + key); } catch (Exception e) { ExceptionLog.LogException(e, "InvoiceController POST"); return("false|An error has occured adding the Invoice to the system."); } }
// POST: api/CustomerOrder public string Post(HttpRequestMessage value) { // try // { Model.Client_Order co = new Model.Client_Order(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject orderDetails = (JObject)json["client"]; JArray parts = (JArray)orderDetails["details"]; string todo = (string)json["action"]; int key = db.Client_Order.Count() == 0 ? 1 : (from t in db.Client_Order orderby t.Client_Order_ID descending select t.Client_Order_ID).First() + 1; co.Client_Order_ID = key; co.Date = (DateTime)orderDetails["Date"]; co.Reference_ID = (string)orderDetails["Reference_ID"]; co.Contact_ID = (int)orderDetails["Contact_ID"]; co.Client_ID = (int)orderDetails["Client_ID"]; co.Settlement_Discount_Rate = (double)orderDetails["Settlement_Discount_Rate"]; co.Client_Order_Type_ID = (int)orderDetails["Client_Order_Type_ID"]; co.Client_Order_Status_ID = 1; co.Settlement_Discount_Rate = (double)orderDetails["Settlement_Discount_Rate"]; co.Delivery_Method_ID = (int)orderDetails["Delivery_Method_ID"]; co.VAT_Inclu = Convert.ToBoolean((string)orderDetails["VAT_Inclu"]); co.Comment = (string)orderDetails["Comment"]; string errorString = "false|"; bool error = false; if ((from t in db.Client_Order where t.Reference_ID == co.Reference_ID select t).Count() != 0) { error = true; errorString += "The Customer Order Reference entered already exists on the system. "; } if (error) { return(errorString); } Job_Card jc = new Job_Card(); int job_key = db.Job_Card.Count() == 0 ? 1 : (from t in db.Job_Card orderby t.Job_Card_ID descending select t.Job_Card_ID).First() + 1; jc.Job_Card_ID = job_key; jc.Job_Card_Date = (DateTime)orderDetails["Date"]; jc.Job_Card_Status_ID = 1; jc.Job_Card_Priority_ID = 1; co.Job_Card_ID = job_key; db.Client_Order.Add(co); db.Job_Card.Add(jc); db.SaveChanges(); int detail_key = db.Job_Card_Detail.Count() == 0 ? 1 : (from t in db.Job_Card_Detail orderby t.Job_Card_Details_ID descending select t.Job_Card_Details_ID).First() + 1; int co_key = db.Client_Order_Detail.Count() == 0 ? 1 : (from t in db.Client_Order_Detail orderby t.Client_Order_Detail_ID descending select t.Client_Order_Detail_ID).First() + 1; foreach (JObject part in parts) { Client_Order_Detail cqd = new Client_Order_Detail(); cqd.Part_Type_ID = (int)part["Part_Type_ID"]; cqd.Quantity = (int)part["Quantity"]; cqd.Quantity_Delivered = 0; cqd.Part_Price = (decimal)part["Part_Price"]; cqd.Client_Discount_Rate = (double)part["Client_Discount_Rate"]; cqd.Client_Order_ID = key; cqd.Client_Order_Detail_ID = co_key; co_key++; db.Client_Order_Detail.Add(cqd); Job_Card_Detail jcd = new Job_Card_Detail(); jcd.Job_Card_Details_ID = detail_key; detail_key++; jcd.Part_Type_ID = (int)part["Part_Type_ID"]; jcd.Job_Card_ID = job_key; jcd.Non_Manual = false; jcd.Quantity = (int)part["Quantity"]; db.Job_Card_Detail.Add(jcd); db.SaveChanges(); //2. Handle child dependencies checkForChildren(jcd.Part_Type_ID, jcd.Quantity, job_key, "Ordering"); generateUniqueParts(jcd.Quantity, jcd.Part_Type_ID, 0, detail_key - 1, "Ordering"); } db.SaveChanges(); if (todo == "email") { Client_Contact_Person_Detail cl = new Client_Contact_Person_Detail(); cl = (from p in db.Client_Contact_Person_Detail where p.Contact_ID == co.Contact_ID select p).First(); string to = cl.Email_Address; string subject = "WME Order #" + key; String orderDate = co.Date.ToShortDateString(); string body = "Walter Meano Engineering Order #" + key + "\nThe order was placed on " + orderDate + "\n\nItems on Order:\n"; foreach (JObject part in parts) { Part_Type pt = new Part_Type(); int part_id = (int)part["Part_Type_ID"]; pt = (from p in db.Part_Type where p.Part_Type_ID == part_id select p).First(); body += pt.Abbreviation + " - " + pt.Name + "\t\tx" + (int)part["Quantity"] + "\tR " + (decimal)part["Part_Price"] + " per unit\n"; } Email.SendEmail(to, subject, body); } return("true|Order #" + key + " has been placed."); /* var return_message = sendSms((int)orderDetails["Contact_ID"], key); * * return return_message; * } * catch(Exception e) * { * ExceptionLog.LogException(e, "CustomerOrderController POST"); * return "false|An error has occured adding the Customer Order to the system."; * }*/ }
// POST: api/PartType public string Post(HttpRequestMessage value) { try { Model.Part_Type part = new Model.Part_Type(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject partDetails = (JObject)json["partType"]; JArray suppDetails = (JArray)json["supplier"]; JArray recipeDetails = (JArray)json["recipe"]; JArray manualDetails = (JArray)json["manual"]; JArray machineDetails = (JArray)json["machine"]; int key = db.Part_Type.Count() == 0 ? 1 : (from t in db.Part_Type orderby t.Part_Type_ID descending select t.Part_Type_ID).First() + 1; part.Part_Type_ID = key; part.Abbreviation = (string)partDetails["Abbreviation"]; part.Name = (string)partDetails["Name"]; part.Description = (string)partDetails["Description"]; part.Selling_Price = (decimal)partDetails["Selling_Price"]; part.Dimension = (string)partDetails["Dimension"]; part.Minimum_Level = (int)partDetails["Minimum_Level"]; part.Maximum_Level = (int)partDetails["Maximum_Level"]; part.Max_Discount_Rate = (int)partDetails["Max_Discount_Rate"]; part.Manufactured = Convert.ToBoolean(Convert.ToInt32(partDetails["Manufactured"])); part.Average_Completion_Time = (int)partDetails["Average_Completion_Time"]; string errorString = "false|"; bool error = false; if ((from t in db.Part_Type where t.Name == part.Name select t).Count() != 0) { error = true; errorString += "The Part Type name entered already exists on the system. "; } if ((from t in db.Part_Type where t.Abbreviation == part.Abbreviation select t).Count() != 0) { error = true; errorString += "The Part Type Abbreviation entered already exists on the system. "; } if (error) { return(errorString); } foreach (JObject supplier in suppDetails) { Part_Supplier ps = new Part_Supplier(); ps.Part_Type_ID = key; ps.Supplier_ID = (int)supplier["Supplier_ID"]; ps.Is_Prefered = Convert.ToBoolean(Convert.ToInt32(supplier["Is_Prefered"])); ps.unit_price = (decimal)supplier["unit_price"]; db.Part_Supplier.Add(ps); } int recipe_key = db.Recipes.Count() == 0 ? 1 : (from t in db.Recipes orderby t.Recipe_ID descending select t.Recipe_ID).First() + 1; int stages = 0; int runtime = 0; foreach (JObject item in recipeDetails) { Recipe rec = new Recipe(); recipe_key++; rec.Recipe_ID = recipe_key; rec.Part_Type_ID = key; rec.Stage_in_Manufacturing = (int)item["Stage_in_Manufacturing"]; rec.Quantity_Required = (int)item["Quantity_Required"]; rec.Item_ID = (int)item["resouce_ID"]; rec.Recipe_Type = (string)item["Recipe_Type"]; rec.Item_Name = (string)item["Item_Name"]; rec.Dimension = (String)item["Dimension"]; stages++; if ((string)item["Recipe_Type"] == "Part Type") { runtime += (from t in db.Part_Type where t.Part_Type_ID == rec.Item_ID select t.Average_Completion_Time).First(); } db.Recipes.Add(rec); } foreach (JObject manual in manualDetails) { Manual_Labour_Type_Part ml = new Manual_Labour_Type_Part(); ml.Manual_Labour_Type_ID = (int)manual["Manual_Labour_Type_ID"]; ml.Stage_In_Manufacturing = (int)manual["Stage_In_Manufacturing"]; ml.Part_Type_ID = key; stages++; runtime += (from t in db.Manual_Labour_Type where t.Manual_Labour_Type_ID == ml.Manual_Labour_Type_ID select t.Duration).First(); db.Manual_Labour_Type_Part.Add(ml); } foreach (JObject machine in machineDetails) { Machine_Part mach = new Machine_Part(); mach.Machine_ID = (int)machine["Machine_ID"]; mach.Stage_In_Manufacturing = (int)machine["Stage_In_Manufacturing"]; mach.Part_Type_ID = key; stages++; runtime += (from t in db.Machines where t.Machine_ID == mach.Machine_ID select t.Run_Time).First(); db.Machine_Part.Add(mach); } part.Number_Of_Stages = stages; part.Average_Completion_Time = runtime; db.Part_Type.Add(part); db.SaveChanges(); return("true|Part Type #" + key + " successfully added.|" + key); } catch (Exception e) { ExceptionLog.LogException(e, "PartTypeController POST"); return("false|An error has occured adding the Part Type to the system."); } }
// POST: api/SupplierQuote public string Post(HttpRequestMessage value) { try { Model.Supplier_Quote supplier = new Model.Supplier_Quote(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject supplierDetails = (JObject)json["quote"]; JArray componentDetails = (JArray)json["components"]; JArray partTypeDetails = (JArray)json["parts"]; JArray RawMaterialDetails = (JArray)json["raw"]; int key = db.Supplier_Quote.Count() == 0 ? 1 : (from t in db.Supplier_Quote orderby t.Supplier_Quote_ID descending select t.Supplier_Quote_ID).First() + 1; supplier.Supplier_Quote_ID = key; supplier.Supplier_Quote_Serial = (string)supplierDetails["Supplier_Quote_Reference"]; supplier.Supplier_Quote_Date = (DateTime)supplierDetails["Supplier_Quote_Date"]; supplier.Supplier_ID = (int)supplierDetails["Supplier_ID"]; supplier.Supplier_Quote_Expiry_Date = (DateTime)supplierDetails["Supplier_Quote_Expiry_Date"]; db.Supplier_Quote.Add(supplier); if (componentDetails != null) { foreach (JObject comp in componentDetails) { Supplier_Quote_Component cs = new Supplier_Quote_Component(); cs.Component_ID = (int)comp["Component_ID"]; cs.Supplier_Quote_ID = key; cs.Quantity_Requested = (int)comp["Quantity"]; cs.Price = (decimal)comp["Price"]; Component_Supplier comp2 = new Component_Supplier(); comp2 = (from d in db.Component_Supplier where d.Supplier_ID == supplier.Supplier_ID && d.Component_ID == cs.Component_ID select d).First(); comp2.unit_price = cs.Price; db.Supplier_Quote_Component.Add(cs); } } if (partTypeDetails != null) { foreach (JObject part in partTypeDetails) { Supplier_Quote_Detail_Part ps = new Supplier_Quote_Detail_Part(); ps.Part_Type_ID = (int)part["Part_Type_ID"]; ps.Supplier_Quote_ID = key; ps.Quantity = (int)part["Quantity"]; ps.Price = (decimal)part["Price"]; Part_Supplier part2 = new Part_Supplier(); part2 = (from d in db.Part_Supplier where d.Supplier_ID == supplier.Supplier_ID && d.Part_Type_ID == ps.Part_Type_ID select d).First(); part2.unit_price = ps.Price; db.Supplier_Quote_Detail_Part.Add(ps); } } if (RawMaterialDetails != null) { foreach (JObject raw in RawMaterialDetails) { Supplier_Quote_Detail_Raw_Material rms = new Supplier_Quote_Detail_Raw_Material(); rms.Raw_Material_ID = (int)raw["Raw_Material_ID"]; rms.Supplier_Quote_ID = key; rms.Quantity = (int)raw["Quantity"]; rms.Price = (decimal)raw["Price"]; rms.Dimension = (string)raw["Dimension"]; Raw_Material_Supplier raw2 = new Raw_Material_Supplier(); raw2 = (from d in db.Raw_Material_Supplier where d.Supplier_ID == supplier.Supplier_ID && d.Raw_Material_ID == rms.Raw_Material_ID select d).First(); raw2.unit_price = rms.Price; db.Supplier_Quote_Detail_Raw_Material.Add(rms); } } db.SaveChanges(); return("true|Supplier Quote #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "SupplierQuoteController POST"); return("false|An error has occured adding the Supplier Quote to the system."); } }
// POST: api/UniqueRawMaterial public string Post(HttpRequestMessage value) { try { Model.Unique_Raw_Material raw = new Model.Unique_Raw_Material(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject rawDetails = JObject.Parse(message); int key = db.Unique_Raw_Material.Count() == 0 ? 1 : (from t in db.Unique_Raw_Material orderby t.Unique_Raw_Material_ID descending select t.Unique_Raw_Material_ID).First() + 1; raw.Unique_Raw_Material_ID = key; raw.Raw_Material_ID = (int)rawDetails["Raw_Material_ID"]; raw.Dimension = (string)rawDetails["Dimension"]; raw.Quality = (string)rawDetails["Quality"]; raw.Date_Added = (DateTime)rawDetails["Date_Added"]; //raw.Date_Used = (DateTime)rawDetails["Date_Used"]; raw.Cost_Price = (decimal)rawDetails["Cost_Price"]; // raw.Supplier_Order_ID = (int)rawDetails["Supplier_Order_ID"]; if (rawDetails["Date_Used"] == null) { raw.Date_Used = null; } else { raw.Date_Used = (DateTime)rawDetails["Date_Used"]; } if ((int)rawDetails["Supplier_Order_ID"] == 0) { raw.Supplier_Order_ID = null; } else { raw.Supplier_Order_ID = (int)rawDetails["Supplier_Order_ID"]; } string errorString = "false|"; bool error = false; /*if ((from t in db.Supplier_Order * where t.Supplier_Order_ID == raw.Supplier_Order_ID * select t).Count() == 0) * { * error = true; * errorString += "The Supplier Order No. does not exist in the system. "; * }*/ if (error) { return(errorString); } db.Unique_Raw_Material.Add(raw); db.SaveChanges(); return("true|Unique Raw Material #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "UniqueRawMaterial POST"); return("false|An error has occured adding the Unique Raw Material to the system."); } }
public string Put(int id) { try { NameValueCollection nvc = HttpContext.Current.Request.Form; string ID = nvc["Part_Type_ID"]; string what = nvc["blueprints"]; JArray bb = JArray.Parse(what); int k = 0; //Check if any blueprints should be removed foreach (JObject blueprint in bb) { bool flag = (bool)blueprint["Removed"]; if (flag == true) { int b_ID = (int)blueprint["Blueprint_ID"]; string File_Type = (string)blueprint["File_Type"]; //Delete from db db.Part_Blueprint.RemoveRange(db.Part_Blueprint.Where(x => x.Blueprint_ID == b_ID)); //Delete physical file string newName = "Blueprint_" + b_ID + "_" + "PartType_" + ID + File_Type; string completePath = HttpContext.Current.Server.MapPath("~/Files/") + newName; if (System.IO.File.Exists(completePath)) { System.IO.File.Delete(completePath); } k++; } } int key = db.Part_Blueprint.Count() == 0 ? 1 : (from t in db.Part_Blueprint orderby t.Blueprint_ID descending select t.Blueprint_ID).First() + 1; for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { HttpPostedFileBase file = new HttpPostedFileWrapper(HttpContext.Current.Request.Files[i]); //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; string fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; Model.Part_Blueprint pb = new Part_Blueprint(); string newName = "Blueprint_" + key + "_" + "PartType_" + ID + GetDefaultExtension(mimeType); pb.Blueprint_ID = key; pb.File_Type = GetDefaultExtension(mimeType); pb.Name = fileName; pb.Part_Type_ID = Convert.ToInt32(ID); pb.location = "/Files/"; db.Part_Blueprint.Add(pb); //To save file, use SaveAs method file.SaveAs(HttpContext.Current.Server.MapPath("~/Files/") + newName); //File will be saved in application root key++; } db.SaveChanges(); return("true|Uploaded " + HttpContext.Current.Request.Files.Count + " files for part Type ID: " + ID + " and deleted " + k); } catch (DbEntityValidationException dbEx) { // Retrieve the error messages as a list of strings. var errorMessages = dbEx.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(dbEx.Message, " The validation errors are: ", fullErrorMessage); return("false|Could not upload the files.|" + exceptionMessage); } catch (Exception ex) { return("false|Could not upload the files.|" + ex.ToString()); } }
// POST: api/Employee Insert function public string Post(HttpRequestMessage value) { try { Model.Employee emp = new Model.Employee(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject empDetails = (JObject)json["emp"]; JArray machines = (JArray)json["machines"]; JArray labour = (JArray)json["manual_labour"]; int key = db.Employees.Count() == 0 ? 1 : (from t in db.Employees orderby t.Employee_ID descending select t.Employee_ID).First() + 1; emp.Employee_ID = key; emp.Name = (string)empDetails["name"]; emp.Surname = (string)empDetails["surname"]; emp.Employee_Type_ID = (int)empDetails["type"]; emp.Gender_ID = (string)empDetails["gender"]; emp.Email = (string)empDetails["email"]; emp.Contact_Number = (string)empDetails["contact_number"]; emp.Username = (string)empDetails["username"]; emp.Employee_Status = true; emp.ID_Number = (string)empDetails["ID"]; if ((string)empDetails["img"] != "") { string img = (string)empDetails["img"]; saveImage(img, Convert.ToString(key)); } string errorString = "false|"; bool error = false; if ((from t in db.Employees where t.ID_Number == emp.ID_Number select t).Count() != 0) { error = true; errorString += "The Employee ID Number entered already exists on the system. "; } if ((from t in db.Employees where t.Contact_Number == emp.Contact_Number select t).Count() != 0) { error = true; errorString += "The Employee Contact Number entered already exists on the system. "; } if ((from t in db.Employees where t.Email == emp.Email && t.Email != "" select t).Count() != 0) { error = true; errorString += "The Employee Email entered already exists on the system. "; } if ((from t in db.Employees where t.Username == emp.Username select t).Count() != 0) { error = true; errorString += "The Employee Username entered already exists on the system. "; } if (error) { return(errorString); } string salt = GetSalt(6); string password = (string)empDetails["password"]; string passwordHashed = sha256(password + salt); emp.Salt = salt; emp.Password = passwordHashed; db.Employees.Add(emp); foreach (int machine in machines) { Machine query = (from p in db.Machines where p.Machine_ID == machine select p).First(); emp.Machines.Add(query); } foreach (int man_lab in labour) { Manual_Labour_Type query = (from p in db.Manual_Labour_Type where p.Manual_Labour_Type_ID == man_lab select p).First(); emp.Manual_Labour_Type.Add(query); } db.SaveChanges(); return("true|Employee #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "EmployeeController POST"); return("false|An error has occured adding the Employee to the system."); //return e.ToString(); } }
private string generateProductionSchedule() { //*************************************** GET EVERYTHING SET UP //Armand: Check if production schedule exists for today bool flag = false; if (flag == true) { return("false|Production Schedule has already been generated."); } TimeSpan day_start = new TimeSpan(8, 0, 0); TimeSpan day_end = new TimeSpan(17, 0, 0); TimeSpan day_duration = day_end.Subtract(day_start); List <Machine> machines = (from p in db.Machines orderby p.Machine_ID select p).ToList(); List <Manual_Labour_Type> manual = (from p in db.Manual_Labour_Type orderby p.Name where p.Sub_Contractor == false select p).ToList(); List <Unique_Machine> um = (from p in db.Unique_Machine orderby p.Machine_ID select p).ToList(); int i = 1440; //Calculate the shortest amount of time. foreach (Machine m in machines) { if (i > m.Run_Time) { i = m.Run_Time; } } foreach (Manual_Labour_Type ml in manual) { if (i > ml.Duration) { i = ml.Duration; } } TimeSpan intervals = new TimeSpan(0, i, 0); int interval_count = Convert.ToInt32(day_duration.TotalMinutes / i); int num_of_resouces = (manual.Count + um.Count + 1); bool[,] grid = new bool[interval_count, num_of_resouces]; //Set all cells to false for (int aa = 0; aa < interval_count; aa++) { for (int bb = 0; bb < num_of_resouces; bb++) { grid[aa, bb] = false; } } string[] resouce_pos = new string[num_of_resouces]; for (int k = 0; k < resouce_pos.Length; k++) { if (k < manual.Count) { resouce_pos[k] = "Manual|" + manual[k].Manual_Labour_Type_ID + "|" + manual[k].Name; } else { resouce_pos[k] = "Machine|" + um[k].Unique_Machine_ID + "|" + um[k].Machine_ID; } } //*************************************** Create production schedule //Armand: Insert a new entry in the production_schedule table. Remeber to save //the intervals as well Production_Schedule ps = new Production_Schedule(); int key = db.Machines.Count() == 0 ? 1 : (from t in db.Production_Schedule orderby t.Production_Schedule_ID descending select t.Production_Schedule_ID).First() + 1; ps.Production_Schedule_ID = key; ps.Production_Schedule_Date = DateTime.Now; ps.intervals = intervals.Ticks; db.Production_Schedule.Add(ps); db.SaveChanges(); //*************************************** Create Part Queue //Armand : Get all the uncompleted job cards order by priority then by date. Client //job cards are more important List <Job_Card> cards = (from p in db.Job_Card orderby p.Job_Card_Priority_ID, p.Job_Card_Date select p).ToList(); //Make an empty queue Queue <ProductionPart> parts = new Queue <ProductionPart>(); foreach (Job_Card jc in cards) { if (jc.Job_Card_Status_ID == 1) { //Armand : IF job card has a status of started then update to in-production Job_Card jcTemp = new Job_Card(); jcTemp = (from p in db.Job_Card where p.Job_Card_ID == jc.Job_Card_ID select p).First(); jcTemp.Job_Card_Status_ID = 2; db.SaveChanges(); } //Armand: Create a list of parts that are tied to this job card. List <Part> pp = new List <Part>(); foreach (Job_Card_Detail jcd in jc.Job_Card_Detail) { List <Part> tmp = (from p in db.Parts where p.Job_Card_Detail.Contains(jcd) select p).ToList(); pp = pp.Concat(tmp).ToList(); } foreach (Part p in pp) { //Create a new production part ProductionPart temp = new ProductionPart(); //Populate with data from the database where p.part_type_ID for part type and //p.Part_ID for part but you can probably just assign p to temp.part temp.part = p; temp.part_type = p.Part_Type; temp.job_card_ID = jc.Job_Card_ID; string[] resourceNeeded = new string[temp.part_type.Machine_Part.Count + temp.part_type.Manual_Labour_Type_Part.Count + 1]; //Create the array of stages we need to go through foreach (Machine_Part m in temp.part_type.Machine_Part) { resourceNeeded[m.Stage_In_Manufacturing] = "Machine|" + m.Machine_ID; } foreach (Manual_Labour_Type_Part m in temp.part_type.Manual_Labour_Type_Part) { resourceNeeded[m.Stage_In_Manufacturing] = "Manual|" + m.Manual_Labour_Type_ID; } temp.partResources = resourceNeeded; parts.Enqueue(temp); } } //*************************************** Start scheduling bool scheduleFull = false; while (parts.Count != 0 && scheduleFull == false) { ProductionPart pp = parts.Dequeue(); for (int s = pp.part.Part_Stage; s <= pp.part_type.Number_Of_Stages; s++) { //Find the recipe entry; Recipe r = new Recipe(); for (int k = 0; k < pp.part_type.Recipes.Count; k++) { if (pp.part_type.Recipes.ElementAt(k).Stage_in_Manufacturing == s) { r = pp.part_type.Recipes.ElementAt(k); } } //Does this stage need another part? if (r.Recipe_Type == "Part Type") { //If no child is available then //skip the remained of the steps if (checkForChildren(pp.part.Part_ID, r.Item_ID, r.Quantity_Required) == false) { continue; } } //resouceneeded[0] == type and [1] == ID string[] resouceNeeded = pp.partResources[s].Split('|'); bool scheduled = false; if (resouceNeeded[0] == "Machine") { for (int k = 0; k < resouce_pos.Length && scheduled == false; k++) { string[] resouce = resouce_pos[k].Split('|'); int duration = 0; if (resouce[0] == "Machine") { int machine_ID = Convert.ToInt32(resouce[2]); int machine_ID_needed = Convert.ToInt32(resouceNeeded[1]); //If we have found a match if (machine_ID == machine_ID_needed) { //Get the duration for (int a = 0; a < machines.Count; a++) { if (machines[a].Machine_ID == machine_ID) { duration = machines[a].Run_Time; } } } } else //it's a Manual Labour { int manual_ID = Convert.ToInt32(resouce[1]); bool sub_contractor = false; //Is this manual labour for a sub-contractor? for (int a = 0; a < manual.Count; a++) { if (manual[a].Manual_Labour_Type_ID == manual_ID) { sub_contractor = manual[a].Sub_Contractor; } } if (sub_contractor == true) //skip the remaining steps { break; } for (int a = 0; a < manual.Count; a++) { if (manual[a].Manual_Labour_Type_ID == manual_ID) { duration = manual[a].Duration; } } } //i is the interval. So if i= 18 then //round up to the nearest factor of 18 duration = RoundUp(duration, i); //How many cells will this occupy? int cellcount = duration / i; //Grid[rows,cols] //k = the pos of the current machine we are working with //Find some space bool space = true; int space_index_start = 0; for (int b = 0; b < interval_count; b++) { for (int g = b; g < cellcount + b; g++) { if (grid[g, k] == true) { space = false; } } //If there is no space in the n spaces then skip them //and continue down the column until space is found else //move on to next resouce. if (space == false) { b = b + cellcount; } else { space_index_start = b; break; } } //Occupy the space in the grid if (space == true) { for (int b = space_index_start; b < b + cellcount; b++) { grid[b, k] = true; } TimeSpan minutes = new TimeSpan(0, space_index_start, 0); TimeSpan start_time = day_start.Add(minutes); minutes = new TimeSpan(0, duration, 0); TimeSpan end_time = start_time.Add(minutes); //Armand: FInd an eligible employee Employee emp = new Employee(); int resource_id; if (resouce[0] == "Machine") { resource_id = Convert.ToInt32(resouce[2]); Machine m = new Machine(); m = (from p in db.Machines where p.Machine_ID == resource_id select p).First(); emp = (from d in db.Employees where d.Machines.Contains(m) select d).FirstOrDefault(); } else { resource_id = Convert.ToInt32(resouce[1]); Manual_Labour_Type m = new Manual_Labour_Type(); m = (from p in db.Manual_Labour_Type where p.Manual_Labour_Type_ID == resource_id select p).First(); emp = (from d in db.Employees where d.Manual_Labour_Type.Contains(m) select d).FirstOrDefault(); } int employee_ID = emp.Employee_ID; //Then create a new task //part_stage = s //resouce_type = resource[0] Production_Task pt = new Production_Task(); int prod_task_key = db.Production_Task.Count() == 0 ? 1 : (from t in db.Production_Task orderby t.Production_Task_ID descending select t.Production_Task_ID).First() + 1; pt.Production_Task_ID = prod_task_key; pt.Production_Task_Type = resouce[0]; pt.start_time = start_time; pt.end_time = end_time; pt.Part_Stage = s; pt.Production_Schedule_ID = key; pt.Employee_ID = employee_ID; pt.complete = false; pt.Part_ID = pp.part.Part_ID; pt.Resource_ID = resource_id; pt.Job_Card_ID = pp.job_card_ID; pt.duration = pp.; scheduled = true; break; } } } } } return("True|Production Schedule has been generated."); }
// POST: api/SupplierOrder public string Post(HttpRequestMessage value) { try { Model.Supplier_Order supplier = new Model.Supplier_Order(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject orderDetails = (JObject)json["order"]; string type = (string)json["type"]; string action = (string)json["action"]; int key = db.Supplier_Order.Count() == 0 ? 1 : (from t in db.Supplier_Order orderby t.Supplier_Order_ID descending select t.Supplier_Order_ID).First() + 1; supplier.Supplier_Order_ID = key; supplier.Supplier_Order_Status_ID = (int)orderDetails["Supplier_Order_Status_ID"]; supplier.Date = (DateTime)orderDetails["Date"]; supplier.Supplier_ID = (int)orderDetails["Supplier_ID"]; db.Supplier_Order.Add(supplier); if (type == "AdHoc") { JArray componentDetails = (JArray)json["components"]; JArray partTypeDetails = (JArray)json["parts"]; JArray RawMaterialDetails = (JArray)json["raw"]; if (componentDetails != null) { foreach (JObject comp in componentDetails) { Supplier_Order_Component cs = new Supplier_Order_Component(); cs.Component_ID = (int)comp["Component_ID"]; cs.Supplier_Order_ID = key; cs.Quantity_Requested = (int)comp["Quantity_Requested"]; cs.Quantity_Received = 0; cs.Price = (decimal)comp["Price"]; db.Supplier_Order_Component.Add(cs); } } if (partTypeDetails != null) { foreach (JObject part in partTypeDetails) { Supplier_Order_Detail_Part ps = new Supplier_Order_Detail_Part(); ps.Part_Type_ID = (int)part["Part_Type_ID"]; ps.Supplier_Order_ID = key; ps.Quantity = (int)part["Quantity"]; ps.Quantity_Received = 0; ps.Price = (decimal)part["Price"]; db.Supplier_Order_Detail_Part.Add(ps); } } if (RawMaterialDetails != null) { foreach (JObject raw in RawMaterialDetails) { Supplier_Order_Detail_Raw_Material rms = new Supplier_Order_Detail_Raw_Material(); rms.Raw_Material_ID = (int)raw["Raw_Material_ID"]; rms.Supplier_Order_ID = key; rms.Quantity = (int)raw["Quantity"]; rms.Price = (decimal)raw["Price"]; rms.Quantity_Received = 0; rms.Dimensions = (string)raw["Dimensions"]; db.Supplier_Order_Detail_Raw_Material.Add(rms); } } } else { JArray componentDetails = (JArray)json["components"]; JArray partTypeDetails = (JArray)json["parts"]; JArray RawMaterialDetails = (JArray)json["raw"]; int quoteID = (int)orderDetails["Supplier_Quote_ID"]; Supplier_Quote sq = new Supplier_Quote(); sq = (from p in db.Supplier_Quote where p.Supplier_Quote_ID == quoteID select p).First(); supplier.Supplier_Quote.Add(sq); if (componentDetails != null) { foreach (JObject comp in componentDetails) { Supplier_Order_Component cs = new Supplier_Order_Component(); cs.Component_ID = (int)comp["Component_ID"]; cs.Supplier_Order_ID = key; cs.Quantity_Requested = (int)comp["Quantity_Requested"]; cs.Quantity_Received = 0; cs.Price = (decimal)comp["Price"]; db.Supplier_Order_Component.Add(cs); } } if (partTypeDetails != null) { foreach (JObject part in partTypeDetails) { Supplier_Order_Detail_Part ps = new Supplier_Order_Detail_Part(); ps.Part_Type_ID = (int)part["Part_Type_ID"]; ps.Supplier_Order_ID = key; ps.Quantity = (int)part["Quantity"]; ps.Quantity_Received = 0; ps.Price = (decimal)part["Price"]; db.Supplier_Order_Detail_Part.Add(ps); } } if (RawMaterialDetails != null) { foreach (JObject raw in RawMaterialDetails) { Supplier_Order_Detail_Raw_Material rms = new Supplier_Order_Detail_Raw_Material(); rms.Raw_Material_ID = (int)raw["Raw_Material_ID"]; rms.Supplier_Order_ID = key; rms.Quantity = (int)raw["Quantity"]; rms.Price = (decimal)raw["Price"]; rms.Quantity_Received = 0; rms.Dimensions = (string)raw["Dimensions"]; db.Supplier_Order_Detail_Raw_Material.Add(rms); } } } db.SaveChanges(); if (action == "email") { JArray componentDetails = (JArray)json["components"]; JArray partTypeDetails = (JArray)json["parts"]; JArray RawMaterialDetails = (JArray)json["raw"]; Model.Supplier sup = new Model.Supplier(); sup = (from p in db.Suppliers where p.Supplier_ID == supplier.Supplier_ID select p).First(); string to = sup.Email; string subject = "WME Supplier Order #" + key; String orderDate = supplier.Date.ToShortDateString(); string body = "Walter Meano Engineering Supplier Order #" + key + "\nThe order was placed on " + orderDate + "\n\nItems in Order:\n"; if (componentDetails != null) { foreach (JObject comp in componentDetails) { Component cs = new Component(); int comp_id = (int)comp["Component_ID"]; cs = (from p in db.Components where p.Component_ID == comp_id select p).First(); body += cs.Name + "\t\tx" + (int)comp["Quantity_Requested"] + "\n"; } } if (partTypeDetails != null) { foreach (JObject part in partTypeDetails) { Part_Type pt = new Part_Type(); int part_id = (int)part["Part_Type_ID"]; pt = (from p in db.Part_Type where p.Part_Type_ID == part_id select p).First(); body += pt.Name + "\t\tx" + (int)part["Quantity"] + "\n"; } } if (RawMaterialDetails != null) { foreach (JObject raw in RawMaterialDetails) { Raw_Material raw2 = new Raw_Material(); int raw_id = (int)raw["Raw_Material_ID"]; raw2 = (from p in db.Raw_Material where p.Raw_Material_ID == raw_id select p).First(); body += raw2.Name + "\t\tx" + (int)raw["Quantity"] + "\n"; } } Email.SendEmail(to, subject, body); } return("true|Supplier Purchase Order #" + key + " successfully added."); } catch (Exception e) { ExceptionLog.LogException(e, "SupplierOrderController POST"); return("false|An error has occured adding the Supplier Order to the system."); } }
// POST: api/DeliveryNote public string Post(HttpRequestMessage value) { try { Model.Delivery_Note dn = new Model.Delivery_Note(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JArray parts = (JArray)json["details"]; int key = db.Delivery_Note.Count() == 0 ? 1 : (from t in db.Delivery_Note orderby t.Delivery_Note_ID descending select t.Delivery_Note_ID).First() + 1; dn.Delivery_Note_ID = key; dn.Delivery_Note_Date = (DateTime)json["Delivery_Note_Date"]; dn.Client_Order_ID = (int)json["Client_Order_ID"]; string action = (string)json["action"]; db.Delivery_Note.Add(dn); foreach (JObject part in parts) { Delivery_Note_Details cod = new Delivery_Note_Details(); cod.Quantity_Delivered = (int)part["Quantity_Delivered"]; cod.Delivery_Note_ID = key; cod.Client_Order_Detail_ID = (int)part["Client_Order_Detail_ID"]; Client_Order_Detail cod2 = new Client_Order_Detail(); cod2 = (from d in db.Client_Order_Detail where d.Client_Order_Detail_ID == cod.Client_Order_Detail_ID select d).First(); cod2.Quantity_Delivered += cod.Quantity_Delivered; db.Delivery_Note_Details.Add(cod); } if (action == "email") { Client_Contact_Person_Detail cl = new Client_Contact_Person_Detail(); string to = (string)json["email"]; string subject = "WME Delivery Note #" + key; String orderDate = dn.Delivery_Note_Date.ToShortDateString(); string body = "Walter Meano Engineering Delivery Note #" + key + "\nThe delivery note was generated on " + orderDate + "\n\nItems delivered:\n"; foreach (JObject part in parts) { body += (string)part["Part_Type_Name"] + "\t\tx" + (int)part["Quantity_Delivered"] + "\n"; } Email.SendEmail(to, subject, body); } db.SaveChanges(); return("true|Delivery Note #" + key + " successfully added.|" + key); } catch (Exception e) { ExceptionLog.LogException(e, "DeliveryNoteController POST"); return("false|An error has occured adding the Delivery Note to the system."); } }
// POST: api/PartType public string Post(HttpRequestMessage value) { // try // { Model.Part_Type part = new Model.Part_Type(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject partDetails = (JObject)json["partType"]; JArray suppDetails = (JArray)json["supplier"]; JArray recipeDetails = (JArray)json["recipe"]; JArray manualDetails = (JArray)json["manual"]; JArray machineDetails = (JArray)json["machine"]; int key = db.Part_Type.Count() == 0 ? 1 : (from t in db.Part_Type orderby t.Part_Type_ID descending select t.Part_Type_ID).First() + 1; part.Part_Type_ID = key; part.Abbreviation = (string)partDetails["Abbreviation"]; part.Name = (string)partDetails["Name"]; part.Description = (string)partDetails["Description"]; part.Selling_Price = (decimal)partDetails["Selling_Price"]; part.Dimension = (string)partDetails["Dimension"]; part.Minimum_Level = (int)partDetails["Minimum_Level"]; part.Maximum_Level = (int)partDetails["Maximum_Level"]; part.Max_Discount_Rate = (int)partDetails["Max_Discount_Rate"]; part.Manufactured = Convert.ToBoolean(Convert.ToInt32(partDetails["Manufactured"])); part.Average_Completion_Time = (int)partDetails["Average_Completion_Time"]; string errorString = "false|"; bool error = false; if ((from t in db.Part_Type where t.Name == part.Name select t).Count() != 0) { error = true; errorString += "The Part Type name entered already exists on the system. "; } if (error) { return(errorString); } db.Part_Type.Add(part); foreach (JObject supplier in suppDetails) { Part_Supplier ps = new Part_Supplier(); ps.Part_Type_ID = key; ps.Supplier_ID = (int)supplier["Supplier_ID"]; ps.Is_Prefered = Convert.ToBoolean(Convert.ToInt32(supplier["Is_Prefered"])); ps.unit_price = (decimal)supplier["unit_price"]; db.Part_Supplier.Add(ps); } db.SaveChanges(); return("true|Part Type successfully added."); /* } * catch * { * return "false|An error has occured adding the Raw Material to the system."; * } */ }