' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.IO

Public Class jeux_d_essai
    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
    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

    Public prénom_bénéficiaire As String
    Public mode_facturation As String

    Private Structure Prestation
        Dim code_acte As String
        Dim produit As String
        Dim montant_facturé As String
        Dim codeexo As String
    End Structure
    Dim Prestations() As Prestation

    Private Sub Facture_Tronc_Commun()

        Dim sr As New StreamReader(Application.StartupPath + "\technique\jeux d'essai\jeux d'essai tronc commun.txt")
        Dim line As String
        Dim num_CV As String
        line = sr.ReadLine
        Dim date_facture, date_soins, date_prescription As Date
        Dim num_prescripteur As String = ""
        While InStr(line, Chapitre_combobox.Text) = 0
            line = sr.ReadLine
        End While
        While InStr(line, "Test n°" + num_Test_TextBox.Text) = 0
            line = sr.ReadLine
        End While
        RichTextBox1.Clear()
        RichTextBox1.AppendText(Chapitre_combobox.Text + " " + line + Chr(10))
        line = sr.ReadLine
        mode_facturation = line
        RichTextBox1.AppendText(line + Chr(10))
        While InStr(line, "CV ") <> 1
            If InStr(line, "Origine de la prescription") <> 0 Then
                COMPTOIR_MAIN.OriginePrescriptionToolStripMenuItem.Checked = True
                COMPTOIR_MAIN.OriginePrescriptionToolStripComboBox.Text = "T prescription établie par le médecin traitant"
            End If
            line = sr.ReadLine
        End While
        num_CV = Mid(line, 4, 4)
        prénom_bénéficiaire = Mid(line, 9)
        While InStr(line, "AMC") <> 1
            line = sr.ReadLine

        End While

        ReDim Prestations(10)
        Dim num_prestation As Integer = 0
        line = sr.ReadLine
        While InStr(line, "Assurance ") <> 1
            num_prestation = num_prestation + 1
            Prestations(num_prestation).code_acte = Mid(line, 1, InStr(line, " ") - 1)
            If Prestations(num_prestation).code_acte = "PA" Then
                Prestations(num_prestation).produit = "Genouillère"
            End If
            If InStr(line, "/") <> 0 Then
                line = Mid(line, InStr(line, "/") - 2)
            End If
            While InStr(line, "/") = 0
                line = sr.ReadLine
                If IsNumeric(Mid(line, 1, 13)) Then
                    Prestations(num_prestation).produit = Mid(line, 1, 13)
                ElseIf IsNumeric(Mid(line, 1, 7)) Then
                    Prestations(num_prestation).produit = Mid(line, 1, 7)
                End If
            End While
            date_facture = Date.ParseExact(Mid(line, 1, 10), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
            date_soins = Date.ParseExact(Mid(line, 12, 10), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
            line = Mid(line, 23)
            Prestations(num_prestation).montant_facturé = Mid(line, 1, InStr(line, " ") - 1)
            Prestations(num_prestation).codeexo = Mid(line, InStr(line, "code") + 5, 1)
            line = sr.ReadLine
        End While

        While InStr(line, "Test n°") = 0

            If InStr(line, "N° prescripteur") <> 0 Then
                num_prescripteur = Replace(Mid(line, 19), " ", "")
                num_prescripteur = Mid(num_prescripteur, 1, 8)
            ElseIf InStr(line, "Date de prescription") <> 0 Then
                While InStr(line, "/") = 0
                    line = sr.ReadLine
                End While
                date_prescription = Convert.ToDateTime(Mid(line, InStr(line, "/") - 2, 10))

            End If
            line = sr.ReadLine
        End While

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '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)
                If carte_virt = num_CV Then
                    RichTextBox1.AppendText("carte virtuelle: " + carte_virt + "  " + "carte physique: " + carte_phys + Chr(10) + "Bénéficiaire: " + prénom_bénéficiaire + Chr(10))
                    MyMsg_Box.Presentation("Officine", "INSERER CARTE " + carte_phys + Chr(10) + "Bénéficiaire: " + prénom_bénéficiaire, False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()

                    SetCle(Config.SesamIni, "TESTSSV", "NomFichierVitale1", carte_virt + ".in")
                End If
                lineCV = srCV.ReadLine
            Loop Until lineCV Is Nothing Or lineCV = ""
        End Using
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        COMPTOIR_MAIN.DatePrecription.Value = date_prescription
        COMPTOIR_MAIN.DateExecution.Value = date_soins
        COMPTOIR_MAIN.DateFacture.Value = date_facture

        COMPTOIR_MAIN.LECTURE_VITALE.PerformClick()
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        RichTextBox1.AppendText("Prescr:" + num_prescripteur + " " + Format(date_prescription, "dd/MM/yyyy") + Chr(10) + _
                                "Date Fact:" + Format(date_facture, "dd/MM/yyyy") + "  Date soins:" + Format(date_soins, "dd/MM/yyyy") + Chr(10))
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'COMPTOIR_MAIN.PrescripteurTextBox.Text = num_prescripteur
        COMPTOIR_MAIN.PrescripteurMyCombobox.Text = "prescr"
        COMPTOIR_MAIN.PrescripteurMyCombobox.Focus()
        COMPTOIR_MAIN.TextBox_Prescripteur_NumID.Focus()

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        For i = 1 To 10
            If Not Prestations(i).code_acte Is Nothing Then
                COMPTOIR_MAIN.Vente.Rows.Add()
            End If
        Next
        For i = 1 To 10
            'Dim code_acte As String
            'Dim produit As String
            'Dim montant_facturé As String
            'Dim codeexo As String


            If Not Prestations(i).code_acte Is Nothing Then

                COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCodeActe.Index, i - 1).Value = Prestations(i).code_acte.ToString
                If Not Prestations(i).produit Is Nothing Then
                    RichTextBox1.AppendText(Prestations(i).code_acte.ToString & "  " & Prestations(i).produit.ToString & "  " & Prestations(i).montant_facturé.ToString & "  " & +Prestations(i).codeexo.ToString & Chr(10))
                    COMPTOIR_MAIN.Vente.Rows(i - 1).Cells(COMPTOIR_MAIN.Vente_produits.Index).Selected = True
                    COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Vente_produits.Index, i - 1).Value = Prestations(i).produit.ToString
                    COMPTOIR_MAIN.Vente.Rows(i - 1).Cells(COMPTOIR_MAIN.Vente_produits.Index + 1).Selected = True
                Else
                    RichTextBox1.AppendText(Prestations(i).code_acte.ToString & "  " & Prestations(i).montant_facturé.ToString & "  " & +Prestations(i).codeexo.ToString & Chr(10))
                End If



                COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VentePrix.Index, i - 1).Value = Prestations(i).montant_facturé.ToString
                COMPTOIR_MAIN.Vente.Rows(i - 1).Cells(COMPTOIR_MAIN.VenteQuantité.Index).Selected = True
                COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteQuantité.Index, i - 1).Value = "1"
                COMPTOIR_MAIN.Vente.Rows(i).Cells(COMPTOIR_MAIN.VenteQuantité.Index).Selected = True


                Select Case Prestations(i).codeexo
                    Case "3"
                        COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, i - 1).Value = "SP"
                        COMPTOIR_MAIN.ventetype_defaut = "SP"
                    Case "4"
                        COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, i - 1).Value = "ALD"
                        COMPTOIR_MAIN.ventetype_defaut = "ALD"
                    Case "7"
                        COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, i - 1).Value = "PREV"
                        COMPTOIR_MAIN.ventetype_defaut = "PREV"
                    Case Else
                        COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, i - 1).Value = "assurance"
                        COMPTOIR_MAIN.ventetype_defaut = "assurance"
                End Select
            End If
        Next

        Select Case mode_facturation
            Case "FSE en TP AMO", "FSE en HTP AMO"
                COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
            Case "FSE en TP AMO / TP AMC", "FSE + DRE en TP AMO / TP AMC", "FSE + DRE en HTP AMO / TP AMC", "FSE + DRE en HTP AMO / HTP AMC", "DRE en TP AMC", "DRE en HTP AMC"
                COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = True
            Case "CAS NON PASSANT"
            Case "DRE D’ANNULATION en TP AMC"
            Case Else
                Facture_complétée_CheckBox.Checked = False
        End Select

        If Facture_complétée_CheckBox.Checked Then
            COMPTOIR_MAIN.Button_Facturation_Click(Nothing, Nothing)

        End If
    End Sub



    Private Sub FACTURATION_Button_Click(sender As Object, e As EventArgs) Handles FACTURATION_Button.Click
        If ComboBox1.Text = "TRONC COMMUN" Then
            Facture_Tronc_Commun()
        ElseIf ComboBox1.Text = "TESTS FONCTIONNELS" Then
            TESTS_FONCTIONNELS()
        End If
    End Sub


    Private Sub Attestation_Button_Click(sender As Object, e As EventArgs) Handles Attestation_Button.Click
        Saisie_Attestation(Attestation_TextBox.Text)
    End Sub
    Public Sub Saisie_Attestation(ByVal num_attestation As String)

        'RAZ des valeurs complementaires
        CurrentBenéficiaire.G107C1 = ""
        CurrentBenéficiaire.G107C2 = ""
        CurrentBenéficiaire.G107C3 = ""
        CurrentBenéficiaire.G107C4 = ""
        CurrentBenéficiaire.G107C5 = ""
        CurrentBenéficiaire.G107C6 = ""
        CurrentBenéficiaire.G109C1 = ""
        CurrentBenéficiaire.G109C2 = ""
        CurrentBenéficiaire.G109C3 = ""
        CurrentBenéficiaire.G109C4 = ""
        CurrentBenéficiaire.G109C5 = ""
        CurrentBenéficiaire.G109C6 = ""
        CurrentBenéficiaire.G109C7 = ""
        CurrentBenéficiaire.G109C8 = ""
        CurrentBenéficiaire.G109C9 = ""
        CurrentBenéficiaire.G109C10 = ""
        CurrentBenéficiaire.G109C11 = ""
        CurrentBenéficiaire.G109C12 = ""

        CurrentBenéficiaire.AMC_Code_Aiguillage_STS = ""
        CurrentBenéficiaire.AMC_CodeRoutage = ""
        CurrentBenéficiaire.AMC_Critère_secondaire = ""
        CurrentBenéficiaire.AMC_DateDébut = ""
        CurrentBenéficiaire.AMC_DateFin = ""
        CurrentBenéficiaire.AMC_IdentifiantAMC = ""
        CurrentBenéficiaire.AMC_IdHote = ""
        CurrentBenéficiaire.AMC_Indicateurdetrairement = ""
        CurrentBenéficiaire.AMC_NomDomaine = ""
        CurrentBenéficiaire.AMC_NumAdherent = ""
        CurrentBenéficiaire.AMC_IdentifiantAMC = ""
        CurrentBenéficiaire.AMC_NumMutuelle = ""
        CurrentBenéficiaire.AMC_NumOrganisme = ""
        CurrentBenéficiaire.AMC_Piecejustificative = ""
        CurrentBenéficiaire.AMC_Type_Convention = ""
        CurrentBenéficiaire.AMC_TypeContrat = ""
        CurrentBenéficiaire.AMC_Critère_secondaire = ""

        CurrentConvention.clear()

        COMPTOIR_MAIN.AMC_Intitule_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_Numadhérent_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_IdentifiantAMC_TextBox.Text = ""
        CurrentBenéficiaire.AMC_TypeContrat = ""
        COMPTOIR_MAIN.AMC_Couverture_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text = ""
        COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text = ""
        COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.Text = ""
        COMPTOIR_MAIN.AMC_CodeRoutage_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_IdHote_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_NomDomaine_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_Type_Convention_TextBox.Text = ""
        COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = ""
        COMPTOIR_MAIN.AMC_Critère_secondaire_TextBox.Text = ""

        Dim sr As New StreamReader("C:\DOSSIER_SV\JEU_PHA_1.40.9_022013\attestations.txt", System.Text.Encoding.Default)
        Dim line As String = sr.ReadLine
        COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.Text = "autre (2)"
        While Not line Is Nothing

            If InStr(line, "ATTESTATION N° " + num_attestation + " ") = 1 Then
                line = sr.ReadLine 'Carte vitale :
                line = sr.ReadLine
                If InStr(line, "Numéro de mutuelle :") = 1 Then
                    line = Mid(line, 22)
                    If IsNumeric(Mid(line, 1, InStr(line, " ") - 1)) Then
                        COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text = Mid(line, 1, InStr(line, " ") - 1)
                        CurrentBenéficiaire.AMC_NumMutuelle = COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text

                        If Trim(Mid(line, InStr(line, "Numéro d’AMC associée :") + 24)) <> "" Then
                            COMPTOIR_MAIN.NuméroDAMCAssociéeToolStripMenuItem.Checked = True
                            COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = Trim(Mid(line, InStr(line, "Numéro d’AMC associée : ") + 24))
                            CurrentBenéficiaire.AMC_NumOrganisme = COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text

                        End If
                    Else
                        COMPTOIR_MAIN.NuméroDAMCAssociéeToolStripMenuItem.Checked = False
                    End If

                    line = sr.ReadLine 'Date
                    COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text = Mid(line, 29, 10)
                    COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text = Mid(line, 66, 10)
                    line = sr.ReadLine 'Indicateur de traitement
                    COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text = Mid(line, 28, 2)
                    CurrentBenéficiaire.AMC_Indicateurdetrairement = COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text
                    line = sr.ReadLine 'STS
                    COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text = Mid(line, 7, 1)
                    CurrentBenéficiaire.AMC_Code_Aiguillage_STS = COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text
                    While InStr(line, "Formules autorisées :") = 0
                        line = sr.ReadLine 'Formules autorisées :
                    End While
                    line = sr.ReadLine
                    While InStr(line, "ATTESTATION N°") = 0 And Not line Is Nothing
                        RichTextBox1.AppendText(line + Chr(10))
                        line = sr.ReadLine

                    End While


                ElseIf InStr(line, "Numéro d’AMC :") = 1 Then
                    line = LTrim(Mid(line, 15))


                    If IsNumeric(Mid(line, 1, InStr(line, " ") - 1)) Then
                        COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = Mid(line, 1, InStr(line, " ") - 1)
                        CurrentBenéficiaire.AMC_NumOrganisme = COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text
                        If Trim(Mid(line, InStr(line, "Numéro d’adhérent :") + 19)) <> "" Then
                            COMPTOIR_MAIN.AMC_Numadhérent_TextBox.Text = Trim(Mid(line, InStr(line, "Numéro d’adhérent :") + 19))
                        End If
                    End If
                    line = sr.ReadLine 'Identifiant AMC :
                    COMPTOIR_MAIN.AMC_IdentifiantAMC_TextBox.Text = Trim(Mid(line, 18))
                    CurrentBenéficiaire.AMC_IdentifiantAMC = COMPTOIR_MAIN.AMC_IdentifiantAMC_TextBox.Text
                    line = sr.ReadLine 'Date
                    COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text = Mid(line, 29, 10)
                    COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text = Mid(line, 66, 10)
                    line = sr.ReadLine 'Indicateur de traitement
                    COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text = Mid(line, 28, 2)
                    CurrentBenéficiaire.AMC_Indicateurdetrairement = COMPTOIR_MAIN.AMC_Indicateurdetrairement_TextBox.Text
                    While InStr(line, "STS") <> 1
                        line = sr.ReadLine 'STS
                    End While
                    COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text = Mid(line, 7, 1)
                    CurrentBenéficiaire.AMC_Code_Aiguillage_STS = COMPTOIR_MAIN.AMC_Code_Aiguillage_STS_TextBox.Text
                    While InStr(line, "Code routage") <> 1
                        line = sr.ReadLine 'Code routage :  Domaine : Identifiant hôte :
                    End While
                    line = Mid(line, 15)
                    COMPTOIR_MAIN.AMC_CodeRoutage_TextBox.Text = (Mid(LTrim(line), 1, 2))
                    line = Mid(line, InStr(line, "Domaine :") + 10)
                    COMPTOIR_MAIN.AMC_NomDomaine_TextBox.Text = Trim(Mid(line, 1, InStr(line, "Identifiant hôte :") - 1))
                    line = Mid(line, InStr(line, "Identifiant hôte :") + 18)
                    COMPTOIR_MAIN.AMC_IdHote_TextBox.Text = Trim(line)
                    line = sr.ReadLine 'Type de convention : Critère secondaire :
                    line = Mid(line, 22)
                    If InStr(line, "Critère secondaire :") <> 1 Then
                        COMPTOIR_MAIN.AMC_Type_Convention_TextBox.Text = Trim(Mid(line, 1, InStr(line, "Critère secondaire :") - 1))
                    End If
                    COMPTOIR_MAIN.AMC_Critère_secondaire_TextBox.Text = Trim(Mid(line, InStr(line, "Critère secondaire :") + 20))
                    While InStr(line, "Formules autorisées :") = 0
                        line = sr.ReadLine 'Formules autorisées :
                    End While
                    line = sr.ReadLine
                    While InStr(line, "ATTESTATION N°") = 0 And Not line Is Nothing

                        RichTextBox1.AppendText(line + Chr(10))
                        line = sr.ReadLine
                    End While
                End If
            End If


            line = sr.ReadLine

        End While

    End Sub

    Private Sub CLEAR_Button_Click(sender As Object, e As EventArgs) Handles CLEAR_Button.Click
        RichTextBox1.Clear()

    End Sub
    Private Sub TESTS_FONCTIONNELS()

    End Sub

End Class