' 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.Xml
Imports System.IO
Imports System.Text

Public Class Configuration

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

    Private Sub Configuration_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ConfigXml As New XmlDocument
        ConfigXml.Load(Application.StartupPath + "\config.xml")
        Dim XMLelementliste As XmlNodeList



        'Sesam-Vitale
        XMLelementliste = ConfigXml.GetElementsByTagName("SESAM-VITALE")
        Label_Version_CDC_SV.Text = "Agrément Sesam Vitale: " + XMLelementliste.Item(0).Item("VersionCDCSV").InnerText

        'STS
        XMLelementliste = ConfigXml.GetElementsByTagName("STS")
        If UCase(XMLelementliste.Item(0).Item("Activer").InnerText) = "OUI" Then
            CheckBox_STS_Activer.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("AfficheDiagNiv1").InnerText) = "OUI" Then
            CheckBox_STS_Afficheniveau1.Checked = True
        End If

        'SMTP
        XMLelementliste = ConfigXml.GetElementsByTagName("SMTP")
        Dim TypeEmetteurFichier As String
        TypeEmetteurFichier = XMLelementliste.Item(0).Item("TypeEmetteurFichier").InnerText
        For i = 0 To ComboBox_Type_émetteur_fichier.Items.Count - 1
            If Mid(ComboBox_Type_émetteur_fichier.Items.Item(i).ToString, 1, 2) = TypeEmetteurFichier Then
                ComboBox_Type_émetteur_fichier.Text = ComboBox_Type_émetteur_fichier.Items.Item(i).ToString
            End If
        Next
        TextBox_Num_émetteur_fichier.Text = XMLelementliste.Item(0).Item("NumEmetteurFichier").InnerText
        SMTP_Serv_ext_TextBox.Text = XMLelementliste.Item(0).Item("Serveur_Externe").InnerText
        SMTP_Serv_ext_port_TextBox.Text = XMLelementliste.Item(0).Item("Serveur_Externe_port").InnerText
        Pop3_BAL.Text = XMLelementliste.Item(0).Item("From").InnerText

        'Pop3
        XMLelementliste = ConfigXml.GetElementsByTagName("Pop3")
        Pop3_Serveur_TextBox.Text = XMLelementliste.Item(0).Item("Serveur").InnerText
        Pop3_Port_TextBox.Text = XMLelementliste.Item(0).Item("Port").InnerText
        Pop3_User_TextBox.Text = XMLelementliste.Item(0).Item("User").InnerText
        Pop3_Password_MaskedTextBox.Text = XMLelementliste.Item(0).Item("Password").InnerText
        Pop3_RepBAL.Text = XMLelementliste.Item(0).Item("RepBAL").InnerText

        'TRANSMISSION
        XMLelementliste = ConfigXml.GetElementsByTagName("SMTP")
        TextBox_G14C3_G94C3.Text = XMLelementliste.Item(0).Item("MessageFormatFichier").InnerText
        ComboBox_Type_émetteur_fichier.Text = XMLelementliste.Item(0).Item("TypeEmetteurFichier").InnerText
        TextBox_Num_émetteur_fichier.Text = XMLelementliste.Item(0).Item("NumEmetteurFichier").InnerText
        If UCase(XMLelementliste.Item(0).Item("CompresserDonnees").InnerText) = "OUI" Then
            CheckBox_compression.Checked = True
        End If

        'modefonctionement
        XMLelementliste = ConfigXml.GetElementsByTagName("Configuration")
        ComboBoxModeFonctionnement.Text = XMLelementliste.Item(0).Item("ModeFonctionnement").InnerText
        If XMLelementliste.Item(0).Item("Desynchroniser").InnerText = "oui" Then
            CheckBox_Desynchroniser.Checked = True
        Else
            CheckBox_Desynchroniser.Checked = False
        End If

        'pmss
        XMLelementliste = ConfigXml.GetElementsByTagName("Data")
        TextBoxPMSS.Text = XMLelementliste.Item(0).Item("PMSS").InnerText

        'contaceptif urgence
        XMLelementliste = ConfigXml.GetElementsByTagName("ContaceptifUrgence")
        TextBoxContraceptifUrgNIR.Text = XMLelementliste.Item(0).Item("NIR").InnerText
        TextBoxContraceptifUrgClé.Text = XMLelementliste.Item(0).Item("cle").InnerText
        TextBoxContraceptifUrgDateNaissance.Text = XMLelementliste.Item(0).Item("datenaissance").InnerText
        TextBoxContraceptifUrgPrescr.Text = XMLelementliste.Item(0).Item("NumPrecripteur").InnerText
        TextBoxContraceptifUrgPrescrClé.Text = XMLelementliste.Item(0).Item("CleNumPrecripteur").InnerText
        TextBoxContraceptifUrgCIP.Text = XMLelementliste.Item(0).Item("ContraceptifCIP").InnerText

        'OCT
        XMLelementliste = ConfigXml.GetElementsByTagName("OCT")
        If UCase(XMLelementliste.Item(0).Item("UtilisationOCT").InnerText) = "OUI" Then
            OCT_CheckBox.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("FSE_FichierUnique").InnerText) = "OUI" Then
            OCT_FSE_FichierUnique_CheckBox1.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("FSE_Eclatement_Source").InnerText) = "OUI" Then
            OCT_FSE_Eclatement_Source_CheckBox.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("FSEenrichies").InnerText) = "OUI" Then
            OCT_FSE_Enrichies_CheckBox1.Checked = True
        End If

        OCT_TextBox_Adresse_Destinataire.Text = XMLelementliste.Item(0).Item("AdressemailOCT").InnerText
        OCT_TextBox_Adresse_TypeDestinataire.Text = XMLelementliste.Item(0).Item("TypeDestinataire").InnerText
        OCT_TextBox_Application.Text = XMLelementliste.Item(0).Item("Application").InnerText

        'Conventions
        XMLelementliste = ConfigXml.GetElementsByTagName("Conventions")
        If UCase(XMLelementliste.Item(0).Item("Activer").InnerText) = "OUI" Then
            Conventions_CheckBox.Checked = True
        End If

        'LOI
        XMLelementliste = ConfigXml.GetElementsByTagName("LOI")
        If UCase(XMLelementliste.Item(0).Item("LOI_Activer").InnerText) = "OUI" Then
            LOI_Activer_CheckBox.Checked = True
        End If
        LOI_serveurPOP_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Pop3_Serveur").InnerText
        LOI_PortPOP_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Pop3_Port").InnerText
        LOI_User_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Pop3_User").InnerText
        LOI_Password_MaskedTextBox.Text = XMLelementliste.Item(0).Item("LOI_Pop3_Password").InnerText
        LOI_Rep_Bal_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Pop3_RepBAL").InnerText
        LOI_Distr_OCT_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Distr_OCT").InnerText
        LOI_Distr_horsOCT_TextBox.Text = XMLelementliste.Item(0).Item("LOI_Distr_horsOCT").InnerText
        LOI_Ref_Label.Text = "Ref: " + XMLelementliste.Item(0).Item("LOI_Ref").InnerText
        LOI_NB_JOUR_Max_TextBox.Text = XMLelementliste.Item(0).Item("LOI_NB_JOUR_Max").InnerText
        If UCase(XMLelementliste.Item(0).Item("LOI_Controle_MAJ_mensuel").InnerText) = "OUI" Then
            LOI_Controle_MAJ_mensuel_CheckBox.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("LOI_Demande_Auto").InnerText) = "OUI" Then
            LOI_Demande_Auto_CheckBox.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("LOI_Verif_InReply_To").InnerText) = "OUI" Then
            LOI_Verif_InReply_To_CheckBox.Checked = True
        End If
        LOI_serveurSMTP_TextBox.Text = XMLelementliste.Item(0).Item("LOI_smtp_serveur").InnerText
        LOI_PortSMTP_TextBox.Text = XMLelementliste.Item(0).Item("LOI_smtp_serveur_port").InnerText
        LOI_BAL.Text = XMLelementliste.Item(0).Item("LOI_Pop3_BAL").InnerText

        'DP
        XMLelementliste = ConfigXml.GetElementsByTagName("DP")
        If UCase(XMLelementliste.Item(0).Item("DP_Activer").InnerText) = "OUI" Then
            DP_activer_CheckBox.Checked = True
        End If
        If UCase(XMLelementliste.Item(0).Item("DP_Inciter").InnerText) = "OUI" Then
            DP_inciter_CheckBox.Checked = True
        End If
        DP_Id_Pharma_TextBox.Text = XMLelementliste.Item(0).Item("DP_EtaIdent").InnerText

        XMLelementliste = ConfigXml.GetElementsByTagName("SCOR")
        Code_Région_TextBox.Text = XMLelementliste.Item(0).Item("CodeRégion").InnerText


        Select Case XMLelementliste.Item(0).Item("Dest_ARL").InnerText
            Case "PS"
                Dest_ARL_SCOR_RadioButton1.Checked = True
            Case "OCT"
                Dest_ARL_SCOR_RadioButton2.Checked = True
            Case "PS/OCT"
                Dest_ARL_SCOR_RadioButton3.Checked = True
        End Select
        If XMLelementliste.Item(0).Item("cdrom").InnerText = "OUI" Then
            SCOR_cdrom_activer_CheckBox.Checked = True
        Else
            SCOR_cdrom_activer_CheckBox.Checked = False
        End If
        SCOR_cdrom_interval_TextBox.Text = XMLelementliste.Item(0).Item("cdrom_inter").InnerText
        Select Case UCase(XMLelementliste.Item(0).Item("Scor_Transmission_via_OCT").InnerText)
            Case "OUI"
                Scor_Transmission_via_OCT_CheckBox1.Checked = True
            Case "NON"
                Scor_Transmission_via_OCT_CheckBox1.Checked = False
        End Select
        '

    End Sub

    'save
    Private Sub Button_Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Fermer.Click
        MyMsg_Box.Presentation("Officine", "Voulez-vous Sauvegarder les modifications?", True, "OUI", True, "NON", True, "Annuler")
        If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
            SaveConfig()
            Config.Load_Config_File()
            Me.Dispose()
        Else
            Me.Dispose()
        End If

    End Sub
    Private Sub SaveConfig()
        Dim ConfigXml As New XmlDocument
        ConfigXml.Load(Application.StartupPath + "\config.xml")
        Dim XMLelementliste As XmlNodeList


        'SMTP
        XMLelementliste = ConfigXml.GetElementsByTagName("SMTP")
        XMLelementliste.Item(0).Item("TypeEmetteurFichier").InnerText = Mid(ComboBox_Type_émetteur_fichier.Text, 1, 2)
        XMLelementliste.Item(0).Item("NumEmetteurFichier").InnerText = TextBox_Num_émetteur_fichier.Text
        XMLelementliste.Item(0).Item("Serveur_Externe").InnerText = SMTP_Serv_ext_TextBox.Text
        XMLelementliste.Item(0).Item("Serveur_Externe_port").InnerText = SMTP_Serv_ext_port_TextBox.Text
        XMLelementliste.Item(0).Item("From").InnerText = Pop3_BAL.Text

        'STS
        XMLelementliste = ConfigXml.GetElementsByTagName("STS")
        If CheckBox_STS_Activer.Checked = True Then
            XMLelementliste.Item(0).Item("Activer").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("Activer").InnerText = "NON"
        End If
        If CheckBox_STS_Afficheniveau1.Checked = True Then
            XMLelementliste.Item(0).Item("AfficheDiagNiv1").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("AfficheDiagNiv1").InnerText = "NON"
        End If

        'Pop3
        XMLelementliste = ConfigXml.GetElementsByTagName("Pop3")
        XMLelementliste.Item(0).Item("Serveur").InnerText = Pop3_Serveur_TextBox.Text
        XMLelementliste.Item(0).Item("Port").InnerText = Pop3_Port_TextBox.Text
        XMLelementliste.Item(0).Item("User").InnerText = Pop3_User_TextBox.Text
        XMLelementliste.Item(0).Item("Password").InnerText = Pop3_Password_MaskedTextBox.Text
        XMLelementliste.Item(0).Item("RepBAL").InnerText = Pop3_RepBAL.Text

        'TRANSMISSION
        XMLelementliste = ConfigXml.GetElementsByTagName("SMTP")
        XMLelementliste.Item(0).Item("MessageFormatFichier").InnerText = TextBox_G14C3_G94C3.Text
        If CheckBox_compression.Checked = True Then
            XMLelementliste.Item(0).Item("CompresserDonnees").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("CompresserDonnees").InnerText = "NON"
        End If
        'modefonctionement
        XMLelementliste = ConfigXml.GetElementsByTagName("Configuration")
        XMLelementliste.Item(0).Item("ModeFonctionnement").InnerText = ComboBoxModeFonctionnement.Text
        If CheckBox_Desynchroniser.Checked = True Then
            XMLelementliste.Item(0).Item("Desynchroniser").InnerText = "oui"
        Else
            XMLelementliste.Item(0).Item("Desynchroniser").InnerText = "non"
        End If
        'pmss
        XMLelementliste = ConfigXml.GetElementsByTagName("Data")
        XMLelementliste.Item(0).Item("PMSS").InnerText = TextBoxPMSS.Text

        'contaceptif urgence
        XMLelementliste = ConfigXml.GetElementsByTagName("ContaceptifUrgence")
        XMLelementliste.Item(0).Item("NIR").InnerText = TextBoxContraceptifUrgNIR.Text
        XMLelementliste.Item(0).Item("cle").InnerText = TextBoxContraceptifUrgClé.Text
        XMLelementliste.Item(0).Item("datenaissance").InnerText = TextBoxContraceptifUrgDateNaissance.Text
        XMLelementliste.Item(0).Item("ContraceptifCIP").InnerText = TextBoxContraceptifUrgCIP.Text
        XMLelementliste.Item(0).Item("NumPrecripteur").InnerText = TextBoxContraceptifUrgPrescr.Text
        XMLelementliste.Item(0).Item("CleNumPrecripteur").InnerText = TextBoxContraceptifUrgPrescrClé.Text

        'OCT
        XMLelementliste = ConfigXml.GetElementsByTagName("OCT")
        If OCT_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("UtilisationOCT").InnerText = "OUI"
            Config.OCT_UtilisationOCT = True
        Else
            XMLelementliste.Item(0).Item("UtilisationOCT").InnerText = "NON"
            Config.OCT_UtilisationOCT = False
        End If

        If OCT_FSE_Enrichies_CheckBox1.Checked = True Then
            XMLelementliste.Item(0).Item("FSEenrichies").InnerText = "OUI"
            Config.OCT_FSEenrichies = True
        Else
            XMLelementliste.Item(0).Item("FSEenrichies").InnerText = "NON"
            Config.OCT_FSEenrichies = False
        End If

        If OCT_FSE_Eclatement_Source_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("FSE_Eclatement_Source").InnerText = "OUI"
            Config.OCT_FSE_Eclatement_Source = True
        Else
            XMLelementliste.Item(0).Item("FSE_Eclatement_Source").InnerText = "NON"
            Config.OCT_FSE_Eclatement_Source = False
        End If

        If OCT_FSE_FichierUnique_CheckBox1.Checked = True Then
            XMLelementliste.Item(0).Item("FSE_FichierUnique").InnerText = "OUI"
            Config.OCT_FSE_FichierUnique = True
        Else
            XMLelementliste.Item(0).Item("FSE_FichierUnique").InnerText = "NON"
            Config.OCT_FSE_FichierUnique = False
        End If

        XMLelementliste.Item(0).Item("AdressemailOCT").InnerText = OCT_TextBox_Adresse_Destinataire.Text
        XMLelementliste.Item(0).Item("TypeDestinataire").InnerText = OCT_TextBox_Adresse_TypeDestinataire.Text
        XMLelementliste.Item(0).Item("Application").InnerText = OCT_TextBox_Application.Text

        'Conventions
        XMLelementliste = ConfigXml.GetElementsByTagName("Conventions")
        If Conventions_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("Activer").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("Activer").InnerText = "NON"
        End If

        'LOI
        XMLelementliste = ConfigXml.GetElementsByTagName("LOI")
        XMLelementliste.Item(0).Item("LOI_Pop3_Serveur").InnerText = LOI_serveurPOP_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Pop3_Port").InnerText = LOI_PortPOP_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Pop3_User").InnerText = LOI_User_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Pop3_Password").InnerText = LOI_Password_MaskedTextBox.Text
        XMLelementliste.Item(0).Item("LOI_Pop3_RepBAL").InnerText = LOI_Rep_Bal_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Distr_OCT").InnerText = LOI_Distr_OCT_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Distr_horsOCT").InnerText = LOI_Distr_horsOCT_TextBox.Text
        If LOI_Activer_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("LOI_Activer").InnerText = "OUI"
            Dim sr As New StreamReader(Config.SesamIni)
            Dim sw As New StreamWriter(Config.SesamIni + "temp")
            Dim line As String
            line = sr.ReadLine
            While Not line Is Nothing
                sw.WriteLine(line)
                line = sr.ReadLine
            End While
            sw.WriteLine("[LOI]")
            'sw.WriteLine("FichierLOI=" + Config.LOI_Pop3_RepBAL + "loi.loi")
            sw.WriteLine("FichierLOI=" + Application.StartupPath + "\LOI\loi.loi")
            sr.Close()
            sw.Close()
            My.Computer.FileSystem.DeleteFile(Config.SesamIni)
            My.Computer.FileSystem.RenameFile(Config.SesamIni + "temp", "sesam.ini")
        Else
            XMLelementliste.Item(0).Item("LOI_Activer").InnerText = "NON"
            Dim sr As New StreamReader(Config.SesamIni)
            Dim sw As New StreamWriter(Config.SesamIni + "temp")
            Dim line As String
            line = sr.ReadLine
            While Not line Is Nothing
                If InStr(line, "[LOI]") = 0 And InStr(line, "FichierLOI=") = 0 Then
                    sw.WriteLine(line)
                End If
                line = sr.ReadLine
            End While
            sr.Close()
            sw.Close()
            My.Computer.FileSystem.DeleteFile(Config.SesamIni)
            My.Computer.FileSystem.RenameFile(Config.SesamIni + "temp", "sesam.ini")
        End If
        XMLelementliste.Item(0).Item("LOI_NB_JOUR_Max").InnerText = LOI_NB_JOUR_Max_TextBox.Text
        If LOI_Demande_Auto_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("LOI_Demande_Auto").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("LOI_Demande_Auto").InnerText = "NON"
        End If
        If LOI_Controle_MAJ_mensuel_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("LOI_Controle_MAJ_mensuel").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("LOI_Controle_MAJ_mensuel").InnerText = "NON"
        End If
        If LOI_Verif_InReply_To_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("LOI_Verif_InReply_To").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("LOI_Verif_InReply_To").InnerText = "NON"
        End If
        XMLelementliste.Item(0).Item("LOI_smtp_serveur").InnerText = LOI_serveurSMTP_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_smtp_serveur_port").InnerText = LOI_PortSMTP_TextBox.Text
        XMLelementliste.Item(0).Item("LOI_Pop3_BAL").InnerText = LOI_BAL.Text

        'DP
        XMLelementliste = ConfigXml.GetElementsByTagName("DP")
        If DP_activer_CheckBox.Checked = True Then


            If Config.DP_Activer = False Then
                If Config.DP_is_local_client = False Then
                    X_DP_Sequence_Number = Val(My.Computer.FileSystem.ReadAllText(Config.LocalNetwork_ServerName & "\DP\X_DP_Sequence_Number"))
                    DP_Connect_thread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf DP_Connect.connect))
                    DP_Connect_thread.Start()
                End If
                Config.DP_Activer = True
            End If

            XMLelementliste.Item(0).Item("DP_Activer").InnerText = "OUI"

        Else
            If Config.DP_Activer = True Then
                If Config.DP_is_local_client = False Then
                    DP_Connect_thread.Abort()
                    DP_Connect.Heartbeat("DESACTIVATION")
                End If
                Config.DP_Activer = False
            End If
            XMLelementliste.Item(0).Item("DP_Activer").InnerText = "NON"

        End If

        XMLelementliste.Item(0).Item("DP_EtaIdent").InnerText = DP_Id_Pharma_TextBox.Text

        If DP_inciter_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("DP_Inciter").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("DP_Inciter").InnerText = "NON"
        End If


        XMLelementliste = ConfigXml.GetElementsByTagName("SCOR")
        XMLelementliste.Item(0).Item("CodeRégion").InnerText = Code_Région_TextBox.Text
        If Dest_ARL_SCOR_RadioButton1.Checked = True Then
            XMLelementliste.Item(0).Item("Dest_ARL").InnerText = "PS"
        ElseIf Dest_ARL_SCOR_RadioButton2.Checked = True Then
            XMLelementliste.Item(0).Item("Dest_ARL").InnerText = "OCT"
        ElseIf Dest_ARL_SCOR_RadioButton3.Checked = True Then
            XMLelementliste.Item(0).Item("Dest_ARL").InnerText = "PS/OCT"
        End If
        If SCOR_cdrom_activer_CheckBox.Checked = True Then
            XMLelementliste.Item(0).Item("cdrom").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("cdrom").InnerText = "NON"
        End If
        XMLelementliste.Item(0).Item("cdrom_inter").InnerText = SCOR_cdrom_interval_TextBox.Text
        If Scor_Transmission_via_OCT_CheckBox1.Checked = True Then
            XMLelementliste.Item(0).Item("Scor_Transmission_via_OCT").InnerText = "OUI"
        Else
            XMLelementliste.Item(0).Item("Scor_Transmission_via_OCT").InnerText = "NON"
        End If

        'save
        ConfigXml.Save("config.xml")
    End Sub

    'AMO
    Private Sub Button_AMO_03_99_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AMO_03_99.Click
        AMO_03_99.ShowDialog()
        AMO_03_99.Dispose()
    End Sub
    Private Sub AMO_Button_Import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AMO_Button_Import.Click
        Dim file As String

        OpenFileDialog1.InitialDirectory = Application.StartupPath
        OpenFileDialog1.ShowDialog()

        file = OpenFileDialog1.FileName
        If file <> "" And My.Computer.FileSystem.FileExists(file) Then
            Dim sr As New StreamReader(file)
            Dim line As String = sr.ReadLine
            Do
                Dim CodeRégime As String = Mid(line, 1, 2)
                Dim CaisseGestionnaire As String = Mid(line, 4, 3)
                Dim CentreGestionnaire As String = Mid(line, 8, 4)
                line = Mid(line, 13)
                Dim libellé = Mid(line, 1, InStr(line, ";") - 1)
                line = Mid(line, libellé.Length + 2)
                Dim OrganismeDestinataire = Mid(line, 1, 3)
                Dim CentreInformatique = Mid(line, 5, 3)
                Select Case Me.AMOTableAdapter.SelectCount(CodeRégime, CaisseGestionnaire, CentreGestionnaire)
                    Case 0
                        Me.AMOTableAdapter.Insert(CodeRégime, CaisseGestionnaire, CentreGestionnaire, OrganismeDestinataire, CentreInformatique, libellé)
                    Case 1
                        Me.AMOTableAdapter.UpdateQuery(CodeRégime, CaisseGestionnaire, CentreGestionnaire, OrganismeDestinataire, CentreInformatique, libellé)
                    Case Else
                        MyMsg_Box.Presentation("Officine", "PLUSIEURS ENREGISTREMENTS POUR:" + Chr(10) + "Code Régime:" + CodeRégime + Chr(10) + "Caisse Gestionnaire:" + CaisseGestionnaire + Chr(10) + "Centre Gestionnaire:" + CentreGestionnaire, False, "", False, "", True, "OK")
                        MyMsg_Box.ShowDialog()
                End Select
                line = sr.ReadLine
            Loop Until line Is Nothing
        End If
    End Sub


    Private Sub TextBox_G14C3_G94C3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox_G14C3_G94C3.KeyPress
        Select Case Asc(e.KeyChar)
            Case Asc("a") To Asc("z"), Asc("A") To Asc("Z"), 32, 8, Asc("1") To Asc("9")
            Case Else
                Beep()
                e.KeyChar = Nothing
        End Select
    End Sub
