Files
BeWoPlaner/ReportImp/AlphaEv/Export/DiamantExporter.cs

383 lines
14 KiB
C#
Raw Normal View History

2018-05-28 15:20:27 +02:00
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;
2018-05-28 15:20:27 +02:00
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
2018-05-28 15:20:27 +02:00
namespace AlphaEv.Export
{
public class DiamantExporter
{
public static QueryDC CreateQuery(QueryDC query)
{
DateTime dt = DateTime.Now;
DateTime.TryParse(query.Parameter[0].Value.ToString(), out dt);
query.FileName = String.Format("Buchungsausgabe{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("Buchungsausgabe"))
{
DateTime dt = DateTime.Now;
DateTime.TryParse(pHeaderCaption[1], out dt);
return GetAbrechnungenString(dt);
}
return null;
}
public static String GetAbrechnungenString(DateTime date)
{
StringBuilder sb = new StringBuilder();
2019-08-19 11:59:38 +02:00
sb.AppendLine("Klient;DebKonto;Datum;Kst;Text;Betrag;leer;leer;leer;Sachkonto");
2018-05-28 15:20:27 +02:00
List<long> oidList = new List<long>();
2019-08-19 11:59:38 +02:00
oidList.Add(1); // Fachleistung
oidList.Add(7); // Nicht-Fachkräfte
var s = GetMonatlicheAbrechnungStringForCats(date, oidList, "4016", null);
2018-05-28 15:20:27 +02:00
if (!String.IsNullOrWhiteSpace(s))
{
sb.Append(s);
}
2018-05-28 15:20:27 +02:00
2019-08-19 11:59:38 +02:00
//Assistenzstunden
2018-05-28 15:20:27 +02:00
oidList.Clear();
oidList.Add(2);
oidList.Add(3);
var costDict = CreateHourlyRateDict(oidList, date);
oidList.Clear();
oidList.Add(2);
2019-08-19 11:59:38 +02:00
s = GetMonatlicheAbrechnungStringForCats(date, oidList, "4019", costDict);
2018-05-28 15:20:27 +02:00
if (!String.IsNullOrWhiteSpace(s))
{
if (sb.Length > 0)
sb.AppendLine();
2018-05-28 15:20:27 +02:00
sb.Append(s);
}
oidList.Clear();
oidList.Add(3);
2019-08-19 11:59:38 +02:00
s = GetMonatlicheAbrechnungStringForCats(date, oidList, "4019", costDict);
if (!String.IsNullOrWhiteSpace(s))
{
if (sb.Length > 0)
sb.AppendLine();
sb.Append(s);
}
2018-05-28 15:20:27 +02:00
2019-08-19 11:59:38 +02:00
//Flex
2019-07-12 10:33:31 +02:00
oidList.Clear();
oidList.Add(10);
oidList.Add(13);
oidList.Add(14);
costDict = CreateHourlyRateDict(oidList, date);
oidList.Clear();
oidList.Add(10);
oidList.Add(13);
oidList.Add(14);
s = GetMonatlicheAbrechnungStringForCats(date, oidList, "4042", costDict);
if (!String.IsNullOrWhiteSpace(s))
{
if (sb.Length > 0)
sb.AppendLine();
sb.Append(s);
}
2018-05-28 15:20:27 +02:00
return sb.ToString();
}
2019-08-19 11:59:38 +02:00
public static String GetMonatlicheAbrechnungStringForCats(DateTime date, IList<long> catOids, String sachkonto, Dictionary<long, decimal> costDict, bool nurGruppen, bool nurNichtGruppen)
2018-05-28 15:20:27 +02:00
{
2019-08-19 11:59:38 +02:00
var sql = "";
if (nurGruppen && nurNichtGruppen)
{
sql = GetMonatlicheAbrechnungStringForCatsSql(date, catOids);
}
else
{
if (nurGruppen)
{
sql = GetMonatlicheAbrechnungStringForCatsSqlMitGruppen(date, catOids);
}
}
var dt = ExecuteQuery(sql, date);
2018-05-28 15:20:27 +02:00
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++)
{
2019-08-19 11:59:38 +02:00
if (i < 9)
2018-05-28 15:20:27 +02:00
{
if (i > 0)
{
sb.Append(";");
}
2018-05-28 15:20:27 +02:00
var item = row[i];
2018-05-28 15:20:27 +02:00
2018-10-08 11:57:16 +02:00
2019-08-19 11:59:38 +02:00
if (i == 5 && row[5] != null)
{
2018-10-08 11:57:16 +02:00
decimal betrag = 0;
2019-08-19 11:59:38 +02:00
Decimal.TryParse(row[5].ToString(), out betrag);
2018-10-08 11:57:16 +02:00
//var betrag = (decimal) row[4];
2019-08-19 11:59:38 +02:00
if (row[9] != null && row[10] != null && costDict != null)
{
2019-08-19 11:59:38 +02:00
var minuten = (decimal) row[9];
2019-08-19 11:59:38 +02:00
var catOid = (long) row[10];
if (costDict.ContainsKey(catOid))
{
var stundensatz = costDict[catOid];
betrag =
Math.Round(
stundensatz * Math.Round(minuten / 60, 2, MidpointRounding.AwayFromZero), 2,
MidpointRounding.AwayFromZero);
}
}
sb.Append(betrag);
}
else
{
sb.Append(item);
}
2019-08-19 11:59:38 +02:00
if (i == 4)
{
sb.Append(" Std");
}
2018-05-28 15:20:27 +02:00
}
2018-05-28 15:20:27 +02:00
}
sb.Append(";");
sb.Append(sachkonto);
}
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;
}
2018-05-28 15:20:27 +02:00
private static String GetMonatlicheAbrechnungStringForCatsSql(DateTime date, IList<long> catOids)
{
StringBuilder oidList = new StringBuilder();
foreach (var catOid in catOids)
{
if (oidList.Length > 0)
oidList.Append(",");
oidList.Append(catOid);
}
String sql = @"
SELECT
2019-08-19 11:59:38 +02:00
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) 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.Replace("CATOIDLIST", oidList.ToString());
}
private static String GetMonatlicheAbrechnungStringForCatsSqlGruppen(DateTime date, IList<long> catOids, bool gruppenHolen)
{
StringBuilder oidList = new StringBuilder();
foreach (var catOid in catOids)
{
if (oidList.Length > 0)
oidList.Append(",");
oidList.Append(catOid);
}
String sql = @"
SELECT
CONCAT(p.Lastname, ', ', p.Firstname),
2018-05-28 15:20:27 +02:00
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
2018-05-28 15:20:27 +02:00
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
2018-05-28 15:20:27 +02:00
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) GROUP BY sr2.`CostBearer2SupportConceptOid`)
AS sr ON sr.`CostBearer2SupportConceptOid` = cb2sc.`Oid`
2018-11-22 14:05:45 +01:00
WHERE c.`IsActive` <> 0 AND sc.`IsActive` <> 0
2018-05-28 15:20:27 +02:00
and (sr.`GeleisteteFLM` is not null)
ORDER BY p.`LastName`, p.`FirstName`, cb2sc.`ApprovedStartDate`
";
return sql.Replace("CATOIDLIST", oidList.ToString());
}
private static String GetAbschlagszahlungenSql(DateTime date)
{
String sql = @"
SELECT
date_format(at.BookingDate, '%d.%m.%Y') as 'Belegdatum',
':Belegnr',
'S13690' as 'Konto Soll',
'' as 'Kostenstelle Soll',
c.DebitorNumber as 'Konto Haben',
'' as 'Kostenstelle Haben',
Round(at.Amount, 2) as 'Betrag',
CONCAT('Abschlagszahlung,', p.FirstName, ' ', p.LastName) as 'Buchungstext'
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;
}
private static String GetSelbstzahlerSql(DateTime date)
{
String sql = @"
SELECT
date_format(ib.InvoiceDate, '%d.%m.%Y') as 'Belegdatum',
ib.InvoiceNumber as 'Belegnummer',
c.DebitorNumber as 'Konto Soll',
'' as 'Kostenstelle Soll',
'S86400' as 'Konto Haben',
c.CostCenter as 'Kostenstelle Haben',
Round(ii.AmountTotal, 2) as 'Betrag',
CONCAT('Eigenanteil, ', p.`FirstName`, ' ', p.`LastName`) as 'Buchungstext'
from invoicebase ib
join invoiceitem ii on ii.invoicebaseoid = ib.oid
join customer c on c.oid = ib.recipientcustomeroid
join `person` p on c.`PersonOid` = p.`Oid`
where ib.`InvoiceDate` >= ':Monat_Start' AND ib.`InvoiceDate` < ':Monat_End'
and ib.type = 0 and ib.isactive = 1 and c.isactive <> 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_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];
}
}
}