Imports System.IO Imports System.Net Imports System.Xml Public Class Winline Private ReadOnly _Logger As Logger Private ReadOnly _EXIM As Database Private ReadOnly _Winline As Database Private ReadOnly _Config As Config Public ReadOnly Property NO_RUNNING_NUMBER_FOUND = "NO_RUNNING_NUMBER_FOUND" Public ReadOnly Property ARTICLE_ALREADY_EXISTS = "ARTICLE_ALREADY_EXISTS" Public ReadOnly Property UNKNOWN_ERROR = "UNKNOWN_ERROR" Private Const ACTIONCODE_CHECK = 0 Private Const ACTIONCODE_IMPORT = 1 Public Sub New(LogConfig As LogConfig, EximDatabase As Database, WinlineDatabase As Database, Config As Config) _Logger = LogConfig.GetLogger() _EXIM = EximDatabase _Winline = WinlineDatabase _Config = Config End Sub Public Function GetVendorsFromWinLine() As List(Of Vendor) Try Dim CurrentYear = (Now.Year - 1900) * 12 Dim CurrentMandator = _Config.WebService.Mandator Dim oDatatable As DataTable = _Winline.GetDatatable( $"SELECT * FROM V005 WHERE C004 = 3 AND c002 IS NOT NULL AND c003 IS NOT NULL AND " & $"mesoyear = {CurrentYear} And mesocomp = '{CurrentMandator}'" & "ORDER BY c003" ) Dim oVendors As New List(Of Vendor) For Each oRow As DataRow In oDatatable.Rows Dim oVendor As New Vendor() With { .WinlineName = oRow.Item("c003"), .WinlineNumber = oRow.Item("c002") } oVendors.Add(oVendor) Next Return oVendors Catch ex As Exception _Logger.Error(ex) Return Nothing End Try End Function Public Function GetVendors() As List(Of Vendor) Try Dim oDatatable As DataTable = _EXIM.GetDatatable("SELECT * FROM TBDD_ARTICLE_GENERATOR_VENDORS ORDER BY CODE") Dim oVendors As New List(Of Vendor) For Each oRow As DataRow In oDatatable.Rows Dim oVendor As New Vendor() With { .Code = oRow.Item("CODE"), .Name = oRow.Item("NAME"), .Guid = oRow.Item("GUID"), .WinlineName = oRow.Item("WINLINE_NAME"), .WinlineNumber = oRow.Item("WINLINE_NUMBER") } oVendors.Add(oVendor) Next Return oVendors Catch ex As Exception _Logger.Error(ex) Return Nothing End Try End Function Private Function NotNull(Of T)(ByVal value As T, ByVal defaultValue As T) As T If IsNothing(value) OrElse String.IsNullOrEmpty(value.ToString) OrElse IsDBNull(value) Then Return defaultValue Else Return value End If End Function Public Function GetGroupsByVendor(VendorCode As String) As List(Of ProductGroup) Try Dim oDatatable = _EXIM.GetDatatable($"SELECT * FROM TBDD_ARTICLE_GENERATOR_GROUPS WHERE CODE = '{VendorCode}'") Dim oGroups As New List(Of ProductGroup) For Each oRow As DataRow In oDatatable.Rows Dim oGroup As New ProductGroup() With { .Guid = NotNull(oRow.Item("GUID"), 0), .Name = NotNull(oRow.Item("NAME"), "(Kein Name)"), .GroupId = NotNull(oRow.Item("GROUP"), 0), .Code = NotNull(oRow.Item("CODE"), "(Kein Code)") } oGroups.Add(oGroup) Next Return oGroups Catch ex As Exception _Logger.Error(ex) Return Nothing End Try End Function Public Function GetVersionsByVendorAndGroup(VendorCode As String, GroupId As String) As List(Of ProductVersion) Try Dim oDatatable = _EXIM.GetDatatable($"SELECT * FROM TBDD_ARTICLE_GENERATOR_PRODUCTS WHERE CODE = '{VendorCode}' AND GROUP_ID = {GroupId}") Dim oVersions As New List(Of ProductVersion) For Each oRow As DataRow In oDatatable.Rows Dim oVersion As New ProductVersion() With { .Guid = oRow.Item("GUID"), .VersionId = oRow.Item("VERSION"), .GroupId = oRow.Item("GROUP_ID"), .Name = oRow.Item("NAME"), .Code = oRow.Item("CODE") } oVersions.Add(oVersion) Next Return oVersions Catch ex As Exception _Logger.Error(ex) Return Nothing End Try End Function Public Function GetNextRunningNumber(VendorCode As String, GroupId As Integer, VersionId As Integer) As String Try Dim oGroupCode As String = GroupId.ToString.PadLeft(2, "0") Dim oVersionCode As String = VersionId.ToString.PadLeft(2, "0") Dim oDbResult = _Winline.GetScalarValue($"SELECT MAX(CONVERT(INT, SUBSTRING(c010, 7, 3))) FROM v021 WHERE c010 LIKE '{VendorCode}{oGroupCode}{oVersionCode}___'") If IsNumeric(oDbResult) Then Dim oNewRunningNumber = Integer.Parse(oDbResult) + 1 Return oNewRunningNumber.ToString.PadLeft(3, "0") ElseIf IsNothing(oDbResult) OrElse IsDBNull(oDbResult) Then Return 1.ToString.PadLeft(3, "0") Else Throw New ApplicationException(UNKNOWN_ERROR) End If Catch ex As Exception _Logger.Error(ex) Throw ex End Try End Function Public Function SendWebserviceRequest(TemplateType As Integer, TemplateName As String, XmlData As String) As Boolean Try Dim oWebserviceConfig = My.Application.ConfigManager.Config.WebService Dim oServer As String = oWebserviceConfig.Server Dim oURI = $"http://{oServer}/ewlservice/import" Dim oQuery As String = "" oQuery &= $"User={oWebserviceConfig.Username}&" oQuery &= $"Password={oWebserviceConfig.Password}&" oQuery &= $"Company={oWebserviceConfig.Mandator}&" oQuery &= $"Type={TemplateType}&" oQuery &= $"Vorlage={TemplateName}&" oQuery &= $"Actioncode={oWebserviceConfig.ActionCode}&" oQuery &= $"byref=0&" oQuery &= $"Data={XmlData}" oURI &= $"?{oQuery}" _Logger.Info("WebService Request: {0}", oURI) Dim oRequest As HttpWebRequest = WebRequest.Create(oURI) Dim oResponse As HttpWebResponse = DirectCast(oRequest.GetResponse(), HttpWebResponse) Dim oXmlResponse As String Using oReader As New StreamReader(oResponse.GetResponseStream()) oXmlResponse = oReader.ReadToEnd() End Using _Logger.Info("WebService Response: {0}", oXmlResponse) Dim oDocument As New XmlDocument Try oDocument.LoadXml(oXmlResponse) oDocument.Save(Console.Out) Catch ex As Exception _Logger.Error(ex) Return False End Try Dim oSuccess As XmlNode = oDocument.DocumentElement.SelectSingleNode("OverallSuccess") Dim oInnerSuccess = True If oSuccess.InnerText.ToUpper = "TRUE" Then Dim oSuccessNodes = oDocument.DocumentElement.SelectNodes("//Success") For Each oNode As XmlNode In oSuccessNodes If oNode.InnerText.ToUpper <> "TRUE" Then oInnerSuccess = False End If Next _Logger.Info("Request was SUCCESSFUL!") If oInnerSuccess = False Then Return False Else Return True End If Else _Logger.Info("Request FAILED!") Return False End If Catch ex As Exception _Logger.Error(ex) Return False End Try End Function Public Function TestArticleExists(ArticleNumber As String) As Boolean Try Dim oResult = _Winline.GetScalarValue($"SELECT c002 FROM v021 WHERE c002 = '{ArticleNumber}'") If IsNothing(oResult) Or IsDBNull(oResult) Then Return False Else Return True End If Catch ex As Exception Return False End Try End Function Public Function CreateArticle(ArticleNumber As String, RunningNumber As String, ArticleDescription As String, Vendor As Vendor, IsSerialnumberArticle As Boolean) As Boolean Dim oTemplateName As String = _Config.WebService.ArticleTemplateName Dim oTemplateType As Integer = _Config.WebService.ArticleTemplateType Dim oTaxCode As Integer = _Config.WebService.TaxCode Dim oDescription As String = ArticleDescription Dim oShapeFrom As String Dim oShapeTo As String Dim oArticleType As Integer Dim oChargeIdentFlag As Integer Dim oShapeFlag As Integer Dim oUseShape As String Dim oDefaultShape As String Dim oMultipleShapes As String Dim oActionCode As Integer = ACTIONCODE_IMPORT If IsSerialnumberArticle = True Then oArticleType = 0 oChargeIdentFlag = 1 oShapeFlag = 1 oUseShape = "true" oShapeFrom = _Config.WebService.ShapeFrom oShapeTo = _Config.WebService.ShapeTo oDefaultShape = "A" oMultipleShapes = "true" Else oArticleType = 0 oChargeIdentFlag = 2 oShapeFlag = 0 oUseShape = "false" oShapeFrom = String.Empty oShapeTo = String.Empty oDefaultShape = String.Empty oMultipleShapes = "false" End If Dim oXmlData oXmlData = "" oXmlData &= $"" oXmlData &= $"" oXmlData &= $"<{oTemplateName}>" ' ====================================================== oXmlData &= $"{ArticleNumber}{RunningNumber}" oXmlData &= $"{oDescription}" oXmlData &= $"{oArticleType}" oXmlData &= $"{oChargeIdentFlag}" oXmlData &= $"{oShapeFlag}" oXmlData &= $"{oUseShape}" oXmlData &= $"{oShapeFrom}" oXmlData &= $"{oShapeTo}" oXmlData &= $"{Vendor.WinlineNumber.Trim}" oXmlData &= $"{oTaxCode}" oXmlData &= $"{RunningNumber}" oXmlData &= $"{oDefaultShape}" oXmlData &= $"{oMultipleShapes}" ' ====================================================== oXmlData &= $"" oXmlData &= $"" Try Return My.Application.Winline.SendWebserviceRequest(oTemplateType, oTemplateName, oXmlData) Catch ex As Exception _Logger.Error(ex) Return False End Try End Function Public Function CreatePriceInfo(ArticleNumber As String, RunningNumber As String, VendorNumber As String) As Boolean Dim oTemplateName As String = _Config.WebService.PriceTemplateName Dim oTemplateType As Integer = _Config.WebService.PriceTemplateType Dim oXmlData oXmlData = "" oXmlData &= $"" oXmlData &= $"" oXmlData &= $"<{oTemplateName}>" ' ====================================================== oXmlData &= $"{ArticleNumber}{RunningNumber}" oXmlData &= $"{VendorNumber.Trim}" ' ====================================================== oXmlData &= $"" oXmlData &= $"" Try Return My.Application.Winline.SendWebserviceRequest(oTemplateType, oTemplateName, oXmlData) Catch ex As Exception _Logger.Error(ex) Return False End Try End Function Public Function RunWinlineMacro(MacroName As String, ParamArray Parameters As Object()) As Boolean Try Dim oCWLObject Dim oParamArray As Object = New Runtime.InteropServices.VariantWrapper(Parameters) oCWLObject = CreateObject("cwlstart.application") oCWLObject.MacroCommands.MRunMacro(MacroName, oParamArray) Return True Catch ex As Exception _Logger.Error(ex) _Logger.Warn($"Das WinLine-Makro [{MacroName}] konnte nicht gestartet werden.") Return False End Try End Function End Class