#Region "CONVENTIONS"
    Private Sub Button_Convention_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Convention.Click
        Convention_form.Visible = True
    End Sub
    Private Sub Button_Regroupement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Regroupement.Click
        Regroupement.Visible = True
    End Sub
    Private Sub Button_Correspondance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Correspondance.Click
        Correspondance.Visible = True
    End Sub
#End Region

#Region "OCT"

    Private Sub OCT_TextBox_Application_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles OCT_TextBox_Application.Validated
        If OCT_TextBox_Application.TextLength <> 2 Then
            OCT_TextBox_Application.Text = "TO"
        Else
            OCT_TextBox_Application.Text = UCase(OCT_TextBox_Application.Text)
        End If
    End Sub

    Private Sub OCT_FSE_FichierUnique_CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OCT_FSE_FichierUnique_CheckBox1.CheckedChanged
        If OCT_FSE_FichierUnique_CheckBox1.Checked = True Then
            OCT_FSE_Eclatement_Source_CheckBox.Checked = False
            OCT_FSE_Enrichies_CheckBox1.Checked = False
        End If
    End Sub
    Private Sub OCT_FSE_Eclatement_Source_CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OCT_FSE_Eclatement_Source_CheckBox.CheckedChanged
        If OCT_FSE_Eclatement_Source_CheckBox.Checked = True Then
            OCT_FSE_FichierUnique_CheckBox1.Checked = False
            OCT_FSE_Enrichies_CheckBox1.Checked = False
        End If
    End Sub
    Private Sub OCT_FSE_Enrichies_CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OCT_FSE_Enrichies_CheckBox1.CheckedChanged
        If OCT_FSE_Enrichies_CheckBox1.Checked = True Then
            OCT_FSE_FichierUnique_CheckBox1.Checked = False
            OCT_FSE_Eclatement_Source_CheckBox.Checked = False
        End If
    End Sub
