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

@ -57,10 +57,10 @@ Partial Class frmImportMain
Me.btnEditRow = New DevExpress.XtraBars.BarButtonItem()
Me.btnCalculatePrices = New DevExpress.XtraBars.BarButtonItem()
Me.RibbonPage1 = New DevExpress.XtraBars.Ribbon.RibbonPage()
Me.RibbonPageGroup1 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroup4 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroup3 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroup7 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroupLoad = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroupReport = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroupTransfer = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroupEdit = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPage2 = New DevExpress.XtraBars.Ribbon.RibbonPage()
Me.RibbonPageGroup5 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroup2 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
@ -383,39 +383,39 @@ Partial Class frmImportMain
'
'RibbonPage1
'
Me.RibbonPage1.Groups.AddRange(New DevExpress.XtraBars.Ribbon.RibbonPageGroup() {Me.RibbonPageGroup1, Me.RibbonPageGroup4, Me.RibbonPageGroup3, Me.RibbonPageGroup7})
Me.RibbonPage1.Groups.AddRange(New DevExpress.XtraBars.Ribbon.RibbonPageGroup() {Me.RibbonPageGroupLoad, Me.RibbonPageGroupReport, Me.RibbonPageGroupTransfer, Me.RibbonPageGroupEdit})
Me.RibbonPage1.Name = "RibbonPage1"
resources.ApplyResources(Me.RibbonPage1, "RibbonPage1")
'
'RibbonPageGroup1
'RibbonPageGroupLoad
'
Me.RibbonPageGroup1.ItemLinks.Add(Me.btnLoadFiles)
Me.RibbonPageGroup1.ItemLinks.Add(Me.btnReloadFile)
Me.RibbonPageGroup1.Name = "RibbonPageGroup1"
resources.ApplyResources(Me.RibbonPageGroup1, "RibbonPageGroup1")
Me.RibbonPageGroupLoad.ItemLinks.Add(Me.btnLoadFiles)
Me.RibbonPageGroupLoad.ItemLinks.Add(Me.btnReloadFile)
Me.RibbonPageGroupLoad.Name = "RibbonPageGroupLoad"
resources.ApplyResources(Me.RibbonPageGroupLoad, "RibbonPageGroupLoad")
'
'RibbonPageGroup4
'RibbonPageGroupReport
'
Me.RibbonPageGroup4.ItemLinks.Add(Me.btnOpenReport)
Me.RibbonPageGroup4.Name = "RibbonPageGroup4"
resources.ApplyResources(Me.RibbonPageGroup4, "RibbonPageGroup4")
Me.RibbonPageGroupReport.ItemLinks.Add(Me.btnOpenReport)
Me.RibbonPageGroupReport.Name = "RibbonPageGroupReport"
resources.ApplyResources(Me.RibbonPageGroupReport, "RibbonPageGroupReport")
'
'RibbonPageGroup3
'RibbonPageGroupTransfer
'
Me.RibbonPageGroup3.Alignment = DevExpress.XtraBars.Ribbon.RibbonPageGroupAlignment.Far
Me.RibbonPageGroup3.ItemLinks.Add(Me.btnTransferFile)
Me.RibbonPageGroup3.ItemLinks.Add(Me.btnTransferAllFiles)
Me.RibbonPageGroup3.ItemLinks.Add(Me.btnTestTransferFile)
Me.RibbonPageGroup3.Name = "RibbonPageGroup3"
resources.ApplyResources(Me.RibbonPageGroup3, "RibbonPageGroup3")
Me.RibbonPageGroupTransfer.Alignment = DevExpress.XtraBars.Ribbon.RibbonPageGroupAlignment.Far
Me.RibbonPageGroupTransfer.ItemLinks.Add(Me.btnTransferFile)
Me.RibbonPageGroupTransfer.ItemLinks.Add(Me.btnTransferAllFiles)
Me.RibbonPageGroupTransfer.ItemLinks.Add(Me.btnTestTransferFile)
Me.RibbonPageGroupTransfer.Name = "RibbonPageGroupTransfer"
resources.ApplyResources(Me.RibbonPageGroupTransfer, "RibbonPageGroupTransfer")
'
'RibbonPageGroup7
'RibbonPageGroupEdit
'
Me.RibbonPageGroup7.ItemLinks.Add(Me.btnEditRow)
Me.RibbonPageGroup7.ItemLinks.Add(Me.btnRemoveRow)
Me.RibbonPageGroup7.ItemLinks.Add(Me.btnCalculatePrices)
Me.RibbonPageGroup7.Name = "RibbonPageGroup7"
resources.ApplyResources(Me.RibbonPageGroup7, "RibbonPageGroup7")
Me.RibbonPageGroupEdit.ItemLinks.Add(Me.btnEditRow)
Me.RibbonPageGroupEdit.ItemLinks.Add(Me.btnRemoveRow)
Me.RibbonPageGroupEdit.ItemLinks.Add(Me.btnCalculatePrices)
Me.RibbonPageGroupEdit.Name = "RibbonPageGroupEdit"
resources.ApplyResources(Me.RibbonPageGroupEdit, "RibbonPageGroupEdit")
'
'RibbonPage2
'
@ -673,7 +673,7 @@ Partial Class frmImportMain
Friend WithEvents RibbonControl As DevExpress.XtraBars.Ribbon.RibbonControl
Friend WithEvents RibbonPage1 As DevExpress.XtraBars.Ribbon.RibbonPage
Friend WithEvents RibbonPageGroup1 As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents RibbonPageGroupLoad As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents RibbonStatusBar As DevExpress.XtraBars.Ribbon.RibbonStatusBar
Friend WithEvents GridControlFiles As DevExpress.XtraGrid.GridControl
Friend WithEvents txtFilesLoaded As DevExpress.XtraBars.BarStaticItem
@ -688,9 +688,9 @@ Partial Class frmImportMain
Friend WithEvents SplitContainerMain As DevExpress.XtraEditors.SplitContainerControl
Friend WithEvents btnReloadFile As DevExpress.XtraBars.BarButtonItem
Friend WithEvents btnTransferAllFiles As DevExpress.XtraBars.BarButtonItem
Friend WithEvents RibbonPageGroup3 As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents RibbonPageGroupTransfer As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents btnOpenReport As DevExpress.XtraBars.BarButtonItem
Friend WithEvents RibbonPageGroup4 As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents RibbonPageGroupReport As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents btnShowXml As DevExpress.XtraBars.BarButtonItem
Friend WithEvents btnOpenLogDirectory As DevExpress.XtraBars.BarButtonItem
Friend WithEvents btnOpenConfigDirectory As DevExpress.XtraBars.BarButtonItem
@ -714,7 +714,7 @@ Partial Class frmImportMain
Friend WithEvents RepositoryItemProgressBar1 As Repository.RepositoryItemProgressBar
Friend WithEvents SplashScreenManager As DevExpress.XtraSplashScreen.SplashScreenManager
Friend WithEvents btnRemoveRow As DevExpress.XtraBars.BarButtonItem
Friend WithEvents RibbonPageGroup7 As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents RibbonPageGroupEdit As DevExpress.XtraBars.Ribbon.RibbonPageGroup
Friend WithEvents GridControl1 As DevExpress.XtraGrid.GridControl
Friend WithEvents GridView1 As GridView
Friend WithEvents GridControl2 As DevExpress.XtraGrid.GridControl

