// PUT: api/UniqueMachine/5
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                Unique_Machine mach = new Unique_Machine();

                mach = (from p in db.Unique_Machine
                        where p.Unique_Machine_ID == id
                        select p).First();

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

                mach.Machine_ID            = (int)machineDetails["Machine_ID"];
                mach.Unique_Machine_Serial = (string)machineDetails["Unique_Machine_Serial"];
                mach.Machine_Status_ID     = (int)machineDetails["Machine_Status_ID"];

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

                if ((from t in db.Unique_Machine
                     where t.Unique_Machine_Serial == mach.Unique_Machine_Serial && t.Unique_Machine_ID != id
                     select t).Count() != 0)
                {
                    error        = true;
                    errorString += "The Unique Machine serial number entered already exists on the system. ";
                }

                if (error)
                {
                    return(errorString);
                }

                db.SaveChanges();
                return("true|Unique Machine successfully updated.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "UniqueMachineController PUT");
                return("false|An error has occured updating the Unique Machine on the system.");
            }
        }
示例#2
0
        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();
                                }
                            }
                        }
                    }
                }
            }
        }