Finish FinalSQL, GLN/EAN for export, double click to open template, exporting multiple documents

This commit is contained in:
Jonathan Jenne
2022-01-20 16:34:52 +01:00
parent ff7c8c63ea
commit c315640d7d
25 changed files with 531 additions and 362 deletions

View File

@@ -1,8 +1,8 @@
Namespace Winline.Entities
Public Class Article
Public Property Id
Public Property Name
Public Property EAN
Public Property Id As String
Public Property Name As String
Public Property EAN As String
Public Property Mandator As Mandator
Public Overrides Function GetHashCode() As Integer

View File

@@ -19,6 +19,8 @@
Public Property IsSelected As Boolean = False
Public Property IsExported As Boolean = False
Public Property FilenameExport As String
End Class
End Namespace

View File

@@ -15,16 +15,18 @@ Namespace Winline
Private ReadOnly Config As WebServiceConfig
Private ReadOnly Serializer As Serializer
Private ReadOnly GeneralConfig As GeneralConfig
Private ReadOnly Winline As WinlineData
Private ReadOnly FileEx As File
Public Event WebServiceProgress As EventHandler(Of String)
Public Sub New(pLogConfig As LogConfig, pWebserviceConfig As WebServiceConfig, pGeneralConfig As GeneralConfig)
Public Sub New(pLogConfig As LogConfig, pWinline As WinlineData, pWebserviceConfig As WebServiceConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig)
Serializer = New Serializer(pLogConfig)
Config = pWebserviceConfig
GeneralConfig = pGeneralConfig
FileEx = New DigitalData.Modules.Filesystem.File(LogConfig)
Winline = pWinline
End Sub
Public Sub RaiseWebServiceProgress(pMessage As String)
@@ -208,6 +210,9 @@ Namespace Winline
Dim oBaseFileName As String = FileEx.GetDateTimeString()
Dim oFileName = FileEx.GetFilenameWithSuffix(oBaseFileName, "Request", "xml")
' Save the filename to the document
pDocument.FilenameExport = oFileName
' Absolute Path to copy Request file
Dim oImportAbsolutePath = IO.Path.Combine(oWS.ImportBasePath, oWS.ImportRelativePath)
Dim oImportAbsoluteFilePath = IO.Path.Combine(FileEx.GetDateDirectory(oImportAbsolutePath), oFileName)
@@ -242,18 +247,21 @@ Namespace Winline
' --- Bring the action!
Try
Dim oResponse As HttpResponseMessage = Await oClient.GetAsync(oURL)
Await HandleExportResponse(oResponse, pTemplate, oBaseFileName)
Await HandleExportResponse(oResponse, pTemplate, pMandator, oBaseFileName)
Return True
Catch ex As Exception
Logger.Error(ex)
Throw ex
Finally
oClient.Dispose()
End Try
End Function
Private Async Function HandleExportResponse(pResponse As HttpResponseMessage, pTemplate As Template, pBaseFileNAme As String) As Task
Private Async Function HandleExportResponse(pResponse As HttpResponseMessage, pTemplate As Template, pMandator As Mandator, pBaseFileNAme As String) As Task
pResponse.EnsureSuccessStatusCode()
Dim oResponseBody As String = Await pResponse.Content.ReadAsStringAsync()
Dim oContentType = pResponse.Content.Headers.ContentType.MediaType
@@ -261,11 +269,13 @@ Namespace Winline
RaiseEvent WebServiceProgress(Me, "Antwort verarbeiten")
oResponseBody = ApplyItemFunctionsForExport(pTemplate, pMandator, oResponseBody)
Select Case oContentType
Case "text/xml"
WriteResponseFile(pTemplate.OutputWebserviceDirectory, pBaseFileNAme, oResponseBody, "xml")
WriteResponseFile(pTemplate.OutputXmlFileDirectory, pBaseFileNAme, oResponseBody, "xml")
WriteResponseFile(FileEx.GetDateDirectory(pTemplate.ArchiveDirectory), pBaseFileNAme, oResponseBody, "xml")
WriteResponseFile(FileEx.CreateDateDirectory(pTemplate.ArchiveDirectory), pBaseFileNAme, oResponseBody, "xml")
Case "text/html"
WriteResponseFile(pTemplate.OutputWebserviceDirectory, pBaseFileNAme, oResponseBody, "txt")
@@ -276,9 +286,54 @@ Namespace Winline
Throw New ApplicationException(oResponseBody)
End Select
End Function
Private Function ApplyItemFunctionsForExport(pTemplate As Template, pMandator As Mandator, oResponseBody As String) As String
Dim oDoc As New XmlDocument()
oDoc.LoadXml(oResponseBody)
For Each oTable In pTemplate.Tables
For Each oItem As Template.Column In oTable.Columns
Dim oTableName As String = oTable.Name
Dim oItemName As String = oItem.Name
If oItem.Config.Function Is Nothing Then
Continue For
End If
Dim oFunction = oItem.Config.Function.Name
Dim oNode = oDoc.SelectSingleNode($"//MESOWebService/{oTableName}/{oItemName}")
If oItem.Config.Function.Name = "GLN" Then
Dim oGLN = Winline.TryGetGLN(oNode.InnerText, pMandator)
If oGLN Is Nothing Then
Throw New Exceptions.MissingAttributeException("GLN")
End If
oNode.InnerText = oGLN
ElseIf oItem.Config.Function.Name = "EAN" Then
Dim oEAN = Winline.TryGetEAN(oNode.InnerText, pMandator)
If oEAN Is Nothing Then
Throw New Exceptions.MissingAttributeException("EAN")
End If
oNode.InnerText = oEAN
End If
Next
Next
Dim oArray As Byte()
Using oStream As New IO.MemoryStream
oDoc.Save(oStream)
oArray = oStream.ToArray()
End Using
Dim oXml = Text.Encoding.UTF8.GetString(oArray)
oResponseBody = oXml
Return oResponseBody
End Function
#End Region
Private Function WriteResponseFile(pPath As String, pBaseFileName As String, pResponseBody As String, pExtension As String)
Private Function WriteResponseFile(pPath As String, pBaseFileName As String, pResponseBody As String, pExtension As String) As Boolean
Try
Dim oRequestFileName As String = FileEx.GetFilenameWithSuffix(pBaseFileName, "Response", pExtension)
Dim oFilePath As String = IO.Path.Combine(pPath, oRequestFileName)

