Add PreferExternalValue to control which value will be the final value

This commit is contained in:
Jonathan Jenne 2022-05-03 15:32:35 +02:00
parent 5fe4079028
commit 5accfbe002
6 changed files with 84 additions and 46 deletions

View File

@ -242,29 +242,20 @@ Namespace Documents
' oValue = oDate.ToString("d") ' oValue = oDate.ToString("d")
'End If 'End If
oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With { Dim oFieldValue = GetFieldValueFromColumn(oColumn, oColumnSortKey)
.Original = oValue, oFieldValue.SetOriginalValue(oValue)
.Final = oValue,
.DataType = oColumn.DataType, oFields.Add(oSubElement.Name.ToString, oFieldValue)
.IsRequired = oRequired,
.IsVirtual = oColumn.Config.IsVirtual,
.SortKey = oColumnSortKey
})
Else Else
Logger.Debug("Creating new field from Configuration: [{0}]", oColumn.Name) Logger.Debug("Creating new field from Configuration: [{0}]", oColumn.Name)
Dim oValue = New DocumentRow.FieldValue With { Dim oFieldValue = GetFieldValueFromColumn(oColumn, oColumnSortKey)
.SortKey = oColumnSortKey,
.IsVirtual = oColumn.Config.IsVirtual
}
'oValue.Error = FieldErrorType.None
If oColumn.Config?.IsRequired Then If oColumn.Config?.IsRequired Then
'oValue.Error = FieldErrorType.MissingValue oFieldValue.AddFieldError(FieldErrorType.MissingValue, $"Attribut {oSubElement.Name} wird benötigt, ist aber nicht gefüllt.")
oValue.AddFieldError(FieldErrorType.MissingValue, $"Attribut {oSubElement.Name} wird benötigt, ist aber nicht gefüllt.")
End If End If
oFields.Add(oColumn.Name, oValue) oFields.Add(oColumn.Name, oFieldValue)
End If End If
oColumnSortKey += 1 oColumnSortKey += 1
@ -291,6 +282,16 @@ Namespace Documents
Return pDocument Return pDocument
End Function End Function
Public Function GetFieldValueFromColumn(pColumn As Template.Column, pSortKey As Integer) As DocumentRow.FieldValue
Return New DocumentRow.FieldValue With {
.DataType = pColumn.DataType,
.IsRequired = pColumn.IsRequired,
.IsVirtual = pColumn.Config.IsVirtual,
.PreferExternalValue = pColumn.Config.PreferExternalValue,
.SortKey = pSortKey
}
End Function
Private Async Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Mandator), pMandator As Mandator, pTemplate As Template) As Task(Of Document) Private Async Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Mandator), pMandator As Mandator, pTemplate As Template) As Task(Of Document)
Dim oMandators As List(Of Mandator) = pMandators. Dim oMandators As List(Of Mandator) = pMandators.
@ -361,8 +362,7 @@ Namespace Documents
Dim oValue = Database.GetScalarValue(oSQL) Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then If oValue IsNot Nothing Then
oField.Value.External = oValue oField.Value.SetExternalValue(oValue)
oField.Value.Final = oValue
End If End If
Next Next
Next Next
@ -494,7 +494,7 @@ Namespace Documents
Dim oValue As String = Utils.NotNull(oRawValue, String.Empty) Dim oValue As String = Utils.NotNull(oRawValue, String.Empty)
If oValue <> String.Empty Then If oValue <> String.Empty Then
oField.Value.Final = oValue oField.Value.SetExternalValue(oValue)
End If End If
Else Else
@ -541,7 +541,7 @@ Namespace Documents
pDocument.Rows. pDocument.Rows.
SelectMany(Function(row) row.Fields). SelectMany(Function(row) row.Fields).
Where(Function(field) field.Key = oMapping.DestinationItem). Where(Function(field) field.Key = oMapping.DestinationItem).
SetValue(Sub(field) field.Value.Final = oMapping.DestinationValue) SetValue(Sub(field) field.Value.SetExternalValue(oMapping.DestinationValue))
Else Else
' don't do anything ' don't do anything
@ -602,8 +602,7 @@ Namespace Documents
Dim oArticlePrice As Double = Await Winline.TryGetArticlePriceAsync(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate, oWaitingDays) Dim oArticlePrice As Double = Await Winline.TryGetArticlePriceAsync(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate, oWaitingDays)
If oArticlePrice > 0 Then If oArticlePrice > 0 Then
oPriceItem.External = oArticlePrice oPriceItem.SetExternalValue(oArticlePrice)
oPriceItem.Final = oArticlePrice
Logger.Info("Price for Item [{0}] set to [{1}]", pPriceField, oArticlePrice) Logger.Info("Price for Item [{0}] set to [{1}]", pPriceField, oArticlePrice)
Else Else
Logger.Warn("Price for Item [{0}] could not be found!", pPriceField) Logger.Warn("Price for Item [{0}] could not be found!", pPriceField)
@ -613,9 +612,9 @@ Namespace Documents
Private Sub SetArticleByEAN(pRow As DocumentRow, pMandator As Mandator, pArticleField As String) Private Sub SetArticleByEAN(pRow As DocumentRow, pMandator As Mandator, pArticleField As String)
Dim oNumberItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pArticleField) Dim oNumberItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pArticleField)
Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator) Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator)
If oArticleNumber IsNot Nothing Then If oArticleNumber IsNot Nothing Then
oNumberItem.External = oArticleNumber oNumberItem.SetExternalValue(oArticleNumber)
oNumberItem.Final = oArticleNumber
Else Else
'oNumberItem.Error = FieldErrorType.ArticleNotFound 'oNumberItem.Error = FieldErrorType.ArticleNotFound
oNumberItem.AddFieldError(FieldErrorType.ArticleNotFound, $"EAN in Attribut '{pArticleField}' konnte nicht aufgelöst werden.") oNumberItem.AddFieldError(FieldErrorType.ArticleNotFound, $"EAN in Attribut '{pArticleField}' konnte nicht aufgelöst werden.")
@ -639,12 +638,10 @@ Namespace Documents
' If an account was found, set it for External and Final value ' If an account was found, set it for External and Final value
If oAccount IsNot Nothing Then If oAccount IsNot Nothing Then
oNumberItem.External = oAccount.Id oNumberItem.SetExternalValue(oAccount.Id)
oNumberItem.Final = oAccount.Id
If oContainsAccountName Then If oContainsAccountName Then
oNameItem.External = oAccount.Name oNameItem.SetExternalValue(oAccount.Name)
oNameItem.Final = oAccount.Name
Else Else
' TODO: What to to if name field is missing or not set? ' TODO: What to to if name field is missing or not set?
'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With { 'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
@ -683,8 +680,7 @@ Namespace Documents
Dim oVersionedNumber = Await Winline.GetVersionedRunningNumberAsync(pDocument, pMandator, oAccountNumber, oRunningNumber) Dim oVersionedNumber = Await Winline.GetVersionedRunningNumberAsync(pDocument, pMandator, oAccountNumber, oRunningNumber)
If oVersionedNumber <> oRunningNumber Then If oVersionedNumber <> oRunningNumber Then
oRunningNumberItem.External = oVersionedNumber oRunningNumberItem.SetExternalValue(oVersionedNumber)
oRunningNumberItem.Final = oVersionedNumber
End If End If
Catch ex As Exception Catch ex As Exception

