Example #1
0
 /// <summary>
 /// This method deletes a jobCard
 /// </summary>
 /// <param name="jobCardId"></param>
 public void DeleteJobCardCrew(int jobCardCrewId)
 {
     using (var context = new MarigoldSystemContext())
     {
         JobCardCrew jobCardCrew = context.JobCardCrews.Find(jobCardCrewId);
         int         count       = context.JobCardCrews
                                   .Where(x => x.JobCardID == jobCardCrew.JobCardID)
                                   .Select(x => x)
                                   .Count();
         if (count > 1)
         {
             context.JobCardCrews.Remove(jobCardCrew);
         }
         else
         {
             int     jobCardId = jobCardCrew.JobCardID;
             JobCard jobCard   = context.JobCards.Find(jobCardId);
             context.JobCardCrews.Remove(jobCardCrew);
             context.JobCards.Remove(jobCard);
         }
         context.SaveChanges();
     }
 }
Example #2
0
        /// <summary>
        /// This method add a JobCard to a crew
        ///     It verifies the same site is not assigned twice to the same crew
        ///     It notifies the user when a Jobcard is assigned to more than one crew
        ///     It adds a Jobcard to a crew.
        /// </summary>
        /// <param name="crewId"></param>
        /// <param name="siteId"></param>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public string AddJobCard(int crewId, int siteId, int taskId)
        {
            string message = "";

            using (var context = new MarigoldSystemContext())
            {
                DateTime today   = context.Crews.Find(crewId).CrewDate;
                JobCard  jobcard = context.JobCardCrews
                                   .Where(x => DbFunctions.TruncateTime(x.Crew.CrewDate) == DbFunctions.TruncateTime(DateTime.Now) && x.JobCard.SiteID == siteId)
                                   .Select(x => x.JobCard)
                                   .FirstOrDefault();

                if (jobcard != null && jobcard.ClosedDate != null)
                {
                    throw new Exception("The current job card was closed for the day. You can no longer assigned it today.");
                }
                else
                {
                    //Check How many times the curent site is done for the current season
                    if (taskId == 1)
                    {
                        //Determines how many times the cuurent site was worked on in the current season.
                        int cycle = context.Sites.Find(siteId).SiteType.NumberOfCyle;
                        int count = context.JobCards
                                    .Where(x => x.SiteID == siteId && ((DateTime)x.ClosedDate).Year == DateTime.Now.Year)
                                    .Select(x => x)
                                    .Count();

                        if (count >= cycle)
                        {
                            throw new Exception("The number of cycle (" + cycle + ") for this site has been reached for this season");
                        }
                    }

                    JobCardCrew jobCardCrew = context.JobCardCrews
                                              .Where(x => x.CrewID == crewId && x.JobCard.SiteID == siteId)
                                              .Select(x => x)
                                              .FirstOrDefault();
                    if (jobCardCrew != null)
                    {
                        //Check if the same site is not already assigned to the current crew
                        throw new Exception("This site is already assigned to the current Crew");
                    }
                    else
                    {
                        List <JobCardCrew> jobCardCrews = context.JobCardCrews
                                                          .Where(x => x.JobCard.SiteID == siteId && DbFunctions.TruncateTime(x.Crew.CrewDate) == DbFunctions.TruncateTime(DateTime.Today))
                                                          .Select(x => x)
                                                          .ToList();

                        //Notifies the users that existing crew(s) are also assigned to work on the same site.
                        if (jobCardCrews.Count > 0)
                        {
                            foreach (JobCardCrew job in jobCardCrews)
                            {
                                string unit = job.Crew.EquipmentID == null ? job.Crew.Truck.TruckDescription : job.Crew.Equipment.Description;
                                message += unit + ", ";
                            }
                            jobCardCrew           = new JobCardCrew();
                            jobCardCrew.CrewID    = crewId;
                            jobCardCrew.JobCardID = jobCardCrews[0].JobCardID;
                            context.JobCardCrews.Add(jobCardCrew);
                        }
                        else
                        {
                            JobCard jobCard = new JobCard();
                            jobCard.SiteID = siteId;
                            jobCard.TaskID = taskId;
                            context.JobCards.Add(jobCard);

                            jobCardCrew        = new JobCardCrew();
                            jobCardCrew.CrewID = crewId;

                            jobCard.JobCardCrews.Add(jobCardCrew);
                        };
                        context.SaveChanges();
                    }
                }

                return(message);
            }
        }