230 lines
9.1 KiB
C#
230 lines
9.1 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 DomizielAnsbach.Invoicing
|
|
{
|
|
public class StationaerInvoiceCreation : InvoiceCreation
|
|
{
|
|
//private const decimal TAGESPAUSCHALE = 129.55m;
|
|
//private const decimal FREIHALTEGEBUEHR = 103.64m;
|
|
|
|
|
|
private DateTime? accountingPeriodStart = null;
|
|
private DateTime? accountingPeriodEnd = null;
|
|
|
|
public override void SetInvoiceId(ServiceInvoice invoice)
|
|
{
|
|
invoice.InvoiceBase.InvoiceId = "RgBezirkDetail";
|
|
invoice.InvoiceBase.InvoiceTypeText = "Sammelrechnung Bezirk";
|
|
}
|
|
|
|
public override List<ServiceInvoiceDC> GenerateServiceInvoices(long costBearerOid,
|
|
DateTimeSpan invoicePeriod,
|
|
Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>> supportConcepts2ServiceRecords)
|
|
{
|
|
var costBearer = DAOFactory.GenericDAO.LoadByID<CostBearer>(costBearerOid);
|
|
var invoiceList = new List<ServiceInvoice>();
|
|
ServiceInvoice invoice;
|
|
|
|
invoice = CreateCollectiveBilling2(costBearer, invoicePeriod, supportConcepts2ServiceRecords);
|
|
if (invoice != null)
|
|
invoiceList.Add(invoice);
|
|
|
|
var result = invoiceList.Select(item => MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(item)).ToList();
|
|
return result.Count > 1 ? result.OrderBy(i => i.InvoiceBase.CustomerLastName).ToList() : result;
|
|
}
|
|
|
|
private ServiceInvoice CreateCollectiveBilling2(CostBearer costBearer, DateTimeSpan invoicePeriod, Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>> supportConcepts2ServiceRecords)
|
|
{
|
|
var invoice = new ServiceInvoice();
|
|
var newDict = new Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>>();
|
|
|
|
foreach (var sc2sr in supportConcepts2ServiceRecords)
|
|
{
|
|
if (sc2sr.Key.IsApproved)
|
|
newDict.Add(sc2sr.Key, sc2sr.Value);
|
|
}
|
|
|
|
SetRecipientInformation(invoice, costBearer);
|
|
SetSenderInformation(invoice);
|
|
SetInvoiceBaseData(0, invoice, costBearer, invoicePeriod, newDict.Keys.ToList());
|
|
SetCollectiveBillingBaseData(0, invoice, costBearer, invoicePeriod, newDict);
|
|
SetCollectiveBillingServiceInvoicePeriods(invoice, invoicePeriod, newDict);
|
|
SetServiceInvoicePeriodAmounts(invoice);
|
|
|
|
SetAmountAdvancePayments(invoice);
|
|
|
|
return invoice;
|
|
}
|
|
|
|
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);
|
|
serviceInvoicePeriod.InvoiceItemList.AddRange(itemList);
|
|
}
|
|
|
|
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 preise = DAOFactory.GenericDAO.GetAllActive<Preis>();
|
|
|
|
var tagessatz = GetPreis(preise, "Tagessatz stationär", start);
|
|
var freihalte = GetPreis(preise, "Platzfreihaltegebühr", start);
|
|
|
|
//Tagessatz stationär
|
|
//Platzfreihaltegebühr
|
|
|
|
//Jungkind ist am 1.ausgezogen, trotzdem werden 31 Tage berechnet
|
|
|
|
//Abwesenheitstage nur im Monat auswerten.Bsp.Bojko
|
|
|
|
|
|
var monate = DateTimeUtils.GetTotalMonths(start, ende);
|
|
|
|
while (start <= ende)
|
|
{
|
|
|
|
DateTime monatStart = new DateTime(start.Year, start.Month, 1);
|
|
DateTime monatEnde = monatStart.AddMonths(1);
|
|
|
|
var tageMonat = monatEnde.Subtract(start).Days;
|
|
|
|
int tageAbwesendImMonat = 0;
|
|
decimal? sumCustomerMonth = 0;
|
|
|
|
foreach (var at in customer.AbsenceTimes)
|
|
{
|
|
DateTime? startAbw = at.Start;
|
|
DateTime? endMinusEinTag = at.End;
|
|
if (at.Start.HasValue && at.End.HasValue && at.End.Value.Date.Subtract(at.Start.Value.Date).TotalDays > 0)
|
|
{
|
|
endMinusEinTag = at.End.Value.AddDays(-1);
|
|
}
|
|
if (startAbw.HasValue && startAbw.Value < monatEnde && (!endMinusEinTag.HasValue || endMinusEinTag.Value >= monatStart))
|
|
{
|
|
DateTime abwStart = at.Start.Value;
|
|
if (start > abwStart)
|
|
{
|
|
abwStart = start;
|
|
}
|
|
|
|
DateTime abwEnd = monatEnde.AddDays(-1);
|
|
if (endMinusEinTag.HasValue && endMinusEinTag < monatEnde)
|
|
{
|
|
abwEnd = endMinusEinTag.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;
|
|
|
|
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;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
start = start.AddMonths(1);
|
|
start = new DateTime(start.Year, start.Month, 1);
|
|
}
|
|
|
|
return ergebnis;
|
|
}
|
|
|
|
private 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;
|
|
}
|
|
}
|
|
}
|