2020-12-01 19:28:26 +03:00

69 lines
2.6 KiB
C#

using System;
using System.Linq;
using EvoCalculator.Core.Calculation.Post;
using EvoCalculator.Core.Models.Calculation.Models.Prepared;
using TridentGoalSeek;
namespace EvoCalculator.Core.Calculation.Columns
{
public class SumColumn : BaseColumnWithGoalSeek
{
private readonly PercentPaymentColumn _percentPaymentColumn;
private readonly PreparedValues _preparedValues;
private readonly PostValues _postValues;
public SumColumn(int count, DateTempColumn dateTempColumn
, PreparedValues preparedValues
, PercentPaymentColumn percentPaymentColumn, PostValues postValues) : base(count, dateTempColumn)
{
_preparedValues = preparedValues;
_percentPaymentColumn = percentPaymentColumn;
_postValues = postValues;
}
public override decimal Calculate(decimal inputVariable)
{
FillValues(inputVariable);
return Convert.ToDecimal(IRR);
}
protected override void FillValues(decimal x)
{
Values[0] = -_postValues.BaseCost.Value;
Values[1] = _preparedValues.FirstPaymentSum;
for (var i = 2; i < Values.Length - 1; i++) Values[i] = x * (decimal) _percentPaymentColumn.Values[i] / 100;
Values[^1] = _preparedValues.LastPaymentSum;
}
public void PostCheck()
{
new CheckTools<decimal>().CheckColumnForZeroValue(Values);
if (Values[1] / Values.Skip(1).Sum() >= 0.5m)
{
throw new Exception(
"Первый платеж по графику более 50% от суммы лизинговых платежей. Необходимо уменьшить первый платеж");
}
}
public override void ComputeValues(decimal requiredValue)
{
var NUMBER_OF_LARGE_PERCENTS = _percentPaymentColumn.Values
.Skip(2)
.Take(_percentPaymentColumn.Values.Length - 1)
.Where(x => x >= 10).ToArray().Length;
var goalSeek = new GoalSeek(this);
goalSeek.SeekResult(requiredValue,
new GoalSeekOptions(
startingStabPoint:
(_postValues.BaseCost.Value - _preparedValues.FirstPaymentSum) / NUMBER_OF_LARGE_PERCENTS
, maximumAttempts: 1000
, initialTineSpacing: (_postValues.BaseCost.Value - _preparedValues.FirstPaymentSum) / _preparedValues.Nmper
// , focusPercentage: 50
));
}
}
}