MultiTool/MultiTool.Form/frmExportMain.vb

285 lines
12 KiB
VB.net

Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language
Imports MultiTool.Common.Templates
Imports MultiTool.Common.Exceptions
Imports MultiTool.Common.Winline
Imports MultiTool.Common.Winline.Entities
Public Class frmExportMain
Private ReadOnly LogConfig As LogConfig
Private ReadOnly ConfigManager As ConfigManager(Of Common.Config)
Private ReadOnly CurrentTemplate As Template = Nothing
Private Logger As Logger
Private FormHelper As FormHelper
Private Database As MSSQLServer
Private WebService As WebServiceData
Private Winline As WinlineData
Private GridBuilder As GridBuilder
Private FileEx As DigitalData.Modules.Filesystem.File
Public Sub New(pLogConfig As LogConfig, pConfigManager As ConfigManager(Of Common.Config), pTemplate As Template)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
LogConfig = pLogConfig
ConfigManager = pConfigManager
CurrentTemplate = pTemplate
End Sub
Private Sub frmExportMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Text = String.Format(My.Resources.frmShared._0____WebService_Multitool_für_WinLine, CurrentTemplate.Name)
Logger = LogConfig.GetLogger()
FormHelper = New FormHelper(LogConfig, Me)
' Initialize Database
Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString)
Database = New MSSQLServer(LogConfig, oConnectionString)
GridBuilder = New GridBuilder(GridViewDocuments, lookupMandator.Properties.View, lookupAccount.Properties.View).
WithDefaults().
WithReadOnlyOptions().
WithClipboardHandler()
Winline = My.Winline
FileEx = New DigitalData.Modules.Filesystem.File(LogConfig)
WebService = New WebServiceData(LogConfig, Database, Winline, My.GeneralConfiguration.Webservice, My.GeneralConfiguration)
AddHandler WebService.WebServiceProgress, AddressOf WebService_Progress
Catch ex As Exception
FormHelper.ShowError(ex, My.Resources.frmImportMainExtra.Initialisieren_der_Anwendungs_Daten)
End Try
End Sub
Private Sub frmExportMain_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Try
lookupMandator.Properties.DataSource = Winline.Mandators
lookupMandator.ForceInitialize()
lookupMandator.Properties.View.BestFitColumns()
lookupAccount.Properties.DataSource = Winline.Accounts
lookupAccount.ForceInitialize()
lookupAccount.Properties.View.BestFitColumns()
comboDocumentKind.Properties.DataSource = Winline.DocumentKinds
comboDocumentKind.Properties.EditValueType = Repository.EditValueTypeCollection.List
comboDocumentKind.EditValue = New List(Of Object)
comboYear.Properties.Items.AddRange(Winline.Years)
comboDocumentType.Properties.DisplayMember = "Key"
comboDocumentType.Properties.ValueMember = "Value"
comboDocumentType.Properties.DataSource = [Enum].
GetValues(GetType(WinlineData.DocumentType)).
Cast(Of WinlineData.DocumentType)().
Select(AddressOf MapDocumentTypeFromEnum).
ToList()
comboDocumentType.EditValue = DirectCast(CurrentTemplate.DocType, Integer)
If ConfigManager.Config.LastUsedMandator <> "" Then
lookupMandator.EditValue = Winline.Mandators.
Where(Function(mandator) mandator.Id = ConfigManager.Config.LastUsedMandator).
FirstOrDefault()
End If
Catch ex As Exception
FormHelper.ShowError(ex, "Initialisierung der Form")
End Try
End Sub
Private Function MapDocumentTypeToEnum(Of T)(pValue As Integer) As T
Return [Enum].Parse(GetType(T), pValue)
End Function
Private Function MapDocumentTypeFromEnum(pValue As WinlineData.DocumentType)
Select Case pValue
Case WinlineData.DocumentType.Undefined
Return New KeyValuePair(Of String, Integer)("Alle", pValue)
Case WinlineData.DocumentType.IncomingOffer
Return New KeyValuePair(Of String, Integer)("Angebot EK", pValue)
Case WinlineData.DocumentType.IncomingOrder
Return New KeyValuePair(Of String, Integer)("Auftrag EK", pValue)
Case WinlineData.DocumentType.IncomingDeliveryNote
Return New KeyValuePair(Of String, Integer)("Lieferschein EK", pValue)
Case WinlineData.DocumentType.IncomingInvoice
Return New KeyValuePair(Of String, Integer)("Rechnung EK", pValue)
Case WinlineData.DocumentType.OutgoingOffer
Return New KeyValuePair(Of String, Integer)("Angebot VK", pValue)
Case WinlineData.DocumentType.OutgoingOrder
Return New KeyValuePair(Of String, Integer)("Auftrag VK", pValue)
Case WinlineData.DocumentType.OutgoingDeliveryNote
Return New KeyValuePair(Of String, Integer)("Lieferschein VK", pValue)
Case WinlineData.DocumentType.OutgoingInvoice
Return New KeyValuePair(Of String, Integer)("Rechnung VK", pValue)
Case Else
Return Nothing
End Select
End Function
Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
SearchDocuments()
End Sub
Private Sub frmExportMain_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F5 Then
SearchDocuments()
End If
End Sub
Private Sub WebService_Progress(sender As Object, e As String)
SplashScreenManager.SetWaitFormDescription(e)
End Sub
Private Sub lookupMandator_EditValueChanged(sender As Object, e As EventArgs) Handles lookupMandator.EditValueChanged
Dim oMandator As Mandator = lookupMandator.EditValue
lookupAccount.Properties.DataSource = Winline.Accounts.Where(Function(acc) acc.Mandator.Equals(oMandator))
comboDocumentKind.Properties.DataSource = Winline.DocumentKinds.Where(Function(kind) kind.Mandator.Equals(oMandator))
ConfigManager.Config.LastUsedMandator = oMandator.Id
ConfigManager.Save()
End Sub
Private Sub lookup_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lookupAccount.Properties.ButtonClick
Dim oLookup As GridLookUpEdit = sender
If e.Button.Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Clear Then
oLookup.EditValue = Nothing
End If
End Sub
Private Sub combo_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles comboDocumentKind.Properties.ButtonClick
Dim oLookup As CheckedComboBoxEdit = sender
If e.Button.Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Clear Then
oLookup.EditValue = New List(Of Object)
End If
End Sub
Private Sub SearchDocuments()
Try
dateDocDateFrom.DoValidate()
dateDocDateTo.DoValidate()
Dim oMandator = lookupMandator.EditValue
Dim oYear = Utils.NotNull(comboYear.EditValue, 0)
Dim oAccount = lookupAccount.EditValue
Dim oKindsAsObjects As List(Of Object) = comboDocumentKind.EditValue
Dim oKinds As List(Of DocumentKind) = oKindsAsObjects.Cast(Of DocumentKind).ToList()
Dim oDateFrom = dateDocDateFrom.EditValue
Dim oDateTo = dateDocDateTo.EditValue
Dim oShowExported = chkShowExported.Checked
Dim oDocNumberFrom = Utils.NotNull(txtDocumentFrom.EditValue, String.Empty)
Dim oDocNumberTo = Utils.NotNull(txtDocumentTo.EditValue, String.Empty)
Dim oDocType As WinlineData.DocumentType = MapDocumentTypeToEnum(Of WinlineData.DocumentType)(comboDocumentType.EditValue)
If oMandator Is Nothing Then
FormHelper.ShowWarning("Bitte einen Mandanten auswählen!")
Exit Sub
End If
'TODO: Make Document Type configurable
Dim oDocuments = Winline.GetDocuments(oMandator, CurrentTemplate, oDocType, New WinlineData.GetDocumentArgs With {
.Account = oAccount,
.Kinds = oKinds,
.DateFrom = oDateFrom,
.DateTo = oDateTo,
.ShowExported = oShowExported,
.DocNumberFrom = oDocNumberFrom,
.DocNumberTo = oDocNumberTo,
.Year = oYear
})
If oDocuments Is Nothing Then
FormHelper.ShowWarning("There was an error in the Query.")
Exit Sub
End If
GridControlDocuments.DataSource = oDocuments
txtResults.Caption = String.Format("{0} Ergebnisse", oDocuments.Count)
Catch ex As Exception
FormHelper.ShowError(ex, "Laden der Daten")
End Try
End Sub
Private Async Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
Try
SplashScreenManager.ShowWaitForm()
Dim oExportResult = False
Dim oRowHandles = GridViewDocuments.GetSelectedRows()
Dim oExportCount = 0
For Each oRowHandle In oRowHandles
oExportCount += 1
Dim oDocument As ExportDocument = GridViewDocuments.GetRow(oRowHandle)
Dim oMandator As Mandator = lookupMandator.EditValue
Dim oMessage = String.Format("Beleg {0} exportieren.. ({1}/{2})", oDocument.Number, oExportCount, oRowHandles.Length)
SplashScreenManager.SetWaitFormDescription(oMessage)
Await WebService.ExportDocumentFromWinline(oDocument, CurrentTemplate, oMandator)
Dim oFinalSqlResult = Await Winline.ExecuteFinalSQLForExport(oDocument, CurrentTemplate, oMandator)
If oFinalSqlResult = False Then
Throw New DatabaseException("FinalSQL was not executed successfully!")
End If
Next
MsgBox($"{oExportCount} Dateien wurden erfolgreich exportiert!", MsgBoxStyle.Information, Text)
Catch ex As MissingAttributeException
FormHelper.ShowError(ex, "Exportieren der Daten")
Catch ex As Exception
FormHelper.ShowError(ex, "Exportieren der Daten")
Finally
SplashScreenManager.CloseWaitForm()
SearchDocuments()
End Try
End Sub
Private Sub btnOpenInputDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenInputDirectory.ItemClick
FormHelper.TryOpenDirectory(CurrentTemplate.InputDirectory, My.Resources.frmImportMainExtra.Eingangsverzeichnis)
End Sub
Private Sub btnOpenOutputDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenOutputDirectory.ItemClick
FormHelper.TryOpenDirectory(CurrentTemplate.OutputDirectory, My.Resources.frmImportMainExtra.Ausgabeverzeichnis)
End Sub
Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
FormHelper.TryOpenDirectory(My.GeneralConfiguration.TemplateDirectory, My.Resources.frmImportMainExtra.Vorlagenverzeichnis)
End Sub
Private Sub GridViewDocuments_CustomDrawCell(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles GridViewDocuments.CustomDrawCell
If e.Column.ColumnType = GetType(Date) AndAlso e.CellValue = Date.MinValue Then
e.DisplayText = ""
End If
End Sub
Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
FormHelper.TryOpenDirectory(LogConfig.LogDirectory, My.Resources.frmImportMainExtra.Logverzeichnis)
End Sub
End Class