first pass of price calculation, making document loader async

This commit is contained in:
Jonathan Jenne
2022-03-04 10:49:13 +01:00
parent c11cba9cf3
commit c1cce9101b
4 changed files with 104 additions and 76 deletions

View File

@@ -29,7 +29,7 @@ Namespace Documents
TemplateConfig = pTemplateConfig
End Sub
Public Function LoadFiles(pTemplate As Template, pMandator As Mandator) As Boolean
Public Async Function LoadFiles(pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
Logger.Info("Loading files from directory [{0}]", pTemplate.InputDirectory)
Files.Clear()
@@ -41,7 +41,7 @@ Namespace Documents
For Each oFile In oFiles
Try
Dim oDocument = LoadFile(oFile, pTemplate, pMandator)
Dim oDocument = Await LoadFile(oFile, pTemplate, pMandator)
Files.Add(oDocument)
Dim oInfo As FileLoadInfo
@@ -69,16 +69,16 @@ Namespace Documents
End Try
End Function
Public Function LoadFile(pFileInfo As FileInfo, pTemplate As Template, pMandator As Mandator) As Document
Public Async Function LoadFile(pFileInfo As FileInfo, pTemplate As Template, pMandator As Mandator) As Task(Of Document)
Dim oFileList As New List(Of FileInfo) From {pFileInfo}
Logger.Info("Loading file [{0}]", pFileInfo.Name)
Try
Return oFileList.
Return Await oFileList.
Select(AddressOf WrapFileInfo).
Select(Function(d) IncludeSchema(d, pTemplate)).
Select(Function(d) LoadDocumentData(d, pTemplate, TemplateConfig)).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators, pMandator, pTemplate)).
Select(Async Function(d) Await MatchDataFromWinLine(d, Winline.Mandators, pMandator, pTemplate)).
SingleOrDefault()
Catch ex As Exception
Logger.Error(ex)
@@ -230,7 +230,7 @@ Namespace Documents
End Function
Private Function MatchDataFromWinLine(pDocument As Document, pMandators As List(Of Mandator), pMandator As Mandator, pTemplate As Template) As 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.
Where(Function(m) m.IsWhitelisted = True).
OrderBy(Function(m) m.Order).
@@ -250,7 +250,7 @@ Namespace Documents
pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator)
' These functions will only be applied if the document does not have errors
pDocument = MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate)
pDocument = Await MaybeApplyPriceFunctions(pDocument, oMandator, pTemplate)
End If
pDocument.Mandator = oMandator
@@ -263,7 +263,7 @@ 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>
Public Function MaybeApplyPriceFunctions(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document
Public Async Function MaybeApplyPriceFunctions(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Task(Of Document)
If pDocument.HasErrors Then
Return pDocument
End If
@@ -297,7 +297,7 @@ Namespace Documents
If oFunctionName = "PRICE" Then
SetPrice(oRow, oField.Key, oParamsDict, pDocument, pMandator, pTemplate)
Await SetPrice(oRow, oField.Key, oParamsDict, pDocument, pMandator, pTemplate)
End If
Next
Next
@@ -378,7 +378,7 @@ Namespace Documents
Return pDocument
End Function
Private Sub SetPrice(pRow As DocumentRow, pPriceField As String, oFieldMap As Dictionary(Of String, String), pDocument As Document, pMandator As Mandator, pTemplate As Template)
Private Async Function SetPrice(pRow As DocumentRow, pPriceField As String, oFieldMap As Dictionary(Of String, String), pDocument As Document, pMandator As Mandator, pTemplate As Template) As Task
Dim oPriceItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pPriceField)
' These fields are fetched from the current row
@@ -397,9 +397,16 @@ Namespace Documents
Dim oQuantity As Integer = 1
Dim oArticlePrice As Double = Winline.TryGetArticlePrice(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate)
Dim oArticlePrice As Double = Await Winline.TryGetArticlePrice(oArticleNumber, oAccountNumber, oQuantity, oDocumentDate, pMandator, pTemplate)
End Sub
If oArticlePrice > 0 Then
oPriceItem.External = oArticlePrice
oPriceItem.Final = oArticlePrice
Logger.Info("Price for Item [{0}] set to [{1}]", pPriceField, oArticlePrice)
Else
Logger.Warn("Price for Item [{0}] could not be found!", pPriceField)
End If
End Function
Private Sub SetArticleByEAN(pRow As DocumentRow, pMandator As Mandator, pArticleField As String)
Dim oNumberItem As DocumentRow.FieldValue = pRow.Fields.GetOrDefault(pArticleField)