333 lines
12 KiB
C#
333 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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.DataContracts.Compact;
|
|
using BS.Shared.Extensions;
|
|
using BS.Shared.Services;
|
|
|
|
namespace ProfEggersStiftung.Invoicing
|
|
{
|
|
|
|
public class PflegeInvoiceCreation : InvoiceCreation
|
|
{
|
|
private DateTime? accountingPeriodStart = null;
|
|
private DateTime? accountingPeriodEnd = null;
|
|
|
|
public override void SetInvoiceId(ServiceInvoice invoice)
|
|
{
|
|
invoice.InvoiceBase.InvoiceId = "RechnungPflege";
|
|
invoice.InvoiceBase.InvoiceTypeText = "Pflegekostenrechnung";
|
|
}
|
|
|
|
|
|
|
|
public override void SetInvoiceBaseData(int invoiceCounter, ServiceInvoice invoice, CostBearer costBearer, DateTimeSpan invoicePeriod,
|
|
IList<CompactSupportConceptDC> supportConceptList)
|
|
{
|
|
if (invoicePeriod != null)
|
|
{
|
|
accountingPeriodStart = invoicePeriod.StartDate;
|
|
accountingPeriodEnd = invoicePeriod.EndDate;
|
|
}
|
|
base.SetInvoiceBaseData(invoiceCounter, invoice, costBearer, invoicePeriod, supportConceptList);
|
|
}
|
|
|
|
public override void SetInvoiceItems(ServiceInvoice invoice,
|
|
ServiceInvoicePeriod serviceInvoicePeriod,
|
|
SupportConceptApprovalPeriodDC supportConceptApprovalPeriod,
|
|
CostBearer2SupportConcept cb2sc,
|
|
DateTimeSpan invoicePeriod,
|
|
List<ServiceRecordDC> serviceRecords)
|
|
{
|
|
serviceInvoicePeriod.ApprovedFixedAmount = null;
|
|
var itemList = CreateInvoiceItems(null, invoicePeriod, serviceInvoicePeriod.SupportConceptApprovalPeriod, null);
|
|
foreach (var item in itemList)
|
|
{
|
|
serviceInvoicePeriod.InvoiceItemList.Add(item);
|
|
}
|
|
|
|
}
|
|
|
|
public override IList<InvoiceItem> CreateInvoiceItems(List<ServiceRecordDC> serviceRecordsInPeriod, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap,
|
|
Calculations calc)
|
|
{
|
|
|
|
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;
|
|
if (scap.EndDate >= invoicePeriod.EndDate)
|
|
ende = invoicePeriod.EndDate;
|
|
else
|
|
ende = (DateTime)scap.EndDate;
|
|
var monate = DateTimeUtils.GetTotalMonths(start, ende);
|
|
|
|
|
|
var tage = invoicePeriod.EndDate.Subtract(invoicePeriod.StartDate).Days + 1;
|
|
DateTime monatStart = new DateTime(start.Year, start.Month, 1);
|
|
DateTime monatEnde = monatStart.AddMonths(1);
|
|
var tageMonat = monatEnde.Subtract(monatStart).Days;
|
|
int tageAbwesendImMonat = 0;
|
|
decimal? sumCustomerMonth = 0;
|
|
foreach (var at in customer.AbsenceTimes)
|
|
{
|
|
if (at.Start.HasValue && at.Start.Value < monatEnde &&
|
|
(!at.End.HasValue || at.End.Value >= monatStart))
|
|
{
|
|
DateTime dt = at.Start.Value;
|
|
if (monatStart > dt)
|
|
{
|
|
dt = monatStart;
|
|
}
|
|
DateTime dtEnde = monatEnde.AddDays(-1);
|
|
|
|
if (at.End.HasValue && at.End.Value < dtEnde)
|
|
{
|
|
dtEnde = at.End.Value;
|
|
}
|
|
|
|
while (dt <= dtEnde)
|
|
{
|
|
tageAbwesendImMonat++;
|
|
dt = dt.AddDays(1);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
var satzAnwesend = GetTagespauschale(scap, "Anwesend", start);
|
|
|
|
var ii = new InvoiceItem();
|
|
ii.UnitCount = tageMonat - tageAbwesendImMonat;
|
|
ii.AmountPerUnit = satzAnwesend;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.ItemDescription = "Leistungsentgeltsatz";
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = ende;
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
|
|
var satzAbwesend = Math.Round(satzAnwesend * 0.8m, 2, MidpointRounding.AwayFromZero);
|
|
|
|
ii = new InvoiceItem();
|
|
ii.UnitCount = tageAbwesendImMonat;
|
|
ii.AmountPerUnit = satzAbwesend;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.MaxApprovedAmount = sumCustomerMonth;
|
|
ii.ItemDescription = "Leistungsentgeltsatz abwesend";
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = ende;
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
|
|
var pauschale = GetTagespauschale(null, "Bekleidungsgeld", start);
|
|
|
|
ii = new InvoiceItem();
|
|
ii.UnitCount = tageMonat;
|
|
ii.AmountPerUnit = pauschale;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.ItemDescription = "Bekleidungsgeld";
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = ende;
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
var betrag = GetTagespauschale(null, "Barbetrag", start);
|
|
|
|
//Barbetrag * AnzahlMonate / tage wenn Bewilligung nicht Anfang oder Ende des Monats startet
|
|
|
|
// Debitor Nr. für Bekleidungsgeld und Taschengeld (Barbetrag) hinterlegen
|
|
|
|
|
|
|
|
ii = new InvoiceItem();
|
|
ii.UnitCount = 1;
|
|
ii.AmountPerUnit = betrag;
|
|
ii.AmountTotal = ii.UnitCount * ii.AmountPerUnit;
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.ItemDescription = "Barbetrag";
|
|
ii.ItemPeriodStart = start;
|
|
ii.ItemPeriodEnd = ende;
|
|
ii.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
var list = CreateVarFieldInvoiceItems(customer, scap, invoicePeriod);
|
|
ergebnis.AddRange(list);
|
|
|
|
list = CreateBetragServiceRecordInvoiceItems(scap, serviceRecordsInPeriod, invoicePeriod);
|
|
ergebnis.AddRange(list);
|
|
|
|
|
|
|
|
|
|
return ergebnis;
|
|
}
|
|
|
|
protected List<InvoiceItem> CreateVarFieldInvoiceItems(Customer customer, SupportConceptApprovalPeriod scap, DateTimeSpan invoicePeriod)
|
|
{
|
|
var iiList = new List<InvoiceItem>();
|
|
|
|
String leistung1 = "";
|
|
decimal betrag1 = 0;
|
|
String leistung2 = "";
|
|
decimal betrag2 = 0;
|
|
|
|
foreach (var vv in customer.VarFieldValueList)
|
|
{
|
|
var varFieldValue = BS.Shared.Core.Utils.XMLDeserializeFromString(vv.SerializedValue, Type.GetType(vv.TypeName));
|
|
if (vv.VarFieldDefOid.Value == 3)
|
|
{
|
|
leistung1 = (String)varFieldValue;
|
|
}
|
|
else if (vv.VarFieldDefOid.Value == 4)
|
|
{
|
|
if (varFieldValue != null)
|
|
{
|
|
betrag1 = (decimal)varFieldValue;
|
|
}
|
|
}
|
|
else if (vv.VarFieldDefOid.Value == 5)
|
|
{
|
|
leistung2 = (String)varFieldValue;
|
|
}
|
|
else if (vv.VarFieldDefOid.Value == 6)
|
|
{
|
|
if (varFieldValue != null)
|
|
{
|
|
betrag2 = (decimal)varFieldValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
var ii = CreateInvoiceItem(leistung1, betrag1, scap, invoicePeriod);
|
|
if (ii != null)
|
|
{
|
|
iiList.Add(ii);
|
|
}
|
|
ii = CreateInvoiceItem(leistung2, betrag2, scap, invoicePeriod);
|
|
if (ii != null)
|
|
{
|
|
iiList.Add(ii);
|
|
}
|
|
return iiList;
|
|
}
|
|
|
|
protected InvoiceItem CreateInvoiceItem(String text, decimal betrag, SupportConceptApprovalPeriod scap, DateTimeSpan invoicePeriod)
|
|
{
|
|
if (betrag == 0 || String.IsNullOrEmpty(text))
|
|
{
|
|
return null;
|
|
}
|
|
var invoiceItem = new InvoiceItem();
|
|
invoiceItem.UnitCount = 1;
|
|
invoiceItem.AmountPerUnit = betrag;
|
|
invoiceItem.AmountTotal = invoiceItem.AmountPerUnit;
|
|
invoiceItem.ItemPeriodStart = invoicePeriod.StartDate;
|
|
invoiceItem.ItemPeriodEnd = invoicePeriod.EndDate;
|
|
invoiceItem.GrossAmountTotal = invoiceItem.AmountPerUnit;
|
|
invoiceItem.ItemDescription = text;
|
|
|
|
invoiceItem.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
return invoiceItem;
|
|
}
|
|
|
|
protected List<InvoiceItem> CreateBetragServiceRecordInvoiceItems(SupportConceptApprovalPeriod scap, List<ServiceRecordDC> serviceRecordsInPeriod, DateTimeSpan invoicePeriod)
|
|
{
|
|
var iiList = new List<InvoiceItem>();
|
|
|
|
if (serviceRecordsInPeriod != null)
|
|
{
|
|
foreach (ServiceRecordDC iServiceRecord in serviceRecordsInPeriod)
|
|
{
|
|
if (iServiceRecord.Betrag.HasValue)
|
|
{
|
|
var invoiceItem = new InvoiceItem();
|
|
invoiceItem.UnitCount = 1;
|
|
invoiceItem.AmountPerUnit = iServiceRecord.Betrag;
|
|
invoiceItem.AmountTotal = invoiceItem.AmountPerUnit;
|
|
invoiceItem.ItemPeriodStart = invoicePeriod.StartDate;
|
|
invoiceItem.ItemPeriodEnd = invoicePeriod.EndDate;
|
|
invoiceItem.GrossAmountTotal = invoiceItem.AmountPerUnit;
|
|
invoiceItem.ItemDescription = iServiceRecord.Notice;
|
|
|
|
invoiceItem.ServiceRecord = DAOFactory.GenericDAO.LoadByID<ServiceRecord>(iServiceRecord.ServiceRecordOid.Value);
|
|
|
|
if (scap != null)
|
|
invoiceItem.SupportConceptApprovalPeriodOid = scap.Oid;
|
|
|
|
iiList.Add(invoiceItem);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
return iiList;
|
|
}
|
|
|
|
private decimal GetTagespauschale(SupportConceptApprovalPeriod scap, String preisBezeichnung, DateTime datum)
|
|
{
|
|
if (scap != null && scap.ServiceCategory != null)
|
|
{
|
|
var crList = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(scap.ServiceCategory.CostRatePeriods);
|
|
|
|
var crp = crList.GetCostRatePeriodForDate(BS.Shared.CostRatePeriodType.AmountOfMoney, datum);
|
|
if (crp != null && crp.CostRateValue.HasValue)
|
|
{
|
|
return crp.CostRateValue.Value;
|
|
}
|
|
}
|
|
var preise = DAOFactory.GenericDAO.GetAllActive<Preis>();
|
|
|
|
return GetPreis(preise, preisBezeichnung, datum);
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
public override IList<InvoiceItem> CreateSummaryInvoiceItems(IList<InvoiceItem> itemList, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap, Calculations calc, bool summaryPerMonth, bool addRateFactorToUnitCount)
|
|
{
|
|
return base.CreateSummaryInvoiceItems(itemList, invoicePeriod, scap, calc, true, true);
|
|
}
|
|
}
|
|
}
|