// POST: api/CustomerQuote public string Post(HttpRequestMessage value) { try { Model.Client_Quote client = new Model.Client_Quote(); string message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5); JObject json = JObject.Parse(message); JObject clientDetails = (JObject)json["client"]; JArray parts = (JArray)clientDetails["details"]; string action = (string)json["action"]; int key = db.Client_Quote.Count() == 0 ? 1 : (from t in db.Client_Quote orderby t.Client_Quote_ID descending select t.Client_Quote_ID).First() + 1; client.Client_Quote_ID = key; client.Client_ID = (int)clientDetails["Client_ID"]; client.Contact_ID = (int)clientDetails["Contact_ID"]; client.Date = (DateTime)clientDetails["Date"]; client.Client_Quote_Expiry_Date = (DateTime)clientDetails["Client_Quote_Expiry_Date"]; db.Client_Quote.Add(client); foreach (JObject part in parts) { Client_Quote_Detail cqd = new Client_Quote_Detail(); cqd.Quantity = (int)part["Quantity"]; cqd.Part_Price = (decimal)part["Part_Price"]; cqd.Client_Discount_Rate = (double)part["Client_Discount_Rate"]; cqd.Settlement_Discount_Rate = (double)clientDetails["Settlement_Discount_Rate"]; cqd.Client_Quote_ID = key; cqd.Part_Type_ID = (int)part["Part_Type_ID"]; db.Client_Quote_Detail.Add(cqd); } bool flag = true; if (action == "email") { Client_Contact_Person_Detail cl = new Client_Contact_Person_Detail(); cl = (from p in db.Client_Contact_Person_Detail where p.Contact_ID == client.Contact_ID select p).First(); string to = cl.Email_Address; string subject = "WME Quote #" + key; DateTime expiry = (DateTime)clientDetails["Client_Quote_Expiry_Date"]; string body = "Walter Meano Engineering Quote #" + key + "\nThe quote is valid until " + expiry.ToShortDateString() + "\n\nItems on Quote:\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"; } flag = Email.SendEmail(to, subject, body); } db.SaveChanges(); string s = ""; if (flag == false) { s += "If the quote has not been emailed successfully you will receive an email notifying you of this in a few minutes."; } return("true|Customer Quote #" + key + " successfully added. " + s + "|" + key); } catch (Exception e) { ExceptionLog.LogException(e, "CustomerQuoteController POST"); return("false|An error has occured adding the Customer Quote 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."); } }
// PUT: api/Customer/5 public string Put(int id, HttpRequestMessage value) { try { Client client = new Client(); client = (from p in db.Clients where p.Client_ID == id select p).First(); 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"]; client.Name = (string)clientDetails["Name"]; client.Address = (string)clientDetails["Address"]; client.City = (string)clientDetails["City"]; client.Zip = (string)clientDetails["Zip"]; 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 && t.Client_ID != id 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 && t.Client_ID != id 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) && t.Client_ID != id 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 != "") && t.Client_ID != id 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 && t.Client_ID != id select t).Count() != 0) { error = true; errorString += "The Customer VAT Number entered already exists on the system. "; } if (error) { return(errorString); } int contact_id = 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; List <Client_Contact_Person_Detail> clientUpdated = new List <Client_Contact_Person_Detail>(); foreach (JObject contact in contactDetails) { string email = (string)contact["Email_Address"]; Client_Contact_Person_Detail ccpd = new Client_Contact_Person_Detail(); ccpd = (from p in db.Client_Contact_Person_Detail where p.Email_Address == email select p).FirstOrDefault(); if (ccpd != null) { 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 && t.Contact_ID != ccpd.Contact_ID 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 && t.Contact_ID != ccpd.Contact_ID select t).Count() != 0) { error = true; errorString += "The Customer Contact Number for " + ccpd.Name + " entered already exists on the system. "; } if (error) { return(errorString); } clientUpdated.Add(ccpd); } else { ccpd = new Client_Contact_Person_Detail(); ccpd.Client_ID = id; ccpd.Contact_ID = contact_id; contact_id++; 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); clientUpdated.Add(ccpd); } } List <Client_Contact_Person_Detail> clientContacts = new List <Client_Contact_Person_Detail>(); clientContacts = (from p in db.Client_Contact_Person_Detail where p.Client_ID == client.Client_ID select p).ToList(); foreach (Client_Contact_Person_Detail clientC in clientContacts) { if (!clientUpdated.Contains(clientC)) { try { var itemToRemove = db.Client_Contact_Person_Detail.SingleOrDefault(x => x.Contact_ID == clientC.Contact_ID); if (itemToRemove != null) { db.Client_Contact_Person_Detail.Remove(itemToRemove); db.SaveChanges(); } return("true|The Customer Contact " + clientC.Name + " has successfully been removed from the system."); } catch { return("false|The Customer Contact " + clientC.Name + " is in use and cannot be removed from the system."); } } } db.Client_Discount_Rate.RemoveRange(db.Client_Discount_Rate.Where(x => x.Client_ID == id)); foreach (JObject part in partDiscounts) { Client_Discount_Rate cdr = new Client_Discount_Rate(); cdr.Client_ID = client.Client_ID; 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 successfully updated."); } catch (Exception e) { ExceptionLog.LogException(e, "CustomerController PUT"); return("false|An error has occured updating the Customer on 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/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."); } }