245 lines
8.1 KiB
C#
245 lines
8.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 int OrdersLoaded = 0;
|
|
|
|
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).
|
|
Where(o => provider.TransportMethod == 0 || provider.TransportMethod == o.DeliveryMethod).
|
|
ToList();
|
|
|
|
OrdersLoaded = orders.Count;
|
|
|
|
LoadIntoViewer(new ReportData {
|
|
Orders = orders
|
|
});
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (data.Orders.Count == 0)
|
|
{
|
|
_documentViewer.DocumentSource = null;
|
|
} else
|
|
{
|
|
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 static SqlCommand GetSQLCommand(DateTime? date)
|
|
{
|
|
var cmd = new SqlCommand();
|
|
var baseSQL = "SELECT " +
|
|
"c010, " +
|
|
"c012, " +
|
|
"c013, " +
|
|
"c014, " +
|
|
"c017, " +
|
|
"c029, " +
|
|
"c044, " +
|
|
"c089, " +
|
|
"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, c089, 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 = GetSQLCommand(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,
|
|
DeliveryMethod = row.ItemEx("c089", 0),
|
|
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;
|
|
}
|
|
}
|
|
}
|