' 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/>.
Imports System.IO
Imports System.Drawing
Public Class Choix_Convention_Facturation

    Private Structure Org_Signataire_Conv_Groupée
        Dim Identifiant_org_signataire As String
        Dim Type_covention As String
        Dim NumAMC As String
    End Structure
    Private Orgs_Signataires_Conv_Groupée() As Org_Signataire_Conv_Groupée
    Private NumAMC As String

    Private Sub Choix_Convention_Facturation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        CurrentConvention.selected = False
        '______________________________________________________________________________________________________________________
        'spécificité régime agricole
        Dim CurrentBenéficiaire_G101C11_tampon As String = CurrentBenéficiaire.G101C11
        If CurrentBenéficiaire.G101C10 = "02" And Mid(CurrentBenéficiaire.G101C11, 3, 1) = "5" Then
            CurrentBenéficiaire.G101C11 = Mid(CurrentBenéficiaire.G101C11, 1, 2) & "1"
        End If
        '______________________________________________________________________________________________________________________
        'recherche de MUTNUM=AMC_NumAMC_TextBox.Text dans table correspondance
        If CurrentFactureVitale.Mode_Gestion = "RO" Then
            Dim Table_Conventions_Correspondance As DataTable
            Table_Conventions_Correspondance = Me.Convention_Table_correspondanceTableAdapter1.GetDataBy_MUTNUM(COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text, CurrentBenéficiaire.G101C10 & CurrentBenéficiaire.G101C11 & CurrentBenéficiaire.G101C12)
            Select Case Table_Conventions_Correspondance.Rows.Count

                Case 0
                    NumAMC = COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text

                Case 1

                    Dim cod_org_obl_ass As String

                    For c = 0 To Table_Conventions_Correspondance.Columns.Count - 1

                        If Table_Conventions_Correspondance.Columns.Item(c).ColumnName = "code_organisme_obligatoire_associé" Then
                            cod_org_obl_ass = Table_Conventions_Correspondance(0).Item(c).ToString
                            If (CurrentBenéficiaire.G101C10 = Mid(cod_org_obl_ass, 1, 2) Or Mid(cod_org_obl_ass, 1, 2) = "??") And (CurrentBenéficiaire.G101C11 = Mid(cod_org_obl_ass, 3, 3) Or Mid(cod_org_obl_ass, 3, 3) = "???") And (CurrentBenéficiaire.G101C12 = Mid(cod_org_obl_ass, 6, 4) Or Mid(cod_org_obl_ass, 6, 4) = "????") Then
                                For c2 = 0 To Table_Conventions_Correspondance.Columns.Count - 1
                                    If Table_Conventions_Correspondance.Columns.Item(c2).ColumnName = "Num_org_complémentaire" Then
                                        NumAMC = Table_Conventions_Correspondance(0).Item(c2).ToString
                                        Exit For
                                    End If
                                Next
                            Else
                                NumAMC = COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text
                            End If
                        End If
                    Next
                Case Else
                    MyMsg_Box.Presentation("Officine !", "ERREUR!" + Chr(10) + "Plusieurs enregistrement pour le MUTNUM " + COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text + " dans le table de correspondance." + Chr(10) + "Facturation Impossible", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
            End Select
            Table_Conventions_Correspondance = Nothing

        Else
            NumAMC = COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text

        End If

        '______________________________________________________________________________________________________________________
        'recherche de NumAMC dans table regroupements
        ReDim Orgs_Signataires_Conv_Groupée(100)
        Dim Table_Convention_regroupement As DataTable
        If NumAMC <> "" Then
            Table_Convention_regroupement = Me.Convention_Table_RegroupementsTableAdapter1.GetDataBy_Num_Org_Complémentaire(NumAMC)
        Else
            Table_Convention_regroupement = Me.Convention_Table_RegroupementsTableAdapter1.GetData()
        End If

        If CurrentFactureVitale.Mode_Gestion = "RO" Then 'Gestion unique

            'verification Type_convention=RO
            Dim r = 0
            Dim r_max = Table_Convention_regroupement.Rows.Count - 1
            While r <= r_max
                For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                    If Table_Convention_regroupement.Columns.Item(c).ColumnName = "Type_convention" Then
                        If Table_Convention_regroupement(r).Item(c).ToString <> "RO" Then
                            Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                            r_max = r_max - 1
                        Else
                            r = r + 1
                        End If
                        Exit For
                    End If
                Next
            End While


            ' verification du critère secondaire
            r = 0
            r_max = Table_Convention_regroupement.Rows.Count - 1
            While r <= r_max
                For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                    If Table_Convention_regroupement.Columns.Item(c).ColumnName = "critère_secondaire" Then
                        Dim critère_secondaire As String = Table_Convention_regroupement(r).Item(c).ToString
                        If (CurrentBenéficiaire.G101C10 <> Mid(critère_secondaire, 1, 2) And Mid(critère_secondaire, 1, 2) <> "??") _
                      Or (CurrentBenéficiaire.G101C11 <> Mid(critère_secondaire, 3, 3) And Mid(critère_secondaire, 3, 3) <> "???") _
                       Or (CurrentBenéficiaire.G101C12 <> Mid(critère_secondaire, 6, 4) And Mid(critère_secondaire, 6, 4) <> "????") Then
                            Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                            r_max = r_max - 1
                        Else
                            r = r + 1
                        End If
                        Exit For
                    End If
                Next
            End While

            '------------------------------------------------------------------------------------------------------------------------------------------------
        Else 'gestion séparée
            'verification Type_convention
            Dim r = 0
            Dim r_max = Table_Convention_regroupement.Rows.Count - 1
            While r <= r_max
                For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                    If Table_Convention_regroupement.Columns.Item(c).ColumnName = "Type_convention" Then
                        If Table_Convention_regroupement(r).Item(c).ToString = "RO" Then
                            Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                            r_max = r_max - 1
                        Else
                            r = r + 1
                        End If
                        Exit For
                    End If
                Next
            End While


            'N° de complémentaire EDI G109C2
            If CurrentBenéficiaire.AMC_Piecejustificative = "4" And CurrentBenéficiaire.G109C2 <> "" Then
                If Mid(CurrentBenéficiaire.G109C2, 1, 1) = "1" Then
                    r = 0
                    r_max = Table_Convention_regroupement.Rows.Count - 1
                    While r <= r_max
                        For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                            If Table_Convention_regroupement.Columns.Item(c).ColumnName = "Type_convention" Then
                                If Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 2, 2) Then
                                    Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                    r_max = r_max - 1
                                Else
                                    r = r + 1
                                End If
                                Exit For
                            End If
                        Next
                    End While
                    r = 0
                    r_max = Table_Convention_regroupement.Rows.Count - 1
                    While r <= r_max
                        For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                            If Table_Convention_regroupement.Columns.Item(c).ColumnName = "critère_secondaire" Then
                                If Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 4, 3) Then
                                    Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                    r_max = r_max - 1
                                Else
                                    r = r + 1
                                End If
                                Exit For
                            End If
                        Next
                    End While

                ElseIf Mid(CurrentBenéficiaire.G109C2, 1, 1) = "2" Then
                    r = 0
                    r_max = Table_Convention_regroupement.Rows.Count - 1
                    While r <= r_max
                        For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                            If Table_Convention_regroupement.Columns.Item(c).ColumnName = "Type_convention" Then
                                If (Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 2, 2)) And (Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 4, 2)) Then
                                    Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                    r_max = r_max - 1
                                Else
                                    r = r + 1
                                End If
                                Exit For
                            End If
                        Next
                    End While
                    r = 0
                    r_max = Table_Convention_regroupement.Rows.Count - 1
                    While r <= r_max
                        For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                            If Table_Convention_regroupement.Columns.Item(c).ColumnName = "critère_secondaire" Then
                                If Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 6, 3) _
                                And Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 9, 3) _
                                And Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 12, 3) _
                                And Table_Convention_regroupement(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 15, 3) Then
                                    Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                    r_max = r_max - 1
                                Else
                                    r = r + 1
                                End If
                                Exit For
                            End If
                        Next
                    End While
                End If
            End If
            'verification type convention(saisie manuelle)
            If CurrentBenéficiaire.AMC_Type_Convention <> "" Then
                r = 0
                r_max = Table_Convention_regroupement.Rows.Count - 1
                While r <= r_max
                    For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                        If Table_Convention_regroupement.Columns.Item(c).ColumnName = "Type_convention" Then
                            If Table_Convention_regroupement(r).Item(c).ToString <> CurrentBenéficiaire.AMC_Type_Convention Then
                                Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                r_max = r_max - 1
                            Else
                                r = r + 1
                            End If
                            Exit For
                        End If
                    Next
                End While
            End If
            ' verification du critère secondaire
            If CurrentBenéficiaire.AMC_Critère_secondaire <> "" Then
                r = 0
                r_max = Table_Convention_regroupement.Rows.Count - 1
                While r <= r_max
                    For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                        If Table_Convention_regroupement.Columns.Item(c).ColumnName = "critère_secondaire" Then
                            If Table_Convention_regroupement(r).Item(c).ToString <> CurrentBenéficiaire.AMC_Critère_secondaire Then
                                Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                r_max = r_max - 1
                            Else
                                r = r + 1
                            End If
                            Exit For
                        End If
                    Next
                End While
            End If
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------
        'remplit le tableau Orgs_Signataires_Conv_Groupée(n)
        Select Case Table_Convention_regroupement.Rows.Count
            Case 0
                Orgs_Signataires_Conv_Groupée(0).Identifiant_org_signataire = "" 'pas de regroupement
            Case Else
                Dim i = 0
                For r = 0 To Table_Convention_regroupement.Rows.Count - 1
                    For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                        Select Case Table_Convention_regroupement.Columns.Item(c).ColumnName
                            Case "Id_org_signataire"
                                Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire = Table_Convention_regroupement(r).Item(c).ToString
                            Case "Type_convention"
                                Orgs_Signataires_Conv_Groupée(i).Type_covention = Table_Convention_regroupement(r).Item(c).ToString
                            Case "Num_Org_Complémentaire"
                                Orgs_Signataires_Conv_Groupée(i).NumAMC = Table_Convention_regroupement(r).Item(c).ToString
                        End Select
                    Next
                    i = i + 1
                Next
                Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire = ""
        End Select

        '______________________________________________________________________________________________________________________

        '4.2.1.5.3.4 Recherche dans la table des conventions
        Convention_Table_ConventionsDataGridView.Rows.Clear()
        Convention_Table_ConventionsDataGridView.Columns.Clear()
        Dim Conventions_Datatable_temp As DataTable = Me.Convention_Table_ConventionsTableAdapter.GetData
        Conventions_Datatable_temp.Rows.Clear()
        Dim Conventions_Datatable As DataTable = Me.Convention_Table_ConventionsTableAdapter.GetData
        Conventions_Datatable.Rows.Clear()
        '------------------------------------------------------------------------------------------------------------------------------------------------

        '1)Gestion unique
        If CurrentFactureVitale.Mode_Gestion = "RO" Then

            '1.1) sans les regroupements
            'remplire la datatable
            If NumAMC <> "" Then
                Me.Convention_Table_ConventionsTableAdapter.RO_SansRegroup_AvecNumAMC_fill(Conventions_Datatable_temp, NumAMC)
            End If
            If Conventions_Datatable_temp.Rows.Count = 0 And NumAMC = "" Then
                Me.Convention_Table_ConventionsTableAdapter.RO_SansRegroup_SansNumAMC_fill(Conventions_Datatable_temp)
            End If
            Conventions_Datatable.Merge(Conventions_Datatable_temp)
            ' verification du critère secondaire
            Dim r = 0
            Dim r_max = Conventions_Datatable.Rows.Count - 1
            While r <= r_max
                For c = 0 To Conventions_Datatable.Columns.Count - 1
                    If Conventions_Datatable.Columns.Item(c).ColumnName = "critère_secondaire" Then
                        Dim critère_secondaire As String = Conventions_Datatable(r).Item(c).ToString
                        If (CurrentBenéficiaire.G101C10 <> Mid(critère_secondaire, 1, 2) And Mid(critère_secondaire, 1, 2) <> "??") _
                      Or (CurrentBenéficiaire.G101C11 <> Mid(critère_secondaire, 3, 3) And Mid(critère_secondaire, 3, 3) <> "???") _
                       Or (CurrentBenéficiaire.G101C12 <> Mid(critère_secondaire, 6, 4) And Mid(critère_secondaire, 6, 4) <> "????") Then
                            Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                            r_max = r_max - 1
                        Else
                            r = r + 1
                        End If
                        Exit For
                    End If
                Next
            End While
            '1.2) avec les regroupements
            'recherche avec Identifiant_org_signataire issus des cinventions groupés
            If Orgs_Signataires_Conv_Groupée(0).Identifiant_org_signataire <> "" Then
                'récupère l'index de num_amc pour affecter le num_amc issu de la table de regroupement

                Dim index_NumAMC As Integer
                For c = 0 To Conventions_Datatable.Columns.Count - 1
                    If Conventions_Datatable.Columns.Item(c).ColumnName = "Num_AMC" Then
                        index_NumAMC = c
                        Exit For
                    End If
                Next

                Dim i = 0 'parcourir tableau des Identifiant_org_signataire
                While Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire <> ""
                    If Orgs_Signataires_Conv_Groupée(i).Type_covention = "RO" Then
                        Me.Convention_Table_ConventionsTableAdapter.RO_AvecRegroup_AvecIDsignataire_fill(Conventions_Datatable_temp, Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire)
                        Conventions_Datatable.Merge(Conventions_Datatable_temp)
                        If Conventions_Datatable(Conventions_Datatable.Rows.Count - 1).Item(index_NumAMC).ToString = "" Then
                            Conventions_Datatable(Conventions_Datatable.Rows.Count - 1).Item(index_NumAMC) = Orgs_Signataires_Conv_Groupée(i).NumAMC
                        End If
                    End If
                    i = i + 1
                End While
            End If

            'aucune convention trouvée en direct et par les groupées
            If Conventions_Datatable.Rows.Count = 0 Then
                Me.Convention_Table_ConventionsTableAdapter.RO_SansRegroup_SansNumAMC_fill(Conventions_Datatable_temp)
                Conventions_Datatable.Merge(Conventions_Datatable_temp)
            End If

            ' verification du critère secondaire
            r = 0
            r_max = Conventions_Datatable.Rows.Count - 1
            While r <= r_max
                For c = 0 To Conventions_Datatable.Columns.Count - 1
                    If Conventions_Datatable.Columns.Item(c).ColumnName = "critère_secondaire" Then
                        Dim critère_secondaire As String = Conventions_Datatable(r).Item(c).ToString
                        If (CurrentBenéficiaire.G101C10 <> Mid(critère_secondaire, 1, 2) And Mid(critère_secondaire, 1, 2) <> "??") _
                      Or (CurrentBenéficiaire.G101C11 <> Mid(critère_secondaire, 3, 3) And Mid(critère_secondaire, 3, 3) <> "???") _
                       Or (CurrentBenéficiaire.G101C12 <> Mid(critère_secondaire, 6, 4) And Mid(critère_secondaire, 6, 4) <> "????") Then
                            Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                            r_max = r_max - 1
                        Else
                            r = r + 1
                        End If
                        Exit For
                    End If
                Next
            End While



            '2)Gestion séparée
        Else
            '2.1) sans les regroupements
            'remplire la datatable
            '-----------------------------------------------------------
            'eviter les doublons avec conventions groupées
            Dim recherche_hors_conventions_groupées As Boolean = True
            If Orgs_Signataires_Conv_Groupée(0).Identifiant_org_signataire <> "" Then
                Dim i = 0 'parcourir tableau des Identifiant_org_signataire
                While Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire <> ""
                    If NumAMC = Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire Then
                        recherche_hors_conventions_groupées = False
                    End If
                    i = i + 1
                End While
            End If
            '-----------------------------------------------------------
            If NumAMC <> "" And recherche_hors_conventions_groupées = True Then
                If CurrentBenéficiaire.AMC_Type_Convention <> "" Then
                    Me.Convention_Table_ConventionsTableAdapter.PasRO_AvecIDsignataire_fill_avecType(Conventions_Datatable_temp, CurrentBenéficiaire.AMC_Type_Convention, NumAMC)
                Else
                    Me.Convention_Table_ConventionsTableAdapter.PasRO_AvecIDsignataire_SansType_fill(Conventions_Datatable_temp, NumAMC)
                End If
                Conventions_Datatable.Merge(Conventions_Datatable_temp)
                '--------------------------------------------------
                'N° de complémentaire EDI G109C2
                If CurrentBenéficiaire.AMC_Piecejustificative = "4" And CurrentBenéficiaire.G109C2 <> "" Then
                    If Mid(CurrentBenéficiaire.G109C2, 1, 1) = "1" Then
                        Dim r = 0
                        Dim r_max = Conventions_Datatable.Rows.Count - 1
                        While r <= r_max
                            For c = 0 To Conventions_Datatable.Columns.Count - 1
                                If Conventions_Datatable.Columns.Item(c).ColumnName = "type_Identifiant_org_signataire" Then
                                    If Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 2, 2) Then
                                        Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                                        r_max = r_max - 1
                                    Else
                                        r = r + 1
                                    End If
                                    Exit For
                                End If
                            Next
                        End While
                        r = 0
                        r_max = Conventions_Datatable.Rows.Count - 1
                        While r <= r_max
                            For c = 0 To Conventions_Datatable.Columns.Count - 1
                                If Conventions_Datatable.Columns.Item(c).ColumnName = "critère_secondaire" Then
                                    If Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 4, 3) Then
                                        Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                                        r_max = r_max - 1
                                    Else
                                        r = r + 1
                                    End If
                                    Exit For
                                End If
                            Next
                        End While

                    ElseIf Mid(CurrentBenéficiaire.G109C2, 1, 1) = "2" Then
                        Dim r = 0
                        Dim r_max = Conventions_Datatable.Rows.Count - 1
                        While r <= r_max
                            For c = 0 To Conventions_Datatable.Columns.Count - 1
                                If Conventions_Datatable.Columns.Item(c).ColumnName = "type_Identifiant_org_signataire" Then
                                    If (Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 2, 2)) And (Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 4, 2)) Then
                                        Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                                        r_max = r_max - 1
                                    Else
                                        r = r + 1
                                    End If
                                    Exit For
                                End If
                            Next
                        End While
                        r = 0
                        r_max = Conventions_Datatable.Rows.Count - 1
                        While r <= r_max
                            For c = 0 To Conventions_Datatable.Columns.Count - 1
                                If Conventions_Datatable.Columns.Item(c).ColumnName = "critère_secondaire" Then
                                    If Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 6, 3) _
                                    And Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 9, 3) _
                                    And Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 12, 3) _
                                    And Conventions_Datatable(r).Item(c).ToString <> Mid(CurrentBenéficiaire.G109C2, 15, 3) Then
                                        Conventions_Datatable.Rows.Remove(Conventions_Datatable.Rows(r))
                                        r_max = r_max - 1
                                    Else
                                        r = r + 1
                                    End If
                                    Exit For
                                End If
                            Next
                        End While
                    End If
                End If

                '-----------------------------------------------------
            End If
            ' verification du critère secondaire(saisie manuelle)
            If CurrentBenéficiaire.AMC_Critère_secondaire <> "" Then
                Dim r = 0
                Dim r_max = Table_Convention_regroupement.Rows.Count - 1
                While r <= r_max
                    For c = 0 To Table_Convention_regroupement.Columns.Count - 1
                        If Table_Convention_regroupement.Columns.Item(c).ColumnName = "critère_secondaire" Then
                            If Table_Convention_regroupement(r).Item(c).ToString <> CurrentBenéficiaire.AMC_Critère_secondaire Then
                                Table_Convention_regroupement.Rows.Remove(Table_Convention_regroupement.Rows(r))
                                r_max = r_max - 1
                            Else
                                r = r + 1
                            End If
                            Exit For
                        End If
                    Next
                End While
            End If
            '2.2) avec les regroupements
            If Orgs_Signataires_Conv_Groupée(0).Identifiant_org_signataire <> "" Then
                Dim i = 0 'parcourir tableau des Identifiant_org_signataire
                While Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire <> ""
                    If CurrentBenéficiaire.AMC_Type_Convention <> "" Then
                        Me.Convention_Table_ConventionsTableAdapter.PasRO_AvecIDsignataire_fill_avecType(Conventions_Datatable_temp, CurrentBenéficiaire.AMC_Type_Convention, Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire)
                    Else
                        Me.Convention_Table_ConventionsTableAdapter.PasRO_AvecIDsignataire_fill_avecType(Conventions_Datatable_temp, Orgs_Signataires_Conv_Groupée(i).Type_covention, Orgs_Signataires_Conv_Groupée(i).Identifiant_org_signataire)
                    End If
                    Conventions_Datatable.Merge(Conventions_Datatable_temp)
                    i = i + 1
                End While
            End If
            End If
        '______________________________________________________________________________________________________________________
            'ajout des colonnes dans le datagridview
            For c = 0 To Conventions_Datatable.Columns.Count - 1
                Convention_Table_ConventionsDataGridView.Columns.Add(Conventions_Datatable.Columns.Item(c).ColumnName, Conventions_Datatable.Columns.Item(c).ColumnName)
            Next
            '--------------------------------------------------------------------------------

            For r = 0 To Conventions_Datatable.Rows.Count - 1 'parcours des conventions 
                Convention_Table_ConventionsDataGridView.Rows.Add()
                For c = 0 To Conventions_Datatable.Columns.Count - 1
                    Convention_Table_ConventionsDataGridView.Item(c, r).Value = Conventions_Datatable(r).Item(c).ToString
                    If Conventions_Datatable.Columns.Item(c).ColumnName = "type_Accord" Then
                        If Convention_Table_ConventionsDataGridView.Item(c, r).Value = "T" Then
                            Convention_Table_ConventionsDataGridView.Rows.Item(r).DefaultCellStyle.BackColor = Color.FromArgb(192, 255, 255)
                        ElseIf Convention_Table_ConventionsDataGridView.Item(c, r).Value = "H" Then
                            Convention_Table_ConventionsDataGridView.Rows.Item(r).DefaultCellStyle.BackColor = Color.WhiteSmoke
                        End If
                    End If
                Next
            Next
            '______________________________________________________________________________________________________________________
            'spécificité régime agricole
        CurrentBenéficiaire.G101C11 = CurrentBenéficiaire_G101C11_tampon
    End Sub


    Private Sub Convention_Table_ConventionsDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Convention_Table_ConventionsDataGridView.CellClick
        CurrentConvention.clear()
        CurrentConvention.selected = True
        For c = 0 To Convention_Table_ConventionsDataGridView.Columns.Count - 1
            Select Case Convention_Table_ConventionsDataGridView.Columns.Item(c).Name
                Case "Code_action"
                    CurrentConvention.Code_action = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Code_catégorie"
                    CurrentConvention.Code_catégorie = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Spécialité"
                    CurrentConvention.Spécialité = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Référence_de_convention"
                    CurrentConvention.Référence_de_convention = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Message"
                    CurrentConvention.Message = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Identifiant_org_signataire"
                    CurrentConvention.Identifiant_org_signataire = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Libellé_org_signataire"
                    CurrentConvention.Libellé_org_signataire = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "indicateur_convention_groupée"
                    CurrentConvention.indicateur_convention_groupée = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "type_Identifiant_org_signataire"
                    CurrentConvention.type_Identifiant_org_signataire = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "critère_secondaire"
                    CurrentConvention.critère_secondaire = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "type_Accord"
                    CurrentConvention.type_Accord = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Num_AMC"
                    CurrentConvention.Num_AMC = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "libellé_Org_complémentaire"
                    CurrentConvention.libellé_Org_complémentaire = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "indic_desactivation_STS"
                    CurrentConvention.indic_desactivation_STS = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Code_aiguillage_STS"
                    CurrentConvention.Code_aiguillage_STS = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "usage_attestation_papier"
                    CurrentConvention.usage_attestation_papier = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Accepatation_DRE_Annulation"
                    CurrentConvention.Accepatation_DRE_Annulation = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Acceptation_DRE_Rectification"
                    CurrentConvention.Acceptation_DRE_Rectification = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Identifiant_Opérateur_règlement"
                    CurrentConvention.Identifiant_Opérateur_règlement = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "Code_routage"
                    CurrentConvention.Code_routage = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "identifiant_hôte"
                    CurrentConvention.identifiant_hôte = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
                Case "nom_domaine"
                    CurrentConvention.nom_domaine = Convention_Table_ConventionsDataGridView.Item(c, Convention_Table_ConventionsDataGridView.CurrentRow.Index).Value
            End Select

        Next
        Me.Close()
    End Sub


    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.Close()
    End Sub

    Private Sub Jeux_D_Essai_AutoClick()
        If Jeux_d_essai2.Visible = True Then
            Dim type_convention As String
            Dim nombre_convention_même_type_accord As Integer
            Dim type_Accord_index As Integer
            If Jeux_d_essai2.TP_AMC_Label.Text = "True" Then
                type_convention = "T"
            Else
                type_convention = "H"
            End If

            For c = 0 To Convention_Table_ConventionsDataGridView.ColumnCount - 1
                If Convention_Table_ConventionsDataGridView.Columns.Item(c).Name = "type_Accord" Then
                    type_Accord_index = c
                End If
            Next

            For r = 0 To Convention_Table_ConventionsDataGridView.Rows.Count - 1
                If Convention_Table_ConventionsDataGridView.Item(type_Accord_index, r).Value = type_convention Then
                    nombre_convention_même_type_accord = nombre_convention_même_type_accord + 1
                End If
            Next

            If nombre_convention_même_type_accord = 1 Then
                For r = 0 To Convention_Table_ConventionsDataGridView.Rows.Count - 1
                    If Convention_Table_ConventionsDataGridView.Item(type_Accord_index, r).Value = type_convention Then
                        Convention_Table_ConventionsDataGridView.CurrentCell = Convention_Table_ConventionsDataGridView.Item(1, r)
                        Convention_Table_ConventionsDataGridView_CellClick(Nothing, Nothing)
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub Choix_Convention_Facturation_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If Convention_Table_ConventionsDataGridView.RowCount = 0 Then
            MyMsg_Box.Presentation("Officine !", "PAS DE CONVENTION TROUVEE", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Me.Close()
        End If
        Jeux_D_Essai_AutoClick()
    End Sub


    Private Sub Convention_Table_ConventionsDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Convention_Table_ConventionsDataGridView.CellContentClick

    End Sub
End Class