using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; 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 BS.Shared.Translation; using NHibernate.Util; using Utils = BS.Shared.Core.Utils; namespace BeWo.Service.Plugins { public class DataExporter : AbstractIDSpecificDefaultClass { 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) { var lSupportConcepts = DAOFactory.GenericDAO.LoadByIDs(pSortedOids); string[] lHeaderCaption = { "Nachname", // 0 "Vorname", // 1 "Aktenzeichen", // 5 = 2 Translator.Translate("Kostenträger"), // 4 = 3 Translator.Translate("Hilfeplan beantragt am"), // = 4 Translator.Translate("Hilfeplan eingereicht am"), // = 5 Translator.Translate("Hilfeplankonferenz"), // 3 = 6 Translator.Translate("Bewilligung eingetroffen am"), // = 7 "Verlängerung beantragt am", // 10 = 8 "beantragt von", // 6 = 9 "beantragt bis", // 7 = 10 "bewilligt von", // 8 = 11 "bewilligt bis", // 9 = 12 Translator.Translate("gen. FLS/Woche"), // 11 = 13 Translator.Translate("gen. FLS Gesamt"), // 12 = 14 "Stundensatz", // 13 = 15 Translator.Translate("Kostenträger Notiz"), // 14 = 16 "BetreuerIn", // 15 = 17 "Betreuung beendet am", // 16 = 18 "Kommentar", // 2 = 19 }; List lContent = new List(); int lContentIndex = 0; for (int iRowCount = 0; iRowCount < lSupportConcepts.Count; iRowCount++) { lContent.Add(new string[lHeaderCaption.Count()]); AddSupportConceptData(lContent[lContentIndex], lSupportConcepts[iRowCount]); int cbCount = 0; foreach (var iCostBearer in lSupportConcepts[iRowCount].CostBearer2SupportConceptList) { if (cbCount++ > 0) { lContent.Add(new string[lHeaderCaption.Count()]); lContentIndex++; } AddSupportConceptData(lContent[lContentIndex], lSupportConcepts[iRowCount]); lContent[lContentIndex][2] = iCostBearer.CustomerReferenceNumber; if (iCostBearer.CostBearer != null && iCostBearer.CostBearer.Organisation != null) { lContent[lContentIndex][3] = iCostBearer.CostBearer.Organisation.Name; var calc = PluginLoader.FindClass(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][13] = String.Format("{0:0.##}", serviceUnit.GetBEInHours(ap.ApprovedBEPerInterval)); } } else { lContent[lContentIndex][13] = String.Format("{0:0.##}", calc.GetApprovedHoursPerWeek(relDC, dt, false)); // iCostBearer.ApprovedFLS.ToStringNullCheck(); } lContent[lContentIndex][14] = String.Format("{0:0.##}", calc.GetApprovedHoursTotal(relDC, false)); //iCostBearer.ApprovedFLSTotal.ToStringNullCheck(); } else { lContent[lContentIndex][3] = string.Empty; } lContent[lContentIndex][9] = iCostBearer.RequestedStartDate.DoIfNotNull(d => d.ToShortDateString()); lContent[lContentIndex][10] = iCostBearer.RequestedEndDate.DoIfNotNull(d => d.ToShortDateString()); lContent[lContentIndex][11] = iCostBearer.ApprovedStartDate.DoIfNotNull(d => d.ToShortDateString()); lContent[lContentIndex][12] = iCostBearer.ApprovedEndDate.DoIfNotNull(d => d.ToShortDateString()); if (iCostBearer.CostBearer != null) { lContent[lContentIndex][15] = iCostBearer.CostBearer.CostRatePeriods.MapToNewDCs() .GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate).ToStringNullCheck(); } lContent[lContentIndex][16] = 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][17] = mainAttendant; } // if (iRowCount < lSupportConcepts.Count - 1) // { // //next Row // lContentIndex++; lContent.Add(new string[lHeaderCaption.Count()]); // } lContentIndex++; } return WriteHTMLFile(lHeaderCaption, lContent.ToArray()); } private void AddSupportConceptData(string[] content, SupportConcept sc) { content[0] = sc.Customer.Person.LastName; content[1] = sc.Customer.Person.FirstName; content[4] = sc.RequisitionSendDate != null ? sc.RequisitionSendDate.Value.ToShortDateString() : string.Empty; content[5] = sc.SupportConceptSendDate != null ? sc.SupportConceptSendDate.Value.ToShortDateString() : string.Empty; content[6] = sc.ConferenceDate != null ? sc.ConferenceDate.Value.ToShortDateString() : string.Empty; content[7] = sc.ApprovalDate != null ? sc.ApprovalDate.Value.ToShortDateString() : string.Empty; content[8] = sc.RenewalSendDate != null ? sc.RenewalSendDate.Value.ToShortDateString() : string.Empty; if (sc.Customer.TerminationDate.HasValue) { content[18] = String.Format("{0:dd.MM.yyyy}", sc.Customer.TerminationDate); } else { content[18] = ""; } content[19] = sc.Notice; } public string WriteHTMLFile(string[] pHeaderCaption, string[][] pContent) { return new ExcelDownload(pHeaderCaption, pContent).CreateExcelOutputString(); } public virtual void CreateBuchungsExportProtokollEintrag(QueryDC query) { var export = new BuchungsExport(); export.Datum = DateTime.Now; export.Name = query.Title; export.FileName = query.FileName; export.Export = query.QueryResult; export.Parameter = CreateParameterString(query); DAOFactory.GenericDAO.Insert(export); } private string CreateParameterString(QueryDC query) { StringBuilder sb = new StringBuilder(); if (query.Parameter != null) { foreach (var p in query.Parameter) { if (sb.Length > 0) { sb.Append(";"); } sb.Append(p.Name); sb.Append("="); if (p.Value != null) { sb.Append(p.Value.ToString()); } } } return sb.ToString(); } } }