Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class MahnListe
    Inherits AbstractForm
    Implements InterfacePrintable
    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

    Const MahnstufeSoll_index As Integer = 1
    Const RechnungNummer_index As Integer = 2
    Const SummeBezahlt_index As Integer = 7

    Sub LoadData()
        Try
            DsMahnungen.EnforceConstraints = False
            BuchvorgangTableAdapter.FillByMahnstufe(DsMahnungen.buchvorgang, Today.AddYears(-3))
            MahnstufeBerechnen()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
    Private Sub KundenListe_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load

        Try
            LoadFilter()
            LoadData()

            ' FillComboBox(Me.MahnstufeComboBox, "SELECT Typ, Bezeichnung FROM buchVorgangTyp ORDER By Bezeichnung", "Bezeichnung", "Typ")

            LoadFilter()
            FilterButton_Click(Nothing, Nothing)
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub


    Private Sub SaveFilter()
        Try

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

    Private Sub LoadFilter()
        Try

        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"
            'filter dataset 
            If MahnstufeComboBox.Text.Length > 0 Then
                filter &= " and Typ Like '" & MahnstufeComboBox.SelectedValue & "%'"
            End If

            BuchvorgangBindingSource.Filter = filter
            SaveFilter()
            MahnstufeBerechnen()
        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 vorg As dsMahnungen.buchvorgangRow = BuchvorgangBindingSource.Current.row
            OeffneVorgangByNummer(VorgangTyp:=vorg.Typ, VorgangNummer:=vorg.Nummer, neuesFenster:=True)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Sub MahnstufeBerechnen()
        For Each dr In VorgangListeDataGridView.Rows
            Try
                Dim RechNummer As String = dr.Cells(RechnungNummer_index).Value
                Dim MahnstufeSoll As String = CalculateMahnStufe(RechNummer)
                Dim BertragEingang = GetBetragEingang(VorgangTypen.AR, RechNummer)
                Dim cell = dr.Cells(MahnstufeSoll_index)
                dr.Cells(MahnstufeSoll_index).Value = MahnstufeSoll
                dr.Cells(SummeBezahlt_index).Value = BertragEingang

                If MahnstufeSoll = MAHNSTUFE_1 Then
                    cell.CellStyle.BackColor = Color.Yellow
                End If
                If MahnstufeSoll = MAHNSTUFE_2 Then
                    cell.CellStyle.BackColor = Color.Red
                End If
                If MahnstufeSoll = MAHNSTUFE_3 Then
                    cell.CellStyle.BackColor = Color.DarkRed
                End If
                VorgangListeDataGridView.Refresh()
            Catch ex As Exception
                HandleAppError(ex, Silent:=True)
            End Try
        Next

    End Sub


    Private Sub MahnstufeSetzenButton_Click(sender As Object, e As System.EventArgs) Handles MahnstufeSetzenButton.Click
        Try
            Dim vorgang As dsMahnungen.buchvorgangRow = BuchvorgangBindingSource.Current.row
            SetzeMahnStufe(vorgang.Nummer, MahnstufeComboBox.Text)
            MsgBox("Die Mahnstufe wurde gesetzt!")
            LoadData()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Setzt die Buttons aktiv oder nicht je nach Auswahl 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetButtons()
        Try
            FilterButton.Enabled = BuchvorgangBindingSource.Current IsNot Nothing
            MahnbriefButton.Enabled = BuchvorgangBindingSource.Current IsNot Nothing
            KassaButton.Enabled = BuchvorgangBindingSource.Current IsNot Nothing
            MahnstufeSetzenButton.Enabled = MahnstufeComboBox.Text.Length > 0 AndAlso BuchvorgangBindingSource.Current IsNot Nothing
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub MahnstufeComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles MahnstufeComboBox.TextChanged
        SetButtons()
    End Sub


#Region "Print"

    Public Sub Print(sender As Object) Implements InterfacePrintable.Print
        Try
            'Start printing for the Vorgang 
            Dim vorgang As dsMahnungen.buchvorgangRow = BuchvorgangBindingSource.Current.row
            Dim resultFile = OpenAusdruck_InWord_Filename_RTF(vorgang.Typ, vorgang.Nummer, DEFAULT_HTML_VORLAGE_MAHNUNG, True)
            Process.Start("file://" & resultFile)
            If MsgBox("Als gemahnt markieren?", MsgBoxStyle.YesNo) = vbYes Then
                MahnstufeSetzenButton_Click(Nothing, Nothing)
            End If
            'ModulePDF.HTMLPrint_PDF_Mail(vorgang)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub VorgangListeDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles VorgangListeDataGridView.SelectionChanged
        SetButtons()
    End Sub

    Private Sub MahnbriefButton_Click(sender As Object, e As System.EventArgs) Handles MahnbriefButton.Click
        Print(Nothing)
    End Sub
#End Region

    Private Sub KassaButton_Click(sender As Object, e As System.EventArgs) Handles KassaButton.Click
        Try
            'Start printing for the Vorgang 
            Dim vorgang As dsMahnungen.buchvorgangRow = BuchvorgangBindingSource.Current.row
            Call KassaBuchung(vorgang.Typ, vorgang.Nummer, vorgang.SummeBrutto)
            LoadData()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub VorgangListeDataGridView_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles VorgangListeDataGridView.DataError
        'Ignore
    End Sub

    Private Sub MahnbriefButtonWord_Click(sender As Object, e As EventArgs) Handles MahnbriefButtonWord.Click
        Try
            'Start printing for the Vorgang 
            Dim vorgang As dsMahnungen.buchvorgangRow = BuchvorgangBindingSource.Current.row
            OpenAusdruck_inWord_XML(vorgang.Typ, vorgang.Nummer, DEFAULT_WORD_VORLAGE_MAHNUNG, VIEWER_WORD, Silent:=False)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class