HilfeDaheimHandInHand/Export/DatevExporter

S_158901
This commit is contained in:
2026-03-30 12:13:55 +02:00
parent 0c01be00bf
commit 3a2f8d1974
3 changed files with 320 additions and 0 deletions

View File

@@ -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 HilfeDaheimHandInHand.Export
{
public class CustomDataExporter : DataExporter
{
public override QueryDC CreateQuery(QueryDC query)
{
return DatevExporter.CreateQuery(query);
}
}
}

View File

@@ -0,0 +1,298 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
namespace HilfeDaheimHandInHand.Export
{
public class DatevExporter
{
public static QueryDC CreateQuery(QueryDC query)
{
DateTime dt = DateTime.Now;
DateTime.TryParse(query.Parameter[0].Value.ToString(), out dt);
query.FileName = String.Format("Datev Export {0:yyyyMM}.csv", dt);
query.QueryResult = GetAbrechnungenString(dt);
return query;
}
public static String GetAbrechnungenString(DateTime date)
{
StringBuilder sb = new StringBuilder();
//sb.AppendLine("Datum;Konto;Gegenkonto;Buchungstext;Belegfeld1;Umsatz Soll;Kostenstelle");
DateTime lastDate = new DateTime(date.Year, date.Month, 1);
lastDate = lastDate.AddMonths(1).AddDays(-1);
sb.Append("EXTF;510;21;Buchungsstapel;7;");
sb.Append(String.Format("{0:yyyyMM}00000000000", new DateTime(date.Year, date.Month, 1)));
sb.Append(";;RE;ownSoft;;322418;4;");
sb.Append(String.Format("{0:yyyyMMdd}", new DateTime(date.Year, 1, 1)));
sb.Append(";4;");
sb.Append(String.Format("{0:yyyyMMdd}", new DateTime(date.Year, date.Month, 1)));
sb.Append(";");
sb.Append(String.Format("{0:yyyyMMdd}", lastDate));
sb.Append(";Buchungen;");
sb.AppendLine(";1;0;0;EUR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;");
sb.AppendLine("Umsatz (ohne Soll/Haben-Kz);Soll/Haben-Kennzeichen;WKZ Umsatz;Kurs;Basis-Umsatz;WKZ Basis-Umsatz;Konto;Gegenkonto (ohne BU-Schlüssel);BU-Schlüssel;Belegdatum;Belegfeld 1;Belegfeld 2;" +
"Skonto;Buchungstext;Postensperre;Diverse Adressnummer;Geschäftspartnerbank;Sachverhalt;Zinssperre;Beleglink;Beleginfo - Art 1;Beleginfo - Inhalt 1;Beleginfo - Art 2;" +
"Beleginfo - Inhalt 2;Beleginfo - Art 3;Beleginfo - Inhalt 3;Beleginfo - Art 4;Beleginfo - Inhalt 4;Beleginfo - Art 5;Beleginfo - Inhalt 5;Beleginfo - Art 6;Beleginfo - Inhalt 6;Beleginfo - Art 7;" +
"Beleginfo - Inhalt 7;Beleginfo - Art 8;Beleginfo - Inhalt 8;KOST1 - Kostenstelle;KOST2 - Kostenstelle;Kost-Menge;EU-Land u. UStID;EU-Steuersatz;Abw. Versteuerungsart;Sachverhalt L+L;" +
"Funktionsergänzung L+L;BU 49 Hauptfunktionstyp;BU 49 Hauptfunktionsnummer;BU 49 Funktionsergänzung;Zusatzinformation - Art 1;Zusatzinformation- Inhalt 1;" +
"Zusatzinformation - Art 2;Zusatzinformation- Inhalt 2;Zusatzinformation - Art 3;Zusatzinformation- Inhalt 3;Zusatzinformation - Art 4;Zusatzinformation- Inhalt 4;" +
"Zusatzinformation - Art 5;Zusatzinformation- Inhalt 5;Zusatzinformation - Art 6;Zusatzinformation- Inhalt 6;Zusatzinformation - Art 7;Zusatzinformation- Inhalt 7;" +
"Zusatzinformation - Art 8;Zusatzinformation- Inhalt 8;Zusatzinformation - Art 9;Zusatzinformation- Inhalt 9;Zusatzinformation - Art 10;Zusatzinformation- Inhalt 10;" +
"Zusatzinformation - Art 11;Zusatzinformation- Inhalt 11;Zusatzinformation - Art 12;Zusatzinformation- Inhalt 12;Zusatzinformation - Art 13;Zusatzinformation- Inhalt 13;" +
"Zusatzinformation - Art 14;Zusatzinformation- Inhalt 14;Zusatzinformation - Art 15;Zusatzinformation- Inhalt 15;Zusatzinformation - Art 16;Zusatzinformation- Inhalt 16;" +
"Zusatzinformation - Art 17;Zusatzinformation- Inhalt 17;Zusatzinformation - Art 18;Zusatzinformation- Inhalt 18;Zusatzinformation - Art 19;Zusatzinformation- Inhalt 19;" +
"Zusatzinformation - Art 20;Zusatzinformation- Inhalt 20;Stück;Gewicht;Zahlweise;Forderungsart;Veranlagungsjahr;Zugeordnete Fälligkeit;Skontotyp;" +
"Auftragsnummer;Buchungstyp;USt-Schlüssel (Anzahlungen);EU-Land (Anzahlungen);Sachverhalt L+L (Anzahlungen);EU-Steuersatz (Anzahlungen);Erlöskonto (Anzahlungen);Herkunft-Kz;Buchungs GUID;KOST-Datum;Mandatsreferenz;" +
"Skontosperre;Gesellschaftername;Beteiligtennummer;Identifikationsnummer;Zeichnernummer;Postensperre bis;Bezeichnung SoBil-Sachverhalt;Kennzeichen SoBil-Buchung;" +
"Festschreibung;Leistungsdatum;Datum Zuord. Steuerperiode");
var s = GetRechnungenString(date);
if (!String.IsNullOrWhiteSpace(s))
{
sb.Append(s);
}
s = GetMonatlicheBetraegeString(date);
if (!String.IsNullOrWhiteSpace(s))
{
if (sb.Length > 0)
sb.AppendLine();
sb.Append(s);
}
return sb.ToString();
}
public static String GetMonatlicheBetraegeString(DateTime date)
{
// Betrag, c.DebitorNumber, c.CostCenter, CONCAT(p.`LastName`, ', ', p.`FirstName`) as 'Verwendung', cb2sc.Oid
var sql = GetMonatlicheBetraegeSql(date);
var dt = ExecuteQuery(sql, date);
StringBuilder sb = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
if (sb.Length > 0)
{
sb.AppendLine();
}
DateTime lastDate = new DateTime(date.Year, date.Month, 1);
lastDate = lastDate.AddMonths(1).AddDays(-1);
sb.Append(String.Format("{0:0.00}", row[0])); //Betrag
sb.Append(";S;;;;;");
sb.Append(String.Format("{0}", row[1])); //DebitorNumber
sb.Append(";4095"); // Erlöskonto
sb.Append(";;");
sb.Append(String.Format("{0:ddMM}", lastDate));
sb.Append(";");
// Belegfeld 1 fehlt hier
sb.Append(";;;");
sb.Append(String.Format("{0}, {1}", row[3], row[4]));
sb.Append(";;;;;;;;;;;;;;;;;;;;;;;BeWo;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;");
}
return sb.ToString();
}
private static String GetMonatlicheBetraegeSql(DateTime date)
{
String sql = @"
SELECT
sum(qry.Betrag),
qry.DebitorNumber,
qry.CostCenter,
qry.Verwendung,
qry.Kostentraeger,
qry.Oid
FROM
(
(SELECT
IF(crpRateFactor.`CostRateValue` is null, ROUND((sr.`GeleisteteFLM` / 60) * (SELECT crp.`CostRateValue` FROM `costrateperiod` crp WHERE crp.`ObjectTid` = 22 AND crp.`CostRateType` = 0 AND crp.`ObjectOid` = cb2sc.`CostBearerOid` AND (crp.`EndDate` is null or crp.`EndDate` > ':Monat_Start') order by IF(crp.`EndDate` is null, MAKEDATE(9999,365),crp.`EndDate`) LIMIT 1 )),
ROUND(ROUND((sr.`GeleisteteFLM` / 60), 2) * (SELECT crp.`CostRateValue` FROM `costrateperiod` crp WHERE crp.`ObjectTid` = 22 AND crp.`CostRateType` = 0 AND crp.`ObjectOid` = cb2sc.`CostBearerOid` AND (crp.`EndDate` is null or crp.`EndDate` > ':Monat_Start') order by IF(crp.`EndDate` is null, MAKEDATE(9999,365),crp.`EndDate`) LIMIT 1 ) * ((100 + crpRateFactor.`CostRateValue`)/100), 2)) AS 'Betrag',
c.DebitorNumber,
c.CostCenter,
cb2sc.KontoHilfeplan,
CONCAT(p.`LastName`, ', ', p.`FirstName`) as 'Verwendung',
org.Name AS Kostentraeger,
cb2sc.Oid
FROM `supportconcept` sc
INNER JOIN `costbearer2supportconcept` cb2sc ON sc.`Oid` = cb2sc.`SupportConceptOid`
INNER JOIN `costbearer` cb ON cb2sc.`CostBearerOid` = cb.`Oid`
LEFT JOIN
(SELECT crp.`ObjectOid`, crp.`CostRateValue` FROM `costrateperiod` crp
WHERE crp.`ObjectTid` = 22 AND crp.`CostRateType` = 2 AND(crp.`EndDate` is null or crp.`EndDate` > NOW()))
AS crpRateFactor ON crpRateFactor.`ObjectOid` = cb.`Oid`
INNER JOIN `organisation` org ON org.`CostBearerOid` = cb.`Oid`
INNER JOIN `customer` c ON sc.`CustomerOid` = c.`Oid`
INNER JOIN `person` p on c.`PersonOid` = p.`Oid`
LEFT JOIN
(SELECT sr2.`CostBearer2SupportConceptOid`, SUM(sr2.`roundedduration` / IF(sr2.`GroupEmployeeCount` is null, 1, sr2.`GroupEmployeeCount`)) AS GeleisteteFLM FROM `servicerecord` sr2
INNER JOIN `servicedescription` sd ON sr2.`ServiceDescriptionOid` = sd.`Oid`
INNER JOIN `servicecategory` sc ON sd.`ServiceCategoryOid` = sc.`Oid`
WHERE sr2.`StartDate` >= ':Monat_Start' AND sr2.`StartDate` < ':Monat_End' AND sc.`Billable` = 1 AND sd.Name <> 'Fehlkontakt' GROUP BY sr2.`CostBearer2SupportConceptOid`)
AS sr ON sr.`CostBearer2SupportConceptOid` = cb2sc.`Oid`
WHERE org.Name LIKE 'LVR%' AND c.`IsActive` <> 0 AND sc.`IsActive` <> 0 AND (sr.`GeleisteteFLM` is not null))
UNION
(SELECT
ROUND(0.8 * (sr.`GeleisteteFLM` / 60) * (SELECT crp.`CostRateValue` FROM `costrateperiod` crp WHERE crp.`ObjectTid` = 22 AND crp.`CostRateType` = 0 AND crp.`ObjectOid` = cb2sc.`CostBearerOid` AND (crp.`EndDate` is null or crp.`EndDate` > ':Monat_Start') order by IF(crp.`EndDate` is null, MAKEDATE(9999,365),crp.`EndDate`) LIMIT 1 ),2) AS 'Betrag',
c.DebitorNumber,
c.CostCenter,
cb2sc.KontoHilfeplan,
CONCAT(p.`LastName`, ', ', p.`FirstName`) as 'Verwendung',
org.Name AS Kostentraeger,
cb2sc.Oid
FROM `supportconcept` sc
INNER JOIN `costbearer2supportconcept` cb2sc ON sc.`Oid` = cb2sc.`SupportConceptOid`
INNER JOIN `costbearer` cb ON cb2sc.`CostBearerOid` = cb.`Oid`
INNER JOIN `organisation` org ON org.`CostBearerOid` = cb.`Oid`
INNER JOIN `customer` c ON sc.`CustomerOid` = c.`Oid`
INNER JOIN `person` p on c.`PersonOid` = p.`Oid`
LEFT JOIN
(SELECT sr2.`CostBearer2SupportConceptOid`, SUM(sr2.`roundedduration` / IF(sr2.`GroupEmployeeCount` is null, 1, sr2.`GroupEmployeeCount`)) AS GeleisteteFLM FROM `servicerecord` sr2
INNER JOIN `servicedescription` sd ON sr2.`ServiceDescriptionOid` = sd.`Oid`
INNER JOIN `servicecategory` sc ON sd.`ServiceCategoryOid` = sc.`Oid`
WHERE sr2.`StartDate` >= ':Monat_Start' AND sr2.`StartDate` < ':Monat_End' AND sc.`Billable` = 1 and sd.Name = 'Fehlkontakt' GROUP BY sr2.`CostBearer2SupportConceptOid`)
AS sr ON sr.`CostBearer2SupportConceptOid` = cb2sc.`Oid`
WHERE org.Name LIKE 'LVR%' AND c.`IsActive` <> 0 AND sc.`IsActive` <> 0 AND (sr.`GeleisteteFLM` is not null))
)qry
GROUP BY qry.OID
ORDER BY qry.Verwendung
";
return sql;
}
public static String GetRechnungenString(DateTime date)
{
var sql = GetRechnungenSql(date);
/*
sip.Claim,
c.DebitorNumber,
ib.InvoiceDate,
ib.invoicenumber,
c.CostCenter,
c2s.Oid,
ib.InvoiceId,
org.DebitorNumber
*/
//"Datum;Konto;Gegenkonto;Buchungstext;Belegfeld1;Umsatz Soll;Kostenstelle"
var dt = ExecuteQuery(sql, date);
StringBuilder sb = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
if (sb.Length > 0)
{
sb.AppendLine();
}
DateTime lastDate = new DateTime(date.Year, date.Month, 1);
lastDate = lastDate.AddMonths(1).AddDays(-1);
sb.Append(String.Format("{0:0.00}", row[0])); //Betrag
sb.Append(";S;;;;;");
sb.Append(String.Format("{0}", row[1])); //DebitorNumber
var gegenkonto = GetErloeskonto(row[8].ToString());
sb.Append(String.Format(";{0}", gegenkonto));
sb.Append(";;");
sb.Append(String.Format("{0:ddMM}", row[2]));
sb.Append(";");
sb.Append(String.Format("{0}", row[3])); // Belegfeld 1
sb.Append(";;;");
sb.Append(String.Format("{0}, {1}", row[7], row[8]));
sb.Append(";;;;;;;;;;;;;;;;;;;;;;;BeWo;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;");
}
return sb.ToString();
}
private static String GetErloeskonto(String orga)
{
if (orga.Contains("Selbstzahler"))
return "4099";
else //LVR
return "4095";
}
private static String GetRechnungenSql(DateTime date)
{
String sql = @"
select
sip.Claim,
c.DebitorNumber,
ib.InvoiceDate,
ib.invoicenumber,
c.CostCenter,
c2s.Oid,
ib.InvoiceId,
CONCAT(p.`LastName`, ', ', p.`FirstName`) as 'Verwendung',
org.Name
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 costbearer cb on c2s.costbeareroid = cb.oid
inner join organisation org on org.costbeareroid = cb.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.InvoiceDate >= ':Monat_Start' AND ib.InvoiceDate < ':Monat_End' and ib.IsActive = 1 AND ib.`IsExported` = 0
Union
SELECT
Round(si.Claim, 2),
c.DebitorNumber,
ib.InvoiceDate,
ib.InvoiceNumber,
c.CostCenter,
c2s.Oid,
ib.InvoiceId,
CONCAT(p.`LastName`, ', ', p.`FirstName`) as 'Verwendung',
org.Name
from invoicebase ib
join settlementinvoice si on si.invoicebaseoid = ib.oid
join supportconcept sc on sc.oid = ib.supportconceptoid
join costbearer2supportconcept c2s on c2s.supportconceptoid = sc.oid
join costbearer cb on c2s.costbeareroid = cb.oid
join organisation org on org.costbeareroid = cb.oid
join customer c on c.oid = sc.customeroid
join `person` p on c.`PersonOid` = p.`Oid`
WHERE ib.InvoiceDate >= ':Monat_Start' AND ib.InvoiceDate < ':Monat_End'
AND ib.type = 1 AND ib.isactive = 1 and c.isactive <> 0 AND ib.`IsExported` = 0
";
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_MM", String.Format("{0:MM}", 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];
}
}
}

View File

@@ -31,6 +31,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DevExpress.DataAccess.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
<Reference Include="DevExpress.Drawing.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Data.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Office.v23.2.Core, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
@@ -39,6 +40,7 @@
<Reference Include="DevExpress.Printing.v23.2.Core, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Data.Desktop.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Utils.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Xpo.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
<Reference Include="DevExpress.XtraPrinting.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.Charts.v23.2.Core, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DevExpress.XtraCharts.v23.2, Version=23.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
@@ -55,6 +57,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Export\CustomDataExporter.cs" />
<Compile Include="Export\DatevExporter.cs" />
<Compile Include="Forderungen.cs">
<SubType>Component</SubType>
</Compile>