' 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.Runtime.InteropServices.Marshal
Imports System.IO
Imports System.Runtime.InteropServices

Public Class Jeux_d_essai2

    Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
#Region "INI File"
    'Retourne un booléen indiquant l'existance ou non d'une section
    Public Function ExisteSection(ByVal File As String, ByVal Section As String) As Boolean
        Dim PtrCh As IntPtr
        Dim Lng As Integer

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)

        Return (Lng)
    End Function

    'Retourne une collection contenant l'ensemble des sections du fichier "File"
    Public Function GetAllSections(ByVal File As String) As Collection
        Dim PtrCh As IntPtr
        Dim Sections As Collection
        Dim I, Lng As Integer
        Dim Chaine, SChaine As String

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSectionNames(PtrCh, 1024, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        Sections = New Collection
        SChaine = ""
        For I = 0 To Lng - 1
            If Chaine.Chars(I) = vbNullChar Then
                Sections.Add(SChaine)
                SChaine = ""
            Else
                SChaine = SChaine & Chaine.Chars(I)
            End If
        Next
        GetAllSections = Sections
        Sections = Nothing
    End Function

    'Retourne une collection contenant l'ensemble des clés de la section "Section" du fichier "File"
    Public Function GetSectionCles(ByVal File As String, ByVal Section As String) As Collection
        Dim PtrCh As IntPtr
        Dim Cles As Collection
        Dim I, Lng As Integer
        Dim Chaine, SChaine As String
        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        Cles = New Collection
        SChaine = ""
        For I = 0 To Lng - 1
            If Chaine.Chars(I) = vbNullChar Then
                Cles.Add(SChaine)
                SChaine = ""
            Else
                SChaine = SChaine & Chaine.Chars(I)
            End If
        Next
        GetSectionCles = Cles
        Cles = Nothing
    End Function

    'Retourne la valeur de la clé "Cle" de la section "Section" du fichier "File"
    Public Function GetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As String
        Dim PtrCh As IntPtr
        Dim Lng As Integer
        Dim Chaine As String

        PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
        Lng = GetPrivateProfileString(Section, Cle, "", PtrCh, 255, File)
        Chaine = PtrToStringAnsi(PtrCh, Lng)
        FreeHGlobal(PtrCh)

        GetCle = Chaine
    End Function


    'Insère une section dans le fichier "File"
    Public Function SetSection(ByVal File As String, ByVal Section As String, ByVal Valeur As String) As Boolean
        SetSection = WritePrivateProfileSection(Section, Valeur, File)
    End Function

    'Insère la clé "Cle" dans la section "Section" du fichier "File"
    Public Function SetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String, ByVal Valeur As String) As Boolean
        SetCle = WritePrivateProfileString(Section, Cle, Valeur, File)
    End Function


    'Efface toute les clés de la section "Section"
    Public Function DelSection(ByVal File As String, ByVal Section As String) As Boolean
        DelSection = WritePrivateProfileSection(Section, "", File)
    End Function

    'Efface la valeur de la clé "Cle" de la section "Section"
    Public Function DelCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As Boolean
        DelCle = WritePrivateProfileString(Section, Cle, "", File)
    End Function
