' 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

Public Class old_data
    Private Structure produit_from_lgpi
        Dim Désignation As String
        Dim CODE_GTIN13 As String
        Dim Cip As String
        Dim Stock As String
        Dim Marque As String
        Dim Code_LPP As String
        Dim Prix_vente_TTC As String
        Dim Répartiteur As String
        Dim TVA As String
    End Structure
    Private Sub Produits_Button_Click(sender As Object, e As EventArgs) Handles Produits_Button.Click

        'produits.csv:
        'Désignation;CODE GTIN13;Cip;Stock;Marque (Laboratoire);Code LPP;Gestion de Stock;Forme;Prix d'achat HT;Prix vente TTC;Répartiteur (attitré);Statut : Assimilé stupéfiant (;Statut : Frigo (0 ou 1);Statut : Médicament d'exceptio;Statut : Vétérinaire (0 ou 1);Date dernier inventaire;Stock maxi;Stock mini;Tableau (Liste);tarif de réf LPP;Taux TVA;
        MyMsg_Box.Presentation("Officine", "La base de données doit contenir seulement les médicaments de la base CEPS. La base LPP doit être importée. Vérifier structure du fichier à importer. Les stocks actuels doivent étre à 0", True, "", False, "", True, "Annuler")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.Cancel Then Exit Sub
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\lgpi data"
        OpenFileDialog1.ShowDialog()

        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim sw = New StreamWriter("C:\produits.csv")
        Dim line = sr.ReadLine
        Bandeau_info_bas_show("Debut importation")
        line = sr.ReadLine
        While Not line Is Nothing

            Dim Cip = CSV_Champ(line, 2)
            Dim stock = CSV_Champ(line, 4)
            Dim Prix_vente_TTC = CSV_Champ(line, 8)
            Dim LPP_ As String = ""

            If Cip <> "" And IsNumeric(Cip) Then
                Dim Vidal_PackService As New Vidal_PackService.PackService
                Vidal_PackService.Timeout = 60000 '3000
                Dim Vidal_id As Integer
                'Dim CIP As String = ""
                Dim EAN13 As String = ""
                Dim désignation As String = ""
                Dim TVA As Single
                Dim Contenance As String = ""
                Dim Unitecontenance As String = ""
                Dim Assimilestupefiant = "0"
                Dim Prestation As String = ""
                Dim liste As String = ""
                Dim Prestation_actCodeValue As String = ""
                Dim CIS As String = ""
                Dim communityAgrement As Boolean
                Dim dispensationPlace As String = ""
                Dim drugInSport As Boolean
                Dim maxPrescriptionDuration As String = ""
                Dim prescriptionName As String = ""
                Dim offMarketDate As Date
                Dim onFreeAccess As Boolean
                Dim onMarketDate As Date
                Dim otc As Boolean
                Dim vidal_productId As Integer
                Dim withoutPrescr As Boolean



                Dim Pack = Vidal_PackService.searchByCip(Cip)


                'If Pack Is Nothing And produitfromlgpi.CODE_GTIN13 <> "" Then
                '    Dim CODE_GTIN13() As String
                '    ReDim CODE_GTIN13(1)
                '    CODE_GTIN13(0) = produitfromlgpi.CODE_GTIN13
                '    If Vidal_PackService.searchByCodes(CODE_GTIN13).Count > 0 Then
                '        Pack = Vidal_PackService.searchByCodes(CODE_GTIN13)(0)
                '    End If
                'End If
                If Not Pack Is Nothing Then
                    Vidal_id = Pack.id
                    If Not Pack.cip Is Nothing Then
                        Cip = Pack.cip
                    Else
                        Cip = ""
                    End If
                    If Not Pack.cip13 Is Nothing Then
                        EAN13 = Pack.cip13
                    Else
                        EAN13 = ""
                    End If

                    désignation = Pack.name
                    If Not Pack.vatRateValue Is Nothing Then
                        TVA = Pack.vatRateValue
                    Else
                        TVA = 20
                    End If
                    If Not Pack.dose Is Nothing Then If Not Pack.dose.ToString = "True" Then If Not Pack.dose.ToString = "False" Then Contenance = Pack.dose
                    If Not Pack.doseUnit Is Nothing Then If Not Pack.doseUnit.ToString = "True" Then If Not Pack.doseUnit.ToString = "False" Then Unitecontenance = Pack.doseUnit
                    If Pack.narcoticPrescription = True Then Assimilestupefiant = "1"
                    If Not Pack.actCode Is Nothing Then If Not Pack.actCode.ToString = "True" Then If Not Pack.actCode.ToString = "False" Then Prestation = Pack.actCode.ToString
                    If Not Pack.listValue Is Nothing Then If Not Pack.listValue.ToString = "True" Then If Not Pack.listValue.ToString = "False" Then liste = Pack.listValue.ToString
                    If Not Pack.actCodeValue Is Nothing Then If Not Pack.actCodeValue.ToString = "True" Then If Not Pack.actCodeValue.ToString = "False" Then Prestation_actCodeValue = Pack.actCodeValue.ToString
                    If Not Pack.cis Is Nothing Then If Not Pack.cis.ToString = "True" Then If Not Pack.cis.ToString = "False" Then If Not Pack.cis.ToString = "False" Then CIS = Pack.cis.ToString
                    communityAgrement = Pack.communityAgrement
                    If Not Pack.dispensationPlace Is Nothing Then dispensationPlace = Pack.dispensationPlace.ToString
                    drugInSport = Pack.drugInSport
                    If Not Pack.maxPrescriptionDuration Is Nothing Then If Not Pack.maxPrescriptionDuration.ToString = "True" Then If Not Pack.maxPrescriptionDuration.ToString = "False" _
                        Then maxPrescriptionDuration = Pack.maxPrescriptionDuration.ToString
                    prescriptionName = Pack.prescriptionName
                    If Not Pack.offMarketDate Is Nothing Then offMarketDate = Pack.offMarketDate
                    onFreeAccess = Pack.onFreeAccess
                    If Not Pack.onMarketDate Is Nothing Then onMarketDate = Pack.onMarketDate
                    otc = Pack.otc
                    vidal_productId = Pack.productId
                    withoutPrescr = Pack.withoutPrescr
                    Dim Vidal_LpprService As New Vidal_LpprService.LpprService
                    Vidal_LpprService.Timeout = 60000 '3000
                    Dim lpp = Vidal_LpprService.searchByPackId(Vidal_id)

                    Dim est_un_kit As Boolean = False
                    Dim Type_prestation As String = ""
                    If Not lpp Is Nothing And lpp.Count > 0 Then


                        If lpp.Count > 1 Then
                            est_un_kit = True
                        Else
                            LPP_ = lpp(0).code
                        End If



                        For Each item In lpp

                            If Not item Is Nothing Then
                                If Not item.code Is Nothing Then
                                    If COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(item.code, Format(Now, "yyyyMMdd")).Rows.Count = 0 Then
                                        Dim Age_maximal As String = ""
                                        If Not item.ageMax Is Nothing Then If Not item.ageMax.ToString = "True" Then If Not item.ageMax.ToString = "False" Then Age_maximal = item.ageMax.ToString
                                        Dim Montant_maximal As String = ""
                                        If Not item.salePriceLimit Is Nothing Then If Not item.salePriceLimit.ToString = "True" Then If Not item.salePriceLimit.ToString = "False" Then Montant_maximal = (item.salePriceLimit * 100).ToString
                                        Dim Quantité_maximale As String = ""
                                        If Not item.amountMax Is Nothing Then If Not item.amountMax.ToString = "True" Then If Not item.amountMax.ToString = "False" Then Quantité_maximale = item.amountMax.ToString
                                        If Not item.service Is Nothing Then
                                            Select Case item.service.Value
                                                Case officine.Vidal_LpprService.LpprServiceType.BUYING
                                                    Type_prestation = "A"
                                                Case officine.Vidal_LpprService.LpprServiceType.DELIVERY
                                                    Type_prestation = "V"
                                                Case officine.Vidal_LpprService.LpprServiceType.MAINTENANCE
                                                    Type_prestation = "E"
                                                Case officine.Vidal_LpprService.LpprServiceType.RENTING
                                                    Type_prestation = "L"
                                                Case officine.Vidal_LpprService.LpprServiceType.REPAIR
                                                    Type_prestation = "R"
                                                Case officine.Vidal_LpprService.LpprServiceType.SERVICE
                                                    Type_prestation = "S"
                                                Case Else
                                                    Type_prestation = "P"
                                            End Select
                                        Else
                                            Type_prestation = "A"
                                        End If
                                        Try
                                            COMPTOIR_MAIN.LPPTableAdapter.Insert(item.code, (item.refundBase * 100).ToString, "99999999", "00010101", False, item.actCode.ToString, Age_maximal, "", "", False, False, item.name, Montant_maximal, "", "", Quantité_maximale, "", Type_prestation)
                                        Catch ex As Exception
                                        End Try
                                    End If

                                    If est_un_kit = True Then
                                        Try
                                            COMPTOIR_MAIN.KitsTableAdapter.Insert("", Pack.name, item.code, Import_Produit_From_Vidal.My_LPPR_get_Coef(Vidal_id, item.code))
                                        Catch ex As Exception
                                        End Try
                                    End If
                                End If
                            End If
                        Next
                    End If
                    '
                    Dim CAB As String = Pack.cip13
                    If Not Pack.ean Is Nothing Then
                        If Not Pack.ean.ToString = "True" And Not Pack.ean.ToString = "False" Then
                            CAB = Pack.ean.ToString
                        End If
                    End If
                    Dim LPP_Quantité As Decimal = 0
                    If LPP_ <> "" And est_un_kit = False Then
                        LPP_Quantité = Import_Produit_From_Vidal.My_LPPR_get_Coef(Vidal_id, LPP_)
                    End If


                    Try
                        Me.ProduitsTableAdapter1.Insert(Cip, EAN13, CAB, désignation, Prix_vente_TTC, TVA.ToString, stock, LPP_, Contenance, Unitecontenance, _
                                                        Now, False, liste, Prestation, "", "", LPP_Quantité, Type_prestation, 100, False, 0, CIS, Prestation_actCodeValue, "", Vidal_id, communityAgrement, _
                                                     dispensationPlace, drugInSport, maxPrescriptionDuration, offMarketDate, onFreeAccess, onMarketDate, otc, _
                                                     prescriptionName, vidal_productId, withoutPrescr, "", False, False, 0, 0, "", "", est_un_kit, False, False, EAN13, "", 0, False, True, False, New Date(3000, 1, 1))
                    Catch ex As Exception
                        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation produit avec données Vidal" & vbCrLf & Cip, True)
                        sw.WriteLine(line)
                    End Try
                Else
                    sw.WriteLine(line)

                End If
            Else
                sw.WriteLine(line)

            End If


            line = sr.ReadLine
        End While
        sr.Close()
        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
    End Sub

    Private Sub StockverificationButton_Click(sender As Object, e As EventArgs) Handles StockverificationButton.Click
        MyMsg_Box.Presentation("Officine", "après insertion des produits, selectionnez le fichier initial pour verifier les stocks", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then Exit Sub
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\lgpi data"
        OpenFileDialog1.ShowDialog()
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim line = sr.ReadLine
        line = sr.ReadLine
        StockverificationButton.Text = 0
        Dim stock_total_avec_doublons = 0
        Dim total_stock_produits_absents = 0
        While Not line Is Nothing
            StockverificationButton.Text = (Val(StockverificationButton.Text) + 1).ToString
            Application.DoEvents()
            Dim Désignation = CSV_Champ(line, 1)
            Dim Cip = CSV_Champ(line, 3)
            Dim stock = CSV_Champ(line, 4)
            Dim CODE_GTIN13 = CSV_Champ(line, 2)
            If Désignation = "" And Cip = "" And CODE_GTIN13 = "" And Val(stock) <> 0 Then
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", "ligne sans valeur:" & line & vbCrLf, True)
            End If

            If Val(stock) <> 0 Then
                stock_total_avec_doublons = stock_total_avec_doublons + Val(stock)
                If Cip <> "" Then
                    Dim databycip = Me.ProduitsTableAdapter1.GetDataByCIP_or_EAN13_or_CAB(Cip)
                    If databycip.Rows.Count = 0 Then
                        If CODE_GTIN13 <> "" Then
                            Dim databyCODE_GTIN13 = Me.ProduitsTableAdapter1.GetDataProduits("@@@@@", CODE_GTIN13, "@@@@@", "@@@@@")
                            If databyCODE_GTIN13.Rows.Count = 0 Then
                                If Désignation <> "" Then
                                    Dim databyDésignation = Me.ProduitsTableAdapter1.GetDataProduits("@@@@@", "@@@@@", Désignation, "@@@@@")
                                    If databyDésignation.Rows.Count = 0 Then
                                        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", "cip:" & Cip & " ; CODE_GTIN13: " & CODE_GTIN13 & _
                                                                            " ; Désignation: " & Désignation & " ; absent bdd" & vbCrLf, True)
                                        total_stock_produits_absents = total_stock_produits_absents + Val(stock)
                                    Else
                                        If Val(databyDésignation.Item(0).Stock) <> Val(stock) Then
                                            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", "cip:" & Cip & " ; CODE_GTIN13: " & CODE_GTIN13 & _
                                                                            " ; Désignation: " & Désignation & " ; stock " & vbCrLf, True)
                                        End If
                                    End If
                                End If
                            Else
                                If Val(databyCODE_GTIN13.Item(0).Stock) <> Val(stock) Then
                                    My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", "cip:" & Cip & " ; CODE_GTIN13: " & CODE_GTIN13 & _
                                                                        " ; Désignation: " & Désignation & " ; stock " & Désignation & vbCrLf, True)
                                End If
                            End If
                        End If
                    Else
                        If Val(databycip.Item(0).Stock) <> Val(stock) Then
                            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", "cip:" & Cip & " ; CODE_GTIN13: " & CODE_GTIN13 & _
                                                                    " ; Désignation: " & Désignation & " ; stock " & vbCrLf, True)
                        End If
                    End If
                End If
            End If
            line = sr.ReadLine
        End While
        sr.Close()
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", vbCrLf & "stock_total_avec_doublons: " & stock_total_avec_doublons.ToString & vbCrLf, True)
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\verif_import_produit.txt", vbCrLf & "total_stock_produits_absents: " & total_stock_produits_absents.ToString & vbCrLf, True)
        StockverificationButton.Text = "Import verif"
    End Sub


    Private Sub AMC_Button_Click(sender As Object, e As EventArgs) Handles AMC_Button.Click
        OpenFileDialog1.ShowDialog()
        'AMC.csv: AMC;Rue1;Rue2;Code postal;Ville;Identifiant télétransmission;
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim line = sr.ReadLine
        line = sr.ReadLine
        While Not line Is Nothing
            Dim Intitulé = CSV_Champ(line, 1) 'Mid(line, 1, InStr(line, ";") - 1)
            'line = Mid(line, InStr(line, ";") + 1)
            Dim Num = CSV_Champ(line, 6) 'Mid(line, 1, InStr(line, ";") - 1)
            Dim adresse = CSV_Champ(line, 2) & " " & CSV_Champ(line, 3) & " " & CSV_Champ(line, 4) & " " & CSV_Champ(line, 5)
            If Num <> "" And IsNumeric(Num) Then
                COMPTOIR_MAIN.AmC_OrganismesTableAdapter1.Insert(Num, Intitulé, adresse, " ", "")
            End If
            line = sr.ReadLine
        End While
        sr.Close()
    End Sub

    Private Structure AMO
        Dim nom As String
        Dim code_régime As String
        Dim Caisse_Gestionnaire As String
        Dim Centre_Gestionnaire As String
    End Structure
    Private Structure G106
        Dim INSEE, Date_naiss, Rang_naiss, qualité As String
        Dim nombre_occurence As Integer
        'Dim C1_occu1, C2_occu1, C3_occu1, C4_occu1 As String
        'Dim C1_occu2, C2_occu2, C3_occu2, C4_occu2 As String
        'Dim C1_occu3, C2_occu3, C3_occu3, C4_occu3 As String
        'Dim C1_occu4, C2_occu4, C3_occu4, C4_occu4 As String
        'Dim C1_occu5, C2_occu5, C3_occu5, C4_occu5 As String
        'Dim C1_occu6, C2_occu6, C3_occu6, C4_occu6 As String
        'Dim C1_occu7, C2_occu7, C3_occu7, C4_occu7 As String
        Dim C1() As String
        Dim C2() As String
        Dim C3() As String
        Dim C4() As String
        Public Sub init()
            nombre_occurence = 1
            ReDim C1(8)
            ReDim C2(8)
            ReDim C3(8)
            ReDim C4(8)
        End Sub
    End Structure



    Private Sub ClientsButton_Click(sender As Object, e As EventArgs) Handles ClientsButton.Click

        '1) récuperer AMO.csv car danbs le fichier clients les AMO sont renseignés par intitulé

        'AMO.CSV: Nom AMO;CODE;Destinataire de Télétrans;Caisse Gestionnaire;Centre Gestionnaire;
        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "AMO.csv"
        OpenFileDialog1.ShowDialog()
        Dim fic_AMO = OpenFileDialog1.FileName
        Dim AMOs As New List(Of AMO)
        Dim AMO_sr = New StreamReader(fic_AMO, System.Text.Encoding.Default)
        Dim amo_line = AMO_sr.ReadLine
        amo_line = AMO_sr.ReadLine
        While Not amo_line Is Nothing
            Dim amo_item As AMO
            amo_item.nom = CSV_Champ(amo_line, 1)
            amo_item.code_régime = CSV_Champ(amo_line, 2)
            amo_item.Caisse_Gestionnaire = CSV_Champ(amo_line, 4)
            amo_item.Centre_Gestionnaire = CSV_Champ(amo_line, 5)
            If amo_item.Centre_Gestionnaire = "" Then
                amo_item.Centre_Gestionnaire = "0000"
            End If
            If Not AMOs.Contains(amo_item) And amo_item.code_régime.Length = 2 And amo_item.Caisse_Gestionnaire.Length = 3 And amo_item.Centre_Gestionnaire.Length = 4 Then
                AMOs.Add(amo_item)
            End If
            amo_line = AMO_sr.ReadLine
        End While
        AMO_sr.Close()

        '2) insérer les clients si pas déja en base
        'Numéro INSEE;Nom;Prénom;Date de naissance;Rang de naissance (rang Gémell;AMO;Code Sesam;Date de début couverture AMO;Date de fin couverture AMO;Qualité;Rue1;Code Postal;Ville;Tél portable;
        'AMC : n° télétransmission;Couverture AMC (mutuelle);Date de fin couverture AMC;Taux PH1;Taux PH2;Taux PH4;Taux PH7;Taux PHN;

        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "CLIENT-AMO-DATA.csv"
        OpenFileDialog1.ShowDialog()
        Dim fic_CLIENT_AMO_DATA = OpenFileDialog1.FileName
        Dim clients_sr = New StreamReader(fic_CLIENT_AMO_DATA, System.Text.Encoding.Default)
        Dim clients_sw = New StreamWriter("C:\Clients.csv")
        Dim line = clients_sr.ReadLine
        RichTextBox1.Clear()
        Dim compteur_line = 0
        Dim compteur_insert = 0
        Dim assuré As String = ""
        Dim groupes_106() As G106
        ReDim groupes_106(0)
        Dim groupes_106_index = 0

        While Not line Is Nothing And line <> ""
            compteur_line = compteur_line + 1
            RichTextBox1.Text = compteur_line.ToString & " lignes dans le fichier" & vbCrLf
            Application.DoEvents()
            Try


                Dim INSEE = Mid(CSV_Champ(line, 1), 1, 13)
                Dim INSEE_clé = Mid(CSV_Champ(line, 1), 14, 2)
                Dim G101C7 As String 'Type d'identification du porteur de la carte
                If Mid(INSEE, 2, 2) = Mid(CSV_Champ(line, 4), 7, 2) And Mid(INSEE, 4, 2) = Mid(CSV_Champ(line, 4), 3, 2) Then
                    G101C7 = "A" 'Le n° national d'immatriculation est celui de l'assuré
                Else
                    G101C7 = "B" 'Le n° national d'immatriculation est celui de l'ayant-droit
                End If
                Dim Nom = CSV_Champ(line, 2)
                Dim Prénom = CSV_Champ(line, 3)
                Dim Date_naiss = Mid(CSV_Champ(line, 4), 5, 4) & Mid(CSV_Champ(line, 4), 3, 2) & Mid(CSV_Champ(line, 4), 1, 2) & "0000"
                Dim Rang_naiss = CSV_Champ(line, 5)
                Dim AMO = CSV_Champ(line, 6)
                Dim code_ald = Mid(CSV_Champ(line, 7), 1, 1)
                Dim code_situ = Mid(CSV_Champ(line, 7), 2, 4)
                Dim début_AMO = CSV_Champ(line, 8)
                If début_AMO <> "" Then
                    début_AMO = Mid(début_AMO, 7, 4) & Mid(début_AMO, 4, 2) & Mid(début_AMO, 1, 2) & "0000"
                End If
                Dim fin_AMO = CSV_Champ(line, 9)
                If fin_AMO <> "" Then
                    fin_AMO = Mid(fin_AMO, 7, 4) & Mid(fin_AMO, 4, 2) & Mid(fin_AMO, 1, 2) & "0000"
                End If
                Dim qualité = CSV_Champ(line, 10)
                Select Case qualité
                    Case "Assuré"
                        qualité = "0"
                    Case "Ascendant, descendant, collatéraux ascendants"
                        qualité = "1"
                    Case "Conjoint"
                        qualité = "2"
                    Case "Conjoint divorcé"
                        qualité = "3"
                    Case "Concubin"
                        qualité = "4"
                    Case "Conjoint séparé"
                        qualité = "5"
                    Case "Enfant"
                        qualité = "6"
                    Case "Conjoint veuf"
                        qualité = "8"
                    Case "Autre ayant-droit"
                        qualité = "9"
                    Case Else
                        qualité = "0"
                End Select
                Dim adresse = CSV_Champ(line, 11) & " " & CSV_Champ(line, 12) & " " & CSV_Champ(line, 13)
                Dim télephone = CSV_Champ(line, 14)
                Dim NumAMC = CSV_Champ(line, 15)
                Dim intitulé_couvAMC = CSV_Champ(line, 16)

                Dim Id_couvAMC = ""
                If COMPTOIR_MAIN.AMC_Contrat_TypeTableAdapter.GetDataBy_Intitulé(intitulé_couvAMC).Rows.Count = 0 Then
                    System.Threading.Thread.Sleep(1000)
                    Id_couvAMC = Format(Now, "yyyyMMddHHmmssfff")
                    COMPTOIR_MAIN.AMC_Contrat_TypeTableAdapter.Insert(Id_couvAMC, intitulé_couvAMC, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, _
                                                                  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0, 100)
                Else
                    Id_couvAMC = COMPTOIR_MAIN.AMC_Contrat_TypeTableAdapter.GetDataBy_Intitulé(intitulé_couvAMC).Item(0).Id
                End If

                Dim fin_AMC = CSV_Champ(line, 17)
                If fin_AMC <> "" Then
                    fin_AMC = Mid(fin_AMC, 7, 4) & Mid(fin_AMC, 4, 2) & Mid(fin_AMC, 1, 2) & "0000"
                End If
                If Assurés.VitaleTableAdapter1.GetDataBénéficiaire(INSEE, Date_naiss, Rang_naiss, qualité).Rows.Count = 0 Then 'pour ne pas importer si déja en base par lecture carte vitale

                    Dim Code_régime = ""
                    Dim Caisse_gestionnaire = ""
                    Dim Centre_gestionnaire = ""
                    For Each item In AMOs
                        If item.nom = AMO Then
                            Code_régime = item.code_régime
                            While Code_régime.Length < 2
                                Code_régime = "0" & Code_régime
                            End While
                            Caisse_gestionnaire = item.Caisse_Gestionnaire
                            While Caisse_gestionnaire.Length < 3
                                Caisse_gestionnaire = "0" & Caisse_gestionnaire
                            End While
                            Centre_gestionnaire = item.Centre_Gestionnaire
                            While Centre_gestionnaire.Length < 4
                                Centre_gestionnaire = "0" & Centre_gestionnaire
                            End While
                            Exit For
                        End If
                    Next
                    If Val(Code_régime) <> 0 And Val(Caisse_gestionnaire) <> 0 Then
                        'Assurés.VitaleTableAdapter1.InsertDégradé("R", G101C7, INSEE, INSEE_clé, Code_régime, Caisse_gestionnaire, Centre_gestionnaire, _
                        '                                     Nom, "", Prénom, Date_naiss, Rang_naiss, qualité, "", "", "", "",
                        '                                     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                        '                                     "0", "202001010000", Caisse_gestionnaire, "", NumAMC, "", "", Id_couvAMC, "", "", "", "", "", "", "", "", "", "", "", adresse, télephone, "", "", "", BDD.VitaleTableAdapter.SELECT_Max_Num + 1, 0, "")
                        BDD.VitaleTableAdapter.Insert("", "", G101C7, INSEE, INSEE_clé, Code_régime, Caisse_gestionnaire, Centre_gestionnaire, "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                      Nom, "", Prénom, "", "", "", "", "", "", "", "", Date_naiss, Rang_naiss, qualité, _
                                                      "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                      "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", Format(Now, "yyyyMMdd") & "0000", fin_AMC, "", NumAMC, "0", Id_couvAMC, "0", _
                                                      "", "", "", "", "", "", "", "", "", "", "", "", "", adresse, _
                                                      "import lgpi", télephone, "", "", BDD.VitaleTableAdapter.SELECT_Max_Num + 1, 0, "", 0, "", "")

                        compteur_insert = compteur_insert + 1

                    End If
                Else

                End If
                'création de la liste des groupes 106 importés en update plus loin, avant leur importation on verifiera qu'il n'existe pas d'enregistrement 106 dans la bdd issus de la carte vitale
                Dim g106_in_liste_G106 As Boolean = False


                For r = 0 To groupes_106.Length - 1
                    If groupes_106(r).INSEE = INSEE And groupes_106(r).Date_naiss = Date_naiss And groupes_106(r).Rang_naiss = Rang_naiss And groupes_106(r).qualité = qualité Then

                        g106_in_liste_G106 = True
                        groupes_106(r).nombre_occurence = groupes_106(r).nombre_occurence + 1
                        groupes_106(r).C1(groupes_106(r).nombre_occurence) = début_AMO
                        groupes_106(r).C2(groupes_106(r).nombre_occurence) = fin_AMO
                        groupes_106(r).C3(groupes_106(r).nombre_occurence) = code_ald
                        groupes_106(r).C4(groupes_106(r).nombre_occurence) = code_situ

                        Exit For

                    End If
                Next

                If g106_in_liste_G106 = False Then

                    Dim item As New G106
                    item.init()
                    item.INSEE = INSEE
                    item.Date_naiss = Date_naiss
                    item.Rang_naiss = Rang_naiss
                    item.qualité = qualité
                    item.C1(1) = début_AMO
                    item.C2(1) = fin_AMO
                    item.C3(1) = code_ald
                    item.C4(1) = code_situ
                    ReDim Preserve groupes_106(groupes_106.Length)
                    groupes_106(groupes_106.Length - 1) = item

                End If
            Catch ex As Exception
                clients_sw.WriteLine(line)
            End Try

            line = clients_sr.ReadLine
        End While
        clients_sr.Close()


        Dim compteur_groupes_106 = 0
        For Each item In groupes_106
            If item.INSEE <> "" Then
                compteur_groupes_106 = compteur_groupes_106 + 1
                Dim assuré_data = Assurés.VitaleTableAdapter1.GetDataBénéficiaire(item.INSEE, item.Date_naiss, item.Rang_naiss, item.qualité)
                If assuré_data.Rows.Count > 1 Then
                    RichTextBox2.AppendText(item.INSEE & " " & item.Date_naiss & " " & item.Rang_naiss & " " & item.qualité & " --> PLUSIEURS ENREGISTREMENTS")
                ElseIf assuré_data.Rows.Count = 1 Then

                    Try 'plantage sur les valeurs DBNULL des enregistrements déja en base fait à partir de carte vitale, il ne faut rien enregitrer

                        If Val(assuré_data.Item(0).G106C3O1) = 0 And Val(assuré_data.Item(0).G106C4O1) = 0 And _
                     Val(assuré_data.Item(0).G106C3O2) = 0 And Val(assuré_data.Item(0).G106C4O2) = 0 And _
                     Val(assuré_data.Item(0).G106C3O3) = 0 And Val(assuré_data.Item(0).G106C4O3) = 0 And _
                     Val(assuré_data.Item(0).G106C3O4) = 0 And Val(assuré_data.Item(0).G106C4O4) = 0 And _
                     Val(assuré_data.Item(0).G106C3O5) = 0 And Val(assuré_data.Item(0).G106C4O5) = 0 And _
                     Val(assuré_data.Item(0).G106C3O6) = 0 And Val(assuré_data.Item(0).G106C4O6) = 0 And _
                     Val(assuré_data.Item(0).G106C3O7) = 0 And Val(assuré_data.Item(0).G106C4O7) = 0 Then 'si il y a des valeurs c'est un enregistrement précedent fait en production réelle à ne pas remplacer
                            For r = 1 To 7
                                If String.IsNullOrEmpty(item.C1(r)) Then item.C1(r) = ""
                                If String.IsNullOrEmpty(item.C2(r)) Then item.C2(r) = ""
                                If String.IsNullOrEmpty(item.C3(r)) Then item.C3(r) = ""
                                If String.IsNullOrEmpty(item.C4(r)) Then item.C4(r) = ""
                            Next
                            Assurés.VitaleTableAdapter1.Update_groupes106(item.C1(1), item.C2(1), item.C3(1), item.C4(1), _
                                                                          item.C1(2), item.C2(2), item.C3(2), item.C4(2), _
                                                                          item.C1(3), item.C2(3), item.C3(3), item.C4(3), _
                                                                          item.C1(4), item.C2(4), item.C3(4), item.C4(4), _
                                                                          item.C1(5), item.C2(5), item.C3(5), item.C4(5), _
                                                                          item.C1(6), item.C2(6), item.C3(6), item.C4(6), _
                                                                          item.C1(7), item.C2(7), item.C3(7), item.C4(7), _
                                                                          item.INSEE, item.Date_naiss, item.Rang_naiss, item.qualité)

                            RichTextBox1.Text = "Importation des droits AMO " & compteur_groupes_106.ToString & "/" & groupes_106.Count.ToString
                            Application.DoEvents()
                        End If
                    Catch ex As Exception

                    End Try
                    'End If
                End If
            End If
        Next

        RichTextBox1.Text = "Importation clients terminée" & vbCrLf & compteur_line.ToString & " lignes dans le fichier" & vbCrLf & _
            compteur_insert.ToString & " enregistrements importés" & vbCrLf



    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "commentaires individuels.csv" 'Nom;Prénom;Commentaire individuel;
        OpenFileDialog1.ShowDialog()
        Dim fic = OpenFileDialog1.FileName
        Dim sr = New StreamReader(fic, System.Text.Encoding.UTF7)
        Dim line = sr.ReadLine
        line = sr.ReadLine
        While Not line Is Nothing
            If line <> ";" And line <> "" Then
                Dim data = line
                While (data.Length - (Replace(data, ";", "").Length)) < 3
                    data = data & " " & sr.ReadLine
                End While
                RichTextBox1.Text = data
                Application.DoEvents()
                COMPTOIR_MAIN.VitaleTableAdapter.Update_Notes_perso_by_nom_prénom(CSV_Champ(data, 3), CSV_Champ(data, 1), CSV_Champ(data, 2))
            End If
            line = sr.ReadLine
        End While

        sr.Close()



    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "commentaires famille.csv" 'Numéro INSEE;Commentaire famille;
        OpenFileDialog1.ShowDialog()
        Dim fic = OpenFileDialog1.FileName
        Dim sr = New StreamReader(fic, System.Text.Encoding.UTF7)
        Dim line = sr.ReadLine
        line = sr.ReadLine
        While Not line Is Nothing
            If line <> ";" And line <> "" Then
                Dim data = line
                While (data.Length - (Replace(data, ";", "").Length)) < 2
                    data = data & " " & sr.ReadLine
                End While
                RichTextBox1.Text = data
                Application.DoEvents()
                COMPTOIR_MAIN.VitaleTableAdapter.Update_Notes_perso_by_NIR(". " & CSV_Champ(data, 2), Mid(CSV_Champ(data, 1), 1, 13))
            End If
            line = sr.ReadLine
        End While

        sr.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If Form1.Stock_iniTableAdapter.GetData.Rows.Count = 0 Then
            Dim compteur = 0

            Dim bp As New StreamReader("C:\base produits.CSV", System.Text.Encoding.UTF7)
            Dim bpline = bp.ReadLine
            bpline = bp.ReadLine
            While Not bpline Is Nothing
                compteur = compteur + 1
                RichTextBox1.Text = compteur.ToString & " lignes fichier"
                Application.DoEvents()
                Dim stock = CSV_Champ(bpline, 4)
                If stock = "" Then
                    stock = "0"
                End If
                Form1.Stock_iniTableAdapter.Insert(CSV_Champ(bpline, 1), CSV_Champ(bpline, 2), CSV_Champ(bpline, 3), CDec(stock))
                bpline = bp.ReadLine
            End While
            bp.Close()
        End If


        Dim base = ProduitsTableAdapter1.GetData
        Dim compteur_no_change = 0
        Dim compteur_stock_0 = 0
        Dim compteur_stock_ini = 0
        For r = 0 To base.Rows.Count - 1
            RichTextBox1.Text = "stock initial: " & (r + 1).ToString & "/" & base.Rows.Count.ToString & vbCrLf & "cip7:" & base(r).CIP & vbCrLf & "ean13:" & base(r).EAN13 & vbCrLf & base(r).Designation
            Application.DoEvents()
            If base(r).CIP = "" Or base(r).EAN13 = "" Then

                Dim good_cip As String = ""
                If base(r).CIP <> "" Then
                    good_cip = base(r).CIP
                Else
                    good_cip = base(r).EAN13
                End If

                Select Case Form1.Stock_iniTableAdapter.GetData_stock(base(r).CIP, base(r).EAN13, base(r).CAB).Rows.Count
                    Case 0
                        compteur_stock_0 = compteur_stock_0 + 1
                        Me.ProduitsTableAdapter1.Set_Stock(0, good_cip, base(r).CAB, base(r).Designation)
                    Case Else
                        compteur_stock_ini = compteur_stock_ini + 1
                        Me.ProduitsTableAdapter1.Set_Stock(Form1.Stock_iniTableAdapter.select_max_stock(base(r).CIP, base(r).EAN13, base(r).CAB), good_cip, base(r).CAB, base(r).Designation)


                End Select



                'Dim sr As New StreamReader("C:\base produits.CSV", System.Text.Encoding.UTF7)
                'Dim line = sr.ReadLine
                'line = sr.ReadLine
                'Dim found As Boolean = False
                'While Not line Is Nothing And found = False
                '    RichTextBox2.Text = line
                '    Application.DoEvents()
                '    Dim Désignation = CSV_Champ(line, 1)
                '    Dim CODE_GTIN13 = CSV_Champ(line, 2)
                '    Dim Cip = CSV_Champ(line, 3)
                '    Dim stock = CSV_Champ(line, 4)
                '    If stock = "" Then
                '        stock = "0"
                '    End If
                'If (Cip <> "" And (base(r).CIP = Cip Or base(r).EAN13 = Cip)) Or (CODE_GTIN13 <> "" And base(r).CAB = CODE_GTIN13) Then
                '    Me.ProduitsTableAdapter1.Set_Stock(CDec(stock), Cip, CODE_GTIN13, Désignation)
                '    found = True
                'End If
                'line = sr.ReadLine
                'End While
                'sr.Close()
                'If found = False Then
                '    Me.ProduitsTableAdapter1.Set_Stock(0, base(r).CIP, base(r).CAB, base(r).Designation)
                'End If
            Else
                compteur_no_change = compteur_no_change + 1
            End If
        Next
        RichTextBox2.Text = Me.ProduitsTableAdapter1.GetData.Rows.Count.ToString & "réferences dans la bdd" & vbCrLf _
            & compteur_no_change.ToString & " réferences à ne pas traiter" & vbCrLf & compteur_stock_ini.ToString & _
            " réferences stock ini" & vbCrLf & compteur_stock_0.ToString & " réferences stock 0" & vbCrLf

        Dim compteur_ajust_actes = 0
        Dim actes = COMPTOIR_MAIN.ActesTableAdapter.GetData
        For r = 0 To actes.Rows.Count - 1
            If actes.Item(r).NumActe <> 0 Then
                RichTextBox1.Text = "ajuste stock sur ventes: " & (r + 1).ToString & "/" & actes.Rows.Count.ToString
                Application.DoEvents()
                If InStr(actes(r).Désignation, "Honoraire") = 0 Then
                    If actes(r).CIP = "" And actes(r).EAN13 = "" Then
                        Me.ProduitsTableAdapter1.UpdateStock_by_Designation((-1) * Val(actes(r).Quantité), actes(r).Désignation)
                        compteur_ajust_actes = compteur_ajust_actes + 1
                    ElseIf actes(r).CIP = "" And actes(r).EAN13 <> "" Then
                        Me.ProduitsTableAdapter1.UpdateStock_By_CIP_CAB((-1) * Val(actes(r).Quantité), actes(r).CIP, "")
                        compteur_ajust_actes = compteur_ajust_actes + 1
                    ElseIf actes(r).CIP <> "" And actes(r).EAN13 = "" Then
                        Me.ProduitsTableAdapter1.UpdateStock_By_CIP_CAB((-1) * Val(actes(r).Quantité), actes(r).EAN13, "")
                        compteur_ajust_actes = compteur_ajust_actes + 1
                    End If
                End If
            End If
        Next
        RichTextBox2.Text = RichTextBox2.Text & vbCrLf & actes.Count.ToString & " Actes en base" & vbCrLf & compteur_ajust_actes.ToString & " ajustements sur acte" & vbCrLf

        Dim compteur_ajust_commandes = 0
        Dim lignes_commandes = Commandes_PharmaML.Commandes_LignesTableAdapter.GetData
        For r = 0 To lignes_commandes.Rows.Count - 1
            RichTextBox1.Text = "ajuste stock sur commandes: " & (r + 1).ToString & "/" & lignes_commandes.Rows.Count.ToString
            Application.DoEvents()
            If lignes_commandes(r).livré_Code_Produit <> "" And lignes_commandes(r).reçu > 0 Then
                Dim prod = Me.ProduitsTableAdapter1.GetDataByCIP_or_EAN13_or_CAB(lignes_commandes(r).livré_Code_Produit).Item(0)
                If ((prod.CIP = "" Or prod.EAN13 = "") And (prod.CIP <> "" Or prod.EAN13 <> "")) Or (prod.CIP = "" And prod.EAN13 = "") Then
                    compteur_ajust_commandes = compteur_ajust_commandes + 1
                    Me.ProduitsTableAdapter1.UpdateStock_By_CIP_CAB(lignes_commandes(r).reçu, lignes_commandes(r).livré_Code_Produit, lignes_commandes(r).livré_Code_Produit)
                End If
            End If
        Next
        RichTextBox2.Text = RichTextBox2.Text & vbCrLf & lignes_commandes.Count.ToString & " lignes commandes en base" & vbCrLf & compteur_ajust_commandes.ToString & " ajustements sur commandes" & vbCrLf
        RichTextBox1.Text = "FIN"

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        PRODUITS_bdd.MAJ_globale_Dernier_PAHT_remisé()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\recup data"
        OpenFileDialog1.ShowDialog()
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim line = sr.ReadLine
        Bandeau_info_bas_show("Debut importation")
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", vbCrLf & "import produits " & Now.ToLongDateString & "  " & OpenFileDialog1.FileName & vbCrLf, True)
        Dim compteur = 0
        While Not line Is Nothing
            compteur = compteur + 1
            Bandeau_info_bas_change_text(compteur.ToString)

            Dim Cip_ = Trim(CSV_Champ(line, 2))

            If Cip_.Length = 15 Then
                Cip_ = Mid(Cip_, 1, 5) & Mid(Cip_, 7, 7) & Mid(Cip_, 15, 1)
            Else
                Cip_ = ""
            End If

            Dim stock As Integer = 0
            Try
                stock = CInt(Trim(CSV_Champ(line, 3)))
            Catch ex As Exception

            End Try

            Dim prixvente As Decimal = 0
            Try
                prixvente = CDec(Trim(CSV_Champ(line, 5)))
            Catch ex As Exception

            End Try


            If IsNumeric(Cip_) Then

                Select Case Me.ProduitsTableAdapter1.GetDataByCIP_or_EAN13_or_CAB(Cip_).Rows.Count
                    Case 1
                        Me.ProduitsTableAdapter1.Set_Stock_By_CIP_CAB(stock, Cip_, Cip_)
                        Me.ProduitsTableAdapter1.set_prx_vente(prixvente, Cip_, Cip_)
                    Case 0
                        Dim Vidal_PackService As New Vidal_PackService.PackService
                        Vidal_PackService.Timeout = 60000 '3000
                        Dim Vidal_id As Integer
                        Dim CIP As String = ""
                        Dim EAN13 As String = ""
                        Dim désignation As String = ""
                        Dim TVA As Single
                        Dim Contenance As String = ""
                        Dim Unitecontenance As String = ""
                        Dim Assimilestupefiant = "0"
                        Dim Prestation As String = ""
                        Dim liste As String = ""
                        Dim Prestation_actCodeValue As String = ""
                        Dim CIS As String = ""
                        Dim communityAgrement As Boolean
                        Dim dispensationPlace As String = ""
                        Dim drugInSport As Boolean
                        Dim maxPrescriptionDuration As String = ""
                        Dim prescriptionName As String = ""
                        Dim offMarketDate As Date
                        Dim onFreeAccess As Boolean
                        Dim onMarketDate As Date
                        Dim otc As Boolean
                        Dim vidal_productId As Integer
                        Dim withoutPrescr As Boolean


                        Dim Pack = Vidal_PackService.searchByCip(Cip_)


                        If Pack Is Nothing Then
                            Dim CODE_GTIN13() As String
                            ReDim CODE_GTIN13(1)
                            CODE_GTIN13(0) = Cip_
                            If Vidal_PackService.searchByCodes(CODE_GTIN13).Count > 0 Then
                                Pack = Vidal_PackService.searchByCodes(CODE_GTIN13)(0)
                            End If
                        End If
                        If Not Pack Is Nothing Then
                            Vidal_id = Pack.id
                            If Not Pack.cip Is Nothing Then
                                CIP = Pack.cip
                            Else
                                CIP = ""
                            End If
                            If Not Pack.cip13 Is Nothing Then
                                EAN13 = Pack.cip13
                            Else
                                EAN13 = ""
                            End If

                            désignation = Pack.name
                            If Not Pack.vatRateValue Is Nothing Then
                                TVA = Pack.vatRateValue
                            Else
                                TVA = 999
                            End If
                            If Not Pack.dose Is Nothing Then If Not Pack.dose.ToString = "True" Then If Not Pack.dose.ToString = "False" Then Contenance = Pack.dose
                            If Not Pack.doseUnit Is Nothing Then If Not Pack.doseUnit.ToString = "True" Then If Not Pack.doseUnit.ToString = "False" Then Unitecontenance = Pack.doseUnit
                            If Pack.narcoticPrescription = True Then Assimilestupefiant = "1"
                            If Not Pack.actCode Is Nothing Then If Not Pack.actCode.ToString = "True" Then If Not Pack.actCode.ToString = "False" Then Prestation = Pack.actCode.ToString
                            If Not Pack.listValue Is Nothing Then If Not Pack.listValue.ToString = "True" Then If Not Pack.listValue.ToString = "False" Then liste = Pack.listValue.ToString
                            If Not Pack.actCodeValue Is Nothing Then If Not Pack.actCodeValue.ToString = "True" Then If Not Pack.actCodeValue.ToString = "False" Then Prestation_actCodeValue = Pack.actCodeValue.ToString
                            If Not Pack.cis Is Nothing Then If Not Pack.cis.ToString = "True" Then If Not Pack.cis.ToString = "False" Then If Not Pack.cis.ToString = "False" Then CIS = Pack.cis.ToString
                            communityAgrement = Pack.communityAgrement
                            If Not Pack.dispensationPlace Is Nothing Then dispensationPlace = Pack.dispensationPlace.ToString
                            drugInSport = Pack.drugInSport
                            If Not Pack.maxPrescriptionDuration Is Nothing Then If Not Pack.maxPrescriptionDuration.ToString = "True" Then If Not Pack.maxPrescriptionDuration.ToString = "False" _
                                Then maxPrescriptionDuration = Pack.maxPrescriptionDuration.ToString
                            prescriptionName = Pack.prescriptionName
                            If Not Pack.offMarketDate Is Nothing Then offMarketDate = Pack.offMarketDate
                            onFreeAccess = Pack.onFreeAccess
                            If Not Pack.onMarketDate Is Nothing Then onMarketDate = Pack.onMarketDate
                            otc = Pack.otc
                            vidal_productId = Pack.productId
                            withoutPrescr = Pack.withoutPrescr
                            Dim Vidal_LpprService As New Vidal_LpprService.LpprService
                            Vidal_LpprService.Timeout = 60000 '3000
                            Dim lpp = Vidal_LpprService.searchByPackId(Vidal_id)

                            Dim est_un_kit As Boolean = False
                            Dim Type_prestation As String = ""
                            If Not lpp Is Nothing And lpp.Count > 0 Then
                                If lpp.Count > 1 Then est_un_kit = True
                                For Each item In lpp
                                    If Not item Is Nothing Then
                                        If Not item.code Is Nothing Then


                                            If COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(item.code, Format(Now, "yyyyMMdd")).Rows.Count = 0 Then

                                                Dim Age_maximal As String = ""
                                                If Not item.ageMax Is Nothing Then If Not item.ageMax.ToString = "True" Then If Not item.ageMax.ToString = "False" Then Age_maximal = item.ageMax.ToString
                                                Dim Montant_maximal As String = ""
                                                If Not item.salePriceLimit Is Nothing Then If Not item.salePriceLimit.ToString = "True" Then If Not item.salePriceLimit.ToString = "False" Then Montant_maximal = (item.salePriceLimit * 100).ToString
                                                Dim Quantité_maximale As String = ""
                                                If Not item.amountMax Is Nothing Then If Not item.amountMax.ToString = "True" Then If Not item.amountMax.ToString = "False" Then Quantité_maximale = item.amountMax.ToString
                                                If Not item.service Is Nothing Then
                                                    Select Case item.service.Value
                                                        Case officine.Vidal_LpprService.LpprServiceType.BUYING
                                                            Type_prestation = "A"
                                                        Case officine.Vidal_LpprService.LpprServiceType.DELIVERY
                                                            Type_prestation = "V"
                                                        Case officine.Vidal_LpprService.LpprServiceType.MAINTENANCE
                                                            Type_prestation = "E"
                                                        Case officine.Vidal_LpprService.LpprServiceType.RENTING
                                                            Type_prestation = "L"
                                                        Case officine.Vidal_LpprService.LpprServiceType.REPAIR
                                                            Type_prestation = "R"
                                                        Case officine.Vidal_LpprService.LpprServiceType.SERVICE
                                                            Type_prestation = "S"
                                                        Case Else
                                                            Type_prestation = "P"
                                                    End Select
                                                Else
                                                    Type_prestation = "A"
                                                End If
                                                Try
                                                    COMPTOIR_MAIN.LPPTableAdapter.Insert(item.code, (item.refundBase * 100).ToString, "99999999", "00010101", False, item.actCode.ToString, Age_maximal, "", "", False, False, item.name, Montant_maximal, "", "", Quantité_maximale, "", Type_prestation)

                                                Catch ex As Exception
                                                    My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation code LPP pour prodduit " & Cip_ & vbCrLf, True)
                                                End Try
                                            End If

                                            If est_un_kit = True Then
                                                Try
                                                    COMPTOIR_MAIN.KitsTableAdapter.Insert("", Pack.name, item.code, Import_Produit_From_Vidal.My_LPPR_get_Coef(Vidal_id, item.code))
                                                Catch ex As Exception
                                                    My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation Kit pour prodduit " & Cip_ & vbCrLf, True)
                                                End Try
                                            End If
                                        Else
                                            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation data LPP Vidal pour produit " & Cip_ & vbCrLf, True)
                                        End If
                                    Else
                                        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation data LPP Vidal pour produit " & Cip_ & vbCrLf, True)
                                    End If
                                Next

                            End If
                            '
                            Dim CAB As String = Pack.cip13
                            If Not Pack.ean Is Nothing Then
                                If Not Pack.ean.ToString = "True" And Not Pack.ean.ToString = "False" Then
                                    CAB = Pack.ean.ToString
                                End If
                            End If
                            Dim prod_lpp = ""
                            Dim LPP_Quantité = 0
                            If est_un_kit = False And lpp.Count > 0 Then
                                prod_lpp = lpp(0).code
                                LPP_Quantité = 1
                            End If


                            Try
                                Me.ProduitsTableAdapter1.Insert(CIP, EAN13, CAB, désignation, 999, TVA.ToString, stock, prod_lpp, Contenance, Unitecontenance, _
                                                                Now, False, liste, Prestation, "", "", LPP_Quantité, Type_prestation, 100, False, 0, CIS, Prestation_actCodeValue, "", Vidal_id, communityAgrement, _
                                                             dispensationPlace, drugInSport, maxPrescriptionDuration, offMarketDate, onFreeAccess, onMarketDate, otc, _
                                                             prescriptionName, vidal_productId, withoutPrescr, "", False, False, 0, 0, "", "", est_un_kit, False, False, EAN13, "", 0, False, True, False, New Date(3000, 1, 1))
                                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "importé " & Cip_ & vbCrLf, True)
                                If est_un_kit = True Then
                                    My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "kit " & Cip_ & vbCrLf, True)
                                End If
                            Catch ex As Exception
                                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "erreur d'importation produit avec données Vidal " & Cip_ & vbCrLf, True)
                            End Try
                        Else
                            Me.ProduitsTableAdapter1.Insert("", "", Cip_, CSV_Champ(line, 1), prixvente.ToString, "20", stock, "", "", "", Nothing, False, "", "", "", "", 0, "", 0, False, 0, "", "", "", 0, False, "", False, "", Nothing, True, Nothing, True, "", 0, True, "", False, False, 0, 0, "", "", False, True, True, "", "", 0, False, True, False, Nothing)
                            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", "Pas de données Vidal pour " & Cip_ & vbCrLf, True)


                        End If


















                End Select
            End If
            line = sr.ReadLine
        End While
        sr.Close()
        Bandeau_info_bas_hide()

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\lgpi data"
        OpenFileDialog1.ShowDialog()
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim line = sr.ReadLine
        Bandeau_info_bas_show("Debut importation")
        line = sr.ReadLine
        Dim compteur = 0
        While Not line Is Nothing
            compteur = compteur + 1
            Bandeau_info_bas_change_text(compteur.ToString)
            'Libell‚ du Produit            ;Code CIP13       ;St.R‚el;CIP7    ;PxAchaCat;Cd.EAN13         ;Tx TVA;PU vente ;

            Dim prod As String = ""
            Dim ean13 As String = ""
            Dim stock As Integer = 0
            Dim cip7 As String = ""
            Dim cab As String = ""
            Dim tva As Decimal
            Dim prix As Decimal = 0
            Dim import = True

            Try
                prod = CSV_Champ(line, 1)
                ean13 = Trim(CSV_Champ(line, 2))
                stock = CInt(Trim(CSV_Champ(line, 3)))
                cip7 = Trim(CSV_Champ(line, 4))
                cab = Trim(CSV_Champ(line, 6))
                tva = CDec(Trim(Replace(CSV_Champ(line, 7), "%", "")))
                prix = CDec(Trim(CSV_Champ(line, 8)))
            Catch ex As Exception
                import = False
            End Try
            Try
                If import = True Then


                    If stock > 0 Then



                        Dim prod_row As PharmaDataSet.ProduitsRow
                        If ean13 <> "" And BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(ean13).Rows.Count = 1 Then
                            prod_row = BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(ean13).Item(0)
                        ElseIf cab <> "" And BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(cab).Rows.Count = 1 Then
                            prod_row = BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(cab).Item(0)
                        ElseIf cip7 <> "" And BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(cip7).Rows.Count = 1 Then
                            prod_row = BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(cip7).Item(0)
                        Else
                            prod_row = Nothing
                        End If

                        If Not prod_row Is Nothing Then
                            If prod_row.PrixventeTTC = "999" Then
                                If prod_row.EAN13 <> "" Then
                                    BDD.ProduitsTableAdapter.set_PrixventeTTC(prix.ToString, prod_row.EAN13, prod_row.CAB, prod_row.Designation)
                                Else
                                    BDD.ProduitsTableAdapter.set_PrixventeTTC(prix.ToString, prod_row.CIP, prod_row.CAB, prod_row.Designation)
                                End If

                            End If
                        Else
                            BDD.ProduitsTableAdapter.Insert(cip7, ean13, cab, prod, prix.ToString, tva.ToString, stock, "", "", "", "", "", "", "", "", "", 0, "", 100, False, 0, "", "", "", _
                                                            0, False, "", False, "", Now, True, Now, False, "", 0, True, "", False, False, 0, 0, "", "", False, True, False, "", "", 0, False, True, False, New Date(3000, 1, 1))
                        End If
                    End If
                End If
            Catch ex As Exception
                My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\import_produit.txt", line.ToString & vbCrLf & ex.Message & vbCrLf & vbCrLf, True)
            End Try
            line = sr.ReadLine
        End While
        Bandeau_info_bas_hide()
    End Sub

    Public Sub MAJ_Produits_Vidal()
        RichTextBox1.Clear()
        RichTextBox2.Clear()
        Bandeau_info_bas_show("MAJ_Produits_Vidal")
        Dim produits = BDD.ProduitsTableAdapter.GetData
        For r = 0 To produits.Rows.Count - 1
            Try


                Bandeau_info_bas_change_text("MAJ_Produits_Vidal " & (r + 1).ToString & "/" & produits.Rows.Count.ToString)
                Dim code_produit_list As New List(Of String)
                If produits(r).CIP <> "" Then
                    code_produit_list.Add(produits(r).CIP)
                End If
                If produits(r).EAN13 <> "" Then
                    code_produit_list.Add(produits(r).EAN13)
                End If
                If produits(r).CAB <> "" Then
                    code_produit_list.Add(produits(r).CAB)
                End If
                If code_produit_list.Count > 0 Then
                    Dim code_produit_tab() As String
                    ReDim code_produit_tab(code_produit_list.Count)
                    For j = 0 To code_produit_list.Count - 1
                        code_produit_tab(j) = code_produit_list(j)
                    Next
                    Dim Vidal_PackService As New Vidal_PackService.PackService
                    Vidal_PackService.Timeout = 60000
                    Dim packs = Vidal_PackService.searchByCodes(code_produit_tab)
                    If Not packs Is Nothing Then
                        If packs.Count > 0 Then
                            Dim Vidal_id As Integer
                            Dim CIP As String = ""
                            Dim EAN13 As String = ""
                            Dim CAB As String = ""
                            Dim désignation As String = ""
                            Dim TVA As Single
                            Dim Contenance As String = ""
                            Dim Unitecontenance As String = ""
                            Dim Assimilestupefiant = "0"
                            Dim Prestation As String = ""
                            Dim liste As String = ""
                            Dim Prestation_actCodeValue As String = ""
                            Dim CIS As String = ""
                            Dim communityAgrement As Boolean
                            Dim dispensationPlace As String = ""
                            Dim drugInSport As Boolean
                            Dim maxPrescriptionDuration As String = ""
                            Dim prescriptionName As String = ""
                            Dim offMarketDate As Date
                            Dim onFreeAccess As Boolean
                            Dim onMarketDate As Date
                            Dim otc As Boolean
                            Dim vidal_productId As Integer
                            Dim withoutPrescr As Boolean
                            Dim LPP_Quantité As Decimal = 0
                            Dim Lpp_Code As String = ""
                            Dim Pack = packs(0)

                            Vidal_id = Pack.id
                            If Not Pack.cip Is Nothing Then
                                CIP = Pack.cip
                            Else
                                CIP = ""
                            End If
                            If Not Pack.cip13 Is Nothing Then
                                EAN13 = Pack.cip13
                            Else
                                EAN13 = ""
                            End If
                            If Not Pack.ean Is Nothing Then
                                CAB = Pack.ean
                            End If
                            désignation = Pack.name
                            If Not Pack.vatRateValue Is Nothing Then
                                TVA = Pack.vatRateValue
                            Else
                                TVA = 0
                            End If
                            If Not Pack.dose Is Nothing Then If Not Pack.dose.ToString = "True" Then If Not Pack.dose.ToString = "False" Then Contenance = Pack.dose
                            If Not Pack.doseUnit Is Nothing Then If Not Pack.doseUnit.ToString = "True" Then If Not Pack.doseUnit.ToString = "False" Then Unitecontenance = Pack.doseUnit
                            If Pack.narcoticPrescription = True Then Assimilestupefiant = "1"
                            If Not Pack.actCode Is Nothing Then If Not Pack.actCode.ToString = "True" Then If Not Pack.actCode.ToString = "False" Then Prestation = Pack.actCode.ToString
                            If Not Pack.listValue Is Nothing Then If Not Pack.listValue.ToString = "True" Then If Not Pack.listValue.ToString = "False" Then liste = Pack.listValue.ToString
                            If Not Pack.actCodeValue Is Nothing Then If Not Pack.actCodeValue.ToString = "True" Then If Not Pack.actCodeValue.ToString = "False" Then Prestation_actCodeValue = Pack.actCodeValue.ToString
                            If Not Pack.cis Is Nothing Then If Not Pack.cis.ToString = "True" Then If Not Pack.cis.ToString = "False" Then If Not Pack.cis.ToString = "False" Then CIS = Pack.cis.ToString
                            communityAgrement = Pack.communityAgrement
                            If Not Pack.dispensationPlace Is Nothing Then dispensationPlace = Pack.dispensationPlace.ToString
                            drugInSport = Pack.drugInSport
                            If Not Pack.maxPrescriptionDuration Is Nothing Then If Not Pack.maxPrescriptionDuration.ToString = "True" Then If Not Pack.maxPrescriptionDuration.ToString = "False" _
                                Then maxPrescriptionDuration = Pack.maxPrescriptionDuration.ToString
                            prescriptionName = Pack.prescriptionName
                            If Not Pack.offMarketDate Is Nothing Then offMarketDate = Pack.offMarketDate
                            onFreeAccess = Pack.onFreeAccess
                            If Not Pack.onMarketDate Is Nothing Then onMarketDate = Pack.onMarketDate
                            otc = Pack.otc
                            vidal_productId = Pack.productId
                            withoutPrescr = Pack.withoutPrescr
                            Dim Vidal_LpprService As New Vidal_LpprService.LpprService
                            Vidal_LpprService.Timeout = 60000 '3000
                            Dim lpp = Vidal_LpprService.searchByPackId(Vidal_id)

                            Dim est_un_kit As Boolean = False
                            Dim Type_prestation As String = ""
                            If Not lpp Is Nothing And lpp.Count > 0 Then
                                If lpp.Count > 1 Then
                                    est_un_kit = True
                                    COMPTOIR_MAIN.KitsTableAdapter.Delete_by_Désignation(Pack.name)
                                End If

                                For Each item In lpp
                                    If Not item Is Nothing Then
                                        If Not item.code Is Nothing Then


                                            If COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(item.code, Format(Now, "yyyyMMdd")).Rows.Count = 0 Then

                                                Dim Age_maximal As String = ""
                                                If Not item.ageMax Is Nothing Then If Not item.ageMax.ToString = "True" Then If Not item.ageMax.ToString = "False" Then Age_maximal = item.ageMax.ToString
                                                Dim Montant_maximal As String = ""
                                                If Not item.salePriceLimit Is Nothing Then If Not item.salePriceLimit.ToString = "True" Then If Not item.salePriceLimit.ToString = "False" Then Montant_maximal = (item.salePriceLimit * 100).ToString
                                                Dim Quantité_maximale As String = ""
                                                If Not item.amountMax Is Nothing Then If Not item.amountMax.ToString = "True" Then If Not item.amountMax.ToString = "False" Then Quantité_maximale = item.amountMax.ToString
                                                If Not item.service Is Nothing Then
                                                    Select Case item.service.Value
                                                        Case officine.Vidal_LpprService.LpprServiceType.BUYING
                                                            Type_prestation = "A"
                                                        Case officine.Vidal_LpprService.LpprServiceType.DELIVERY
                                                            Type_prestation = "V"
                                                        Case officine.Vidal_LpprService.LpprServiceType.MAINTENANCE
                                                            Type_prestation = "E"
                                                        Case officine.Vidal_LpprService.LpprServiceType.RENTING
                                                            Type_prestation = "L"
                                                        Case officine.Vidal_LpprService.LpprServiceType.REPAIR
                                                            Type_prestation = "R"
                                                        Case officine.Vidal_LpprService.LpprServiceType.SERVICE
                                                            Type_prestation = "S"
                                                        Case Else
                                                            Type_prestation = "P"
                                                    End Select
                                                Else
                                                    Type_prestation = "A"
                                                End If
                                                If BDD.LPPTableAdapter.GetDataBy_CodeLPP(item.code).Rows.Count = 0 Then
                                                    COMPTOIR_MAIN.LPPTableAdapter.Insert(item.code, (item.refundBase * 100).ToString, "99999999", "00010101", False, item.actCode.ToString, Age_maximal, "", "", False, False, item.name, Montant_maximal, "", "", Quantité_maximale, "", Type_prestation)
                                                End If


                                            End If

                                            If est_un_kit = True Then


                                                COMPTOIR_MAIN.KitsTableAdapter.Insert("", Pack.name, item.code, Import_Produit_From_Vidal.My_LPPR_get_Coef(Vidal_id, item.code))

                                            Else
                                                Lpp_Code = item.code
                                                LPP_Quantité = Import_Produit_From_Vidal.My_LPPR_get_Coef(Vidal_id, item.code)
                                            End If
                                        End If
                                    End If
                                Next
                            End If
                            '
                            '
                            Dim PrixventeTTC As String = produits(r).PrixventeTTC
                            Dim Stock = produits(r).Stock
                            Dim Répartiteur = produits(r).Répartiteur_principal



                            BDD.ProduitsTableAdapter.Delete_By_CIP_EAN13_CAN_Designation(produits(r).CIP, produits(r).EAN13, produits(r).CAB, produits(r).Designation)

                            Me.ProduitsTableAdapter1.Insert(CIP, EAN13, CAB, désignation, PrixventeTTC, TVA.ToString, Stock, Lpp_Code, Contenance, Unitecontenance, _
                                                            Now, False, liste, Prestation, "", "", LPP_Quantité, Type_prestation, 100, False, 0, CIS, Prestation_actCodeValue, "", Vidal_id, communityAgrement, _
                                                         dispensationPlace, drugInSport, maxPrescriptionDuration, offMarketDate, onFreeAccess, onMarketDate, otc, _
                                                         prescriptionName, vidal_productId, withoutPrescr, "", False, False, 0, 0, Répartiteur, "", est_un_kit, False, False, EAN13, "", 0, False, True, False, New Date(3000, 1, 1))

                        End If
                    End If
                End If



            Catch ex As Exception
                RichTextBox1.Text = produits(r).CIP & ";" & produits(r).EAN13 & ";" & produits(r).CAB & ";" & produits(r).EAN13
                RichTextBox2.Text = ex.ToString
                Exit Sub
            End Try

        Next
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        MAJ_Produits_Vidal()

    End Sub

    Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        OpenFileDialog1.ShowDialog()

        Dim sr As New StreamReader(OpenFileDialog1.FileName, True)
        Dim line = sr.ReadLine
        While Not line Is Nothing
            Dim ean13 = CSV_Champ(line, 1)
            If Val(ean13) = 0 Then
                ean13 = ""
            End If
            Dim stock = CInt(CSV_Champ(line, 2))
            Dim cab = CSV_Champ(line, 3)
            If Val(cab) = 0 Then
                cab = ""
            End If
            Dim data = BDD.ProduitsTableAdapter.GetDataBY_Cipean13_Or_Cab_Or_Designation(ean13, cab, "@@@@@@@@@")
            If data.Rows.Count > 0 Then
                BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(stock, data(0).EAN13, data(0).CAB)
            Else
                BDD.ProduitsTableAdapter.Insert("", ean13, cab, "", "", "0", stock, "", "", "", "01/01/1901", "", "", "", "", "", 0, "", 100, False, 0, "", "", "", 0, False, "", False, "", Now, False, Now, False, "", 0, False, "", False, False, 0, 0, "", "", False, False, True, "", "", 0, False, True, False, Now)

            End If
            line = sr.ReadLine
        End While

    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\lgpi data"
        OpenFileDialog1.ShowDialog()
        Bandeau_info_bas_show("")
        Dim produits_from_lgpi As New List(Of produit_from_lgpi)
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim sw = New StreamWriter("C:\produits.csv")
        Dim line = sr.ReadLine

        While Not line Is Nothing
            Bandeau_info_bas_change_text(line)
            Dim stock = 0
            Try
                stock = CInt(CSV_Champ(line, 4))

            Catch ex As Exception

            End Try
            If stock <> 0 Then
                Dim inbase As Boolean = False
                Dim CIP As String = CSV_Champ(line, 2)
                If CIP.Length = 13 And IsNumeric(CIP) Then
                    If BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(CIP).Rows.Count > 0 Then
                        BDD.ProduitsTableAdapter.Set_Stock_By_CIP_CAB(stock, CIP, CIP)
                        inbase = True
                    End If
                End If


                If inbase = False Then
                    sw.WriteLine(line)
                End If


            End If
            line = sr.ReadLine
        End While
        sr.Close()
        sw.Close()
        Bandeau_info_bas_hide()
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\lgpi data"
        OpenFileDialog1.ShowDialog()
        Bandeau_info_bas_show("")
        Dim produits_from_lgpi As New List(Of produit_from_lgpi)
        Dim sr = New StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default)
        Dim sw = New StreamWriter("C:\produits.csv")

        Dim line = sr.ReadLine
        Dim prodlist As New List(Of String)
        While Not line Is Nothing
            Try

                Bandeau_info_bas_change_text(line)
                Dim designation = CSV_Champ(line, 1)
                Dim codeprod = CSV_Champ(line, 2)
                If codeprod = "" Then
                    codeprod = CSV_Champ(line, 3)
                End If

                Dim stock = CSV_Champ(line, 4)
                Dim Prix_vente_TTC = CSV_Champ(line, 8)
                Dim prestation = CSV_Champ(line, 10)

                Dim LPP_ As String = CSV_Champ(line, 12)

                If Not prodlist.Contains(designation) Then
                    prodlist.Add(designation)
                    BDD.ProduitsTableAdapter.Insert("", "", codeprod, designation, Replace(Prix_vente_TTC, ".", ","), 20, CInt(stock), LPP_, "", "", "", "", "", prestation, "", "", 1, "A", 100, False, 0, "", "", "", 0, False, "", False, "", Now, False, Now, False, "", 0, False, "", False, False, 0, 0, "", "", False, False, True, codeprod, "", 0, False, True, False, Now)


                End If

            Catch ex As Exception
                sw.WriteLine(line)
            End Try
            line = sr.ReadLine
        End While
        sr.Close()
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

        
        'Numéro INSEE;Nom;Prénom;Date de naissance;Rang de naissance (rang Gémell;AMO;Code Sesam;Date de début couverture AMO;Date de fin couverture AMO;Qualité;Rue1;Code Postal;Ville;Tél portable;
        'AMC : n° télétransmission;Couverture AMC (mutuelle);Date de fin couverture AMC;Taux PH1;Taux PH2;Taux PH4;Taux PH7;Taux PHN;

        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "CLIENT-AMO-DATA.csv"
        OpenFileDialog1.ShowDialog()
        Dim fic_CLIENT_AMO_DATA = OpenFileDialog1.FileName
        Dim clients_sr = New StreamReader(fic_CLIENT_AMO_DATA, System.Text.Encoding.Default)
        Dim line = clients_sr.ReadLine
        RichTextBox1.Clear()
        RichTextBox2.Clear()
        While Not line Is Nothing And line <> ""
            Try
                RichTextBox1.Text = line
                Application.DoEvents()

                Dim INSEE = Mid(CSV_Champ(line, 1), 1, 13)
                Dim Nom = CSV_Champ(line, 2)
                Dim Prénom = CSV_Champ(line, 3)
                Dim intitulé_couvAMC = CSV_Champ(line, 16)
                If Trim(intitulé_couvAMC) <> "" Then
                    BDD.VitaleTableAdapter.Update_NotesPerso_By_Nom_Prénom_NIR(vbCrLf & vbCrLf & "AMC ancien intitule couverture" & vbCrLf & intitulé_couvAMC, Nom, Prénom, INSEE)
                End If



            Catch ex As Exception
                RichTextBox2.AppendText(vbCrLf & vbCrLf & line & vbCrLf & ex.Message)
            End Try
            line = clients_sr.ReadLine
        End While


    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        'Prescripteur : Nom;Prescripteur : Prénom;Code Spécialité;Libellé spécialité;Prescripteur : N° AM ou Finess;Prescripteur : N° RPPS;
        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.FileName = "prescripteurs"
        OpenFileDialog1.ShowDialog()
        Dim fic = OpenFileDialog1.FileName
        Dim sr = New StreamReader(fic, System.Text.Encoding.Default)
        Dim line = sr.ReadLine
        RichTextBox1.Clear()
        RichTextBox2.Clear()
        While Not line Is Nothing And line <> ""
            Try
                RichTextBox1.Text = line
                Application.DoEvents()
                Dim Nom
                Dim Prénom
                Dim Code_Spécialité
                Dim Finess
                Dim RPPS

            Catch ex As Exception
                RichTextBox2.AppendText(vbCrLf & vbCrLf & line & vbCrLf & ex.Message)
            End Try
            line = sr.ReadLine
        End While
    End Sub
End Class