' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Public Class Encaissements
    Private Sub Fill_Encaissements_DataGridView(ByVal Mydatatable As DataTable)

        Me.Cursor = Cursors.WaitCursor



        Encaissements_DataGridView.Rows.Clear()
        Application.DoEvents()
        If Mydatatable.Rows.Count = 0 Then
            Me.Cursor = Cursors.Default
            Exit Sub
        End If

        Dim Num_index, Type_index, Valeur_index, NumFactureSV_index, NumFactureVenteDirecte_index, Date_index, Encaissement_sur_Crédit_Type_index, _
            Encaissement_sur_Crédit_Date_index, Encaissement_sur_Crédit_Valeur_index, Encaissement_Nom_Complet_index As Integer
        For c = 0 To Mydatatable.Columns.Count - 1
            Select Case Mydatatable.Columns.Item(c).ColumnName
                Case "Num"
                    Num_index = c
                Case "Type"
                    Type_index = c
                Case "Valeur"
                    Valeur_index = c
                Case "NumFactureSV"
                    NumFactureSV_index = c
                Case "NumFactureVenteDirecte"
                    NumFactureVenteDirecte_index = c
                Case "Date"
                    Date_index = c
                Case "Encaissement_sur_Crédit_Type"
                    Encaissement_sur_Crédit_Type_index = c
                Case "Encaissement_sur_Crédit_Date"
                    Encaissement_sur_Crédit_Date_index = c
                Case "Encaissement_sur_Crédit_Valeur"
                    Encaissement_sur_Crédit_Valeur_index = c
                Case "Nom_Complet"
                    Encaissement_Nom_Complet_index = c
            End Select
        Next


        For r = 0 To Mydatatable.Rows.Count - 1
            Encaissements_DataGridView.Rows.Add()
        Next

        For r = 0 To Mydatatable.Rows.Count - 1
            Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value = Mydatatable(r).Item(Num_index)
            Encaissements_DataGridView.Item(Encaissement_Type.Index, r).Value = Mydatatable(r).Item(Type_index)
            Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value = Mydatatable(r).Item(Valeur_index)
            Encaissements_DataGridView.Item(Encaissement_NumFactureSV.Index, r).Value = Mydatatable(r).Item(NumFactureSV_index)
            Encaissements_DataGridView.Item(Encaissement_NumFactureVenteDirecte.Index, r).Value = Mydatatable(r).Item(NumFactureVenteDirecte_index)
            Encaissements_DataGridView.Item(Encaissement_Date.Index, r).Value = Mydatatable(r).Item(Date_index)
            If Not Mydatatable(r).Item(Encaissement_sur_Crédit_Type_index) Is DBNull.Value Then
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Type.Index, r).Value = Mydatatable(r).Item(Encaissement_sur_Crédit_Type_index)
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Date.Index, r).Value = Mydatatable(r).Item(Encaissement_sur_Crédit_Date_index)
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Valeur.Index, r).Value = Mydatatable(r).Item(Encaissement_sur_Crédit_Valeur_index)
            Else
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Type.Index, r).Value = ""
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Date.Index, r).Value = ""
                Encaissements_DataGridView.Item(Encaissement_sur_Crédit_Valeur.Index, r).Value = ""
            End If

            Encaissements_DataGridView.Item(Encaissement_NomComplet.Index, r).Value = Mydatatable(r).Item(Encaissement_Nom_Complet_index)
        Next
        Me.Cursor = Cursors.Default

    End Sub



    Private Sub Tous_Encaissements_Button_Click(sender As Object, e As EventArgs) Handles Tous_Encaissements_Button.Click
        Bandeau_info_bas_show("Recherche Encaissement")
        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.GetDataByDate_like_Nom_Complet(DateTimePicker_enc_deb.Value, DateTimePicker_enc_fin.Value, Tri_Encaissement_TextBox.Text))
        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Bandeau_info_bas_show("Recherche crédits en cours")
        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.Get_CréditsEnCours_like_Nom_Complet(DateTimePicker_cred_deb.Value, DateTimePicker_cred_fin.Value, Tri_Crédit_TextBox.Text))
        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
    End Sub

    Private Sub Encaissements_Load(sender As Object, e As EventArgs) Handles Me.Load
        Encaissements_DataGridView.BackgroundColor = Me.BackColor
        Encaissement_Crédit_Type_ComboBox.Items.Clear()
        Encaissement_Crédit_Type_ComboBox.Items.Add("")
        Encaissement_Organisme_Type_MyCombobox.Items.Clear()
        Dim types = BDD.Type_PaiementTableAdapter.GetData
        For r = 0 To types.Rows.Count - 1
            Encaissement_Crédit_Type_ComboBox.Items.Add(types(r).Type)
            Encaissement_Organisme_Type_MyCombobox.Items.Add(types(r).Type)
        Next
        Organismes_ComboBox.Items.Clear()
        Dim organismes = BDD.OrganismesTableAdapter.GetData_OrderByNom
        For r = 0 To organismes.Rows.Count - 1
            Organismes_ComboBox.Items.Add(organismes(r).Nom)
        Next
    End Sub

    Private Sub Encaissements_DataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Encaissements_DataGridView.CellClick
        If e.RowIndex = -1 Then
            If e.ColumnIndex = Solder.Index Then
                If Encaissements_DataGridView.RowCount > 0 Then
                    Dim coch As Boolean = Not (Encaissements_DataGridView.Item(Solder.Index, 0).Value)
                    For r = 0 To Encaissements_DataGridView.RowCount - 1
                        Encaissements_DataGridView.Item(Solder.Index, r).Value = coch
                    Next
                End If


            End If
            Exit Sub
        End If
        Encaissement_Crédit_Type_ComboBox.SelectedItem = Nothing

        If e.ColumnIndex = Edition.Index Then
            Encaissements_edition.num = CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, Encaissements_DataGridView.CurrentRow.Index).Value)
            Encaissements_edition.ShowDialog()
            Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.GetDataByDate_like_Nom_Complet(DateTimePicker_enc_deb.Value, DateTimePicker_enc_fin.Value, Tri_Encaissement_TextBox.Text))

        End If



    End Sub


    Private Sub Solder_crédits_Button_Click(sender As Object, e As EventArgs) Handles Solder_crédits_Button.Click
        If Encaissements_DataGridView.RowCount = 0 Then Exit Sub



        'Crédit uniquement
        Dim go As Boolean = False
        Dim montant_total_théorique As Decimal = 0
        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value = True And Encaissements_DataGridView.Item(Encaissement_Type.Index, r).Value <> "Crédit" And Encaissements_DataGridView.Item(Encaissement_Type.Index, r).Value <> "Organisme" Then
                MyMsg_Box.Presentation("Officine !", "Encaissement Crédits:" & vbCrLf & "Selectionnez uniquement des crédits.", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            ElseIf Encaissements_DataGridView.Item(Solder.Index, r).Value Then
                montant_total_théorique = montant_total_théorique + CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value)
                go = True
            End If
        Next
        If go = False Then
            MyMsg_Box.Presentation("Officine !", "Aucun crédit n'a été selectionné", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        Dim total As Decimal
        Try
            total = CDec(Crédits_Total_TextBox.Text)
            If total <> montant_total_théorique Then
                MyMsg_Box.Presentation("Officine", "Le total des montants à recouvrir est " & montant_total_théorique.ToString & " ." & vbCrLf & _
                                       "Vous avez saisi " & total.ToString & " .", True, "OK", True, "Annuler", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", "Total incohérent, abandon.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End Try

        If Encaissement_Crédit_Type_ComboBox.Text = "" Then
            MyMsg_Box.Presentation("Officine !", "Renseignez le type d'encaissement (carte, espèces,...).", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value = True Then
                If total >= CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value) Then
                    Me.EncaissementsTableAdapter1.Update_Crédit_data_ByNum(Encaissement_Crédit_Type_ComboBox.Text, Now, CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value), _
                                                                           True, CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value))
                    total = total - CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value)
                Else
                    Me.EncaissementsTableAdapter1.Update_Crédit_data_ByNum(Encaissement_Crédit_Type_ComboBox.Text, Now, total, _
                                                                          True, CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value))
                    total = 0
                End If
            End If
        Next
        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.Get_CréditsEnCours_like_Nom_Complet(DateTimePicker_cred_deb.Value, DateTimePicker_cred_fin.Value, Tri_Crédit_TextBox.Text))
        If Crédits_Edition_Facture_CheckBox.Checked = True Then
            Edition_Facture(False)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click, Button4.Click
        Edition_Facture(False)
    End Sub

    Private Sub Edition_Facture(ByVal facture_organisme As Boolean)

        Try

            Dim parameters As String = ""
            'If facture_organisme = True Then
            '    Bandeau_info_bas_show("création du document..")

            '    Dim encaissements_list As New List(Of Integer)

            '    Dim patients = BDD.VitaleTableAdapter.GetDataBy_Organisme(Organismes_ComboBox.Text)
            '    MsgBox("patients.Rows.Count  " & patients.Rows.Count.ToString)
            '    For Each patient In patients
            '        Bandeau_info_bas_change_text(patient.Num.ToString & " " & patient.G104C1 & " " & patient.G104C3)
            '        Dim fsvs = BDD.FacturesTableAdapter.GetDataBy_Bénéficiaire_début_fin(patient.G104C1 & " " & patient.G104C3, patient.G101C8, CInt(Format(DateTimePicker_org_deb.Value, "yyyyMMdd")), CInt(Format(DateTimePicker_org_fin.Value, "yyyyMMdd")))
            '        MsgBox("fsvs.Count " & fsvs.Count.ToString)
            '        If fsvs.Count > 0 Then
            '            For Each fsv In fsvs
            '                Bandeau_info_bas_change_text(patient.Num.ToString & " " & patient.G104C1 & " " & patient.G104C3 & " " & fsv.G1110C1)
            '                Dim encaissements = BDD.EncaissementsTableAdapter.GetDataBy_NumFactureSV(CInt(fsv.G1110C1))
            '                MsgBox(encaissements.Rows.Count)
            '                For Each encaissement In encaissements
            '                    If Not encaissements_list.Contains(encaissement.Num) Then
            '                        encaissements_list.Add(encaissement.Num)
            '                    End If
            '                Next
            '            Next
            '        End If
            '    Next
            '    For Each encaissementNum In encaissements_list
            '        parameters = parameters & "&Encaissements_num=" & encaissementNum.ToString
            '    Next

            'Else


            For r = 0 To Encaissements_DataGridView.RowCount - 1
                If Encaissements_DataGridView.Item(Solder.Index, r).Value = True Then
                    parameters = parameters & "&Encaissements_num=" & Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value.ToString
                End If
            Next
            'End If
            'MsgBox(parameters)
            If parameters = "" Then Exit Sub

            Dim edition As New Encaissements_Edition_Facture
            edition.parameters = parameters
            edition.Visible = True
            edition.BringToFront()

        Catch ex As Exception

        End Try
        Bandeau_info_bas_hide()
    End Sub

    Private Sub Encaissements_DataGridView_CellToolTipTextChanged(sender As Object, e As DataGridViewCellEventArgs) Handles Encaissements_DataGridView.CellToolTipTextChanged

    End Sub

    Private Sub Encaissements_DataGridView_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles Encaissements_DataGridView.CellValueChanged
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        If e.ColumnIndex = Solder.Index Then
            Dim total As Decimal = 0
            For r = 0 To Encaissements_DataGridView.RowCount - 1
                If Encaissements_DataGridView.Item(Solder.Index, r).Value = True Then
                    total = total + CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value)
                End If
            Next
            Crédits_Total_TextBox.Text = total.ToString
            Organisme_Total_TextBox.Text = total.ToString

        End If
    End Sub

    Private Sub Encaissements_DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Encaissements_DataGridView.CellContentClick
        Encaissements_DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End Sub


    Private Sub Encaissement_Crédit_Type_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Encaissement_Crédit_Type_ComboBox.SelectedIndexChanged

    End Sub

    Private Sub Encais_part_Cred_Button_Click(sender As Object, e As EventArgs) Handles Encais_part_Cred_Button.Click

        'Crédit uniquement
        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value = True And Encaissements_DataGridView.Item(Encaissement_Type.Index, r).Value <> "Crédit" Then
                MyMsg_Box.Presentation("Officine !", "Encaissement partiel:" & vbCrLf & "Selectionnez un Crédit Unique.", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
        Next

        'encaissement partiel sur crédit seul uniquement
        Dim nbre_cred = 0
        Dim index_cred = 0
        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value = True Then
                nbre_cred = nbre_cred + 1
                index_cred = r
            End If
        Next
        If nbre_cred <> 1 Then
            MyMsg_Box.Presentation("Officine !", "Encaissement partiel: Selectionnez un Crédit Unique", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If


        Try
            CDec(Crédits_Total_TextBox.Text).ToString()
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine !", "Montant incohérent, abandon.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End Try

        If CDec(Crédits_Total_TextBox.Text) >= CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, index_cred).Value) Then
            MyMsg_Box.Presentation("Officine !", "Montant incohérent, abandon.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        If Encaissement_Crédit_Type_ComboBox.Text = "" Then
            MyMsg_Box.Presentation("Officine !", "Renseignez le type d'encaissement (carte, espèces,...).", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        'solde le crédit puis ajoute la difference dans un nouveau crédit
        Me.EncaissementsTableAdapter1.Update_Crédit_data_ByNum(Encaissement_Crédit_Type_ComboBox.Text, Now, CDec(Crédits_Total_TextBox.Text), True, CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, index_cred).Value))

        Dim valeur As Decimal = CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, index_cred).Value) - CDec(Crédits_Total_TextBox.Text)
        Me.EncaissementsTableAdapter1.Insert(Me.EncaissementsTableAdapter1.Select_Max_Num + 1, "Crédit", valeur, _
                                            CDec(Encaissements_DataGridView.Item(Encaissement_NumFactureSV.Index, index_cred).Value), _
                                            CDec(Encaissements_DataGridView.Item(Encaissement_NumFactureVenteDirecte.Index, index_cred).Value), _
                                            Encaissements_DataGridView.Item(Encaissement_Date.Index, index_cred).Value, _
                                           Encaissements_DataGridView.Item(Encaissement_Date.Index, index_cred).Value, "", _
                                         Encaissements_DataGridView.Item(Encaissement_NomComplet.Index, index_cred).Value, 0, False, 0, "Encaissement partiel de crédit, reste " & valeur.ToString & "€", False, 0)



        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.Get_CréditsEnCours_like_Nom_Complet(DateTimePicker_cred_deb.Value, DateTimePicker_cred_fin.Value, Tri_Crédit_TextBox.Text))
        If Crédits_Edition_Facture_CheckBox.Checked = True Then
            Edition_Facture(False)
        End If
    End Sub

    Private Sub Encaissement_Total_TextChanged(sender As Object, e As EventArgs) Handles Crédits_Total_TextBox.TextChanged
        Crédits_Total_TextBox.Text = Replace(Crédits_Total_TextBox.Text, ".", ",")
        Crédits_Total_TextBox.SelectionStart = Crédits_Total_TextBox.Text.Length + 1
    End Sub









    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Orgtanismes_Affiche_Button.Click
        Bandeau_info_bas_show("Recherche Encaissement")
        ''******************************************
        'Dim FV = BDD.FacturesTableAdapter.GetDataBy_Bénéficiaire_Date(, NirTextBox.Text, date_min)
        ''Dim VD = Me.Facture_VenteDirecteTableAdapter1.GetDataBy_bénéficiaire(ComboBoxLECTURE_VITALE.Text, NirTextBox.Text)
        'Dim VD = Me.Facture_VenteDirecteTableAdapter1.GetDataBy_Bénéficiaire_Date(ComboBoxLECTURE_VITALE.Text, NirTextBox.Text, date_min)
        '******************************************
        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.GetData_Organisme(DateTimePicker_org_deb.Value, DateTimePicker_org_fin.Value, Tri_Organisme_TextBox.Text, _
                                                                                        Organismes_ComboBox.Text, Organisme_à_solder_CheckBox.Checked))
        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
    End Sub

    Private Sub Solder_Organisme_Button_Click(sender As Object, e As EventArgs) Handles Solder_Organisme_Button.Click
        If Encaissements_DataGridView.RowCount = 0 Then Exit Sub



        'Crédit uniquement
        Dim go As Boolean = False
        Dim montant_total_théorique As Decimal = 0
        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value Then
                montant_total_théorique = montant_total_théorique + CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value)
                go = True
            End If
        Next
        If go = False Then
            MyMsg_Box.Presentation("Officine !", "Aucun crédit n'a été selectionné", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        Dim total As Decimal
        Try
            total = CDec(Organisme_Total_TextBox.Text)
            If total <> montant_total_théorique Then
                MyMsg_Box.Presentation("Officine", "Le total des montants à recouvrir est " & montant_total_théorique.ToString & " ." & vbCrLf & _
                                       "Vous avez saisi " & total.ToString & " .", True, "OK", True, "Annuler", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", "Total incohérent, abandon.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End Try

        If Encaissement_Organisme_Type_MyCombobox.Text = "" Then
            MyMsg_Box.Presentation("Officine !", "Renseignez le type d'encaissement (carte, espèces,...).", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        For r = 0 To Encaissements_DataGridView.RowCount - 1
            If Encaissements_DataGridView.Item(Solder.Index, r).Value = True Then
                If total >= CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value) Then
                    Me.EncaissementsTableAdapter1.Update_Crédit_data_ByNum(Encaissement_Organisme_Type_MyCombobox.Text, Now, CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value), _
                                                                           True, CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value))
                    total = total - CDec(Encaissements_DataGridView.Item(Encaissement_Valeur.Index, r).Value)
                Else
                    Me.EncaissementsTableAdapter1.Update_Crédit_data_ByNum(Encaissement_Organisme_Type_MyCombobox.Text, Now, total, _
                                                                          True, CDec(Encaissements_DataGridView.Item(Encaissement_Num.Index, r).Value))
                    total = 0
                End If
            End If
        Next
        Fill_Encaissements_DataGridView(Me.EncaissementsTableAdapter1.GetData_Organisme(DateTimePicker_org_deb.Value, DateTimePicker_org_fin.Value, Tri_Organisme_TextBox.Text, _
                                                                                  Organismes_ComboBox.Text, Organisme_à_solder_CheckBox.Checked))
        If Organisme_Edition_Facture_CheckBox.Checked = True Then
            Edition_Facture(True)
        End If
    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Edition_Facture(True)
    End Sub

    Private Sub Encaissement_Organisme_Type_MyCombobox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Encaissement_Organisme_Type_MyCombobox.SelectedIndexChanged

    End Sub

    Private Sub Organismes_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Organismes_ComboBox.SelectedIndexChanged

    End Sub

    Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
        MyDataGridViewPrinter = New DataGridViewPrinter.DataGridViewPrinter() 'Reception_DataGridView, PrintDocument1, True, True, "Commandes", New Font("Ariel", 10, FontStyle.Regular), Color.Black, New Font("Ariel", 10, FontStyle.Regular), Color.Black, True)
        If MyDataGridViewPrinter.SetupThePrinting(Encaissements_DataGridView, "", MyDataGridViewPrinter_PrintDocument) = True Then
            MyDataGridViewPrinter_PrintDocument.Print()
        End If
        Try
            System.Threading.Thread.Sleep(1000)
            Process.Start(Application.StartupPath & "\temp.XPS")
        Catch ex As Exception
        End Try

    End Sub
    Private Sub MyDataGridViewPrinter_PrintDocument_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles MyDataGridViewPrinter_PrintDocument.PrintPage

        Dim more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If (more = True) Then
            e.HasMorePages = True
        End If
    End Sub
End Class