View File

@ -51,6 +51,18 @@ Namespace Documents
Return SortKey.CompareTo(DirectCast(other, DocumentRow).SortKey) Return SortKey.CompareTo(DirectCast(other, DocumentRow).SortKey)
End Function End Function
''' <summary>
''' TODO: Use this class to
''' </summary>
Public Class Field
Public ReadOnly Property Name
Public Property Value As FieldValue
Public Sub New(pName As String)
Name = pName
End Sub
End Class
Public Class FieldValue Public Class FieldValue
Private _Final As String = "" Private _Final As String = ""
Private _External As String = "" Private _External As String = ""
@ -60,38 +72,33 @@ Namespace Documents
Public Property Errors As New List(Of FieldError) Public Property Errors As New List(Of FieldError)
Public Property Original As String Public ReadOnly Property Original As String
Get Get
Return FormatValue(_Original, DataType) Return FormatValue(_Original, DataType)
End Get End Get
Set(value As String)
_Original = value
End Set
End Property End Property
Public Property External As String Public ReadOnly Property External As String
Get Get
Return FormatValue(_External, DataType) Return FormatValue(_External, DataType)
End Get End Get
Set(value As String)
_External = value
End Set
End Property End Property
Public Property Final As String Public ReadOnly Property Final As String
Get Get
Return FormatValue(_Final, DataType) Return FormatValue(_Final, DataType)
End Get End Get
Set(value As String)
_Final = value
End Set
End Property End Property
Public Property IsRequired As Boolean = False Public Property IsRequired As Boolean = False
Public Property IsVirtual As Boolean = False Public Property IsVirtual As Boolean = False
Public Property PreferExternalValue As Boolean = True
Public Property SortKey As Integer = 0 Public Property SortKey As Integer = 0
Public Function GetValue(pValueType) As String Public Sub New()
End Sub
Public Function GetValue(pValueType As String) As String
Select Case pValueType Select Case pValueType
Case "Original" Case "Original"
Return Original Return Original
@ -104,6 +111,19 @@ Namespace Documents
End Select End Select
End Function End Function
Public Sub SetValue(pValueType As String, pValue As String)
Select Case pValueType
Case "Original"
_Original = pValue
Case "External"
_External = pValue
Case "Final"
_Final = pValue
Case Else
' Noop
End Select
End Sub
Public Sub AddFieldError(pType As FieldErrorType, pMessage As String) Public Sub AddFieldError(pType As FieldErrorType, pMessage As String)
Errors.Add(New FieldError() With { Errors.Add(New FieldError() With {
.Type = pType, .Type = pType,
@ -111,6 +131,27 @@ Namespace Documents
}) })
End Sub End Sub
Public Sub SetExternalValue(pValue As String)
_External = pValue
' Set the external value as the final value, overriding the original / previous external value
' if the external value should be preferred
If PreferExternalValue = True Then
_Final = pValue
End If
' If there is no Original value (because the field is virtual),
' set the external value as the final value regardless of the PreferExternalValue setting
If Original = String.Empty Then
_Final = pValue
End If
End Sub
Public Sub SetOriginalValue(pValue As String)
_Original = pValue
_Final = pValue
End Sub
Public ReadOnly Property HasError As Boolean Public ReadOnly Property HasError As Boolean
Get Get
' Required check was moved to DocumentLoader ' Required check was moved to DocumentLoader

View File

@ -122,7 +122,7 @@ Public Class ReportGenerator(Of TReport As IReport)
Dim oValue = Database.GetScalarValue(oSQL) Dim oValue = Database.GetScalarValue(oSQL)
If oValue IsNot Nothing Then If oValue IsNot Nothing Then
oField.Value.Final = oValue oField.Value.SetExternalValue(oValue)
End If End If
Next Next
Next Next

View File

@ -14,6 +14,7 @@ Namespace Templates
Public Property IsVisible As Boolean Public Property IsVisible As Boolean
Public Property IsRequired As Boolean Public Property IsRequired As Boolean
Public Property IsVirtual As Boolean Public Property IsVirtual As Boolean
Public Property PreferExternalValue As Boolean
Public Property [Function] As ColumnFunction Public Property [Function] As ColumnFunction

View File

@ -168,6 +168,7 @@ Namespace Templates
.IsRequired = oRow.ItemEx("IS_REQUIRED", False), .IsRequired = oRow.ItemEx("IS_REQUIRED", False),
.IsVirtual = oRow.ItemEx("IS_VIRTUAL", False), .IsVirtual = oRow.ItemEx("IS_VIRTUAL", False),
.IsHead = oRow.ItemEx("IS_HEAD", True), .IsHead = oRow.ItemEx("IS_HEAD", True),
.PreferExternalValue = oRow.ItemEx("PREFER_EXTERNAL", True),
.[Function] = New TemplateConfigItem.ColumnFunction With { .[Function] = New TemplateConfigItem.ColumnFunction With {
.Id = oRow.ItemEx("FUNCTION_ID", 0), .Id = oRow.ItemEx("FUNCTION_ID", 0),
.Name = oRow.ItemEx("FUNCTION_NAME", String.Empty), .Name = oRow.ItemEx("FUNCTION_NAME", String.Empty),

View File

@ -184,11 +184,10 @@ Public Class frmRowEditor
'End If 'End If
' 03.12.21: For now we always remove the error if ANYTHING changed about the field ' 03.12.21: For now we always remove the error if ANYTHING changed about the field
'oFieldValue.Error = FieldErrorType.None
oFieldValue.Errors.Clear() oFieldValue.Errors.Clear()
' Save the grid value to the Field ' Save the grid value to the Field
oFieldValue.Final = oValueFromGrid.Trim() oFieldValue.SetValue("Final", oValueFromGrid.Trim())
If _DocumentRow.Fields.ContainsKey(oField.Key) Then If _DocumentRow.Fields.ContainsKey(oField.Key) Then
_DocumentRow.Fields.Item(oField.Key) = oFieldValue _DocumentRow.Fields.Item(oField.Key) = oFieldValue