2020-09-22 10:50:20 +02:00

305 lines
12 KiB
VB.net

Imports System.IO
Imports System.Net
Imports System.Xml
Public Class Winline
Private _Logger As Logger
Private _EXIM As Database
Private _Winline As Database
Private _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"
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
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 = oRow.Item("GUID"),
.Name = oRow.Item("NAME"),
.GroupId = oRow.Item("GROUP"),
.Code = oRow.Item("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, C223)) 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
Throw New ApplicationException(NO_RUNNING_NUMBER_FOUND)
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)
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}"
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
Dim oDocument As New XmlDocument
oDocument.LoadXml(oXmlResponse)
oDocument.Save(Console.Out)
Dim oSuccess As XmlNode = oDocument.DocumentElement.SelectSingleNode("OverallSuccess")
If oSuccess.InnerText.ToUpper = "TRUE" Then
Return True
Else
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 c000 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)
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
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 &= $"<?xml version=""1.0"" encoding=""UTF-8""?>"
oXmlData &= $"<MESOWebService TemplateType=""{oTemplateType}"" Template=""{oTemplateName}"">"
oXmlData &= $"<{oTemplateName}>"
' ======================================================
oXmlData &= $"<Artikelnummer>{ArticleNumber}{RunningNumber}</Artikelnummer>"
oXmlData &= $"<Bezeichnung>{oDescription}</Bezeichnung>"
oXmlData &= $"<Artikeltyp>{oArticleType}</Artikeltyp>"
oXmlData &= $"<ChargeIdentflag>{oChargeIdentFlag}</ChargeIdentflag>"
oXmlData &= $"<Auspraegungsflag>{oShapeFlag}</Auspraegungsflag>"
oXmlData &= $"<VerwendungAuspr1>{oUseShape}</VerwendungAuspr1>"
oXmlData &= $"<Auspr1von>{oShapeFrom}</Auspr1von>"
oXmlData &= $"<Auspr1bis>{oShapeTo}</Auspr1bis>"
oXmlData &= $"<Hersteller>{Vendor.WinlineNumber.Trim}</Hersteller>"
oXmlData &= $"<Steuerzeile>{oTaxCode}</Steuerzeile>"
oXmlData &= $"<LaufendeNummer>{RunningNumber}</LaufendeNummer>"
oXmlData &= $"<Standardauspraegung1>{oDefaultShape}</Standardauspraegung1>"
oXmlData &= $"<Mehrfachauspraegung>{oMultipleShapes}</Mehrfachauspraegung>"
' ======================================================
oXmlData &= $"</{oTemplateName}>"
oXmlData &= $"</MESOWebService>"
Try
My.Application.Winline.SendWebserviceRequest(oTemplateType, oTemplateName, oXmlData)
Return True
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)
Dim oTemplateName As String = _Config.WebService.PriceTemplateName
Dim oTemplateType As Integer = _Config.WebService.PriceTemplateType
Dim oXmlData
oXmlData = ""
oXmlData &= $"<?xml version=""1.0"" encoding=""UTF-8""?>"
oXmlData &= $"<MESOWebService TemplateType=""{oTemplateType}"" Template=""{oTemplateName}"">"
oXmlData &= $"<{oTemplateName}>"
' ======================================================
oXmlData &= $"<Artikelnummer>{ArticleNumber}{RunningNumber}</Artikelnummer>"
oXmlData &= $"<Kontonummer>{VendorNumber.Trim}</Kontonummer>"
' ======================================================
oXmlData &= $"</{oTemplateName}>"
oXmlData &= $"</MESOWebService>"
Try
My.Application.Winline.SendWebserviceRequest(oTemplateType, oTemplateName, oXmlData)
Return True
Catch ex As Exception
_Logger.Error(ex)
Return False
End Try
End Function
Public Sub RunWinlineMacro(MacroName As String, ParamArray Parameters As Object())
Try
Dim oCWLObject
Dim oParamArray As Object = New Runtime.InteropServices.VariantWrapper(Parameters)
oCWLObject = CreateObject("cwlstart.application")
oCWLObject.MacroCommands.MRunMacro(MacroName, oParamArray)
Catch ex As Exception
_Logger.Error(ex)
MsgBox($"Das WinLine-Makro [{MacroName}] konnte nicht gestartet werden. Mehr Informationen im Log.", MsgBoxStyle.Critical, "WinLine")
End Try
End Sub
End Class