
Imports System.Data.SqlClient
Imports System.Reflection

Public Class cOfferteList
    Property KundenNr As Object = Nothing
    ' Property OffertenNr As Object = Nothing
    Public LIST As New List(Of cOfferte)
    Dim SQL As New SQL
    'Dim listTodelete As New List(Of cOfferte)

    Sub New(KundenNr, loadPos)
        Me.KundenNr = KundenNr
        '  Me.OffertenNr = OffertenNr
        LOAD_LIST(Me.KundenNr, loadPos)
    End Sub
    Public Sub CLEAR()
        LIST.Clear()
    End Sub
    Public Sub ADD(eb_EMail As String, Optional cc As Boolean = False, Optional bcc As Boolean = False)
        ' Dim l As New cOfferte
        'l.eb_bcc = bcc
        'LIST.Add(l)
    End Sub

    Public Sub LOAD_LIST(KundenNr, loadPos)
        Try
            LIST.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Offerten WHERE KundenNr=@KundenNr ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)
                    '  cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOfferte
                        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
                        If loadPos Then l.LOAD_POSITIONEN()
                        LIST.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 SAVE()
    '    If SQL.doSQLVarList("DELETE FROM tblEmailBenachrichtigung WHERE eb_KundenNr=" & Me.eb_KundenNr & " and eb_ebartId=" & Me.eb_ebartId & " ", "FMZOLL") Then
    '        For Each l In LIST
    '            If Not l.INSERT() Then Return False
    '        Next
    '       Return True
    '    Else
    '       Return False
    '   End If
    'End Function
End Class

