156 lines
5.7 KiB
C#
156 lines
5.7 KiB
C#
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<DepartmentProgress>
|
|
{
|
|
private IReadOnlyCollection<IPayoutRule> DepartmentRules { get; }
|
|
private IReadOnlyCollection<IPayoutRule> PersonalRules { get; }
|
|
|
|
public DepartmentProgress(int departmentId, MonthRange monthRange)
|
|
{
|
|
DepartmentId = departmentId;
|
|
MonthRange = monthRange;
|
|
DepartmentRules = new List<IPayoutRule>();
|
|
PersonalRules = new List<IPayoutRule>();
|
|
Progress = new PerProductActivity();
|
|
ActivityAudit = new List<Activity>();
|
|
GoalAudit = new List<Goal>();
|
|
Goals = new PerProductGoal();
|
|
EmployeeProgress = new Dictionary<string, PerProductActivity>();
|
|
EmployeeGoals = new Dictionary<string, PerProductGoal>();
|
|
}
|
|
|
|
public DepartmentProgress(
|
|
int deptDepartmentId,
|
|
MonthRange monthRangeOfAssignment,
|
|
List<string> employeeList,
|
|
IReadOnlyCollection<IPayoutRule> departmentRules,
|
|
IReadOnlyCollection<IPayoutRule> 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<Activity> ActivityAudit { get; set; }
|
|
public IEnumerable<Goal> GoalAudit { get; set; }
|
|
public IReadOnlyCollection<string> EmployeeList { get; }
|
|
public IDictionary<string, PerProductActivity> EmployeeProgress { get; }
|
|
public IDictionary<string, PerProductGoal> EmployeeGoals { get; }
|
|
public int GapInsuranceEligibleActivityCount { get; private set; }
|
|
|
|
public void ApplyActivity(ICollection<Activity> 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<Goal> 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;
|
|
}
|
|
}
|