View File

@ -253,16 +253,16 @@
<data name="RibbonControl.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 0</value>
</data>
<data name="RibbonPageGroup1.Text" xml:space="preserve">
<data name="RibbonPageGroupLoad.Text" xml:space="preserve">
<value>Einlesen</value>
</data>
<data name="RibbonPageGroup4.Text" xml:space="preserve">
<data name="RibbonPageGroupReport.Text" xml:space="preserve">
<value>Auswertung</value>
</data>
<data name="RibbonPageGroup3.Text" xml:space="preserve">
<data name="RibbonPageGroupTransfer.Text" xml:space="preserve">
<value>Übermittlung</value>
</data>
<data name="RibbonPageGroup7.Text" xml:space="preserve">
<data name="RibbonPageGroupEdit.Text" xml:space="preserve">
<value>Bearbeitung</value>
</data>
<data name="RibbonPage1.Text" xml:space="preserve">
@ -947,28 +947,28 @@
<data name="&gt;&gt;RibbonPage1.Type" xml:space="preserve">
<value>DevExpress.XtraBars.Ribbon.RibbonPage, DevExpress.XtraBars.v21.2, Version=21.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a</value>
</data>
<data name="&gt;&gt;RibbonPageGroup1.Name" xml:space="preserve">
<value>RibbonPageGroup1</value>
<data name="&gt;&gt;RibbonPageGroupLoad.Name" xml:space="preserve">
<value>RibbonPageGroupLoad</value>
</data>
<data name="&gt;&gt;RibbonPageGroup1.Type" xml:space="preserve">
<data name="&gt;&gt;RibbonPageGroupLoad.Type" xml:space="preserve">
<value>DevExpress.XtraBars.Ribbon.RibbonPageGroup, DevExpress.XtraBars.v21.2, Version=21.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a</value>
</data>
<data name="&gt;&gt;RibbonPageGroup4.Name" xml:space="preserve">
<value>RibbonPageGroup4</value>
<data name="&gt;&gt;RibbonPageGroupReport.Name" xml:space="preserve">
<value>RibbonPageGroupReport</value>
</data>
<data name="&gt;&gt;RibbonPageGroup4.Type" xml:space="preserve">
<data name="&gt;&gt;RibbonPageGroupReport.Type" xml:space="preserve">
<value>DevExpress.XtraBars.Ribbon.RibbonPageGroup, DevExpress.XtraBars.v21.2, Version=21.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a</value>
</data>
<data name="&gt;&gt;RibbonPageGroup3.Name" xml:space="preserve">
<value>RibbonPageGroup3</value>
<data name="&gt;&gt;RibbonPageGroupTransfer.Name" xml:space="preserve">
<value>RibbonPageGroupTransfer</value>
</data>
<data name="&gt;&gt;RibbonPageGroup3.Type" xml:space="preserve">
<data name="&gt;&gt;RibbonPageGroupTransfer.Type" xml:space="preserve">
<value>DevExpress.XtraBars.Ribbon.RibbonPageGroup, DevExpress.XtraBars.v21.2, Version=21.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a</value>
</data>
<data name="&gt;&gt;RibbonPageGroup7.Name" xml:space="preserve">
<value>RibbonPageGroup7</value>
<data name="&gt;&gt;RibbonPageGroupEdit.Name" xml:space="preserve">
<value>RibbonPageGroupEdit</value>
</data>
<data name="&gt;&gt;RibbonPageGroup7.Type" xml:space="preserve">
<data name="&gt;&gt;RibbonPageGroupEdit.Type" xml:space="preserve">
<value>DevExpress.XtraBars.Ribbon.RibbonPageGroup, DevExpress.XtraBars.v21.2, Version=21.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a</value>
</data>
<data name="&gt;&gt;RibbonPage2.Name" xml:space="preserve">

