SchaumReportViewer/ReportViewer/ReportDataLoader.cs
Jonathan Jenne 77deea925a 09-03-23
2023-03-09 11:17:29 +01:00

267 lines
9.1 KiB
C#

using DevExpress.DataProcessing.InMemoryDataProcessor;
using DevExpress.Mvvm.Native;
using DevExpress.PivotGrid.DataCalculation;
using DevExpress.XtraPrinting.Preview;
using DevExpress.XtraReports.UI;
using DigitalData.Modules.Database;
using DigitalData.Modules.Language;
using DigitalData.Modules.Logging;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace ReportViewer
{
public class ReportDataLoader
{
private LogConfig _logConfig;
private Logger _logger;
private Config _config;
private DocumentViewer _documentViewer;
private MSSQLServer _database;
public ReportDataLoader(LogConfig logConfig, Config config, MSSQLServer database, DocumentViewer documentViewer)
{
_logConfig = logConfig;
_logger = logConfig.GetLogger();
_config = config;
_documentViewer = documentViewer;
_database = database;
}
public void Load(DateTime? date, Config.ServiceProvider provider)
{
var orders = LoadFromDatabase(date, provider).Where(o => o.ShipmentIdentifiers.Length > 0).ToList();
var data = new ReportData { Orders = orders };
LoadIntoViewer(data);
}
public void Print()
{
if (_documentViewer.DocumentSource == null)
return;
ReportTemplate report = (ReportTemplate)_documentViewer.DocumentSource;
ReportPrintTool printTool = new ReportPrintTool(report);
printTool.PreviewRibbonForm.PrintControl.UseDirectXPaint = DevExpress.Utils.DefaultBoolean.True;
printTool.PrintDialog();
}
private void LoadIntoViewer(ReportData data)
{
var report = new ReportTemplate() { DataSource = data, DataMember = "Orders" };
report.CreateDocument(false);
_documentViewer.DocumentSource = report;
}
public class PackagingInfo
{
public string Identifier = "";
public int? LoadingAmount;
public string? LoadingType;
public string Loading
{
get
{
return $"{LoadingAmount} {LoadingType}";
}
}
}
public static List<PackagingInfo> ParsePackagingInfo(string packagingInfo)
{
if (packagingInfo == null)
return new List<PackagingInfo>();
var lines = packagingInfo.Split("\n");
var result = new List<PackagingInfo>();
// Example:
// 310055680000000387 | EP
// 310055680000000394 | GP
// 310055680000000400 | KW
// 310055680000000417 | HP
foreach (var line in lines)
{
var list = line.Split("|").Select(s => s.Trim()).ToList();
if (list.Count == 0)
continue;
var identifier = list[0];
result.Add(new PackagingInfo()
{
Identifier = identifier,
LoadingType = GetLoadingType(list),
LoadingAmount = 1
});
};
return result;
}
public static string GetLoadingType(List<string> list)
{
var colliOnlyRegex = new System.Text.RegularExpressions.Regex("\\d{0,2}\\s?(\\w+)");
if (list.Count == 0)
{
return "";
}
else
{
var value = (list[1] ?? "").Trim();
if (colliOnlyRegex.IsMatch(value))
{
var match = colliOnlyRegex.Match(value);
return match.Groups[1].Value;
}
else
{
return value;
}
}
}
private SqlCommand GetSQL(DateTime? date)
{
var cmd = new SqlCommand();
var baseSQL = "SELECT " +
"c010, " +
"c012, " +
"c013, " +
"c014, " +
"c017, " +
"c029, " +
"c044, " +
"c154, " +
"STRING_AGG(c045, ', ') AS c045, " +
"STRING_AGG(u047, CHAR(13)) AS u047 " +
"FROM t025 " +
"WHERE c044 IS NOT NULL AND c045 IS NOT NULL AND U047 IS NOT NULL " +
"AND NOT (c023 = 'L' AND c024 = 'L' AND c025 = 'L' AND c026 = 'L')" +
"GROUP BY c010, c012, c013, c014, c017, c029, c044, c154";
if (date != null)
{
cmd.CommandText = baseSQL + " AND c029 = '@DATE'";
cmd.Parameters.Add("DATE", SqlDbType.DateTime).Value = date;
}
else
{
cmd.CommandText = baseSQL;
}
return cmd;
}
private List<ReportData.Order> LoadFromDatabase(DateTime? date, Config.ServiceProvider provider)
{
var orders = new List<ReportData.Order>();
try
{
var sqlCommand = GetSQL(date);
var table = _database.GetDatatable(sqlCommand);
var position = 1;
foreach (DataRow row in table.Rows)
{
var trackingNumber = row.ItemEx<string>("U047", "");
var list = ParsePackagingInfo(trackingNumber);
var identifiers = list.Select(i => i.Identifier).JoinString(Environment.NewLine);
var loadings = list.Select(i => i.Loading).JoinString(Environment.NewLine);
orders.Add(new ReportData.Order()
{
Id = position++,
OrderDate = row.ItemEx<DateTime>("c029"),
OrderNumber = row.ItemEx<string>("c044", ""),
WeightAmount = row.ItemEx<double>("c154"),
WeightUnit = "kg",
ShipmentIdentifiers = identifiers,
Loadings = loadings,
DeliveryNotes = row.ItemEx("c045", ""),
InformationForDriver = row.ItemEx("u046", ""),
InformationForDeliveryNotice = row.ItemEx("u048", ""),
InformationForDevlieryAdditional = row.ItemEx("u049", ""),
Receiver = new ReportData.Address()
{
Name = row.ItemEx<string>("c010", ""),
Street = row.ItemEx<string>("c012", ""),
City = row.ItemEx<string>("c014", ""),
Zip = row.ItemEx<string>("c013", ""),
Country = row.ItemEx<string>("c017", "")
},
Head = new ReportData.HeadData()
{
Title = provider.Title,
Receiver = new ReportData.Address()
{
Name = provider.Name,
Street = provider.Street,
Zip = provider.ZipCode,
City = provider.City
}
}
});
}
}
catch (Exception e)
{
_logger.Error(e);
}
return orders;
//return new List<ReportData.Order>()
//{
// new ReportData.Order()
// {
// Id = 1,
// OrderDate = DateTime.Now,
// OrderNumber = "SchaumGLS37564",
// WeightAmount = 17.4,
// WeightUnit = "kg",
// ShipmentIdentifiers = "9378556904370895740985",
// Receiver = new ReportData.Receiver()
// {
// Name = "Ernst Stein GmbH & Co. KG",
// Street = "Herrbert von Ingenhausenweg 21",
// City = "Hemer - Westig",
// Zip = "12345",
// Country = "D"
// }
// },
// new ReportData.Order()
// {
// Id = 1,
// OrderDate = DateTime.Now,
// OrderNumber = "SchaumGLS37564",
// WeightAmount = 17.4,
// WeightUnit = "kg",
// ShipmentIdentifiers = "9378556904370895740985",
// Receiver = new ReportData.Receiver()
// {
// Name = "Ernst Stein GmbH & Co. KG",
// Street = "Herrbert von Ingenhausenweg 21",
// City = "Hemer - Westig",
// Zip = "12345",
// Country = "D"
// }
// }
//};
}
}
}