Esempio n. 1
0
        public void AddLabourersToFirstSchedule(int jobId)
        {
            var      jobSelected      = _context.Job.Where(j => j.JobId == jobId).FirstOrDefault();
            var      jobSkillSelected = _context.Job.Where(j => j.JobId == jobId).Select(j => j.JobSkill).FirstOrDefault();
            var      duration         = (jobSelected.EndDate - jobSelected.StartDate).TotalDays;
            DateTime eDate;

            if (duration <= 14)
            {
                eDate = jobSelected.EndDate;
                jobSelected.ScheduleDone = true;
            }
            else
            {
                eDate = jobSelected.StartDate.AddDays(ScheduleHelper.CalculateLastDay(jobSelected.StartDate));
            }

            GetHighestRated rated = new GetHighestRated(_context);

            foreach (JobSkill js in jobSkillSelected)
            {
                var             ratedLabourers = rated.GetHighestRatedLabourers(js.SkillId).Where(l => l.IsAvailable == true && l.OnLeave == false).ToList();
                List <Labourer> labourers      = new List <Labourer>();
                labourers.AddRange(ratedLabourers.GetRange(0, js.NumberNeeded));

                foreach (Labourer labourer in labourers)
                {
                    _context.JobLabourer.Add(new JobLabourer
                    {
                        JobId                  = jobId,
                        SkillId                = js.SkillId,
                        LabourerId             = labourer.LabourerId,
                        LabourerSafetyRating   = 5,
                        SafetyMeetingCompleted = false,
                        ClientQualityRating    = 0,
                        StartDay               = jobSelected.StartDate,
                        EndDay                 = eDate,
                        Duration               = GetBusinessDays(jobSelected.StartDate, eDate)
                    });
                    PopulateLabourerAttendance(jobId, labourer.LabourerId, jobSelected.StartDate, eDate);
                    EmailSettings emailSetting = new EmailSettings
                    {
                        Domain           = "smtp.sendgrid.net",
                        Port             = 587,
                        UsernameLogin    = "******",
                        UsernamePassword = "******",
                        FromEmail        = "*****@*****.**",
                        DisplayName      = "Admin",
                        ToEmail          = ""
                    };
                    EmailContent emailContent = new EmailContent
                    {
                        LabourerFirstName = labourer.LabourerFirstName,
                        LabourerLastName  = labourer.LabourerLastName,
                        JobAddress        = jobSelected.Street,
                        JobStart          = jobSelected.StartDate,
                        JobEnd            = eDate
                    };
                    EmailHelper emailHelper = new EmailHelper(emailSetting);
                    emailHelper.SendMail(labourer.LabourerEmail, "Your Job Schedule", emailContent);

                    labourer.IsAvailable = false;
                }
                _context.SaveChanges();
            }
        }
Esempio n. 2
0
        public void AddWeeklySchedule()
        {
            GetHighestRated rated        = new GetHighestRated(_context);
            var             ratedClients = rated.GetHighestRatingClients();


            foreach (Client client in ratedClients)
            {
                var jobs = _context.Job.Where(j => j.ClientId == client.ClientId && j.ScheduleDone != true).ToList();
                foreach (Job j in jobs)
                {
                    var jobSkills = _context.JobSkill.Where(js => js.JobId == j.JobId).ToList();

                    foreach (JobSkill js in jobSkills)
                    {
                        var ratedLabourers       = rated.GetHighestRatedLabourers(js.SkillId).ToList();
                        var unAvailableLabourers = _context.JobLabourer.Where(jb => jb.StartDay > DateTime.Now.AddDays(9)).Select(l => l.Labourer)
                                                   .Distinct().Where(l => l.OnLeave == true).ToList();
                        var             availableLabourers = ratedLabourers.Except(unAvailableLabourers).ToList();
                        List <Labourer> chosenLabourers    = new List <Labourer>();
                        chosenLabourers.AddRange(availableLabourers.GetRange(0, js.NumberNeeded));
                        foreach (Labourer l in chosenLabourers)
                        {
                            var      jobLabourer = _context.JobLabourer.Where(jl => jl.JobId == j.JobId && jl.LabourerId == l.LabourerId).FirstOrDefault();
                            DateTime sDate       = DateTime.Now.AddDays(10);
                            DateTime eDate       = j.EndDate > DateTime.Now.AddDays(15) ? DateTime.Now.AddDays(15) : j.EndDate;
                            if (jobLabourer == null)
                            {
                                _context.Add(new JobLabourer
                                {
                                    JobId                  = j.JobId,
                                    SkillId                = js.SkillId,
                                    LabourerId             = l.LabourerId,
                                    LabourerSafetyRating   = 5,
                                    SafetyMeetingCompleted = false,
                                    ClientQualityRating    = 0,
                                    StartDay               = sDate,
                                    EndDay                 = eDate,
                                    Duration               = GetBusinessDays(sDate, eDate)
                                });
                            }
                            else
                            {
                                _context.Add(new JobLabourer
                                {
                                    JobId                  = j.JobId,
                                    SkillId                = js.SkillId,
                                    LabourerId             = l.LabourerId,
                                    LabourerSafetyRating   = jobLabourer.LabourerSafetyRating,
                                    SafetyMeetingCompleted = jobLabourer.SafetyMeetingCompleted,
                                    ClientQualityRating    = jobLabourer.ClientQualityRating,
                                    StartDay               = sDate,
                                    EndDay                 = eDate,
                                    Duration               = GetBusinessDays(sDate, eDate)
                                });
                            }
                            PopulateLabourerAttendance(j.JobId, l.LabourerId, sDate, eDate);
                            EmailSettings emailSetting = new EmailSettings
                            {
                                Domain           = "smtp.sendgrid.net",
                                Port             = 587,
                                UsernameLogin    = "******",
                                UsernamePassword = "******",
                                FromEmail        = "*****@*****.**",
                                DisplayName      = "Admin",
                                ToEmail          = ""
                            };
                            EmailContent emailContent = new EmailContent
                            {
                                LabourerFirstName = l.LabourerFirstName,
                                LabourerLastName  = l.LabourerLastName,
                                JobAddress        = j.Street,
                                JobStart          = sDate,
                                JobEnd            = eDate
                            };
                            EmailHelper emailHelper = new EmailHelper(emailSetting);
                            emailHelper.SendMail(l.LabourerEmail, "Your Job Schedule", emailContent);
                            l.IsAvailable = false;
                            _context.SaveChanges();
                        }
                        ;
                    }
                    ;
                }
                ;
            }
        }