130 lines
5.0 KiB
VB.net
130 lines
5.0 KiB
VB.net
Imports System.Text.RegularExpressions
|
|
Imports DevExpress.XtraReports
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Logging
|
|
Imports MultiTool.Common.Documents
|
|
Imports MultiTool.Common.Documents.DocumentRow
|
|
Imports MultiTool.Common.Report
|
|
Imports MultiTool.Common.Templates
|
|
Imports MultiTool.Common.Winline.Entities
|
|
|
|
Public Class ReportGenerator(Of TReport As IReport)
|
|
Inherits BaseClass
|
|
|
|
Private ReadOnly TemplateConfig As TemplateConfig
|
|
Private ReadOnly GeneralConfig As GeneralConfig
|
|
Private ReadOnly Patterns As Patterns
|
|
Private ReadOnly FileEx As DigitalData.Modules.Filesystem.File
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pTemplateConfig As TemplateConfig, pGeneralConfig As GeneralConfig)
|
|
MyBase.New(pLogConfig)
|
|
Database = pDatabase
|
|
GeneralConfig = pGeneralConfig
|
|
TemplateConfig = pTemplateConfig
|
|
Patterns = New Patterns(pLogConfig, pGeneralConfig)
|
|
FileEx = New DigitalData.Modules.Filesystem.File(LogConfig)
|
|
End Sub
|
|
|
|
Public Function GetReportFilePath(pDocument As Document, pTemplate As Template)
|
|
Dim oFinalDirectory = FileEx.CreateDateDirectory(pTemplate.OutputReportDirectory)
|
|
Dim oFileName = FileEx.GetFilenameWithSuffix(IO.Path.GetFileNameWithoutExtension(pDocument.File.Name), FileEx.GetDateTimeString, "pdf")
|
|
Dim oFilePath As String = IO.Path.Combine(oFinalDirectory, oFileName)
|
|
Return oFilePath
|
|
End Function
|
|
|
|
Public Function GenerateReport(pDocument As Document, pTemplate As Template) As TReport
|
|
|
|
Dim oMapperConfig As New Mapper(LogConfig)
|
|
Dim oHeadMapper = oMapperConfig.GetMapper(Of ReportHead)(New Dictionary(Of String, String) From {
|
|
{"Fakt_Kontoname", "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"},
|
|
{"Positionstext", "Text2"},
|
|
{"Bezeichnung", "Text3"},
|
|
{"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 = Constants.FUNCTION_SQL).
|
|
ToList()
|
|
|
|
FillFieldValuesFromSQL(pDocument, oSQLConfig)
|
|
|
|
Dim oHeadRow = pDocument.Rows.
|
|
Where(Function(r) r.TableName.EndsWith("T025")).
|
|
Select(Function(r) r.Fields).
|
|
FirstOrDefault()
|
|
Dim oPositionRows = pDocument.Rows.
|
|
Where(Function(r) r.TableName.EndsWith("T026")).
|
|
ToList()
|
|
|
|
Dim oReportHead = oHeadMapper.Map(Of Dictionary(Of String, FieldValue), ReportHead)(oHeadRow)
|
|
oReportHead.Title = pTemplate.Name
|
|
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 oSQL = oSQLConfigItem.Function.Params
|
|
oSQL = Patterns.ReplaceForImport(pDocument, oRow, oSQL)
|
|
|
|
Dim oValue = Database.GetScalarValue(oSQL)
|
|
|
|
If oValue IsNot Nothing Then
|
|
oField.Value.Final = oValue
|
|
End If
|
|
Next
|
|
Next
|
|
End Sub
|
|
End Class
|