Files

750 lines
21 KiB
C#
Raw Permalink Normal View History

2016-06-27 01:45:38 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using BeWo.Data;
using BeWo.Service.Plugins;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceContracts;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using Utils = BeWo.Service.Core.Utils;
using BeWo.Service.Invoicing;
using Dakota.Logic;
using Dakota;
using Dakota.Models;
2024-05-06 13:14:35 +02:00
using System.Net;
using System.Collections.Specialized;
using Newtonsoft.Json;
using BS.Shared.Extensions;
2024-10-25 17:30:27 +02:00
using BS.Shared.Exceptions;
2024-10-28 15:48:18 +01:00
using BS.Shared.AppSender;
2024-12-09 11:16:17 +01:00
using BeWo.Data.Security;
2025-01-20 15:54:17 +01:00
using System.IO;
2016-06-27 01:45:38 +02:00
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class AccountingServiceImp : IAccountingService
{
public string Tenant => MultitenancyOperationContextExt.Current?.Tenant;
public void DeactivateInvoiceBases(List<InvoiceBaseDC> invoices)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
s.DeactivateInvoiceBases(invoices);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public void StorniereInvoiceBases(List<InvoiceBaseDC> invoices)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
s.StorniereInvoiceBases(invoices);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public void DeactivateServiceInvoices(List<ServiceInvoiceDC> invoices)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
s.DeactivateServiceInvoices(invoices);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public void DeactivateSettlementInvoice(List<Settlement2DC> invoices)
{
try
{
ServiceLogic.SetActivationType<SettlementInvoice>(invoices.ToDictionary(i => i.SettlementIvoiceOid.Value, i => i.SettlementInvoiceVersion.Value), ActivationTypeId.Deleted);
ServiceLogic.SetActivationType<InvoiceBase>(invoices.ToDictionary(i => i.InvoiceBaseOid.Value, i => i.InvoiceBaseVersion.Value), ActivationTypeId.Deleted);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<ServiceInvoiceDC> GenerateInitialServiceInvoices(
long costBearerOid,
long? supportConceptOid,
DateTimeSpan invoicePeriod)
{
try
{
var acc = PluginLoader.FindClass<AccountingService>();
return acc.GenerateInitialServiceInvoices(costBearerOid, supportConceptOid, invoicePeriod);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetAllActiveInvoiceBases()
{
try
{
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(DAOFactory.GenericDAO.GetAllActive<InvoiceBase>());
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetInvoiceBases(DateTime? startDate, DateTime? endDate)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
return s.GetInvoiceBases(startDate, endDate);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetInvoiceBases2(DateTime? startDate, DateTime? endDate, bool filterByInvoiceDate)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
return s.GetInvoiceBases(startDate, endDate, filterByInvoiceDate);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetInvoiceBasesForCostBearerAndId(DateTime? startDate, DateTime? endDate, long costBearerOid, String invoiceId)
{
try
{
var s = PluginLoader.FindClass<AccountingService>();
return s.GetInvoiceBases(startDate, endDate, costBearerOid, invoiceId);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetInvoiceBasesForCostBearerAndIdAndIsUsed(DateTime? startDate, DateTime? endDate, long costBearerOid, String invoiceId)
{
try
{
var gkv_abrechnungen = DAOFactory.GenericDAO.GetAllActive<GkvAbrechnung>();
var s = PluginLoader.FindClass<AccountingService>();
var invoiceBasesFuerGkv = s.GetInvoiceBases(startDate, endDate, costBearerOid, invoiceId);
invoiceBasesFuerGkv.AddRange(s.GetInvoiceBases(startDate, endDate, costBearerOid, "RechnungNichtaerztlicheDialysesachleistung"));
invoiceBasesFuerGkv.AddRange(s.GetInvoiceBases(startDate, endDate, costBearerOid, "RechnungHaeuslicheKrankenpflege"));
HashSet<long> invoices = new HashSet<long>();
foreach (var gkv_abrechnung in gkv_abrechnungen)
{
foreach (var invoice in gkv_abrechnung.InvoiceBases)
{
invoices.Add(invoice.Oid.Value);
}
}
foreach (var invoice in invoiceBasesFuerGkv)
{
invoice.IsAlreadyInActiveGkvAbrechnung = invoices.Contains(invoice.InvoiceBaseOid.Value);
}
return invoiceBasesFuerGkv;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetGeneralInvoiceBasesForCustomer(long customerOid)
{
try
{
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(DAOFactory.SearchDAO.GetInvoiceBaseByCustomerOid(InvoiceType.General, customerOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetGeneralInvoiceBasesForOrganisation(long organisationOid)
{
try
{
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(DAOFactory.SearchDAO.GetInvoiceBaseByOrganisationOid(InvoiceType.General, organisationOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetGeneralInvoiceBasesForPerson(long personOid)
{
try
{
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(DAOFactory.SearchDAO.GetInvoiceBaseByPersonOid(InvoiceType.General, personOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceBaseDC> GetInvoiceBasesForSupportConcept(InvoiceType type, long supportConceptOid)
{
try
{
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(DAOFactory.SearchDAO.GetInvoiceBaseByTypeAndSupportConceptOid(type, supportConceptOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public ServiceInvoiceDC GetServiceInvoiceByInvoiceBaseOid(long invoiceBaseOid)
{
try
{
var dc = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(DAOFactory.SearchDAO.GetServiceInvoiceByInvoiceBaseOid(invoiceBaseOid));
SortServiceInvoicePeriods(dc);
return dc;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<ServiceInvoiceDC> GetServiceInvoices(long costBearerOid, long? supportConceptOid, DateTimeSpan period)
{
try
{
List<ServiceInvoice> entities = DAOFactory.SearchDAO.GetServiceInvoices(costBearerOid, supportConceptOid, period);
entities.Sort((x, y) =>
{
if (y.InvoiceBase.InvoiceDate.Value.Date.Equals(x.InvoiceBase.InvoiceDate.Value.Date))
{
return y.InvoiceBase.InvoiceNumber.CompareTo(x.InvoiceBase.InvoiceNumber);
}
return y.InvoiceBase.InvoiceDate.Value.Date.CompareTo(x.InvoiceBase.InvoiceDate.Value.Date);
});
var dcList = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDCs(entities);
foreach (var serviceInvoice in dcList)
{
SortServiceInvoicePeriods(serviceInvoice);
}
return dcList;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
private void SortServiceInvoicePeriods(ServiceInvoiceDC serviceInvoice)
{
if (serviceInvoice.ServiceInvoicePeriods != null)
{
serviceInvoice.ServiceInvoicePeriods.Sort((x, y) =>
{
if (x.Customer != null && y.Customer != null)
return x.Customer.ToString().CompareTo(y.Customer.ToString());
else if (x.Start != null && y.Start != null)
{
x.Start.Value.CompareTo(y.Start.Value);
}
return 0;
});
}
}
public Settlement2DC GetSettlementByInvoiceBaseOid(long invoiceBaseOid)
{
try
{
return MapperFactory.SettlementDC_SettlementInvoice.MapToNewDC(DAOFactory.SearchDAO.GetSettlementInvoiceByInvoiceBaseOid(invoiceBaseOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<Settlement2DC> GetSettlementInvoicesForCostBearerAndPeriod(long costBearerOid, DateTime periodStart, DateTime periodEnd)
{
try
{
return MapperFactory.SettlementDC_SettlementInvoice.MapToNewDCs(DAOFactory.SearchDAO.GetSettlementInvoicesForCostBearerAndPeriod(costBearerOid, periodStart, periodEnd));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<Settlement2DC> GetSettlementInvoicesForSupportConcept(long supportConceptOid)
{
try
{
return MapperFactory.SettlementDC_SettlementInvoice.MapToNewDCs(DAOFactory.SearchDAO.GetSettlementInvoiceBySupportConceptOid(supportConceptOid));
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> InsertNewInvoiceBases(List<InvoiceBaseDC> invoices)
{
try
{
if (invoices != null && LoggedInUserOperationContextExt.Current != null &&
LoggedInUserOperationContextExt.Current.User != null)
{
var loggedInUser = LoggedInUserOperationContextExt.Current.User;
foreach (var ib in invoices)
{
ib.SenderContactInformations = new List<ContactDC>();
foreach (var contact in loggedInUser.Employee.Person.Contacts)
{
var newContact = new ContactDC();
newContact.ContactType = contact.Type;
newContact.ContactValue = contact.Value;
ib.SenderContactInformations.Add(newContact);
}
}
}
var newInvoices = MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewEntities(invoices);
int count = 0;
var inc = PluginLoader.FindClass<InvoiceNumberGenerator>();
foreach (var ib in newInvoices)
{
if (ib.Type == InvoiceType.General)
{
foreach (var item in ib.InvoiceItems)
{
item.AmountTotal = Math.Round(item.AmountTotal ?? 0, 2, MidpointRounding.AwayFromZero);
}
}
var nextNumber = inc.GetNextInvoiceNumber(count, ib);
if (!String.IsNullOrEmpty(nextNumber))
{
if (!String.IsNullOrEmpty(ib.InvoiceNumber)) // Rechnungsnummer schon vorher erzeugt oder per Hand vergeben.
{
if (ib.InvoiceNumber == nextNumber)
//Die per Hand vergebene Rechnungsnummer ist gleich der nächsten berechneten Rechnungsnummer, daher Rechnungsnummerzähler erhöhen
{
count++;
}
}
else
{
ib.InvoiceNumber = nextNumber;
count++;
}
}
}
var factory = PluginLoader.FindClass<InvoiceFactory>();
if (factory != null)
{
foreach (var ib in newInvoices)
{
if (ib.Type == InvoiceType.CustomerEquity)
{
var creator = factory.CreateEquityInvoiceCreator(ib);
creator.InitEquityInvoice(ib);
}
else if (ib.Type == InvoiceType.General)
{
var creator = factory.CreateGeneralInvoiceCreator(ib);
creator.InitInvoice(ib);
}
}
}
DAOFactory.GenericDAO.Insert(newInvoices);
inc.IncreaseInvoiceNumber(count, newInvoices);
return newInvoices.Select(i => i.Oid.Value).ToList();
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> InsertNewServiceInvoices(List<ServiceInvoiceDC> invoices)
{
try
{
//Prüfe Flags
var neueRechnungen = new List<ServiceInvoiceDC>();
var diffRechnungen = invoices.Where(i => i.InvoiceBase.DiffErstellen).ToList();
//Bei neuer Rechung prüfen, ob eine Diff Rechnung vorher generiert wurde.
foreach (var item in invoices.Where(i => i.InvoiceBase.NeuErstellen))
{
if (item.OriginalZurDifferenz != null)
{
neueRechnungen.Add(item.OriginalZurDifferenz);
}
else
{
neueRechnungen.Add(item);
}
}
var newInvoices = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewEntities(neueRechnungen);
newInvoices.AddRange(MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewEntities(diffRechnungen));
if (newInvoices.Count > 0)
{
int count = 0;
var inc = PluginLoader.FindClass<InvoiceNumberGenerator>();
foreach (var i in newInvoices)
{
var nextNumber = inc.GetNextInvoiceNumber(count, i.InvoiceBase);
if (!String.IsNullOrEmpty(nextNumber))
{
//if (!String.IsNullOrEmpty(i.InvoiceBase.InvoiceNumber) && newInvoices.Count == 1)
// // Rechnungsnummer schon vorher erzeugt oder per Hand vergeben.
//{
// if (i.InvoiceBase.InvoiceNumber == nextNumber || newInvoices.Count > 1)
// //Die per Hand vergebene Rechnungsnummer ist gleich der nächsten berechneten Rechnungsnummer, daher Rechnungsnummerzähler erhöhen
// {
// count++;
// }
//}
//else
//{
i.InvoiceBase.InvoiceNumber = nextNumber;
count++;
//}
}
}
DAOFactory.GenericDAO.Insert(newInvoices);
inc.IncreaseInvoiceNumberForServiceInvoices(count, newInvoices);
}
return newInvoices.Select(i => i.Oid.Value).ToList();
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> InsertNewSettlementInvoice(List<Settlement2DC> invoices)
{
try
{
var newInvoices = MapperFactory.SettlementDC_SettlementInvoice.MapToNewEntities(invoices);
int count = 0;
var inc = PluginLoader.FindClass<InvoiceNumberGenerator>();
foreach (var i in newInvoices)
{
var nextNumber = inc.GetNextInvoiceNumber(count, i.InvoiceBase);
if (!String.IsNullOrEmpty(nextNumber))
{
if (!String.IsNullOrEmpty(i.InvoiceBase.InvoiceNumber)) // Rechnungsnummer schon vorher erzeugt oder per Hand vergeben.
{
if (i.InvoiceBase.InvoiceNumber == nextNumber)
//Die per Hand vergebene Rechnungsnummer ist gleich der nächsten berechneten Rechnungsnummer, daher Rechnungsnummerzähler erhöhen
{
count++;
}
}
else
{
i.InvoiceBase.InvoiceNumber = nextNumber;
count++;
}
}
}
DAOFactory.GenericDAO.Insert(newInvoices);
inc.IncreaseInvoiceNumber(count, newInvoices);
return newInvoices.Select(i => i.Oid.Value).ToList();
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public InvoiceBaseDC LoadInvoiceBaseByOid(long invoiceBaseOid)
{
try
{
var r = MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDC(
DAOFactory.GenericDAO.LoadByID<InvoiceBase>(invoiceBaseOid));
if (r.Type == InvoiceType.Settlement && r.SupportConceptOid != null)
{
SupportConcept sc = DAOFactory.GenericDAO.LoadByID<SupportConcept>(r.SupportConceptOid.Value);
r.CustomerFirstName = sc.Customer.Person.FirstName;
r.CustomerLastName = sc.Customer.Person.LastName;
}
return r;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public InvoiceNumberDC LoadInvoiceNumber()
{
try
{
var inr = DAOFactory.GenericDAO.LoadByID<InvoiceNumber>(1);
if (inr != null)
return MapperFactory.InvoiceNumberDC_InvoiceNumber.MapToNewDC(inr);
return null;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public ServiceInvoiceDC LoadServiceInvoiceByOid(long serviceInvoiceOid)
{
try
{
var r = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(DAOFactory.GenericDAO.LoadByID<ServiceInvoice>(serviceInvoiceOid));
return r;
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> UpdateInvoiceBases(List<InvoiceBaseDC> invoices)
{
try
{
var originals = DAOFactory.GenericDAO.LoadByIDs<InvoiceBase>(invoices.Select(i => i.InvoiceBaseOid.Value));
bool macheMerge = true;
foreach (var entity in originals)
{
var dc = invoices.Where(i => i.InvoiceBaseOid == entity.Oid).FirstOrDefault();
if (dc.IsPaid != entity.IsPaid ||
dc.IsExported != entity.IsExported ||
dc.IsReviewed != entity.IsReviewed ||
dc.IsSent != entity.IsSent ||
dc.PartialPayment != entity.PartialPayment)
{
//Kann nur über die Rechnungsübersicht geändert werden, daher kein Merge mit dem dc machen!
entity.IsPaid = dc.IsPaid;
entity.IsExported = dc.IsExported;
entity.IsReviewed = dc.IsReviewed;
entity.IsSent = dc.IsSent;
entity.PartialPayment = dc.PartialPayment;
macheMerge = false;
DAOFactory.GenericDAO.Update(entity);
}
}
if (macheMerge)
{
MapperFactory.InvoiceBaseDC_InvoiceBase.MergeWithEntitys(invoices, originals);
DAOFactory.GenericDAO.Update(originals);
}
return originals.Select(e => e.Version.Value).ToList();
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> SetInvoicePaid(List<InvoiceBaseDC> invoices, bool isPaid)
{
try
{
var originals = DAOFactory.GenericDAO.LoadByIDs<InvoiceBase>(invoices.Select(i => i.InvoiceBaseOid.Value));
foreach (var invoiceBase in originals)
{
invoiceBase.IsPaid = isPaid;
}
DAOFactory.GenericDAO.Update(originals);
return originals.Select(e => e.Version.Value).ToList();
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public InvoiceNumberDC UpdateInvoiceNumber(InvoiceNumberDC dc)
{
try
{
var old = DAOFactory.GenericDAO.LoadByID<InvoiceNumber>(1);
MapperFactory.InvoiceNumberDC_InvoiceNumber.MergeWithEntity(dc, old);
DAOFactory.GenericDAO.Update(old);
return MapperFactory.InvoiceNumberDC_InvoiceNumber.MapToNewDC(old);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<long> UpdateServiceInvoices(List<ServiceInvoiceDC> invoices)
{
try
{
var acc = PluginLoader.FindClass<AccountingService>();
return acc.UpdateServiceInvoices(invoices);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public void UpdateSettlementInvoice(List<Settlement2DC> invoices)
{
try
{
var originals = DAOFactory.GenericDAO.LoadByIDs<SettlementInvoice>(invoices.Select(i => i.SettlementIvoiceOid.Value));
MapperFactory.SettlementDC_SettlementInvoice.MergeWithEntitys(invoices, originals);
//originals.ForEach(
// i =>
// {
//i.InvoiceBase.Type = InvoiceType.Settlement;
// We´ll add an invoiceitem, so we can treat a settlement like any other invoice
//i.InvoiceBase.InvoiceItems.Clear();
//i.InvoiceBase.InvoiceItems.Add(
// new InvoiceItem
// {
// AmountTotal = i.Claim,
// ItemPeriodStart = i.InvoiceBase.AccountingPeriodStart,
// ItemPeriodEnd = i.InvoiceBase.AccountingPeriodEnd,
// ItemDescription = "Spitzabrechung"
// });
//});
DAOFactory.GenericDAO.Update(originals);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceNumberDC> LoadInvoiceNumberList()
{
try
{
var numbers = DAOFactory.GenericDAO.GetAllActive<InvoiceNumber>();
return MapperFactory.InvoiceNumberDC_InvoiceNumber.MapToNewDCs(numbers);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
public List<InvoiceNumberDC> UpdateInvoiceNumberList(List<InvoiceNumberDC> dclist)
{
try
{
var numbers = DAOFactory.GenericDAO.GetAllActive<InvoiceNumber>();
MapperFactory.InvoiceNumberDC_InvoiceNumber.MergeWithEntitys(dclist, numbers);
DAOFactory.GenericDAO.Update(numbers);
return MapperFactory.InvoiceNumberDC_InvoiceNumber.MapToNewDCs(numbers);
}
catch (Exception e)
{
throw Utils.CreateBeWoFaultException(e);
}
}
}
2016-06-27 01:45:38 +02:00
}