186 lines
7.6 KiB
C#
186 lines
7.6 KiB
C#
using BeWo.Data.Entities;
|
|
using BeWo.Service.Invoicing;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace ProIntegra.Invoicing
|
|
{
|
|
public class CustomSettlementInvoiceCreation : SettlementInvoiceCreation
|
|
{
|
|
public override bool CanCreateInvoiceTheOldWay(Settlement2DC si, CostBearer2SupportConcept c2s)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public override InvoiceItemDC CreateInvoiceItemForAccountingPeriod(AccountingPeriod accountingPeriod)
|
|
{
|
|
var item = base.CreateInvoiceItemForAccountingPeriod(accountingPeriod);
|
|
var mins = accountingPeriod.CostRatesInPeriod.FirstOrDefault(p => p.CostRateType == BS.Shared.CostRatePeriodType.MinutesPerServiceUnit);
|
|
|
|
if (mins != null && mins.CostRateValue != null)
|
|
{
|
|
item.UnitCount *= 60 / mins.CostRateValue;
|
|
item.UnitCountBudget *= 60 / mins.CostRateValue;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
|
|
public override List<InvoiceItemDC> CreateInvoiceItemsForAccountingPeriods(Settlement2DC si, List<AccountingPeriod> accountingPeriods,
|
|
List<CostRatePeriodDC> costRatePeriods, Calculations calc)
|
|
{
|
|
Dictionary<long, SupportConceptApprovalPeriodDC> oid2Scap = new Dictionary<long, SupportConceptApprovalPeriodDC>();
|
|
List<InvoiceItemDC> invoiceItems = new List<InvoiceItemDC>();
|
|
|
|
foreach (var ap in accountingPeriods)
|
|
{
|
|
InvoiceItemDC ii = CreateInvoiceItemForAccountingPeriod(ap);
|
|
invoiceItems.Add(ii);
|
|
|
|
// ANPASSUNG
|
|
if (si.RecipientOrganisation.ToLower().Contains("neu"))
|
|
{
|
|
InvoiceItemDC pauschalenItem = CreateKontaktpauschalenItem(ap, si.RecipientOrganisation);
|
|
if (pauschalenItem != null)
|
|
invoiceItems.Add(pauschalenItem);
|
|
}
|
|
|
|
|
|
var scap = ap.SupportConceptApprovalPeriod;
|
|
SupportConceptApprovalPeriodDC scapHelper = null;
|
|
|
|
if (scap != null)
|
|
{
|
|
if (oid2Scap.ContainsKey(scap.SupportConceptApprovalPeriodOid.Value))
|
|
scapHelper = oid2Scap[scap.SupportConceptApprovalPeriodOid.Value];
|
|
else
|
|
{
|
|
scapHelper = new SupportConceptApprovalPeriodDC();
|
|
scapHelper.ApprovedBETotal = scap.ApprovedBETotal;
|
|
if (!scapHelper.ApprovedBETotal.HasValue)
|
|
scapHelper.ApprovedBETotal = calc.GetApprovedBETotal(scap, costRatePeriods);
|
|
|
|
oid2Scap[scap.SupportConceptApprovalPeriodOid.Value] = scapHelper;
|
|
}
|
|
}
|
|
|
|
if (!si.ApprovedFLS.HasValue)
|
|
{
|
|
if (scapHelper.ApprovedBETotal.HasValue && ii.UnitCount.HasValue)
|
|
{
|
|
if (scapHelper.ApprovedBETotal >= ii.UnitCount.Value)
|
|
{
|
|
scapHelper.ApprovedBETotal -= ii.UnitCount.Value;
|
|
ii.MaxApprovedUnitCount = null;
|
|
}
|
|
else
|
|
{
|
|
ii.MaxApprovedUnitCount = scapHelper.ApprovedBETotal;
|
|
}
|
|
}
|
|
|
|
CalculateInvoiceItemAmounts(ii);
|
|
}
|
|
|
|
if (ii.UnitCount.HasValue)
|
|
{
|
|
si.RecordedBillableFLSBrutto = (si.RecordedBillableFLSBrutto ?? 0) + ii.UnitCount;
|
|
si.RecordedBillableFLSNetto = si.RecordedBillableFLSBrutto ?? 0;
|
|
}
|
|
|
|
var list = CreateAdditionalInvoiceItems(ap);
|
|
invoiceItems.AddRange(list);
|
|
}
|
|
return invoiceItems;
|
|
}
|
|
|
|
public override void CalculateInvoiceItemAmounts(Settlement2DC si)
|
|
{
|
|
decimal flsBudgetGesamt = si.ApprovedFLS ?? 0;
|
|
decimal flsAufgebraucht = 0;
|
|
|
|
foreach (var ii in si.InvoiceItems)
|
|
{
|
|
// Anpassung: bei Kontaktpauschale nichts vom Budget abziehen
|
|
if (ii.ItemDescription == "Kontaktpauschale")
|
|
continue;
|
|
|
|
#region standard
|
|
if (ii.UnitCount.HasValue)
|
|
{
|
|
if (ii.UnitCount > 0)
|
|
{
|
|
var unitCountBudget = Math.Round(ii.UnitCountBudget ?? ii.UnitCount ?? 0, 2, MidpointRounding.AwayFromZero);
|
|
ii.UnitCount = Math.Round(ii.UnitCount.Value, 2, MidpointRounding.AwayFromZero);
|
|
|
|
var relation = unitCountBudget / ii.UnitCount.Value;
|
|
var gewichteteStunden = ii.UnitCount * relation;
|
|
if (gewichteteStunden > flsBudgetGesamt - flsAufgebraucht)
|
|
{
|
|
gewichteteStunden = flsBudgetGesamt - flsAufgebraucht;
|
|
ii.UnitCount = gewichteteStunden / relation;
|
|
}
|
|
flsAufgebraucht += ii.UnitCount.Value * relation;
|
|
}
|
|
|
|
if (ii.AmountPerUnit.HasValue)
|
|
{
|
|
if (!String.IsNullOrEmpty(ii.ItemDescription) && ii.ItemDescription == "Fehlkontakte" && ii.ProzentAbrechenbar.HasValue)
|
|
{
|
|
ii.AmountPerUnit = ii.AmountPerUnit * ii.ProzentAbrechenbar / 100;
|
|
}
|
|
ii.AmountTotal = ii.UnitCount.Value * ii.AmountPerUnit.Value;
|
|
if (ii.RateFactor.HasValue && ii.RateFactor.Value != 0)
|
|
ii.GrossAmountTotal = Math.Round(ii.AmountTotal.Value * ((100m + ii.RateFactor.Value) / 100m), 2,
|
|
MidpointRounding.AwayFromZero);
|
|
else
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
|
|
}
|
|
if (ii.MaxApprovedUnitCount < ii.UnitCount)
|
|
{
|
|
ii.MaxApprovedUnitCount = ii.UnitCount;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
private static InvoiceItemDC CreateKontaktpauschalenItem(AccountingPeriod accountingPeriod, string recipientOrganisation)
|
|
{
|
|
InvoiceItemDC invoiceItem = null;
|
|
decimal unitCount = 0;
|
|
|
|
foreach (var sr in accountingPeriod.ServiceRecordsInPeriod)
|
|
{
|
|
if (sr.ServiceDescription.Name.ToLower().Contains("face to face") || sr.ServiceDescription.Name.ToLower().Contains("face to face"))
|
|
{
|
|
unitCount++;
|
|
}
|
|
}
|
|
|
|
if (unitCount > 0)
|
|
{
|
|
decimal pauschale = PauschalenHelper.PAUSCHALE;
|
|
|
|
invoiceItem = new InvoiceItemDC()
|
|
{
|
|
UnitCount = unitCount,
|
|
ItemDescription = "Kontaktpauschale",
|
|
UnitDescription = "Pauschale pro direktem Kontakt",
|
|
AmountPerUnit = pauschale,
|
|
AmountTotal = pauschale * unitCount,
|
|
GrossAmountTotal = pauschale * unitCount,
|
|
ItemPeriodStart = accountingPeriod.PeriodStart,
|
|
ItemPeriodEnd = accountingPeriod.PeriodEnd,
|
|
};
|
|
}
|
|
|
|
return invoiceItem;
|
|
}
|
|
}
|
|
}
|