// 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.");
            }
        }
Exemple #2
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.";
             *      } */
        }