View File

@ -183,17 +183,17 @@ Public Class frmImportMain
End Function
Private Sub btnLoadFiles_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLoadFiles.ItemClick
LoadFiles()
End Sub
Private Async Function btnLoadFiles_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) As Task Handles btnLoadFiles.ItemClick
Await LoadFiles()
End Function
Private Sub frmImportMain_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Private Async Function frmImportMain_KeyDown(sender As Object, e As KeyEventArgs) As Task Handles MyBase.KeyDown
If e.KeyCode = Keys.F5 Then
LoadFiles()
Await LoadFiles()
End If
End Sub
End Function
Private Sub btnReloadFile_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadFile.ItemClick
Private Async Sub btnReloadFile_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadFile.ItemClick
Dim oCurrentMandator As Mandator = TryCast(lookupMandator.EditValue, Mandator)
If oCurrentMandator Is Nothing Then
MsgBox(My.Resources.frmImportMainExtra.Bitte_wählen_Sie_einen_Mandanten_aus__bevor_Sie_fortfahren, MsgBoxStyle.Exclamation, Text)
@ -206,7 +206,7 @@ Public Class frmImportMain
Try
If oResult = DialogResult.Yes Then
Dim oDocument As Document = GridViewFiles.GetRow(GridViewFiles.FocusedRowHandle)
Dim oNewDocument = DocumentLoader.LoadFile(oDocument.File, CurrentTemplate, lookupMandator.EditValue)
Dim oNewDocument = Await DocumentLoader.LoadFile(oDocument.File, CurrentTemplate, lookupMandator.EditValue)
Dim oIndex = DocumentLoader.Files.IndexOf(oDocument)
DocumentLoader.Files.Item(oIndex) = oNewDocument
@ -598,7 +598,7 @@ Public Class frmImportMain
GridViewFiles.RefreshData()
End Sub
Private Sub LoadFiles()
Private Async Function LoadFiles() As Task
Try
BeginLoadingUI()
@ -607,7 +607,7 @@ Public Class frmImportMain
SplashScreenManager.SetWaitFormDescription(oMessage)
End Sub
If DocumentLoader.LoadFiles(CurrentTemplate, lookupMandator.EditValue) Then
If Await DocumentLoader.LoadFiles(CurrentTemplate, lookupMandator.EditValue) Then
GridControlFiles.DataSource = Nothing
GridControlFiles.DataSource = DocumentLoader.Files
@ -622,7 +622,7 @@ Public Class frmImportMain
Finally
EndLoadingUI()
End Try
End Sub
End Function
@ -733,13 +733,21 @@ Public Class frmImportMain
SplashScreenManager.ShowWaitForm()
SetDocumentButtonsEnabled(False)
GridControlFiles.Enabled = False
btnLoadFiles.Enabled = False
SplitContainerGrids.Enabled = False
RibbonPageGroupLoad.Enabled = False
RibbonPageGroupEdit.Enabled = False
RibbonPageGroupReport.Enabled = False
RibbonPageGroupTransfer.Enabled = False
End Sub
Private Sub EndLoadingUI()
RibbonPageGroupLoad.Enabled = True
RibbonPageGroupEdit.Enabled = True
RibbonPageGroupReport.Enabled = True
RibbonPageGroupTransfer.Enabled = True
SplitContainerGrids.Enabled = True
btnLoadFiles.Enabled = True
GridControlFiles.Enabled = True
SetDocumentButtonsEnabled(True)
SplashScreenManager.CloseWaitForm()
@ -755,17 +763,30 @@ Public Class frmImportMain
btnEditRow.Enabled = pEnabled
End Sub
Private Sub btnCalculatePrices_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCalculatePrices.ItemClick
Private Async Sub btnCalculatePrices_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCalculatePrices.ItemClick
Dim oCurrentMandator As Mandator = TryCast(lookupMandator.EditValue, Mandator)
If oCurrentMandator Is Nothing Then
MsgBox(My.Resources.frmImportMainExtra.Bitte_wählen_Sie_einen_Mandanten_aus__bevor_Sie_fortfahren, MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
Dim oNewDocument = DocumentLoader.MaybeApplyPriceFunctions(CurrentDocument, oCurrentMandator, CurrentTemplate)
BeginLoadingUI()
SplashScreenManager.SetWaitFormDescription("Lade Preisinformationen..")
DocumentLoader.ReplaceDocument(oNewDocument)
LoadDocument(oNewDocument)
Dim oStopWatch As New Stopwatch()
Try
oStopWatch.Start()
Dim oNewDocument = Await DocumentLoader.MaybeApplyPriceFunctions(CurrentDocument, oCurrentMandator, CurrentTemplate)
oStopWatch.Stop()
DocumentLoader.ReplaceDocument(oNewDocument)
LoadDocument(oNewDocument)
Catch ex As Exception
FormHelper.ShowError(ex, "Laden der Preisinformationen")
Finally
EndLoadingUI()
Logger.Debug("Loading Priceinfo took [{0}] ms", oStopWatch.ElapsedMilliseconds)
End Try
End Sub
#End Region

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)