View File

@@ -13,6 +13,7 @@ Namespace Winline
Private ReadOnly Property Config As GeneralConfig
Private ReadOnly Property MandatorConfig As MandatorConfig
Private ReadOnly Property MappingConfig As MappingConfig
Private ReadOnly Property Patterns As Patterns
Public Property Articles As New List(Of Article)
Public Property Accounts As New List(Of Account)
@@ -23,6 +24,7 @@ Namespace Winline
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As GeneralConfig, pMappingConfig As MappingConfig, pMandatorConfig As MandatorConfig)
MyBase.New(pLogConfig)
Patterns = New Patterns(pLogConfig, pConfig)
Database = pDatabase
Config = pConfig
MandatorConfig = pMandatorConfig
@@ -223,7 +225,7 @@ Namespace Winline
[c052], -- Ort
[c051] -- PLZ
FROM [{pMandator.Database}].[dbo].[v050]
WHERE [c004] = 2 -- KontoTyp
WHERE [c004] IN (2, 3) -- KontoTyp Debitor/Kreditor
AND [c260] = '{pGLN}'
AND [mesocomp] = '{pMandator.Id}' and [mesoyear] = {oYear}"
Dim oTable As DataTable = Database.GetDatatable(oSQL)
@@ -264,6 +266,37 @@ Namespace Winline
End Try
End Function
Public Function TryGetGLN(pAccountId As String, pMandator As Mandator) As String
Try
If pAccountId Is Nothing OrElse pAccountId = String.Empty Then
Return Nothing
End If
Dim oYear As Integer = Config.GetWinLineYear()
Dim oSQL = $"
SELECT
[c260] -- GLN
FROM [{pMandator.Database}].[dbo].[v050]
WHERE [c004] IN (2, 3) -- KontoTyp Debitor/Kreditor
AND [c002] = '{pAccountId}'
AND [mesocomp] = '{pMandator.Id}' and [mesoyear] = {oYear}"
Dim oGLN As String = Database.GetScalarValue(oSQL)
' GLN not found in this Mandator, continue to next one
If oGLN Is Nothing Then
Logger.Debug("Account [{0}] was not found in Mandator: [{1}]", pAccountId, pMandator.Id)
Return Nothing
End If
Return oGLN
Catch ex As Exception
Logger.Warn("Error while trying to get GLN for Account [{0}]", pAccountId)
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function TryGetArticleNumber(pEAN As String, pMandator As Mandator) As String
Try
Dim oYear As Integer = Config.GetWinLineYear()
@@ -303,6 +336,33 @@ Namespace Winline
End Try
End Function
Public Function TryGetEAN(pArticleNumber As String, pMandator As Mandator) As String
Try
Dim oYear As Integer = Config.GetWinLineYear()
Dim oSQL As String = $"
SELECT
[c075] -- EAN-Nummer
FROM [{pMandator.Database}].[dbo].[v021]
WHERE [c011] = '{pArticleNumber}'
AND [mesocomp] = '{pMandator.Id}' AND [mesoyear] = {oYear}"
Dim oEAN As String = Database.GetScalarValue(oSQL)
' EAN not found in this Mandator, continue to next one
If oEAN Is Nothing Then
Logger.Debug("ArticleNumber [{0}] was not found in Mandator: [{1}]", pArticleNumber, pMandator.Id)
Return Nothing
End If
Return oEAN
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function GetContacts(pAccountNumber As String, pMandator As Mandator) As List(Of Contact)
Try
Dim oContacts As New List(Of Contact)
@@ -480,7 +540,7 @@ Namespace Winline
oDateConstraint = $""
End If
Dim oExportedConstraint = "c111 = 0 AND"
Dim oExportedConstraint = "U010 = 0 AND"
If pOptions.ShowExported Then
oExportedConstraint = ""
End If
@@ -509,7 +569,7 @@ Namespace Winline
c100 GROSS_AMOUNT,
c114 NET_AMOUNT,
c111 ALREADY_EXPORTED
U010 ALREADY_EXPORTED
FROM [{pMandator.Database}].[dbo].[T025]
WHERE
@@ -543,13 +603,14 @@ Namespace Winline
End Try
End Function
Public Async Function ExecuteFinalSQL(pDocument As Document, pTemplate As Template) As Task(Of Boolean)
Public Async Function ExecuteFinalSQL(pDocument As Document, pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
Try
Dim oSql As String = pTemplate.FinalSQL
Dim oSql As String = Patterns.ReplaceForExport(pTemplate, pDocument, pMandator, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function