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

    Private Sub Choix_Facturation_TP_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed, Me.FormClosed
        date_accident_droit_commun()
    End Sub

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

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        If jeux_d_essai.Visible = True Or Jeux_d_essai2.Visible = True Or CurrentBenéficiaire.G101C8 = Config.Data_ContaceptifUrgence_NIR Then
            Accident_CheckBox.Checked = False
        ElseIf jeux_d_essai_CAS_STANDARD.Visible = True Then
            If jeux_d_essai_CAS_STANDARD.InfosParticulières_Label1.Text = "AVP" Then
                Accident_CheckBox.Checked = True
                Accident_DateTimePicker.Value = New Date(2011, 4, 16)
            Else
                Accident_CheckBox.Checked = False
            End If
            'ElseIf COMPTOIR_MAIN.NatureAssurance_Button.Text <> "ACCIDENT DU TRAVAIL" Then
            '    MyMsg_Box.Presentation("Officine ?", "LES SOINS DISPENSÉS SONT-ILS LA CONSÉQUENCE D'UN ACCIDENT CAUSÉ PAR UN TIERS?", True, "OUI", True, "NON", False, "")
            '    If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
            '        Accident_CheckBox.Checked = True
            '    End If
        Else
            Accident_CheckBox.Checked = False
        End If

        If ((COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = "" And COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text = "") Or COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False) _
        And COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text <> "75500017" And COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text <> "75500017" Then
            Button_Facturation_TP.Visible = False
        End If
        If COMPTOIR_MAIN.FS_Papier = True Or COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text = "" Then
            Button_Facturation_AMO.Visible = False
            Button_Facturation_AMC.Visible = False
            Button_Facturation_HTP.Visible = False
            Button_Facturation_TP.Visible = False
        End If
        If COMPTOIR_MAIN.NatureAssurance_Button.Text = "MALADIE" Then
            Accident_CheckBox.Visible = True
        End If

        If (COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False Or (CurrentConvention.selected = True And CurrentConvention.type_Accord = "H")) _
        And COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text <> "75500017" And COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text <> "75500017" Then
            Button_Facturation_TP.Visible = False
            Button_Facturation_AMC.Visible = False
        End If
        If CurrentConvention.selected = True And CurrentConvention.type_Accord = "T" Then
            Button_Facturation_AMO.Visible = False
            Button_Facturation_AMC.Visible = True
            Button_Facturation_HTP.Visible = False
            Button_Facturation_TP.Visible = True
        End If
        If CurrentConvention.selected = True And CurrentConvention.type_Accord = "H" Then
            Button_Facturation_AMO.Visible = True
            Button_Facturation_HTP.Visible = True
            Button_Facturation_TP.Visible = False
            Button_Facturation_AMC.Visible = False
        End If
        If CurrentConvention.selected = True Then
            Convention_Label.Text = "CONVENTION: "
            If CurrentConvention.type_Accord = "T" Then
                Convention_Label.Text = Convention_Label.Text + "Tiers-Payant" + Chr(10)
            ElseIf CurrentConvention.type_Accord = "H" Then
                Convention_Label.Text = Convention_Label.Text + "Hors Tiers-Payant" + Chr(10)
            End If
            Convention_Label.Text = Convention_Label.Text + "org signataire: " + CurrentConvention.Libellé_org_signataire + Chr(10) + "org complémentaire: " + CurrentConvention.libellé_Org_complémentaire
        Else
            Convention_Label.Text = "PAS DE CONVENTION"
        End If
        If tab_current_formule_STS.Length > 1 Then
            Convention_Label.Text = Convention_Label.Text + Chr(10) + Chr(10) + "Formules AMC:"
            For i = 1 To tab_current_formule_STS.Length - 1
                Convention_Label.Text = Convention_Label.Text + Chr(10) + "Prestation n°" + i.ToString + " --> formule " + tab_current_formule_STS(i).Numéro + ", " + tab_current_formule_STS(i).Libellé
            Next
        ElseIf current_formule_STS.selected = True And tab_current_formule_STS.Length = 1 Then
            Convention_Label.Text = Convention_Label.Text + Chr(10) + " Formule AMC appliquée au niveau facture: " + current_formule_STS.Numéro + ", " + current_formule_STS.Libellé
        Else
            Convention_Label.Text = Convention_Label.Text + Chr(10) + "Pas de Formule AMC"
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        'CMU
        If COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False Then
            CurrentFactureVitale.TOP_CMU = False
        End If
        If CurrentFactureVitale.TOP_CMU = True Then
            Button_Facturation_AMO.Visible = False
            Button_Facturation_HTP.Visible = True
            Button_Facturation_TP.Visible = True
            Button_Facturation_AMC.Visible = False
            Convention_Label.Text = "CMU"
            'If CurrentFactureVitale.Mode_Gestion = "RO" Then
            '    Button_Facturation_AMO.Visible = False
            '    Button_Facturation_HTP.Visible = True
            '    Button_Facturation_TP.Visible = True
            '    Button_Facturation_AMC.Visible = False
            'Else
            '    Button_Facturation_AMO.Visible = True
            '    Button_Facturation_HTP.Visible = True
            '    Button_Facturation_TP.Visible = True
            '    Button_Facturation_AMC.Visible = False
            'End If
            If CurrentBenéficiaire.CMU__assimilation_mode_gestion_unique Is Nothing Or CurrentBenéficiaire.CMU__assimilation_mode_gestion_unique = False Then
                For r = 0 To COMPTOIR_MAIN.Vente.RowCount - 2
                    If UCase(COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, r).Value.ToString) <> "VENTE DIRECTE" Then
                        Dim TypeNomenclature As String
                        Dim codeacte As String = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCodeActe.Index, r).Value.ToString
                        TypeNomenclature = UCase(Me.CodePrestationsTableAdapter1.SelectTypeNomenclatureByCodeActe(codeacte))
                        If TypeNomenclature = "LPP" Or TypeNomenclature = "FRAIS PH" Then

                            If COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text <> "" And COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text <> "  /  /" Then
                                If Val(Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 7, 4) & Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 4, 2) & Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 1, 2)) _
                                    > Val(Format(COMPTOIR_MAIN.DateExecution.Value, "yyyyMMdd")) Then
                                    If Forçage_Date_CMU_Contenue_en_CV() = False Then
                                        Button_Facturation_TP.Visible = False
                                        COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
                                        Convention_Label.Text = Convention_Label.Text + Chr(10) + "prestation  hors période CMU (" + codeacte + ") TP IMPOSSIBLE"
                                    End If
                                    Exit For
                                End If
                            End If

                            If COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text <> "" And COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text <> "  /  /" Then
                                If Val(Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 7, 4) & Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 4, 2) & Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 1, 2)) _
                                  < Val(Format(COMPTOIR_MAIN.DateExecution.Value, "yyyyMMdd")) Then
                                    If Forçage_Date_CMU_Contenue_en_CV() = False Then
                                        Button_Facturation_TP.Visible = False
                                        COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
                                        Convention_Label.Text = Convention_Label.Text + Chr(10) + "prestation hors période CMU (" + codeacte + ") TP IMPOSSIBLE"
                                    End If
                                    Exit For
                                End If
                            End If

                        Else

                            If COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text <> "" And COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text <> "  /  /" Then
                                If Val(Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 7, 4) & Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 4, 2) & Mid(COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text, 1, 2)) _
                                    > Val(Format(COMPTOIR_MAIN.DatePrecription.Value, "yyyyMMdd")) Then
                                    If Forçage_Date_CMU_Contenue_en_CV() = False Then
                                        Button_Facturation_TP.Visible = False
                                        COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
                                        Convention_Label.Text = Convention_Label.Text + Chr(10) + "prestation  hors période CMU (" + codeacte + ") TP IMPOSSIBLE"
                                    End If
                                    Exit For
                                End If
                            End If

                            If COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text <> "" And COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text <> "  /  /" Then
                                If Val(Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 7, 4) & Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 4, 2) & Mid(COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text, 1, 2)) _
                                  < Val(Format(COMPTOIR_MAIN.DatePrecription.Value, "yyyyMMdd")) Then
                                    If Forçage_Date_CMU_Contenue_en_CV() = False Then
                                        Button_Facturation_TP.Visible = False
                                        COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
                                        Convention_Label.Text = Convention_Label.Text + Chr(10) + "prestation hors période CMU (" + codeacte + ") TP IMPOSSIBLE"
                                    End If
                                    Exit For
                                End If
                            End If

                        End If
                    End If
                Next
            End If

        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        If COMPTOIR_MAIN.NatureAssurance_Button.Text = "SMG" Then
            Button_Facturation_TP.Visible = False
        End If
        '------------------------------------------------------------------------------------------------------------------------------------------------------
        If CurrentBenéficiaire.G101C8 = Config.Data_ContaceptifUrgence_NIR Then
            Button_Facturation_AMO.Visible = True
            Button_Facturation_FSPapier.Visible = False
            Button_Facturation_HTP.Visible = False
        End If
        If (CurrentFactureVitale.TOP_CMU = True Or CurrentFactureVitale.Mode_Gestion = "AME_Base" Or CurrentFactureVitale.Mode_Gestion = "AME_Compl") And Button_Facturation_TP.Visible = True And Button_Facturation_TP.Enabled = True Then
            Button_Facturation_TP.Select()
            Button_Facturation_TP.BackColor = Color.Yellow
        Else
            Button_Annuler.Select()
            Button_Annuler.BackColor = Color.Yellow
        End If

        If COMPTOIR_MAIN.NatureAssurance_Button.Text = "ACCIDENT DU TRAVAIL" Then
            Button_Facturation_HTP.Visible = False
            Button_Facturation_AMO.Select()
            Button_Facturation_AMO.BackColor = Color.Yellow
        ElseIf COMPTOIR_MAIN.NatureAssurance_Button.Text = "SMG" Then
            Button_Facturation_AMO.Select()
            Button_Facturation_AMO.BackColor = Color.Yellow
        End If
        If CurrentFactureVitale.Mode_Gestion = "AME_Base" Or CurrentFactureVitale.Mode_Gestion = "AME_Compl" Then
            Button_Facturation_AMO.Visible = False
            Button_Facturation_HTP.Visible = False
            Button_Facturation_TP.Visible = True
            Button_Facturation_AMC.Visible = False
            Button_Facturation_TP.Select()
            Button_Facturation_TP.BackColor = Color.Yellow
        End If
        '        If (COMPTOIR_MAIN.MUT_NumMutuelle_TextBox.Text = "55555551" And COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False) Then
        If (CurrentFactureVitale.Sortant_CMU = True And COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False) Then
            Button_Facturation_AMO.Visible = True
            Button_Facturation_AMO.Select()
            Button_Facturation_AMO.BackColor = Color.Yellow
        End If
    End Sub
    Private Sub Button_Facturation_TP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_TP.Click
        CurrentFactureVitale.TP_AMO = True
        CurrentFactureVitale.TP_AMC = True
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = True
        Me.Dispose()
    End Sub

    Private Sub Button_Facturation_HTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_HTP.Click
        CurrentFactureVitale.TP_AMO = False
        CurrentFactureVitale.TP_AMC = False
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = True
        Me.Dispose()
    End Sub

    Private Sub Button_Facturation_FSPapier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_FSPapier.Click
        COMPTOIR_MAIN.FS_Papier = True
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = True
        Me.Dispose()
    End Sub

    Private Sub Button_Facturation_AMO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_AMO.Click
        CurrentFactureVitale.TP_AMO = True
        CurrentFactureVitale.TP_AMC = False
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = True
        Me.Dispose()
    End Sub

    Private Sub Button_Facturation_AMC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_AMC.Click
        CurrentFactureVitale.TP_AMO = False
        CurrentFactureVitale.TP_AMC = True
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = True
        Me.Dispose()
    End Sub

    Private Sub Accident_CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Accident_CheckBox.CheckedChanged
        If Accident_CheckBox.Checked = True Then
            Accident_DateTimePicker.Enabled = True
        Else
            Accident_DateTimePicker.Enabled = False
        End If
    End Sub

    Private Sub date_accident_droit_commun()
        If Accident_CheckBox.Checked = True Then
            CurrentFactureVitale.G1511C1 = Format(Accident_DateTimePicker.Value, "yyyyMMdd") + "0000"
        End If
    End Sub

    Private Sub Button_Annuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Annuler.Click
        COMPTOIR_MAIN.Choix_Facturation_TP_retour = False
        If CurrentBenéficiaire.G101C8 = Config.Data_ContaceptifUrgence_NIR Then
            'COMPTOIR_MAIN.ResetForm()
            'CurrentBenéficiaire.clear()
            CurrentBenéficiaire.clear()
            COMPTOIR_MAIN.Dispose()
            COMPTOIR_MAIN.Visible = True

        End If
        ReDim tab_current_formule_STS(0)
        current_formule_STS.Clear()
        CurrentConvention.clear()
        COMPTOIR_MAIN.Button_Facturation.Visible = True
        Me.Dispose()
    End Sub



    Private Sub Change_Couleur_in(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_TP.Enter, Button_Facturation_AMO.Enter, Button_Facturation_HTP.Enter, Button_Facturation_FSPapier.Enter, Button_Annuler.Enter, Button_Facturation_AMC.Enter
        sender.BackColor = Color.Yellow
        sender.Forecolor = Color.Black

    End Sub
    Private Sub Change_Couleur_out(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Facturation_TP.LostFocus, Button_Facturation_AMO.LostFocus, Button_Facturation_HTP.LostFocus, Button_Facturation_FSPapier.LostFocus, Button_Annuler.LostFocus, Button_Facturation_AMC.LostFocus
        sender.BackColor = Color.FromArgb(64, 64, 64)
        sender.Forecolor = Color.White
    End Sub

    Private Function Forçage_Date_CMU_Contenue_en_CV() As Boolean
        '3.2.2.3 Principes de forçage pour des données liées à un bénéficiaire de la CMUC, à un sortant de CMU-C
        MyMsg_Box.Presentation("Officine ?", "Les Droits CMU contenu en carte vitale sont fermés." + Chr(10) + "Voulez vous prolonger ces droits?", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
            CurrentFactureVitale.G1440C1 = CurrentFactureVitale.G1440C1 + "N"
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub Jeux_D_Essai_AutoClick()
        If Jeux_d_essai2.Visible = True Then
            If Jeux_d_essai2.TP_AMO_Label.Text = "True" And Jeux_d_essai2.TP_AMC_Label.Text = "True" Then
                Button_Facturation_TP.PerformClick()
            ElseIf Jeux_d_essai2.TP_AMO_Label.Text = "True" And Jeux_d_essai2.TP_AMC_Label.Text = "False" Then
                Button_Facturation_AMO.PerformClick()
            ElseIf Jeux_d_essai2.TP_AMO_Label.Text = "False" And Jeux_d_essai2.TP_AMC_Label.Text = "False" Then
                Button_Facturation_HTP.PerformClick()
            ElseIf Jeux_d_essai2.TP_AMO_Label.Text = "False" And Jeux_d_essai2.TP_AMC_Label.Text = "True" Then
                Button_Facturation_AMC.PerformClick()
            End If
        ElseIf jeux_d_essai_CAS_STANDARD.Visible = True Then
            Button_Facturation_AMO.PerformClick()
        End If
    End Sub

    Private Sub Choix_Facturation_TP_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Jeux_D_Essai_AutoClick()
    End Sub
End Class