Public Class cOfferte
    Property KundenNr As Object = Nothing
    Property OffertenNr As Object = Nothing
    Property FilialenNr As Object = Nothing
    Property Pauschalpreis As Object = Nothing
    Property Standardofferte As Object = Nothing
    Property Besonderheiten As Object = Nothing
    Property Eingegeben_am As Object = Nothing
    Property Geändert_am As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property Gesperrt As Object = Nothing
    Property Gesperrt_Datum As Object = Nothing
    Property Gesperrt_Grund As Object = Nothing

    Property POSITIONEN As New List(Of cOffertPosition) '= Nothing

    Dim SQL As New SQL

    Sub New()
    End Sub
    Sub New(KundenNr, OffertenNr, loadPos)
        Me.KundenNr = KundenNr
        Me.OffertenNr = OffertenNr
        LOAD(KundenNr, OffertenNr, loadPos)
    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("KundenNr", KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("OffertenNr", OffertenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FilialenNr", FilialenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Pauschalpreis", Pauschalpreis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Standardofferte", Standardofferte))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Besonderheiten", Besonderheiten))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eingegeben am", Eingegeben_am, "Eingegeben_am"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Geändert am", Geändert_am, "Geändert_am"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachbearbeiter", Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gesperrt", Gesperrt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gesperrt_Datum", Gesperrt_Datum))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Gesperrt_Grund", Gesperrt_Grund))

        Return list
    End Function

    Public Sub LOAD(KundenNr, OffertenNr, loadPos)
        Try
            POSITIONEN.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Offerten WHERE KundenNr=@KundenNr AND OffertenNr=@OffertenNr ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", KundenNr)
                    cmd.Parameters.AddWithValue("@OffertenNr", OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.Read Then
                        'Dim l As New cOfferte
                        For Each i In getParameterList()
                            Dim propInfo As PropertyInfo = Me.GetType.GetProperty(i.Scalarvariable)
                            If dr.Item(i.Text) Is DBNull.Value Then
                                propInfo.SetValue(Me, Nothing)
                            Else
                                propInfo.SetValue(Me, dr.Item(i.Text))
                            End If
                        Next
                        If loadPos Then 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 LOAD_POSITIONEN()
        Try
            POSITIONEN.Clear()
            Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
                Using cmd As New SqlCommand("SELECT * FROM Offertenpositionen WHERE KundenNr=@KundenNr and OffertenNr=@OffertenNr  ", conn)
                    cmd.Parameters.AddWithValue("@KundenNr", Me.KundenNr)
                    cmd.Parameters.AddWithValue("@OffertenNr", Me.OffertenNr)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim l As New cOffertPosition
                        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

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

    Public Function SAVE() As Boolean

        Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM Offerten WITH(updlock,serializable) WHERE KundenNr=@KundenNr and OffertenNr=@OffertenNr ) " &
                       " BEGIN " & getUpdateCmd() & " END " &
                       " Else " &
                       " BEGIN " & getInsertCmd() & " END " &
                       " commit tran "
        Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()

        Return SQL.doSQLVarList(sqlstr, "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 Offerten  (" & 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 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 [Offerten] SET " & str & " WHERE KundenNr=@KundenNr and OffertenNr=@OffertenNr  ")

        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


    Function RESET_STANDARTOFFERTEN(Sachbearbeiter) As Boolean
        'Zuserst alle OffertenPos von dem Offert löschen
        If SQL.doSQL("DELETE FROM [Offertenpositionen] WHERE KundenNr=" & Me.KundenNr & " and OffertenNr=" & Me.OffertenNr, "FMZOLL") Then
            'jetzt die Standartofferten  einfügen
            For Each r In SQL.loadDgvBySql("SELECT * FROM [Standardofferten] WHERE [OffertenNr]=" & Me.OffertenNr, "FMZOLL").Rows
                Dim P As New cOffertPosition()
                Dim Leistung As New cLeistungen(r("LeistungsNr"))


                P.KundenNr = Me.KundenNr
                P.OffertenNr = Me.OffertenNr

                P.LeistungsNr = r("LeistungsNr")
                P.LeistungsBez = r("LeistungsBez")
                P.BerechnungsartNr = Leistung.BerechnungsartNr
                P.Preis = r("Preis")
                P.Anzahl = r("Anzahl")
                P.BGebLeistungsNr = r("BGebLeistungsNr")
                P.BGebProzent = r("BGebProzent")
                P.BGebMinBetrag = r("BGebMinBetrag")
                P.Eingegeben_am = Now
                'P.Geändert_am = XXXXXXXXX
                If r("BerechnungsartPosInkludiert") IsNot DBNull.Value Then P.BerechnungsartPosInkludiert = r("BerechnungsartPosInkludiert")
                If r("BerechnungsartNr") IsNot DBNull.Value Then P.BerechnungsartNr = r("BerechnungsartNr") 'Falls hier, dann übernommen, sonst von Leistung
                P.Sachbearbeiter = Sachbearbeiter
                'P.Preisänderung = XXXXXXXXX
                'P.PreisänderungProzent = XXXXXXXXX
                'P.PreisAlt = XXXXXXXXX
                If Not P.INSERT() Then
                    Return False
                End If
            Next
        Else
            Return False
        End If
        Return True
    End Function
    Public Function RESET_STANDARTOFFERTEN_FIRMA(Sachbearbeiter, Firma) As Boolean
        If Firma = "" Or Firma = "VERAG" Then
            Return RESET_STANDARTOFFERTEN(Sachbearbeiter)
        Else

            'Zuserst alle OffertenPos von dem Offert löschen
            If SQL.doSQL("DELETE FROM [Offertenpositionen] WHERE KundenNr=" & Me.KundenNr & " and OffertenNr=" & Me.OffertenNr, "FMZOLL") Then
                'jetzt die Standartofferten  einfügen
                For Each r In SQL.loadDgvBySql("SELECT * FROM [StandardoffertenFirma] WHERE Firma='" & Firma & "' AND [OffertenNr]=" & Me.OffertenNr, "FMZOLL").Rows
                    Dim P As New cOffertPosition()
                    Dim Leistung As New cLeistungen(r("LeistungsNr"))


                    P.KundenNr = Me.KundenNr
                    P.OffertenNr = Me.OffertenNr

                    P.LeistungsNr = r("LeistungsNr")
                    P.LeistungsBez = r("LeistungsBez")
                    P.BerechnungsartNr = Leistung.BerechnungsartNr
                    P.Preis = r("Preis")
                    P.Anzahl = r("Anzahl")
                    P.BGebLeistungsNr = r("BGebLeistungsNr")
                    P.BGebProzent = r("BGebProzent")
                    P.BGebMinBetrag = r("BGebMinBetrag")
                    P.Eingegeben_am = Now
                    'P.Geändert_am = XXXXXXXXX
                    P.Sachbearbeiter = Sachbearbeiter
                    If r("BerechnungsartPosInkludiert") IsNot DBNull.Value Then P.BerechnungsartPosInkludiert = r("BerechnungsartPosInkludiert")
                    If r("BerechnungsartNr") IsNot DBNull.Value Then P.BerechnungsartNr = r("BerechnungsartNr") 'Falls hier, dann übernommen, sonst von Leistung
                    'P.Preisänderung = XXXXXXXXX
                    'P.PreisänderungProzent = XXXXXXXXX
                    'P.PreisAlt = XXXXXXXXX
                    If Not P.INSERT() Then
                        Return False
                    End If
                Next
            Else
                Return False
            End If
            Return True
        End If
    End Function


End Class


Public Class cOffertPosition
    Property KundenNr As Object = Nothing
    Property OffertenNr As Object = Nothing
    Property LeistungsNr As Object = Nothing
    Property LeistungsBez As Object = Nothing
    Property BerechnungsartNr As Object = Nothing
    Property Preis As Object = Nothing
    Property Anzahl As Object = Nothing
    Property BGebLeistungsNr As Object = Nothing
    Property BGebProzent As Object = Nothing
    Property BGebMinBetrag As Object = Nothing
    Property Eingegeben_am As Object = Nothing
    Property Geändert_am As Object = Nothing
    Property Sachbearbeiter As Object = Nothing
    Property Preisänderung As Object = Nothing
    Property PreisänderungProzent As Object = Nothing
    Property PreisAlt As Object = Nothing
    Property BerechnungsartPosInkludiert As Object = Nothing

    Dim SQL As New SQL

    Sub New()
    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("KundenNr", KundenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("OffertenNr", OffertenNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsNr", LeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LeistungsBez", LeistungsBez))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BerechnungsartNr", BerechnungsartNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Preis", Preis))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Anzahl", Anzahl))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BGebLeistungsNr", BGebLeistungsNr))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BGebProzent", BGebProzent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BGebMinBetrag", BGebMinBetrag))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Eingegeben am", Eingegeben_am, "Eingegeben_am"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Geändert am", Geändert_am, "Geändert_am"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Sachbearbeiter", Sachbearbeiter))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Preisänderung", Preisänderung))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PreisänderungProzent", PreisänderungProzent))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PreisAlt", PreisAlt))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BerechnungsartPosInkludiert", BerechnungsartPosInkludiert))
        Return list
    End Function

    Public Function INSERT() As Boolean
        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 Offertenpositionen  (" & 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
