Files
BeWoPlaner/ReportImp/FortisNova/Invoicing/SettlementInvoiceCreation.cs
2021-05-30 16:31:45 +02:00

113 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Invoicing;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BS.Shared.Services;
namespace LeWoAachen.Invoicing
{
public class CustomSettlementInvoiceCreation : SettlementInvoiceCreation
{
public override bool CanCreateInvoiceTheOldWay(Settlement2DC si, CostBearer2SupportConcept c2s)
{
var relDC = c2s.MapToNewDC();
DateTime start = si.AccountingPeriodStart.Value.Date;
DateTime end = si.AccountingPeriodEnd.Value.Date;
if (end > DateTime.Now)
{
var srList = c2s.ServiceRecords;
var max = srList.Max(s => s.End);
if (max.HasValue && max < end)
{
end = max.Value;
}
}
var hourlyRatesInSpanList = relDC.CostBearer.CostRatePeriods.GetSpans(CostRatePeriodType.HourlyRate, start, end).ToList();
if (hourlyRatesInSpanList.Count > 3)
return false;
var factorInSpanList = relDC.CostBearer.CostRatePeriods.GetSpans(CostRatePeriodType.RateFactor, start, end).ToList();
if (factorInSpanList.Count > 1)
return false;
return true;
}
public override void CalculateSettlementInvoiceApprovedAmounts(Settlement2DC si, SupportConceptCostBearerRelDC relDC, Calculations calc)
{
decimal flsWeek = calc.GetApprovedHoursPerWeekAverage(relDC, true) ?? 0m;
decimal flsTotal = GetApprovedHoursTotal(relDC, true, calc) ?? 0m;
flsTotal = Math.Round(flsTotal, 2, MidpointRounding.AwayFromZero);
si.ApprovedFLS = flsTotal;
si.ApprovedFLSWeekly = flsWeek;
}
public decimal? GetApprovedHoursTotal(SupportConceptCostBearerRelDC relDC, bool useIsCalculationWithFactor, Calculations calc)
{
if (relDC.ApprovalPeriodList == null || !relDC.GetApprovedDuration().HasValue)
{
return null;
}
decimal? total = null;
foreach (var scap in relDC.ApprovalPeriodList)
{
if (scap.ServiceCategory == null || !scap.ServiceCategory.Name.Contains("Assistenz"))
{
decimal? hours = calc.GetApprovedHoursForPeriod(scap, relDC.CostBearer.CostRatePeriods, scap.StartDate, scap.EndDate);
if (useIsCalculationWithFactor)
{
DateTime dt = DateTime.Now;
if (scap.StartDate < dt)
{
dt = scap.StartDate.Value;
}
hours = calc.ApplyIsCalculationWithFactor(relDC, hours, dt);
}
if (hours.HasValue)
{
if (!total.HasValue)
{
total = 0;
}
total += hours;
}
}
}
//decimal? hoursTotal = relDC.ApprovalPeriodList.Sum(i => this.GetApprovedHoursTotal(i, relDC.CostBearer.CostRatePeriods));
//if (useIsCalculationWithFactor)
// hoursTotal = ApplyIsCalculationWithFactor(relDC, hoursTotal);
if (total.HasValue)
total = Math.Round(total.Value, 10, MidpointRounding.AwayFromZero);
return total;
}
public override bool IsServiceRecordBillable(ServiceRecord iRecord)
{
if (iRecord.ServiceDescription.ServiceCategory.Name.Contains("Assistenz"))
{
return false;
}
return true;
}
}
}