Imports IntraSell_DLL
Imports IntraSell_Net_Base
Public Class KassaListe
    Inherits AbstractForm
    Implements InterfaceExportableGrid

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub
    Public Overloads Sub Export(sender As Object) Implements InterfaceExportableGrid.Export
        FunctionsDataGrid.ExportDGToCSV(VorgangListeDataGridView)
    End Sub

    Sub LoadData()
        Try
            If Loading Then Exit Sub
            BuchkassabuchTableAdapter.Fill(DsKassa.buchkassabuch, DatumVon.Value, DatumUntil.Value)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub KassaListe_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            Me.ds = DsKassa
            Me.ParentBindingNavigator.BindingSource = BuchkassabuchBindingSource

            Loading = True
            If Not HasRecht(MitarbeiterID, Rechte.Kassabuch, True) Then
                Close()
                Exit Sub
            End If

            BuchkassabuchTableAdapter.Fill(DsKassa.buchkassabuch, DatumVon.Value, DatumUntil.Value)
            DatumVon.Format = DateTimePickerFormat.Custom
            DatumVon.CustomFormat = "dd MM yyyy HH mm"

            DatumUntil.Format = DateTimePickerFormat.Custom
            DatumUntil.CustomFormat = "dd MM yyyy HH mm"
            LoadFilter()
            Loading = False
            FilterButton_Click(Nothing, Nothing)
            CheckBoxPOSFormat.Checked = VarValue_Default(Variablen.KASSA_REPORTS_FORMAT_POS, True)

            FillComboBox(ComboBoxGruppe, "select Id, GruppeName from buchGruppen order by GruppeName", "GruppeName", "Id")

            Dim fU = New FormUtility
            fU.MakeSplitterCustomizable(SplitContainer, "KASSALISTE_SPLITTER_SIZE")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub SaveFilter()
        Try
            SetVarValue(MitarbeiterID & "_" & Name & "_Filter_" & DatumVon.Name, DatumVon.Value)
            SetVarValue(MitarbeiterID & "_" & Name & "_Filter_" & DatumUntil.Name, DatumUntil.Value)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub LoadFilter()
        Try
            If VarValue_Default("KASSALISTE_FILTER_MERKEN", "false") Then
                DatumVon.Value = VarValue_Default(MitarbeiterID & "_" & Name & "_Filter_" & DatumVon.Name, Today)
                DatumUntil.Value = VarValue_Default(MitarbeiterID & "_" & Name & "_Filter_" & DatumUntil.Name, Today.AddDays(1))
            Else
                DatumVon.Value = AdaptZeiten(AdaptToday())
                DatumUntil.Value = AdaptZeiten(AdaptToday().AddDays(1).AddSeconds(-1))
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    'Filtern
    Private Sub FilterButton_Click(sender As Object, e As System.EventArgs) Handles FilterButton.Click
        Try
            LoadData()
            Dim filter As String = "1=1"
            ' If ArtikelControl1.ArtNr > 0 Then
            'filter = filter & " and ArtNr Like '" + ArtikelControl1.Text + "%'"
            'End If

            If NotizTextBox.Text.Length > 0 Then
                filter &= " and ( Beschreibung Like '%" + NotizTextBox.Text + "%' ) "
            End If

            If txtKBIDVon.Text.Length > 0 Then
                filter &= " and ( KBID >= " & txtKBIDVon.Text & " ) "
            End If

            If txtKBIDBis.Text.Length > 0 Then
                filter &= " and ( KBID <= " & txtKBIDBis.Text & " ) "
            End If

            If IsNumeric(MitarbeiterControl1.IDNR) Then
                If MitarbeiterControl1.IDNR > 0 Then
                    filter = filter & " and ( MitarbeiterNr = " & MitarbeiterControl1.IDNR & ") "
                End If
            End If

            If ComboBoxTyp.Text.Length > 0 Then
                filter &= " and ( Typ = '" & ComboBoxTyp.Text & "' ) "
            End If

            If ComboBoxGruppe.Text.Length > 0 Then
                filter &= " and ( Gruppe = " & ComboBoxGruppe.SelectedValue & " ) "
            End If

            If AdressenControlIDNR.IDNR > 0 Then
                filter &= " and ( IDNR = " & AdressenControlIDNR.IDNR & " ) "
            End If

            If filter IsNot Nothing Then
                BuchkassabuchBindingSource.Filter = filter
            Else
                BuchkassabuchBindingSource.Filter = Nothing
            End If

            SaveFilter()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    'KundenDetail öffnen 
    Private Sub VorgangListeDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles VorgangListeDataGridView.RowHeaderMouseDoubleClick
        Try
            Dim row As dsKassa.buchkassabuchRow = BuchkassabuchBindingSource.Current.row
            Dim Typ = row.VorgangTyp ' VorgangListeDataGridView.SelectedRows(0).Cells(0).Value
            Dim Nummer = row.VorgangNummer ' VorgangListeDataGridView.SelectedRows(0).Cells(1).Value
            OeffneVorgangByNummer(VorgangTyp:=Typ, VorgangNummer:=Nummer, NeuesFenster:=True)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub


    Private Sub DatumVon_ValueChanged(sender As Object, e As System.EventArgs) Handles DatumVon.ValueChanged
        Try
            LoadData()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub DatumUntil_ValueChanged(sender As Object, e As EventArgs) Handles DatumUntil.ValueChanged
        Try
            If DatumUntil.Value.Hour = 0 Then
                DatumUntil.Value = DatumUntil.Value.AddMinutes(24 * 60 - 1)
            End If
            LoadData()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Summen bilden
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) _
     Handles VorgangListeDataGridView.DataBindingComplete
        If VorgangListeDataGridView.Rows.Count < 0 Then Return
        Const Ausgang_index As Integer = 5
        Const Eingang_index As Integer = 6

        Dim totalAusgang, totalEingang As Decimal
        totalAusgang = 0
        totalEingang = 0

        For Each dr In VorgangListeDataGridView.Rows
            Dim cellAusgang = dr.Cells(Ausgang_index).Value
            Dim celleingang = dr.Cells(Eingang_index).Value
            If cellAusgang IsNot Nothing Then
                totalAusgang += cellAusgang
            End If
            If celleingang IsNot Nothing Then
                totalEingang += celleingang
            End If
        Next

        txtTotalAusgang.Text = FormatCurrency(totalAusgang)
        txtTotalEingang.Text = FormatCurrency(totalEingang)
        txtSaldo.Text = FormatCurrency(totalEingang - totalAusgang)
    End Sub

    Private Sub NotizTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NotizTextBox.TextChanged
        FilterButton_Click(Nothing, Nothing)
    End Sub

    Private Sub btnTagesjournal_Click(sender As Object, e As EventArgs) Handles btnTagesjournal.Click
        If CheckBoxPOSFormat.Checked Then
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "KassabuchPOS.rdlc")
        Else
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "Kassabuch.rdlc")
        End If
    End Sub

    Private Sub ButtonMonatsjournal_Click(sender As Object, e As EventArgs) Handles ButtonMonatsjournal.Click
        If CheckBoxPOSFormat.Checked Then
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "KassaMonatsjournalPOS.rdlc")
        Else
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "KassaMonatsjournal.rdlc")
        End If
    End Sub

    Private Sub ButtonMWST_Click(sender As Object, e As EventArgs) Handles ButtonMWST.Click
        If CheckBoxPOSFormat.Checked Then
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "ReportMWSTPOS.rdlc", "ShowReport_MWST")
        Else
            ModuleReports.ShowReport(DatumVon.Value, DatumUntil.Value, "ReportMWST.rdlc", "ShowReport_MWST")
        End If
    End Sub

    Private Sub CheckBoxPOSFormat_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxPOSFormat.CheckedChanged
        SetVarValue(Variablen.KASSA_REPORTS_FORMAT_POS, CheckBoxPOSFormat.Checked)
    End Sub

    Public Sub TagesReport()
        btnTagesjournal_Click(Nothing, Nothing)
    End Sub


    Public Sub MonatsReport()
        SetLastMonth(DatumVon, DatumUntil)
        ButtonMonatsjournal_Click(Nothing, Nothing)
    End Sub

    Private Sub KassabewegungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KassabewegungToolStripMenuItem.Click
        Try
            KassaBewegung.MdiParent = Main
            KassaBewegung.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class