Files
BeWoPlaner/Service/Plugins/DataExporter.cs
2017-02-01 13:53:59 +01:00

195 lines
9.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
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.Extensions;
using BS.Shared.Services;
using Utils = BS.Shared.Core.Utils;
namespace BeWo.Service.Plugins
{
public class DataExporter : AbstractIDSpecificDefaultClass<DataExporter>
{
public virtual string CreateExportString(string pFileID, string[] pHeaderCaption, string[][] pContent)
{
if (pHeaderCaption != null && pHeaderCaption.Length > 0 && pHeaderCaption[0].Contains("Diamant"))
{
return DiamantExporter.CreateExportString(pHeaderCaption, pContent);
}
return WriteHTMLFile(pHeaderCaption, pContent);
}
public virtual QueryDC CreateQuery(QueryDC query)
{
return null;
}
public virtual string PrepareExcelFileSupportConceptExport(string pFileId, long[] pSortedOids)
{
string lPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pFileId + ".tmp");
var lSupportConcepts = DAOFactory.GenericDAO.LoadByIDs<SupportConcept>(pSortedOids);
string[] lHeaderCaption = {
"Nachname", // 0
"Vorname", // 1
"Kommentar", // 2
"Hilfeplankonferenz", // 3
"Kostenträger", // 4
"Aktenzeichen", // 5
"beantragt von", // 6
"beantragt bis", // 7
"bewilligt von", // 8
"bewilligt bis", // 9
"Verlängerung beantragt am", // 10
"gen. FLS/Woche", // 11
"gen. FLS Gesamt", // 12
"Stundensatz", // 13
"Kostenträger Notiz", // 14
"BetreuerIn", // 15
"Betreuung beendet am" // 16
};
List<string[]> lContent = new List<string[]>();
int lContentIndex = 0;
for (int iRowCount = 0; iRowCount < lSupportConcepts.Count; iRowCount++)
{
lContent.Add(new string[lHeaderCaption.Count()]);
lContent[lContentIndex][0] = lSupportConcepts[iRowCount].Customer.Person.LastName;
lContent[lContentIndex][1] = lSupportConcepts[iRowCount].Customer.Person.FirstName;
lContent[lContentIndex][2] = lSupportConcepts[iRowCount].Notice;
lContent[lContentIndex][3] = lSupportConcepts[iRowCount].ConferenceDate != null
? lSupportConcepts[iRowCount].ConferenceDate.Value.ToShortDateString()
: string.Empty;
lContent[lContentIndex][10] = lSupportConcepts[iRowCount].RenewalSendDate != null
? lSupportConcepts[iRowCount].RenewalSendDate.Value.ToShortDateString()
: string.Empty;
int cbCount = 0;
foreach (var iCostBearer in lSupportConcepts[iRowCount].CostBearer2SupportConceptList)
{
if (cbCount++ > 0)
{
lContent.Add(new string[lHeaderCaption.Count()]);
lContentIndex++;
}
if (iCostBearer.CostBearer != null && iCostBearer.CostBearer.Organisation != null)
{
lContent[lContentIndex][4] = iCostBearer.CostBearer.Organisation.Name;
var calc = PluginLoader.FindClass<Calculations>(iCostBearer.CostBearer.ID) ?? Calculations.GetInstance(iCostBearer.CostBearer.ID);
SupportConceptCostBearerRelDC relDC = MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MapToNewDC(iCostBearer);
DateTime dt = DateTime.Now;
DateTime end = DateTime.MaxValue;
if (iCostBearer.ApprovedEndDate.HasValue)
end = iCostBearer.ApprovedEndDate.Value;
else if (iCostBearer.RequestedEndDate.HasValue)
end = iCostBearer.RequestedEndDate.Value;
if (dt > end)
dt = end;
if (relDC.ApprovalPeriodList.Count == 1)
{
var ap = relDC.ApprovalPeriodList[0];
if (ap.ApprovedBEPerInterval.HasValue && ap.ApprovedBEInterval.HasValue && ap.ApprovedBEInterval.Value == SupportConceptApprovalInterval.Weekly)
{
var serviceUnit = relDC.CostBearer.CostRatePeriods.GetCostRatePeriodForDate(CostRatePeriodType.MinutesPerServiceUnit, DateTime.Now);
if (serviceUnit != null)
lContent[lContentIndex][11] = String.Format("{0:0.##}", serviceUnit.GetBEInHours(ap.ApprovedBEPerInterval));
}
}
else
{
lContent[lContentIndex][11] = String.Format("{0:0.##}", calc.GetApprovedHoursPerWeek(relDC, dt, false)); // iCostBearer.ApprovedFLS.ToStringNullCheck();
}
lContent[lContentIndex][12] = String.Format("{0:0.##}", calc.GetApprovedHoursTotal(relDC, false)); //iCostBearer.ApprovedFLSTotal.ToStringNullCheck();
}
else
{
lContent[lContentIndex][4] = string.Empty;
}
lContent[lContentIndex][5] = iCostBearer.CustomerReferenceNumber;
lContent[lContentIndex][6] = iCostBearer.RequestedStartDate.DoIfNotNull(d => d.ToShortDateString());
lContent[lContentIndex][7] = iCostBearer.RequestedEndDate.DoIfNotNull(d => d.ToShortDateString());
lContent[lContentIndex][8] = iCostBearer.ApprovedStartDate.DoIfNotNull(d => d.ToShortDateString());
lContent[lContentIndex][9] = iCostBearer.ApprovedEndDate.DoIfNotNull(d => d.ToShortDateString());
if (iCostBearer.CostBearer != null)
{
lContent[lContentIndex][13] = iCostBearer.CostBearer.CostRatePeriods.MapToNewDCs()
.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate).ToStringNullCheck();
}
lContent[lContentIndex][14] = iCostBearer.Notice;
String mainAttendant = "";
foreach (Employee2Customer e2c in lSupportConcepts[iRowCount].Customer.Employee2CustomerList)
{
foreach (ValueListEntry2Object vle2o in e2c.ValueList)
{
ValueListEntry vle = vle2o.Entry;
if (vle.SystemEntryID != null && vle.SystemEntryID.Value == SystemEntryID.EmployeeRoleMainAttendant)
{
string empName = e2c.Employee.Person.FirstName + " " + e2c.Employee.Person.LastName;
if (String.IsNullOrEmpty(mainAttendant))
mainAttendant = empName;
else
{
if (!mainAttendant.Contains(empName))
{
mainAttendant += ", " + empName;
}
}
}
}
}
lContent[lContentIndex][15] = mainAttendant;
if (lSupportConcepts[iRowCount].Customer.TerminationDate.HasValue)
{
lContent[lContentIndex][16] = String.Format("{0:dd.MM.yyyy}", lSupportConcepts[iRowCount].Customer.TerminationDate);
}
else
{
lContent[lContentIndex][16] = "";
}
}
// if (iRowCount < lSupportConcepts.Count - 1)
// {
// //next Row
// lContentIndex++; lContent.Add(new string[lHeaderCaption.Count()]);
// }
lContentIndex++;
}
return WriteHTMLFile(lHeaderCaption, lContent.ToArray());
}
public string WriteHTMLFile(string[] pHeaderCaption, string[][] pContent)
{
return new ExcelDownload(pHeaderCaption, pContent).CreateExcelOutputString();
}
}
}