Imports System.IO Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Columns Imports DevExpress.XtraRichEdit Imports DevExpress.XtraEditors Imports DevExpress.XtraReports.UI Imports DevExpress.DataAccess.ObjectBinding Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Database Imports DigitalData.Modules.Config Imports DigitalData.Controls.SQLConfig Imports DigitalData.GUIs.Common Imports ImporterShared.Documents Imports ImporterShared.Winline Imports ImporterShared.Schemas Imports ImporterForm.Positions Imports DevExpress.XtraLayout Public Class frmMain Public LogConfig As LogConfig Public Logger As Logger Public ConfigManager As ConfigManager(Of ImporterShared.Config) Public Database As MSSQLServer Public DocumentLoader As DocumentLoader Public GridBuilder As GridBuilder Public PositionData As PositionData Public Winline As Data Public WebService As WebService Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try txtVersion.Caption = String.Format(txtVersion.Caption, Application.ProductVersion) LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "EDI Document Importer") Logger = LogConfig.GetLogger() Logger.Info("EDI Document Importer, Version [{0}]", Application.ProductVersion) ConfigManager = New ConfigManager(Of ImporterShared.Config)(LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath, Application.StartupPath) ' If ConnectionString does not exist, show SQL Config Form If ConfigManager.Config.ConnectionString = String.Empty Then Dim oForm As New frmSQLConfig(LogConfig) With { .FormTitle = "EDI Document Importer" } Dim oResult = oForm.ShowDialog() If oResult = DialogResult.OK Then ConfigManager.Config.ConnectionString = oForm.ConnectionString ConfigManager.Save() End If End If ' Initialize Database Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString) Database = New MSSQLServer(LogConfig, oConnectionString) Winline = New Data(LogConfig, Database, ConfigManager.Config) WebService = New WebService(LogConfig, ConfigManager.Config) PositionData = New PositionData(LogConfig, Winline) ' Load WinLine Data Winline.Mandators.Clear() Winline.LoadMandators() Winline.LoadEconomicYears() Winline.LoadDocumentKinds(Winline.Mandators) For Each oMandator In Winline.Mandators Winline.LoadAccounts(oMandator) Next ' Load data for UI Fields cmbMandator.Properties.DataSource = Winline.Mandators cmbYear.Properties.Items.AddRange(Winline.Years) cmbYear.SelectedItem = ConfigManager.Config.GetYear() ' Initialize Grids GridBuilder = New GridBuilder(New List(Of GridView) From {GridViewFiles, GridViewPositions}) GridBuilder.WithDefaults() PositionGrid.LoadPositionViewAndColumns(GridViewPositions, DocumentType.Order) ' Construct classes related to the xml data DocumentLoader = New DocumentLoader(LogConfig, Winline) Catch ex As Exception Logger.Error(ex) Dim oMessage = GetErrorMessage(ex, "Fehler beim Laden der Anwendung") MsgBox(oMessage, MsgBoxStyle.Critical, Text) End Try End Sub #Region "Helpers" Private Function GetValidationMessage(FieldName As String) As String Return $"Das Feld '{FieldName}' muss ausgefüllt werden!" End Function Private Function GetErrorMessage(pException As Exception, pErrorText As String) Return $"{pErrorText}:{vbNewLine}{vbNewLine}{pException.Message}" End Function Private Function GetFocusedDocument() As Document Dim oRowHandles = GridViewFiles.GetSelectedRows().ToList() Dim oDocument As Document = GridViewFiles.GetRow(oRowHandles.First()) Return oDocument End Function Private Function GetFocusedPosition() As Position Dim oRowHandles = GridViewPositions.GetSelectedRows().ToList() Dim oPosition As Position = GridViewPositions.GetRow(oRowHandles.First()) Return oPosition End Function Private Sub TryOpenDirectory(pPath As String, pDisplayName As String) If Directory.Exists(pPath) Then Process.Start(pPath) Else MsgBox($"{pDisplayName} nicht konfiguriert oder nicht gefunden!", MsgBoxStyle.Exclamation, Text) End If End Sub #End Region Private Sub btnLoadDocuments_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLoadDocuments.ItemClick Try If DocumentLoader.LoadFiles(ConfigManager.Config.InputDirectory) = True Then RibbonGroupDataTransmission.Enabled = True RibbonGroupDocument.Enabled = True RibbonGroupPositions.Enabled = True LayoutControlHead.Enabled = True GridControlPositions.Enabled = True btnReloadDocument.Enabled = True GridControlFiles.DataSource = DocumentLoader.Files txtFilesLoaded.Caption = $"{DocumentLoader.Files.Count} Dokumente geladen" End If Catch ex As Exception Dim oMessage = ex.Message If ex.InnerException IsNot Nothing Then oMessage &= vbNewLine & vbNewLine & ex.InnerException.Message End If MsgBox(oMessage, MsgBoxStyle.Critical, Text) End Try End Sub Private Sub GridViewFiles_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewFiles.FocusedRowChanged Dim oDocument As Document = GridViewFiles.GetRow(e.FocusedRowHandle) If oDocument Is Nothing Then Exit Sub End If ShowDocument(oDocument) End Sub Private Sub ShowDocument(pDocument As Document) ' Load XML File in the sidebar RichEditXml.LoadDocument(pDocument.FullName, DocumentFormat.PlainText) If pDocument.Mandator Is Nothing Then Dim oForm As New frmMandatorSelection() With { .Mandators = Winline.Mandators, .SelectedMandator = Nothing } Dim oResult = oForm.ShowDialog() If oResult <> DialogResult.OK Then Exit Sub End If pDocument.Mandator = oForm.SelectedMandator.Id End If Try Select Case pDocument.Type Case DocumentType.Order ShowOrderDocument(pDocument) End Select Catch ex As Xml.XmlException Dim oMessage = GetErrorMessage(ex, "Fehler beim Laden des Dokuments") MsgBox(oMessage, MsgBoxStyle.Critical, Text) Logger.Error(ex) Catch ex As Exceptions.NoMandatorException Dim oMessage = GetErrorMessage(ex, "Fehler beim Laden des Dokuments") MsgBox(oMessage, MsgBoxStyle.Critical, Text) Logger.Error(ex) Catch ex As Exception Dim oMessage = GetErrorMessage(ex, "Fehler beim Laden des Dokuments") MsgBox(oMessage, MsgBoxStyle.Critical, Text) Logger.Error(ex) End Try End Sub Private Sub ShowOrderDocument(pDocument As Document) If pDocument.Mandator Is Nothing Then Throw New Exceptions.NoMandatorException("Es konnte kein Mandant gefunden werden") End If Dim oData As Orders.Input.MESOWebService = DirectCast(pDocument.Data, Orders.Input.MESOWebService) Dim oDataOriginal As Orders.Input.MESOWebService = DirectCast(pDocument.DataOriginal, Orders.Input.MESOWebService) Dim oHead = Orders.Helpers.GetOrderHead(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT025)(oData) Dim oHeadOriginal = Orders.Helpers.GetOrderHead(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT025)(oDataOriginal) ' --- Head Data -------------------------------------------------------------------------------------- ' Original, Unreplaced Data txtCustomerGLN.Text = oHeadOriginal.Fakt_Kontonummer txtDeliveryAddressGLN.Text = oHeadOriginal.Lief_Kontonummer ' Regular Data from EDI txtRunningNumber.Text = oHead.Laufnummer cmbOrderIssuer.Text = oHead.Fakt_Ansprechpartner txtOrderNumber.Text = oHead.Auftragsreferenz txtInfoText.Text = oHead.Infotext dateOrderDate.EditValue = oHead.Datum_AuftragBestellung ' --- Find Mandator -------------------------------------------------------------------------------------- Dim oMandator = Winline.Mandators. Where(Function(m) m.Id = pDocument.Mandator). SingleOrDefault() cmbMandator.EditValue = oMandator ' --- Find DocumentKinds --------------------------------------------------------------------------------- Dim oMandatorKinds = Winline.DocumentKinds. Where(Function(k) k.Mandator = oMandator.Id). ToList() Dim oSelectedKind = oMandatorKinds. Where(Function(k) k.Id.ToString = oHead.Belegart). SingleOrDefault() cmbDocumentKind.Properties.Items.Clear() cmbDocumentKind.Properties.Items.AddRange(oMandatorKinds) If oSelectedKind Is Nothing Then cmbDocumentKind.ErrorText = "Es wurde keine passende Belegart gefunden." cmbDocumentKind.SelectedItem = Nothing Else cmbDocumentKind.SelectedItem = oSelectedKind End If ' --- Find Accounts -------------------------------------------------------------------------------------- Dim oMandatorAccounts = Winline.Accounts. Where(Function(a) a.Mandator = oMandator.Id). ToList() cmbCustomer.Properties.DataSource = oMandatorAccounts cmbDeliveryAddress.Properties.DataSource = oMandatorAccounts ' FAKTURA If oHead.Fakt_Kontonummer = oHeadOriginal.Fakt_Kontonummer Then cmbCustomer.ErrorText = GetValidationMessage("Faktura Konto") cmbCustomer.EditValue = Nothing Else Dim oMatchingAccounts = oMandatorAccounts. Where(Function(oAccount) oAccount.Id = oHead.Fakt_Kontonummer) If oMatchingAccounts.Count() > 1 Then cmbCustomer.EditValue = Nothing cmbCustomer.ErrorText = "Für die Kontonummer wurden mehrere Konten gefunden." ElseIf oMatchingAccounts.Count() = 0 Then cmbCustomer.EditValue = Nothing cmbCustomer.ErrorText = "Für die Kontonummer wurde kein Konto gefunden." Else cmbCustomer.EditValue = oMatchingAccounts.SingleOrDefault() End If End If ' CONTACTS If cmbCustomer.EditValue IsNot Nothing Then Dim oContacts = Winline.GetContacts(oHead.Fakt_Kontonummer, oMandator) cmbOrderIssuer.Properties.DataSource = oContacts End If ' DEVLIERY If oHead.Lief_Kontonummer = oHeadOriginal.Lief_Kontonummer Then If oHeadOriginal.Lief_Kontonummer = String.Empty Then txtPlace.EditValue = oHeadOriginal.Lief_Ort txtZIP.EditValue = oHeadOriginal.Lief_PLZ txtStreetName.EditValue = oHeadOriginal.Lief_Strasse txtName.EditValue = oHeadOriginal.Lief_Name Else cmbDeliveryAddress.ErrorText = GetValidationMessage("Lieferanten Konto") cmbDeliveryAddress.EditValue = Nothing End If Else Dim oMatchingAccounts = oMandatorAccounts. Where(Function(oAccount) oAccount.Id = oHead.Lief_Kontonummer) If oMatchingAccounts.Count() > 1 Then cmbDeliveryAddress.EditValue = Nothing cmbDeliveryAddress.ErrorText = "Für die Kontonummer wurden mehrere Konten gefunden." ElseIf oMatchingAccounts.Count() = 0 Then cmbDeliveryAddress.EditValue = Nothing cmbDeliveryAddress.ErrorText = "Für die Kontonummer wurde kein Konto gefunden." Else cmbDeliveryAddress.EditValue = oMatchingAccounts.SingleOrDefault() End If End If ' --- Find Positions ------------------------------------------------------------------------------------ Dim oPositionList = PositionData.Load(oMandator, oData, oDataOriginal) GridControlPositions.DataSource = oPositionList GridViewPositions.BestFitColumns() End Sub Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick TryOpenDirectory(ConfigManager.Config.InputDirectory, "Eingangsverzeichnis") End Sub Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick TryOpenDirectory(ConfigManager.Config.OutputDirectory, "Ausgangsverzeichnis") End Sub Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick Dim oUserConfigDirectory = New FileInfo(ConfigManager.UserConfigPath).Directory TryOpenDirectory(oUserConfigDirectory.FullName, "Konfigurationsverzeichnis") End Sub Private Sub checkShowXml_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles checkShowXml.CheckedChanged SplitContainerControl3.Collapsed = Not checkShowXml.Checked End Sub Private Async Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick Dim oMessageText As String = "" Dim oMessageStyle As MsgBoxStyle = MsgBoxStyle.Critical Dim oDocument As Document = GetFocusedDocument() If oDocument Is Nothing Then Exit Sub End If SplashScreenManager1.ShowWaitForm() ChangeUIState(False) Try Dim oFinalDocument As Document = TransferChangesToDocument(oDocument) Dim oResult = Await WebService.TransferDocumentToWinLine(oFinalDocument) oMessageText = "Beleg wurde erfolgreich übertragen!" oMessageStyle = MsgBoxStyle.Information Catch ex As Exception Logger.Error(ex) oMessageText = $"Fehler beim Übertragen des Dokuments:{vbNewLine}{vbNewLine}{ex.Message}" oMessageStyle = MsgBoxStyle.Critical Finally SplashScreenManager1.CloseWaitForm() ChangeUIState(True) MsgBox(oMessageText, oMessageStyle, Text) End Try End Sub Private Sub ChangeUIState(pEnabled As Boolean) GridControlPositions.Enabled = pEnabled GridControlFiles.Enabled = pEnabled LayoutControlHead.Enabled = pEnabled RibbonGroupDataTransmission.Enabled = pEnabled RibbonGroupDataLoading.Enabled = pEnabled RibbonGroupDocument.Enabled = pEnabled RibbonGroupPositions.Enabled = pEnabled End Sub Private Function TransferChangesToDocument(pDocument As Document) Dim oData As Orders.Input.MESOWebService = pDocument.Data Try Orders.Helpers.SetOrderHead(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT025)(oData, AddressOf UpdateOrderHead) Catch ex As Exception Logger.Error(ex) Throw ex End Try Return pDocument End Function Private Function UpdateOrderHead(pObject As Orders.Input.MESOWebServiceEXIMVRG_ordersT025) As Orders.Input.MESOWebServiceEXIMVRG_ordersT025 ' Update DocumentKind If cmbDocumentKind.EditValue IsNot Nothing AndAlso TypeOf cmbDocumentKind.EditValue Is DocumentKind Then pObject.Belegart = DirectCast(cmbDocumentKind.EditValue, DocumentKind).Id End If ' Update Talking Person lol If cmbOrderIssuer.EditValue IsNot Nothing AndAlso TypeOf cmbOrderIssuer.EditValue Is Contact Then pObject.Fakt_Ansprechpartner = DirectCast(cmbOrderIssuer.EditValue, Contact).Id End If ' Update Delivery Address If cmbDeliveryAddress.EditValue IsNot Nothing AndAlso TypeOf cmbDeliveryAddress.EditValue Is Account Then pObject.Lief_Kontonummer = DirectCast(cmbDeliveryAddress.EditValue, Account)?.Id End If ' Update Order Number If txtOrderNumber.EditValue IsNot Nothing AndAlso txtOrderNumber.EditValue <> String.Empty Then pObject.Auftragsreferenz = txtOrderNumber.EditValue End If ' Update Order Date If dateOrderDate IsNot Nothing Then pObject.Datum_AuftragBestellung = DirectCast(dateOrderDate.EditValue, Date).ToString("yyyy-MM-dd") End If ' Update Extra Address pObject.Lief_Name = txtName.EditValue pObject.Lief_Ort = txtPlace.EditValue pObject.Lief_PLZ = txtZIP.EditValue pObject.Lief_Strasse = txtStreetName.EditValue Return pObject End Function Private Sub BarButtonItem8_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem8.ItemClick TryOpenDirectory(LogConfig.LogDirectory, "Logverzeichnis") End Sub Private Sub cmbCustomer_Validating(sender As BaseEdit, e As System.ComponentModel.CancelEventArgs) Handles cmbCustomer.Validating, cmbDocumentKind.Validating, cmbMandator.Validating, cmbYear.Validating If sender.EditValue Is Nothing Then sender.ErrorText = "Dieses Feld muss ausgefüllt werden" Else sender.ErrorText = "" End If End Sub Private Sub btnPreviewReport_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnPreviewReport.ItemClick Dim oDocument As Document = GetFocusedDocument() If oDocument Is Nothing Then Exit Sub End If Dim oReport As New OrderReport() Dim oDataSource = New DevExpress.DataAccess.ObjectBinding.ObjectDataSource With { .DataSource = New Orders.ReportSource With { .Head = Orders.Helpers.GetOrderHead(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT025)(oDocument.Data), .Positions = Orders.Helpers.GetOrderPositions(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT025)(oDocument.Data) } } oDataSource.Fill() oReport.DataSource = oDataSource Dim oReportPrintTool As New ReportPrintTool(oReport) oReportPrintTool.ShowPreview() End Sub Private Sub btnReloadDocument_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadDocument.ItemClick Dim oResult = MsgBox("Wenn sie das Dokument neu laden, werden alle manuell geänderten Wert verworfen. Wollen Sie fortfahren?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, Text) If oResult = MsgBoxResult.Yes Then Dim oDocument As Document = GetFocusedDocument() If oDocument Is Nothing Then Exit Sub End If ShowDocument(oDocument) End If End Sub Private Sub btnDeletePosition_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDeletePosition.ItemClick Dim oPosition = GetFocusedPosition() If oPosition Is Nothing Then MsgBox("Bitte wählen Sie eine Position aus!", MsgBoxStyle.Exclamation, Text) Exit Sub End If Dim oResult = MsgBox($"Wollen Sie die ausgewählte Position Artikel [{oPosition.ArticleNumber}] wirklich löschen?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, Text) If oResult = MsgBoxResult.Yes Then GridViewPositions.DeleteSelectedRows() MsgBox("Position gelöscht") End If End Sub Private Sub BarButtonItem7_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick MsgBox("Mach et!") End Sub Private Sub BarButtonItem6_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Dim oUserConfigDirectory = New FileInfo(ConfigManager.UserConfigPath).Directory TryOpenDirectory(oUserConfigDirectory.FullName, "Konfigurationsverzeichnis") End Sub End Class