#End Region

    Structure SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Structure

    <DllImport("kernel32.dll", setLastError:=True)> Public Shared Function SetSystemTime(ByRef theDateTime As SYSTEMTIME) As Boolean
    End Function

    Private Function Carte_Physique(ByVal Carte_Virtuelle As String) As String

        Dim line, carte_phys, carte_virt As String
        Using sr As New StreamReader("C:\ProgramData\santesocial\fsv\1.40.9\adm\Phys-Virt.txt")
            line = sr.ReadLine
            Do
                carte_phys = Mid(line, 1, InStr(line, ";") - 1)
                line = Mid(line, InStr(line, ";") + 1)
                carte_virt = Mid(line, 1, InStr(line, ";") - 1)
                If carte_virt = Carte_Virtuelle Then
                    Carte_Physique_Label.Text = carte_phys
                    sr.Close()
                    Return carte_phys
                End If
                line = sr.ReadLine
            Loop Until line Is Nothing Or line = ""
        End Using
        Return ""
    End Function

    Private Sub Ok_Button_Click(sender As Object, e As EventArgs) Handles Ok_Button.Click
        RichTextBox1.AppendText(Chr(10) + Serie_ComboBox.Text + " " + Num_ComboBox.Text)
        COMPTOIR_MAIN.Visible = True
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'DATE
        COMPTOIR_MAIN.DatePrecription.Value = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Date_Prescription
        COMPTOIR_MAIN.DateExecution.Value = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Date_Soins
        COMPTOIR_MAIN.DateFacture.Value = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Date_Facture

        Dim thetimetoset As SYSTEMTIME
        thetimetoset.wYear = COMPTOIR_MAIN.DateFacture.Value.Year
        thetimetoset.wMonth = COMPTOIR_MAIN.DateFacture.Value.Month
        thetimetoset.wDayOfWeek = COMPTOIR_MAIN.DateFacture.Value.DayOfWeek
        thetimetoset.wDay = COMPTOIR_MAIN.DateFacture.Value.Day
        thetimetoset.wHour = COMPTOIR_MAIN.DateFacture.Value.Hour
        thetimetoset.wMinute = COMPTOIR_MAIN.DateFacture.Value.Minute
        thetimetoset.wSecond = COMPTOIR_MAIN.DateFacture.Value.Second
        thetimetoset.wMilliseconds = COMPTOIR_MAIN.DateFacture.Value.Millisecond
        If SetSystemTime(thetimetoset) = False Then
            MyMsg_Box.Presentation("Officine", "failed to set System Time", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'CV
        Carte_Virtuelle_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).CV_Virtuelle
        SetCle(Config.SesamIni, "TESTSSV", "NomFichierVitale1", JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).CV_Virtuelle + ".IN")
        Bénéficiaire_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Bénéficiaire
        If CV_ComboBox.Text = "" Then
            MyMsg_Box.Presentation("Officine", "INSERER CARTE PHYSIQUE " + Carte_Physique(Carte_Virtuelle_Label.Text), False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
        COMPTOIR_MAIN.LECTURE_VITALE.PerformClick()
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'PRESCRIPTEUR
        COMPTOIR_MAIN.PrescripteurMyCombobox.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Prescripteur
        COMPTOIR_MAIN.recherche_prescripteur()
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        TP_AMO_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).TP_AMO
        TP_AMC_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).TP_AMC
        Transmettre_AMO_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Transmettre_AMO
        Transmettre_AMC_Label.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Transmettre_AMC
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'T prescription établie par le médecin traitant
        If Not JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Factutation_Origine_Prescr Is DBNull.Value Then
            If JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Factutation_Origine_Prescr <> "" Then
                COMPTOIR_MAIN.OriginePrescriptionToolStripComboBox.Text = "T prescription établie par le médecin traitant"
                COMPTOIR_MAIN.OriginePrescriptionToolStripMenuItem.Checked = True
            End If
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'nature assurance
        Select Case JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Nature_Assurance
            Case "maternité"
                COMPTOIR_MAIN.RadioButtonDateDébutGrossesse.Checked = True
                COMPTOIR_MAIN.NatureAssuranceDateTimePicker.Value = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Maternité_Date_Début_Grossesse
                COMPTOIR_MAIN.NatureAssurance_Button.Text = "MATERNITE"
            Case "maladie"
                COMPTOIR_MAIN.NatureAssurance_Button.Text = "MALADIE"
            Case "accident du travail"
                COMPTOIR_MAIN.NatureAssurance_Button.Text = "ACCIDENT DU TRAVAIL"
            Case "SMG"
                COMPTOIR_MAIN.NatureAssurance_Button.Text = "SMG"
        End Select
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'grouper lpp
        If JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Factutation_Grouper_LPP = True Then
            COMPTOIR_MAIN.GrouperLPPToolStripMenuItem.Checked = True
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'Attestations
        If Not JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Attestation_Num Is DBNull.Value Then
            If Not JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Attestation_Num = "" Then
                Dim Num_Attestion = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(0).Attestation_Num
                COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = "2"
                COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_de_mutuelle
                If Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_AMC_associée <> "" Then
                    COMPTOIR_MAIN.NuméroDAMCAssociéeToolStripMenuItem.Checked = True
                    COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_AMC_associée
                End If
                COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Date_début_validité
                COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Date_fin_validité
                COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Indicateur_de_traitement
                COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).STS
                If Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_AMC <> "" Then
                    COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_AMC
                End If
                COMPTOIR_MAIN.AMC_Numadhérent_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Numéro_adhérent
                COMPTOIR_MAIN.AMC_IdentifiantAMC_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Identifiant_AMC
                COMPTOIR_MAIN.AMC_CodeRoutage_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Code_routage
                COMPTOIR_MAIN.AMC_NomDomaine_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Domaine
                COMPTOIR_MAIN.AMC_IdHote_TextBox.Text = Me.JEUX_D_ESSAI_AttestationsTableAdapter.GetDataBy_Num(Num_Attestion)(0).Identifiant_hôte
            End If
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'LIGNES
        For r = 0 To JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text).Rows.Count - 1
            COMPTOIR_MAIN.Vente.Rows.Add()
        Next
        For r = 0 To JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text).Rows.Count - 1
            COMPTOIR_MAIN.Vente.CurrentCell = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Vente_produits.Index, r)
            COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Vente_produits.Index, r).Value = JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Produit
            'COMPTOIR_MAIN.Vente.CurrentCell = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Vente_produits.Index + 1, r)
            COMPTOIR_MAIN.Vente.CurrentCell = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteQuantité.Index, r)
            COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteQuantité.Index, r).Value = JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Quantité
            COMPTOIR_MAIN.Vente.CurrentCell = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VentePrix.Index, r)
            COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VentePrix.Index, r).Value = JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Prix_Unitaire
            COMPTOIR_MAIN.Vente.CurrentCell = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VentePrix.Index + 1, r)
        Next
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'code exo
        For r = 0 To JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text).Rows.Count - 1
            Select Case JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Code_Exo
                Case "3"
                    COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, r).Value = "SP"
                Case "4"
                    COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, r).Value = "ALD"
                Case "7"
                    COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, r).Value = "PREV"
            End Select
        Next
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'entente préalable
        ReDim CurrentFactureVitale.G1840(JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text).Rows.Count)
        For r = 0 To JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text).Rows.Count - 1
            If JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Entente_préalable_CodeEP <> "" Then


                CurrentFactureVitale.G1840(JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Num_Ligne).G1840C1 = JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Num_Ligne
                CurrentFactureVitale.G1840(JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Num_Ligne).G1840C2 = JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Entente_préalable_CodeEP
                CurrentFactureVitale.G1840(JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Num_Ligne).G1840C3 = Format(JEUX_D_ESSAI_LignesTableAdapter.GetDataBy_Serie_Num(Serie_ComboBox.Text, Num_ComboBox.Text)(r).Entente_préalable_DateEP, "yyyyMMdd") + "0000"
            End If
        Next
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        If Transmettre_AMC_Label.Text = "False" Then
            COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
        End If
        If Transmettre_AMO_Label.Text = "False" Then
            COMPTOIR_MAIN.NePasGénérerDeFSEToolStripMenuItem.Checked = True
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        'CAS PARTICULIERS
        'III.04.01 Test n°108
        If Serie_ComboBox.Text = "III.04.01" And Num_ComboBox.Text = "108" Then
            COMPTOIR_MAIN.ToppréventioncommuneAMOAMCToolStripTextBox.Text = "O"
            COMPTOIR_MAIN.QualifiantdispositifpreventionToolStripTextBox.Text = "PR"
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        COMPTOIR_MAIN.Button_Facturation_Click(Nothing, Nothing)

        CurrentBenéficiaire.clear()
        COMPTOIR_MAIN.ResetForm()

        COMPTOIR_MAIN.Close()
    End Sub

  
    Private Sub OK2_Button_Click(sender As Object, e As EventArgs) Handles OK2_Button.Click
        For r = 0 To JEUX_D_ESSAITableAdapter.GetDataBy_Serie_CVPphysique(Serie2_ComboBox.Text, CV_ComboBox.Text).Rows.Count - 1

            Serie_ComboBox.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_CVPphysique(Serie2_ComboBox.Text, CV_ComboBox.Text)(r).Serie
            Num_ComboBox.Text = JEUX_D_ESSAITableAdapter.GetDataBy_Serie_CVPphysique(Serie2_ComboBox.Text, CV_ComboBox.Text)(r).Num
            Me.Refresh()
            Ok_Button.PerformClick()
        Next
    End Sub



    Private Function retourne_date_ou_dbnull(ByVal date_en_string)
        If date_en_string = "" Then
            Return DBNull.Value
        Else
            Return CDate(date_en_string)
        End If
    End Function




    Private Sub LinkLabel2_LinkClicked(sender As Object, e As EventArgs) Handles LinkLabel2.LinkClicked
        Dim sr As New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun.txt", System.Text.Encoding.Default)
        Dim line As String
        Dim sw As New StreamWriter(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes_temp.txt", True, System.Text.Encoding.Default)
        line = sr.ReadLine
        While Not line Is Nothing
            If InStr(line, "III.04.") = 1 Then
                sw.WriteLine(line)
            ElseIf InStr(line, "Test n°") = 1 Then
                sw.WriteLine(line)
            ElseIf line = "Part" Then
                line = sr.ReadLine
                If line = "AMC" Then
                    line = sr.ReadLine
                    While InStr(line, "Assurance") = 0
                        sw.WriteLine(line)
                        line = sr.ReadLine
                    End While
                End If

            End If
            line = sr.ReadLine
        End While

        sr.Close()
        sw.Close()
        sr = New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes_temp.txt", System.Text.Encoding.Default)
        sw = New StreamWriter(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes.txt", True, System.Text.Encoding.Default)
        line = sr.ReadLine
        While Not line Is Nothing

            If InStr(line, "III.04.") = 1 Then
                sw.WriteLine()
                sw.WriteLine(line)

            ElseIf InStr(line, "Test n°") = 1 Then
                sw.WriteLine()
                sw.WriteLine(line)
            Else
                Dim New_line As String = line
                While InStr(New_line, "/") = 0
                    New_line = New_line + " " + sr.ReadLine
                End While
                'If InStr(New_line, "1249374 ESCARRES, COUSSIN EN MOUSSE & GEL, S/CLASSE IB") <> 0 Then
                '    New_line = New_line + " " + sr.ReadLine + " " + sr.ReadLine
                'End If
                sw.WriteLine(New_line)
            End If
            line = sr.ReadLine
        End While
        sr.Close()
        sw.Close()
        My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes_temp.txt")
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
    End Sub

    Private Sub LinkLabel3_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked

        Dim sr As New StreamReader("C:\DOSSIER_SV\JEU_PHA_1.40.9_022013\attestations.txt", System.Text.Encoding.Default)
        Dim line As String
        Dim num As String = ""
        Dim Numéro_de_mutuelle As String = ""
        Dim Numéro_AMC_associée As String = ""
        Dim Date_début_validité As String = ""
        Dim Date_fin_validité As String = ""
        Dim Indicateur_de_traitement As String = ""
        Dim STS As String = ""
        Dim Formules_autorisées As String = ""
        Dim Numéro_AMC As String = ""
        Dim Numéro_adhérent As String = ""
        Dim Identifiant_AMC As String = ""
        Dim Code_routage As String = ""
        Dim Domaine As String = ""
        Dim Identifiant_hôte As String = ""
        line = sr.ReadLine
        While Not line Is Nothing

            If InStr(line, "ATTESTATION N°") = 1 Then
                If num <> "" Then
                    JEUX_D_ESSAI_AttestationsTableAdapter.Insert(num, Numéro_de_mutuelle, Numéro_AMC_associée, Date_début_validité, Date_fin_validité, Indicateur_de_traitement, STS, Formules_autorisées, Numéro_AMC, Numéro_adhérent, Identifiant_AMC, Code_routage, Domaine, Identifiant_hôte)
                End If
                line = Mid(line, InStr(line, " ") + 4)
                num = Trim(Mid(line, 1, InStr(line, " ") - 1))
                Numéro_de_mutuelle = ""
                Numéro_AMC_associée = ""
                Date_début_validité = ""
                Date_fin_validité = ""
                Indicateur_de_traitement = ""
                STS = ""
                Formules_autorisées = ""
                Numéro_AMC = ""
                Numéro_adhérent = ""
                Identifiant_AMC = ""
                Code_routage = ""
                Domaine = ""
                Identifiant_hôte = ""


            ElseIf InStr(line, "Numéro de mutuelle : ") = 1 Then
                line = LTrim(Mid(line, "Numéro de mutuelle :".Length + 1))
                Numéro_de_mutuelle = Mid(line, 1, InStr(line, " ") - 1)
                line = Mid(line, InStr(line, "Numéro d’AMC associée :") + "Numéro d’AMC associée :".Length)
                Numéro_AMC_associée = Trim(line)


            ElseIf InStr(line, "Date de début de validité :") = 1 Then
                line = Mid(line, "Date de début de validité :".Length + 2)
                Date_début_validité = Mid(line, 1, 10)
                line = Mid(line, InStr(line, " Date de fin de validité :") + " Date de fin de validité :".Length + 1)
                Date_fin_validité = Trim(line)


            ElseIf InStr(line, "Indicateur de traitement :") = 1 Then
                line = LTrim(Mid(line, "Indicateur de traitement :".Length + 1))
                If line <> "" Then
                    Indicateur_de_traitement = Trim(Mid(line, 1, InStr(line, " ") - 1))
                End If


            ElseIf InStr(line, "STS :") = 1 Then
                line = LTrim(Mid(line, "STS :".Length + 1))
                If line <> "" Then
                    STS = Mid(line, 1, InStr(line, " ") - 1)
                End If

            ElseIf InStr(line, "Numéro d’AMC :") = 1 Then
                line = LTrim(Mid(line, "Numéro d’AMC :".Length + 1))
                Numéro_AMC = Mid(line, 1, InStr(line, " ") - 1)
                line = LTrim(Mid(line, InStr(line, "Numéro d’adhérent :") + "Numéro d’adhérent :".Length))
                Numéro_adhérent = Trim(line)


            ElseIf InStr(line, "Identifiant AMC :") = 1 Then
                line = LTrim(Mid(line, "Identifiant AMC :".Length + 1))
                Identifiant_AMC = Trim(line)



            ElseIf InStr(line, "Code routage :") = 1 Then
                line = LTrim(Mid(line, "Code routage :".Length + 1))
                Code_routage = Mid(line, 1, InStr(line, " ") - 1)
                line = LTrim(Mid(line, InStr(line, "Domaine :") + "Domaine :".Length))
                Domaine = Mid(line, 1, InStr(line, " ") - 1)
                line = LTrim(Mid(line, InStr(line, "Identifiant hôte :") + "Identifiant hôte :".Length))
                Identifiant_hôte = Trim(line)


            End If
            line = sr.ReadLine

        End While
        JEUX_D_ESSAI_AttestationsTableAdapter.Insert(num, Numéro_de_mutuelle, Numéro_AMC_associée, Date_début_validité, Date_fin_validité, Indicateur_de_traitement, STS, Formules_autorisées, Numéro_AMC, Numéro_adhérent, Identifiant_AMC, Code_routage, Domaine, Identifiant_hôte)
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()

    End Sub

    Private Sub LinkLabel4_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel4.LinkClicked
        Dim sr As New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun.txt")
        Dim line As String
        Dim Serie As String = ""
        Dim Num As String = ""
        Dim Date_Facture As Date = CDate("01/01/1901")
        Dim Date_Soins As Date = CDate("01/01/1901")
        Dim Date_Prescription As Date = CDate("01/01/1901")
        Dim Prescripteur As String = "pr"
        Dim CV_Virtuelle As String = ""
        Dim CV_Physique As String = ""
        Dim Bénéficiaire As String = ""
        Dim Nature_Assurance As String = ""
        Dim Maternité_Date_Début_Grossesse As Date = CDate("01/01/1901")
        Dim Maternité_Date_Accouchement As Date = CDate("01/01/1901")
        Dim AT_Org_Gestionnaire As String = ""
        Dim SMG_Montant_Total__PEC_SMG As Integer
        Dim Délivrance_Renouvellement As Integer
        Dim Délivrance_Medic_Detournables As Boolean
        Dim Factutation_Date_AMC As String = ""
        Dim Factutation_Flux_TP_coordonné As Boolean
        Dim Factutation_Forcer_Code_Service_AMO As String = ""
        Dim Factutation_Compl_Ref_PEC As String = ""
        Dim Factutation_Compl_Montant_PEC As Integer
        Dim Factutation_Origine_Prescr As String = ""
        Dim Factutation_Grouper_LPP As Boolean
        Dim Message As String = ""
        Dim TP_AMO As Boolean
        Dim TP_AMC As Boolean
        Dim Transmettre_AMO As Boolean
        Dim Transmettre_AMC As Boolean
        Dim Attestation_Num As String = ""
        Dim cas_non_passant As Boolean = False


        line = sr.ReadLine
        While Not line Is Nothing

            If InStr(line, "III.04.") = 1 Then
                Serie = Mid(line, 1, 9)

            ElseIf InStr(line, "Test n°") = 1 Then
                If Num <> "" And cas_non_passant = False And InStr(Num, "SUSPENDU") = 0 Then
                    Me.JEUX_D_ESSAITableAdapter.Insert(Serie, Num, Date_Facture, Date_Soins, Date_Prescription, Prescripteur, CV_Virtuelle, "", Nature_Assurance, _
                                                      Maternité_Date_Début_Grossesse, Maternité_Date_Accouchement, SMG_Montant_Total__PEC_SMG, _
                                                       Délivrance_Renouvellement, Délivrance_Medic_Detournables, Factutation_Date_AMC, Factutation_Flux_TP_coordonné, Factutation_Forcer_Code_Service_AMO, _
                                                       Factutation_Compl_Ref_PEC, Factutation_Compl_Montant_PEC, Factutation_Origine_Prescr, Factutation_Grouper_LPP, "", Bénéficiaire, TP_AMC, TP_AMO, _
                                                       Transmettre_AMC, Transmettre_AMO, Attestation_Num)
                End If
                Num = ""
                Date_Facture = CDate("01/01/1901")
                Date_Soins = CDate("01/01/1901")
                Date_Prescription = CDate("01/01/1901")
                Prescripteur = "pr"
                CV_Virtuelle = ""
                CV_Physique = ""
                Bénéficiaire = ""
                Nature_Assurance = ""
                Maternité_Date_Début_Grossesse = CDate("01/01/1901")
                Maternité_Date_Accouchement = CDate("01/01/1901")
                AT_Org_Gestionnaire = ""
                SMG_Montant_Total__PEC_SMG = 0
                Délivrance_Renouvellement = 0
                Délivrance_Medic_Detournables = False
                Factutation_Date_AMC = ""
                Factutation_Flux_TP_coordonné = False
                Factutation_Forcer_Code_Service_AMO = ""
                Factutation_Compl_Ref_PEC = ""
                Factutation_Compl_Montant_PEC = 0
                Factutation_Origine_Prescr = ""
                Factutation_Grouper_LPP = False
                Message = ""
                TP_AMO = False
                TP_AMC = False
                Transmettre_AMO = False
                Transmettre_AMC = False
                Attestation_Num = ""
                cas_non_passant = False


                Num = Mid(line, 8)

            ElseIf Mid(line, 3, 1) = "/" And Mid(line, 6, 1) = "/" And Mid(line, 14, 1) = "/" And Mid(line, 17, 1) = "/" Then
                Date_Facture = CDate(Mid(line, 1, 10))
                Date_Soins = CDate(Mid(line, 12, 10))


            ElseIf InStr(line, "Date de prescription :") = 1 Then
                line = sr.ReadLine
                Date_Prescription = CDate(Mid(line, 1, 10))

            ElseIf InStr(line, "CV ") = 1 Then
                line = Mid(line, InStr(line, " ") + 1)
                CV_Virtuelle = Mid(line, 1, InStr(line, " ") - 1)
                Bénéficiaire = Mid(line, InStr(line, " ") + 1)

            ElseIf InStr(line, "Assurance ") = 1 Then
                Nature_Assurance = Mid(line, 11)
                If InStr(Nature_Assurance, "soins médicaux") <> 0 Then
                    Nature_Assurance = "SMG"
                End If

            ElseIf InStr(line, "Date présumée de grossesse:") = 1 Then
                line = sr.ReadLine
                Maternité_Date_Début_Grossesse = CDate(Mid(line, 1, 10))
                Maternité_Date_Accouchement = CDate(Mid(line, 1, 10))

            ElseIf InStr(line, "Organisme gestionnaire de l’AT indiqué sur le support =   ") = 1 Then
                AT_Org_Gestionnaire = Mid(line, "Organisme gestionnaire de l’AT indiqué sur le support = ".Length + 1)

            ElseIf InStr(line, "Montant PEC SMG =") = 1 Then
                line = Trim(Mid(line, "Montant PEC SMG =".Length + 1))
                SMG_Montant_Total__PEC_SMG = Mid(line, 1, InStr(line, "€") - 1)

            ElseIf InStr(UCase(line), "RENOUVELLEMENT") <> 0 Then
                Délivrance_Renouvellement = InputBox("Valeur Renouvellemnt pour test " + Serie + " " + Num)

            ElseIf InStr(line, "détournable") <> 0 Then
                Délivrance_Medic_Detournables = True

            ElseIf line = "FSE en TP AMO" Then
                TP_AMO = True
                TP_AMC = False
                Transmettre_AMO = True
                Transmettre_AMC = False

            ElseIf line = "FSE en TP AMO / TP AMC" Then
                TP_AMO = True
                TP_AMC = True
                Transmettre_AMO = True
                Transmettre_AMC = True

            ElseIf line = "FSE en HTP AMO" Then
                TP_AMO = False
                TP_AMC = False
                Transmettre_AMO = True
                Transmettre_AMC = False


            ElseIf line = "FSE + DRE en TP AMO / TP AMC" Then
                TP_AMO = True
                TP_AMC = True
                Transmettre_AMO = True
                Transmettre_AMC = True

            ElseIf line = "FSE + DRE en HTP AMO / TP AMC" Then
                TP_AMO = False
                TP_AMC = True
                Transmettre_AMO = True
                Transmettre_AMC = True

            ElseIf line = "FSE + DRE en HTP AMO / HTP AMC" Then
                TP_AMO = False
                TP_AMC = False
                Transmettre_AMO = True
                Transmettre_AMC = True

            ElseIf line = "DRE en TP AMC" Then
                TP_AMO = False
                TP_AMC = True
                Transmettre_AMO = False
                Transmettre_AMC = True

            ElseIf line = "DRE en HTP AMC" Then
                TP_AMO = False
                TP_AMC = False
                Transmettre_AMO = False
                Transmettre_AMC = True

            ElseIf InStr(line, "Origine de la prescription à ") = 1 Or InStr(line, "Origine de la prescription = ") Then
                Factutation_Origine_Prescr = Mid(line, "Origine de la prescription à ".Length + 1, 1)

            ElseIf InStr(line, "Attestation ") = 1 Then
                If IsNumeric(Trim(Mid(line, "Attestation ".Length + 1))) Then
                    Attestation_Num = Trim(Mid(line, "Attestation ".Length + 1))
                End If

            ElseIf InStr(line, "CAS NON PASSANT") <> 0 Then
                cas_non_passant = True
            End If





            line = sr.ReadLine

        End While
        If cas_non_passant = False And InStr(Num, "SUSPENDU") = 0 Then
            Me.JEUX_D_ESSAITableAdapter.Insert(Serie, Num, Date_Facture, Date_Soins, Date_Prescription, Prescripteur, CV_Virtuelle, "", Nature_Assurance, Maternité_Date_Début_Grossesse, _
                                                           Maternité_Date_Accouchement, SMG_Montant_Total__PEC_SMG, Délivrance_Renouvellement, Délivrance_Medic_Detournables, Factutation_Date_AMC, _
                                                           Factutation_Flux_TP_coordonné, Factutation_Forcer_Code_Service_AMO, Factutation_Compl_Ref_PEC, Factutation_Compl_Montant_PEC, _
                                                           Factutation_Origine_Prescr, Factutation_Grouper_LPP, "", Bénéficiaire, TP_AMC, TP_AMO, Transmettre_AMC, Transmettre_AMO, Attestation_Num)
        End If
        sr = Nothing
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()

    End Sub

    Private Sub LinkLabel5_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel5.LinkClicked
        'carte vitale

        Dim lineCV, carte_phys, carte_virt As String
        Using srCV As New StreamReader("C:\ProgramData\santesocial\fsv\1.40.9\adm\Phys-Virt.txt")
            lineCV = srCV.ReadLine
            Do
                carte_phys = Mid(lineCV, 1, InStr(lineCV, ";") - 1)
                lineCV = Mid(lineCV, InStr(lineCV, ";") + 1)
                carte_virt = Mid(lineCV, 1, InStr(lineCV, ";") - 1)
                Me.JEUX_D_ESSAITableAdapter.Update_CV_Physique(carte_phys, carte_virt)
                lineCV = srCV.ReadLine
            Loop Until lineCV Is Nothing Or lineCV = ""
        End Using
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()

    End Sub

    Private Sub LinkLabel6_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel6.LinkClicked
        Dim sr As New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes.txt", System.Text.Encoding.Default)
        Dim line As String
        line = sr.ReadLine
        Dim lignes() As String
        ReDim lignes(10000)
        Dim indexcourant As Integer = 0
        While Not line Is Nothing
            If InStr(line, "III.04.") <> 1 And InStr(line, "Test n°") <> 1 Then
                Dim found As Boolean = False
                For i = 0 To 9999
                    If lignes(i) = line Then
                        found = True
                    End If
                Next
                If found = False Then
                    lignes(indexcourant) = line
                    indexcourant = indexcourant + 1
                End If
            End If
            line = sr.ReadLine
        End While
        Dim sw As New StreamWriter(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Ligne_unique.txt", True, System.Text.Encoding.Default)
        For i = 0 To 9999
            If lignes(i) <> "" Then
                sw.WriteLine(lignes(i))
            End If
        Next
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
    End Sub

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

        Dim sr As New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes.txt", System.Text.Encoding.Default)
        Dim line As String
        line = sr.ReadLine
        Dim série As String = ""
        Dim num_test As String = ""
        Dim num_ligne, quantité As Integer
        Dim produit As String = ""
        Dim prix_unitaire As String = ""
        Dim Code_Exo As String

        While Not line Is Nothing
            If line <> "" Then
                If InStr(line, "III.04.") = 1 Then
                    série = Mid(line, 1, InStr(line, " ") - 1)
                ElseIf InStr(line, "Test n°") = 1 Then
                    num_ligne = 0
                    num_test = Mid(line, 8)
                Else
                    num_ligne = num_ligne + 1

                    If IsNumeric(Mid(line, 1, InStr(line, " ") - 1)) Then
                        quantité = Val(Mid(line, 1, InStr(line, " ") - 1))
                    Else
                        quantité = 1
                    End If

                    If InStr(line, "(Prix public ") <> 0 Then
                        Dim linetemp As String = Mid(line, InStr(line, "(Prix public ") + "(Prix public ".Length)
                        prix_unitaire = Mid(linetemp, 1, InStr(linetemp, ")") - 1)
                    ElseIf InStr(line, "(PU ") <> 0 Then
                        Dim linetemp As String = Mid(line, InStr(line, "(PU ") + "(PU ".Length)
                        prix_unitaire = Mid(linetemp, 1, InStr(linetemp, ")") - 1)

                    End If

                    Code_Exo = Mid(line, InStr(line, "code ") + "code ".Length, 1)

                    produit = ""
                    Dim linetemp2 As String = line
                    Dim mot As String = ""
                    While InStr(linetemp2, " ") <> 0
                        mot = Mid(linetemp2, 1, InStr(linetemp2, " ") - 1)
                        If IsNumeric(mot) And (mot.Length = 7 Or mot.Length = 13) Then
                            produit = mot
                            linetemp2 = ""
                        Else
                            If InStr(linetemp2, " ") <> 0 Then
                                linetemp2 = Mid(linetemp2, InStr(linetemp2, " ") + 1)
                            Else
                                linetemp2 = ""
                            End If
                        End If
                    End While


                    'If InStr(line, "PA (PU 7,39) (Prix public 10,39)") <> 0 Then
                    '    produit = "PA (PU 7,39) (Prix public 10,39)"
                    'ElseIf InStr(line, "B 10,00 (PU 0,27)") <> 0 Then
                    '    produit = "Acte de biologie 0,27"
                    '    quantité = 10
                    'ElseIf InStr(line, "CPH (PU 0,15)") <> 0 Then
                    '    produit = "COPIE ORDONNANCE 0.15"
                    'ElseIf InStr(line, "UPH (PU 6,00)") <> 0 Then
                    '    produit = "URGENCE AP 21H"
                    'ElseIf InStr(line, "PHA (PU 20,73)") <> 0 Then
                    '    produit = "PHA (PU 20,73)"
                    'ElseIf InStr(line, "XYZ (PU 18,29)") <> 0 Then
                    '    produit = "XYZ (PU 18,29)"
                    'ElseIf InStr(line, "PMR (PU 4,57)") <> 0 Then
                    '    produit = "PMR (PU 4,57)"
                    'End If

                    If produit = "" Then
                        produit = Mid(line, 1, InStr(line, "/") - 3)
                        Dim sw As New StreamWriter(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun_Lignes.log", True, System.Text.Encoding.Default)
                        sw.WriteLine(série + "  test " + num_test + "  ligne " + num_ligne.ToString + "  produit:" + produit)
                        sw.Close()

                    End If




                    Me.JEUX_D_ESSAI_LignesTableAdapter.Insert(série, num_test, num_ligne, produit, quantité, Nothing, Nothing, Nothing, Nothing, Nothing, prix_unitaire, Nothing, Code_Exo, CDate("01/01/1901"), CDate("01/01/1901"))
                End If


            End If
            line = sr.ReadLine

        End While
        MyMsg_Box.Presentation("Officine", "done", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
    End Sub

End Class