Files
BeWoPlaner/ReportImp/MuenchenerAidsHilfeEV/Service/CustomAccountingService.cs

295 lines
15 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MuenchenerAidsHilfeEV.Service
{
public class CustomAccountingService : AccountingService
{
protected override List<ServiceInvoiceDC> ErstelleNeueRechnungen(List<DifferenzRechnungCheck> differenzRechnungChecks)
{
var rechnungen = new List<ServiceInvoiceDC>();
foreach (var item in differenzRechnungChecks)
{
if (item.VorhandeneRechungen.Count == 0 || !item.VorhandeneRechungen.Any(r => item.NeueRechnung.InvoiceBase.CustomerLastName == r.InvoiceBase.CustomerLastName &&
item.NeueRechnung.InvoiceBase.CustomerFirstName == r.InvoiceBase.CustomerFirstName))
{
item.NeueRechnung.InvoiceBase.NeuErstellen = true;
rechnungen.Add(item.NeueRechnung);
}
else
{
var diffRechnung = ErstelleDifferenzRechnung(item);
if (diffRechnung != null)
{
diffRechnung.InvoiceBase.NeuErstellen = false;
rechnungen.Add(diffRechnung);
}
}
}
return rechnungen;
}
protected override ServiceInvoiceDC ErstelleDifferenzRechnung(DifferenzRechnungCheck item)
{
ServiceInvoiceDC diffRechnung = null;
//var alterBetrag = item.VorhandeneRechungen.Sum(i => i.ClaimSum ?? 0);
var letzteRg = item.VorhandeneRechungen.OrderByDescending(r => r.ServiceInvoiceOid).FirstOrDefault();
letzteRg = item.VorhandeneRechungen.FirstOrDefault(r => item.NeueRechnung.InvoiceBase.CustomerOid == r.InvoiceBase.CustomerOid);
if (letzteRg != null)
{
if (item.NeueRechnung.ClaimSum == letzteRg.ClaimSum)
{
diffRechnung = item.NeueRechnung;
diffRechnung.InvoiceBase.NeuErstellen = false;
diffRechnung.InvoiceBase.Hinweise = String.Format("Rechnung wurde bereits erstellt");
}
else
{
diffRechnung = new ServiceInvoiceDC();
diffRechnung.OriginalZurDifferenz = item.NeueRechnung;
diffRechnung.InvoiceBase = new InvoiceBaseDC();
CopyInvoiceBaseDC(item.NeueRechnung.InvoiceBase, diffRechnung);
ServiceInvoice counterInvoice = new ServiceInvoice();
var serviceInvoice = DAOFactory.SearchDAO.GetServiceInvoiceByInvoiceBaseOid(letzteRg.InvoiceBase.InvoiceBaseOid.Value);
CopyServiceInvoice(serviceInvoice, counterInvoice);
SetzeStornoInfos(counterInvoice);
SetzeRechnungspositionenServiceInvoice(diffRechnung, counterInvoice, item);
diffRechnung.InvoiceBase.NeuErstellen = true;
diffRechnung.InvoiceBase.Hinweise = String.Format("Rechnungsdifferenz zu Rechnung {0} in Höhe von {1:c}", letzteRg.InvoiceBase.InvoiceNumber, letzteRg.InvoiceBase.TotalAmount.Value);
}
}
return diffRechnung;
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private void CopyServiceInvoice(ServiceInvoice existingInvoice, ServiceInvoice copyInvoice)
{
copyInvoice.AmountAdvancePayments = existingInvoice.AmountAdvancePayments;
copyInvoice.AmountEquityContribution = existingInvoice.AmountEquityContribution;
copyInvoice.ServiceUnitOid = existingInvoice.ServiceUnitOid;
CopyInvoiceBase(existingInvoice.InvoiceBase, copyInvoice.InvoiceBase);
foreach (var sip in existingInvoice.ServiceInvoicePeriodList)
{
var copySip = new ServiceInvoicePeriod();
copyInvoice.ServiceInvoicePeriodList.Add(copySip);
CopyServiceInvoicePeriod(sip, copySip);
}
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private void CopyInvoiceBase(InvoiceBase existing, InvoiceBase copy)
{
copy.AccountingPeriodEnd = existing.AccountingPeriodEnd;
copy.AccountingPeriodStart = existing.AccountingPeriodStart;
copy.CostBearer2SupportConcept = existing.CostBearer2SupportConcept;
copy.CreatorFirstName = existing.CreatorFirstName;
copy.CreatorLastName = existing.CreatorLastName;
copy.CustomerReferenceNumber = existing.CustomerReferenceNumber;
copy.DueDate = existing.DueDate;
copy.DunningLetterCount = existing.DunningLetterCount;
copy.InvoiceDate = existing.InvoiceDate;
copy.InvoiceId = existing.InvoiceId;
copy.InvoiceNumber = existing.InvoiceNumber;
copy.InvoiceTitle = existing.InvoiceTitle;
copy.InvoiceTypeText = existing.InvoiceTypeText;
copy.IsPaid = existing.IsPaid;
copy.IsPrinted = existing.IsPrinted;
copy.RecipientCostBearerOid = existing.RecipientCostBearerOid;
copy.RecipientCustomerOid = existing.RecipientCustomerOid;
copy.RecipientDivision = existing.RecipientDivision;
copy.RecipientOrganisation = existing.RecipientOrganisation;
copy.RecipientOrganisationOid = existing.RecipientOrganisationOid;
copy.RecipientPersonFirstName = existing.RecipientPersonFirstName;
copy.RecipientPersonLastName = existing.RecipientPersonLastName;
copy.RecipientPersonOid = existing.RecipientPersonOid;
copy.SenderDivision = existing.SenderDivision;
copy.SenderFirstName = existing.SenderFirstName;
copy.SenderLastName = existing.SenderLastName;
copy.SenderOrganisation = existing.SenderOrganisation;
copy.SupportConceptOid = existing.SupportConceptOid;
copy.Type = existing.Type;
copy.XMLData = existing.XMLData;
foreach (var ii in existing.InvoiceItems)
{
var copyItem = new InvoiceItem();
copy.InvoiceItems.Add(copyItem);
CopyInvoiceItem(ii, copyItem);
}
if (existing.RecipientAddress != null)
{
copy.RecipientAddress = CreateCopyAddress(existing.RecipientAddress);
}
if (existing.RecipientContacts != null)
{
copy.RecipientContacts = new List<Contact>();
foreach (var existingContact in existing.RecipientContacts)
{
copy.RecipientContacts.Add(CreateCopyContact(existingContact));
}
}
if (existing.SenderAddress != null)
{
copy.SenderAddress = existing.SenderAddress;
}
if (existing.SenderContacts != null)
{
copy.SenderContacts = new List<Contact>();
foreach (var existingContact in existing.SenderContacts)
{
copy.SenderContacts.Add(CreateCopyContact(existingContact));
}
}
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private void CopyInvoiceItem(InvoiceItem existingItem, InvoiceItem copyItem)
{
copyItem.AccountingInterval = existingItem.AccountingInterval;
copyItem.AmountPerUnit = existingItem.AmountPerUnit;
copyItem.AmountTotal = existingItem.AmountTotal;
copyItem.ApprovalUnit = existingItem.AccountingInterval;
copyItem.ApprovedPerUnit = existingItem.ApprovedPerUnit;
copyItem.GrossAmountTotal = existingItem.GrossAmountTotal;
copyItem.ItemDescription = existingItem.ItemDescription;
copyItem.ItemPeriodEnd = existingItem.ItemPeriodEnd;
copyItem.ItemPeriodStart = existingItem.ItemPeriodStart;
copyItem.MaxApprovedAmount = existingItem.MaxApprovedAmount;
copyItem.MaxApprovedUnitCount = existingItem.MaxApprovedUnitCount;
copyItem.RateFactor = existingItem.RateFactor;
copyItem.ReceivedAmount = existingItem.ReceivedAmount;
copyItem.SupportConceptApprovalPeriodOid = existingItem.SupportConceptApprovalPeriodOid;
copyItem.UnitCount = existingItem.UnitCount;
copyItem.UnitDescription = existingItem.UnitDescription;
copyItem.Notice = existingItem.Notice;
copyItem.ServiceRecord = existingItem.ServiceRecord;
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private Address CreateCopyAddress(Address address)
{
Address newAddress = new Address();
newAddress.AddressLine1 = address.AddressLine1;
newAddress.AddressLine2 = address.AddressLine2;
newAddress.Country = address.Country;
newAddress.PostalCode = address.PostalCode;
newAddress.State = address.State;
newAddress.Street = address.Street;
newAddress.Town = address.Town;
return newAddress;
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private Contact CreateCopyContact(Contact existingContact)
{
Contact newContact = new Contact();
newContact.Value = existingContact.Value;
newContact.Type = existingContact.Type;
return newContact;
}
private void SetzeRechnungspositionenServiceInvoice(ServiceInvoiceDC diffRechnung, ServiceInvoice counterInvoice, DifferenzRechnungCheck item)
{
var siDCAlt = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(counterInvoice);
if (siDCAlt.ServiceInvoicePeriods != null && siDCAlt.ServiceInvoicePeriods.Count > 0)
{
diffRechnung.ServiceInvoicePeriods = siDCAlt.ServiceInvoicePeriods;
}
if (item.NeueRechnung.ServiceInvoicePeriods != null && item.NeueRechnung.ServiceInvoicePeriods.Count > 0)
{
if (diffRechnung.ServiceInvoicePeriods == null)
{
diffRechnung.ServiceInvoicePeriods = item.NeueRechnung.ServiceInvoicePeriods;
}
else
{
foreach (var NewSip in item.NeueRechnung.ServiceInvoicePeriods)
{
if (NewSip.InvoiceItems != null && NewSip.InvoiceItems.Count > 0)
{
if (NewSip.Customer != null && diffRechnung.ServiceInvoicePeriods.Any(s => s.Customer.CustomerOid == NewSip.Customer.CustomerOid) &&
NewSip.SupportConceptApprovalPeriod != null && NewSip.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.HasValue
&& diffRechnung.ServiceInvoicePeriods.Any(s => s.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.HasValue
&& s.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.Value == NewSip.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.Value))
{
var ip = diffRechnung.ServiceInvoicePeriods.Where(s => s.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.Value == NewSip.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.Value).First();
if (ip.InvoiceItems != null && ip.InvoiceItems.Count > 0)
{
ip.Claim += NewSip.Claim;
foreach (var ii in NewSip.InvoiceItems)
{
ip.InvoiceItems.Add(ii);
}
// Bei Rechnungspositionen mit negativem Betrag auf alte Rechnung verweisen
foreach (var iitem in ip.InvoiceItems)
{
if (iitem.AmountTotal < 0)
{
var vorhandeneRechnung = item.VorhandeneRechungen.FirstOrDefault(r => r.InvoiceBase != null && r.InvoiceBase.CustomerOid == ip.Customer.CustomerOid);
if (vorhandeneRechnung != null)
iitem.ItemDescription += string.Format(" (am {0:dd.MM.yyyy} in Rechnung {1} berechnet)", vorhandeneRechnung.InvoiceBase.InvoiceDate, vorhandeneRechnung.InvoiceBase.InvoiceNumber);
}
}
}
}
else
{
diffRechnung.ServiceInvoicePeriods.Add(NewSip);
}
}
}
}
}
}
// Standard, aber in der Basis private und dahar musste es hier mit rein da ich sonst nicht nicht SetzeRechnungspositionenServiceInvoice überschreiben konnte
private void CopyServiceInvoicePeriod(ServiceInvoicePeriod existingSip, ServiceInvoicePeriod copySip)
{
copySip.AccountingInterval = existingSip.AccountingInterval;
copySip.ApprovedAmountDefaultHourlyRate = existingSip.ApprovedAmountDefaultHourlyRate;
copySip.ApprovedBE = existingSip.ApprovedBE;
copySip.ApprovedFixedAmount = existingSip.ApprovedFixedAmount;
copySip.ApprovedHours = existingSip.ApprovedHours;
copySip.Claim = existingSip.Claim;
copySip.End = existingSip.End;
copySip.HoursNotBillableAbsence = existingSip.HoursNotBillableAbsence;
copySip.HoursNotBillableNotApproved = existingSip.HoursNotBillableNotApproved;
copySip.ServiceUnitName = existingSip.ServiceUnitName;
copySip.Start = existingSip.Start;
copySip.Notice = existingSip.Notice;
copySip.Customer = existingSip.Customer;
copySip.SupportConceptApprovalPeriod = existingSip.SupportConceptApprovalPeriod;
foreach (var ii in existingSip.InvoiceItemList)
{
var copyItem = new InvoiceItem();
copySip.InvoiceItemList.Add(copyItem);
CopyInvoiceItem(ii, copyItem);
}
}
}
}