Imports System.Data.SqlClient
Imports System.Reflection


Public Class cVERAG_in_TRAviso

    Public Property Company As String
    Public Property Department As String
    Public Property PartnerTID As String
    Public Property Reference As String

    Public Property TotPackage As Integer = 0
    Public Property TotGrossWeight As Object = Nothing
    Public Property TransportModeBorder As Object = Nothing
    Public Property TransportLicensePlate As String = ""
    Public Property TransportCountryCode As String = ""
    Public Property TransportBorderType As Object = Nothing
    Public Property FreightCompany As Object = Nothing
    Public Property FreightCompanyID As Object = Nothing
    Public Property Info As String = ""

    Public Property POSITIONS As New List(Of cVERAG_in_TRAviso_shipments)
    Public Property DOCUMENTS As New List(Of cVERAG_in_TRAviso_document)

End Class


Public Class cVERAG_in_TRAviso_Adressen
    Public Property AddressCode As String = ""  '                     VARCHAR (10)  NULL,
    Public Property CustomerID As String = ""  '                     VARCHAR (10)  NULL,
    Public Property EORI As String = ""  '                 VARCHAR (17)  NULL,
    Public Property NLNR As String = ""  '                 DECIMAL (4)   NULL,
    Public Property Name1 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Name2 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Name3 As String = ""  '                     VARCHAR (120) NULL,
    Public Property Street1 As String = ""  '                 VARCHAR (35)  NULL,
    Public Property Street2 As String = ""  '                 VARCHAR (35)  NULL,
    Public Property City As String = ""  '                       VARCHAR (35)  NULL,
    Public Property CountryCode As String = ""  '                       VARCHAR (3)   NULL,
    Public Property PostalCode As String = ""  '                            VARCHAR (9)   NULL,
    Public Property UStID As String = ""  '                  VARCHAR (14)  NULL,

End Class

Public Class cVERAG_in_TRAviso_shipments
    Public Property Nr As Integer = 1
    Public Property Reference As Object = Nothing

    Public Property DeclarationCountry As String = ""
    Public Property DeclarationType As String = ""
    Public Property CustomsCodeDeparture As String = ""
    Public Property CustomsCodeDestination As String = ""
    Public Property CountryCodeDeparture As String = ""
    Public Property CountryCodeDestination As String = ""
    Public Property DEStateCodeDestination As String = ""
    Public Property OriginCountryCode As String = ""
    Public Property GrossWeight As Object = Nothing
    Public Property NetWeight As Object = Nothing
    Public Property NumberOfPackages As Integer = 0
    Public Property KindOfPackages As String = ""
    Public Property Incoterm As String = ""
    Public Property PrePaperCode As String = ""
    Public Property PrePaperNr As String = ""
    Public Property InvoiceAmount As Object = Nothing
    Public Property InvoiceCurrency As String = ""
    Public Property ContainerNr1 As String = ""
    Public Property ContainerNr2 As String = ""
    Public Property ContainerNr3 As String = ""
    Public Property ContainerNr4 As String = ""
    Public Property GoodsDescription As String = ""
    Public Property Info As String = ""

    Public Property DOCUMENTS As New List(Of cVERAG_in_TRAviso_document)
    Public Property ADDRESS As New List(Of cVERAG_in_TRAviso_Adressen)

    Sub New()
    End Sub
End Class


Public Class cVERAG_in_TRAviso_document
    Public Property DocumentType As String = ""
    Public Property DocumentNr As String = ""
    Public Property DocumentDate As Date = CDate("01.01.1800")
    Public Property base64BinaryStream As String = ""
    Public Property FileName As String = ""

    'Shared Function SAVE_toDS() As Integer

    '    Return -1
    'End Function


End Class





