Imports System.Data.SqlClient
Imports System.Reflection
Imports VERAG_PROG_ALLGEMEIN

Public Class cDakosy_Zollanmeldungen

    Property dy_Id As Integer = -1
    Property dy_ART As String = ""
    Property dy_BezugsNr As String = ""
    Property dy_Status As Integer
    Property dy_Status_KEWILL_Equivalent As Integer
    Property dy_AvisoId As Object = Nothing
    Property dy_SendungsId As Object = Nothing
    Property dy_Erstellung As DateTime = Now
    Property dy_Erstellung_SB As Object = Nothing
    Property dy_LetzteBearbeitung As DateTime = Now
    Property dy_LetzteBearbeitung_SB As Object = Nothing
    Property dy_Steuerbescheid_dsId As Object = Nothing
    Property dy_VBD_dsId As Object = Nothing
    Property dy_ABD_dsId As Object = Nothing
    Property dy_AVM_dsId As Object = Nothing

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    'Sub New()
    ' End Sub

    Sub New(dy_ART, dy_BezugsNr, dy_AvisoId, dy_SendungsId)
        Me.dy_ART = dy_ART
        Me.dy_BezugsNr = dy_BezugsNr
        Me.dy_AvisoId = dy_AvisoId
        Me.dy_SendungsId = dy_SendungsId
        Me.dy_Erstellung_SB = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        Me.dy_LetzteBearbeitung_SB = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
        SAVE()
    End Sub

    Sub New()
    End Sub


    Sub New(dy_Id)
        Me.dy_Id = dy_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("dy_Id", dy_Id, , True, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_ART", dy_ART))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_BezugsNr", dy_BezugsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status", dy_Status))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status_KEWILL_Equivalent", dy_Status_KEWILL_Equivalent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_AvisoId", dy_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_SendungsId", dy_SendungsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Erstellung", dy_Erstellung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Erstellung_SB", dy_Erstellung_SB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_LetzteBearbeitung", dy_LetzteBearbeitung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_LetzteBearbeitung_SB", dy_LetzteBearbeitung_SB))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Steuerbescheid_dsId", dy_Steuerbescheid_dsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_VBD_dsId", dy_VBD_dsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_ABD_dsId", dy_ABD_dsId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_AVM_dsId", dy_AVM_dsId))

        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 tblDakosy_Zollanmeldungen WITH(updlock,serializable) WHERE dy_Id=@dy_Id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

        dy_Id = SQL.doSQLVarListID(dy_Id, sqlstr, "FMZOLL", , list)
        Return dy_Id > 0
    End Function


    Public Function tryFindAviso() As Boolean
        Dim srchStrFiliale = ""
        Dim srchStrAbf = ""
        If Me.dy_BezugsNr.Contains("/") Then
            Dim split = Me.dy_BezugsNr.Split("/")
            If split.Length >= 2 Then
                srchStrFiliale = split(0)

                'Wenn Abfertigungsnummer korrekt gefunden wurde...
                If IsNumeric(split(1)) Then
                    srchStrAbf = split(1)
                Else
                    Dim tmp = ""
                    'Bis zum ersten Buchstaben...
                    For Each s In split(1)
                        If IsNumeric(s) Then : tmp &= s
                        Else : Exit For : End If
                    Next
                    srchStrAbf = tmp
                End If

                ' ElseIf  Then
                '      srchStrAbf = split(X)
            Else
                ' Return False
            End If

        End If

        If IsNumeric(srchStrFiliale) AndAlso IsNumeric(srchStrAbf) AndAlso srchStrFiliale.Length = 4 Then
            'If srchStrAbf.Length > 8 Then srchStrAbf = srchStrAbf.Substring(0, 8)
            If srchStrAbf.Length < 8 Then srchStrAbf = Me.dy_Erstellung.ToString("yy") & CInt(srchStrAbf).ToString("000000")

            Dim sqlstr = " select [tblSnd_SendungID],[tblSnd_AvisoID]  FROM [tblSendungen] where '" & srchStrAbf & "' like ('%' + cast(AbfertigungsNr as varchar(10))+ '%') and FilialenNr ='" & srchStrFiliale & "' and AbfertigungsNr >0"

            Dim dt2 As DataTable = SQL.loadDgvBySql(sqlstr, "AVISO")
            If dt2 IsNot Nothing AndAlso dt2.Rows.Count > 0 Then
                Me.dy_SendungsId = dt2.Rows(0)("tblSnd_SendungID")
                Me.dy_AvisoId = dt2.Rows(0)("tblSnd_AvisoID")
                Return True
            End If
        End If

        'UNISPED:
        Dim sqlstr2 = " select [tblSnd_SendungID],[tblSnd_AvisoID]  FROM [tblSendungen] where '" & Me.dy_BezugsNr & "' like (  cast(tblSnd_DakosyRef as varchar(20))+ '%') AND tblSnd_DakosyRef is not null AND len(tblSnd_DakosyRef) > 10  "
        Dim dt = SQL.loadDgvBySql(sqlstr2, "AVISO")
        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            Me.dy_SendungsId = dt.Rows(0)("tblSnd_SendungID")
            Me.dy_AvisoId = dt.Rows(0)("tblSnd_AvisoID")
            Return True
        End If

        Return False
    End Function


    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblDakosy_Zollanmeldungen WHERE dy_Id=@dy_Id  ", conn)
                    cmd.Parameters.AddWithValue("@dy_Id", dy_Id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(l.Text))
                            End If

                        Next

                    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 UPDATE_Status(status As Integer) As String
        If dy_Id > 0 Then
            Try
                Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Id", dy_Id, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status_KEWILL_Equivalent", status, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status", status, , True))
                dy_Status_KEWILL_Equivalent = status
                dy_Status = status

                Return SQL.doSQLVarList(" UPDATE [tblDakosy_Zollanmeldungen] SET dy_Status_KEWILL_Equivalent=@dy_Status_KEWILL_Equivalent,dy_Status=@dy_Status WHERE dy_Id=@dy_Id ", "FMZOLL", , list)

            Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            End Try
        End If
        Return ""
    End Function


    Shared Function UPDATE_StatusById(dy_Id As Integer, status As Integer) As String
        If dy_Id > 0 Then
            Try
                Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Id", dy_Id, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status_KEWILL_Equivalent", status, , True))
                list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("dy_Status", status, , True))
                'dy_Status_KEWILL_Equivalent = status
                'dy_Status = status
                Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

                Return SQL.doSQLVarList(" UPDATE [tblDakosy_Zollanmeldungen] SET dy_Status_KEWILL_Equivalent=@dy_Status_KEWILL_Equivalent,dy_Status=@dy_Status WHERE dy_Id=@dy_Id ", "FMZOLL", , list)

            Catch ex As Exception
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            End Try
        End If
        Return ""
    End Function



    Shared Function LOADById(dy_Id As String) As cDakosy_Zollanmeldungen
        Try
            Dim NCTS As New cDakosy_Zollanmeldungen
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDakosy_Zollanmeldungen WHERE dy_Id=@dy_Id  ", conn)
                    cmd.Parameters.AddWithValue("@dy_Id", dy_Id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In NCTS.getParameterList()
                            Dim propInfo As PropertyInfo = NCTS.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(NCTS, Nothing)
                            Else
                                propInfo.SetValue(NCTS, dr.Item(l.Text))
                            End If

                        Next

                        dr.Close()
                        Return NCTS
                    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
        Return Nothing
    End Function


    Shared Function LOADByBezugsNr(bezugsNr As String) As cDakosy_Zollanmeldungen
        Try
            Dim NCTS As New cDakosy_Zollanmeldungen
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
            If bezugsNr = "" Then Return Nothing

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDakosy_Zollanmeldungen WHERE dy_BezugsNr=@bezugsNr  ", conn)
                    cmd.Parameters.AddWithValue("@bezugsNr", bezugsNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each l In NCTS.getParameterList()
                            Dim propInfo As PropertyInfo = NCTS.GetType.GetProperty(l.Scalarvariable)

                            If dr.Item(l.Text) Is DBNull.Value Then
                                propInfo.SetValue(NCTS, Nothing)
                            Else
                                propInfo.SetValue(NCTS, dr.Item(l.Text))
                            End If

                        Next

                        dr.Close()
                        Return NCTS
                    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)
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)

        End Try
        Return Nothing
    End Function

    Shared Function ExistsBezugsNr(bezugsNr As String) As Boolean
        Try
            Dim NCTS As New cDakosy_Zollanmeldungen
            Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 dy_id FROM tblDakosy_Zollanmeldungen WHERE dy_BezugsNr=@bezugsNr  ", conn)
                    cmd.Parameters.AddWithValue("@bezugsNr", bezugsNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        Return 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
        Return False
    End Function

    Public Sub UPDATE_AVISO_SND(dy_SendungsId)

        'Beim Senden soll die Sendung überhnommen + tblSnd_VG_MA gesetzt werden --> damit gleich gesehen wird, dass der Akt in Bearbeitung ist.
        If dy_SendungsId IsNot Nothing AndAlso dy_SendungsId > 0 Then
            Dim SND As New VERAG_PROG_ALLGEMEIN.cSendungen(dy_SendungsId)
            If SND.tblSnd_VG_MA <> VERAG_PROG_ALLGEMEIN.cAllgemein.USRID Then ' Nur wenn nicht bereits übernommen
                SND.tblSnd_VG_MA = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
                SND.LetzterMitarbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
                SND.LetzterMitarbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
                If Not VERAG_PROG_ALLGEMEIN.cAllgemein.isCLUSTER Then ' nur, wenn BO, dann darf das Team geändert werden... Damit das zuständige Team nachvollzeiehn kann, was mit dem LKW ist...
                    SND.tblSnd_TeamId = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_teamId
                End If
                If SND.SAVE() Then
                    Dim AvisoDAL As New cAvisoDAL
                    AvisoDAL.addAenderung(SND.tblSnd_AvisoID, "Sendung übernommen", "Sendung Nr. " & SND.tblSnd_PosUnterNr & " übernommen von " & VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME & " am " & Format(Now, "dd.MM.yyyy HH:mm") & " (AUTO - DY/Send)")
                End If
            End If
        End If

    End Sub
    Public Sub UPDATE_DATA()
        Try
            Select Case dy_ART
                Case "T1", "T2"
                    'SQL, die daten aus den jeweiligen tabellen ausliest und hier aktualisiert
                    Dim Dakosy_NCTSOut As New DAKOSY_Worker.cDakosyNCTSOut
                    Dakosy_NCTSOut.LOADByAnmID(dy_Id, False)
                    If Dakosy_NCTSOut IsNot Nothing Then
                        Me.dy_BezugsNr = Dakosy_NCTSOut.ncts_ObjectName
                        Me.dy_Status = Dakosy_NCTSOut.ncts_Status
                        Me.dy_Status_KEWILL_Equivalent = Dakosy_NCTSOut.ncts_Status_KEWILL_Equivalent
                        Me.dy_Erstellung = Dakosy_NCTSOut.ncts_Erstellung
                        Me.dy_Erstellung_SB = Dakosy_NCTSOut.ncts_Erstellung_SB
                        Me.dy_LetzteBearbeitung = Dakosy_NCTSOut.ncts_LetzteBearbeitung
                        Me.dy_LetzteBearbeitung_SB = Dakosy_NCTSOut.ncts_LetzteBearbeitung_SB
                    End If

            End Select

        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 [tblDakosy_Zollanmeldungen] SET " & str & " WHERE dy_Id=@dy_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 tblDakosy_Zollanmeldungen  (" & 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
