297 lines
9.5 KiB
C#
297 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.DCEntityMapper;
|
|
using BS.Shared;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.Extensions;
|
|
using BS.Shared.Services;
|
|
|
|
namespace BeWo.Service.Invoicing
|
|
{
|
|
[IDSpecificClass(Identifier = "LWLBeWo67")]
|
|
public class LWLBeWo67SettlementInvoiceCreation : SettlementInvoiceCreation
|
|
{
|
|
public override bool CanCreateInvoiceTheOldWay(Settlement2DC si, CostBearer2SupportConcept c2s)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override void CreateInvoiceDetailsTheOldWay(Settlement2DC si, CostBearer2SupportConcept c2s)
|
|
{
|
|
String id = c2s.CostBearer.ID;
|
|
|
|
var calc = Calculations.GetInstance(id);
|
|
var relDc = c2s.MapToNewDC();
|
|
|
|
var apList = CreateApprovalPeriods(si, c2s);
|
|
si.ApprovedFLS = CalculateApprovedTotalFLS(apList);
|
|
|
|
DateTime periodStart = si.AccountingPeriodStart.Value.Date;
|
|
DateTime periodEnd = si.AccountingPeriodEnd.Value.Date;
|
|
|
|
//Prüfe ob vorher abgemeldet
|
|
if (c2s.SupportConcept.Customer.TerminationDate.HasValue &&
|
|
c2s.SupportConcept.Customer.TerminationDate.Value < periodEnd)
|
|
{
|
|
periodEnd = c2s.SupportConcept.Customer.TerminationDate.Value;
|
|
|
|
}
|
|
|
|
var hourlyRatesInSpanList = relDc.CostBearer.CostRatePeriods.GetSpans(CostRatePeriodType.HourlyRate, periodStart, periodEnd).ToList();
|
|
|
|
si.DifferentHourlyRateCount = hourlyRatesInSpanList.Count;
|
|
|
|
var rateFactorsInSpanList = relDc.CostBearer.CostRatePeriods.GetSpans(CostRatePeriodType.RateFactor, periodStart, periodEnd).ToList();
|
|
if (rateFactorsInSpanList.Count > 0)
|
|
{
|
|
var cdc = rateFactorsInSpanList.Last().Key;
|
|
if (cdc.CostRateValue.HasValue)
|
|
si.RateFactor = cdc.CostRateValue.Value;
|
|
}
|
|
var records = c2s.ServiceRecords;
|
|
|
|
records = records.Where(i => i.Start.HasValue && i.Start.Value.InBetween(periodStart, periodEnd, true)).ToList();
|
|
|
|
var recordDCS = records.MapToNewDCs();
|
|
|
|
var absenceTimes = MapperFactory.AbsenceTimeDC_AbsenceTime.MapToNewDCs(c2s.SupportConcept.Customer.AbsenceTimes);
|
|
Dictionary<DateTimeSpan, decimal> span2Hours = calc.GetAbsencesTimesNotBillable(absenceTimes);
|
|
|
|
si.RecordedBillableFLSNotBillableCustomerAbsent
|
|
= calc.GetHoursNotBillableDueToAbsenceTimes(relDc, recordDCS,
|
|
span2Hours, false, false);
|
|
|
|
si.RecordedBillableFLSNotBillableOutOfApprovedSpan = calc.GetHoursNotBillableDueToOutOfSpan(relDc, recordDCS);
|
|
//calc.GetHoursNotBillableDueToMoreThanApproved(relDC, recordDCS, false, false);
|
|
|
|
Dictionary<long, bool> groupBookingDict = new Dictionary<long, bool>();
|
|
|
|
|
|
decimal flmCleared = 0;
|
|
decimal approvedMinutes = ((si.ApprovedFLS ?? 0) * 60);
|
|
if (!CheckMaxApprovedHours())
|
|
{
|
|
approvedMinutes = Decimal.MaxValue;
|
|
}
|
|
|
|
records = records.OrderBy<ServiceRecord, DateTime>(sr => sr.Start.HasValue ? sr.Start.Value : DateTime.MinValue).ToList<ServiceRecord>();
|
|
|
|
foreach (var iRecord in records)
|
|
{
|
|
if (iRecord.ServiceDescription.ServiceCategory.IsBillable)
|
|
{
|
|
if (iRecord.GroupOid == null || !groupBookingDict.ContainsKey(iRecord.GroupOid.Value))
|
|
{
|
|
decimal rd = iRecord.RoundedDuration;
|
|
var prozent = iRecord.ServiceDescription.ServiceCategory.ProzentAbrechnung;
|
|
if (iRecord.ServiceDescription.ProzentAbrechnung.HasValue)
|
|
{
|
|
prozent = iRecord.ServiceDescription.ProzentAbrechnung.Value;
|
|
}
|
|
rd = (rd * prozent) / 100;
|
|
|
|
var ap = apList.FirstOrDefault(iap => iap.PeriodStartDate <= iRecord.Start && (iap.PeriodEndDate == DateTime.MaxValue || iRecord.Start < iap.PeriodEndDate.AddDays(1)));
|
|
|
|
//decimal duration = rd / 60;
|
|
if (ap != null)
|
|
{
|
|
//si.RecordedBillableFLSNetto += duration;
|
|
decimal durationToAdd = rd;
|
|
|
|
if (flmCleared + rd > approvedMinutes)
|
|
{
|
|
durationToAdd = approvedMinutes - flmCleared;
|
|
if (durationToAdd < 0)
|
|
durationToAdd = 0;
|
|
}
|
|
flmCleared += rd;
|
|
|
|
ap.RecordedMinutes += durationToAdd;
|
|
}
|
|
|
|
if (iRecord.GroupOid != null && !groupBookingDict.ContainsKey(iRecord.GroupOid.Value))
|
|
{
|
|
groupBookingDict.Add(iRecord.GroupOid.Value, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//decimal? maxAmount = CalculateMaxAmount(si, apList);
|
|
|
|
si.InvoiceItems = CreateInvoiceItems(si, apList);
|
|
|
|
decimal totalAmount = 0;
|
|
foreach (var ii in si.InvoiceItems)
|
|
{
|
|
totalAmount += ii.GrossAmountTotal ?? 0;
|
|
}
|
|
|
|
si.RecordedBillableFLSNetto = (flmCleared / 60); //eigentlich falsch dass netto = brutto ist.
|
|
si.RecordedBillableFLSBrutto = si.RecordedBillableFLSNetto;
|
|
|
|
//if (maxAmount.HasValue && totalAmount > maxAmount.Value)
|
|
//{
|
|
// totalAmount = maxAmount.Value;
|
|
//}
|
|
si.AmountBillableTotal = totalAmount;
|
|
|
|
//AdvancedPayments
|
|
CalculateAdvancedPayments(si, c2s, null, null, hourlyRatesInSpanList);
|
|
//EquityContribution (Eigenanteil)
|
|
CalculateEquityContribution(si, c2s.SupportConcept, null, null);
|
|
|
|
si.Claim = si.AmountBillableTotal - (si.AmountAdvancedPayments ?? 0) - si.EquityContribution;
|
|
|
|
|
|
}
|
|
|
|
|
|
public virtual List<InvoiceItemDC> CreateInvoiceItems(Settlement2DC si, IList<ApprovalPeriod> apList)
|
|
{
|
|
List<InvoiceItemDC> iiList = new List<InvoiceItemDC>();
|
|
|
|
decimal rf = GetRateFactor(si);
|
|
|
|
var apsByHourlyRate = new Dictionary<decimal, IList<ApprovalPeriod>>();
|
|
|
|
foreach (var ap in apList)
|
|
{
|
|
IList<ApprovalPeriod> list = null;
|
|
if (apsByHourlyRate.ContainsKey(ap.AmountPerUnit))
|
|
{
|
|
list = apsByHourlyRate[ap.AmountPerUnit];
|
|
}
|
|
else
|
|
{
|
|
list = new List<ApprovalPeriod>();
|
|
apsByHourlyRate[ap.AmountPerUnit] = list;
|
|
}
|
|
|
|
list.Add(ap);
|
|
}
|
|
foreach (var hr in apsByHourlyRate.Keys)
|
|
{
|
|
IList<ApprovalPeriod> list = apsByHourlyRate[hr];
|
|
var ii = new InvoiceItemDC();
|
|
|
|
decimal minutesTotal = 0;
|
|
DateTime start = DateTime.MaxValue;
|
|
DateTime end = DateTime.MinValue;
|
|
|
|
foreach (var approvalPeriod in list)
|
|
{
|
|
minutesTotal += approvalPeriod.RecordedMinutes;
|
|
if (approvalPeriod.PeriodStartDate < start)
|
|
start = approvalPeriod.PeriodStartDate;
|
|
if (approvalPeriod.PeriodEndDate > end)
|
|
end = approvalPeriod.PeriodEndDate;
|
|
}
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = end;
|
|
ii.RateFactor = si.RateFactor;
|
|
ii.AmountPerUnit = hr;
|
|
decimal hours = minutesTotal / 60m;
|
|
//hours *= rf;
|
|
hours = Math.Round(hours, 2, MidpointRounding.AwayFromZero);
|
|
ii.UnitCount = hours;
|
|
ii.AmountTotal = Math.Round(hr * hours, 2, MidpointRounding.AwayFromZero);
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
|
|
iiList.Add(ii);
|
|
}
|
|
if (rf > 0)
|
|
{
|
|
// 28.10.2024 Änderung von Summe der pauschalen Leistungen zu Stundensatz x Faktor und Zwischenrundung
|
|
decimal totalAmountFW = 0;
|
|
decimal totalAmountAF = 0;
|
|
int rfFahrzeit = (int)Math.Min(20, rf);
|
|
decimal rfAusfall = rf - rfFahrzeit;
|
|
DateTime? start = DateTime.MaxValue;
|
|
DateTime? end = DateTime.MinValue;
|
|
decimal hours = 0;
|
|
decimal hr = 0;
|
|
|
|
foreach (var iii in iiList)
|
|
{
|
|
if (iii.ItemPeriodStart < start)
|
|
start = iii.ItemPeriodStart;
|
|
if (iii.ItemPeriodEnd > end)
|
|
end = iii.ItemPeriodEnd;
|
|
hours = iii.UnitCount.Value;
|
|
hr = iii.AmountPerUnit.Value;
|
|
totalAmountFW += Math.Round(Math.Round(hr * rfFahrzeit / 100, 2, MidpointRounding.AwayFromZero) * hours, 2, MidpointRounding.AwayFromZero);
|
|
totalAmountAF += Math.Round(Math.Round(hr * rfAusfall / 100, 2, MidpointRounding.AwayFromZero) * hours, 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
|
|
//zzgl. Fahr+Wegezeiten (20 %)
|
|
var ii = new InvoiceItemDC();
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = end;
|
|
ii.RateFactor = rfFahrzeit;
|
|
ii.AmountPerUnit = totalAmountFW;
|
|
ii.UnitCount = 1;
|
|
ii.AmountTotal = totalAmountFW;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.ItemDescription = "Fahrt- und Wegezeiten";
|
|
iiList.Add(ii);
|
|
|
|
//zzgl. Ausfallzeiten (5 %)
|
|
if (rf > 0)
|
|
{
|
|
ii = new InvoiceItemDC();
|
|
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = end;
|
|
ii.RateFactor = rf;
|
|
ii.AmountPerUnit = totalAmountAF;
|
|
ii.UnitCount = 1;
|
|
ii.AmountTotal = totalAmountAF;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.ItemDescription = "Ausfallzeiten Fehlkontakte";
|
|
|
|
iiList.Add(ii);
|
|
}
|
|
}
|
|
|
|
return iiList.OrderBy(ii => ii.ItemPeriodStart).ToList();
|
|
}
|
|
|
|
private decimal GetRateFactor(Settlement2DC si)
|
|
{
|
|
decimal rf = 0;
|
|
if (si.RateFactor.HasValue && si.RateFactor.Value != 0)
|
|
{
|
|
rf = si.RateFactor.Value;
|
|
}
|
|
return rf;
|
|
}
|
|
|
|
private decimal? CalculateApprovedTotalFLS(IList<ApprovalPeriod> apList)
|
|
{
|
|
decimal flsTotal = 0;
|
|
foreach (var ap in apList)
|
|
{
|
|
flsTotal += ap.ApprovedTotal;
|
|
}
|
|
return flsTotal;
|
|
}
|
|
|
|
public override void InitApprovalPeriod(Settlement2DC si, ApprovalPeriod approvalPeriod)
|
|
{
|
|
base.InitApprovalPeriod(si, approvalPeriod);
|
|
approvalPeriod.Weeks = Math.Round(approvalPeriod.Days * 0.1427m, 2, MidpointRounding.AwayFromZero);
|
|
if (approvalPeriod.ApprovalUnit == AccountingIntervalType.Weekly)
|
|
{
|
|
approvalPeriod.ApprovedTotal = Math.Round(approvalPeriod.Weeks * approvalPeriod.ApprovedPerUnit, 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
approvalPeriod.ApprovedAmountTotal = Math.Round(approvalPeriod.ApprovedTotal * approvalPeriod.AmountPerUnit, 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
}
|
|
}
|