Public Class cVERAG_in_TRAvisoFULL

    Property trAv_Id As Integer
    Property trAv_PartnerSystem As Object = Nothing '               VARCHAR(50)  Not NULL,
    Property trAv_datetime As Object = Nothing '                    DATETIME      Default (getdate()) Not NULL,
    Property trAv_Firma As Object = Nothing '                       VARCHAR(50)  Not NULL,
    Property trAv_Niederlassung As Object = Nothing '               VARCHAR(50)  Not NULL,
    Property trAv_Reference As Object = Nothing '                   VARCHAR(500) Not NULL,
    Property trAv_Kennzeichen As Object = Nothing '                 VARCHAR(50)  NULL,
    Property trAv_Nationalitaet As Object = Nothing '               VARCHAR(50)  NULL,
    Property trAv_Avisierer As Object = Nothing '                   VARCHAR(200) NULL,
    Property trAv_AvisiererKdNr As Object = Nothing '               INT           NULL,
    Property trAv_Fraechter As Object = Nothing '                   VARCHAR(200) NULL,
    Property trAv_FraechterKdNr As Object = Nothing '               INT           NULL,
    Property trAv_Grenzueberschreitung As Object = Nothing '        INT           NULL,
    Property trAv_Transportmittel As Object = Nothing '             INT           NULL,
    Property trAv_colli As Object = Nothing '                       FLOAT(53)    NULL,
    Property trAv_gewicht As Object = Nothing '                     FLOAT(53)    NULL,
    Property trAv_VoraussichtlichesEintreffen As Object = Nothing ' Date          NULL,
    Property trAv_Info As Object = Nothing '                   VARCHAR(max) NULL,
    Property trAv_AVISO_einarbeitet As Boolean = False '                   VARCHAR(max) NULL,


    Property ANHAENGE As New List(Of cVERAG_in_TRAvisoAnhaenge)
    'Property SHIPMENTS As New List(Of cVERAG_in_shippment)


    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL



    Sub New()
    End Sub


    Sub New(trAv_Id)
        Me.trAv_Id = trAv_Id
        LOAD()
    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("trAv_Id", trAv_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_PartnerSystem", trAv_PartnerSystem)) '               VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_datetime", trAv_datetime)) '                    DATETIME      Default (getdate()) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Firma", trAv_Firma)) '                       VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Niederlassung", trAv_Niederlassung)) '               VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Reference", trAv_Reference)) '                   VARCHAR(500) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Kennzeichen", trAv_Kennzeichen)) '                 VARCHAR(50)  NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Nationalitaet", trAv_Nationalitaet)) '               VARCHAR(50)  NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Avisierer", trAv_Avisierer)) '                   VARCHAR(200) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_AvisiererKdNr", trAv_AvisiererKdNr)) '               INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Fraechter", trAv_Fraechter)) '                   VARCHAR(200) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_FraechterKdNr", trAv_FraechterKdNr)) '               INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Grenzueberschreitung", trAv_Grenzueberschreitung)) '        INT           NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Transportmittel", trAv_Transportmittel)) '             INT           NULL, 
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_colli", trAv_colli)) '                       FLOAT(53)    NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_gewicht", trAv_gewicht)) '                     FLOAT(53)    NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_VoraussichtlichesEintreffen", trAv_VoraussichtlichesEintreffen)) ' Date          NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_Info", trAv_Info)) '                   VARCHAR(max) NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAv_AVISO_einarbeitet", trAv_AVISO_einarbeitet)) '                   VARCHAR(max) NULL,

        Return list
    End Function



    Public Function SAVE(Optional saveAll = True) As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblTR_Aviso WITH(updlock,serializable) WHERE trAv_Id=@trAv_Id) " &
                       " BEGIN " & getUpdateCmd() & " End " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " End " &
                       " commit tran "

        trAv_Id = SQL.doSQLVarListID(trAv_Id, sqlstr, "FMZOLL", , list)
        If trAv_Id > 0 And saveAll Then
            SAVE_Anhaenge()
            ' SAVE_Sicherheit()
        End If
        Return trAv_Id > 0
    End Function





    Public Sub LOAD(Optional loadALL = True)
        Try
            If loadALL Then
                ANHAENGE.Clear()
            End If
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblTR_Aviso WHERE trAv_Id=@trAv_Id  ", conn)
                    cmd.Parameters.AddWithValue("@trAv_Id", trAv_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
                        If loadALL Then
                            LOAD_Anhaenge()
                        End If
                    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 [tblTR_Aviso] SET " & str & " WHERE trAv_Id=@trAv_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 tblTR_Aviso  (" & 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


    Shared Function getWaehr(s As String, SQL As VERAG_PROG_ALLGEMEIN.SQL) As String
        'Return KDSQL.getValueTxtBySql("SELECT TOP 1 isnull([LandNr],-1) FROM [Länderverzeichnis für die Außenhandelsstatistik] WHERE [Währungscode]='" & s & "'", "FMZOLL")
        s = s.Replace("TRY", "TRL") ' Türkische Lira
        Return SQL.getValueTxtBySql("SELECT TOP 1 isnull([Währungsschlüssel],-1) FROM [Währungstabelle] WHERE [Währungscode]='" & s & "'", "FMZOLL")

    End Function



    Public Sub LOAD_Anhaenge()
        Try
            ANHAENGE.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblTR_AvisoAnheange WHERE trAvAh_trAvId=@trAv_Id  ", conn)
                    cmd.Parameters.AddWithValue("@trAv_Id", Me.trAv_Id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cVERAG_in_TRAvisoAnhaenge
                        For Each i In l.getParameterList()
                            Dim propInfo As PropertyInfo = l.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(l, Nothing)
                            Else
                                propInfo.SetValue(l, dr.Item(i.Text))
                            End If
                        Next
                        'l.LOAD_Packstücke()
                        ANHAENGE.Add(l)
                    End While
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
    End Sub




    Function DELETE_Anhaenge() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblTR_AvisoAnheange] WHERE trAvAh_trAvId =" & Me.trAv_Id & " "
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Public Function SAVE_Anhaenge() As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        If DELETE_Anhaenge() Then 'And DELETE_PositionsdatenUnterlagen() And DELETE_PositionsdatenVerbrauchssteuer() Then
            'jetzt die Standartofferten  einfügen
            For Each p In ANHAENGE
                p.trAvAh_trAvId = trAv_Id
                If p.INSERT() Then

                Else
                    Return False
                End If
            Next
        Else
            Return False
        End If

        Return True
    End Function


End Class


Public Class cVERAG_in_TRAvisoAnhaenge

    Property trAvAh_id As Integer
    Property trAvAh_trAvId As Integer
    Property DocumentType As String = ""
    Property DocumentNr As String = ""
    Property DocumentDate As Date = CDate("01.01.1800")
    Property trAvAh_docID As Integer

    Sub New()

    End Sub

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    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("trAvAh_id", trAvAh_id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAvAh_trAvId", trAvAh_trAvId)) '               VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentType", DocumentType)) '                    DATETIME      Default (getdate()) Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentNr", DocumentNr)) '                       VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DocumentDate", DocumentDate)) '               VARCHAR(50)  Not NULL,
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("trAvAh_docID", trAvAh_docID)) '                   VARCHAR(500) Not NULL, 

        Return list
    End Function


    Public Function INSERT() As Boolean
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        trAvAh_id = SQL.doSQLVarListID(trAvAh_id, getInsertCmd, "FMZOLL", , list)
        Return trAvAh_id > 0
    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 tblTR_AvisoAnheange  (" & 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
