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; 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 ParsePackagingInfo(string packagingInfo) { if (packagingInfo == null) return new List(); var lines = packagingInfo.Split("\n"); var result = new List(); // 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 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 List LoadFromDatabase(DateTime? date, Config.ServiceProvider provider) { var orders = new List(); try { var sqlCommand = new SqlCommand("SELECT * FROM t025 WHERE c044 IS NOT NULL AND c045 IS NOT NULL AND U047 IS NOT NULL"); if (date != null) { sqlCommand = new SqlCommand("SELECT * FROM t025 WHERE c044 IS NOT NULL AND c045 IS NOT NULL WHERE c029 = '@DATE'"); sqlCommand.Parameters.Add("DATE", SqlDbType.DateTime).Value = date; } var table = _database.GetDatatable(sqlCommand); var position = 1; foreach (DataRow row in table.Rows) { var trackingNumber = row.ItemEx("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("c029"), OrderNumber = row.ItemEx("c044", ""), WeightAmount = row.ItemEx("c154"), WeightUnit = "kg", ShipmentIdentifiers = identifiers, Loadings = loadings, InformationForDriver = row.ItemEx("u046", ""), InformationForDeliveryNotice = row.ItemEx("u048", ""), InformationForDevlieryAdditional = row.ItemEx("u049", ""), Receiver = new ReportData.Address() { Name = row.ItemEx("c010", ""), Street = row.ItemEx("c012", ""), City = row.ItemEx("c014", ""), Zip = row.ItemEx("c013", ""), Country = row.ItemEx("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() //{ // 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" // } // } //}; } } }