Validation and stuff

This commit is contained in:
Jonathan Jenne
2021-11-16 16:16:46 +01:00
parent 964f2ee60c
commit 47c22e9361
13 changed files with 140 additions and 70 deletions

View File

@@ -77,7 +77,7 @@ Namespace Documents
Select(AddressOf WrapFileInfo).
Select(Function(d) IncludeSchema(d, pSchema)).
Select(Function(d) LoadDocumentData(d, pSchema)).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators, pMandator)).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators, pMandator, pSchema)).
SingleOrDefault()
Catch ex As Exception
Logger.Error(ex)
@@ -145,9 +145,9 @@ Namespace Documents
Where(Function(t) t.Name = oTopLevelElement.Name).
FirstOrDefault()
' TODO: All fields in the schema should be generated,
' only creating the ones with values leads to wrong visual cues when asking for
' docs/rows/fields with errors
For Each oSubElement As XElement In oSubElements
Dim oSchemaField = oTable.Columns.
@@ -167,6 +167,25 @@ Namespace Documents
.Final = oValue,
.DataType = oSchemaField.DataType
})
Next
' TODO: All fields in the schema should be generated,
' only creating the ones with values leads to wrong visual cues when asking for
' docs/rows/fields with errors
For Each oColumn In oTable.Columns
If oFields.ContainsKey(oColumn.Name) Then
Continue For
End If
Dim oColumnError = DocumentRow.FieldError.None
If oColumn.Config?.IsRequired Then
oColumnError = DocumentRow.FieldError.MissingValue
End If
oFields.Add(oColumn.Name, New DocumentRow.FieldValue With {
.[Error] = oColumnError
})
Next
' Create a DocumentRow object for each Top Level Element
@@ -189,7 +208,7 @@ Namespace Documents
End Function
Private Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Mandator), pMandator As Mandator) As Document
Private Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Mandator), pMandator As Mandator, pSchema As Schema) As Document
Dim oMandators As List(Of Winline.Mandator) = pMandators.
Where(Function(m) m.IsWhitelisted = True).
OrderBy(Function(m) m.Order).
@@ -207,45 +226,77 @@ Namespace Documents
Throw New Exceptions.NoMandatorException($"Mandator not found for file [{pDocument.File.Name}]")
End If
pDocument = MatchDocumentData(pDocument, oMandator)
pDocument = MatchDocumentData(pDocument, oMandator, pSchema)
pDocument.Mandator = oMandator
Return pDocument
End Function
Private Function MatchDocumentData(pDocument As Document, pMandator As Winline.Mandator) As Document
Private Function MatchDocumentData(pDocument As Document, pMandator As Winline.Mandator, pSchema As Schema) As Document
Dim oYear = Winline.GetWinLineYear()
If pMandator Is Nothing Then
Return pDocument
End If
Dim oHead As DocumentRow = pDocument.Rows.
Where(Function(r) r.Name.ToUpper.EndsWith("T025")).
SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Fakt_Kontonummer", "Fakt_Name")).
SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Lief_Kontonummer", "Lief_Name")).
FirstOrDefault()
For Each oRow As DocumentRow In pDocument.Rows
Dim oTable = pSchema.Tables.Where(Function(t) t.Name = oRow.Name).SingleOrDefault()
Dim oPositions As List(Of DocumentRow) = pDocument.Rows.
Where(Function(r) r.Name.ToUpper.EndsWith("T026")).
SetValue(Sub(oRow As DocumentRow)
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault("Artikelnummer")
If oNumberItem Is Nothing Then
Exit Sub
End If
For Each oField In oRow.Fields
If oTable Is Nothing Then
Exit For
End If
Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator)
If oArticleNumber IsNot Nothing Then
oNumberItem.External = oArticleNumber
oNumberItem.Final = oArticleNumber
Else
oNumberItem.Error = DocumentRow.FieldError.ArticleNotFound
End If
End Sub).
ToList()
Dim oColumn = oTable.Columns.Where(Function(c) c.Name = oField.Key).SingleOrDefault()
If oColumn Is Nothing Then
Continue For
End If
Dim oList As New List(Of DocumentRow) From {oHead}
pDocument.Rows = oList.Concat(oPositions).ToList()
Dim oFunctionName = oColumn.Config?.Function?.Name
If oFunctionName = "GLN" Then
SetAccountByGLN(oRow, pMandator, oField.Key, Nothing)
End If
If oFunctionName = "EAN" Then
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(oField.Key)
Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator)
If oArticleNumber IsNot Nothing Then
oNumberItem.External = oArticleNumber
oNumberItem.Final = oArticleNumber
Else
oNumberItem.Error = DocumentRow.FieldError.ArticleNotFound
End If
End If
Next
Next
'Dim oHead As DocumentRow = pDocument.Rows.
' Where(Function(r) r.Name.ToUpper.EndsWith("T025")).
' SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Fakt_Kontonummer", "Fakt_Name")).
' SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Lief_Kontonummer", "Lief_Name")).
' FirstOrDefault()
'Dim oPositions As List(Of DocumentRow) = pDocument.Rows.
' Where(Function(r) r.Name.ToUpper.EndsWith("T026")).
' SetValue(Sub(oRow As DocumentRow)
' Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault("Artikelnummer")
' If oNumberItem Is Nothing Then
' Exit Sub
' End If
' Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator)
' If oArticleNumber IsNot Nothing Then
' oNumberItem.External = oArticleNumber
' oNumberItem.Final = oArticleNumber
' Else
' oNumberItem.Error = DocumentRow.FieldError.ArticleNotFound
' End If
' End Sub).
' ToList()
'Dim oList As New List(Of DocumentRow) From {oHead}
'pDocument.Rows = oList.Concat(oPositions).ToList()
Return pDocument
End Function
@@ -272,10 +323,11 @@ Namespace Documents
oNameItem.External = oAccount.Name
oNameItem.Final = oAccount.Name
Else
oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
.External = oAccount.Name,
.Final = oAccount.Name
})
' TODO: What to to if name field is missing or not set?
'oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
' .External = oAccount.Name,
' .Final = oAccount.Name
'})
End If
Else
oNumberItem.Error = DocumentRow.FieldError.AccountNotFound