
Public Class frmSecureLoginKassenbuch
    Dim SQL As New SQL
    Dim FMZOLL_SQL As New FMZOLL_SQL
    Public PERSONAL As cPersonal = Nothing
    Public PERSONAL_LG As cPersonal = Nothing
    Public KassenbuchNr_tmp As Integer = -1
    Public openKB As Boolean = False
    Public disableCBO As Boolean = True
    Dim focusPW = False

    Dim Niederlassung = ""

    Private Sub TextBox1_TextChanged(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Return Then
            FlatButton1.PerformClick()
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        Dim benutzerID As Integer = -1
        Dim benutzerID_LG As String = ""
        PERSONAL_LG = Nothing
        If IsNumeric(cboSB._value) Then benutzerID = cboSB._value
        If cboSB_LG.Visible Then
            If IsNumeric(cboSB_LG._value) Then
                benutzerID_LG = cboSB_LG._value.ToString
                PERSONAL_LG = FMZOLL_SQL.getPersonalKassenbuch(benutzerID_LG) 'LG-Benutzer definieren
            End If
        End If
        'Dim Niederlassung = ""
        'Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        '    Case "ATILLA"
        '        Niederlassung = "SUA"
        '    Case "VERAG"

        'End Select

        LoginPocedure(FMZOLL_SQL.getPersonalKassenbuch(benutzerID, TextBox1.Text, Niederlassung, benutzerID_LG))

    End Sub
    Sub LoginPocedure(p, Optional BH = False)
        PERSONAL = p

        If PERSONAL IsNot Nothing Then
            If Not BH And PERSONAL.PasswortChanged Is Nothing Then
                Dim f As New frmKassenbuchPWD(PERSONAL)

                If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Dim frmSecureLoginKassenbuch As New SDL.frmSecureLoginKassenbuch()
                    frmSecureLoginKassenbuch.openKB = True
                    frmSecureLoginKassenbuch.ShowDialog(Me.Parent)
                    Me.Close()
                End If
                Exit Sub
            End If

            lblError.Visible = False
            Panel1.Visible = False
            Label5.Visible = True
            Me.BackColor = Color.Green
            If openKB Then
                Timer1.Enabled = True
            Else
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            End If
        Else
            lblError.Visible = True
            Me.DialogResult = Windows.Forms.DialogResult.None
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim f As New frmKassenbuch(PERSONAL, PERSONAL_LG)
        f.Owner = Me.Owner
        f.Show()
        Timer1.Enabled = False
        Me.Close()
    End Sub

    Private Sub frmSecureLoginKassenbuch_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim where = ""

        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "ATILLA"
                Niederlassung = "SUA"
            Case "IMEX"
                Niederlassung = "IME"
            Case "VERAG"

        End Select

        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "ATILLA" : where = " AND Niederlassung = 'SUA' "
            Case "IMEX" : where = " AND Mandant = 'IMEX' "
            Case "FRONTOFFICE" : where = " AND Mandant = 'FO' "
            Case Else
                If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE_BH_LOGIN", Me) Then
                    where = " AND Niederlassung <> 'SUA' "
                End If
        End Select

        cboSB.fillWithSQL("SELECT [KassenbuchNr],[Name] FROM [Personal] WHERE ((Status='A' AND KassenbuchNr IS NOT NULL AND BLC='R') OR (BLC='V00')) " & where & " ORDER BY Name", False, "FMZOLL")
        cboSB_LG.fillWithSQL("SELECT [KassenbuchNr],[Name] FROM [Personal] WHERE ((Status='A' AND KassenbuchNr IS NOT NULL AND BLC='R') OR (BLC='V00')) AND isLeihgeldkasse=0 " & where & " ORDER BY Name", False, "FMZOLL")

        If cboSB.Items.Count > 0 Then
            cboSB.SelectedIndex = 0
        End If

        If KassenbuchNr_tmp > 0 Then
            cboSB.changeItem(KassenbuchNr_tmp)
            If cboSB._value <> "" Then focusPW = True : cboSB.Enabled = False
        Else
            changeDefaultKBUser(cboSB)
            cboSB.Enabled = True
            If cboSB._value <> "" Then focusPW = True
        End If

        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE_BH_LOGIN", Me) Then
            FlatButton3.Visible = True
            btnStatistik.Visible = True
            PictureBox1.Visible = False
            Saldo()
          
        End If
    
      
    End Sub

    Sub changeDefaultKBUser(cboSB As VERAG_PROG_ALLGEMEIN.MyComboBox)
        Dim PersID As Integer = SQL.getValueTxtBySql("SELECT isnull(mit_FMZollPersID,-1) FROM tblMitarbeiter WHERE [mit_id]=" & VERAG_PROG_ALLGEMEIN.cAllgemein.USRID, "ADMIN")
        Dim KBNr = SQL.getValueTxtBySql("SELECT isnull(KassenbuchNr,-1) FROM Personal WHERE [ID]=" & PersID, "FMZOLL")
        cboSB.changeItem(KBNr)
    End Sub

    Sub Saldo()
        Try
            Dim benutzerID As Integer = -1
            If IsNumeric(cboSB._value) Then benutzerID = cboSB._value


            Dim PERSONAL_tmp = FMZOLL_SQL.getPersonalKassenbuch(benutzerID)

            Dim FirmaTmp = ""
            If PERSONAL_tmp.cRKSV_KasseID > 0 Then
                Dim KASSE As New cRKSV_Kasse(PERSONAL_tmp.cRKSV_KasseID)
                FirmaTmp = KASSE.rksv_firma
            End If


            Dim sqlstr = "SELECT Sum(tblKassenbuch.Soll) AS Soll, Sum(tblKassenbuch.Haben) AS Haben, (Sum(tblKassenbuch.Soll)-Sum(tblKassenbuch.Haben)) AS Saldo " &
                 " FROM Personal INNER JOIN tblKassenbuch ON Personal.ID = tblKassenbuch.Benutzer " &
                " WHERE tblKassenbuch.Mandant='" & PERSONAL_tmp.Mandant & "' AND tblKassenbuch.Niederlassung='" & PERSONAL_tmp.Niederlassung & "' " &
                "  And tblKassenbuch.Benutzer=" & PERSONAL_tmp.ID & " AND tblKassenbuch.Geschäftsjahr=" & cRKSV.getGJ_FIRMA(Now, FirmaTmp) & " AND tblKassenbuch.Konto=" & PERSONAL_tmp.KassenbuchNr & ""

            Dim dt As DataTable = SQL.loadDgvBySql(sqlstr, "FMZOLL")
            Label6.Text = CDbl(If(dt.Rows.Count > 0, cSqlDb.checkNullReturnValue(dt.Rows(0)("Saldo"), 0), 0)).ToString("C")


            Label4.Visible = True
            Label6.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub FlatButton2_Click(sender As Object, e As EventArgs) Handles FlatButton2.Click
        Me.Close()
    End Sub


    Private Sub frmSecureLoginKassenbuch_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If focusPW Then TextBox1.Focus()
    End Sub

    Private Sub FlatButton3_Click(sender As Object, e As EventArgs) Handles FlatButton3.Click
        Dim benutzerID As Integer = -1
        If IsNumeric(cboSB._value) Then benutzerID = cboSB._value

        LoginPocedure(FMZOLL_SQL.getPersonalKassenbuch(benutzerID), True)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSB.SelectedIndexChanged
        cboSB_LG.Visible = False
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE_Saldo_Anzeige", Me) Then
            Saldo()
        End If

        If IsNumeric(cboSB._value) Then
            Dim PERS_TMP = FMZOLL_SQL.getPersonalKassenbuch(cboSB._value)
            If PERS_TMP.isLeihgeldkasse Then
                cboSB_LG.Visible = True
                changeDefaultKBUser(cboSB_LG)
                If cboSB_LG._value <> "" Then focusPW = True

            End If
        End If

    End Sub

    Private Sub btnStatistik_Click(sender As Object, e As EventArgs) Handles btnStatistik.Click
        Dim f As New frmKassenbuchSuche
        f.Show(Me.ParentForm)

        'Me.Close()
    End Sub
End Class

Public Class cPersonal
    Property ID As Integer = -1
    Property Name As String = ""
    Property Kennwort As String = ""
    Property KassenbuchNr As Integer = -1
    Property Supervisor As Boolean = False
    Property Status As String = ""
    Property Mandant As String = ""
    Property Niederlassung As String = ""
    Property SBSichtbar As Boolean = False
    Property BLC As String = ""
    Property CMR As Boolean = False

    Property cRKSV_KasseID As Integer = -1
    Property cRKSV_Kasse As cRKSV_Kasse
    Property PasswortChanged As Object = Nothing
    Property isLeihgeldkasse As Boolean = False
    Property AllowLG As Boolean = False
End Class