Imports System.Data.SqlClient
Imports System.Reflection

Public Class cGestellungsmitteilung


    Property gstm_id As Integer
    Property gstm_AvisoId As Integer
    Property gstm_T1Gestellung_ATB As String = ""
    Property gstm_BezugsNrSumA As String = ""
    Property gstm_LKWKennzeichen As String = ""
    Property gstm_GesamtColli As String = ""
    Property gstm_GesamtGewicht As String = ""


    Public hasEntry As Boolean = False

    Public POSITIONEN As New List(Of cGestellungsmitteilungPositionen)
    Shared SQL As New SQL

    Sub New()
    End Sub

    Sub New(gstm_id)
        Me.gstm_id = gstm_id
        LOAD() : LOAD_POSITIONEN()
    End Sub



    Public 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("gstm_id", gstm_id, , True, True))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_AvisoId", gstm_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_T1Gestellung_ATB", gstm_T1Gestellung_ATB))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_BezugsNrSumA", gstm_BezugsNrSumA))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_LKWKennzeichen", gstm_LKWKennzeichen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_GesamtColli", gstm_GesamtColli))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstm_GesamtGewicht", gstm_GesamtGewicht))
        Return list
    End Function




    Public Function SAVE(Optional saveAll As Boolean = True) As Boolean

        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblGestellungsmitteilung  WHERE gstm_id=@gstm_id) " &
                           " BEGIN " & getUpdateCmd() & " END " &
                     " Else " &
                     " BEGIN " & getInsertCmd() & " END " &
                     " commit tran "

        gstm_id = SQL.doSQLVarListID(-1, sqlstr, "FMZOLL", , list)

        If gstm_id > 0 Then
            If saveAll Then SAVE_POSITIONEN(gstm_id) ' positionen speicehrn 
        End If
        Return gstm_id > 0
    End Function


    Public Function SAVE_POSITIONEN(gstm_id) As Boolean
        If DELETE_POSITIONEN() Then ' zuerst Einträge löschen...
            For Each p In POSITIONEN
                If Not p.INSERT(gstm_id) Then Return False ' ... dann insert
            Next
        Else : Return False
        End If
        Return True
    End Function


    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblGestellungsmitteilung WHERE gstm_id=@gstm_id  ", conn)
                    cmd.Parameters.AddWithValue("@gstm_id", gstm_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
                        LOAD_POSITIONEN()
                    End If
                    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
    Public Sub LOADFromAvisoId(gstm_AvisoId)
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblGestellungsmitteilung WHERE gstm_AvisoId=@gstm_AvisoId  ", conn)
                    cmd.Parameters.AddWithValue("@gstm_AvisoId", gstm_AvisoId)
                    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
                        LOAD_POSITIONEN()
                    End If
                    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

    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 [tblGestellungsmitteilung] SET " & str & " WHERE gstm_id=@gstm_id ")

        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        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 tblGestellungsmitteilung  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function


    Public Function DELETE(Optional delPos = True) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        If delPos Then DELETE_POSITIONEN()
        Dim sqlstr = " DELETE FROM [tblGestellungsmitteilung]  WHERE gstm_id=" & Me.gstm_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Function DELETE_AVISOID(gstm_AvisoId, Optional delPos = True) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        If delPos Then DELETE_POSITIONEN_AVISOID(gstm_AvisoId)
        Dim sqlstr = " DELETE FROM [tblGestellungsmitteilung]  WHERE gstm_AvisoId=" & gstm_AvisoId
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


    Public Function DELETE_POSITIONEN() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblGestellungsmitteilung_Positionen]  WHERE gstmp_GstmID=" & Me.gstm_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Function DELETE_POSITIONEN_AVISOID([gstmp_AvisoID]) As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblGestellungsmitteilung_Positionen]  WHERE [gstmp_AvisoID]=" & [gstmp_AvisoID]
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function

    Public Sub LOAD_POSITIONEN()
        Try
            POSITIONEN.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("Select * FROM tblGestellungsmitteilung_Positionen WHERE gstmp_GstmID=@gstmp_GstmID  order by gstmp_T1Nr", conn)
                    cmd.Parameters.AddWithValue("@gstmp_GstmID", Me.gstm_id)

                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cGestellungsmitteilungPositionen(gstm_id, gstm_AvisoId)
                        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
                        POSITIONEN.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

End Class



Public Class cGestellungsmitteilungPositionen

    Property gstmp_id As Integer
    Property gstmp_GstmID As Integer
    Property gstmp_SendungID As Integer
    Property gstmp_T1Nr As Object = Nothing
    Property gstmp_AvisoID As Integer
    Property gstmp_Abfertigungsart_ID As Object = Nothing
    Property gstmp_Abfertigungsart As String = ""
    Property gstmp_Empfaenger As String = ""
    Property gstmp_Bemerkung As String = ""
    Property gstmp_FilialenNr As Object = Nothing
    Property gstmp_AbfertigungsNr As Object = Nothing
    Property gstmp_UnterNr As Object = Nothing
    Property gstmp_Colli As String = ""
    Property gstmp_Gewicht As String = ""
    Property gstmp_Warenbezeichnung As String = ""
    Property gstmp_Absender As String = ""
    Property gstmp_ATANr As String = ""


    'FROM [VERAG].[dbo].[tblGestellungsmitteilung_Positionen]


    Dim SQL As New SQL

    Sub New(gstmp_GstmID, gstmp_AvisoID, Optional gstmp_SendungID = 0)
        Me.gstmp_GstmID = gstmp_GstmID
        Me.gstmp_AvisoID = gstmp_AvisoID
        Me.gstmp_SendungID = gstmp_SendungID
    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("gstmp_id", gstmp_id,, True))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_GstmID", gstmp_GstmID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_SendungID", gstmp_SendungID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_T1Nr", gstmp_T1Nr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_AvisoID", gstmp_AvisoID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Abfertigungsart_ID", gstmp_Abfertigungsart_ID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Abfertigungsart", gstmp_Abfertigungsart))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Empfaenger", gstmp_Empfaenger))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Bemerkung", gstmp_Bemerkung))


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_FilialenNr", gstmp_FilialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_AbfertigungsNr", gstmp_AbfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_UnterNr", gstmp_UnterNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Colli", gstmp_Colli))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Gewicht", gstmp_Gewicht)) '!!!!
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Warenbezeichnung", gstmp_Warenbezeichnung))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_Absender", gstmp_Absender))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("gstmp_ATANr", gstmp_ATANr))
        Return list
    End Function

    Public Function INSERT(gstmp_GstmID) As Boolean
        Me.gstmp_GstmID = gstmp_GstmID
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
        Return SQL.doSQLVarList(getInsertCmd, "FMZOLL", , list)
    End Function

    Public Function getInsertCmd() As String
        Try
            'Me.Eingegeben_am = Now
            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 tblGestellungsmitteilung_Positionen  (" & str & ") VALUES(" & values & ")  ")
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try
        Return ""
    End Function
End Class
