212 lines
9.1 KiB
C#
212 lines
9.1 KiB
C#
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.DCEntityMapper;
|
|
using BS.Shared;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.Extensions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DomizielAnsbach.Invoicing
|
|
{
|
|
public class InvoiceHelper
|
|
{
|
|
public static IList<InvoiceItem> BerechneRechnungsPositionenFuerStationaer(DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap)
|
|
{
|
|
IList<InvoiceItem> ergebnis = new List<InvoiceItem>();
|
|
var customer = scap.CostBearer2SupportConcept.SupportConcept.Customer;
|
|
DateTime start;
|
|
DateTime ende;
|
|
if (scap.StartDate <= invoicePeriod.StartDate)
|
|
start = invoicePeriod.StartDate;
|
|
else
|
|
start = (DateTime)scap.StartDate; //Einzugstag wird mit abgerechnet
|
|
if (scap.EndDate >= invoicePeriod.EndDate)
|
|
ende = invoicePeriod.EndDate;
|
|
else
|
|
ende = (DateTime)scap.EndDate.Value.AddDays(-1); //Auszugstag wird NICHT abgerechnet
|
|
|
|
var preise = DAOFactory.GenericDAO.GetAllActive<Preis>();
|
|
|
|
while (start <= ende)
|
|
{
|
|
DateTime monatStart = start; //new DateTime(start.Year, start.Month, 1);
|
|
var tagessatz = GetPreis(preise, "Tagessatz stationär", monatStart);
|
|
var freihalte = GetPreis(preise, "Platzfreihaltegebühr", monatStart);
|
|
var fls2 = GetPreis(preise, "Fachleistung 2 Einzelzimmer klein", monatStart);
|
|
var fls2gross = GetPreis(preise, "Fachleistung 2, Einzelzimmer groß", monatStart);
|
|
DateTime monatEnde = monatStart.AddMonths(1); //new DateTime(start.Year, start.AddMonths(1).Month, 1); //ende.AddDays(1);
|
|
monatEnde = new DateTime(monatEnde.Year, monatEnde.Month, 1);
|
|
if (ende.AddDays(2) < monatStart.AddMonths(1))
|
|
{
|
|
monatEnde = ende.AddDays(1);
|
|
}
|
|
|
|
var tageMonat = monatEnde.Subtract(start).Days;
|
|
|
|
int tageAbwesendImMonat = 0;
|
|
decimal? sumCustomerMonth = 0;
|
|
|
|
foreach (var at in customer.AbsenceTimes)
|
|
{
|
|
DateTime? startAbw = at.Start;
|
|
DateTime? endAbw = at.End;
|
|
//18.04.2023: Frau Sachsenhauser sagt, jeder Tag Abwesenheit muss mit Platzfreihaltepauschale bezahlt werden
|
|
if (at.Start.HasValue && at.End.HasValue && at.End.Value.Date.Subtract(at.Start.Value.Date).TotalDays > 2)
|
|
{
|
|
endAbw = at.End.Value.AddDays(-1);
|
|
|
|
if (startAbw.HasValue && startAbw.Value < monatEnde && (!endAbw.HasValue || endAbw.Value >= monatStart)
|
|
&& !at.AbsenceReason.Description.Contains("ZV") && !at.AbsenceReason.Description.Contains("Urlaub stationär im Haus"))
|
|
{
|
|
DateTime abwStart = at.Start.Value;
|
|
if (start > abwStart)
|
|
{
|
|
abwStart = start;
|
|
}
|
|
|
|
DateTime abwEnd = monatEnde.AddDays(-1);
|
|
if (endAbw.HasValue && endAbw < monatEnde)
|
|
{
|
|
abwEnd = endAbw.Value;
|
|
}
|
|
DateTime dt = abwStart;
|
|
while (dt <= abwEnd)
|
|
{
|
|
tageAbwesendImMonat++;
|
|
dt = dt.AddDays(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var ii = new InvoiceItem();
|
|
ii.UnitCount = tageMonat - tageAbwesendImMonat;
|
|
ii.AmountPerUnit = tagessatz;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
sumCustomerMonth += ii.AmountTotal;
|
|
ii.MaxApprovedAmount = null;
|
|
ii.ItemDescription = "Klientenname";
|
|
ii.UnitDescription = "Vergütung";
|
|
ii.ItemPeriodStart = monatStart;
|
|
ii.Notice = monatStart.ToString("MMMM");
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
ii.ItemDescription = "";
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
ii = new InvoiceItem();
|
|
ii.UnitCount = tageAbwesendImMonat;
|
|
ii.AmountPerUnit = freihalte;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
sumCustomerMonth += ii.AmountTotal;
|
|
ii.MaxApprovedAmount = sumCustomerMonth;
|
|
ii.ItemDescription = "Klientenname";
|
|
ii.UnitDescription = "Platzfreihaltegeb.";
|
|
ii.ItemPeriodStart = monatStart;
|
|
ii.Notice = monatStart.ToString("MMMM");
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
ii.ItemDescription = GetAbwesenheiten(customer, monatStart, monatEnde);
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
var scapFls = scap.CostBearer2SupportConcept.ApprovalPeriodList.Where(ap => ap.ServiceCategory != null && ap.ServiceCategory.Name.Contains("Fachleistung") &&
|
|
ap.StartDate < invoicePeriod.EndDate && ap.EndDate > invoicePeriod.StartDate).FirstOrDefault();
|
|
|
|
if (scapFls != null && scapFls.ServiceCategory != null && scapFls.ServiceCategory.Name.Contains("Fachleistung"))
|
|
{
|
|
ii = new InvoiceItem();
|
|
ii.UnitCount = tageMonat;
|
|
if (scapFls.ServiceCategory.Name.Contains("klein"))
|
|
{
|
|
//ii.UnitDescription = "Fachleistung 2 - EZ klein";
|
|
ii.AmountPerUnit = fls2;
|
|
}
|
|
else
|
|
{
|
|
//ii.UnitDescription = "Fachleistung 2 - EZ groß";
|
|
ii.AmountPerUnit = fls2gross;
|
|
}
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
sumCustomerMonth += ii.AmountTotal;
|
|
ii.MaxApprovedAmount = sumCustomerMonth;
|
|
ii.UnitDescription = "Fachleistung 2";
|
|
ii.ItemPeriodStart = monatStart;
|
|
ii.Notice = monatStart.ToString("MMMM");
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
ii.ItemDescription = "";
|
|
|
|
ergebnis.Add(ii);
|
|
}
|
|
|
|
|
|
start = start.AddMonths(1);
|
|
start = new DateTime(start.Year, start.Month, 1);
|
|
}
|
|
|
|
return ergebnis;
|
|
}
|
|
|
|
private static decimal GetPreis(IList<Preis> allePreise, String name, DateTime datum)
|
|
{
|
|
var tagessatz = allePreise.Where(p => p.Name.Contains(name)).FirstOrDefault();
|
|
if (tagessatz != null)
|
|
{
|
|
var crlist = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(tagessatz.CostRatePeriods);
|
|
var cr = crlist.GetCostRatePeriodForDate(CostRatePeriodType.AmountOfMoney, datum);
|
|
if (cr != null && cr.CostRateValue.HasValue)
|
|
{
|
|
return cr.CostRateValue.Value;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static string GetAbwesenheiten(Customer c, DateTime start, DateTime end)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
if (c.AbsenceTimes != null)
|
|
{
|
|
foreach (var at in c.AbsenceTimes)
|
|
{
|
|
if ((!at.Start.HasValue || at.Start.Value <= end) && (!at.End.HasValue || at.End.Value >= start)
|
|
&& !at.AbsenceReason.Description.Contains("ZV") && !at.AbsenceReason.Description.Contains("Urlaub stationär im Haus"))
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append(", ");
|
|
}
|
|
|
|
sb.Append(String.Format("{0:dd.MM.}", at.Start));
|
|
if (!at.End.HasValue || at.End.Value > at.Start)
|
|
{
|
|
sb.Append(" bis ");
|
|
if (at.End.HasValue)
|
|
{
|
|
sb.Append(String.Format("{0:dd.MM.}", at.End));
|
|
}
|
|
else
|
|
{
|
|
sb.Append("unbekannt");
|
|
}
|
|
}
|
|
sb.Append(": " + at.AbsenceReason.Description);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
sb.Append("");
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|