using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using LCU.Library.Internal.IncentiveApiModels.Common; namespace LCU.API.Internal.Operations.IncentiveApi.Models; public class DepartmentProgress : IViewTransformable { private IReadOnlyCollection DepartmentRules { get; } private IReadOnlyCollection PersonalRules { get; } public DepartmentProgress(int departmentId, MonthRange monthRange) { DepartmentId = departmentId; MonthRange = monthRange; DepartmentRules = new List(); PersonalRules = new List(); Progress = new PerProductActivity(); ActivityAudit = new List(); GoalAudit = new List(); Goals = new PerProductGoal(); EmployeeProgress = new Dictionary(); EmployeeGoals = new Dictionary(); } public DepartmentProgress( int deptDepartmentId, MonthRange monthRangeOfAssignment, List employeeList, IReadOnlyCollection departmentRules, IReadOnlyCollection personalRules ) : this(deptDepartmentId, monthRangeOfAssignment) { EmployeeList = employeeList; DepartmentRules = departmentRules; PersonalRules = personalRules; foreach (var employeeId in employeeList) { EmployeeGoals[employeeId] = new PerProductGoal(); EmployeeProgress[employeeId] = new PerProductActivity(); } } public int DepartmentId { get; set; } public MonthRange MonthRange { get; } public PerProductActivity Progress { get; } public PerProductGoal Goals { get; } public IEnumerable ActivityAudit { get; set; } public IEnumerable GoalAudit { get; set; } public IReadOnlyCollection EmployeeList { get; } public IDictionary EmployeeProgress { get; } public IDictionary EmployeeGoals { get; } public int GapInsuranceEligibleActivityCount { get; private set; } public void ApplyActivity(ICollection activities) { ActivityAudit = ActivityAudit.Concat(activities); foreach (var product in ProductDetail.ProductDetails.Select(productDetail => productDetail.Product)) { foreach (var activity in activities) { var employeeId = activity.EmployeeId; if (product == Product.Vehicle && activity.Product == Product.Vehicle) { GapInsuranceEligibleActivityCount += activity.CalculatedValueTowardsGapInsurance(); if (!EmployeeGoals.ContainsKey(employeeId)) { EmployeeGoals[activity.EmployeeId] = new PerProductGoal(); }; EmployeeGoals[activity.EmployeeId].ProductGoals[Product.GAPInsurance] += activity.CalculatedValueTowardsGapInsurance(); }; if (!EmployeeProgress.ContainsKey(employeeId)) { EmployeeProgress[employeeId] = new PerProductActivity(); } var calculatedValueTowardsOther = activity.CalculatedValueTowardsOther(product); EmployeeProgress[employeeId].ProductOther[product] += calculatedValueTowardsOther; Progress.ProductOther[product] += calculatedValueTowardsOther; var isEmployeeRestrictedByPayoutRule = PersonalRules.Any(payoutRule => payoutRule.RestrictedByPayoutRule(activity, product)); var isDepartmentRestrictedByPayoutRule = DepartmentRules.Any(payoutRule => payoutRule.RestrictedByPayoutRule(activity, product)); if (isEmployeeRestrictedByPayoutRule && isDepartmentRestrictedByPayoutRule) { continue; } var calculatedValueTowardsGoal = activity.CalculatedValueTowardsGoal(product); if (!isEmployeeRestrictedByPayoutRule) { EmployeeProgress[employeeId].ProductActivity[product] += calculatedValueTowardsGoal; } if (!isDepartmentRestrictedByPayoutRule) { Progress.ProductActivity[product] += calculatedValueTowardsGoal; } } } } public void ApplyGoals(ICollection goals) { GoalAudit = GoalAudit.Concat(goals); foreach (var goal in goals) { var product = goal.Product; var employeeId = goal.EmployeeId; var value = goal.Value; if (employeeId is null) { Goals.ProductGoals[product] += value; continue; } if (!EmployeeGoals.ContainsKey(employeeId)) { EmployeeGoals[employeeId] = new PerProductGoal(); } EmployeeGoals[employeeId].ProductGoals[product] += value; } if (GapInsuranceEligibleActivityCount == 0) { return; } var gapInsurance = Product.GAPInsurance; if (!Goals.ProductGoals.ContainsKey(gapInsurance)) { Goals.ProductGoals.Add(gapInsurance, GapInsuranceEligibleActivityCount); return; } Goals.ProductGoals[gapInsurance] = GapInsuranceEligibleActivityCount; } public DepartmentProgress TransformForView() { foreach (var activity in ActivityAudit) { activity?.TransformForView(); } return this; } }