Add ReportGenerator class

This commit is contained in:
Jonathan Jenne 2021-12-06 10:23:13 +01:00
parent 8ab2842594
commit 52ac886c1f
3 changed files with 174 additions and 131 deletions

View File

@ -30,6 +30,7 @@ Public Class frmImportMain
Private FileEx As DigitalData.Modules.Filesystem.File
Private WebService As WebServiceData
Private DocumentLoader As DocumentLoader
Private ReportGenerator As ReportGenerator(Of OrderReport)
Private FormHelper As FormHelper
Private Grids As List(Of GridControl)
@ -119,6 +120,7 @@ Public Class frmImportMain
DocumentLoader = New DocumentLoader(LogConfig, Winline, My.MappingConfiguration, My.TemplateConfiguration)
GridLoader = New GridLoader(LogConfig)
ReportGenerator = New ReportGenerator(Of OrderReport)(LogConfig, Database, My.TemplateConfiguration, My.GeneralConfiguration)
SplashScreenManager.SetWaitFormDescription(My.Resources.frmImportMainExtra.Lade_Vorlagen)
@ -411,15 +413,23 @@ Public Class frmImportMain
btnLoadFiles.Enabled = False
SplitContainerGrids.Enabled = False
' Get the document
Dim oDocument As Document = GridViewFiles.GetRow(GridViewFiles.FocusedRowHandle)
Dim oFinalDirectory = My.Helpers.GetDateDirectory(My.GeneralConfiguration.OutputReportDirectory)
Dim oFileName = My.Helpers.GetFilenameWithSuffix(IO.Path.GetFileNameWithoutExtension(oDocument.File.Name), My.Helpers.GetDateTimeString, "pdf")
Dim oFilePath As String = IO.Path.Combine(oFinalDirectory, oFileName)
Dim oReport = GenerateReport(Of OrderReport)(oDocument)
oReport.ExportToPdf(oFilePath)
' Transfer to winline
Await WebService.TransferDocumentToWinline(oDocument, lookupMandator.EditValue)
WebService.RaiseWebServiceProgress("Bericht erzeugen")
' Generate the report
Dim oReport = ReportGenerator.GenerateReport(oDocument)
Dim oFilePath = ReportGenerator.GetReportFilePath(oDocument)
WebService.RaiseWebServiceProgress("Bericht exportieren")
' Export it to pdf
oReport.ExportToPdf(oFilePath)
MsgBox(My.Resources.frmImportMainExtra.Datei_erfolgreich_in_die_WinLine_übertragen, MsgBoxStyle.Information, Text)
Catch ex As HttpRequestException
FormHelper.ShowError(ex, My.Resources.frmImportMainExtra.Übertragung_zur_WinLine, My.Resources.frmImportMainExtra.Die_Verbindung_zum_WinLine_Server_ist_fehlgeschlagen)
@ -519,7 +529,10 @@ Public Class frmImportMain
SplitContainerGrids.Enabled = False
Dim oDocument As Document = GridViewFiles.GetRow(GridViewFiles.FocusedRowHandle)
Dim oReport = GenerateReport(Of OrderReport)(oDocument)
Dim oReportGenerator = New ReportGenerator(Of OrderReport)(LogConfig, Database,
My.TemplateConfiguration,
My.GeneralConfiguration)
Dim oReport As OrderReport = oReportGenerator.GenerateReport(oDocument)
Dim oPrintTool As New ReportPrintTool(oReport)
oPrintTool.Report.CreateDocument(False)
oPrintTool.ShowPreview()
@ -529,135 +542,12 @@ Public Class frmImportMain
GridControlFiles.Enabled = True
SetDocumentButtonsEnabled(True)
SplashScreenManager.CloseWaitForm()
Catch ex As Exception
FormHelper.ShowError(ex, My.Resources.frmImportMainExtra.Erstellen_der_Berichtsvorschau)
End Try
End Sub
Private Function GenerateReport(Of TReport)(pDocument As Document) As TReport
Dim oMapperConfig As New Mapper(LogConfig)
Dim oHeadMapper = oMapperConfig.GetMapper(Of ReportHead)(New Dictionary(Of String, String) From {
{"Fakt_Kontonummer[External]", "Text1"},
{"Fakt_Kontonummer[Final]", "Text2"},
{"Auftrags-Bestellnummer", "Text3"},
{"Datum_Auftrag-Bestellung", "Text4"},
{"Bestellt_von", "Text5"}
})
Dim oPositionMapper = oMapperConfig.GetMapper(Of ReportPosition)(New Dictionary(Of String, String) From {
{"Artikelnummer", "Text1"},
{"Lieferantenartikelnummer", "Text2"},
{"Menge_bestellt", "Text4"},
{"Menge_geliefert", "Text5"},
{"Colli", "Text6"},
{"Einzelpreis[Original]", "Text7"},
{"Einzelpreis[Final]", "Text8"},
{"EinheitProPalette", "Text9"},
{"Lagerstand", "Text10"}
})
Dim oSQLConfig = My.TemplateConfiguration.Items.
Where(Function(item) item.Function.Name = "SQL").
ToList()
FillFieldValuesFromSQL(pDocument, oSQLConfig)
Dim oHeadRow = pDocument.Rows.
Where(Function(r) r.Name.EndsWith("T025")).
Select(Function(r) r.Fields).
FirstOrDefault()
Dim oPositionRows = pDocument.Rows.
Where(Function(r) r.Name.EndsWith("T026")).
ToList()
Dim oReportHead = oHeadMapper.Map(Of Dictionary(Of String, FieldValue), ReportHead)(oHeadRow)
oReportHead.Title = "EDI Bestellung (orders)"
oReportHead.Subtitle = "Schaum"
oReportHead.Filename = pDocument.FileName
Dim oReportPositions As New List(Of ReportPosition)
Dim oCounter = 1
For Each oRow As DocumentRow In oPositionRows
Dim oReportPosition As ReportPosition = oPositionMapper.Map(Of Dictionary(Of String, FieldValue), ReportPosition)(oRow.Fields)
oReportPosition.Id = oCounter
oReportPositions.Add(oReportPosition)
oCounter += 1
Next
Dim oReportSource As New ReportSource With {
.Head = oReportHead,
.Positions = oReportPositions
}
Dim oReport = Activator.CreateInstance(GetType(TReport))
Dim oDataSource = New DevExpress.DataAccess.ObjectBinding.ObjectDataSource With {
.DataSource = oReportSource
}
oDataSource.Fill()
oReport.DataSource = oDataSource
Return oReport
End Function
Private Sub FillFieldValuesFromSQL(pDocument As Document, oSQLConfig As List(Of TemplateConfigItem))
For Each oSQLConfigItem In oSQLConfig
' FIeldList is a list of fields that will be changed
' Example: Setting SQL for Article StorageLocation will invoke the sql for each row
Dim oRowList = pDocument.Rows.
Where(Function(row) row.Fields.Any(Function(field) field.Key = oSQLConfigItem.Name)).
ToList()
For Each oRow As DocumentRow In oRowList
Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault()
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oSQL = oSQLConfigItem.Function.Params
Dim oMatches As MatchCollection = oRegex.Matches(oSQL)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case "FIELD"
Dim oFieldName = oPlaceholderValue
Dim oTargetField = oRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
oSQL = oSQL.Replace(oPlaceholderString, oTargetField.Value.Final)
Case "CONST"
Select Case oMatch.Groups.Item(2).Value.ToUpper
Case "MESOYEAR"
oSQL = oSQL.Replace(oPlaceholderString, My.GeneralConfiguration.GetWinLineYear())
Case "MESOCOMP"
oSQL = oSQL.Replace(oPlaceholderString, pDocument.Mandator.Id)
End Select
End Select
Next
Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then
oField.Value.Final = oValue
End If
Next
Next
End Sub
Private Sub GridViewFiles_CustomDrawCell(sender As Object, e As Views.Base.RowCellCustomDrawEventArgs) Handles GridViewFiles.CustomDrawCell
Dim oDocument As Document = GridViewFiles.GetRow(e.RowHandle)

