Imports System.Data.SqlClient
Imports System.Reflection
Imports System.Runtime.InteropServices.ComTypes
Imports com.sun.tools.javac.util
Imports java.lang.ProcessBuilder
Imports sun.util.resources.cldr.aa
Imports VERAG_PROG_ALLGEMEIN.TESTJSON
' ==========================================================================
' MASTER CLASS
' Customs Declaration (Header + Items + Duties + Documents)
' ==========================================================================
Public Class cVERAG_CustomsDeclarations
Public Property za_Id As Integer
Public Property za_System As String ' z.B. "DAKOSY"
Public Property za_CustomsSystem As String ' z.B. "ATLAS"
Public Property za_CustomsSystemCountry As String ' z.B. "DE"
Public Property za_IsExternalSystem As Boolean = False
Public Property za_IsFinalDeclaration As Boolean = False
Public Property za_UCR As String
Public Property za_Mandant_ID As String
Public Property za_Bereich_ID As String
Public Property za_Firma As String
Public Property za_Niederlassung As String
Public Property za_MRN As String
Public Property za_LRN As String
Public Property za_DeclarationNo As String
Public Property za_ReferenceCustomer As String
Public Property za_REGIME As String ' IMPORT, EXPORT, ...
Public Property za_RegistrationType As String
Public Property za_MainProcedure As String
Public Property za_AdditionalProcedure As String
Public Property za_DeclarationDate As Date?
Public Property za_ReleaseDate As Date?
Public Property za_AcceptanceDate As Date?
Public Property za_CustomsOffice As String
Public Property za_EntryCustomsOffice As String
Public Property za_RepresentationCode As String
Public Property za_TransportModeInland As String
Public Property za_TransportModeBorder As String
Public Property za_MeansOfTransport_Vehicle As String
Public Property za_MeansOfTransport_Type As String
Public Property za_MeansOfTransport_Nationality As String
Public Property za_ContainerNr1 As String
Public Property za_ContainerNr2 As String
Public Property za_ContainerNr3 As String
Public Property za_ContainerNr4 As String
Public Property za_ContainerNr5 As String
Public Property za_ContainerNr6 As String
Public Property za_ContainerNr7 As String
Public Property za_ContainerNr8 As String
Public Property za_ContainerNr9 As String
Public Property za_CountryDispatch As String
Public Property za_CountryDestination As String
Public Property za_CountryDestinationState As String
Public Property za_CountryImport As String
Public Property za_WarehouseCode As String
Public Property za_Incoterms As String
Public Property za_IncotermsPlace As String
Public Property za_InvoiceAmount As Decimal?
Public Property za_InvoiceCurrency As String
Public Property za_FiscalRepFlag As Boolean?
Public Property za_TaxDeduction As Boolean?
Public Property za_ApplicantTaxOffice As String
Public Property za_ApplicantVATNo As String
Public Property za_DV1Flag As Boolean?
Public Property za_PrevDocument_Type As String
Public Property za_PrevDocument_No As String
Public Property za_AvisoId As Integer?
Public Property za_SendungsId As Integer?
Public Property za_Remarks As String
Public Property za_Sachbearbeiter As String
Public Property za_SachbearbeiterId As String
Public Property za_TotGrossMass As Decimal?
Public Property Parties As New List(Of cVERAG_CustomsDeclarations_Parties)
Public Property Items As New List(Of cVERAG_CustomsDeclarations_Item)
Public Property Duties As New List(Of cVERAG_CustomsDeclarations_Duty)
Public Property Documents As New List(Of cVERAG_CustomsDeclarations_Document)
Public Property hasEntry As Boolean = False
Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
Public Function getParameterList() As List(Of SQLVariable)
Return New List(Of SQLVariable) From {
New SQLVariable("za_Id", za_Id,, True),
New SQLVariable("za_System", za_System),
New SQLVariable("za_CustomsSystem", za_CustomsSystem),
New SQLVariable("za_CustomsSystemCountry", za_CustomsSystemCountry),
New SQLVariable("za_IsExternalSystem", za_IsExternalSystem),
New SQLVariable("za_IsFinalDeclaration", za_IsFinalDeclaration),
New SQLVariable("za_UCR", za_UCR),
New SQLVariable("za_Mandant_ID", za_Mandant_ID),
New SQLVariable("za_Bereich_ID", za_Bereich_ID),
New SQLVariable("za_Firma", za_Firma),
New SQLVariable("za_Niederlassung", za_Niederlassung),
New SQLVariable("za_MRN", za_MRN),
New SQLVariable("za_LRN", za_LRN),
New SQLVariable("za_DeclarationNo", za_DeclarationNo),
New SQLVariable("za_ReferenceCustomer", za_ReferenceCustomer),
New SQLVariable("za_REGIME", za_REGIME),
New SQLVariable("za_RegistrationType", za_RegistrationType),
New SQLVariable("za_MainProcedure", za_MainProcedure),
New SQLVariable("za_AdditionalProcedure", za_AdditionalProcedure),
New SQLVariable("za_DeclarationDate", za_DeclarationDate),
New SQLVariable("za_ReleaseDate", za_ReleaseDate),
New SQLVariable("za_AcceptanceDate", za_AcceptanceDate),
New SQLVariable("za_CustomsOffice", za_CustomsOffice),
New SQLVariable("za_EntryCustomsOffice", za_EntryCustomsOffice),
New SQLVariable("za_RepresentationCode", za_RepresentationCode),
New SQLVariable("za_TransportModeInland", za_TransportModeInland),
New SQLVariable("za_TransportModeBorder", za_TransportModeBorder),
New SQLVariable("za_MeansOfTransport_Vehicle", za_MeansOfTransport_Vehicle),
New SQLVariable("za_MeansOfTransport_Type", za_MeansOfTransport_Type),
New SQLVariable("za_MeansOfTransport_Nationality", za_MeansOfTransport_Nationality),
New SQLVariable("za_ContainerNr1", za_ContainerNr1),
New SQLVariable("za_ContainerNr2", za_ContainerNr2),
New SQLVariable("za_ContainerNr3", za_ContainerNr3),
New SQLVariable("za_ContainerNr4", za_ContainerNr4),
New SQLVariable("za_ContainerNr5", za_ContainerNr5),
New SQLVariable("za_ContainerNr6", za_ContainerNr6),
New SQLVariable("za_ContainerNr7", za_ContainerNr7),
New SQLVariable("za_ContainerNr8", za_ContainerNr8),
New SQLVariable("za_ContainerNr9", za_ContainerNr9),
New SQLVariable("za_CountryDispatch", za_CountryDispatch),
New SQLVariable("za_CountryDestination", za_CountryDestination),
New SQLVariable("za_CountryDestinationState", za_CountryDestinationState),
New SQLVariable("za_CountryImport", za_CountryImport),
New SQLVariable("za_WarehouseCode", za_WarehouseCode),
New SQLVariable("za_Incoterms", za_Incoterms),
New SQLVariable("za_IncotermsPlace", za_IncotermsPlace),
New SQLVariable("za_InvoiceAmount", za_InvoiceAmount),
New SQLVariable("za_InvoiceCurrency", za_InvoiceCurrency),
New SQLVariable("za_FiscalRepFlag", za_FiscalRepFlag),
New SQLVariable("za_TaxDeduction", za_TaxDeduction),
New SQLVariable("za_ApplicantTaxOffice", za_ApplicantTaxOffice),
New SQLVariable("za_ApplicantVATNo", za_ApplicantVATNo),
New SQLVariable("za_DV1Flag", za_DV1Flag),
New SQLVariable("za_PrevDocument_Type", za_PrevDocument_Type),
New SQLVariable("za_PrevDocument_No", za_PrevDocument_No),
New SQLVariable("za_AvisoId", za_AvisoId),
New SQLVariable("za_SendungsId", za_SendungsId),
New SQLVariable("za_Remarks", za_Remarks),
New SQLVariable("za_Sachbearbeiter", za_Sachbearbeiter),
New SQLVariable("za_SachbearbeiterId", za_SachbearbeiterId),
New SQLVariable("za_TotGrossMass", za_TotGrossMass)
}
End Function
Public Function getInsertCmd() As String
Dim f As String = "", v As String = ""
For Each i In getParameterList()
If Not i.isPrimaryParam Then
f &= "[" & i.Text & "],"
v &= "@" & i.Scalarvariable & ","
End If
Next
Return "INSERT INTO tblVERAG_CustomsDeclarations (" &
f.TrimEnd(","c) & ") VALUES (" & v.TrimEnd(","c) & ")"
End Function
Public Function getUpdateCmd() As String
Dim s As String = ""
For Each i In getParameterList()
If Not i.isPrimaryParam Then
s &= "[" & i.Text & "]=@" & i.Scalarvariable & ","
End If
Next
Return "UPDATE tblVERAG_CustomsDeclarations SET " &
s.TrimEnd(","c) & " WHERE za_Id=@za_Id"
End Function
Public Sub LOAD()
Try
hasEntry = False
Using conn = SQL.GetNewOpenConnectionFMZOLL()
Using cmd As New SqlCommand(
"SELECT * FROM tblVERAG_CustomsDeclarations WHERE za_Id=@_BASE_id", conn)
cmd.Parameters.AddWithValue("@_BASE_id", za_Id)
Dim dr = cmd.ExecuteReader()
If dr.Read Then
For Each li In getParameterList()
Dim pi = Me.GetType.GetProperty(li.Scalarvariable)
If dr.Item(li.Text) Is DBNull.Value Then
pi.SetValue(Me, Nothing)
Else
pi.SetValue(Me, dr.Item(li.Text))
End If
Next
hasEntry = True
End If
dr.Close()
End Using
End Using
Parties = cVERAG_CustomsDeclarations_Parties.LOAD_BY_ZAID(za_Id)
Items = cVERAG_CustomsDeclarations_Item.LOAD_BY_ZAID(za_Id)
Duties = cVERAG_CustomsDeclarations_Duty.LOAD_BY_ZAID(za_Id)
Documents = cVERAG_CustomsDeclarations_Document.LOAD_HEAD_BY_ZAID(za_Id)
Catch ex As Exception
cErrorHandler.ERR(ex.Message, ex.StackTrace, MethodInfo.GetCurrentMethod.Name)
End Try
End Sub
Public Shared Function LOAD_List_CBAM(EORI() As String, ImportCountry As String, datFrom As Date, datTo As Date, Optional CustomsSystem As String = "", Optional loadAll As Boolean = True, Optional indirect As Boolean = False) As List(Of cVERAG_CustomsDeclarations)
Dim LIST As New List(Of cVERAG_CustomsDeclarations)
Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
Try
' ------------------------------------------------------------
' CBAM Pattern (HSCode)
' ------------------------------------------------------------
Dim cbamWhere As String =
VERAG_PROG_ALLGEMEIN.cGreendeal_CBAM_Trn.BuildCBAMPatternWhereClause("i.zaItem_HSCode")
' ------------------------------------------------------------
' SQL: ZA-IDs ermitteln
' ------------------------------------------------------------
Dim eoriIn As String =
String.Join(",", EORI.
Where(Function(x) Not String.IsNullOrWhiteSpace(x)).
Select(Function(x) "'" & x.Replace("'", "''") & "'"))
Dim sqlstr As String =
"SELECT DISTINCT z.* " &
"FROM VERAG.dbo.tblVERAG_CustomsDeclarations z " &
"INNER JOIN VERAG.dbo.tblVERAG_CustomsDeclarations_Items i " &
" ON z.za_Id = i.zaItem_zaId " &
"LEFT JOIN VERAG.dbo.tblVERAG_CustomsDeclarations_Parties p " &
" ON z.za_Id = p.zaParty_zaId " &
"WHERE z.za_REGIME = 'IMPORT' " &
" AND z.za_IsFinalDeclaration = 1 " &
" AND cast(z.za_DeclarationDate as date) >= @dateFrom " &
" AND cast(z.za_DeclarationDate as date) <= @dateTo " &
If(eoriIn <> "", " AND p.zaParty_EORI IN (" & eoriIn & ") ", "") &
" AND " & cbamWhere
If indirect Then
sqlstr &= " AND (
(za_System IN('DHF','DHF_UNISPED') AND za_RepresentationCode=3) OR
(za_System IN('TELOTEC') AND za_RepresentationCode=3) OR
(za_System IN('DAKOSY') AND za_RepresentationCode=2)
)"
End If
If ImportCountry <> "" Then
sqlstr &= " AND z.za_CountryImport = @importCountry "
End If
If CustomsSystem <> "" Then
sqlstr &= " AND z.za_System = @sys "
End If
Using conn = SQL.GetNewOpenConnectionFMZOLL()
Using cmd As New SqlCommand(sqlstr, conn)
' cmd.Parameters.AddWithValue("@eori", EORI)
cmd.Parameters.AddWithValue("@dateFrom", datFrom.ToShortDateString)
cmd.Parameters.AddWithValue("@dateTo", datTo.ToShortDateString)
If ImportCountry <> "" Then
cmd.Parameters.AddWithValue("@importCountry", ImportCountry)
End If
If CustomsSystem <> "" Then
cmd.Parameters.AddWithValue("@sys", CustomsSystem)
End If
Using dr = cmd.ExecuteReader()
While dr.Read()
Dim CD As New cVERAG_CustomsDeclarations
CD.hasEntry = False
' ------------------------------------------------
' DIREKTE BEFÜLLUNG (wie gewünscht)
' ------------------------------------------------
For Each li In CD.getParameterList()
Dim pi = CD.GetType().GetProperty(li.Scalarvariable)
If pi Is Nothing Then Continue For
If dr.Item(li.Text) Is DBNull.Value Then
pi.SetValue(CD, Nothing)
Else
pi.SetValue(CD, dr.Item(li.Text))
End If
Next
CD.hasEntry = True
' ------------------------------------------------
' OPTIONALES NACHLADEN
' ------------------------------------------------
If loadAll Then
CD.Parties = cVERAG_CustomsDeclarations_Parties.LOAD_BY_ZAID(CD.za_Id)
CD.Items = cVERAG_CustomsDeclarations_Item.LOAD_BY_ZAID(CD.za_Id)
CD.Duties = cVERAG_CustomsDeclarations_Duty.LOAD_BY_ZAID(CD.za_Id)
CD.Documents = cVERAG_CustomsDeclarations_Document.LOAD_HEAD_BY_ZAID(CD.za_Id)
End If
LIST.Add(CD)
End While
End Using
End Using
End Using
Catch ex As Exception
cErrorHandler.ERR(ex.Message, ex.StackTrace, Reflection.MethodInfo.GetCurrentMethod.Name)
End Try
Return LIST
End Function
Public Shared Function loadByMRN(MRN As String, loadall As Boolean) As cVERAG_CustomsDeclarations
Dim ZA As New cVERAG_CustomsDeclarations
If If(MRN, "") = "" Then Return ZA
Try
ZA.hasEntry = False
Using conn = SQL.GetNewOpenConnectionFMZOLL()
Using cmd As New SqlCommand(
"SELECT * FROM tblVERAG_CustomsDeclarations WHERE za_MRN=@_BASE_id", conn)
cmd.Parameters.AddWithValue("@_BASE_id", MRN)
Dim dr = cmd.ExecuteReader()
If dr.Read Then
For Each li In ZA.getParameterList()
Dim pi = ZA.GetType.GetProperty(li.Scalarvariable)
If dr.Item(li.Text) Is DBNull.Value Then
pi.SetValue(ZA, Nothing)
Else
pi.SetValue(ZA, dr.Item(li.Text))
End If
Next
ZA.hasEntry = True
End If
dr.Close()
End Using
End Using
If loadall Then
ZA.Parties = cVERAG_CustomsDeclarations_Parties.LOAD_BY_ZAID(ZA.za_Id)
ZA.Items = cVERAG_CustomsDeclarations_Item.LOAD_BY_ZAID(ZA.za_Id)
ZA.Duties = cVERAG_CustomsDeclarations_Duty.LOAD_BY_ZAID(ZA.za_Id)
ZA.Documents = cVERAG_CustomsDeclarations_Document.LOAD_HEAD_BY_ZAID(ZA.za_Id)
End If
Catch ex As Exception
cErrorHandler.ERR(ex.Message, ex.StackTrace, MethodInfo.GetCurrentMethod.Name)
End Try
Return ZA
End Function
Public Sub checkfreshCBAMWarning()
Try
Dim CBAM_ITEMS As New List(Of cVERAG_CustomsDeclarations_Item)
If hasCBAM(CBAM_ITEMS) Then
Dim importerEORI As String = Me.Parties.Where(Function(p) {"CN", "IM", "IMP", "IMPORTER"}.Contains(p.zaParty_Role)).Select(Function(p) p.zaParty_EORI).FirstOrDefault()
Dim ImporterString As String = Me.Parties.Where(Function(p) {"CZ", "EX", "EXP", "EXPORTER"}.Contains(p.zaParty_Role)).Select(Function(p) p.zaParty_Name).FirstOrDefault()
Dim ExporterEORI As String = Me.Parties.Where(Function(p) {"CZ", "EX", "EXP", "EXPORTER"}.Contains(p.zaParty_Role)).Select(Function(p) p.zaParty_EORI).FirstOrDefault()
Dim ExporterString As String = Me.Parties.Where(Function(p) {"CZ", "EX", "EXP", "EXPORTER"}.Contains(p.zaParty_Role)).Select(Function(p) p.zaParty_Name).FirstOrDefault()
Dim KD As cKunde = Nothing
Dim AD As cAdressen = Nothing
If If(importerEORI, "") <> "" Then
KD = cKunde.LOAD_ByEORI(importerEORI, If(za_Firma, ""))
If KD Is Nothing Then KD = cKunde.LOAD_ByEORI(importerEORI) 'Wenn kein Kunde mit Firma gefunden wurde, nur nach EORI suchen (default VERAG)
AD = New cAdressen(KD.KundenNr)
End If
If AD IsNot Nothing Then
ImporterString = AD.Name_1 & " " & If(AD.Name_2, "") & "
" & If(AD.LandKz, "") & " " & If(AD.PLZ, "") & " " & If(AD.Ort, "")
End If
Dim KD_EX As cKunde = Nothing
Dim AD_EX As cAdressen = Nothing
If If(ExporterEORI, "") <> "" Then
KD_EX = cKunde.LOAD_ByEORI(ExporterEORI, If(za_Firma, ""))
If KD_EX Is Nothing Then KD_EX = cKunde.LOAD_ByEORI(ExporterEORI) 'Wenn kein Kunde mit Firma gefunden wurde, nur nach EORI suchen (default VERAG)
AD_EX = New cAdressen(KD_EX.KundenNr)
End If
If AD_EX IsNot Nothing Then
ExporterString = AD_EX.Name_1 & " " & If(AD_EX.Name_2, "") & "
" & If(AD_EX.LandKz, "") & " " & If(AD_EX.PLZ, "") & " " & If(AD_EX.Ort, "")
End If
If isIndirect() Then
'==================>> INDIRECT <<=========================
Dim itemsHtml As String =
String.Join("", CBAM_ITEMS.Select(Function(i) "
" &
"Bei der Zollanmeldung mit der MRN " & Me.za_MRN & " wurden CBAM-relevante Warenpositionen erkannt.
" &
"Bitte prüfen Sie diese Anmeldung im Hinblick auf die CBAM-Meldepflicht." &
"
| PosNr | " & "HS-Code | " & "Beschreibung | " & "
|---|
" &
"Importeur:
" &
If(importerEORI, "") & "
" & If(ImporterString, "") &
"
" &
"Exporteur:
" &
If(ExporterEORI, "") & " – " & vbNewLine &
If(AD_EX Is Nothing, ExporterString, AD_EX.Name_1 & " " & If(AD_EX.Name_2, "") & "
" &
If(AD_EX.LandKz, "") & " " & If(AD_EX.PLZ, "") & " " & If(AD_EX.Ort, "")) &
"
VERAG – CBAM Compliance Services
" & "" VERAG_PROG_ALLGEMEIN.cProgramFunctions.sendMail("al@verag.ag", "CBAM WARNUNG - " & Me.za_MRN, bodyHtml, "cbam@verag.ag") 'VERAG_PROG_ALLGEMEIN.cProgramFunctions.sendMail("al@verag.ag", "CBAM WARNUNG", ' "CBAM-Warnmeldung bei Zollanmeldung " & Me.za_MRN & vbNewLine & ' "Bei der Zollanmeldung mit der MRN " & Me.za_MRN & " wurden CBAM-relevante Warenpositionen erkannt." & vbCrLf & ' "Bitte prüfen Sie diese Anmeldung im Hinblick auf die CBAM-Meldepflicht." & vbCrLf & vbCrLf & ' "Erkannte CBAM-Positionen:" & vbCrLf & ' String.Join(vbCrLf, CBAM_ITEMS.Select(Function(i) " - PosNr: " & i.zaItem_PosNo.ToString & " | HS-Code: " & If(i.zaItem_HSCode, "") & " | Beschreibung: " & If(i.zaItem_Description, ""))) ' ) ''========================================================= Else '==================>> DIRECT <<========================= Try If KD IsNot Nothing Then Dim ERW As New cKundenErweitert(KD.KundenNr) Select Case ERW.kde_CBAM_Status Case "", "50to" ' Nur hier wird eine Warnung gesendet!! -> WEnn der Kunde bereits Registriert ist, nicht notwendig. Dim dateCBAM = If(If(za_ReleaseDate, za_DeclarationDate), Now) Dim eigenmasseSumme_Shipment As Decimal = CBAM_ITEMS.Sum(Function(i) If(i.zaItem_NetMass, 0D)) / 1000 Dim eigenmasseSumme_YEAR As Decimal = getCBAM_KG(importerEORI, CDate("01.01." & dateCBAM.Year), CDate("31.12." & dateCBAM.Year), za_MRN) / 1000 Dim WarnLevel As String = "" Dim Schwellenwert As Decimal = 0 Dim Warn1 As Decimal = If(If(ERW.kde_CBAM_Warn1, 0) > 0, ERW.kde_CBAM_Warn1, 30) Dim Warn2 As Decimal = If(If(ERW.kde_CBAM_Warn2, 0) > 0, ERW.kde_CBAM_Warn2, 40) Dim Warn50to As Decimal = 50 'geprüft werden die bisherigen CBAM Waren im Jahr + die aktuelle Sendung If eigenmasseSumme_YEAR < Warn50to And (eigenmasseSumme_Shipment + eigenmasseSumme_YEAR) >= Warn50to Then '--> WARN 2 überschritten WarnLevel = "3" Schwellenwert = 50 End If 'geprüft werden die bisherigen CBAM Waren im Jahr + die aktuelle Sendung If eigenmasseSumme_YEAR < Warn2 And (eigenmasseSumme_Shipment + eigenmasseSumme_YEAR) >= Warn2 Then '--> WARN 2 überschritten WarnLevel = "2" Schwellenwert = Warn2 End If 'geprüft werden die bisherigen CBAM Waren im Jahr + die aktuelle Sendung If eigenmasseSumme_YEAR < Warn1 And (eigenmasseSumme_Shipment + eigenmasseSumme_YEAR) >= Warn1 Then '--> WARN 1 überschritten WarnLevel = "1" Schwellenwert = Warn1 End If '===========>>>> WARNUNG <<<<<<<================================= If WarnLevel <> "" Then Dim bodyHtml As String = "" & "" &
"Laut unserem System wurden in der Zollanmeldung mit der MRN " & Me.za_MRN & " " &
"CBAM-pflichtige Warenpositionen erkannt.
" &
"Für Ihr Unternehmen liegen uns derzeit keine Informationen über eine gültige CBAM-Registrierung vor." &
"
| Warnstufe | " & WarnLevel & If(WarnLevel = 3, " - ÜBERSCHREITUNG!", "") & " |
| Schwellenwert | " & Schwellenwert.ToString("N0") & " Tonnen |
" &
"Importeur:
" &
If(importerEORI, "") & "
" & If(ImporterString, "") &
"
" &
"Exporteur:
" &
If(ExporterEORI, "") & "
" & If(ExporterString, "") &
"
WICHTIG
" & "" &
"Bei Überschreitung einer jährlichen Gesamtmenge von 50 Tonnen CBAM-pflichtiger Waren (Jahressumme) " &
"ist eine gültige CBAM-Registrierung zwingend erforderlich.
" &
"Ohne gültige Registrierung kann es zu Zollblockaden oder Abfertigungsstopps kommen." &
"
" & "Bitte teilen Sie uns kurzfristig unter " & "cbam@verag.ag mit, wie Sie weiter vorgehen möchten " & "(z. B. bestehende Registrierung, Beantragung mit VERAG oder Bevollmächtigung)." & "
" & "VERAG DCS – CBAM Compliance Services
" & "VERAG Spedition AG
A-4975 Suben, Suben 100