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

@@ -3,5 +3,6 @@
Public Class Config
<ConnectionString>
Public Property ConnectionString As String = ""
Public Property LastUsedMandator As String = ""
Public Property Debug As Boolean = False
End Class

View File

@@ -241,15 +241,15 @@ Namespace Documents
If oMandator Is Nothing Then
Logger.Warn("Mandator not found for File [{0}]", pDocument.File.Name)
Else
pDocument = MatchDocumentDataFromHardcodedMappings(pDocument, oMandator, pTemplate)
pDocument = MatchDocumentDataFromDynamicMappings(pDocument, oMandator)
pDocument = ApplyDefinedItemFunctionsForImport(pDocument, oMandator, pTemplate)
pDocument = ApplyDynamicItemFunctionsForImport(pDocument, oMandator)
End If
pDocument.Mandator = oMandator
Return pDocument
End Function
Private Function MatchDocumentDataFromHardcodedMappings(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document
Private Function ApplyDefinedItemFunctionsForImport(pDocument As Document, pMandator As Mandator, pTemplate As Template) As Document
If pMandator Is Nothing Then
Return pDocument
End If
@@ -284,7 +284,7 @@ Namespace Documents
Return pDocument
End Function
Private Function MatchDocumentDataFromDynamicMappings(pDocument As Document, pMandator As Mandator) As Document
Private Function ApplyDynamicItemFunctionsForImport(pDocument As Document, pMandator As Mandator) As Document
If pMandator Is Nothing Then
Return pDocument
End If

View File

@@ -20,49 +20,57 @@
End Sub
End Class
Public Class WebServiceException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MissingAttributeException
Inherits MultiToolException
Public Sub New(attributeName As String)
MyBase.New(attributeName)
End Sub
End Class
Public Class MultipleAccountsException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoAccountException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoMandatorException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MalformedXmlException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class DatabaseException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class WebServiceException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MissingAttributeException
Inherits MultiToolException
Public Sub New(attributeName As String)
MyBase.New(attributeName)
End Sub
End Class
Public Class MultipleAccountsException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoAccountException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class NoMandatorException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
Public Class MalformedXmlException
Inherits MultiToolException
Public Sub New(message As String)
MyBase.New(message)
End Sub
End Class
End Class

View File

@@ -2,6 +2,7 @@
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.Documents
Imports MultiTool.Shared.Templates
Imports MultiTool.Shared.Winline.Entities
Public Class Patterns
Inherits BaseClass
@@ -13,7 +14,7 @@ Public Class Patterns
GeneralConfig = pGeneralConfig
End Sub
Public Function Replace(pDocument As Document, pRow As DocumentRow, oString As String)
Public Function ReplaceForImport(pDocument As Documents.Document, pRow As DocumentRow, oString As String)
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oMatches As MatchCollection = oRegex.Matches(oString)
@@ -34,7 +35,7 @@ Public Class Patterns
Case "CONST"
Dim oValue = ""
Select Case oMatch.Groups.Item(2).Value.ToUpper
Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR"
oValue = GeneralConfig.GetWinLineYear()
@@ -56,7 +57,59 @@ Public Class Patterns
End Select
If oValue <> "" Then
oString = oString.Replace(oPlaceholderValue, oValue)
oString = oString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return oString
End Function
Public Function ReplaceForExport(pTemplate As Template, pDocument As Winline.Entities.Document, pMandator As Mandator, oString As String)
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+")
Dim oMatches As MatchCollection = oRegex.Matches(oString)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case "CONST"
Dim oValue = ""
Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR"
oValue = GeneralConfig.GetWinLineYear()
Case "MESOCOMP"
oValue = pMandator.Id
Case "USERNAME"
oValue = Environment.UserName
Case "CURRENTDATE"
oValue = Now.ToString()
Case "FILENAME"
oValue = pDocument.FilenameExport
Case "ACCOUNTNUMBER"
oValue = pDocument.Account.Id
Case "RUNNINGNUMBER"
oValue = pDocument.RunningNumber
Case "MANDATORDB"
oValue = pMandator.Database
Case Else
oValue = ""
End Select
If oValue <> "" Then
oString = oString.Replace(oPlaceholderString, oValue)
End If
End Select
Next

View File

@@ -116,7 +116,7 @@ Public Class ReportGenerator(Of TReport As IReport)
SingleOrDefault()
Dim oSQL = oSQLConfigItem.Function.Params
oSQL = Patterns.Replace(pDocument, oRow, oSQL)
oSQL = Patterns.ReplaceForImport(pDocument, oRow, oSQL)
Dim oValue = Database.GetScalarValue(oSQL)

View File

@@ -2,8 +2,6 @@
Public Class GeneralConfig
Public Property TemplateDirectory As String = ""
Public Property Templates As New List(Of TemplateConfig)
Public Property Webservice As New WebServiceConfig()
Public Property DefaultYearOverride As Integer = 0

View File

@@ -46,6 +46,10 @@
''' </summary>
Public Property IsRequired As Boolean
Public Property Config As TemplateConfigItem
Public Overrides Function ToString() As String
Return Name
End Function
End Class
End Class
End Namespace

View File

@@ -8,9 +8,9 @@ Namespace Templates
Public Class TemplateConfig
Public Property Items As List(Of TemplateConfigItem)
Public Function GetColumn(pName As String) As TemplateConfigItem
Public Function GetColumn(pName As String, pTable As String) As TemplateConfigItem
Return Items.
Where(Function(c) c.Name = pName).
Where(Function(c) c.Name = pName And c.Table = pTable).
FirstOrDefault()
End Function
End Class

View File

@@ -242,7 +242,7 @@ Namespace Templates
For Each oTable In pTemplate.Tables
For Each oColumn As Template.Column In oTable.Columns
Dim oConfig = pTemplateConfig.GetColumn(oColumn.Name)
Dim oConfig = pTemplateConfig.GetColumn(oColumn.Name, oTable.Name)
If oConfig Is Nothing Then
oConfig = New TemplateConfigItem With {

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