Files
BeWoPlaner/ReportImp/LebenshilfeFrankfurtOder/CustomReportCreator.cs
2020-11-10 02:25:15 +01:00

682 lines
26 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BeWo.Service.ServiceImplementations;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using DevExpress.XtraReports.UI;
using LebenshilfeFrankfurtOder.Invoicing;
namespace LebenshilfeFrankfurtOder
{
public class CustomReportCreator : DefaultReportCreator
{
public override XtraReport CreateServiceOverviewAllCustomersReport(int month, int year, long orgaOid, long empOid, long? serviceCategoryOid, int startDay, int endDay)
{
List<ServicesOverviewRO> lROs = ServicesOverviewRO.Create(month, year, orgaOid, empOid, startDay, endDay, serviceCategoryOid);
if (lROs.Count > 0)
{
var rootReport = CreateServicesOverviewReportForRo(lROs[0], serviceCategoryOid);
if (rootReport.Pages.Count == 0)
{
rootReport.CreateDocument();
}
for (int i = 1; i < lROs.Count; i++)
{
var lNext = CreateServicesOverviewReportForRo(lROs[i], serviceCategoryOid);
if (lNext.Pages.Count == 0)
{
lNext.CreateDocument();
}
rootReport.Pages.AddRange(lNext.Pages);
}
return rootReport;
}
return new XtraReport();
}
public override XtraReport CreateServiceOverviewSingleCustomerReport(long customerOid, int month, int year, long orgaOid, long empOid, long? serviceCategoryOid, int startDay, int endDay)
{
var ro = ServicesOverviewRO.Create(customerOid, month, year, false, orgaOid, empOid, startDay, endDay, serviceCategoryOid);
return CreateServicesOverviewReportForRo(ro, serviceCategoryOid);
}
private XtraReport CreateServicesOverviewReportForRo(ServicesOverviewRO ro, long? serviceCategoryOid)
{
XtraReport report = null;
ServicesOverviewRO entRo = CopyFromRO(ro);
ServicesOverviewRO kurzRo = CopyFromRO(ro);
ServicesOverviewRO verRo = CopyFromRO(ro);
if (ro.Services != null)
{
foreach (ServicesOverviewRO.ServiceDetail s in ro.Services)
{
if (s.IsBillable && !s.ServiceDescription.Contains("Kurskosten"))
{
ServicesOverviewRO.CreateSignatureString(s);
var cat = s.ServiceCategory.ToLower().Replace(" ", "");
if (cat.Contains("§45"))
{
entRo.Services.Add(s);
}
else if (s.ServiceCategory.ToLower().StartsWith("§42"))
{
kurzRo.Services.Add(s);
}
else if (s.ServiceCategory.ToLower().StartsWith("§39"))
{
verRo.Services.Add(s);
}
}
}
}
report = AddReportToReport(report, new LeistungsnachweisVerhinderungspflege(), verRo);
report = AddReportToReport(report, new LeistungsnachweisKurzzeitpflege(), kurzRo);
report = AddReportToReport(report, new LeistungsnachweisEntlastungsleistungen(), entRo);
//report = AddReportToReport(report, eingliederungshilfe);
if (report == null)
report = new XtraReport();
return report;
}
private ServicesOverviewRO CopyFromRO(ServicesOverviewRO ro)
{
ServicesOverviewRO newRo = new ServicesOverviewRO();
newRo.StartDate = ro.StartDate;
newRo.Abwesenheiten = ro.Abwesenheiten;
newRo.AbsenceTimes = ro.AbsenceTimes;
newRo.ApprovedEndDate = ro.ApprovedEndDate;
newRo.ApprovedFLSPerMonth = ro.ApprovedFLSPerMonth;
newRo.ApprovedFLSPerMonthTotal = ro.ApprovedFLSPerMonthTotal;
newRo.ApprovedFLSPerWeek = ro.ApprovedFLSPerWeek;
newRo.ApprovedFLSPerWeekTotal = ro.ApprovedFLSPerWeekTotal;
newRo.ApprovedFLSTotal = ro.ApprovedFLSTotal;
newRo.ApprovedStartDate = ro.ApprovedStartDate;
newRo.ContactEmployee = ro.ContactEmployee;
newRo.CostBearer2SupportConceptList = ro.CostBearer2SupportConceptList;
newRo.Costbearer = ro.Costbearer;
newRo.CustomerBirthday = ro.CustomerBirthday;
newRo.CustomerLastName = ro.CustomerLastName;
newRo.CustomerCity = ro.CustomerCity;
newRo.CustomerFirstName = ro.CustomerFirstName;
newRo.CustomerName = ro.CustomerName;
newRo.CustomerPostalCode = ro.CustomerPostalCode;
newRo.CustomerStreet = ro.CustomerStreet;
newRo.EndDate = ro.EndDate;
newRo.FreeMinutesThisMonth = ro.FreeMinutesThisMonth;
newRo.Month = ro.Month;
newRo.Year = ro.Year;
newRo.MainAttendant = ro.MainAttendant;
newRo.MainAttendantCommaSeperated = ro.MainAttendantCommaSeperated;
newRo.ReferenceNumber = ro.ReferenceNumber;
newRo.TotalFLMBillable = ro.TotalFLMBillable;
newRo.TotalFLM = ro.TotalFLM;
newRo.TotalFLMQualified = ro.TotalFLMQualified;
newRo.TotalFLMUnqualified = ro.TotalFLMUnqualified;
newRo.TotalFLS = ro.TotalFLS;
newRo.TotalFLSQualified = ro.TotalFLSQualified;
newRo.TotalFLSUnqualified = ro.TotalFLSUnqualified;
newRo.TotalFLSPerCostBearer = ro.TotalFLSPerCostBearer;
newRo.TotalGefahreneKilometer = ro.TotalGefahreneKilometer;
newRo.SupportConceptCostBearer = ro.SupportConceptCostBearer;
newRo.Mandator = ro.Mandator;
return newRo;
}
private XtraReport AddReportToReport(XtraReport masterReport, IBeWoReport<ServicesOverviewRO> newReport, ServicesOverviewRO ro)
{
if (ro.Services.Count > 0)
{
newReport.SetReportDataSource(ro);
((XtraReport)newReport).CreateDocument();
if (masterReport == null)
{
masterReport = newReport as XtraReport;
}
else
{
if (masterReport.Pages.Count == 0)
{
masterReport.CreateDocument();
}
masterReport.Pages.AddRange(((XtraReport)newReport).Pages);
}
}
return masterReport;
}
public override XtraReport CreateAdditionalServiceBookingReport(long addServiceRegionOid, Monat month, int year)
{
IBeWoReport<AdditionalServiceBookingRO> report = null;
var region = DAOFactory.GenericDAO.LoadByID<AdditionalServiceRegion>(addServiceRegionOid);
var ro = AdditionalServiceBookingRO.Create(region, month, year);
if (region.AdditionalService.Name.ToLower().Contains("wohnform"))
{
report = new AnwesenheitWohnstaette();
}
if (report == null)
{
report = new AdditionalServiceBookingReport();
}
report.SetReportDataSource(ro);
return report as XtraReport;
}
public override XtraReport CreateServiceInvoiceReport(string dcIds, long? invoiceBaseOid)
{
if (invoiceBaseOid.HasValue)
{
var serviceInvoice = DAOFactory.SearchDAO.GetServiceInvoiceByInvoiceBaseOid(invoiceBaseOid.Value);
dcIds = String.Format("{0}", serviceInvoice.Oid);
}
if (!string.IsNullOrEmpty(dcIds))
{
var dcList = new List<ServiceInvoiceDC>();
var dcidStrs = dcIds.Split(';');
foreach (var oidStr in dcidStrs)
{
var oid = long.Parse(oidStr);
var invoice = DAOFactory.GenericDAO.LoadByID<ServiceInvoice>(oid);
var dc = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewDC(invoice);
dcList.Add(dc);
}
return CreateInvoiceReport(dcList);
}
return new XtraReport();
}
public override XtraReport CreateQueryReport(long queryOid, string queryReportId)
{
if (queryOid == 1000)
{
return CreateAbrechnung(queryOid, queryReportId, true, false);
}
if (queryOid == 1100)
{
return CreateAbrechnung(queryOid, queryReportId, false, true);
}
return base.CreateQueryReport(queryOid, queryReportId);
}
private XtraReport CreateAbrechnung(long queryOid, string queryReportId, bool abrechnungTagesstruktur, bool abrechnungWst)
{
XtraReport invoiceReport = null;
var query = DAOFactory.GenericDAO.LoadByID<Query>(queryOid);
if (query == null)
return new XtraReport();
var path = Path.Combine(TempPath, queryReportId + ".xml");
var table = BS.Shared.Core.Utils.XMLDeserialize<DataTable>(path);
var qro = QueryRO.Create(query, table);
if (qro.Rows.Count > 0)
{
DateTime dt = DateTime.Now;
DateTime.TryParse(qro.Rows[0].Field1.ToString(), out dt);
DateTime start = new DateTime(dt.Year, dt.Month, 1);
List<ServiceInvoiceDC> invoices = null;
if (abrechnungTagesstruktur)
{
invoices = CreateAbrechnungTagesstruktur(start, start.AddMonths(1).AddTicks(-1));
}
else if (abrechnungWst)
{
invoices = CreateAbrechnungWst(start, start.AddMonths(1).AddTicks(-1));
}
var reallyNewInvoices = GetNewInvoices(invoices, start, start.AddMonths(1).AddTicks(-1));
var newInvoices = MapperFactory.ServiceInvoiceDC_ServiceInvoice.MapToNewEntities(reallyNewInvoices);
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))
{
i.InvoiceBase.InvoiceNumber = nextNumber;
count++;
}
}
DAOFactory.GenericDAO.Insert(newInvoices);
if (count > 0)
{
inc.IncreaseInvoiceNumber(count, null);
}
}
invoiceReport = CreateInvoiceReport(reallyNewInvoices);
}
if (invoiceReport == null)
{
invoiceReport = new XtraReport();
}
return invoiceReport;
}
private List<ServiceInvoiceDC> GetNewInvoices(List<ServiceInvoiceDC> invoices, DateTime start, DateTime end)
{
List<ServiceInvoiceDC> newInvoices = new List<ServiceInvoiceDC>();
var existingInvoices = DAOFactory.SearchDAO.GetInvoiceBases(start, end, false);
foreach (var si in invoices)
{
foreach (var sip in si.ServiceInvoicePeriods)
{
if (sip.Customer.LastNameFirstName.Contains("Götze"))
{
int test = 0;
}
}
bool exist = false;
foreach (var i2 in existingInvoices)
{
if (si.InvoiceBase.InvoiceId == i2.InvoiceId && si.InvoiceBase.Type == i2.Type)
{
if (si.InvoiceBase.SupportConceptCostBearerRelDc != null &&
si.InvoiceBase.RecipientCustomerOid.HasValue)
{
if (si.InvoiceBase.SupportConceptCostBearerRelDc.CostBearer2SupportConceptOid == i2.CostBearer2SupportConceptOid &&
si.InvoiceBase.RecipientCustomerOid == i2.RecipientCustomerOid)
{
exist = true;
}
}
else
{
if (si.InvoiceBase.RecipientOrganisation == i2.RecipientOrganisation &&
si.InvoiceBase.RecipientDivision == i2.RecipientDivision &&
si.InvoiceBase.RecipientPersonFirstName == i2.RecipientPersonFirstName &&
si.InvoiceBase.RecipientPersonLastName == i2.RecipientPersonLastName)
{
if (i2.InvoiceId == "RechnungTagesstruktur")
{
String typAlt = "";
String typNeu = "";
foreach (var sip in si.ServiceInvoicePeriods)
{
foreach (var ii in sip.InvoiceItems)
{
typNeu = ii.ItemDescription;
}
}
var siAlt = DAOFactory.SearchDAO.GetServiceInvoiceByInvoiceBaseOid(i2.Oid.Value);
foreach (var sip in siAlt.ServiceInvoicePeriodList)
{
foreach (var ii in sip.InvoiceItemList)
{
typAlt = ii.ItemDescription;
}
}
if (typAlt == typNeu)
{
exist = true;
}
}
else
{
exist = true;
}
}
}
}
}
if (!exist)
{
newInvoices.Add(si);
}
}
return newInvoices;
}
private XtraReport CreateInvoiceReport(List<ServiceInvoiceDC> invoices)
{
XtraReport allReports = null;
foreach (var iDC in invoices)
{
XtraReport invoiceReport = null;
if (iDC.InvoiceBase.InvoiceId == "RechnungTagesstruktur")
{
var seite1 = new RechnungTagesstruktur();
var anhang = new RechnungTagesstrukturAnhang();
var ro = ServiceInvoiceRO.Create(iDC);
seite1.SetReportDataSource(ro);
seite1.CreateDocument();
anhang.SetReportDataSource(ro);
anhang.CreateDocument();
foreach (var sip in ro.ServiceInvoicePeriods)
{
var newro = ServiceInvoiceRO.Create(iDC);
newro.ServiceInvoicePeriods = new List<ServiceInvoicePeriodRO>();
newro.ServiceInvoicePeriods.Add(sip);
var anhangKlient = new RechnungTagesstrukturAnhangKlient();
anhangKlient.SetReportDataSource(newro);
anhangKlient.CreateDocument();
seite1.Pages.AddRange(anhangKlient.Pages);
}
seite1.Pages.AddRange(anhang.Pages);
invoiceReport = seite1;
}
else if (iDC.InvoiceBase.InvoiceId == "RechnungWst")
{
IBeWoReport<ServiceInvoiceRO> seite1 = null;
var ro = ServiceInvoiceRO.Create(iDC);
if (ro.ServiceInvoicePeriods != null)
{
ro.ServiceInvoicePeriods.Sort((a, b) => a.Customer.LastNameFirstName.CompareTo(b.Customer.LastNameFirstName));
}
if (ro.ServiceInvoicePeriods != null && ro.ServiceInvoicePeriods.Count == 1)
{
seite1 = new RechnungWstEinzel();
}
else
{
seite1 = new RechnungWst();
}
seite1.SetReportDataSource(ro);
((XtraReport)seite1).CreateDocument();
var anhang = new RechnungWstAnhang();
anhang.SetReportDataSource(ro);
anhang.CreateDocument();
((XtraReport)seite1).Pages.AddRange(anhang.Pages);
// Anhang mit Abwesenheitsliste aus Ergänzende Dienste:
List<long> customerOids = new List<long>();
foreach (var sip in ro.ServiceInvoicePeriods)
{
if (sip.Customer != null)
{
customerOids.Add(sip.Customer.Oid.Value);
}
}
Monat month;
Enum.TryParse(ro.AccountingPeriodStart.Month.ToString(), out month);
var region = DAOFactory.GenericDAO.LoadByID<AdditionalServiceRegion>(16); //OID für Wohnform
var asbro = AdditionalServiceBookingRO.Create(region, month, ro.AccountingPeriodStart.Year);
var anhang2 = new AnwesenheitWohnstaette();
anhang2.SetReportDataSource(asbro, customerOids);
anhang2.CreateDocument();
((XtraReport)seite1).Pages.AddRange(anhang2.Pages);
invoiceReport = seite1 as XtraReport;
}
else
{
IBeWoReport<ServiceInvoiceRO> singleReport = FindReportImp<ServiceInvoiceRO>(iDC.InvoiceBase.InvoiceId);
singleReport.SetReportDataSource(ServiceInvoiceRO.Create(iDC));
((XtraReport)singleReport).CreateDocument();
invoiceReport = singleReport as XtraReport;
}
if (allReports == null)
allReports = invoiceReport;
else
((XtraReport)allReports).Pages.AddRange(invoiceReport.Pages);
}
return allReports as XtraReport;
}
private List<ServiceInvoiceDC> CreateAbrechnungTagesstruktur(DateTime start, DateTime end)
{
var invoicePeriod = new DateTimeSpan(start, end);
IList<CompactSupportConceptDC> supportConcepts = GetSupportConcepts(start, end, "Tagesstruktur", "pw11813");
var supportConcepts2ServiceRecords = new Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>>();
foreach (var sc in supportConcepts)
{
if (!supportConcepts2ServiceRecords.ContainsKey(sc))
{
supportConcepts2ServiceRecords.Add(sc, new List<ServiceRecordDC>());
}
}
var creator = new TagesstrukturInvoiceCreation();
return creator.GenerateServiceInvoices(0, invoicePeriod, supportConcepts2ServiceRecords);
}
private List<ServiceInvoiceDC> CreateAbrechnungWst(DateTime start, DateTime end)
{
var invoicePeriod = new DateTimeSpan(start, end);
IList<CompactSupportConceptDC> supportConcepts = GetSupportConcepts(start, end, "besondere Wohnform", "pw110840");
var supportConcepts2ServiceRecords = new Dictionary<CompactSupportConceptDC, List<ServiceRecordDC>>();
foreach (var sc in supportConcepts)
{
if (!supportConcepts2ServiceRecords.ContainsKey(sc))
{
supportConcepts2ServiceRecords.Add(sc, new List<ServiceRecordDC>());
}
}
var creator = new WstInvoiceCreation();
return creator.GenerateServiceInvoices(0, invoicePeriod, supportConcepts2ServiceRecords);
}
private IList<CompactSupportConceptDC> GetSupportConcepts(DateTime start, DateTime end, String serviceName, String categoryName)
{
var scList = new List<CompactSupportConceptDC>();
var service = DAOFactory.GenericDAO.GetAllActive<AdditionalService>().FirstOrDefault(s => s.Name == serviceName);
AdditionalServiceRegion region = null;
if (service != null)
{
region = DAOFactory.GenericDAO.GetAllActive<AdditionalServiceRegion>().FirstOrDefault(s => s.AdditionalServiceOid == service.Oid);
}
if (region != null)
{
var additionalServiceBookings = DAOFactory.SearchDAO.GetAdditionalServiceBookings(region.Oid.Value, start, end);
var customerDict = new Dictionary<CompactCustomerDC, bool>();
foreach (var item in additionalServiceBookings)
{
if (item.Datum == null) continue;
foreach (var e in item.Customer2AddServiceBookings)
{
var customer = DAOFactory.GenericDAO.GetByID<Customer>(e.CustomerOid);
var cdc = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(customer);
if (!customerDict.ContainsKey(cdc))
{
customerDict.Add(cdc, true);
foreach (var sc in customer.SupportConcepts)
{
foreach (var c2s in sc.CostBearer2SupportConceptList)
{
foreach (var scap in c2s.ApprovalPeriodList)
{
if (scap.StartDate < end && scap.EndDate >= start && scap.ServiceCategory != null)
{
String name = scap.ServiceCategory.Name.ToLower().Trim();
if (name.Contains(categoryName))
{
scList.Add(CreateFlatSupportConcept(sc, c2s));
}
}
}
}
}
}
}
}
}
return scList;
}
private CompactSupportConceptDC CreateFlatSupportConcept(SupportConcept sc, CostBearer2SupportConcept cb2sc)
{
var dc = new CompactSupportConceptDC();
dc.SupportConceptOid = sc.Oid.Value;
dc.SupportConceptVersion = sc.Version.Value;
dc.Customer = CreateCustomerForSupportConceptTree(sc.Customer);
dc.ActivationType = sc.IsActive;
dc.ConferenceDate = sc.ConferenceDate;
dc.IsApproved = cb2sc.Status == CostBearer2SupportConceptStatus.Approved;
var costBearer = new CompactCostBearerDC();
if (cb2sc.CostBearer.Organisation != null)
{
costBearer.Organisation = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(cb2sc.CostBearer.Organisation);
}
costBearer.CostBearerID = cb2sc.CostBearer.ID;
costBearer.CostBearerOid = cb2sc.CostBearer.Oid.Value;
costBearer.CostBearer2SupportConceptOid = cb2sc.Oid.Value;
costBearer.SupportConceptStatus = cb2sc.Status;
costBearer.RequestedStartDate = cb2sc.RequestedStartDate;
costBearer.RequestedEndDate = cb2sc.RequestedEndDate;
costBearer.ApprovedStartDate = cb2sc.ApprovedStartDate;
costBearer.ApprovedEndDate = cb2sc.ApprovedEndDate;
costBearer.Bezeichnung = cb2sc.AuswahlBezeichnung;
// costBearer.ApprovedFLS = cb2sc.ApprovedFLS;
// costBearer.ApprovedFLSTotal = cb2sc.ApprovedFLSTotal;
costBearer.CustomerReferenceNumber = cb2sc.CustomerReferenceNumber;
costBearer.IsCalculatingWithFactor = cb2sc.CostBearer.IsCalculatingWithFactor;
dc.CostBearerList.Add(costBearer);
dc.CustomerReferenceNumbers.Add(cb2sc.CustomerReferenceNumber);
dc.CostBearerRelOids.Add(cb2sc.Oid.Value);
dc.CostBearerOids2CostBearerRelOids.Add(costBearer.CostBearerOid, cb2sc.Oid.Value);
return dc;
}
private CompactCustomerDC CreateCustomerForSupportConceptTree(Customer pEntity)
{
var dc = new CompactCustomerDC();
dc.CustomerOid = pEntity.Oid.Value;
dc.CustomerVersion = pEntity.Version.Value;
dc.FirstName = pEntity.Person.FirstName;
dc.LastName = pEntity.Person.LastName;
dc.DateOfBirth = pEntity.Person.DateOfBirth;
dc.ActivationType = pEntity.IsActive;
dc.TerminationDate = pEntity.TerminationDate;
dc.Sex = pEntity.Person.Sex;
return dc;
}
}
}