' 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 Facture

    Public Sub Supprimer_FSE(ByVal NumFacture As String, ByVal demande_confirmation As Boolean, ByVal message_confirmation As Boolean, ByVal demande_réassort_stock As Boolean, ByVal demande_suppr_encaissement As Boolean, ByVal réassort_stock As Boolean)


        If NumFacture = "" Then Exit Sub
        If Not IsNumeric(NumFacture) Then Exit Sub




        Dim mydatatable As DataTable = Me.FacturesTableAdapter.GetFactureDataByNum1(NumFacture)

        If mydatatable.Rows.Count = 0 Then
            MyMsg_Box.Presentation("Officine !", "facture num " + NumFacture + " inexistante, suppression impossible.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        If mydatatable.Rows.Count > 1 Then
            MyMsg_Box.Presentation("Officine !", "ANOMALIE: Plusieurs enregistrements pour facture num " + NumFacture, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If

        If Me.FacturesTableAdapter.GetFactureDataByNum1(NumFacture).Item(0).Num_Lot_FSE <> "" Or Me.FacturesTableAdapter.GetFactureDataByNum1(NumFacture).Item(0).Num_Lot_DRE <> "" Then
            MyMsg_Box.Presentation("Officine !", "facture num " + NumFacture + " mise en lot, suppression impossible.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        Dim go As Boolean = False
        If demande_confirmation = False Then
            go = True
        Else
            MyMsg_Box.Presentation("Officine !", "Confirmez suppression facture num " + NumFacture + " ?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
                go = True
            End If
        End If

        If go = True Then
            Dim Action As String = ""
            Dim Info As String = ""
            If demande_confirmation = True Then
                Action = "Supression FSE"
            Else
                Action = "FSE non aboutie"
            End If
            Dim facture_data = Me.FacturesTableAdapter.GetFactureDataByNum1(NumFacture).Item(0)

            Info = NumFacture & Chr(10) & facture_data.Bénéficiaire & Chr(10) & facture_data.G1130C2 & " " & facture_data.G1130C3 & Chr(10)
            Info = Info & Mid(facture_data.G1130C3, 7, 2) & "/" & Mid(facture_data.G1130C3, 5, 2) & "/" & Mid(facture_data.G1130C3, 1, 4) & Chr(10)
            If facture_data.TP_AMO = True Then
                Info = Info & "TP AMO/"
            Else
                Info = Info & "HTP AMO/"
            End If
            If facture_data.TP_AMC = True Then
                Info = Info & "TP AMC" & Chr(10)
            Else
                Info = Info & "HTP AMC" & Chr(10)
            End If
            Dim actes = COMPTOIR_MAIN.ActesTableAdapter.GetDataByNumFacturesSV(NumFacture)
            If actes.Rows.Count > 0 Then
                Info = Info & "Facture créée par " & actes.Item(0).ID_user & Chr(10)
            End If
            For r = 0 To actes.Rows.Count - 1
                Info = Info & "(" & actes(r).Quantité & ")" & actes(r).Désignation & Chr(10)
            Next

            'suppr FSE---------------------------------------------------------
            Dim file As String = ""
            Dim file_chemin_complet As String = ""
            Dim modesecur = facture_data.ModeSecur.ToString
            If modesecur = "4" Then
                file = Dir(Config.Repertoires_RepertoireFactures & "Desync\*.fse")
                file_chemin_complet = Config.Repertoires_RepertoireFactures & "Desync\" + file
            Else
                file = Dir(Config.Repertoires_RepertoireFactures & "*.fse")
                file_chemin_complet = Config.Repertoires_RepertoireFactures & file
            End If

            If file <> "" Then
                Dim file_is_removed As Boolean = False
                Do While file_is_removed = False
                    file = Mid(file, InStr(file, "@") + 1)
                    file = Mid(file, 1, InStr(file, "@") - 1)
                    If Val(file) = Val(NumFacture) Then
                        My.Computer.FileSystem.DeleteFile(file_chemin_complet)
                        file_is_removed = True
                    Else
                        file = Dir()
                        If modesecur = "4" Then
                            file_chemin_complet = Config.Repertoires_RepertoireFactures & "Desync\" + file
                        Else
                            file_chemin_complet = Config.Repertoires_RepertoireFactures & file
                        End If
                    End If
                    If file = "" Then
                        file_is_removed = True
                    End If
                Loop
            End If
            'suppr DRE---------------------------------------------------------
            If modesecur = "4" Then
                file = Dir(Config.Repertoires_RepertoireFactures & "Desync\*.dre")
                file_chemin_complet = Config.Repertoires_RepertoireFactures & "Desync\" + file
            Else
                file = Dir(Config.Repertoires_RepertoireFactures & "*.dre")
                file_chemin_complet = Config.Repertoires_RepertoireFactures & file
            End If
            If file <> "" Then


                Dim file_is_removed = False
                Do While file_is_removed = False
                    file = Mid(file, InStr(file, "@") + 1)
                    file = Mid(file, 1, InStr(file, "@") - 1)
                    If Val(file) = Val(NumFacture) Then
                        My.Computer.FileSystem.DeleteFile(file_chemin_complet)
                        file_is_removed = True
                    Else
                        file = Dir()
                        If modesecur = "4" Then
                            file_chemin_complet = Config.Repertoires_RepertoireFactures & "Desync\" + file
                        Else
                            file_chemin_complet = Config.Repertoires_RepertoireFactures & file
                        End If
                    End If
                    If file = "" Then
                        file_is_removed = True
                    End If
                Loop
            End If
            '------------------------------------------------------------------
            If réassort_stock = True Then
                Info = Info & "PRODUITS RESTOCKÉS" & Chr(10)
                For r = 0 To actes.Rows.Count - 1
                    If actes(r).EAN13 <> "" Then
                        BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).EAN13, actes(r).EAN13)
                    ElseIf actes(r).CIP <> "" Then
                        BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).CIP, actes(r).CIP)
                    ElseIf actes(r).Désignation <> "" Then
                        BDD.ProduitsTableAdapter.UpdateStock_by_Designation(Val(actes(r).Quantité), actes(r).Désignation)
                    End If
                Next
            ElseIf demande_réassort_stock = True Then
                MyMsg_Box.Presentation("Suppression Facture ?", "Faut-il remettre les produits en stock?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    Info = Info & "PRODUITS RESTOCKÉS" & Chr(10)
                    For r = 0 To actes.Rows.Count - 1
                        If actes(r).EAN13 <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).EAN13, actes(r).EAN13)
                        ElseIf actes(r).CIP <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).CIP, actes(r).CIP)
                        ElseIf actes(r).Désignation <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_by_Designation(Val(actes(r).Quantité), actes(r).Désignation)
                        End If
                    Next
                Else
                    Info = Info & "PRODUITS NON RESTOCKÉS" & Chr(10)
                End If
            Else
                Info = Info & "PRODUITS NON RESTOCKÉS" & Chr(10)

            End If

            Me.FacturesTableAdapter.DeleteByNum(NumFacture)
            Me.Facture_LigneFactureTableAdapter1.Delete_By_Num_Facture(NumFacture)
            Me.Facture_DétailCodeCIPTableAdapter1.Delete_By_Num_Facture(NumFacture)
            Me.Facture_DétailLppTableAdapter1.Delete_By_Num_Facture(NumFacture)
            Me.ActesTableAdapter.Delete_by_NumFacturesSV(NumFacture)

            Dim fichier_scor = Dir(Config.LocalNetwork_ServerName & "\scor\index\" & NumFacture & ".xml")
            If fichier_scor <> "" Then
                My.Computer.FileSystem.DeleteFile(Config.LocalNetwork_ServerName & "\scor\index\" & NumFacture & ".xml")
            End If
            fichier_scor = Dir(Config.LocalNetwork_ServerName & "\scor\doc\" + NumFacture + "_*.pdf")
            While fichier_scor <> ""
                While My.Computer.FileSystem.FileExists(Config.LocalNetwork_ServerName & "\scor\doc\" & fichier_scor)
                    Try
                        My.Computer.FileSystem.DeleteFile(Config.LocalNetwork_ServerName & "\scor\doc\" & fichier_scor)
                    Catch ex As Exception

                    End Try
                End While

                fichier_scor = Dir(Config.LocalNetwork_ServerName & "\scor\doc\" + NumFacture + "_*.pdf")
            End While
            ScanOrdo.SCOR_PJTableAdapter.Delete_ny_Num_Facture(NumFacture)

            Dim encaissements = VenteForm.EncaissementsTableAdapter.GetDataBy_NumFactureSV(Val(NumFacture))
            If encaissements.Rows.Count > 0 Then

                Dim the_message As String = "Encaissements lié à cette facture:" & Chr(10) & Chr(10)

                For r = 0 To encaissements.Rows.Count - 1
                    the_message = the_message & encaissements.Item(r).Num & ") " & encaissements.Item(r).Valeur & "€ " & encaissements.Item(r).Type
                    If Val(encaissements.Item(r).NumFactureVenteDirecte) <> 0 Then
                        the_message = the_message & " Fact vente directe liée à cette encaissement " & encaissements.Item(r).NumFactureVenteDirecte.ToString
                    End If
                    the_message = the_message & Chr(10) & Chr(10)
                Next


                go = False
                If demande_suppr_encaissement = False Then
                    go = True
                Else
                    MyMsg_Box.Presentation("Officine ?", the_message & "SUPPRIMER?", True, "OUI", True, "NON", False, "")
                    If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
                        go = True
                    End If
                End If
                If go = True Then
                    For r = 0 To encaissements.Rows.Count - 1
                        Info = Info & "Encaissement Supprimé: " & encaissements.Item(r).Type & " " & encaissements.Item(r).Valeur.ToString & "€"
                    Next
                    VenteForm.EncaissementsTableAdapter.Delete_NumFactureSV(CDec(NumFacture))

                    If Not Ventes_enchainées_encaissement_en_attente Is Nothing Then
                        Dim Ventes_enchainées_encaissement_en_attente_to_remove As New List(Of Vente_enchainée_encaissement_en_attente)
                        For Each Vente_enchainée_encaissement_en_attente As Vente_enchainée_encaissement_en_attente In Ventes_enchainées_encaissement_en_attente
                            For r = 0 To encaissements.Rows.Count - 1
                                If Vente_enchainée_encaissement_en_attente.num_encaissement = encaissements(r).Num Then
                                    If Not Ventes_enchainées_encaissement_en_attente_to_remove.Contains(Vente_enchainée_encaissement_en_attente) Then
                                        Ventes_enchainées_encaissement_en_attente_to_remove.Add(Vente_enchainée_encaissement_en_attente)
                                    End If
                                    Exit For
                                End If
                            Next
                        Next
                        For Each Vente_enchainée_encaissement_en_attente As Vente_enchainée_encaissement_en_attente In Ventes_enchainées_encaissement_en_attente_to_remove
                            Ventes_enchainées_encaissement_en_attente.Remove(Vente_enchainée_encaissement_en_attente)
                        Next
                    End If

                Else
                    For r = 0 To encaissements.Rows.Count - 1
                        Info = Info & "Encaissement NON Supprimé: " & encaissements.Item(r).Type & " " & encaissements.Item(r).Valeur.ToString & "€"
                    Next
                End If
            End If
            BDD.Historique_ActionsTableAdapter.Insert(current_User.ID, Now, Action, Info)
            If message_confirmation = True And demande_suppr_encaissement = True Then
                MyMsg_Box.Presentation("Officine !", "FACTURE " + NumFacture + " SUPPRIMÉE", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
            End If

        End If

    End Sub

    Public Sub DRE_d_annulation(ByVal num_facture As String)
        If Not IsNumeric(num_facture) Then
            MyMsg_Box.Presentation("Officine !", "Num Facture Invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        MyMsg_Box.Presentation("Officine !", "Voulez-vous générer une DRE d'annullation pour la facture num " + num_facture + " ?", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
            Exit Sub
        End If

        'verif facture existe------------------------------------------------
        Dim facture_data As DataTable = Me.FacturesTableAdapter.GetFactureDataByNum1(CDec(num_facture))
        If facture_data.Rows.Count = 0 Then
            MyMsg_Box.Presentation("Officine !", "PAS DE FACTURE PORTANT CE NUMÉRO", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        ElseIf facture_data.Rows.Count > 1 Then
            MyMsg_Box.Presentation("Officine !", "PLUSIEURS FACTURES PORTANT CE NUMÉRO", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        'verif DRE générée------------------------------------------------
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "FactureAcree" Then
                If facture_data.Rows.Item(0).Item(c).ToString <> "2" And facture_data.Rows.Item(0).Item(c).ToString <> "3" Then
                    MyMsg_Box.Presentation("Officine !", "AUCUNE DRE N'A ÉTÉE GÉNÉRÉE POUR CETTE FACTURE.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                End If
                Exit For
            End If
        Next
        'verif DRE mise enlot------------------------------------------------
        Dim Num_Lot_DRE As String = ""
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "Num_Lot_DRE" Then
                If facture_data.Rows.Item(0).Item(c).ToString = "" Then
                    MyMsg_Box.Presentation("Officine !", "DRE NON MISE EN LOT", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
                Else
                    Num_Lot_DRE = facture_data.Rows.Item(0).Item(c).ToString
                End If
                Exit For
            End If
        Next
        'verif lot transmis------------------------------------------------
        Dim Lot_Data As DataTable = Me.LotsTableAdapter.GetDataByRéférence(Nothing, Num_Lot_DRE)
        For c = 0 To Lot_Data.Columns.Count - 1
            If Lot_Data.Columns.Item(c).ColumnName = "NombreTransmission" Then
                If Lot_Data.Rows.Item(0).Item(c) = 0 Then
                    MyMsg_Box.Presentation("Officine !", "DRE NON TRANSMISE (LOT " + Num_Lot_DRE + ")", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
                End If
                Exit For
            End If
        Next
        'verif convention admet DRE d'annulation------------------------------------------------
        Dim Composite_Convention As String = ""
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "Composite_Convention" Then
                Composite_Convention = facture_data.Rows.Item(0).Item(c)
                Exit For
            End If
        Next
        Dim Identifiant_org_signataire As String = Mid(Composite_Convention, 1, InStr(Composite_Convention, "@@") - 1)
        Composite_Convention = Mid(Composite_Convention, InStr(Composite_Convention, "@@") + 2)
        Dim type_Identifiant_org_signataire As String = Mid(Composite_Convention, 1, InStr(Composite_Convention, "@@") - 1)
        Composite_Convention = Mid(Composite_Convention, InStr(Composite_Convention, "@@") + 2)
        Dim critère_secondaire As String = Mid(Composite_Convention, 1, InStr(Composite_Convention, "@@") - 1)
        Composite_Convention = Mid(Composite_Convention, InStr(Composite_Convention, "@@") + 2)
        Dim Num_AMC As String = Mid(Composite_Convention, 1, InStr(Composite_Convention, "@@") - 1)
        Dim type_Accord As String = Mid(Composite_Convention, InStr(Composite_Convention, "@@") + 2)

        Dim convention_Data As DataTable = Me.Convention_Table_ConventionsTableAdapter1.GetDataBy_Composite(Identifiant_org_signataire, type_Identifiant_org_signataire, critère_secondaire, Num_AMC, type_Accord)
        For c = 0 To convention_Data.Columns.Count - 1
            If convention_Data.Columns.Item(c).ColumnName = "Accepatation_DRE_Annulation" Then
                If convention_Data.Rows.Item(0).Item(c) <> "1" Then
                    MyMsg_Box.Presentation("Officine !", "LA CONVENTION DE COMPLÉMENTAIRE N'ACCEPTE PAS LES DRE D'ANNULATION", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
                End If
                Exit For
            End If
        Next

        '-----------------------------------------------------------------------
        SGD_LibererSession(NumeroSessionFacture)
        SGD_ReserverSession(20000, NumeroSessionFacture)

        Trace_Reset_compteur()
        FichierTrace = Application.StartupPath + "\traces\DRE_Annulation_in.txt"

        '1110------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1110, Visu_API, FichierTrace)

        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1110C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1110C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1110C3" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1110C4" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1120------------------------------------------------------------------------------------------------------------------------
        ValeurChamp = "12345678"
        TailleMax = 8
        If SGD_LireChamp(NumeroSessionCPS, 2, Occurence_Grp2, 9, ValeurChamp, TailleMax) <> 0 Then
            'If SGD_LireChamp(1, 2, 1, 9, ValeurChamp, TailleMax) <> 0 Then
            MyMsg_Box.Presentation("Officine !", "ERREUR LECTURE SESSION CPS", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If

        ValeurChamp = Mid(ValeurChamp, 1, TailleMax)
        Dim G1120C1_Facture As String = ""

        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1120C1" Then
                G1120C1_Facture = facture_data.Rows.Item(0).Item(c).ToString
                Exit For
            End If
        Next
        If G1120C1_Facture <> ValeurChamp Then
            MyMsg_Box.Presentation("Officine !", "Introduire CPS utilisée pour la facturaion", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        COMPTOIR_MAIN.groupe1120(NumeroSessionFacture)

        '1130------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1130, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C3" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C4" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C5" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C6" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C7" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1130C8" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1140------------------------------------------------------------------------------------------------------------------------
        COMPTOIR_MAIN.groupe1140(NumeroSessionFacture)

        '1170------------------------------------------------------------------------------------------------------------------------
        COMPTOIR_MAIN.groupe1170(NumeroSessionFacture)

        '1211-1213-1214--------------------------------------------------------------------------------------------------------------
        Dim groupe As String = ""
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1211C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1211"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1213C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1213"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1214C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1214"
                Exit For
            End If
        Next

        If groupe = "G1211" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1211, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            'For c = 0 To facture_data.Columns.Count - 1
            '    If facture_data.Columns.Item(c).ColumnName = "G1211C4" Then
            '        SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
            '        Exit For
            '    End If
            'Next
            SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C5" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C6" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1211C7" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

        ElseIf groupe = "G1213" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1213, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C4" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C5" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C6" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C7" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1213C8" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

        ElseIf groupe = "G1214" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1214, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C4" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C5" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C6" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C7" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1214C8" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
        End If

        '1310------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1310, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1310C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1310C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1310C3" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1310C4" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1310C5" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        '1321------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1321, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C3" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace) '1321-4 à blanc
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C5" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, Mid(facture_data.Rows.Item(0).Item(c).ToString, 1, 3) + "2", Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C6" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C7" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C8" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C9" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C10" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C11" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1321C12" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1330------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1330, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1330C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1330C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1410------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1410, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1410C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1420------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1420, Visu_API, FichierTrace)
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "0", Visu_API, FichierTrace)
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)
        SGD_AjouterChamp_Tracé(NumeroSessionFacture, "", Visu_API, FichierTrace)

        '1440------------------------------------------------------------------------------------------------------------------------
        Dim creer_grp As Boolean = False
        For c = 0 To facture_data.Columns.Count - 1
            If (facture_data.Columns.Item(c).ColumnName = "G1440C1" And facture_data.Rows.Item(0).Item(c).ToString <> "") Or _
               (facture_data.Columns.Item(c).ColumnName = "G1440C2" And facture_data.Rows.Item(0).Item(c).ToString <> "") Or _
               (facture_data.Columns.Item(c).ColumnName = "G1440C3" And facture_data.Rows.Item(0).Item(c).ToString <> "") Then
                creer_grp = True
                Exit For
            End If
        Next
        If creer_grp = True Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1440, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1440C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1440C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1440C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
        End If

        '1511,1512,1513,1514,1515 (nature d'assurance)-------------------------------------------------------------------------------
        groupe = "G1511"
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1511C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1511"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1512C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1512"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1513C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1513"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1514C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1514"
                Exit For
            ElseIf facture_data.Columns.Item(c).ColumnName = "G1515C1" And facture_data.Rows.Item(0).Item(c).ToString <> "" Then
                groupe = "G1515"
                Exit For
            End If
        Next
        If groupe = "G1511" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1511, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1511C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1511C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

        ElseIf groupe = "G1512" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1512, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1512C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
        ElseIf groupe = "G1513" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1513, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1513C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1513C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1513C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

        ElseIf groupe = "G1514" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1514, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1514C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1514C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1514C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1514C4" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

        ElseIf groupe = "G1515" Then
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1515, Visu_API, FichierTrace)
            For c = 0 To facture_data.Columns.Count - 1
                If facture_data.Columns.Item(c).ColumnName = "G1515C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
        End If


        Dim lignes_fact As DataTable
        lignes_fact = Me.Facture_LigneFactureTableAdapter1.GetDataByNumFacture(num_facture)
        For r = 0 To lignes_fact.Rows.Count - 1
            '1610------------------------------------------------------------------------------------------------------------------------
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1610, Visu_API, FichierTrace)
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C1" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C3" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C4" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C5" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C6" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C7" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C8" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C9" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C10" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C11" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C12" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C13" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C14" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C15" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1610C16" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next

            '1820------------------------------------------------------------------------------------------------------------------------
            SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1820, Visu_API, FichierTrace)
            SGD_AjouterChamp_Tracé(NumeroSessionFacture, (r + 1).ToString, Visu_API, FichierTrace)
            For c = 0 To lignes_fact.Columns.Count - 1
                If lignes_fact.Columns.Item(c).ColumnName = "G1820C2" Then
                    SGD_AjouterChamp_Tracé(NumeroSessionFacture, lignes_fact.Rows.Item(r).Item(c).ToString, Visu_API, FichierTrace)
                    Exit For
                End If
            Next
        Next
        '1910------------------------------------------------------------------------------------------------------------------------
        SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 1910, Visu_API, FichierTrace)
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C1" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C2" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C3" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C4" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C5" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next
        For c = 0 To facture_data.Columns.Count - 1
            If facture_data.Columns.Item(c).ColumnName = "G1910C6" Then
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, facture_data.Rows.Item(0).Item(c).ToString, Visu_API, FichierTrace)
                Exit For
            End If
        Next

        'format facture
        Dim ModeSecur As String = ""
        MyMsg_Box.Presentation("Officine ?", "Voulez vous utiliser le mode dégradé? (en l'abscence de carte vitale)", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
            ModeSecur = "5"
        Else
            If Config.Configuration_Desynchroniser = False Then
                ModeSecur = "3"
            Else
                ModeSecur = "4"
            End If
        End If
        Dim erreur = SIS_FormaterFactures(NomRessourcePS, NomRessourceLecteur, "", "2", ModeSecur, NumeroSessionFacture)
        AfficheErreur(erreur)
        If erreur = 0 Then
            ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
            TailleMax = 71

            If ModeSecur = "4" Then
                SGD_LireChamp(NumeroSessionFacture, 161, 1, 1, ValeurChamp, TailleMax) 'ValeurChamp=CRL de DRE
            Else
                SGD_LireChamp(NumeroSessionFacture, 160, 1, 1, ValeurChamp, TailleMax) 'ValeurChamp=CRL de DRE
            End If
            ValeurChamp = Mid(ValeurChamp, 1, TailleMax)
            CurrentFactureVitale.CRLdeDRE = ValeurChamp
            Dim fichierDRE As String = ""

            If ModeSecur = "4" Then
                fichierDRE = Config.Repertoires_RepertoireFactures & "Desync\" + ValeurChamp + "@" + num_facture + "@" + "dre_annul" + "@" + Format(Now, "yyyyMMddHHmmss") + ".dre"
            Else
                fichierDRE = Config.Repertoires_RepertoireFactures & ValeurChamp + "@" + num_facture + "@" + "dre_annul" + "@" + Format(Now, "yyyyMMddHHmmss") + ".dre"
            End If

            If ModeSecur = "4" Then
                SGD_LireChampFichier(NumeroSessionFacture, 161, 1, 2, fichierDRE)
            Else
                SGD_LireChampFichier(NumeroSessionFacture, 160, 1, 2, fichierDRE)
            End If

            MyMsg_Box.Presentation("Officine !", "DRE ANNULATION FACTURE " + num_facture + " GÉNÉRÉE", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
        '------------------------------------------------------
        'Visu_API agrement CNDA
        If Visu_API = True Then
            Technique_MAIN.LireSession_Out_VersFichier2(NumeroSessionFacture, Application.StartupPath + "\traces\DRE_Annulation_out.txt")
        End If
        '------------------------------------------------------




    End Sub

    Public Sub FacturedonnéesTechniques(ByVal num_facture As String)
        If Not IsNumeric(num_facture) Then
            MyMsg_Box.Presentation("Officine !", "Num Facture Invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        RichTextBox1.Clear()
        Dim facture As DataTable
        facture = Me.FacturesTableAdapter.GetFactureDataByNum1(CDec(num_facture))
        If facture.Rows.Count <> 1 Then
            MyMsg_Box.Presentation("Officine !", facture.Rows.Count.ToString + " facture(s) trouvée(s)", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            If facture.Rows.Count = 0 Then
                Exit Sub
            End If
        End If
        For c = 0 To facture.Columns.Count - 1
            If facture(0).Item(c).ToString <> "" Then
                RichTextBox1.AppendText(Chr(10) + facture.Columns.Item(c).ColumnName + ":" + facture(0).Item(c).ToString)
            End If
        Next

        Dim lignes_facture, Détail_CIP, Détail_LPP As DataTable

        lignes_facture = Me.Facture_LigneFactureTableAdapter1.GetDataByNumFacture(num_facture)
        Détail_CIP = Me.Facture_DétailCodeCIPTableAdapter1.GetDataByNumFacture(num_facture)
        Détail_LPP = Me.Facture_DétailLppTableAdapter1.GetDataByNumFacture(num_facture)

        For l = 0 To lignes_facture.Rows.Count - 1
            Dim numligne As String = ""
            For c = 0 To lignes_facture.Columns.Count - 1
                If lignes_facture.Columns.Item(c).ColumnName = "NumLigne" Then
                    numligne = lignes_facture(l).Item(c).ToString
                    Exit For
                End If
            Next
            RichTextBox1.AppendText(Chr(10) + "--------------------------------" + Chr(10) + "détail ligne " + numligne)
            For c = 0 To lignes_facture.Columns.Count - 1
                If lignes_facture(l).Item(c).ToString <> "" Then
                    RichTextBox1.AppendText(Chr(10) + lignes_facture.Columns.Item(c).ColumnName + ":" + lignes_facture(l).Item(c).ToString)
                End If
            Next


            'détail cip
            For d = 0 To Détail_CIP.Rows.Count - 1
                For c = 0 To Détail_CIP.Columns.Count - 1
                    If Détail_CIP.Columns.Item(c).ColumnName = "NumLigne" Then
                        If Détail_CIP(d).Item(c).ToString = numligne Then
                            For cc = 0 To Détail_CIP.Columns.Count - 1
                                If Mid(Détail_CIP.Columns.Item(cc).ColumnName, 1, 5) = "G1740" Then
                                    If Détail_CIP(d).Item(cc).ToString <> "" Then
                                        RichTextBox1.AppendText(Chr(10) + Détail_CIP.Columns.Item(cc).ColumnName + ":" + Détail_CIP(d).Item(cc).ToString)
                                    End If
                                End If
                            Next
                            Exit For
                        End If
                    End If
                Next
            Next
            '

            'détail lpp
            For d = 0 To Détail_LPP.Rows.Count - 1
                For c = 0 To Détail_LPP.Columns.Count - 1
                    If Détail_LPP.Columns.Item(c).ColumnName = "NumLigne" And Détail_LPP(d).Item(c).ToString = numligne Then
                        For cc = 0 To Détail_LPP.Columns.Count - 1
                            If Mid(Détail_LPP.Columns.Item(cc).ColumnName, 1, 5) = "G1730" Then
                                If Détail_LPP(d).Item(cc).ToString <> "" Then
                                    RichTextBox1.AppendText(Chr(10) + Détail_LPP.Columns.Item(cc).ColumnName + ":" + Détail_LPP(d).Item(cc).ToString)
                                End If
                            End If
                        Next
                        Exit For
                    End If
                Next
            Next
            '

        Next 'lignes facture



    End Sub

    Private Sub Facture_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        COMPTOIR_MAIN.DP_Affiche_Msg()
    End Sub

    Public Sub Supprimer_VenteDirecte(ByVal numventeDirecte As Decimal, ByVal demande_confirmation As Boolean, ByVal message_confirmation As Boolean, ByVal réassort_stock As Boolean)

        If numventeDirecte = 0 Then Exit Sub
        Dim go As Boolean = False
        If demande_confirmation = False Then
            go = True
        Else
            MyMsg_Box.Presentation("Officine ?", "Confirmez suppression facture num " + numventeDirecte.ToString + " ?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
                go = True
            End If
        End If
        If go = True Then
            Dim Action As String = ""
            Dim Info As String = ""
            If demande_confirmation = True Then
                Action = "Supression VD"
            Else
                Action = "VD non aboutie"
            End If
            Info = numventeDirecte.ToString & Chr(10)
            Try
                Dim Facture_data = COMPTOIR_MAIN.Facture_VenteDirecteTableAdapter1.GetDataBy_Num(numventeDirecte).Item(0)
                Info = Info & Mid(Facture_data.Date_, 7, 2) & "/" & Mid(Facture_data.Date_, 5, 2) & "/" & Mid(Facture_data.Date_, 1, 4) & " " & Mid(Facture_data.Date_, 9, 2) & ":" & Mid(Facture_data.Date_, 11, 2) & Chr(10)
                If Facture_data.Nom_Complet <> "" Then
                    Info = Info & Facture_data.Nom_Complet & Chr(10)
                End If
                If Facture_data.Nir <> "" Then
                    Info = Info & Facture_data.Nir & Chr(10)
                End If
            Catch ex As Exception

            End Try

            Dim actes = COMPTOIR_MAIN.ActesTableAdapter.GetDataBy_NumFactureVenteDirecte(numventeDirecte.ToString)
            If actes.Rows.Count > 0 Then
                Info = Info & "Facture créée par " & actes.Item(0).ID_user & Chr(10)
            End If
            For r = 0 To actes.Rows.Count - 1
                Info = Info & "(" & actes(r).Quantité & ")" & actes(r).Désignation & Chr(10)
            Next

            If réassort_stock = True Then

                MyMsg_Box.Presentation("Suppression Facture ?", "Faut-il remettre les produits en stock?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    Info = Info & "PRODUITS RESTOCKÉS" & Chr(10)
                    For r = 0 To actes.Rows.Count - 1
                        If actes(r).EAN13 <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).EAN13, actes(r).EAN13)
                        ElseIf actes(r).CIP <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(actes(r).Quantité), actes(r).CIP, actes(r).CIP)
                        ElseIf actes(r).Désignation <> "" Then
                            BDD.ProduitsTableAdapter.UpdateStock_by_Designation(Val(actes(r).Quantité), actes(r).Désignation)
                        End If

                    Next
                Else
                    Info = Info & "PRODUITS NON RESTOCKÉS" & Chr(10)
                End If
            Else
                Info = Info & "PRODUITS RESTOCKÉS" & Chr(10)
            End If

            COMPTOIR_MAIN.Facture_VenteDirecteTableAdapter1.Delete_By_Num(numventeDirecte)

            COMPTOIR_MAIN.ActesTableAdapter.Delete_By_NumFactureVenteDirecte(numventeDirecte.ToString)
            Dim encaissements = VenteForm.EncaissementsTableAdapter.GetDataBy_NumFactureVenteDirecte(numventeDirecte)
            If encaissements.Rows.Count > 0 Then
                If demande_confirmation = True Then
                    Dim the_message As String = "Encaissements lié à cette facture:" & vbCrLf & vbCrLf
                    For r = 0 To encaissements.Rows.Count - 1
                        the_message = the_message & encaissements.Item(r).Num & ") " & encaissements.Item(r).Valeur & "€ " & encaissements.Item(r).Type
                        If Val(encaissements.Item(r).NumFactureVenteDirecte) <> 0 Then
                            the_message = the_message & " Fact vente directe liée à cette encaissement " & encaissements.Item(r).NumFactureVenteDirecte.ToString
                        End If
                        the_message = the_message & vbCrLf & vbCrLf
                    Next
                    MyMsg_Box.Presentation("Officine ?", the_message & "SUPPRIMER?", True, "OUI", True, "NON", False, "")
                    If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
                        For r = 0 To encaissements.Rows.Count - 1
                            Info = Info & "Encaissement Supprimé: " & encaissements.Item(r).Type & " " & encaissements.Item(r).Valeur.ToString & "€" & Chr(10)
                        Next
                        VenteForm.EncaissementsTableAdapter.Delete_NumFactureVenteDirecte(numventeDirecte)

                        If Not Ventes_enchainées_encaissement_en_attente Is Nothing Then
                            Dim Ventes_enchainées_encaissement_en_attente_to_remove As New List(Of Vente_enchainée_encaissement_en_attente)
                            For Each Vente_enchainée_encaissement_en_attente As Vente_enchainée_encaissement_en_attente In Ventes_enchainées_encaissement_en_attente
                                For r = 0 To encaissements.Rows.Count - 1
                                    If Vente_enchainée_encaissement_en_attente.num_encaissement = encaissements(r).Num Then
                                        If Not Ventes_enchainées_encaissement_en_attente_to_remove.Contains(Vente_enchainée_encaissement_en_attente) Then
                                            Ventes_enchainées_encaissement_en_attente_to_remove.Add(Vente_enchainée_encaissement_en_attente)
                                        End If
                                        Exit For
                                    End If
                                Next
                            Next
                            For Each Vente_enchainée_encaissement_en_attente As Vente_enchainée_encaissement_en_attente In Ventes_enchainées_encaissement_en_attente_to_remove
                                Ventes_enchainées_encaissement_en_attente.Remove(Vente_enchainée_encaissement_en_attente)
                            Next
                        End If

                    Else
                        For r = 0 To encaissements.Rows.Count - 1
                            Info = Info & "Encaissement NON Supprimé: " & encaissements.Item(r).Type & " " & encaissements.Item(r).Valeur.ToString & "€" & Chr(10)
                        Next
                    End If
                Else
                    VenteForm.EncaissementsTableAdapter.Delete_NumFactureVenteDirecte(numventeDirecte)
                End If
            End If
            BDD.Historique_ActionsTableAdapter.Insert(current_User.ID, Now, Action, Info)
            If message_confirmation = True Then
                MyMsg_Box.Presentation("Officine !", "FACTURE " + numventeDirecte.ToString + " SUPPRIMÉE", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
            End If
        End If
    End Sub

  
End Class