341 lines
13 KiB
C#
341 lines
13 KiB
C#
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 FlingernMobilReports.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 CreateExportString(string[] pHeaderCaption, string[][] pContent)
|
|
{
|
|
if (pHeaderCaption != null && pHeaderCaption.Length > 1 && pHeaderCaption[0].Contains("DatevListe"))
|
|
{
|
|
DateTime dt = DateTime.Now;
|
|
|
|
DateTime.TryParse(pHeaderCaption[1], out dt);
|
|
|
|
return GetAbrechnungenString(dt);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public static String GetAbrechnungenString(DateTime date)
|
|
{
|
|
|
|
//Fr.Selter 355931 201
|
|
//0211 355931 376
|
|
//Kostenstelle: 602000
|
|
//Ertragskonto: 8100
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.AppendLine("Datum;Konto;Gegenkonto;Buchungstext;Belegnummer;Betrag;Kostenstelle");
|
|
|
|
var s = GetMonatlicheAbrechnungString(date);
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
{
|
|
sb.AppendLine(s);
|
|
}
|
|
|
|
|
|
s = GetAbschlagszahlungenString(date);
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
{
|
|
sb.Append(s);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
|
|
public static String GetMonatlicheAbrechnungString(DateTime date)
|
|
{
|
|
var sql = GetSpitzabrechnungSql(date);
|
|
|
|
var dt = ExecuteQuery(sql, date);
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.AppendLine();
|
|
}
|
|
for (int i = 0; i < row.ItemArray.Length; i++)
|
|
{
|
|
|
|
|
|
if (i > 0)
|
|
{
|
|
sb.Append(";");
|
|
}
|
|
|
|
var item = row[i];
|
|
|
|
sb.Append(item);
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static String GetAbschlagszahlungenString(DateTime date)
|
|
{
|
|
var dt = ExecuteQuery(GetAbschlagszahlungenSql(date), date);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.AppendLine();
|
|
}
|
|
for (int i = 0; i < row.ItemArray.Length; i++)
|
|
{
|
|
|
|
|
|
if (i > 0)
|
|
{
|
|
sb.Append(";");
|
|
}
|
|
|
|
var item = row[i];
|
|
|
|
sb.Append(item);
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static Dictionary<long, decimal> CreateHourlyRateDict(List<long> catOids, DateTime date)
|
|
{
|
|
Dictionary<long, decimal> dict = new Dictionary<long, decimal>();
|
|
|
|
foreach (var oid in catOids)
|
|
{
|
|
var cat = DAOFactory.GenericDAO.LoadByID<ServiceCategory>(oid);
|
|
|
|
var crps = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(cat.CostRatePeriods);
|
|
|
|
var cp = crps.GetCostRatePeriodForDate(CostRatePeriodType.AmountOfMoney, date);
|
|
|
|
if (cp != null && cp.CostRateValue.HasValue)
|
|
{
|
|
dict.Add(oid, cp.CostRateValue.Value);
|
|
}
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
private static String GetMonatlicheRechnungenSql(DateTime date)
|
|
{
|
|
|
|
String sql = @"
|
|
select
|
|
sip.Claim,
|
|
c.DebitorNumber,
|
|
ib.InvoiceDate,
|
|
ib.invoicenumber,
|
|
c.CostCenter,
|
|
c2s.Oid,
|
|
ib.InvoiceId,
|
|
org.DebitorNumber
|
|
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.`AccountingPeriodEnd` >= ':Monat_Start' AND ib.`AccountingPeriodEnd` < ':Monat_End' and ib.IsActive = 1
|
|
order by ib.invoicenumber
|
|
";
|
|
|
|
return sql;
|
|
}
|
|
|
|
private static String GetSpitzabrechnungSql(DateTime date)
|
|
{
|
|
//sb.AppendLine("Datum;Konto;Gegenkonto;Buchungstext;Belegnummer;Betrag;Kostenstelle");
|
|
//Fr.Selter 355931 201
|
|
//0211 355931 376
|
|
//Kostenstelle: 602000
|
|
//Ertragskonto: 8100
|
|
|
|
String sql = @"
|
|
SELECT
|
|
date_format(ib.InvoiceDate, '%d.%m.%Y'),
|
|
c.DebitorNumber,
|
|
'8100',
|
|
CONCAT('Abrechnung ', p.`FirstName`, ' ', p.`LastName`),
|
|
ib.InvoiceNumber,
|
|
Round(si.Claim, 2),
|
|
'602000'
|
|
from invoicebase ib
|
|
join settlementinvoice si on si.invoicebaseoid = ib.oid
|
|
join supportconcept sc on sc.oid = ib.supportconceptoid
|
|
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
|
|
";
|
|
|
|
return sql;
|
|
}
|
|
|
|
|
|
// private static String GetMonatlicheAbrechnungSql(DateTime date)
|
|
// {
|
|
|
|
// String sql = @"
|
|
//SELECT distinct
|
|
//c.`DebitorNumber` AS 'Debitoren Nr.',
|
|
//c.`CostCenter` AS 'Kostenstelle',
|
|
//CONCAT(p.`LastName`, ', ', p.`FirstName`) as Name,
|
|
//org.`Name` as 'Kostentraeger',
|
|
//ROUND(sr.`GeleisteteFLM` / 60, 2) as 'FLS',
|
|
// IF(crpRateFactor.`CostRateValue` is null, ROUND((sr.`GeleisteteFLM` / 60) * crpHourlyRate.`CostRateValue`, 2),ROUND(ROUND((sr.`GeleisteteFLM` / 60), 2) * crpHourlyRate.`CostRateValue` * ((100 + crpRateFactor.`CostRateValue`)/100), 2)) AS 'Forderung',
|
|
//':Monat_MM' AS 'Belegnr.',
|
|
//ROUND(crpHourlyRate.`CostRateValue`,2) as 'Stundensatz', ROUND(crpRateFactor.`CostRateValue`,2) as 'Pauschaler Faktor in %'
|
|
//FROM `supportconcept` sc
|
|
//INNER JOIN `costbearer2supportconcept` cb2sc ON sc.`Oid` = cb2sc.`SupportConceptOid`
|
|
//INNER JOIN `costbearer` cb ON cb2sc.`CostBearerOid` = cb.`Oid`
|
|
//INNER JOIN
|
|
// (SELECT crp.`ObjectOid`, crp.costratetype, crp.`CostRateValue`, If(crp.Enddate is null, '9999-01-01', crp.enddate) as aenddatum, crptemp.enddatum FROM `costrateperiod` crp
|
|
// inner join (SELECT crpi.`ObjectOid`, Min(If(crpi.Enddate is null, '9999-01-01', crpi.enddate)) as enddatum FROM `costrateperiod` crpi
|
|
// WHERE crpi.`ObjectTid` = 22 AND crpi.`CostRateType` = 0 AND (crpi.`EndDate` is null or crpi.`EndDate` > ':Monat_Start')
|
|
// group by crpi.objectoid) as crptemp on crptemp.objectoid = crp.objectoid
|
|
// WHERE crp.`ObjectTid` = 22 and crp.costratetype = 0
|
|
// having aenddatum = crptemp.enddatum)
|
|
// AS crpHourlyRate ON crpHourlyRate.`ObjectOid` = cb.`Oid`
|
|
//LEFT JOIN
|
|
// (SELECT crp.`ObjectOid`, crp.costratetype, crp.`CostRateValue`, If(crp.Enddate is null, '9999-01-01', crp.enddate) as aenddatum, crptemp.enddatum FROM `costrateperiod` crp
|
|
// inner join (SELECT crpi.`ObjectOid`, Min(If(crpi.Enddate is null, '9999-01-01', crpi.enddate)) as enddatum FROM `costrateperiod` crpi
|
|
// WHERE crpi.`ObjectTid` = 22 AND crpi.`CostRateType` = 2 AND (crpi.`EndDate` is null or crpi.`EndDate` > ':Monat_Start')
|
|
// group by crpi.objectoid) as crptemp on crptemp.objectoid = crp.objectoid
|
|
// WHERE crp.`ObjectTid` = 22 and crp.costratetype = 2
|
|
// having aenddatum = crptemp.enddatum)
|
|
// 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`
|
|
//INNER 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:IncludeCatOidList:ExcludeCatOidList GROUP BY sr2.`CostBearer2SupportConceptOid`)
|
|
// AS sr ON sr.`CostBearer2SupportConceptOid` = cb2sc.`Oid`
|
|
//WHERE c.`IsActive` <> 0 AND sc.`IsActive` <> 0 and
|
|
//((If (cb2sc.`ApprovedStartDate` is null, cb2sc.`RequestedStartDate`, cb2sc.`ApprovedStartDate`) < ':Monat_End' and
|
|
//If (cb2sc.`ApprovedEndDate` is null, cb2sc.`RequestedEndDate`, cb2sc.`ApprovedEndDate`) >= ':Monat_Start'
|
|
//and GeleisteteFLM is null) or GeleisteteFLM is not null)
|
|
//ORDER BY p.`LastName`
|
|
//";
|
|
|
|
//String sqlalt = @"
|
|
// SELECT
|
|
//CONCAT(p.Lastname, ', ', p.Firstname),
|
|
//c.DebitorNumber as 'DebKonto',
|
|
//DATE_FORMAT(DATE_ADD(':Monat_End',INTERVAL -1 DAY), '%d.%m.%Y') as 'Datum',
|
|
//c.`CostCenter` AS 'Kst',
|
|
//ROUND(sr.`GeleisteteFLM` / 60, 2) as 'Text',
|
|
//IF(crpRateFactor.`CostRateValue` is null,
|
|
// 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 ), 2),
|
|
// ROUND(ROUND((sr.`GeleisteteFLM` / 60) * ((100 + crpRateFactor.`CostRateValue`)/100), 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 ), 2)) AS 'Betrag',
|
|
// '',
|
|
// '',
|
|
// '',
|
|
//sr.`GeleisteteFLM`,
|
|
//sr.CatOid
|
|
//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, sc.Oid as CatOid 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 sc.oid in (CATOIDLIST) :Gruppenfilter GROUP BY sr2.`CostBearer2SupportConceptOid`)
|
|
// AS sr ON sr.`CostBearer2SupportConceptOid` = cb2sc.`Oid`
|
|
//WHERE c.`IsActive` <> 0 AND sc.`IsActive` <> 0
|
|
//and (sr.`GeleisteteFLM` is not null)
|
|
//ORDER BY p.`LastName`, p.`FirstName`, cb2sc.`ApprovedStartDate`
|
|
//";
|
|
// return sql;
|
|
// }
|
|
|
|
|
|
private static String GetAbschlagszahlungenSql(DateTime date)
|
|
{
|
|
String sql = @"
|
|
SELECT
|
|
date_format(at.BookingDate, '%d.%m.%Y'),
|
|
c.DebitorNumber,
|
|
'8100',
|
|
CONCAT('Abschlagszahlung ', p.FirstName, ' ', p.LastName) as 'Buchungstext',
|
|
'',
|
|
Round(at.Amount, 2) as 'Betrag',
|
|
'602000'
|
|
from accountingtransaction at
|
|
join costbearer2supportconcept c2s on c2s.oid = at.costbearer2supportconceptoid
|
|
join supportconcept sc on sc.oid = c2s.supportconceptoid
|
|
join customer c on sc.customeroid = c.oid
|
|
join person p on c.personoid = p.oid
|
|
WHERE at.`BookingDate` >= ':Monat_Start' AND at.`BookingDate` < ':Monat_End' and c.`IsActive` <> 0 AND sc.`IsActive` <> 0
|
|
ORDER BY p.`LastName`, p.`FirstName`
|
|
";
|
|
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];
|
|
}
|
|
|
|
|
|
}
|
|
} |