// PUT: api/ManualLabour/5
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                Manual_Labour_Type mlt = new Manual_Labour_Type();
                mlt = (from p in db.Manual_Labour_Type
                       where p.Manual_Labour_Type_ID == id
                       select p).First();

                string  message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5);
                JObject json    = JObject.Parse(message);

                string errorString = "false|";
                bool   error       = false;

                if ((from t in db.Manual_Labour_Type
                     where t.Name == mlt.Name && t.Manual_Labour_Type_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Manual Labour Type name entered already exists on the system. ";
                }

                if (error)
                {
                    return(errorString);
                }

                mlt.Name           = (string)json["Name"];
                mlt.Description    = (string)json["Description"];
                mlt.Duration       = (int)json["Duration"];
                mlt.Sub_Contractor = Convert.ToBoolean((string)json["Sub_Contractor"]);

                db.SaveChanges();
                return("true|Manual Labour Type successfully updated.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "ManualLabourController PUT");
                return("false|An error has occured updating the Manual Labour Type on the system.");
            }
        }
示例#2
0
        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.");
        }
        // PUT: api/Employee/5 Update Function
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                Model.Employee emp = new Model.Employee();
                emp = (from p in db.Employees
                       where p.Employee_ID == id
                       select p).First();

                string  message    = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5);
                JObject json       = JObject.Parse(message);
                JObject empDetails = (JObject)json["employee"];
                JArray  machines   = (JArray)json["machines"];
                JArray  labour     = (JArray)json["manual_labour"];

                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"] != "")
                {
                    db.Employee_Photo.RemoveRange(db.Employee_Photo.Where(x => x.Employee_ID == id));
                    string img = (string)empDetails["img"];
                    saveImage(img, Convert.ToString(id));
                }



                if ((string)empDetails["password"] != "")
                {
                    string salt     = GetSalt(6);
                    string password = (string)empDetails["password"];

                    string passwordHashed = sha256(password + salt);
                    emp.Salt     = salt;
                    emp.Password = passwordHashed;
                }

                string errorString = "false|";
                bool   error       = false;

                if ((from t in db.Employees
                     where t.ID_Number == emp.ID_Number && t.Employee_ID != id
                     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.Email == emp.Email && t.Employee_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Employee Email entered already exists on the system. ";
                }

                if ((from t in db.Employees
                     where t.Contact_Number == emp.Contact_Number && t.Employee_ID != id
                     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.Username == emp.Username && t.Employee_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Employee Username entered already exists on the system. ";
                }

                if (error)
                {
                    return(errorString);
                }

                emp.Machines.Clear();

                foreach (int machine in machines)
                {
                    Machine query = (from p in db.Machines
                                     where p.Machine_ID == machine
                                     select p).First();
                    emp.Machines.Add(query);
                }

                emp.Manual_Labour_Type.Clear();

                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 successfully updated.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "EmployeeController PUT");
                return("false|An error has occured updating the Employee on the system.");
            }
        }