Add support for multiple functions per field, add ADDRESS function

This commit is contained in:
Jonathan Jenne
2023-06-26 11:27:51 +02:00
parent 7846e660b9
commit 68e4c59e63
9 changed files with 274 additions and 154 deletions

View File

@@ -8,6 +8,7 @@ Imports MultiTool.Common.Winline
Imports MultiTool.Common.Winline.Entities
Imports MultiTool.Common.Constants
Imports DigitalData.Modules.Database
Imports DevExpress.Utils.CommonDialogs
Namespace Documents
Public Class DocumentLoader
@@ -88,7 +89,7 @@ Namespace Documents
Public Async Function LoadFile(pFileInfo As FileInfo, pTemplate As Template, pMandator As Mandator) As Task(Of Document)
Logger.Debug("Creating new Document object for file [{0}]", pFileInfo.Name)
Dim oDocument As Document = New Document With {
Dim oDocument As New Document With {
.File = pFileInfo,
.Schema = pTemplate
}
@@ -349,26 +350,37 @@ Namespace Documents
Private Function ApplySQLFunctionForImport(pDocument As Document, pSQLConfig As List(Of FieldConfig)) 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 oFunction In oSQLConfigItem.Functions
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.SetExternalValue(oValue)
If Not oFunction.Name = FUNCTION_SQL Then
Continue For
End If
' 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 = oFunction.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.SetExternalValue(oValue)
End If
Next
Next
Next
Return pDocument
@@ -400,22 +412,27 @@ Namespace Documents
Continue For
End If
Dim oFunctionName = oColumn.Config.FunctionName
Dim oFunctionParams = oColumn.Config.FunctionParams
For Each oFunction As FieldConfig.ColumnFunction In oColumn.Config.Functions
If oFunctionName = String.Empty Then
Continue For
End If
Logger.Debug("Running Function: [{0}]", oFunctionName)
Logger.Debug("With Parameters: [{0}]", oFunctionParams)
Dim oFunctionName = oFunction.Name
Dim oFunctionParams = oFunction.Params
Dim oParamsDict = Parameters.Parse(oFunctionParams)
If oFunctionName = Constants.FUNCTION_PRICE Then
Await SetPrice(oRow, oField.Key, oParamsDict, pDocument, pMandator, pTemplate)
End If
If oFunctionName = String.Empty Then
Continue For
End If
Logger.Debug("Running Function: [{0}]", oFunctionName)
Logger.Debug("With Parameters: [{0}]", oFunctionParams)
Dim oParamsDict = Parameters.Parse(oFunctionParams)
If oFunctionName = Constants.FUNCTION_PRICE Then
Await SetPrice(oRow, oField.Key, oParamsDict, pDocument, pMandator, pTemplate)
End If
Next
Next
Next
@@ -431,40 +448,49 @@ Namespace Documents
Exit For
End If
Dim oColumn = oTable.Columns.Where(Function(c) c.Name = oField.Key).SingleOrDefault()
Dim oItemName As String = oField.Key
Dim oColumn = oTable.Columns.Where(Function(c) c.Name = oItemName).SingleOrDefault()
If oColumn Is Nothing Then
Continue For
End If
Dim oFunctionName = oColumn.Config.FunctionName
Dim oFunctionParams = oColumn.Config.FunctionParams
For Each oFunction As FieldConfig.ColumnFunction In oColumn.Config.Functions
Dim oFunctionName = oFunction.Name
Dim oFunctionParams = oFunction.Params
' The code below needs a defined function
If oFunctionName = String.Empty Then
Continue For
End If
' The code below needs a defined function
If oFunctionName = String.Empty Then
Continue For
End If
Dim oParamsDict = Parameters.Parse(oFunctionParams)
Dim oParamsDict = Parameters.Parse(oFunctionParams)
' The main identifier will be checked for String.empty and not required.
' This makes sure that optional fields do not generate errors.
Dim oIdentifier As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(oField.Key)
If oIdentifier.Original = String.Empty And oIdentifier.IsRequired = False Then
Continue For
End If
' The main identifier will be checked for String.empty and not required.
' This makes sure that optional fields do not generate errors.
Dim oIdentifier As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(oItemName)
If oIdentifier.Original = String.Empty And oIdentifier.IsRequired = False Then
Continue For
End If
Select Case oFunctionName
Case FUNCTION_GLN
SetAccountByGLN(oRow, pMandator, oField.Key, Nothing, oParamsDict)
Select Case oFunctionName
Case FUNCTION_GLN
SetAccountByGLN(oRow, pMandator, oItemName, Nothing, oParamsDict)
Case FUNCTION_EAN
SetArticleByEAN(oRow, pMandator, oField.Key)
Case FUNCTION_EAN
SetArticleByEAN(oRow, pMandator, oItemName)
Case FUNCTION_RUNNINGNUMBER
Await SetVersionedRunningNumber(pDocument, oRow, pMandator, oField.Key, oParamsDict)
Case FUNCTION_RUNNINGNUMBER
Await SetVersionedRunningNumber(pDocument, oRow, pMandator, oItemName, oParamsDict)
Case FUNCTION_ADDRESS
Await SetAddressByAccountNumber(pDocument, oRow, pMandator, oItemName, oParamsDict)
End Select
Next
End Select
Next
Next
@@ -489,48 +515,53 @@ Namespace Documents
End If
Dim oFunctionName = oColumn.Config.FunctionName
Dim oFunctionParams = oColumn.Config.FunctionParams
Dim oParamsDict = Parameters.Parse(oFunctionParams)
For Each oFunction As FieldConfig.ColumnFunction In oColumn.Config.Functions
If oFunctionName = Constants.FUNCTION_FIELD Then
Try
Logger.Debug("Applying function FIELD to field [{0}]", oField.Key)
Dim oFunctionName = oFunction.Name
Dim oFunctionParams = oFunction.Params
Dim oParamsDict = Parameters.Parse(oFunctionParams)
Dim oParam = oParamsDict.FirstOrDefault()
If oFunctionName = FUNCTION_FIELD Then
Try
Logger.Debug("Applying function FIELD to field [{0}]", oField.Key)
If IsNothing(oParam) Then
Logger.Warn("Function FIELD needs exactly one parameter. Skipping")
Continue For
Dim oParam = oParamsDict.FirstOrDefault()
End If
If IsNothing(oParam) Then
Logger.Warn("Function FIELD needs exactly one parameter. Skipping")
Continue For
Dim oFieldName = oParam.Key
Dim oSubKey = oParam.Value
End If
Dim oReferencedField = oRow.Fields.
Dim oFieldName = oParam.Key
Dim oSubKey = oParam.Value
Dim oReferencedField = oRow.Fields.
Where(Function(field) field.Key = oFieldName).
FirstOrDefault()
If IsNothing(oReferencedField) = False Then
Dim oRawValue = oReferencedField.Value?.GetValue(oSubKey)
Dim oValue As String = Utils.NotNull(oRawValue, String.Empty)
If IsNothing(oReferencedField) = False Then
Dim oRawValue = oReferencedField.Value?.GetValue(oSubKey)
Dim oValue As String = Utils.NotNull(oRawValue, String.Empty)
If oValue <> String.Empty Then
oField.Value.SetExternalValue(oValue)
End If
Else
Logger.Warn("Referenced Field [{0}] was not found. Skipping.", oFieldName)
Continue For
If oValue <> String.Empty Then
oField.Value.SetExternalValue(oValue)
End If
Else
Logger.Warn("Referenced Field [{0}] was not found. Skipping.", oFieldName)
Catch ex As Exception
Logger.Warn("Function FIELD could not be applied to field [{0}]. Skipping.", oField.Key)
Continue For
End If
Catch ex As Exception
Logger.Warn("Function FIELD could not be applied to field [{0}]. Skipping.", oField.Key)
Continue For
End Try
End If
Next
End Try
End If
Next
Next
@@ -629,6 +660,7 @@ Namespace Documents
Logger.Info("Price for Item [{0}] set to [{1}]", pPriceField, oArticlePrice)
Else
Logger.Warn("Price for Item [{0}] could not be found!", pPriceField)
oPriceItem.AddFieldError(FieldErrorType.PriceNotCalculated, "Der Preis für diese Position konnte nicht ermittelt werden.")
End If
End Function
@@ -662,8 +694,8 @@ Namespace Documents
End If
' Try to find an account that matches the GLN
Dim oAlternateField = pParams.GetOrDefault("AltField", String.Empty)
Dim oAccount = Winline.TryGetAccount(oNumberItem.Original, pMandator, "c260", oAlternateField)
Dim oAlternateField As String = pParams.GetOrDefault("AltField", String.Empty)
Dim oAccount As Account = Winline.TryGetAccount(oNumberItem.Original, pMandator, "c260", oAlternateField)
' If an account was found, set it for External and Final value
If oAccount IsNot Nothing Then
@@ -715,6 +747,68 @@ Namespace Documents
Throw ex
End Try
End Function
Public Function SetAddressByAccountNumber(pDocument As Document, pRow As DocumentRow, pMandator As Mandator, pAccountField As String, pParams As Dictionary(Of String, String)) As Task
Try
Const PARAMETER_NAME = "Name"
Const PARAMETER_STREET = "Street"
Const PARAMETER_ZIP = "Zip"
Const PARAMETER_CITY = "City"
Dim oAccountNumberItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pAccountField)
Dim oAccountNumber = oAccountNumberItem.Final
Dim oNameField As String = pParams.GetOrDefault(PARAMETER_NAME, Nothing)
Dim oNameFieldItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(oNameField)
If oNameField Is Nothing Then
Logger.Warn("Parameter '{0}' not found for Function ADDRESS", PARAMETER_NAME)
oAccountNumberItem.AddFieldError(FieldErrorType.MissingParameter, $"Parameter '{PARAMETER_NAME}' wurde nicht gefüllt.")
End If
Dim oStreetField As String = pParams.GetOrDefault(PARAMETER_STREET, Nothing)
Dim oStreetFieldItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(oStreetField)
If oStreetField Is Nothing Then
Logger.Warn("Parameter '{0}' not found for Function ADDRESS", PARAMETER_STREET)
oAccountNumberItem.AddFieldError(FieldErrorType.MissingParameter, $"Parameter '{PARAMETER_STREET}' wurde nicht gefüllt.")
End If
Dim oZipField As String = pParams.GetOrDefault(PARAMETER_ZIP, Nothing)
Dim oZipFieldItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(oZipField)
If oZipField Is Nothing Then
Logger.Warn("Parameter '{0}' not found for Function ADDRESS", PARAMETER_ZIP)
oAccountNumberItem.AddFieldError(FieldErrorType.MissingParameter, $"Parameter '{PARAMETER_ZIP}' wurde nicht gefüllt.")
End If
Dim oCityField As String = pParams.GetOrDefault(PARAMETER_CITY, Nothing)
Dim oCityFieldItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(oCityField)
If oCityField Is Nothing Then
Logger.Warn("Parameter '{0}' not found for Function ADDRESS", PARAMETER_CITY)
oAccountNumberItem.AddFieldError(FieldErrorType.MissingParameter, $"Parameter '{PARAMETER_CITY}' wurde nicht gefüllt.")
End If
Dim oAccount = Winline.Accounts.Where(Function(a) a.Id = oAccountNumber And a.Mandator.Equals(pMandator)).SingleOrDefault()
If oAccount Is Nothing Then
Logger.Warn("Account with Id [{0}] in Mandator [{1}] could not be found.", oAccountNumber, pMandator.Id)
End If
oNameFieldItem.SetExternalValue(oAccount.Name)
oStreetFieldItem.SetExternalValue(oAccount.StreetName)
oZipFieldItem.SetExternalValue(oAccount.ZipCode)
oCityFieldItem.SetExternalValue(oAccount.CityName)
Catch ex As Exception
Logger.Error(ex)
Throw ex
End Try
Return Task.CompletedTask
End Function
End Class
End Namespace