// POST: api/JobCard
        public string Post(HttpRequestMessage value)
        {
            try
            {
                string  message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5);
                JObject json    = JObject.Parse(message);

                JArray partDetails = (JArray)json["details"];

                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)json["Job_Card_Date"];
                jc.Job_Card_Status_ID   = (int)json["Job_Card_Status_ID"];
                jc.Job_Card_Priority_ID = 2;

                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;

                foreach (JObject part in partDetails)
                {
                    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   = Convert.ToBoolean((string)part["Non_Manual"]);
                    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, "Manufacturing");

                    generateUniqueParts(jcd.Quantity, jcd.Part_Type_ID, 0, detail_key - 1, "Manufacturing");
                }

                return("true|Job Card #" + job_key + " successfully added.");
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "JobCardController POST");
                return("false|An error has occured adding the Job Card to the system.");
            }
        }
Example #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.");
        }
Example #3
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();
                                }
                            }
                        }
                    }
                }
            }
        }
        // PUT: api/JobCard/5
        public string Put(int id, HttpRequestMessage value)
        {
            try
            {
                string  message = HttpContext.Current.Server.UrlDecode(value.Content.ReadAsStringAsync().Result).Substring(5);
                JObject json    = JObject.Parse(message);

                JArray partDetails = (JArray)json["details"];


                Job_Card jc = new Job_Card();
                jc = (from p in db.Job_Card
                      where p.Job_Card_ID == id
                      select p).First();

                int old_status = jc.Job_Card_Status_ID;
                jc.Job_Card_Status_ID = (int)json["Job_Card_Status_ID"];

                if (jc.Job_Card_Status_ID == 4) //if job card is being cancelled.
                {
                    if (old_status == 1)
                    {
                        jc.Job_Card_Status_ID = 4;
                    }
                    else
                    {
                        return("false|The Job Card is already in production, thus it cannot be cancelled.");
                    }

                    List <Job_Card_Detail> details = (from d in db.Job_Card_Detail
                                                      where d.Job_Card_ID == jc.Job_Card_ID
                                                      select d).ToList();

                    foreach (Job_Card_Detail det in details)
                    {
                        List <Part> parts = (from d in db.Parts
                                             where d.Job_Card_Detail.Contains(db.Job_Card_Detail.Where(x => x.Job_Card_Details_ID == det.Job_Card_Details_ID).FirstOrDefault()) &&
                                             (d.Part_Status_ID == 1 || d.Part_Status_ID == 2)
                                             select d).ToList();

                        foreach (Part p in parts)
                        {
                            p.Part_Status_ID = 5;
                        }
                    }

                    db.SaveChanges();
                    return("true|The Job Card has been cancelled.");
                }
                else if (jc.Job_Card_Status_ID == 3) //if job card is being set to complete.
                {
                    List <Job_Card_Detail> details = (from d in db.Job_Card_Detail
                                                      where d.Job_Card_ID == jc.Job_Card_ID
                                                      select d).ToList();

                    foreach (Job_Card_Detail det in details)
                    {
                        List <Part> parts = (from d in db.Parts
                                             where d.Job_Card_Detail.Contains(db.Job_Card_Detail.Where(x => x.Job_Card_Details_ID == det.Job_Card_Details_ID).FirstOrDefault()) &&
                                             (d.Part_Status_ID == 1 || d.Part_Status_ID == 2) && d.Parent_ID == 0
                                             select d).ToList();

                        foreach (Part p in parts)
                        {
                            p.Part_Status_ID = 3;

                            updateChildren(p.Part_ID, 2, jc.Job_Card_ID); //2 is for Complete.
                        }
                    }

                    db.SaveChanges();
                    return("True|Job Card has been set to completed.");
                }
                else //Only some parts are being removed
                {
                    foreach (JObject jcd in partDetails)
                    {
                        int remove_quantity = (int)jcd["Remove_Quantity"];
                        if (remove_quantity > 0)
                        {
                            List <Job_Card_Detail> details = (from d in db.Job_Card_Detail
                                                              where d.Job_Card_ID == jc.Job_Card_ID
                                                              select d).ToList();

                            foreach (Job_Card_Detail det in details)
                            {
                                int part_type_ID = (int)jcd["Part_Type_ID"];
                                //(Armand) Get all the parts of the above part type tied to the job card
                                //and order them by putting the last added part first
                                List <Part> parts = (from d in db.Parts
                                                     where d.Job_Card_Detail.Contains(db.Job_Card_Detail.Where(x => x.Job_Card_Details_ID == det.Job_Card_Details_ID).FirstOrDefault()) &&
                                                     d.Part_Type_ID == part_type_ID
                                                     select d).ToList();

                                int i = 0;
                                foreach (Part p in parts)
                                {
                                    if (p.Part_Status_ID == 1) //If the part is still in raw state but not in production
                                    {
                                        p.Part_Status_ID = 5;

                                        db.Parts.Where(x => x.Part_ID == p.Part_ID).First().Job_Card_Detail.Remove(det);

                                        i++;
                                    }

                                    det.Quantity = det.Quantity - remove_quantity;

                                    if (i == remove_quantity)
                                    {
                                        break;
                                    }
                                }

                                if (i == (int)jcd["Quantity"])
                                {
                                    //If all the parts have been removed then remove the entry from job_card_detail
                                    //for that part_type_ID
                                    db.Job_Card_Detail.Remove(det);
                                }
                            }
                        }
                    }

                    db.SaveChanges();
                    return("true|Job Card successfully updated.");
                }
            }
            catch (Exception e)
            {
                ExceptionLog.LogException(e, "JobCardController PUT");
                return("false|An error has occured updating the Job Card on the system.");
            }
        }
Example #5
0
        // 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.";
             *   }*/
        }