Fix DocumentTypes for Exports, Prepare Price calculation, load template parameters

This commit is contained in:
Jonathan Jenne
2022-02-28 11:58:01 +01:00
parent 2e7b0bef8a
commit 213bcef732
6 changed files with 188 additions and 81 deletions

View File

@@ -86,6 +86,11 @@ Namespace Documents
End Try
End Function
Public Sub ReplaceDocument(pDocument As Document)
Dim oIndex = Files.IndexOf(pDocument)
Files.Item(oIndex) = pDocument
End Sub
Private Function IncludeSchema(pDocument As Document, pTemplate As Template) As Document
pDocument.Schema = pTemplate
@@ -243,7 +248,9 @@ Namespace Documents
Else
pDocument = ApplyDefinedItemFunctionsForImport(pDocument, oMandator, pTemplate)
pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator)
pDocument = ApplyPriceFunctions(pDocument, oMandator, pTemplate)
' These functions will only be applied if the document does not have errors
pDocument = MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate)
End If
pDocument.Mandator = oMandator
@@ -256,7 +263,11 @@ Namespace Documents
''' This needs to be strictly seperated from `ApplyDefinedItemFunctionsForImport`
''' because prices can only be calculated if GLN and EAN functions were already (successfully) processed
''' </summary>
Private Function ApplyPriceFunctions(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document
Public Function MaybeApplyPriceFunctions(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document
If pDocument.HasErrors Then
Return pDocument
End If
For Each oRow As DocumentRow In pDocument.Rows
Dim oTable = pTemplate.Tables.Where(Function(table) table.Name = oRow.TableName).SingleOrDefault()
@@ -273,16 +284,20 @@ Namespace Documents
Dim oFunctionName = oColumn.Config?.Function?.Name
' TODO: Make more nice
Dim oParams = oColumn.Config?.Function.Params
Dim oParamList = oParams.Split("|").ToList()
Dim oParams = oColumn.Config?.Function?.Params
Dim oParamsDict As New Dictionary(Of String, String)
For Each oParam In oParamList
Dim oParamSplit = oParam.Split("=")
oParamsDict.Add(oParamSplit(0), oParamSplit(1))
Next
If oParams IsNot Nothing AndAlso oParams <> String.Empty Then
Dim oParamList = oParams.Split("|").ToList()
For Each oParam In oParamList
Dim oParamSplit = oParam.Split("=")
oParamsDict.Add(oParamSplit(0), oParamSplit(1))
Next
End If
If oFunctionName = "PRICE" Then
SetPrice(oRow, oField.Key, oParamsDict, pMandator, pTemplate)
SetPrice(oRow, oField.Key, oParamsDict, pDocument, pMandator, pTemplate)
End If
Next
Next
@@ -363,17 +378,23 @@ Namespace Documents
Return pDocument
End Function
Private Sub SetPrice(pRow As DocumentRow, pPriceField As String, oFieldMap As Dictionary(Of String, String), pMandator As Mandator, pTemplate As Template)
Private Sub SetPrice(pRow As DocumentRow, pPriceField As String, oFieldMap As Dictionary(Of String, String), pDocument As Document, pMandator As Mandator, pTemplate As Template)
Dim oPriceItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pPriceField)
Dim oArticleNumberField As String = oFieldMap.GetOrDefault("Article", Nothing)
Dim oAccountNumberField As String = oFieldMap.GetOrDefault("Account", Nothing)
Dim oDocumentDateField As String = oFieldMap.GetOrDefault("DocumentDate", Nothing)
' These fields are fetched from the current row
Dim oArticleNumberField As String = oFieldMap.GetOrDefault("Article", Nothing)
Dim oArticleNumber As String = pRow.Fields.Item(oArticleNumberField).Final
' These fields a fetched from the head row, ie. the first row
Dim oAccountNumberField As String = oFieldMap.GetOrDefault("Account", Nothing)
Dim oAccountNumber = pDocument.GetFieldValue(oAccountNumberField)
Dim oDocumentDateField As String = oFieldMap.GetOrDefault("DocumentDate", Nothing)
Dim oDocumentDate = pDocument.GetFieldValue(oDocumentDateField)
' TODO: Add Field Names as Constants
' TODO: Check for missing values
Dim oArticleNumber As String = pRow.Fields.Item(oArticleNumberField).Final
Dim oAccountNumber As String = pRow.Fields.Item(oAccountNumberField).Final
Dim oDocumentDate As Date = pRow.Fields.Item(oDocumentDateField).Final
Dim oQuantity As Integer = 1
Dim oArticlePrice As Double = Winline.TryGetArticlePrice(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate)