211 lines
10 KiB
C#
211 lines
10 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 PflegedienstWessel.Invoicing
|
|
{
|
|
|
|
public class CustomInvoiceCreation : InvoiceCreation
|
|
{
|
|
private DateTime? accountingPeriodStart = null;
|
|
private DateTime? accountingPeriodEnd = null;
|
|
|
|
|
|
public override List<ServiceInvoiceDC> GenerateServiceInvoices(long costBearerOid, DateTimeSpan invoicePeriod, Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>> supportConcepts2ServiceRecords)
|
|
{
|
|
// bei den weiteren Kostenträgern (außer Persönliches Budget 24 Stunden) wird je eine Rechnung pro Leistungskategorie benötigt
|
|
// -> es werden für jeden Kunden pro Kostenträger Rechnungen erstellt und hintereinandergefügt
|
|
var invoiceList = new List<ServiceInvoice>();
|
|
var invoiceCounter = 0;
|
|
var sortedScs = supportConcepts2ServiceRecords.Keys.OrderBy(s => s.Customer != null ? s.Customer.LastNameFirstName : "").ToList();
|
|
|
|
foreach (var sc in sortedScs)
|
|
{
|
|
var serviceRecords = supportConcepts2ServiceRecords[sc];
|
|
serviceRecords = serviceRecords.Where(s => !s.ServiceDescription.CategoryName.Contains("Fachleistung")).ToList(); // die sind in Spitzabrechnung
|
|
if (sc.CostBearerList.Count > 0)
|
|
{
|
|
var costBearer = DAOFactory.GenericDAO.LoadByID<CostBearer>(sc.CostBearerList[0].CostBearerOid);
|
|
Dictionary<string, List<ServiceRecordDC>> srDict = new Dictionary<string, List<ServiceRecordDC>>();
|
|
|
|
foreach (var sr in serviceRecords)
|
|
{
|
|
String key = sr.ServiceDescription.CategoryName;
|
|
|
|
if (!srDict.ContainsKey(key))
|
|
srDict.Add(key, new List<ServiceRecordDC>());
|
|
|
|
srDict[key].Add(sr);
|
|
}
|
|
|
|
foreach (var catName in srDict.Keys)
|
|
{
|
|
var list = srDict[catName];
|
|
var invoice = CreateSingleInvoice(invoiceCounter, costBearer, invoicePeriod, sc, list);
|
|
|
|
if (invoice != null)
|
|
{
|
|
invoice.InvoiceBase.InvoiceId = "Service";
|
|
invoiceList.Add(invoice);
|
|
invoiceCounter++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var result = invoiceList.Select(item => MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(item)).ToList();
|
|
return result;
|
|
}
|
|
|
|
|
|
public override ServiceInvoice CreateSingleInvoice(int invoiceCounter, CostBearer costBearer, DateTimeSpan invoicePeriod, CompactSupportConceptDC supportConcept, List<ServiceRecordDC> serviceRecords)
|
|
{
|
|
var i = base.CreateSingleInvoice(invoiceCounter, costBearer, invoicePeriod, supportConcept, serviceRecords);
|
|
|
|
if (i != null)
|
|
{
|
|
var claim = i.GetTotalClaim();
|
|
|
|
if (!claim.HasValue || claim.Value == 0)
|
|
return null;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
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 SetRecipientInformation(ServiceInvoice serviceInvoice, CostBearer costBearer)
|
|
{
|
|
if (costBearer.Organisation.Name.ToLower().Contains("persönliches Budget"))
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientDivision = costBearer.Organisation.Name2;
|
|
serviceInvoice.InvoiceBase.RecipientOrganisationOid = costBearer.Organisation.Oid;
|
|
serviceInvoice.InvoiceBase.RecipientOrganisation = costBearer.Organisation.Name;
|
|
serviceInvoice.InvoiceBase.RecipientContacts = costBearer.Organisation.Contacts.Select(i => i.CopyToNew()).ToList();
|
|
serviceInvoice.InvoiceBase.RecipientCostBearerOid = costBearer.Oid;
|
|
|
|
if (serviceInvoice.InvoiceBase.CostBearer2SupportConcept != null)
|
|
{
|
|
var cust = serviceInvoice.InvoiceBase.CostBearer2SupportConcept.SupportConcept.Customer;
|
|
|
|
if (cust.Person.InvoiceAddress != null)
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientAddress = cust.Person.InvoiceAddress.CopyToNew();
|
|
}
|
|
else if (cust.Person.Address != null)
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientAddress = cust.Person.Address.CopyToNew();
|
|
}
|
|
|
|
if (cust.Person.InvoiceAddress != null && cust.Person.InvoiceAddress.AddressLine1 != null)
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientPersonFirstName = cust.Person.InvoiceAddress.AddressLine1;
|
|
serviceInvoice.InvoiceBase.RecipientPersonLastName = "";
|
|
}
|
|
else
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientPersonFirstName = cust.Person.FirstName;
|
|
serviceInvoice.InvoiceBase.RecipientPersonLastName = cust.Person.LastName;
|
|
}
|
|
return;
|
|
}
|
|
if (costBearer.Organisation.InvoiceAddress != null)
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientAddress = costBearer.Organisation.InvoiceAddress.CopyToNew();
|
|
}
|
|
else if (costBearer.Organisation.Address != null)
|
|
{
|
|
serviceInvoice.InvoiceBase.RecipientAddress = costBearer.Organisation.Address.CopyToNew();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
base.SetRecipientInformation(serviceInvoice, costBearer);
|
|
}
|
|
}
|
|
|
|
public override void SetServiceInvoicePeriods(ServiceInvoice invoice, CostBearer2SupportConcept costBearer2SupportConcept,
|
|
DateTimeSpan invoicePeriod, List<ServiceRecordDC> serviceRecords)
|
|
{
|
|
var invoiceAccountingPeriodSpan = new DateTimeSpan
|
|
{
|
|
StartDateTime = invoice.InvoiceBase.AccountingPeriodStart.Value,
|
|
EndDateTime = invoice.InvoiceBase.AccountingPeriodEnd.Value
|
|
};
|
|
|
|
foreach (SupportConceptApprovalPeriod iPeriod in costBearer2SupportConcept.ApprovalPeriodList)
|
|
{
|
|
SupportConceptApprovalPeriodDC approvalPeriodDC = MapperFactory.SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod.MapToNewDC(iPeriod);
|
|
|
|
|
|
if (approvalPeriodDC.Span.EndDateTime < invoiceAccountingPeriodSpan.StartDateTime || approvalPeriodDC.Span.StartDateTime > invoiceAccountingPeriodSpan.EndDate)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ServiceInvoicePeriod serviceInvoicePeriod = CreateServiceInvoicePeriod(approvalPeriodDC, invoiceAccountingPeriodSpan);
|
|
serviceInvoicePeriod.SupportConceptApprovalPeriod = iPeriod;
|
|
serviceInvoicePeriod.Customer = costBearer2SupportConcept.SupportConcept.Customer;
|
|
|
|
CostBearer costBearer = costBearer2SupportConcept.CostBearer;
|
|
List<CostRatePeriodDC> costRates = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(costBearer.CostRatePeriods);
|
|
|
|
CostRatePeriodDC serviceUnit = costRates.GetCostRatePeriodForDate(CostRatePeriodType.MinutesPerServiceUnit, DateTime.Now);
|
|
serviceInvoicePeriod.ServiceUnitName = serviceUnit.UnitName;
|
|
|
|
SetInvoiceItems(invoice, serviceInvoicePeriod, approvalPeriodDC, costBearer2SupportConcept, invoicePeriod, serviceRecords);
|
|
|
|
if (serviceInvoicePeriod.InvoiceItemList.Count > 0)
|
|
invoice.ServiceInvoicePeriodList.Add(serviceInvoicePeriod);
|
|
}
|
|
}
|
|
|
|
|
|
public override InvoiceItem CreateInvoiceItem(ServiceRecordDC iServiceRecord, SupportConceptApprovalPeriod scap, Calculations calc)
|
|
{
|
|
if (iServiceRecord.ServiceDescription.CategoryName.Contains("Fachleistung"))
|
|
return null;
|
|
|
|
var ii = base.CreateInvoiceItem(iServiceRecord, scap, calc);
|
|
DateTime start = ii.ItemPeriodStart.Value;
|
|
decimal preisLeistung = 0;
|
|
string catName = iServiceRecord.ServiceDescription.CategoryName.ToString();
|
|
|
|
var cat = DAOFactory.SearchDAO.FindServiceCategoryByName(catName);
|
|
if (cat != null)
|
|
{
|
|
var crps = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(cat.CostRatePeriods);
|
|
var cp = crps.GetCostRatePeriodForDate(CostRatePeriodType.AmountOfMoney, start);
|
|
if (cp != null && cp.CostRateValue.HasValue)
|
|
preisLeistung = (decimal)cp.CostRateValue;
|
|
ii.AmountPerUnit = preisLeistung;
|
|
ii.RateFactor = null;
|
|
ii.AmountTotal = preisLeistung * ii.UnitCount;
|
|
ii.GrossAmountTotal = preisLeistung * ii.UnitCount;
|
|
ii.ItemDescription = catName;
|
|
}
|
|
return ii;
|
|
}
|
|
}
|
|
}
|