From 5accfbe0021552521e2e60ef80a19f44f9444023 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 3 May 2022 15:32:35 +0200 Subject: [PATCH] Add PreferExternalValue to control which value will be the final value --- MultiTool.Common/Documents/DocumentLoader.vb | 56 +++++++--------- MultiTool.Common/Documents/DocumentRow.vb | 67 +++++++++++++++---- MultiTool.Common/Report/ReportGenerator.vb | 2 +- .../Templates/TemplateConfigItem.vb | 1 + MultiTool.Common/Templates/TemplateLoader.vb | 1 + MultiTool.Form/frmRowEditor.vb | 3 +- 6 files changed, 84 insertions(+), 46 deletions(-) diff --git a/MultiTool.Common/Documents/DocumentLoader.vb b/MultiTool.Common/Documents/DocumentLoader.vb index 4c309a7..16f6790 100644 --- a/MultiTool.Common/Documents/DocumentLoader.vb +++ b/MultiTool.Common/Documents/DocumentLoader.vb @@ -242,29 +242,20 @@ Namespace Documents ' oValue = oDate.ToString("d") 'End If - oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With { - .Original = oValue, - .Final = oValue, - .DataType = oColumn.DataType, - .IsRequired = oRequired, - .IsVirtual = oColumn.Config.IsVirtual, - .SortKey = oColumnSortKey - }) + Dim oFieldValue = GetFieldValueFromColumn(oColumn, oColumnSortKey) + oFieldValue.SetOriginalValue(oValue) + + oFields.Add(oSubElement.Name.ToString, oFieldValue) Else Logger.Debug("Creating new field from Configuration: [{0}]", oColumn.Name) - Dim oValue = New DocumentRow.FieldValue With { - .SortKey = oColumnSortKey, - .IsVirtual = oColumn.Config.IsVirtual - } + Dim oFieldValue = GetFieldValueFromColumn(oColumn, oColumnSortKey) - 'oValue.Error = FieldErrorType.None If oColumn.Config?.IsRequired Then - 'oValue.Error = FieldErrorType.MissingValue - oValue.AddFieldError(FieldErrorType.MissingValue, $"Attribut {oSubElement.Name} wird benötigt, ist aber nicht gefüllt.") + oFieldValue.AddFieldError(FieldErrorType.MissingValue, $"Attribut {oSubElement.Name} wird benötigt, ist aber nicht gefüllt.") End If - oFields.Add(oColumn.Name, oValue) + oFields.Add(oColumn.Name, oFieldValue) End If oColumnSortKey += 1 @@ -291,6 +282,16 @@ Namespace Documents Return pDocument 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) Dim oMandators As List(Of Mandator) = pMandators. @@ -361,8 +362,7 @@ Namespace Documents Dim oValue = Database.GetScalarValue(oSQL) If oValue IsNot Nothing Then - oField.Value.External = oValue - oField.Value.Final = oValue + oField.Value.SetExternalValue(oValue) End If Next Next @@ -494,7 +494,7 @@ Namespace Documents Dim oValue As String = Utils.NotNull(oRawValue, String.Empty) If oValue <> String.Empty Then - oField.Value.Final = oValue + oField.Value.SetExternalValue(oValue) End If Else @@ -541,7 +541,7 @@ Namespace Documents pDocument.Rows. SelectMany(Function(row) row.Fields). Where(Function(field) field.Key = oMapping.DestinationItem). - SetValue(Sub(field) field.Value.Final = oMapping.DestinationValue) + SetValue(Sub(field) field.Value.SetExternalValue(oMapping.DestinationValue)) Else ' don't do anything @@ -602,8 +602,7 @@ Namespace Documents Dim oArticlePrice As Double = Await Winline.TryGetArticlePriceAsync(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate, oWaitingDays) If oArticlePrice > 0 Then - oPriceItem.External = oArticlePrice - oPriceItem.Final = oArticlePrice + oPriceItem.SetExternalValue(oArticlePrice) Logger.Info("Price for Item [{0}] set to [{1}]", pPriceField, oArticlePrice) Else 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) Dim oNumberItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pArticleField) Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator) + If oArticleNumber IsNot Nothing Then - oNumberItem.External = oArticleNumber - oNumberItem.Final = oArticleNumber + oNumberItem.SetExternalValue(oArticleNumber) Else 'oNumberItem.Error = FieldErrorType.ArticleNotFound 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 oAccount IsNot Nothing Then - oNumberItem.External = oAccount.Id - oNumberItem.Final = oAccount.Id + oNumberItem.SetExternalValue(oAccount.Id) If oContainsAccountName Then - oNameItem.External = oAccount.Name - oNameItem.Final = oAccount.Name + oNameItem.SetExternalValue(oAccount.Name) Else ' TODO: What to to if name field is missing or not set? 'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With { @@ -683,8 +680,7 @@ Namespace Documents Dim oVersionedNumber = Await Winline.GetVersionedRunningNumberAsync(pDocument, pMandator, oAccountNumber, oRunningNumber) If oVersionedNumber <> oRunningNumber Then - oRunningNumberItem.External = oVersionedNumber - oRunningNumberItem.Final = oVersionedNumber + oRunningNumberItem.SetExternalValue(oVersionedNumber) End If Catch ex As Exception diff --git a/MultiTool.Common/Documents/DocumentRow.vb b/MultiTool.Common/Documents/DocumentRow.vb index 1f334fe..f8a55b5 100644 --- a/MultiTool.Common/Documents/DocumentRow.vb +++ b/MultiTool.Common/Documents/DocumentRow.vb @@ -51,6 +51,18 @@ Namespace Documents Return SortKey.CompareTo(DirectCast(other, DocumentRow).SortKey) End Function + ''' + ''' TODO: Use this class to + ''' + 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 Private _Final As String = "" Private _External As String = "" @@ -60,38 +72,33 @@ Namespace Documents Public Property Errors As New List(Of FieldError) - Public Property Original As String + Public ReadOnly Property Original As String Get Return FormatValue(_Original, DataType) End Get - Set(value As String) - _Original = value - End Set End Property - Public Property External As String + Public ReadOnly Property External As String Get Return FormatValue(_External, DataType) End Get - Set(value As String) - _External = value - End Set End Property - Public Property Final As String + Public ReadOnly Property Final As String Get Return FormatValue(_Final, DataType) End Get - Set(value As String) - _Final = value - End Set End Property Public Property IsRequired As Boolean = False Public Property IsVirtual As Boolean = False + Public Property PreferExternalValue As Boolean = True 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 Case "Original" Return Original @@ -104,6 +111,19 @@ Namespace Documents End Select 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) Errors.Add(New FieldError() With { .Type = pType, @@ -111,6 +131,27 @@ Namespace Documents }) 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 Get ' Required check was moved to DocumentLoader diff --git a/MultiTool.Common/Report/ReportGenerator.vb b/MultiTool.Common/Report/ReportGenerator.vb index 0f57199..7c214ee 100644 --- a/MultiTool.Common/Report/ReportGenerator.vb +++ b/MultiTool.Common/Report/ReportGenerator.vb @@ -122,7 +122,7 @@ Public Class ReportGenerator(Of TReport As IReport) Dim oValue = Database.GetScalarValue(oSQL) If oValue IsNot Nothing Then - oField.Value.Final = oValue + oField.Value.SetExternalValue(oValue) End If Next Next diff --git a/MultiTool.Common/Templates/TemplateConfigItem.vb b/MultiTool.Common/Templates/TemplateConfigItem.vb index 973b559..4c3cff7 100644 --- a/MultiTool.Common/Templates/TemplateConfigItem.vb +++ b/MultiTool.Common/Templates/TemplateConfigItem.vb @@ -14,6 +14,7 @@ Namespace Templates Public Property IsVisible As Boolean Public Property IsRequired As Boolean Public Property IsVirtual As Boolean + Public Property PreferExternalValue As Boolean Public Property [Function] As ColumnFunction diff --git a/MultiTool.Common/Templates/TemplateLoader.vb b/MultiTool.Common/Templates/TemplateLoader.vb index ab6c89a..75c003c 100644 --- a/MultiTool.Common/Templates/TemplateLoader.vb +++ b/MultiTool.Common/Templates/TemplateLoader.vb @@ -168,6 +168,7 @@ Namespace Templates .IsRequired = oRow.ItemEx("IS_REQUIRED", False), .IsVirtual = oRow.ItemEx("IS_VIRTUAL", False), .IsHead = oRow.ItemEx("IS_HEAD", True), + .PreferExternalValue = oRow.ItemEx("PREFER_EXTERNAL", True), .[Function] = New TemplateConfigItem.ColumnFunction With { .Id = oRow.ItemEx("FUNCTION_ID", 0), .Name = oRow.ItemEx("FUNCTION_NAME", String.Empty), diff --git a/MultiTool.Form/frmRowEditor.vb b/MultiTool.Form/frmRowEditor.vb index 1afbdeb..87ecfbe 100644 --- a/MultiTool.Form/frmRowEditor.vb +++ b/MultiTool.Form/frmRowEditor.vb @@ -184,11 +184,10 @@ Public Class frmRowEditor 'End If ' 03.12.21: For now we always remove the error if ANYTHING changed about the field - 'oFieldValue.Error = FieldErrorType.None oFieldValue.Errors.Clear() ' Save the grid value to the Field - oFieldValue.Final = oValueFromGrid.Trim() + oFieldValue.SetValue("Final", oValueFromGrid.Trim()) If _DocumentRow.Fields.ContainsKey(oField.Key) Then _DocumentRow.Fields.Item(oField.Key) = oFieldValue