From 9b9b7b4d6d2a2b57390007650bf2613d4ca0f302 Mon Sep 17 00:00:00 2001 From: Andreas Luxbauer Date: Fri, 7 Feb 2025 08:08:08 +0100 Subject: [PATCH] HMRC --- VERAG_PROG_ALLGEMEIN/Classes/cHMRCToken.vb | 158 ++++++ .../Schnittstellen/Modaltrans/cModalENS.vb | 468 ++++++++++++++++++ .../Modaltrans/cOregonNCTSDeclarations.vb | 3 + .../Schnittstellen/OPEN_AI/cOpenAI.vb | 42 ++ .../VERAG_PROG_ALLGEMEIN.vbproj | 1 + 5 files changed, 672 insertions(+) create mode 100644 VERAG_PROG_ALLGEMEIN/Classes/cHMRCToken.vb create mode 100644 VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cModalENS.vb diff --git a/VERAG_PROG_ALLGEMEIN/Classes/cHMRCToken.vb b/VERAG_PROG_ALLGEMEIN/Classes/cHMRCToken.vb new file mode 100644 index 00000000..24292d21 --- /dev/null +++ b/VERAG_PROG_ALLGEMEIN/Classes/cHMRCToken.vb @@ -0,0 +1,158 @@ +Imports System.Data.SqlClient +Imports System.Reflection + +Public Class cHMRCToken + Property token_id As Integer + Property token_BEARER_TOKEN As String = "" + Property token_datetime As Date = Now + Property token_Firma As String = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA + Property token_Application As String = "" + Property token_refresh_datetime As Object = Nothing + + + + Public hasEntry = False + + Dim SQL As New SQL + + Sub New(token_id) + Me.token_id = token_id + LOAD() + End Sub + Sub New(token_Firma, token_Application) + Me.token_Firma = token_Firma + Me.token_Application = token_Application + LOAD_ByFirmaAppl() + End Sub + Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) + Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_id", token_id,, True)) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_BEARER_TOKEN", token_BEARER_TOKEN)) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_datetime", token_datetime)) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_Firma", token_Firma)) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_Application", token_Application)) + list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("token_refresh_datetime", token_refresh_datetime)) + + Return list + End Function + + + + Public Function SAVE() As Boolean + Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() + + Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblHMRCToken WHERE token_id=@token_id) " & + " BEGIN " & getUpdateCmd() & " END " & + " Else " & + " BEGIN " & getInsertCmd() & " END " & + " commit tran " + + If SQL.doSQLVarList(sqlstr, "FMZOLL", , list) Then + hasEntry = True + Return True + Else + Return False + End If + End Function + + Public Sub LOAD() + Try + hasEntry = False + Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL() + Using cmd As New SqlCommand("SELECT * FROM tblHMRCToken WHERE token_id=@token_id ", conn) + cmd.Parameters.AddWithValue("@token_id", token_id) + Dim dr = cmd.ExecuteReader() + If dr.Read Then + For Each li In getParameterList() + Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable) + + If dr.Item(li.Text) Is DBNull.Value Then + propInfo.SetValue(Me, Nothing) + Else + propInfo.SetValue(Me, dr.Item(li.Text)) + End If + + Next + hasEntry = True + End If + dr.Close() + End Using + End Using + Catch ex As Exception + VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) + End Try + End Sub + + + Public Sub LOAD_ByFirmaAppl() + Try + hasEntry = False + Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL() + Using cmd As New SqlCommand("SELECT * FROM tblHMRCToken WHERE token_Firma=@token_Firma and token_Application=@token_Application ", conn) + cmd.Parameters.AddWithValue("@token_Firma", token_Firma) + cmd.Parameters.AddWithValue("@token_Application", token_Application) + Dim dr = cmd.ExecuteReader() + If dr.Read Then + For Each li In getParameterList() + Dim propInfo As PropertyInfo = Me.GetType.GetProperty(li.Scalarvariable) + + If dr.Item(li.Text) Is DBNull.Value Then + propInfo.SetValue(Me, Nothing) + Else + propInfo.SetValue(Me, dr.Item(li.Text)) + End If + + Next + hasEntry = True + End If + dr.Close() + End Using + End Using + Catch ex As Exception + VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) + End Try + End Sub + + + + Public Function getUpdateCmd() As String + Try + Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() + + Dim str As String = "" + For Each i In list + If Not i.isPrimaryParam Then + str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & "," + End If + Next + str = str.Substring(0, str.Length - 1) 'wg. ',' + Return (" UPDATE [tblHMRCToken] SET " & str & " WHERE token_id=@token_id ") + + Catch ex As Exception + VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) + End Try + Return "" + End Function + + + Public Function getInsertCmd() As String + Try + Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList() + Dim str As String = "" + Dim values As String = "" + For Each i In list + If Not i.isPrimaryParam Then + str &= "[" & i.Text & "]," + values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & "," + End If + Next + str = str.Substring(0, str.Length - 1) 'wg. ',' + values = values.Substring(0, values.Length - 1) 'wg. ',' + Return (" INSERT INTO tblHMRCToken (" & str & ") VALUES(" & values & ") ") + Catch ex As Exception + VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) + End Try + Return "" + End Function + +End Class diff --git a/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cModalENS.vb b/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cModalENS.vb new file mode 100644 index 00000000..b6b9a60d --- /dev/null +++ b/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cModalENS.vb @@ -0,0 +1,468 @@ + +Imports System +Imports Newtonsoft.Json +Imports Newtonsoft.Json.Converters +Imports System.Runtime.CompilerServices + +Namespace cModalENS + + ''' + ''' CreateEnsDeclaration + ''' + Partial Public Class ApidogModel + + Public Property Manifesto As Manifesto + End Class + + Partial Public Class Manifesto + + Public Property NctsJsonData As NctsJsonData + End Class + + Partial Public Class NctsJsonData + ''' + ''' Code of the arrival customs + ''' + + Public Property ArrivalCustomCode As String + + ''' + ''' Carrier Address + ''' + + Public Property CarrierAddress As String + + ''' + ''' Carrier City + ''' + + Public Property CarrierCity As String + + ''' + ''' Carrier Country Code + ''' + + Public Property CarrierCountryId As String + + ''' + ''' Carrier Name + ''' + + Public Property CarrierName As String + + ''' + ''' Carrier Postcode + ''' + + Public Property CarrierPostcode As String + + ''' + ''' Carrier Tax No + ''' + + Public Property CarrierTaxno As String + + ''' + ''' Container (if cargo is containerized) + ''' + + Public Property Container As String + + ''' + ''' Code of the departure customs + ''' + + Public Property DepartureCustomCode As String + + ''' + ''' Destination Country Code + ''' + + Public Property DestinationCountryId As String + + ''' + ''' Dispatch Country Code + ''' + + Public Property DispatchCountryId As String + + ''' + ''' Type of the declaration. Should be "ENS". + ''' + + Public Property DocType As DocType + + ''' + ''' Voyage NO + ''' + + Public Property EnsVoyageNo As String + + + Public Property GoodsAttributes As GoodsAttribute() + + ''' + ''' Departure Place + ''' + + Public Property LoadPlace As String + + ''' + ''' RORO Operator + ''' + + Public Property RoroOperatorCode As String + + ''' + ''' Seal NO + ''' + + Public Property SealInfo As String + + ''' + ''' Trailer Code/Plate + ''' + + Public Property TrailerCode As String + + ''' + ''' Trailer Country Code + ''' + + Public Property TrailerCoun As String + + ''' + ''' Transit Method + ''' + + Public Property TransMethod As TransMethod + + ''' + ''' Transit Countries (write it for every transit country) + ''' + + Public Property TransitCountriesAttributes As TransitCountriesAttribute() + + ''' + ''' Destination Place + ''' + + Public Property UnloadPlace As String + + ''' + ''' Vehicle Code/Plate + ''' + + Public Property VehicleCode As String + + ''' + ''' Vehicle Country Code + ''' + + Public Property VehicleCoun As String + End Class + + Partial Public Class GoodsAttribute + ''' + ''' Gross weight of goods + ''' + + Public Property BrutWg As Double? + + ''' + ''' Goods description + ''' + + Public Property Commodity As String + + ''' + ''' Consignee's address + ''' + + Public Property ConsigneeAddress As String + + ''' + ''' Consignee's city + ''' + + Public Property ConsigneeCity As String + + ''' + ''' Consignee's country code + ''' + + Public Property ConsigneeCountryId As String + + ''' + ''' Consignee's name + ''' + + Public Property ConsigneeName As String + + ''' + ''' Consignee's postcode + ''' + + Public Property ConsigneePostcode As String + + ''' + ''' Consignee's tax number + ''' + + Public Property ConsigneeTaxno As String + + ''' + ''' Container number + ''' + + Public Property ContainerNo As String + + ''' + ''' HS code for the goods + ''' + + Public Property GtipCode As String + + ''' + ''' Invoice amount + ''' + + Public Property InvoiceAmount As Double + + ''' + ''' Net weight of goods + ''' + + Public Property NetWg As Double? + + ''' + ''' Packaging details + ''' + + Public Property PacksAttributes As PacksAttribute() + + ''' + ''' Related documents + ''' + + Public Property ProducedDocumentsAttributes As ProducedDocumentsAttribute() + + ''' + ''' Sender's address + ''' + + Public Property SenderAddress As String + + ''' + ''' Sender's city + ''' + + Public Property SenderCity As String + + ''' + ''' Sender's country code + ''' + + Public Property SenderCountryId As String + + ''' + ''' Sender's name + ''' + + Public Property SenderName As String + + ''' + ''' Sender's postcode + ''' + + Public Property SenderPostcode As String + + ''' + ''' Sender's tax number + ''' + + Public Property SenderTaxno As String + End Class + + Partial Public Class PacksAttribute + ''' + ''' Packaging notes + ''' + + Public Property Notes As String + + ''' + ''' Number of packages + ''' + + Public Property PackCount As Double + + ''' + ''' Pack Code + ''' + + Public Property PackType As String + End Class + + Partial Public Class ProducedDocumentsAttribute + ''' + ''' Document code + ''' + + Public Property Code As String + + ''' + ''' Line NO + ''' + + Public Property Quantity As Double? + + ''' + ''' Additonal Information + ''' + + Public Property Reason As String + + ''' + ''' Document reference + ''' + + Public Property Reference As String + + ''' + ''' Document Type + ''' + + Public Property Scope As String + End Class + + Partial Public Class TransitCountriesAttribute + ''' + ''' Country Code + ''' + + Public Property CountryId As String + End Class + + ''' + ''' Type of the declaration. Should be "ENS". + ''' + Public Enum DocType + Ens + End Enum + + ''' + ''' Transit Method + ''' + Public Enum TransMethod + Air + Rail + Road + RoroAccompanied + RoroUnaccompanied + End Enum + + Partial Public Class ApidogModel + Public Shared Function FromJson(json As String) As ApidogModel + Return JsonConvert.DeserializeObject(Of ApidogModel)(json, Settings) + End Function + End Class + + Public Module Serialize + + Public Function ToJson(Meself As ApidogModel) As String + Return JsonConvert.SerializeObject(Meself, Settings) + End Function + End Module + + Friend Module Converter + Public ReadOnly Settings As JsonSerializerSettings = New JsonSerializerSettings With { + .MetadataPropertyHandling = MetadataPropertyHandling.Ignore, + .DateParseHandling = DateParseHandling.None +} + End Module + + Friend Class DocTypeConverter + Inherits JsonConverter + Public Overrides Function CanConvert(t As Type) As Boolean + Return t Is GetType(DocType) OrElse t Is GetType(DocType?) + End Function + + Public Overrides Function ReadJson(reader As JsonReader, t As Type, existingValue As Object, serializer As JsonSerializer) As Object + If reader.TokenType = JsonToken.Null Then Return Nothing + Dim value = serializer.Deserialize(Of String)(reader) + If value Is "ENS" Then + Return DocType.Ens + End If + Throw New Exception("Cannot unmarshal type DocType") + End Function + + Public Overrides Sub WriteJson(writer As JsonWriter, untypedValue As Object, serializer As JsonSerializer) + If untypedValue Is Nothing Then + serializer.Serialize(writer, Nothing) + Return + End If + Dim value = CType(untypedValue, DocType) + If value = DocType.Ens Then + serializer.Serialize(writer, "ENS") + Return + End If + Throw New Exception("Cannot marshal type DocType") + End Sub + + Public Shared ReadOnly Singleton As DocTypeConverter = New DocTypeConverter() + End Class + + Friend Class TransMethodConverter + Inherits JsonConverter + Public Overrides Function CanConvert(t As Type) As Boolean + Return t Is GetType(TransMethod) OrElse t Is GetType(TransMethod?) + End Function + + Public Overrides Function ReadJson(reader As JsonReader, t As Type, existingValue As Object, serializer As JsonSerializer) As Object + If reader.TokenType = JsonToken.Null Then Return Nothing + Dim value = serializer.Deserialize(Of String)(reader) + Select Case value + Case "air" + Return TransMethod.Air + Case "rail" + Return TransMethod.Rail + Case "road" + Return TransMethod.Road + Case "roro_accompanied" + Return TransMethod.RoroAccompanied + Case "roro_unaccompanied" + Return TransMethod.RoroUnaccompanied + End Select + Throw New Exception("Cannot unmarshal type TransMethod") + End Function + + Public Overrides Sub WriteJson(writer As JsonWriter, untypedValue As Object, serializer As JsonSerializer) + If untypedValue Is Nothing Then + serializer.Serialize(writer, Nothing) + Return + End If + Dim value = CType(untypedValue, TransMethod) + Select Case value + Case TransMethod.Air + serializer.Serialize(writer, "air") + Return + Case TransMethod.Rail + serializer.Serialize(writer, "rail") + Return + Case TransMethod.Road + serializer.Serialize(writer, "road") + Return + Case TransMethod.RoroAccompanied + serializer.Serialize(writer, "roro_accompanied") + Return + Case TransMethod.RoroUnaccompanied + serializer.Serialize(writer, "roro_unaccompanied") + Return + End Select + Throw New Exception("Cannot marshal type TransMethod") + End Sub + + Public Shared ReadOnly Singleton As TransMethodConverter = New TransMethodConverter() + End Class +End Namespace diff --git a/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cOregonNCTSDeclarations.vb b/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cOregonNCTSDeclarations.vb index 6ebae8d8..2d4ed99a 100644 --- a/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cOregonNCTSDeclarations.vb +++ b/VERAG_PROG_ALLGEMEIN/Schnittstellen/Modaltrans/cOregonNCTSDeclarations.vb @@ -1521,6 +1521,9 @@ End Class Public Class cModalAPI + ' Dim ENS As New cModalENS.NctsJsonData + + 'DEV Shared API_STRING As String = "https://modaltrans.com" Shared token As String = "" diff --git a/VERAG_PROG_ALLGEMEIN/Schnittstellen/OPEN_AI/cOpenAI.vb b/VERAG_PROG_ALLGEMEIN/Schnittstellen/OPEN_AI/cOpenAI.vb index 1037ab91..0189ddac 100644 --- a/VERAG_PROG_ALLGEMEIN/Schnittstellen/OPEN_AI/cOpenAI.vb +++ b/VERAG_PROG_ALLGEMEIN/Schnittstellen/OPEN_AI/cOpenAI.vb @@ -22,6 +22,45 @@ Public Class cOpenAI ' Console.ReadKey() End Sub + Shared Function askAI_TruckPlate(text) As String + 'Dim frage As String = "Wie viele Tage braucht ein Versandschein von Istanbul nach München? Gib nur die Zahl zurück." + + 'ASKAI_TruckPlate + + Dim Prompt = "Extrahiere das oder die LKW-Kennzeichen aus folgendem Text. Es handelt sich vorwiegend um Kennzeichen aus der Türkei, Osteuropa und dem Balkan. Die Kennzeichen sind entweder zusammengeschrieben oder enthalten Leerzeichen. + Regeln: + Falls kein Kennzeichen im Text vorhanden ist, gib eine leere Zeichenkette zurück. + Falls genau ein Kennzeichen erkannt wird, gib es ohne Leerzeichen zurück. + Falls genau zwei Kennzeichen erkannt werden, gib sie ohne Leerzeichen durch einen / getrennt zurück. + Falls mehr als zwei Kennzeichen erkannt werden, gib eine leere Zeichenkette zurück. + + Beispiel: + Text: 'Der LKW mit dem Kennzeichen BG 1234 AB wurde kontrolliert, ebenso TR AB123CD' + Erwartete Ausgabe 'BG1234AB/TRAB123CD' + + Text: 'Keine Kennzeichen gefunden' + Erwartete Ausgabe '' + + Text: 'Die Fahrzeuge RO 123 ABC, TR 45ABC67, PL 123456, UA AB 1234' + Erwartete Ausgabe '' + + Text: 'Fahrzeug mit Kennzeichen SRB AB123 und eines mit MD 45 ABC' + Erwartete Ausgabe 'SRBAB123/MD45ABC' + + Hier ist der zu analysierende Text '{TEXT}' + + gib nur das extrahierte Kennzeichen bzw. die leere Zeichenkette zurück, ohne weitere Kommentare oder Erklärungen." + Console.WriteLine("Go..") + Console.WriteLine(Prompt.Replace("{TEXT}", text)) + + ' API-Aufruf und Ausgabe der Antwort + Return AskOpenAI(Prompt.Replace("{TEXT}", text).Replace("""", "\""").Replace(vbCrLf, " ").Replace(vbLf, " ").Replace(vbCr, " ")) + + 'Console.WriteLine("Antwort von OpenAI: " & antwort) + + ' Console.WriteLine("Drücke eine beliebige Taste zum Beenden...") + ' Console.ReadKey() + End Function ' Funktion zur Kommunikation mit OpenAI API (synchrones Verhalten) @@ -56,12 +95,15 @@ Public Class cOpenAI Return answer ' Antwort zurückgeben Else + Console.WriteLine($"Fehler: {response.StatusCode} - {response.Content.ReadAsStringAsync().Result}") + ' Fehlerausgabe bei API-Problemen Return $"Fehler: {response.StatusCode} - {response.Content.ReadAsStringAsync().Result}" End If End Using Catch ex As Exception ' Allgemeine Fehlerbehandlung + Console.WriteLine($"Fehler: {ex.Message}") Return $"Fehler: {ex.Message}" End Try End Function diff --git a/VERAG_PROG_ALLGEMEIN/VERAG_PROG_ALLGEMEIN.vbproj b/VERAG_PROG_ALLGEMEIN/VERAG_PROG_ALLGEMEIN.vbproj index 1020f880..a948c2bf 100644 --- a/VERAG_PROG_ALLGEMEIN/VERAG_PROG_ALLGEMEIN.vbproj +++ b/VERAG_PROG_ALLGEMEIN/VERAG_PROG_ALLGEMEIN.vbproj @@ -362,6 +362,7 @@ +