#End Region



    Private Sub Button_Remplacement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Remplacement.Click
        SGD_LibererSession(NumeroSessionCPS)
        If SGD_ReserverSession(0, NumeroSessionCPS) <> 0 Then
            MyMsg_Box.Presentation("Officine", "ERREUR POUR LA RESERVATION DE SESSION CPS", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Me.Close()
        End If
        AfficheErreur(SIS_LireCartePS(NomRessourcePS, NomRessourceLecteur, "", NumeroSessionCPS))
        Dim num_titulaire As String = "123456789012345678901234567890"
        TailleMax = 30
        SGD_LireChamp(NumeroSessionCPS, 1, 1, 3, num_titulaire, TailleMax)
        num_titulaire = Mid(num_titulaire, 1, TailleMax)
        If Config.Activer_Session_Remplacement = True And num_titulaire.ToString <> Config.Titulaire_Num_Id_Nat.ToString Then
            MyMsg_Box.Presentation("Officine", "Insérez la carte PS déclarée Titulaire pour accéder à la configuration ou la désactivation du remplaçant.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        Config_Remplacement.Visible = True
    End Sub

    Private Sub Button_Lecteur_SV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Lecteur_SV.Click
        Config_Lecteur.Visible = True
    End Sub


    Private Sub Button_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clear.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub Button_LireVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LireVersion.Click
        Dim numerosession As Integer
        AfficheErreur(SGD_ReserverSession(10000, numerosession), "SGD_ReserverSession")
        STS_InitLIB2(Config.SesamIni)
        AfficheErreur(STS_LireVersion_S(numerosession), "STS_LireVersion")
        Me.UseWaitCursor = True
        Dim IdGroupeSuivant As UInt16
        Dim OccurrenceGroupeSuivant As UInt16
        Dim IdGroupeCourant As UInt16 = 0
        Dim OccurrenceGroupeCourant As UInt16 = 0
        While SGD_LireGroupeSuivant(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, IdGroupeSuivant, OccurrenceGroupeSuivant) = 0
            IdGroupeCourant = IdGroupeSuivant
            OccurrenceGroupeCourant = OccurrenceGroupeSuivant
            Dim i As UInt16 = 1
            Dim ValeurChamp As String = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
            Dim TailleMax As Integer = 200
            While SGD_LireChamp(numerosession, IdGroupeCourant, OccurrenceGroupeCourant, i, ValeurChamp, TailleMax) = 0
                Select Case i
                    Case 1
                        RichTextBox1.AppendText(Chr(10) + "Identification du module STS: " + Mid(ValeurChamp, 1, TailleMax))
                    Case 2
                        RichTextBox1.AppendText("  |  Version du module: " + Mid(ValeurChamp, 1, TailleMax))
                    Case 3
                        RichTextBox1.AppendText("  |  Version du fichier de tables STS: " + Mid(ValeurChamp, 1, TailleMax))
                    Case 4
                        RichTextBox1.AppendText("  |  Variante: " + Mid(ValeurChamp, 1, TailleMax))
                    Case 5
                        RichTextBox1.AppendText("  |  Commentaire: " + Mid(ValeurChamp, 1, TailleMax))

                End Select
                ValeurChamp = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
                TailleMax = 200
                i = i + 1
            End While
        End While
        Me.UseWaitCursor = False
        SGD_LibererSession(numerosession)
    End Sub

    Private Sub OCT_CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OCT_CheckBox.CheckedChanged
        MyMsg_Box.Presentation("Officine", "ATTENTION, le changement de mode de transmission peut entrainer la perte de données." + Chr(10) + "Il est conseillé de vider la BAL Sesam-Vitale avant le changement.", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
    End Sub



    Private Sub LOI_NB_JOUR_Max_TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles LOI_NB_JOUR_Max_TextBox.Validated
        If IsNumeric(LOI_NB_JOUR_Max_TextBox.Text) = False Then
            LOI_NB_JOUR_Max_TextBox.Text = "7"
        End If
    End Sub






    Private Sub CDC_Table7_Button_Click(sender As Object, e As EventArgs) Handles CDC_Table7_Button.Click
        Dim file As String
        OpenFileDialog1.ShowDialog()
        file = OpenFileDialog1.FileName
        If file <> "" Then
            CDC_Table7TableAdapter.DeleteAll()
            Dim sr As New StreamReader(file)
            Dim line As String = sr.ReadLine
            Do
                Dim Code_Prestation As String
                Dim Gratuit, Déplacement_non_prescrit, Dépassement_exigence, Entente_Directe, Non_remboursable, Dépassement_autorisé, Dépassement_maitrisé, Cumul_DA_ED, Cumul_DM_DE, Prise_en_charge_SMG As Boolean
                Code_Prestation = Mid(line, 1, InStr(line, " ") - 1)

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Gratuit = False
                Else
                    Gratuit = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Déplacement_non_prescrit = False
                Else
                    Déplacement_non_prescrit = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Dépassement_exigence = False
                Else
                    Dépassement_exigence = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Entente_Directe = False
                Else
                    Entente_Directe = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Non_remboursable = False
                Else
                    Non_remboursable = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Dépassement_autorisé = False
                Else
                    Dépassement_autorisé = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Dépassement_maitrisé = False
                Else
                    Dépassement_maitrisé = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Cumul_DA_ED = False
                Else
                    Cumul_DA_ED = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Cumul_DM_DE = False
                Else
                    Cumul_DM_DE = True
                End If

                line = Mid(line, InStr(line, " ") + 1)
                If Mid(line, 1, 1) = "0" Then
                    Prise_en_charge_SMG = False
                Else
                    Prise_en_charge_SMG = True
                End If

                CDC_Table7TableAdapter.InsertQuery(Code_Prestation, Gratuit, Déplacement_non_prescrit, Dépassement_exigence, Entente_Directe, Non_remboursable, Dépassement_autorisé, Dépassement_maitrisé, _
                                                   Cumul_DA_ED, Cumul_DM_DE, Prise_en_charge_SMG)
                line = sr.ReadLine

            Loop Until line Is Nothing

            MyMsg_Box.Presentation("Officine", "T7 chargée", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If

    End Sub



    Private Sub Desync_cps_button_Click(sender As Object, e As EventArgs) Handles Desync_cps_button.Click
        COMPTOIR_MAIN.Lire_CPS()
        Dim infoproXml As New XmlDocument
        infoproXml.Load(Application.StartupPath + "\InfoPro.xml")
        Dim XMLelementliste As XmlNodeList

        XMLelementliste = infoproXml.GetElementsByTagName("Desync")

        ValeurChamp = "@@@@@@@@"
        TailleMax = 8
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 9, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C9").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@"
        TailleMax = 1
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 10, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C10").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        TailleMax = 40
        SGD_LireChamp(NumeroSessionCPS, 1, 1, 6, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G1C6").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        TailleMax = 40
        SGD_LireChamp(NumeroSessionCPS, 1, 1, 7, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G1C7").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@"
        TailleMax = 1
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 13, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C13").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@"
        TailleMax = 2
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 14, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C14").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@"
        TailleMax = 2
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 15, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C15").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@"
        TailleMax = 2
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 16, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C16").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@"
        TailleMax = 1
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 17, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C17").InnerText = Mid(ValeurChamp, 1, TailleMax)

        '<ConditionDexercice>L</ConditionDexercice>
        '<G1C3></G1C3>
        '<G1C4></G1C4>

        ValeurChamp = "@"
        TailleMax = 1
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 5, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C5").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@@@@@@@@"
        TailleMax = 9
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 6, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C6").InnerText = Mid(ValeurChamp, 1, TailleMax)

        ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        TailleMax = 40
        SGD_LireChamp(NumeroSessionCPS, 2, 1, 8, ValeurChamp, TailleMax)
        XMLelementliste.Item(0).Item("G2C8").InnerText = Mid(ValeurChamp, 1, TailleMax)

        infoproXml.Save("InfoPro.xml")
    End Sub



    Private Sub DP_Visualiser_traces_Button_Click(sender As Object, e As EventArgs) Handles DP_Visualiser_traces_Button.Click
        Dim cherche As String
        cherche = InputBox("nom?")

    End Sub


    Private Sub DP_EtaIdent_Button_Click(sender As Object, e As EventArgs) Handles DP_EtaIdent_Button.Click
        SGD_LireChamp(NumeroSessionCPS, 2, Occurence_Grp2, 6, ValeurChamp, 14)
        DP_Id_Pharma_TextBox.Text = Mid(ValeurChamp, 1, 9)
    End Sub



    Private Sub Code_Région_TextBox_Validated(sender As Object, e As EventArgs) Handles Code_Région_TextBox.Validated
        If Code_Région_TextBox.Text.Length <> 2 Or Not IsNumeric(Code_Région_TextBox.Text) Then
            MyMsg_Box.Presentation("Officine", "Code Région Invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Code_Région_TextBox.Text = ""
        End If
    End Sub

    Private Sub SCOR_cdrom_activer_CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles SCOR_cdrom_activer_CheckBox.CheckedChanged
        If SCOR_cdrom_activer_CheckBox.Checked = True Then
            MyMsg_Box.Presentation("Officine", "SCOR" & vbCrLf & "Voulez vous activer le mode secours(problème de transmission des lots de PJ)?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = MsgBoxResult.Yes Then
                Teletransmission_MAIN.SCOR_lotsTableAdapter.Update_cdrom_secours()
            End If
        End If

    End Sub

    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 Import_BDM_CIP_Button_Click(sender As Object, e As EventArgs) Handles Import_BDM_CIP_Button.Click
        'http://www.codage.ext.cnamts.fr/codif/bdm_it/index_presentation.php?p_site=AMELI
        'Fichier tarifaire total  LPPTOT
        MyMsg_Box.Presentation("Officine", "http://www.codage.ext.cnamts.fr/codif/bdm_it/index_presentation.php?p_site=AMELI", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then Exit Sub
        OpenFileDialog1.DefaultExt = "csv"
        OpenFileDialog1.InitialDirectory = Application.StartupPath & "\"
        OpenFileDialog1.FileName = "BDM_CIP"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName = "" Then
                Exit Sub
            End If
            Import_BDM_CIP(OpenFileDialog1.FileName)
        End If
    End Sub


    Private Sub Import_BDM_CIP(ByVal filename As String)
        Bandeau_info_bas_show("importation BDM_CIP")
        Try

            Dim sr As New StreamReader(filename)
            Dim line = sr.ReadLine
            line = sr.ReadLine
            While Not line Is Nothing
                Dim DEBUT_REMB As Date
                If CSV_Champ(line, 7) = "" Then
                    DEBUT_REMB = Now
                Else
                    DEBUT_REMB = CDate(CSV_Champ(line, 7))
                End If
                Dim FIN_REMB As Date
                If CSV_Champ(line, 8) = "" Then
                    FIN_REMB = DateAdd(DateInterval.Year, 20, Now)
                Else
                    FIN_REMB = CDate(CSV_Champ(line, 8))
                End If
                Me.BDM_CIPTableAdapter.Insert(CSV_Champ(line, 1), CSV_Champ(line, 2), CSV_Champ(line, 3), CSV_Champ(line, 4), CSV_Champ(line, 5), CSV_Champ(line, 6), DEBUT_REMB, _
                                              FIN_REMB, CSV_Champ(line, 9), CSV_Champ(line, 10), CSV_Champ(line, 11), CSV_Champ(line, 12), CSV_Champ(line, 13), CSV_Champ(line, 14), _
                                              CSV_Champ(line, 15), CSV_Champ(line, 16), CSV_Champ(line, 17), CSV_Champ(line, 18), CSV_Champ(line, 19), CSV_Champ(line, 20), CSV_Champ(line, 21), _
                                              CSV_Champ(line, 22), CSV_Champ(line, 23), CSV_Champ(line, 24), CSV_Champ(line, 25), CSV_Champ(line, 26), CSV_Champ(line, 27), CSV_Champ(line, 28), _
                                              CSV_Champ(line, 29), CSV_Champ(line, 30), CSV_Champ(line, 31), CSV_Champ(line, 32), CSV_Champ(line, 33), CSV_Champ(line, 34))
                line = sr.ReadLine

            End While
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", ex.ToString, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End Try

        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)



    End Sub
    Private Sub Import_LPP_Button_Click(sender As Object, e As EventArgs) Handles Import_LPP_Button.Click
        'http://www.codage.ext.cnamts.fr/codif/tips//telecharge/index_tele.php?p_site=AMELI
        'Fichier tarifaire total  LPPTOT
        MyMsg_Box.Presentation("Officine", "http://www.codage.ext.cnamts.fr/codif/tips//telecharge/index_tele.php?p_site=AMELI" & Chr(10) & "Fichier tarifaire total  LPPTOT", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then Exit Sub
        OpenFileDialog1.DefaultExt = ""
        OpenFileDialog1.InitialDirectory = Application.StartupPath & "\LPP"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName = "" Then
                Exit Sub
            End If
            Import_LPP(OpenFileDialog1.FileName)
        End If
    End Sub
    Public Sub Import_LPP(ByVal FileName)
        'http://www.codage.ext.cnamts.fr/codif/tips//telecharge/index_tele.php?p_site=AMELI
        'Fichier tarifaire total  LPPTOT

        Bandeau_info_bas_show("Debut importation base LPP")

        Try


            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\LPP\import_LPP.txt", Now.ToLongDateString & " " & Now.ToLongTimeString & "  " & FileName.ToString & vbCrLf, True)







            Dim bytes() = My.Computer.FileSystem.ReadAllBytes(FileName)

            COMPTOIR_MAIN.LPPTableAdapter.Delete_All() 'après avoir ouvert fichier pour ne pas delete si erreur ouverture fichier
            System.Threading.Thread.Sleep(2000)


            Dim line_bytes() As Byte
            ReDim line_bytes(128)

            Dim PrixLPP, DateMin, DateMax_110, Prestation, Tarif_de_référence, Coefficients_majorations_DOM, _
                Quantité_maximale, Montant_maximal, Prix_unitaire_réglementé, Prise_en_charge_particulière As String
            Dim CodeLPP = ""
            Dim Type_prestation = ""
            Dim Libellé = ""
            Dim DateMax_101 = ""
            Dim Code_identifiant_de_prothèse = ""
            Dim Age_maximal = ""
            Dim Entente_Préalable, Indication_médicale, Indication_PU_sur_devis As Boolean

            For i = 1 To bytes.Length / 128 'la première ligne "Enregistrement « Début de fichier » - type 000" est passée (on pourra programmer le controle des données de cette ligne ultèrieurement)
                Array.Copy(bytes, CInt(i * 128), line_bytes, 0, 128)
                Dim line As String = System.Text.Encoding.ASCII.GetString(line_bytes)

                Select Case Mid(line, 1, 3)

                    Case "101" 'Enregistrements « Code référence LPP » - type 101 
                        Select Case Mid(line, 4, 2)
                            Case "01" '(rubrique 1)
                                CodeLPP = Mid(line, 8, 13)
                                Libellé = Mid(line, 21, 80)
                                Bandeau_info_bas_change_text("Importation LPP " + CodeLPP + " " + Libellé)

                                Application.DoEvents()
                            Case "02" '(rubrique 2)
                                DateMax_101 = Mid(line, 8, 8)
                                Age_maximal = Mid(line, 16, 3)
                                Type_prestation = Mid(line, 19, 1)
                                If UCase(Mid(line, 20, 1)) = "O" Then
                                    Indication_médicale = True
                                Else
                                    Indication_médicale = False
                                End If
                                Code_identifiant_de_prothèse = Mid(line, 93, 4)
                        End Select

                    Case "110" 'Enregistrement « Historique » - type 110
                        DateMin = Mid(line, 8, 8)
                        DateMax_110 = Mid(line, 16, 8)
                        If Val(DateMax_110) = 0 And Val(DateMax_101) <> 0 Then
                            DateMax_110 = DateMax_101
                        End If
                        If DateMax_110 = "00000000" Then DateMax_110 = "99999999"
                        Prestation = Mid(line, 24, 3)
                        If UCase(Mid(line, 27, 1)) = "O" Then
                            Entente_Préalable = True
                        Else
                            Entente_Préalable = False
                        End If
                        If Mid(line, 44, 1) = "1" Then
                            Indication_PU_sur_devis = True
                        Else
                            Indication_PU_sur_devis = False
                        End If
                        PrixLPP = Mid(line, 45, 10)
                        While InStr(PrixLPP, "0") = 1 And PrixLPP.Length > 1
                            PrixLPP = Mid(PrixLPP, 2)
                        End While
                        Tarif_de_référence = Mid(line, 45, 10)
                        Coefficients_majorations_DOM = Mid(line, 55, 16)
                        Quantité_maximale = Mid(line, 71, 3)
                        Montant_maximal = Mid(line, 74, 10)
                        Prix_unitaire_réglementé = Mid(line, 84, 10)
                        Prise_en_charge_particulière = Mid(line, 94, 6)

                        COMPTOIR_MAIN.LPPTableAdapter.Insert(CodeLPP, PrixLPP, DateMax_110, DateMin, Entente_Préalable, Prestation, Age_maximal, Code_identifiant_de_prothèse, Coefficients_majorations_DOM, _
                                                             Indication_PU_sur_devis, Indication_médicale, Libellé, Montant_maximal, Prise_en_charge_particulière, Prix_unitaire_réglementé, Quantité_maximale, _
                                                             Tarif_de_référence, Type_prestation)

                    Case "120" 'Enregistrement « Incompatibilités » - Type 120
                    Case "130" 'Enregistrement « Compatibilités » - Type 130
                    Case "199" 'Enregistrement « Fin code référence LPP » - Type 199

                    Case "999" 'Enregistrement « Fin de fichier » - Type 999
                        My.Computer.FileSystem.DeleteFile(FileName)
                        Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
                        Exit For

                End Select

            Next
            'importation code des produits Vidal
            Bandeau_info_bas_hide()

            Bandeau_info_bas_show("Importation LPP VIDAL")

            Dim list_code_lpp As New List(Of String)
            Dim prods = ProduitsTableAdapter1.CodeLPP_with_no_inscription
            For r = 0 To prods.Rows.Count - 1
                If Not list_code_lpp.Contains(prods.Item(r).LPP) Then
                    list_code_lpp.Add(prods.Item(r).LPP)
                End If
            Next
            Dim Kits = COMPTOIR_MAIN.KitsTableAdapter.CodeLPP_with_no_inscription
            For r = 0 To Kits.Rows.Count - 1
                If Not list_code_lpp.Contains(Kits.Item(r).Code_LPP) Then
                    list_code_lpp.Add(Kits.Item(r).Code_LPP)
                End If
            Next
            For Each code_lpp In list_code_lpp
                Bandeau_info_bas_change_text("Importation LPP VIDAL " & code_lpp)

                Import_Produit_From_Vidal.import_LPP_from_vidal(code_lpp)
            Next
            Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)

        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", ex.Message, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
        End Try

    End Sub


    Private Sub Import_CEPS_Button_Click(sender As Object, e As EventArgs) Handles Import_CEPS_Button.Click
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\CEPS"
        OpenFileDialog1.FileName = ""
        Dim CEPS_File As String = ""
        MyMsg_Box.Presentation("Officine", "SELECTIONNER CEPS QUOTIDIEN", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName <> "" Then
                PRODUITS_bdd.CEPS_import_xml_quotidien(OpenFileDialog1.FileName)
            End If
        End If
    End Sub


    Private Sub Recup_DP_Alerte_Button_Click(sender As Object, e As EventArgs) Handles Recup_DP_Alerte_Button.Click
        DP_Alerte.LECTURE_MSG(InputBox("Ref MESSAGE"))
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\CEPS"
        OpenFileDialog1.FileName = ""
        MyMsg_Box.Presentation("Officine", "SELECTIONNER CEPS MENSUEL", False, "", False, "", True, "OK")
        MyMsg_Box.ShowDialog()
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If OpenFileDialog1.FileName <> "" Then
                PRODUITS_bdd.CEPS_import_xml_mensuel(OpenFileDialog1.FileName, True)
            End If
        End If

    End Sub


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


End Class