diff --git a/ReportImp/DiakonischesWerkRegensburg/DiakonischesWerkRegensburg.csproj b/ReportImp/DiakonischesWerkRegensburg/DiakonischesWerkRegensburg.csproj index 54104d024..1f411470a 100644 --- a/ReportImp/DiakonischesWerkRegensburg/DiakonischesWerkRegensburg.csproj +++ b/ReportImp/DiakonischesWerkRegensburg/DiakonischesWerkRegensburg.csproj @@ -56,6 +56,8 @@ + + diff --git a/ReportImp/DiakonischesWerkRegensburg/Export/CustomDataExporter.cs b/ReportImp/DiakonischesWerkRegensburg/Export/CustomDataExporter.cs new file mode 100644 index 000000000..31ddf8d0c --- /dev/null +++ b/ReportImp/DiakonischesWerkRegensburg/Export/CustomDataExporter.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using BeWo.Service.Core; +using BeWo.Service.Plugins; +using BS.Shared.Core; +using BS.Shared.DataContracts; +using Utils = BS.Shared.Core.Utils; + +namespace DiakonischesWerkRegensburg.Export +{ + public class CustomDataExporter : DataExporter + { + public override QueryDC CreateQuery(QueryDC query) + { + return EGeckoExporter.CreateQuery(query); + } + } +} diff --git a/ReportImp/DiakonischesWerkRegensburg/Export/EGeckoExporter.cs b/ReportImp/DiakonischesWerkRegensburg/Export/EGeckoExporter.cs new file mode 100644 index 000000000..005431d33 --- /dev/null +++ b/ReportImp/DiakonischesWerkRegensburg/Export/EGeckoExporter.cs @@ -0,0 +1,234 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Text; +using System.Xml; +using BeWo.Data.Access; +using BS.Shared.DataContracts; +using System.Xml.Linq; +using System.Linq; + +namespace DiakonischesWerkRegensburg.Export +{ + public class EGeckoExporter + { + //private static readonly XNamespace xsd = "http://www.w3.org/2001/XMLSchema"; + //private static readonly XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; + + public static QueryDC CreateQuery(QueryDC query) + { + DateTime dt = DateTime.Now; + DateTime.TryParse(query.Parameter[0].Value.ToString(), out dt); + + query.FileName = String.Format("Buchungen{0:yyyyMM}.xml", dt); + query.QueryResult = GetAbrechnungenString(dt); + + return query; + } + + public static String GetAbrechnungenString(DateTime date) + { + return GetMonatlicheRechnungenString(date); + } + + public static string GetMonatlicheRechnungenString(DateTime date) + { + var dt = ExecuteQuery(GetMonatlicheRechnungenSql(date), date); + var doc = CreateXML(dt); + + using (var sw = new StringWriter()) + { + doc.Save(sw); + return sw.ToString(); + } + } + + public static XDocument CreateXML(DataTable dt) + { + var groups = dt.AsEnumerable().GroupBy(r => r["InvoiceOid"].ToString()).ToList(); + + return new XDocument( + //new XDeclaration("1.0", "utf-8", null), + new XElement("FibuBelege", + new XElement("firmaNr", "12"), + new XElement("datumFormat", "dd.MM.yyyy"), + new XElement("anzahlObjekte", groups.Count), + + groups.Select((g, index) => CreateFibuBeleg(g, index + 1)) + ) + ); + } + + private static XElement CreateFibuBeleg(IGrouping group, int lfdnr) + { + var first = group.First(); + + return new XElement("FibuBeleg", + CreateBelegkopf(first, lfdnr), + CreatePositionen(group) + ); + } + + private static XElement CreateBelegkopf(DataRow row, int lfdnr) + { + DateTime date = Convert.ToDateTime(row["InvoiceDate"]); + DateTime period = Convert.ToDateTime(row["Periode"]); + + return new XElement("Belegkopf", + new XElement("lfdnr", lfdnr), + new XElement("belegart", "ra"), + new XElement("belegnummer", row["InvoiceNumber"]), + new XElement("belegdatum", date.ToString("dd.MM.yyyy")), + new XElement("belegperiode", String.Format("{0:yyyy}/{0:MM}", date)), + new XElement("belegwaehrung", "EUR"), + new XElement("buchungstext", row["Verwendungszweck"]) + ); + } + + private static XElement CreatePositionen(IGrouping group) + { + var first = group.First(); + + return new XElement("FibuBelegpositionen", + CreateDebitorPosition(first), + CreateErloesPosition(first) + ); + } + + private static XElement CreateDebitorPosition(DataRow row) + { + DateTime date = Convert.ToDateTime(row["InvoiceDate"]); + + return new XElement("FibuBelegposition", + new XElement("buchungsschluessel", "210"), + new XElement("kontonummer", row["CustomerDebitor"]), + new XElement("betrag", row["Claim"]), + + // werden inhaltlich nicht benötigt, ansonsten Zahlziel 21 Tage + new XElement("OpInfos", + new XElement("OpAngaben", "") // erzeugt + //new XElement("OpInfos", + // new XElement("OpAngaben" //, + //new XElement("opNr", row["InvoiceNumber"]), + //new XElement("zahlungskondition", "PK04"), + //new XElement("valutadatum", date.ToString("dd.MM.yyyy")), + //new XElement("faelligAm", date.ToString("dd.MM.yyyy")), + //new XElement("zahlart", "0") + //) + //) + ) + ); + } + + private static XElement CreateErloesPosition(DataRow row) + { + return new XElement("FibuBelegposition", + new XElement("buchungsschluessel", "150"), + new XElement("kontonummer", row["Erloeskonto"]), + new XElement("betrag", row["Claim"]), + new XElement("mitSteuer", "j"), + new XElement("steuerschluessel", "00"), + new XElement("steuerbetrag", "0.00"), + new XElement("koreBetraegeNeuBerechnen", "j"), + new XElement("FibuKoreBelegpositionen", + new XElement("FibuKoreBelegposition", + new XElement("kostenart", row["Erloeskonto"]), + new XElement("kostenstelle", row["CustomerCostCenter"]), + new XElement("nettobetrag", row["Claim"]))) + ); + } + + private static String GetMonatlicheRechnungenSql(DateTime date) // Einzel- und Sammelrechnungen + { + String sql = @" + SELECT * FROM + (SELECT + ib.InvoiceDate AS InvoiceDate, + c.DebitorNumber AS CustomerDebitor, + '1660' AS CustomerCostCenter, + ROUND(SUM(sip.Claim), 2) AS Claim, + CONCAT(p.LastName, ', ', p.FirstName) AS Verwendungszweck, + ib.InvoiceNumber AS InvoiceNumber, + (SELECT t.name FROM team t INNER JOIN team2customer t2c ON t2c.teamoid = t.oid WHERE t2c.customeroid = c.Oid limit 1) AS TeamName, + CASE WHEN ib.RecipientOrganisation LIKE '%Selbstzahler%' THEN 049030 ELSE 049020 END AS Erloeskonto, + ib.AccountingPeriodEnd AS Periode, + ib.Oid AS InvoiceOid + FROM invoicebase ib + JOIN serviceinvoice si ON si.invoicebaseoid = ib.oid + JOIN serviceinvoiceperiod sip ON sip.serviceinvoiceoid = si.oid + JOIN customer c ON sip.CustomerOid = c.Oid + INNER JOIN person p ON c.personoid = p.oid + WHERE ib.AccountingPeriodEnd >= ':Monat_Start' AND ib.AccountingPeriodEnd < ':Monat_End' AND ib.IsActive = 1 AND ib.InvoiceId = 'Sammelrechnung' AND ib.IsExported = 0 + GROUP BY ib.Oid, ib.InvoiceNumber, ib.InvoiceDate, ib.AccountingPeriodEnd, c.Oid, c.DebitorNumber, p.LastName, p.FirstName + UNION SELECT DISTINCT + ib.InvoiceDate AS InvoiceDate, + c.DebitorNumber AS CustomerDebitor, + '1660' AS CustomerCostCenter, + ROUND(SUM(sip.Claim),2) AS Claim, + CONCAT(p.LastName, ', ', p.FirstName) AS Verwendungszweck, + ib.invoicenumber AS InvoiceNumber , + (SELECT t.name FROM team t INNER JOIN team2customer t2c ON t2c.teamoid = t.oid WHERE t2c.customeroid = c.Oid limit 1) AS TeamName, + CASE WHEN ib.RecipientOrganisation LIKE '%Selbstzahler%' THEN 049030 ELSE 049020 END AS Erloeskonto, + ib.AccountingPeriodEnd AS Periode, + ib.Oid AS InvoiceOid + FROM + person p + INNER JOIN customer c ON c.personoid = p.oid + INNER JOIN supportconcept sc ON sc.customeroid = c.oid + INNER JOIN costbearer2supportconcept c2s ON c2s.supportconceptoid = sc.oid + INNER JOIN invoicebase ib ON ib.costbearer2supportconceptoid = c2s.oid + INNER JOIN serviceinvoice si ON si.invoicebaseoid = ib.oid + INNER JOIN serviceinvoiceperiod sip ON sip.serviceinvoiceoid = si.oid + WHERE ib.`AccountingPeriodEnd` >= ':Monat_Start' AND ib.`AccountingPeriodEnd` < ':Monat_End' AND ib.IsActive = 1 AND ib.InvoiceId IS NULL AND ib.IsExported = 0 + ) t + WHERE t.InvoiceOid IS NOT NULL + ORDER BY t.invoicenumber + "; + + return sql; + } + + private static String GetMonatlicheSammelRechnungPositionenSql(DateTime date, DateTime rechnungsDatumVon, DateTime rechnungsDatumBis) + { + String sql = @" + SELECT + ib.InvoiceDate AS InvoiceDate, + c.DebitorNumber AS CustomerDebitor, + '1660' AS CustomerCostCenter, + ROUND(SUM(sip.Claim),2) AS Claim, + CONCAT(p.LastName, ', ', p.FirstName) AS Verwendungszweck, + ib.invoicenumber AS InvoiceNumber , + (select t.name from team t inner join team2customer t2c on t2c.teamoid = t.oid where t2c.customeroid = c.Oid limit 1) AS TeamName, + CASE WHEN ib.RecipientOrganisation = 'Selbstzahler' THEN 049030 ELSE 049020 END AS Erloeskonto, + ib.AccountingPeriodEnd AS Periode, + ib.Oid AS InvoiceOid + FROM + invoicebase ib + JOIN serviceinvoice si ON si.invoicebaseoid = ib.oid + JOIN serviceinvoiceperiod sip ON sip.serviceinvoiceoid = si.oid + JOIN invoiceitem ii ON ii.serviceinvoiceperiodoid = sip.oid + JOIN customer c ON sip.CustomerOid = c.Oid + inner join person p on c.personoid = p.oid + WHERE ib.`AccountingPeriodEnd` >= ':MonatAbrechnung_Start' AND ib.`AccountingPeriodEnd` < ':MonatAbrechnung_End' and ib.IsActive = 1 + AND ib.`InvoiceDate` >= ':Monat_Start' AND ib.`InvoiceDate` < ':Monat_End' AND ib.InvoiceId = 'Sammelrechnung' AND ib.`IsExported` = 0 + + ORDER BY ii.ItemDescription + "; + + return sql; + } + + public static DataTable ExecuteQuery(String sql, DateTime dt) + { + var newsql = sql; + newsql = newsql.Replace(":Abrechnungsmonat", String.Format("{0:dd.MM.yyyy}", dt)); + newsql = newsql.Replace(":Belegnr", String.Format("{0:yyMM}", dt)); + newsql = newsql.Replace(":Monat_Start", String.Format("{0:yyyy-MM}-01", dt)); + dt = dt.AddMonths(1); + newsql = newsql.Replace(":Monat_End", String.Format("{0:yyyy-MM}-01", dt)); + + return DAOFactory.AdoDAO.ExecuteQuery(newsql).Tables[0]; + } + } +} \ No newline at end of file