Add ReportGenerator class
This commit is contained in:
151
MultiTool.Shared/Report/ReportGenerator.vb
Normal file
151
MultiTool.Shared/Report/ReportGenerator.vb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user