Public Class frmKasseJournalNeuBerechnen
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim FMZOLL_SQL As New FMZOLL_SQL
    Dim SaldoALT As Double = 0.0
    Dim SaldoNEU As Double = 0.0

    Dim PERSONAL As cPersonal = Nothing
    Private Sub frmKasseAbschliessen_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblTESTSYSTEM.Visible = VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM

        MyComboBox2.fillWithSQL("SELECT [KassenbuchNr],[Name] FROM [Personal] WHERE ((Status='A' AND KassenbuchNr IS NOT NULL AND BLC='R') OR (BLC='V00')) ORDER BY Name", False, "FMZOLL")
        NumericUpDown.Value = Now.Year
    End Sub

    Private Sub MyComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox2.SelectedIndexChanged
        PERSONAL = Nothing
        MyTextBox1.Text = ""
        Label4.Text = ""

        If MyComboBox2._value <> "" Then
            PERSONAL = FMZOLL_SQL.getPersonalKassenbuch(MyComboBox2._value)
            Label4.Text = PERSONAL.ID
            initSALDO()
        End If


    End Sub

    Sub initSALDO()
        SaldoALT = SQL.getValueTxtBySql("SELECT isnull((Sum(tblKassenbuch.Soll)-Sum(tblKassenbuch.Haben)),0) AS Saldo " &
            " FROM Personal INNER JOIN tblKassenbuch ON Personal.ID = tblKassenbuch.Benutzer " &
           " WHERE tblKassenbuch.Mandant='" & PERSONAL.Mandant & "' AND tblKassenbuch.Niederlassung='" & PERSONAL.Niederlassung & "' " &
           "  And tblKassenbuch.Benutzer=" & PERSONAL.ID & " AND tblKassenbuch.Geschäftsjahr=" & NumericUpDown.Value & " AND tblKassenbuch.Konto=" & PERSONAL.KassenbuchNr & "", "FMZOLL")

        MyTextBox1.Text = SaldoALT


    End Sub

    Private Sub btnKassensaldoUbertragen_Click(sender As Object, e As EventArgs) Handles btnKassensaldoUbertragen.Click
        Me.Cursor = Cursors.WaitCursor

        If PERSONAL Is Nothing Then MsgBox("Kasse wählen!") : Exit Sub
        Dim JournalMinus = SQL.getValueTxtBySql("select COUNT(*)  FROM [tblKassenbuch]  where Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "' and JournalNr<0 ", "FMZOLL",,, 0)

        If JournalMinus = 0 Then

            Dim dt = SQL.loadDgvBySql("SELECT * FROM [tblKassenbuch]  where Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "'   order by /*BelegDat,*/KB_Zeitstempel", "FMZOLL")
            Dim JournalNrNEG = -1
            For Each r In dt.Rows
                SQL.doSQL("UPDATE [tblKassenbuch]  SET JournalNr='" & JournalNrNEG & "' where Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "' and JournalNr='" & r("JournalNr") & "' ", "FMZOLL")
                JournalNrNEG -= 1
            Next

            MsgBox("JETZT MINUS KORREKTUR")
            'Update setzt alle Jounalnunmmern wieder Positiv
            SQL.doSQL("UPDATE [tblKassenbuch]  SET JournalNr=JournalNr * -1 where Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "' ", "FMZOLL")
            MsgBox("JETZT JOURNAL KORREKTUR")
            SQL.doSQL("update  [VERAG].[dbo].[tblGeschäftsjahr] 
                        set LfdJournalNr=( select isnull(max(JournalNr),1) from tblKassenbuch where   Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "' )
                        where  Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "' ", "FMZOLL")


        Else
            MsgBox("Es sind negative JournalNummern vorhanden - Vorgang wird abgebrochen!")
        End If


        initSALDO()

        Me.Cursor = Cursors.Default


    End Sub





    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If PERSONAL Is Nothing Then MsgBox("Kasse wählen!") : Exit Sub

        With MyDatagridview1
            .DataSource = SQL.loadDgvBySql("SELECT * FROM [tblKassenbuch]  where Benutzer='" & PERSONAL.ID & "'  and  Mandant='" & PERSONAL.Mandant & "' and Niederlassung='" & PERSONAL.Niederlassung & "' and Geschäftsjahr ='" & NumericUpDown.Value & "'   order by /*BelegDat,*/KB_Zeitstempel", "FMZOLL")
        End With
    End Sub
End Class