Skip to content

Instantly share code, notes, and snippets.

@MongkonEiadon
Last active February 2, 2018 06:11
Show Gist options
  • Save MongkonEiadon/ab19306c2ea3a4447642d97a9fbf568e to your computer and use it in GitHub Desktop.
Save MongkonEiadon/ab19306c2ea3a4447642d97a9fbf568e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PP.Core;
using PP.Core.Logging;
using PP.Core.Models;
using PP.Libraries.Models;
namespace PP.Libraries
{
public class CommissionCalculator : DepositeExemption
{
public CommissionCalculator(ILogger logger,
ISavingCalculation savingCalculation,
IPremiumCalculation premiumCalculation) : base(logger, savingCalculation, premiumCalculation) { }
private int getRetirementAge(int? retirementAge)
{
if (retirementAge == null)
{
_logHandler.WarningLog("Note: RetirementAge not set, assuming 67 years.");
return 67;
}
return retirementAge.Value;
}
public int CalculateCommision(
CalculateSavingModel calculateSavingModel,
int yearlyInsurance,
CommissionCalculatorModel commissionCalculatorModel
)
{
if (base._logHandler == null)
{
base.logger.Error("DepositeExemption : AddDEPremium > PLEASE SET LOG HANDLER BEFORE.");
_logHandler = new LoggerHandler(logger)
{
//BatchId = _batchId,
CompanyId = Guid.Empty
};
}
var premium = 0;
try
{
// NOTE : Move to be parameter.
//var age = savingCalculation.CalculateAge(person.DateOfBirth.Value, period);
// NOTE : Sheet Ark1
var retirementAge = getRetirementAge(calculateSavingModel.SalaryInfo.RetirementAge);
var totalInsurance = (retirementAge - commissionCalculatorModel.Age) * yearlyInsurance;
// NOTE : Sheet Ark2
var weeklyInsurance = Math.Round((decimal)(yearlyInsurance / commissionCalculatorModel.NoOfYearlyWeek), 0, MidpointRounding.ToEven);
var supplementReplacmentTime = (weeklyInsurance * commissionCalculatorModel.Rate) + (weeklyInsurance * commissionCalculatorModel.SupplementReplacmentTime);
var discountTime = (supplementReplacmentTime * commissionCalculatorModel.DiscountTime) + supplementReplacmentTime;
var additionalAge = (discountTime + (discountTime * commissionCalculatorModel.AdditionalAge)) * commissionCalculatorModel.AdditionalAgeRate;
// NOTE : Sheet Ark3
var grossRateValue = (totalInsurance * commissionCalculatorModel.GrossRate) / 1000;
// Result
premium = (int)Math.Round((decimal)(additionalAge + grossRateValue) * commissionCalculatorModel.DuviRate, MidpointRounding.ToEven);
//myLog = string.Format("Deposits Exemption: GrossYearly {0, 10:n0}, WP {1,5:n1}%, Age {4,2:n0}, RateLow {2,4:n1}, RateHigh {3,4:n1}, ",
// (int)calculateSavingModel.GrossYearlySalary,
// calculateSavingModel.WorkPercent,
// calculateSavingModel.RateLow,
// calculateSavingModel.RateHigh,
// age);
//_logHandler.WarningLog(myLog);
//myLog = string.Format("From G {0,1:n0}, <20% {1,1}, <20Y {2,1} => SumInsured {4,2:n0} => Premium {3,5:n0}\n",
// calculateSavingModel.FromG,
// (calculateSavingModel.Savings_20p ? "Y" : "N"),
// (calculateSavingModel.Savings_20years ? "Y" : "N"),
// premium,
// sumInsured);
//_logHandler.WarningLog(myLog);
//if (company == null)
//{
// myLog = string.Format("Age {8,2:n0}, PeriodSalary {0, 10:n0}, WP {1,5:n1}%, RateLow {2,4:n1}, RateHigh {3,4:n1}, From G {4,1:n0} => Sum Insured {9,2:n0} (week {11,2:n0}) => Premium {7,5:n0}\n",
// (int)calculateSavingModel.SalaryInfo.PeriodSalary,
// calculateSavingModel.WorkPercent,
// calculateSavingModel.RateLow,
// calculateSavingModel.RateHigh,
// calculateSavingModel.FromG,
// (calculateSavingModel.Savings_20p ? "Y" : "N"),
// (calculateSavingModel.Savings_20years ? "Y" : "N"),
// premium,
// age,
// sumInsured,
// calculateSavingModel.SavingPeriod,
// sumInsuredWeek);
// person.Comment = myLog;
//}
}
catch (Exception err)
{
logger.Error("Error in AddDEPremium: " + err);
throw new Exception(err.ToString());
}
return premium;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PP.Core.Models;
namespace PP.Libraries.PensionCalculator
{
public class DepExTariffCalculator : IPensionCalculator
{
private readonly IDepExTariffDetail _calcualtionDepExTariffDetail;
private readonly int _totalSumInsuredAtSpecificAge;
private readonly int _sumInsured;
private readonly StringBuilder _logBuilder;
public DepExTariffCalculator(IDepExTariffDetail calcualtionDepExTariffDetail,
int sumInsured,
int totalSumInsuredAtSpecificAge)
{
_calcualtionDepExTariffDetail = calcualtionDepExTariffDetail;
_totalSumInsuredAtSpecificAge = totalSumInsuredAtSpecificAge;
_sumInsured = sumInsured;
_logBuilder = new StringBuilder($"\n{nameof(DepExTariffCalculator)} : \n");
}
/// <summary>
/// Premium = (TTD + PTD) * Totalfactor
/// Check the calculate result by call to <see cref="GetCalculationLog"/>
/// </summary>
/// <returns></returns>
public int Calculate()
{
var sumInsuredWeek = Math.Round((_sumInsured / 52M), 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"{nameof(Calculate)} :");
_logBuilder.AppendLine($"TotalSumInsured {_totalSumInsuredAtSpecificAge,10:n0}, SumInsured {_sumInsured,10:n0}, Week {sumInsuredWeek,10:n0}");
var premiumAddTime = CalculatePremiumAddTime(sumInsuredWeek, _calcualtionDepExTariffDetail.TTDrate, _calcualtionDepExTariffDetail.AddCompTime);
var premiumWatingPeriod = CalculatePremiumWatingPeriod(premiumAddTime, _calcualtionDepExTariffDetail.DiscountWaitingPeriod);
var ttd = CalculateTTD(premiumWatingPeriod, _calcualtionDepExTariffDetail.AgeAddon, _calcualtionDepExTariffDetail.TTDfactor);
var ptd = CalculatePTD(_totalSumInsuredAtSpecificAge, _calcualtionDepExTariffDetail.GrossRates, _calcualtionDepExTariffDetail.PTDfactor);
var premium = (int)Math.Round((decimal)(ttd + ptd) * (decimal)_calcualtionDepExTariffDetail.Totalfactor, 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"Caculated Premium = {premium,16:n0}");
return premium;
}
public string GetCalculationLog()
{
return _logBuilder.ToString();
}
#region Internal Methods
/// <summary>
///
/// </summary>
/// <param name="premiumWaitingPeriod">Rabatt karenstid</param>
/// <param name="ageAddon"></param>
/// <param name="ttdFactor"></param>
/// <returns></returns>
public int CalculateTTD(decimal premiumWaitingPeriod, int ageAddon, decimal ttdFactor = 0.7m)
{
var ttd = (int)Math.Round((decimal)premiumWaitingPeriod * (1 + (decimal)ageAddon / 100) * (decimal)ttdFactor, 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"TTD = {ttd,30:n0}\t//TTD = PremiumWaitingPeriod * (1 + AgeAddon%) * TTDfactor - Alders tillegg");
return ttd;
}
/// <summary>
///
/// </summary>
/// <param name="sumInsured">Total Suminsured for specific age</param>
/// <param name="grossRate">GrossRates value from DepExDetailed</param>
/// <param name="ptdFactor">PTD Factor to calculate Ptd from default</param>
/// <returns></returns>
public int CalculatePTD(decimal sumInsured, decimal grossRate, decimal ptdFactor = 0.7m)
{
var ptd = (int)Math.Round(sumInsured * grossRate / 1000 * ptdFactor, 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"PTD = {ptd,30:n0}\t//PTD = sumInsured * GrossRates%0 * PTDfactor");
return ptd;
}
public int CalculatePremiumAddTime(decimal sumInsuredWeek, decimal ttdRate, int addCompTime)
{
var premiumAddTime = (int)Math.Round(sumInsuredWeek * (ttdRate + (decimal)addCompTime / 100), 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"PremiumAddTime = {premiumAddTime,19:n0}\t//PremiumAddTime = sumInsuredWeek * (TTDrate + AddCompTime%) - Tillegg erst. Tid");
return premiumAddTime;
}
/// <summary>
/// Calculate Premium Waiting Period (Rabatt karenstid)
/// </summary>
/// <param name="premiumAddTime"></param>
/// <param name="discountWaitingPeriod">Rabatt karenstid</param>
/// <returns></returns>
public int CalculatePremiumWatingPeriod(decimal premiumAddTime, int discountWaitingPeriod)
{
//Then Calculate the premium from the tariff
//Tillegg erst. Tid:
//PremiumAddTime = sumInsuredWeek * (TTDrate + AddCompTime%) - Tillegg erst. Tid
var premiumWatingPeriod = (int)Math.Round((decimal)premiumAddTime * (1 + (decimal)discountWaitingPeriod / 100), 0, MidpointRounding.ToEven);
_logBuilder.AppendLine($"PremiumWatingPeriod = {premiumWatingPeriod,14:n0} \t//PremiumWaitingPeriod = PremiumAddTime * (1 + DiscountWaitingPeriod%) - Rabatt karenstid");
return premiumWatingPeriod;
}
#endregion
}
}
using System;
using System.Linq;
using System.Text;
namespace PP.Libraries.PensionCalculator.Insurance
{
public class DisabilityInsuracePensionCalculator : IPensionCalculator
{
private readonly IDisabilityInsuranceParameter _calculateModel;
private StringBuilder _log;
public int[] GValueRange => Enumerable.Range(0, 13).Select(x => x * _calculateModel.GValue).ToArray();
public double FullTimeSalary => _calculateModel.YearlySalary >= 0 ? _calculateModel.YearlySalary / (_calculateModel.WorkingPercent * 0.01) : 0;
public DisabilityInsuracePensionCalculator(IDisabilityInsuranceParameter calculateModel)
{
//validate
var validate = new DisabilityInsuranceValidation().Validate(calculateModel);
if (!validate.IsValid)
{
throw new DisabilityInsuranceValidatorException(validate);
}
_calculateModel = calculateModel;
_log = new StringBuilder($"{nameof(DisabilityInsuracePensionCalculator)} calculate report: \n");
}
/// <summary>
/// % erstatning av lønn inntil 12G, maks 3%
/// </summary>
public int BasicCoverage => _calculateBasicCoverage();
/// <summary>
/// Up to 25% replacement of 1G, max 6% of salary
/// </summary>
public int AdditionCoverage => _calculateAdditionCoverage();
/// <summary>
/// Salary over 6 G, % compensation of salary between 6-12G, max 66%
/// </summary>
public int SalaryCoverage => _calculateSalaryCoverage();
/// <summary>
/// Child supplement (Barnetillegg), Child supplement 4% per child, max 12% of 6G (Duvi fixed it at 6%)
/// </summary>
public int ChildSupplement => _calculateChildSupplement();
#region Interfacers
public int Calculate()
{
var sumInsured = _summarizeInsurance();
_log.AppendLine($"=> Calculate Premium : ({sumInsured,10:n0}) <====");
return (int)Math.Round(sumInsured * _calculateModel.WorkingPercent * 0.01, MidpointRounding.AwayFromZero);
}
public string GetCalculationLog()
{
return _log.ToString();
}
#endregion
#region [Privates Calculations]
private int _calculateBasicCoverage()
{
var _PercentSalary = _calculateModel.BasicCoveragePercent * 0.01 * this.FullTimeSalary;
var _Percent12G = _calculateModel.BasicCoveragePercent * 0.01 * this.GValueRange[12];
var result = (int)Math.Round(_PercentSalary >= _Percent12G ? _Percent12G : _PercentSalary, MidpointRounding.AwayFromZero);
_log.AppendLine($"BasicCoverage ({_calculateModel.BasicCoveragePercent}%) : {result,13:n0}");
return result;
}
private int _calculateAdditionCoverage()
{
var _6pOfFullTime = 0.06 * FullTimeSalary;
var _additionCovOfGValue = _calculateModel.AdditionCoveragePercent * 0.01 * GValueRange[1];
var result = (int)Math.Round(_6pOfFullTime >= _additionCovOfGValue ? _additionCovOfGValue : _6pOfFullTime, MidpointRounding.AwayFromZero);
_log.AppendLine($"AdditionalCoverage ({_calculateModel.AdditionCoveragePercent}%): {result,10:n0}");
return result;
}
private int _calculateSalaryCoverage()
{
var _over6GPercent = (FullTimeSalary - GValueRange[6]) * _calculateModel.SalaryOver6GCoveragePercent * 0.01;
var _6gTo12GOfCompensatePercent = (GValueRange[12] - GValueRange[6]) * _calculateModel.SalaryOver6GCoveragePercent * 0.01;
var result = _over6GPercent <= 0
? 0
: (_over6GPercent >= _6gTo12GOfCompensatePercent
? _6gTo12GOfCompensatePercent
: _over6GPercent);
result = (int)Math.Round(result, MidpointRounding.AwayFromZero);
_log.AppendLine($"SalaryCoverage ({_calculateModel.BasicCoveragePercent}%) : {result,13:n0}");
return (int)result;
}
private int _calculateChildSupplement()
{
var _childAmendment = GValueRange[6] * 0.06; //fixed by duvi @6%
var result = (int) Math.Round(_calculateModel.HasChild ? _childAmendment : 0, MidpointRounding.AwayFromZero);
_log.AppendLine($"ChildSupplement {(_calculateModel.HasChild ? "Y" : "N"),20}");
return result;
}
private int _summarizeInsurance()
{
var result = BasicCoverage + AdditionCoverage + SalaryCoverage + ChildSupplement - _calculateModel.DeductionAmount;
_log.AppendLine($"{nameof(_calculateModel.DeductionAmount)} (PAS): {_calculateModel.DeductionAmount,13:n0}");
return result;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment