58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace LCU.Library.Internal.IncentiveApiModels.Entities
|
|
{
|
|
public static class PayoutProfileExtensions
|
|
{
|
|
public static PayoutRange FindPayoutRange(this PayoutProfile profile, double percentage, DateTime runDate)
|
|
{
|
|
var percentageToHundreds = Math.Truncate(100 * percentage) / 100;
|
|
var defaultPayout = profile.Payouts.First(defaultPayout => defaultPayout.Key == DateTime.MinValue).Value;
|
|
if (profile.Payouts.Count == 1)
|
|
{
|
|
return defaultPayout.FirstOrDefault(payoutRange => payoutRange.IsInRange(percentageToHundreds));
|
|
}
|
|
|
|
var payout = profile.Payouts.FirstOrDefault(payout =>
|
|
{
|
|
var dateTimeOfPayout = payout.Key;
|
|
return dateTimeOfPayout.Month == runDate.Month && dateTimeOfPayout.Year == runDate.Year;
|
|
}).Value;
|
|
|
|
var t = payout == null ? defaultPayout.FirstOrDefault(payoutRange => payoutRange.IsInRange(percentageToHundreds)) : payout.FirstOrDefault(payoutRange => payoutRange.IsInRange(percentageToHundreds));
|
|
return t;
|
|
}
|
|
|
|
public static decimal CalculateLineItemPayout(this PayoutProfile profile, int goal, decimal actual, DateTime runDate, decimal payoutPercent = 100)
|
|
{
|
|
if (profile == null)
|
|
{
|
|
throw new Exception("Attempt to calculate payout with null profile");
|
|
}
|
|
|
|
if (goal == 0) return 0;
|
|
|
|
var goalPercentage = (double)actual / goal;
|
|
var payoutRange = profile.FindPayoutRange(goalPercentage, runDate);
|
|
|
|
if (payoutRange == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
decimal payoutAmt = payoutRange.Payout;
|
|
if (profile.PayEach)
|
|
{
|
|
payoutAmt *= actual;
|
|
}
|
|
|
|
double payoutPercentage = (double)payoutPercent / 100;
|
|
double finalPayout = (double)payoutAmt * payoutPercentage;
|
|
payoutAmt = (decimal)Math.Round(finalPayout, 2);
|
|
|
|
return payoutAmt;
|
|
}
|
|
}
|
|
}
|