View File

@ -0,0 +1,151 @@
Imports System.Text.RegularExpressions
Imports DevExpress.XtraReports
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.Documents
Imports MultiTool.Shared.Documents.DocumentRow
Imports MultiTool.Shared.Report
Imports MultiTool.Shared.Templates
Public Class ReportGenerator(Of TReport As IReport)
Inherits BaseClass
Private ReadOnly Database As MSSQLServer
Private ReadOnly TemplateConfig As TemplateConfig
Private ReadOnly GeneralConfig As GeneralConfig
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pTemplateConfig As TemplateConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig)
Database = pDatabase
GeneralConfig = pGeneralConfig
TemplateConfig = pTemplateConfig
End Sub
Public Function GetReportFilePath(pDocument As Document)
Dim oFinalDirectory = Helpers.GetDateDirectory(GeneralConfig.OutputReportDirectory)
Dim oFileName = Helpers.GetFilenameWithSuffix(IO.Path.GetFileNameWithoutExtension(pDocument.File.Name), Helpers.GetDateTimeString, "pdf")
Dim oFilePath As String = IO.Path.Combine(oFinalDirectory, oFileName)
Return oFilePath
End Function
Public Function GenerateReport(pDocument As Document) As TReport
Dim oMapperConfig As New Mapper(LogConfig)
Dim oHeadMapper = oMapperConfig.GetMapper(Of ReportHead)(New Dictionary(Of String, String) From {
{"Fakt_Kontonummer[External]", "Text1"},
{"Fakt_Kontonummer[Final]", "Text2"},
{"Auftrags-Bestellnummer", "Text3"},
{"Datum_Auftrag-Bestellung", "Text4"},
{"Bestellt_von", "Text5"}
})
Dim oPositionMapper = oMapperConfig.GetMapper(Of ReportPosition)(New Dictionary(Of String, String) From {
{"Artikelnummer", "Text1"},
{"Lieferantenartikelnummer", "Text2"},
{"Menge_bestellt", "Text4"},
{"Menge_geliefert", "Text5"},
{"Colli", "Text6"},
{"Einzelpreis[Original]", "Text7"},
{"Einzelpreis[Final]", "Text8"},
{"EinheitProPalette", "Text9"},
{"Lagerstand", "Text10"}
})
Dim oSQLConfig = TemplateConfig.Items.
Where(Function(item) item.Function.Name = "SQL").
ToList()
FillFieldValuesFromSQL(pDocument, oSQLConfig)
Dim oHeadRow = pDocument.Rows.
Where(Function(r) r.Name.EndsWith("T025")).
Select(Function(r) r.Fields).
FirstOrDefault()
Dim oPositionRows = pDocument.Rows.
Where(Function(r) r.Name.EndsWith("T026")).
ToList()
Dim oReportHead = oHeadMapper.Map(Of Dictionary(Of String, FieldValue), ReportHead)(oHeadRow)
oReportHead.Title = "EDI Bestellung (orders)"
oReportHead.Subtitle = "Schaum"
oReportHead.Filename = pDocument.FileName
Dim oReportPositions As New List(Of ReportPosition)
Dim oCounter = 1
For Each oRow As DocumentRow In oPositionRows
Dim oReportPosition As ReportPosition = oPositionMapper.Map(Of Dictionary(Of String, FieldValue), ReportPosition)(oRow.Fields)
oReportPosition.Id = oCounter
oReportPositions.Add(oReportPosition)
oCounter += 1
Next
Dim oReportSource As New ReportSource With {
.Head = oReportHead,
.Positions = oReportPositions
}
Dim oReport = Activator.CreateInstance(GetType(TReport))
Dim oDataSource = New DevExpress.DataAccess.ObjectBinding.ObjectDataSource With {
.DataSource = oReportSource
}
oDataSource.Fill()
oReport.DataSource = oDataSource
Return oReport
End Function
Private Sub FillFieldValuesFromSQL(pDocument As Document, oSQLConfig As List(Of TemplateConfigItem))
For Each oSQLConfigItem In oSQLConfig
' FieldList is a list of fields that will be changed
' Example: Setting SQL for Article StorageLocation will invoke the sql for each row
Dim oRowList = pDocument.Rows.
Where(Function(row) row.Fields.Any(Function(field) field.Key = oSQLConfigItem.Name)).
ToList()
For Each oRow As DocumentRow In oRowList
Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault()
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oSQL = oSQLConfigItem.Function.Params
Dim oMatches As MatchCollection = oRegex.Matches(oSQL)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case "FIELD"
Dim oFieldName = oPlaceholderValue
Dim oTargetField = oRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
oSQL = oSQL.Replace(oPlaceholderString, oTargetField.Value.Final)
Case "CONST"
Select Case oMatch.Groups.Item(2).Value.ToUpper
Case "MESOYEAR"
oSQL = oSQL.Replace(oPlaceholderString, GeneralConfig.GetWinLineYear())
Case "MESOCOMP"
oSQL = oSQL.Replace(oPlaceholderString, pDocument.Mandator.Id)
End Select
End Select
Next
Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then
oField.Value.Final = oValue
End If
Next
Next
End Sub
End Class

View File

@ -13,18 +13,20 @@ Namespace Winline
Private ReadOnly Config As WebServiceConfig
Private ReadOnly Serializer As Serializer
Private ReadOnly OutputDirectory As String
Private ReadOnly Helpers As Helpers
Public Event WebServiceProgress As EventHandler(Of String)
Public Sub New(pLogConfig As LogConfig, pWebserviceConfig As WebServiceConfig, pOutputDirectoryPath As String)
MyBase.New(pLogConfig)
Helpers = New Helpers(pLogConfig)
Serializer = New Serializer(pLogConfig)
Config = pWebserviceConfig
OutputDirectory = pOutputDirectoryPath
End Sub
Public Sub RaiseWebServiceProgress(pMessage As String)
RaiseEvent WebServiceProgress(Me, pMessage)
End Sub
Public Async Function TransferDocumentToWinline(pDocument As Document, pMandator As Mandator, Optional pIsTest As Boolean = False) As Task(Of Boolean)
Dim oBytes As Byte() = GetBytesFromDocument(pDocument)
Dim oWS = Config