Execute SQL function when loading document

This commit is contained in:
Jonathan Jenne
2022-05-03 12:29:08 +02:00
parent 623e1717d6
commit 5fe4079028
4 changed files with 57 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ Imports MultiTool.Common.Templates
Imports MultiTool.Common.Winline
Imports MultiTool.Common.Winline.Entities
Imports MultiTool.Common.Constants
Imports DigitalData.Modules.Database
Namespace Documents
Public Class DocumentLoader
@@ -16,6 +17,7 @@ Namespace Documents
Private ReadOnly MappingConfig As MappingConfig
Private ReadOnly TemplateConfig As TemplateConfig
Private ReadOnly ApplicationConfig As Config
Private ReadOnly Patterns As Patterns
Public Property Files As New List(Of Document)
@@ -45,12 +47,13 @@ Namespace Documents
End Sub
End Class
Public Sub New(pLogConfig As LogConfig, pWinline As WinlineData, pMappingConfig As MappingConfig, pTemplateConfig As TemplateConfig, pApplicationConfig As Config)
MyBase.New(pLogConfig)
Public Sub New(pLogConfig As LogConfig, pWinline As WinlineData, pDatabase As MSSQLServer, pMappingConfig As MappingConfig, pTemplateConfig As TemplateConfig, pGeneralConfig As GeneralConfig, pApplicationConfig As Config)
MyBase.New(pLogConfig, pDatabase)
Winline = pWinline
MappingConfig = pMappingConfig
TemplateConfig = pTemplateConfig
ApplicationConfig = pApplicationConfig
Patterns = New Patterns(pLogConfig, pGeneralConfig)
End Sub
Public Async Function LoadFiles(pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
@@ -314,34 +317,60 @@ Namespace Documents
' Set mandator befor applying any functions that depend on a valid mandator
pDocument.Mandator = oMandator
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {
.RunningFunction = "Winline-Funktionen"
})
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Winline-Funktionen"})
pDocument = Await ApplyDefinedItemFunctionsForImportAsync(pDocument, oMandator, pTemplate)
pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator)
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {
.RunningFunction = "Preis-Funktionen"
})
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Preis-Funktionen"})
If ApplicationConfig.AutomaticPriceCalculation = True Then
' These functions will only be applied if the document does not have errors
pDocument = Await MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate)
End If
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {
.RunningFunction = "Feld-Funktionen"
})
RaiseEvent FileLoadProgress(Me, New FileLoadProgressInfo(FilesTotal, FilesLoaded) With {.RunningFunction = "Feld-Funktionen"})
pDocument = ApplySQLFunctionForImport(pDocument, TemplateConfig.SqlItems)
' This function needs to be the last one because
' it can relate to any previously set value
ApplyFieldFunctionForImport(pDocument, oMandator, pTemplate)
pDocument = ApplyFieldFunctionForImport(pDocument, oMandator, pTemplate)
End If
Return pDocument
End Function
Private Function ApplySQLFunctionForImport(pDocument As Document, pSQLConfig As List(Of TemplateConfigItem)) As Document
For Each oSQLConfigItem In pSQLConfig
' 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 oSQL = oSQLConfigItem.Function.Params
Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault()
oSQL = Patterns.ReplaceForImport(pDocument, oRow, oSQL)
Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then
oField.Value.External = oValue
oField.Value.Final = oValue
End If
Next
Next
Return pDocument
End Function
''' <summary>
''' Apply price calculation to the documents products
'''

View File

@@ -56,11 +56,7 @@ Public Class ReportGenerator(Of TReport As IReport)
{"Lagerstand", "Text10"}
})
Dim oSQLConfig = TemplateConfig.Items.
Where(Function(item) item.Function.Name = Constants.FUNCTION_SQL).
ToList()
pDocument = FillFieldValuesFromSQL(pDocument, oSQLConfig, oFilePath)
pDocument = FillFieldValuesFromSQL(pDocument, TemplateConfig.SqlItems, oFilePath)
Dim oHeadRow = pDocument.Rows.
Where(Function(r) r.TableName.EndsWith("T025")).
@@ -116,12 +112,12 @@ Public Class ReportGenerator(Of TReport As IReport)
ToList()
For Each oRow As DocumentRow In oRowList
Dim oSQL = oSQLConfigItem.Function.Params
Dim oField = oRow.Fields.
Where(Function(field) field.Key = oSQLConfigItem.Name).
SingleOrDefault()
Dim oSQL = oSQLConfigItem.Function.Params
oSQL = Patterns.ReplaceForImport(pDocument, oRow, pReportFileName, oSQL)
oSQL = Patterns.ReplaceForImportFinalSQL(pDocument, pReportFileName, oSQL)
Dim oValue = Database.GetScalarValue(oSQL)

View File

@@ -8,6 +8,14 @@ Namespace Templates
Public Class TemplateConfig
Public Property Items As List(Of TemplateConfigItem)
Public ReadOnly Property SqlItems As List(Of TemplateConfigItem)
Get
Return Items.
Where(Function(item) item.Function.Name = Constants.FUNCTION_SQL).
ToList()
End Get
End Property
Public Function GetColumn(pName As String, pTable As String) As TemplateConfigItem
Return Items.
Where(Function(c) c.Name = pName And c.Table = pTable).

View File

@@ -91,7 +91,11 @@ Public Class frmImportMain
lookupMandator.ForceInitialize()
lookupMandator.Properties.View.BestFitColumns()
DocumentLoader = New DocumentLoader(LogConfig, Winline, My.MappingConfiguration, My.TemplateConfiguration, ConfigManager.Config)
DocumentLoader = New DocumentLoader(LogConfig, Winline, Database,
My.MappingConfiguration,
My.TemplateConfiguration,
My.GeneralConfiguration,
ConfigManager.Config)
DocumentCleaner = New DocumentCleaner(LogConfig, CurrentTemplate)
GridLoader = New GridLoader(LogConfig)