285 lines
12 KiB
C#
285 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 DomizielAnsbach.Invoicing
|
|
{
|
|
public class CollectiveInvoiceCreation : InvoiceCreation
|
|
{
|
|
private DateTime? accountingPeriodStart = null;
|
|
private DateTime? accountingPeriodEnd = null;
|
|
|
|
//public virtual 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);
|
|
|
|
// if (invoicePeriod == null)
|
|
// 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 IList<InvoiceItem> CreateInvoiceItems(List<ServiceRecordDC> serviceRecordsInPeriod, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap,
|
|
Calculations calc)
|
|
{
|
|
return base.CreateInvoiceItems(serviceRecordsInPeriod, invoicePeriod, scap, calc);
|
|
}
|
|
|
|
public override InvoiceItem CreateInvoiceItem(ServiceRecordDC iServiceRecord, SupportConceptApprovalPeriod scap, Calculations calc)
|
|
{
|
|
var ii = base.CreateInvoiceItem(iServiceRecord, scap, calc);
|
|
if (iServiceRecord != null)
|
|
{
|
|
ii.ItemDescription = iServiceRecord.ServiceDescription.CategoryName;
|
|
}
|
|
return ii;
|
|
}
|
|
|
|
public override void CreateFixedAmounts(ServiceInvoicePeriod serviceInvoicePeriod, SupportConceptApprovalPeriodDC scap, CostBearer2SupportConcept cb2sc)
|
|
{
|
|
var calc = new CustomCalculations();
|
|
|
|
String cat = "";
|
|
if (scap.ServiceCategory != null)
|
|
{
|
|
cat = scap.ServiceCategory.Name;
|
|
}
|
|
if (cat.Contains("Fachleistung"))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var data = calc.GetFixedBillingData(scap, serviceInvoicePeriod.Start, serviceInvoicePeriod.End);
|
|
|
|
if (cat.ToLower().Contains("stationär"))
|
|
{
|
|
serviceInvoicePeriod.ApprovedFixedAmount = null;
|
|
DateTimeSpan span = new DateTimeSpan(accountingPeriodStart.Value, accountingPeriodEnd.Value);
|
|
|
|
var items = InvoiceHelper.BerechneRechnungsPositionenFuerStationaer(span, serviceInvoicePeriod.SupportConceptApprovalPeriod);
|
|
|
|
foreach (var item in items)
|
|
{
|
|
item.ItemPeriodEnd = serviceInvoicePeriod.End;
|
|
if (item.UnitDescription == "Vergütung")
|
|
{
|
|
item.UnitDescription = cat;
|
|
item.ItemDescription = cat;
|
|
}
|
|
else
|
|
{
|
|
item.ItemDescription = item.UnitDescription;
|
|
}
|
|
if (item.UnitCount.HasValue && item.UnitCount.Value > 0)
|
|
{
|
|
serviceInvoicePeriod.InvoiceItemList.Add(item);
|
|
}
|
|
}
|
|
|
|
}
|
|
else if (cat.ToLower().Contains("miete"))
|
|
{
|
|
// eigentlich Monatsweise Abrechnung wobei ein Monat aus 30.42 Tagen besteht
|
|
// bei späterem Beginn oder Krankheit werden die Tage einzeln abgezogen, bei früherem Ende wird der letzte Tag nicht berechnet
|
|
serviceInvoicePeriod.ApprovedFixedAmount = null;
|
|
DateTimeSpan span = new DateTimeSpan(accountingPeriodStart.Value, accountingPeriodEnd.Value);
|
|
|
|
IList<InvoiceItem> ergebnis = new List<InvoiceItem>();
|
|
var customer = cb2sc.SupportConcept.Customer;
|
|
|
|
DateTime start;
|
|
DateTime ende;
|
|
decimal tageMonat;
|
|
int count = 0;
|
|
decimal firstDays = 0;
|
|
|
|
if (scap.StartDate <= span.StartDate)
|
|
start = span.StartDate;
|
|
else
|
|
{
|
|
start = (DateTime)scap.StartDate;
|
|
// im Fall von Umzug (z.B. von EZ in DZ) darf die Summe der Tage beider Mieten - nicht die max. 30,42 Tage überschreiten
|
|
foreach (var i in cb2sc.ApprovalPeriodList)
|
|
{
|
|
if (i.EndDate >= span.StartDate && i.EndDate <= scap.StartDate && i.ServiceCategory.Name.ToLower().Contains("miete"))
|
|
{
|
|
firstDays = (int)(i.EndDate - span.StartDate).Value.TotalDays;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (scap.EndDate >= span.EndDate)
|
|
ende = span.EndDate;
|
|
else
|
|
{
|
|
// im Fall des Umzugs (z.B. von EZ in DZ) wird der letzte Tag der ersten Miete auch übernommen
|
|
foreach (var i in cb2sc.ApprovalPeriodList)
|
|
{
|
|
if (i.StartDate >= scap.EndDate && i.StartDate <= span.EndDate && i.ServiceCategory.Name.ToLower().Contains("miete"))
|
|
count += 1;
|
|
}
|
|
if (count >= 1)
|
|
ende = (DateTime)scap.EndDate;
|
|
|
|
else
|
|
{
|
|
ende = (DateTime)scap.EndDate;
|
|
ende.AddDays(-1);
|
|
}
|
|
}
|
|
|
|
tageMonat = ende.Subtract(start).Days;
|
|
if (scap.EndDate >= span.EndDate || count >= 1)
|
|
tageMonat += 1;
|
|
if (firstDays > 0 && firstDays + tageMonat >= 30.42m)
|
|
tageMonat = 30.42m - Convert.ToDecimal(firstDays);
|
|
|
|
var crpDcs = scap.ServiceCategory.CostRatePeriods;
|
|
var crp = crpDcs.GetCostRatePeriodForDate(CostRatePeriodType.AmountOfMoney, start);
|
|
decimal? monatssatz = 0;
|
|
|
|
if (crp != null && crp.CostRateValue.HasValue)
|
|
monatssatz = crp.CostRateValue;
|
|
var tagessatz = Convert.ToInt32(monatssatz) / 30.42;
|
|
|
|
while (start <= ende)
|
|
{
|
|
//int tageAbwesendImMonat = 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 < ende && (!endMinusEinTag.HasValue || endMinusEinTag.Value >= start))
|
|
// {
|
|
// DateTime abwStart = at.Start.Value;
|
|
// if (start > abwStart)
|
|
// {
|
|
// abwStart = start;
|
|
// }
|
|
|
|
// DateTime abwEnd = ende.AddDays(-1);
|
|
// if (endMinusEinTag.HasValue && endMinusEinTag < ende)
|
|
// {
|
|
// abwEnd = endMinusEinTag.Value;
|
|
// }
|
|
|
|
// DateTime dt = abwStart;
|
|
|
|
// while (start <= ende)
|
|
// {
|
|
// tageAbwesendImMonat++;
|
|
// dt = dt.AddDays(1);
|
|
// }
|
|
// }
|
|
//}
|
|
var ii = new InvoiceItem();
|
|
|
|
if (accountingPeriodStart.Value == start && accountingPeriodEnd.Value == ende)
|
|
{
|
|
ii.AmountTotal = monatssatz;
|
|
}
|
|
else
|
|
{
|
|
ii.UnitCount = tageMonat;
|
|
ii.AmountPerUnit = monatssatz;
|
|
ii.AmountTotal = ii.UnitCount * Convert.ToDecimal(tagessatz);
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
}
|
|
|
|
ii.GrossAmountTotal = ii.AmountTotal;
|
|
ii.MaxApprovedAmount = null;
|
|
ii.ItemDescription = cat;
|
|
ii.UnitDescription = cat;
|
|
ii.ItemPeriodStart = start;
|
|
ii.Notice = start.ToString("MMMM");
|
|
ii.SupportConceptApprovalPeriodOid = scap.SupportConceptApprovalPeriodOid;
|
|
|
|
ergebnis.Add(ii);
|
|
|
|
start = start.AddMonths(1);
|
|
start = new DateTime(start.Year, start.Month, 1);
|
|
|
|
foreach (var item in ergebnis)
|
|
{
|
|
item.ItemPeriodEnd = serviceInvoicePeriod.End;
|
|
serviceInvoicePeriod.InvoiceItemList.Add(item);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var billingData in data)
|
|
{
|
|
serviceInvoicePeriod.InvoiceItemList.Add(new InvoiceItem
|
|
{
|
|
AmountTotal = billingData.AmountTotal,
|
|
GrossAmountTotal = billingData.GrossAmountTotal,
|
|
ItemPeriodStart = billingData.BillingPeriodStart,
|
|
ItemPeriodEnd = billingData.BillingPeriodEnd,
|
|
AccountingInterval = billingData.AccountingInterval,
|
|
AmountPerUnit = billingData.AmountPerUnit,
|
|
UnitCount = billingData.UnitCount,
|
|
ItemDescription = cat
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|