// PUT: api/Supplier/5
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                Model.Supplier supplier = new Model.Supplier();
                supplier = (from p in db.Suppliers
                            where p.Supplier_ID == id
                            select p).First();

                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"];

                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(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 && t.Supplier_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Supplier name entered already exists on the system. ";
                }

                if ((from t in db.Suppliers
                     where t.Contact_Name == supplier.Contact_Name && t.Supplier_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Contact Name entered already exists on the system. ";
                }

                if ((from t in db.Suppliers
                     where t.Email == supplier.Email && t.Supplier_ID != id
                     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 && t.Supplier_ID != id
                     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) && t.Supplier_ID != id
                     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) && t.Supplier_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Supplier Banking details entered already exists on the system. ";
                }

                if (error)
                {
                    return(errorString);
                }

                db.Component_Supplier.RemoveRange(db.Component_Supplier.Where(x => x.Supplier_ID == id));

                if (componentDetails != null)
                {
                    foreach (JObject comp in componentDetails)
                    {
                        Component_Supplier cs = new Component_Supplier();
                        cs.Component_ID = (int)comp["Component_ID"];
                        cs.Supplier_ID  = id;
                        cs.is_preferred = Convert.ToBoolean(Convert.ToInt32(comp["Is_Prefered"]));
                        cs.unit_price   = (decimal)comp["unit_price"];

                        db.Component_Supplier.Add(cs);
                    }
                }

                db.Part_Supplier.RemoveRange(db.Part_Supplier.Where(x => x.Supplier_ID == id));

                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  = id;
                        ps.Is_Prefered  = Convert.ToBoolean(Convert.ToInt32(part["Is_Prefered"]));
                        ps.unit_price   = (decimal)part["unit_price"];

                        db.Part_Supplier.Add(ps);
                    }
                }

                db.Raw_Material_Supplier.RemoveRange(db.Raw_Material_Supplier.Where(x => x.Supplier_ID == id));

                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     = id;
                        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 successfully updated.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "SupplierController PUT");
                return("false|An error has occured updating the Suplier on 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.");
            }
        }
        // PUT: api/PartType/5
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                Model.Part_Type part = new Model.Part_Type();
                part = (from p in db.Part_Type
                        where p.Part_Type_ID == id
                        select p).First();

                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"];

                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 && t.Part_Type_ID != id
                     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 && t.Part_Type_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Part Type Abbreviation entered already exists on the system. ";
                }

                if (error)
                {
                    return(errorString);
                }

                db.Part_Supplier.RemoveRange(db.Part_Supplier.Where(x => x.Part_Type_ID == id));

                foreach (JObject supplier in suppDetails)
                {
                    Part_Supplier ps = new Part_Supplier();
                    ps.Part_Type_ID = id;
                    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;
                db.Recipes.RemoveRange(db.Recipes.Where(x => x.Part_Type_ID == id));

                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           = id;
                    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);
                }

                db.Manual_Labour_Type_Part.RemoveRange(db.Manual_Labour_Type_Part.Where(x => x.Part_Type_ID == id));
                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           = id;
                    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);
                }

                db.Machine_Part.RemoveRange(db.Machine_Part.Where(x => x.Part_Type_ID == id));
                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           = id;
                    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.SaveChanges();
                return("true|Part Type successfully updated.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "PartTypeController PUT");
                return("false|An error has occured updating the Part Type on the system.");
            }
        }
Beispiel #4
0
        // 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.";
             *      } */
        }