Files
BeWoPlaner/Service/Plugins/AccountingService.cs

339 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
namespace BeWo.Service.Plugins
{
public class AccountingService : AbstractIDSpecificDefaultClass<AccountingService>
{
public virtual List<InvoiceBaseDC> GetInvoiceBases(DateTime? startDate, DateTime? endDate)
{
DateTime? end = endDate;
if (end.HasValue)
{
end = end.Value.Date.AddDays(1).AddTicks(-1);
}
var list = DAOFactory.SearchDAO.GetInvoiceBases(startDate, end);
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(list);
}
public virtual List<InvoiceBaseDC> GetInvoiceBases(DateTime? startDate, DateTime? endDate, bool filterByInvoiceDate)
{
DateTime? end = endDate;
if (end.HasValue)
{
end = end.Value.Date.AddDays(1).AddTicks(-1);
}
var list = DAOFactory.SearchDAO.GetInvoiceBases(startDate, end, filterByInvoiceDate);
return MapperFactory.InvoiceBaseDC_InvoiceBase.MapToNewDCs(list);
}
public virtual void DeactivateInvoiceBases(List<InvoiceBaseDC> invoices)
{
ServiceLogic.SetActivationType<InvoiceBase>(invoices.ToDictionary(i => i.InvoiceBaseOid.Value, i => i.InvoiceBaseVersion.Value), ActivationTypeId.Deleted);
}
public virtual void DeactivateServiceInvoices(List<ServiceInvoiceDC> invoices)
{
ServiceLogic.SetActivationType<ServiceInvoice>(invoices.ToDictionary(i => i.ServiceInvoiceOid.Value, i => i.ServiceInvoiceVersion.Value), ActivationTypeId.Deleted);
ServiceLogic.SetActivationType<InvoiceBase>(invoices.ToDictionary(i => i.InvoiceBase.InvoiceBaseOid.Value, i => i.InvoiceBase.InvoiceBaseVersion.Value), ActivationTypeId.Deleted);
}
public virtual void StorniereInvoiceBases(List<InvoiceBaseDC> invoices)
{
foreach (var item in invoices)
{
if (item.Type == InvoiceType.Service)
{
ErstelleStornoRechnung(item);
}
else if (item.Type == InvoiceType.Settlement)
{
//ErstelleStornoSpitzabrechnung(item);
}
else
{
ErstelleStornoAllgemeineRechnung(item);
}
}
}
public virtual InvoiceBase ErstelleStornoAllgemeineRechnung(InvoiceBaseDC ibdc)
{
InvoiceBase newInvoice = new InvoiceBase();
var oldInvoice = DAOFactory.GenericDAO.LoadByID<InvoiceBase>(ibdc.InvoiceBaseOid.Value);
CopyInvoiceBase(oldInvoice, newInvoice);
SetzeStornoInfos(newInvoice);
return newInvoice;
}
public virtual ServiceInvoice ErstelleStornoRechnung(InvoiceBaseDC ibdc)
{
ServiceInvoice newInvoice = new ServiceInvoice();
var serviceInvoice = DAOFactory.SearchDAO.GetServiceInvoiceByInvoiceBaseOid(ibdc.InvoiceBaseOid.Value);
CopyServiceInvoice(serviceInvoice, newInvoice);
SetzeStornoInfos(newInvoice);
return newInvoice;
}
//public virtual InvoiceBase ErstelleStornoSpitzabrechnung(InvoiceBaseDC ibdc)
//{
// InvoiceBase newInvoice = new InvoiceBase();
// var oldInvoice = DAOFactory.GenericDAO.LoadByID<InvoiceBase>(ibdc.InvoiceBaseOid.Value);
// CopyInvoiceBase(oldInvoice, newInvoice);
// SetzeStornoInfos(newInvoice);
// return newInvoice;
//}
public virtual ServiceInvoice ErstelleStornoRechnung(ServiceInvoiceDC sidc)
{
ServiceInvoice newInvoice = new ServiceInvoice();
var serviceInvoice = DAOFactory.GenericDAO.LoadByID<ServiceInvoice>(sidc.ServiceInvoiceOid.Value);
CopyServiceInvoice(serviceInvoice, newInvoice);
SetzeStornoInfos(newInvoice);
return newInvoice;
}
public virtual void SetzeStornoInfos(InvoiceBase newInvoice)
{
foreach (var ii in newInvoice.InvoiceItems)
{
if (ii.AmountTotal.HasValue)
{
ii.AmountTotal *= -1;
}
if (ii.GrossAmountTotal.HasValue)
{
ii.GrossAmountTotal *= -1;
}
}
}
public virtual void SetzeStornoInfos(ServiceInvoice newInvoice)
{
foreach (var sip in newInvoice.ServiceInvoicePeriodList)
{
sip.HoursNotBillableAbsence = 0;
sip.HoursNotBillableNotApproved = 0;
if (sip.Claim.HasValue)
{
sip.Claim *= -1;
}
foreach (var ii in sip.InvoiceItemList)
{
if (ii.AmountTotal.HasValue)
{
ii.AmountTotal *= -1;
}
if (ii.GrossAmountTotal.HasValue)
{
ii.GrossAmountTotal *= -1;
}
}
}
}
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);
}
}
//private void CopySettlementInvoice(SettlementInvoice existingInvoice, SettlementInvoice copyInvoice)
// {
// CopyInvoiceBase(existingInvoice.InvoiceBase, copyInvoice.InvoiceBase);
// }
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));
}
}
}
private Contact CreateCopyContact(Contact existingContact)
{
Contact newContact = new Contact();
newContact.Value = existingContact.Value;
newContact.Type = existingContact.Type;
return newContact;
}
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;
}
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);
}
}
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;
}
}
}