Imports System.Data.SqlClient
Imports System.Reflection


Public Class cVorauskasse


    Property vk_id As Integer = -1
    Property vk_FilialeNr As Object = Nothing
    Property vk_AbfertigungsNr As Object = Nothing
    Property vk_Datum As Object = Nothing
    Property vk_LKW As Object = Nothing
    Property vk_KdNr As Object = Nothing
    Property vk_Firma As Object = Nothing
    Property vk_Betrag As Decimal = 0.0
    Property vk_QS_SachbearbeiterId As Integer
    Property vk_QS_Sachbearbeiter As Object = Nothing
    Property vk_QS_Bemerkung As Object = Nothing
    Property vk_GeldEingetroffen As Boolean = False
    Property vk_BH_SachbearbeiterId As Object = Nothing
    Property vk_BH_Sachbearbeiter As Object = Nothing
    Property vk_BH_Bemerkung As Object = Nothing
    Property vk_BH_DatumGeldeingang As Object = Nothing
    Property vk_storno As Boolean = False
    Property vk_stornoDatum As Object = Nothing


    Property vk_Rolle As Object = Nothing
    Property vk_Anrede As Object = Nothing
    Property vk_Firmenwortlauf As Object = Nothing
    Property vk_Aresse1 As Object = Nothing
    Property vk_Adresse2 As Object = Nothing
    Property vk_UID As Object = Nothing
    Property vk_EORI As Object = Nothing
    Property vk_Tel As Object = Nothing
    Property vk_Fax As Object = Nothing
    Property vk_Email As Object = Nothing
    Property vk_RgNr As Object = Nothing
    Property vk_Absender As Object = Nothing
    Property vk_ATREUR1 As Object = Nothing
    Property vk_Fraechter As Object = Nothing
    Property vk_Anzahl As Object = Nothing
    Property vk_Warenbezeichnung As Object = Nothing
    Property vk_Colli As Object = Nothing
    Property vk_FaStNr As Object = Nothing

    Property vk_Berechnung_Rechnungsbetrag As Object = Nothing
    Property vk_Berechnung_FrachtkostAusland As Object = Nothing
    Property vk_Berechnung_ZollProz As Object = Nothing
    Property vk_Berechnung_FrachtkostInland As Object = Nothing
    Property vk_Berechnung_AbgabenSonst_Bez As Object = Nothing
    Property vk_Berechnung_AbgabenSonst_Betrag As Object = Nothing
    Property vk_Berechnung_EUSTProz As Object = Nothing
    Property vk_Berechnung_KostenSonst_Bez As Object = Nothing
    Property vk_Berechnung_KostenSonst_Betrag As Object = Nothing
    Property vk_Berechnung_Verzollung As Object = Nothing
    Property vk_Berechnung_jeWeitere As Object = Nothing
    Property vk_Berechnung_ATLAS As Object = Nothing
    Property vk_Berechnung_Clearing As Object = Nothing
    Property vk_Berechnung_korrRg As Object = Nothing
    Property vk_Berechnung_Übersetzung As Object = Nothing
    Property vk_Berechnung_Aviso As Object = Nothing
    Property vk_Berechnung_SVS As Object = Nothing
    Property vk_Berechnung_SonstigeKosten As Object = Nothing
    Property vk_Berechnung_SonstigeKostenText As Object = Nothing
    Property vk_Berechnung_PP As Object = Nothing
    Property vk_Freigegeben As Boolean = False
    Property vk_SendungId As Object = Nothing
    Property vk_AvisoId As Object = Nothing
    Property vk_VermerkId As Object = Nothing


    Dim SQL As New SQL

    Sub New()
    End Sub

    Sub New(vk_id)
        Me.vk_id = vk_id
        LOAD()
    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("vk_id", vk_id, , True, True))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_FilialeNr", vk_FilialeNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_AbfertigungsNr", vk_AbfertigungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Datum", vk_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_LKW", vk_LKW))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_KdNr", vk_KdNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Firma", vk_Firma))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Betrag", (vk_Betrag))) '.ToString("N2")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_SachbearbeiterId", vk_QS_SachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_Sachbearbeiter", vk_QS_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_QS_Bemerkung", vk_QS_Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_GeldEingetroffen", vk_GeldEingetroffen))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_SachbearbeiterId", vk_BH_SachbearbeiterId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_Sachbearbeiter", vk_BH_Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_Bemerkung", vk_BH_Bemerkung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_BH_DatumGeldeingang", vk_BH_DatumGeldeingang))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_storno", vk_storno))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_stornoDatum", vk_stornoDatum))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Rolle", vk_Rolle))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Anrede", vk_Anrede))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Firmenwortlauf", vk_Firmenwortlauf))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Aresse1", vk_Aresse1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Adresse2", vk_Adresse2))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_UID", vk_UID))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_EORI", vk_EORI))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Tel", vk_Tel))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Fax", vk_Fax))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Email", vk_Email))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_RgNr", vk_RgNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Absender", vk_Absender))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_ATREUR1", vk_ATREUR1))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Fraechter", vk_Fraechter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Anzahl", vk_Anzahl))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Warenbezeichnung", vk_Warenbezeichnung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Colli", vk_Colli))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_FaStNr", vk_FaStNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_Rechnungsbetrag", vk_Berechnung_Rechnungsbetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_FrachtkostAusland", vk_Berechnung_FrachtkostAusland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_ZollProz", vk_Berechnung_ZollProz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_FrachtkostInland", vk_Berechnung_FrachtkostInland))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_AbgabenSonst_Bez", vk_Berechnung_AbgabenSonst_Bez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_AbgabenSonst_Betrag", vk_Berechnung_AbgabenSonst_Betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_EUSTProz", vk_Berechnung_EUSTProz))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_KostenSonst_Bez", vk_Berechnung_KostenSonst_Bez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_KostenSonst_Betrag", vk_Berechnung_KostenSonst_Betrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_Verzollung", vk_Berechnung_Verzollung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_jeWeitere", vk_Berechnung_jeWeitere))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_ATLAS", vk_Berechnung_ATLAS))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_Clearing", vk_Berechnung_Clearing))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_korrRg", vk_Berechnung_korrRg))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_Übersetzung", vk_Berechnung_Übersetzung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_Aviso", vk_Berechnung_Aviso))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_SVS", vk_Berechnung_SVS))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_SonstigeKosten", vk_Berechnung_SonstigeKosten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Berechnung_PP", vk_Berechnung_PP))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_Freigegeben", vk_Freigegeben))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_SendungId", vk_SendungId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_AvisoId", vk_AvisoId))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("vk_VermerkId", vk_VermerkId))

        Return list
    End Function



    Public Shared Function LOADByFilialeAbfNr(FilialeNr As Integer, AbfertigungsNr As Integer, Optional vk_Rolle As String = "", Optional vk_KdNr As String = "") As cVorauskasse
        Try
            Dim VK As New cVorauskasse
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblVorauskassen WHERE vk_AbfertigungsNr=@vk_AbfertigungsNr AND  vk_FilialeNr=@vk_FilialeNr " & If(If(vk_Rolle, "") <> "", "AND vk_Rolle=@vk_Rolle ", "") & If(If(vk_KdNr, "") <> "", "AND vk_KdNr=@vk_KdNr ", ""), conn)
                    cmd.Parameters.AddWithValue("@vk_FilialeNr", FilialeNr)
                    cmd.Parameters.AddWithValue("@vk_AbfertigungsNr", AbfertigungsNr)
                    cmd.Parameters.AddWithValue("@vk_Rolle", vk_Rolle)
                    cmd.Parameters.AddWithValue("@vk_KdNr", vk_KdNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        For Each li In VK.getParameterList()
                            Dim propInfo As PropertyInfo = VK.GetType.GetProperty(li.Scalarvariable)

                            If dr.Item(li.Text) Is DBNull.Value Then
                                propInfo.SetValue(VK, Nothing)
                            Else
                                propInfo.SetValue(VK, dr.Item(li.Text))
                            End If
                        Next
                        dr.Close()
                        Return VK
                    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
        Return Nothing
    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 tblVorauskassen WITH(updlock,serializable) WHERE vk_id=@vk_id) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "

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


    Public Function SET_GELDEINGANG(vk_GeldEingetroffen As Boolean, Optional vk_BH_Bemerkung As Object = Nothing) As Boolean
        Me.vk_GeldEingetroffen = vk_GeldEingetroffen
        Me.vk_Freigegeben = vk_GeldEingetroffen

        If Me.vk_GeldEingetroffen Then
            Me.vk_BH_DatumGeldeingang = Now
            Me.vk_BH_SachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_id
            Me.vk_BH_Sachbearbeiter = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        Else
            Me.vk_BH_DatumGeldeingang = Nothing
            Me.vk_BH_SachbearbeiterId = Nothing
            Me.vk_BH_Sachbearbeiter = Nothing
        End If
        Me.vk_BH_Bemerkung = vk_BH_Bemerkung

        Return SAVE()
    End Function

    Public Function SET_FREIGEGEBEN(vk_Freigegeben As Boolean, Optional vk_BH_Bemerkung As Object = Nothing) As Boolean
        Me.vk_Freigegeben = vk_Freigegeben
        Me.vk_BH_Bemerkung = vk_BH_Bemerkung
        Return SAVE()
    End Function

    Public Function SET_STORNO() As Boolean
        Me.vk_storno = Not Me.vk_storno
        Return SAVE()
    End Function


    Public Sub LOAD()
        Try
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM tblVorauskassen WHERE vk_id=@vk_id  ", conn)
                    cmd.Parameters.AddWithValue("@vk_id", vk_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

                    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 [tblVorauskassen] SET " & str & " WHERE vk_id=@vk_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 tblVorauskassen  (" & 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() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
        Dim sqlstr = " DELETE FROM [tblVorauskassen] WITH(updlock,serializable) WHERE vk_id=" & Me.vk_id
        Return SQL.doSQL(sqlstr, "FMZOLL")
    End Function


End Class
