private async Task <bool> HasAllowance(string userID, WorkingDay workingDay, OffenseDegree degree) { int allowanceOccurrence = await offenseRepository.CountAllowancesInThisMonth(userID, workingDay.Date, degree); int allowanceRule = await monthlyAllowanceRuleRepository.GetAllowance(degree); // Check the logically-related offenses (feeding allowances) if (degree == OffenseDegree.LateLessThanHourNoMakeUp || degree == OffenseDegree.LateLessThanHourWithMakeUp) { // LateMoreThanHourWithMakeUp can feed LateLessThanHourWithMakeUp // LateMoreThanHourNoMakeUp can feed LateLessThanHourNoMakeUp // (difference between related offense degrees is 2 in the enum OffenseDegree) int feedingAllowanceOccurrence = await offenseRepository.CountAllowancesInThisMonth(userID, workingDay.Date, degree + 2); int feedingAllowanceRule = await monthlyAllowanceRuleRepository.GetAllowance(degree + 2); int balance = feedingAllowanceRule - feedingAllowanceOccurrence; if (balance > 0) { allowanceRule += balance; } } // Check if the user has allowances taken from this degree else if (degree == OffenseDegree.LateMoreThanHourNoMakeUp || degree == OffenseDegree.LateMoreThanHourWithMakeUp) { int feedingAllowanceRule = await monthlyAllowanceRuleRepository.GetAllowance(degree - 2); int feedingAllowanceOccurrence = await offenseRepository.CountAllowancesInThisMonth(userID, workingDay.Date, degree - 2); int balance = feedingAllowanceRule - feedingAllowanceOccurrence; if (balance < 0) { allowanceRule += balance; } } return(allowanceOccurrence < allowanceRule); }
public async Task <int> GetAllowance(OffenseDegree degree) { return(await db.MonthlyAllowanceRules.Where(record => record.Degree == degree).Select(record => record.UserMonthlyAllowance).FirstOrDefaultAsync()); }
public MonthlyAllowanceRule(OffenseDegree degree, int userMonthlyAllowance) { Degree = degree; UserMonthlyAllowance = userMonthlyAllowance; }
public async Task RecordDetectedOffense(ApplicationUser user, WorkingDay workingDay, OffenseDegree degree) { WorkingDay userWorkingDay; if (workingDay == null) // user was absent { // Create new empty working day object for the day that the user miss userWorkingDay = new WorkingDay(user.Id, DateTime.Today.AddDays(-1)); await workingDayRepository.Add(userWorkingDay); } else { userWorkingDay = workingDay; } Offense offense; if (await HasAllowance(user.Id, userWorkingDay, degree)) { offense = new Offense(userWorkingDay.ID, 0, degree) { HasAllowance = true }; } else { // Get how many times this offense occurred within // the last 90 days and increment it by one (for absence, count the unexcused ones only) int occurrence; if (degree == OffenseDegree.Absence) { occurrence = await offenseRepository.CountAbsenceInLast90Days(user.Id, userWorkingDay) + 1; } else { occurrence = await offenseRepository.CountOffensesInLast90Days(user.Id, userWorkingDay, degree) + 1; } // If the occurence is not within the Occurrence enum, assign it to the last enum (FifthOrMore) if (!Enum.IsDefined(typeof(Occurrence), occurrence)) { Array occurrencesArray = Enum.GetValues(typeof(Occurrence)); // Get the last object in the occurence enum & assign it to occurrence occurrence = (int)occurrencesArray.GetValue(occurrencesArray.Length - 1); } // Get penalty percent per day desired for this offense int penaltyPercent = ruleRepository.GetPenaltyPercent(degree, (Occurrence)occurrence); // Create new offence object & save it offense = new Offense(userWorkingDay.ID, penaltyPercent, degree); // Check if it needs approval if (degree == OffenseDegree.Absence || degree == OffenseDegree.LeaveEarly) { offense.NeedApproval = true; } } await offenseRepository.Add(offense); }
public Rule(OffenseDegree offenseDegree, Occurrence occurrence, int penaltyPercent) { OffenseDegree = offenseDegree; Occurrence = occurrence; PenaltyPercent = penaltyPercent; }
public int GetPenaltyPercent(OffenseDegree degree, Occurrence occurrence) { return(db.Rules.Where(record => record.OffenseDegree == degree && record.Occurrence == occurrence).Select(record => record.PenaltyPercent).FirstOrDefault()); }
public Offense(string workingDayID, int penaltyPercent, OffenseDegree degree) { WorkingDayID = workingDayID; PenaltyPercent = penaltyPercent; Degree = degree; }
public async Task <int> CountAllowancesInThisMonth(string userID, DateTime endDate, OffenseDegree degree) { DateTime startDate = endDate.GetMonthStartDate(); return(await db.Offenses.CountAsync(record => record.HasAllowance && record.WorkingDay.UserID == userID && record.Degree == degree && record.WorkingDay.Date < endDate && record.WorkingDay.Date >= startDate)); }
public async Task <int> CountOffensesInLast90Days(string userID, WorkingDay workingDay, OffenseDegree degree) { return(await db.Offenses .CountAsync(record => record.WorkingDay.UserID == userID && record.WorkingDay.Date >= workingDay.Date.AddDays(-90) && record.WorkingDay.Date < workingDay.Date && record.Degree == degree && record.HasAllowance == false)); }