' 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/>.
Public Class Controle_Retours


    Private Sub affichage()
        Me.Enabled = False
         Bandeau_info_bas_show("Préparation de l'affichage...")
        Try
            CInt(Jours_Lots_TextBox1.Text).ToString()
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine !", "Nombre de jours incorrect", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
            Exit Sub
        End Try
        Dim datemax_lots = DateAdd(DateInterval.Day, (CDbl(Jours_Lots_TextBox1.Text)) * (-1), Now)
        Dim datemax_lots_string = CInt(Format(datemax_lots, "yyyyMMdd")).ToString & "0000"
        Dim Factures_Sans_Retour_AMO = BDD.FacturesTableAdapter.Factures_Sans_Retour_AMO(datemax_lots_string)
        Dim Factures_Sans_Retour_AMC = BDD.FacturesTableAdapter.Factures_Sans_Retour_AMC(datemax_lots_string)
        DataGridView1.Rows.Clear()
        For r = 0 To Factures_Sans_Retour_AMC.Rows.Count - 1
            DataGridView1.Rows.Add()
        Next
        For r = 0 To Factures_Sans_Retour_AMC.Rows.Count - 1
            Try

          
            DataGridView1.Item(DG_Numfact.Index, r).Value = Factures_Sans_Retour_AMC(r).Num
            DataGridView1.Item(DG_Date.Index, r).Value = CDate(Mid(Factures_Sans_Retour_AMC(r).G1110C3, 7, 2) & "/" & Mid(Factures_Sans_Retour_AMC(r).G1110C3, 5, 2) & "/" & Mid(Factures_Sans_Retour_AMC(r).G1110C3, 1, 4)).ToShortDateString
            DataGridView1.Item(DG_Lot.Index, r).Value = Val(Factures_Sans_Retour_AMC(r).Num_Lot_FSE)
            DataGridView1.Item(DG_Date_Lot.Index, r).Value = CDate(Mid(Factures_Sans_Retour_AMC(r).Date_Lot_FSE, 7, 2) & "/" & Mid(Factures_Sans_Retour_AMC(r).Date_Lot_FSE, 5, 2) & "/" & Mid(Factures_Sans_Retour_AMC(r).Date_Lot_FSE, 1, 4)).ToShortDateString
                DataGridView1.Rows(r).Cells(DG_Retour_AMC.Index).Value = "Non"
            DataGridView1.Rows(r).Cells(DG_Béneficiaire.Index).Value = Factures_Sans_Retour_AMC(r).Bénéficiaire
            DataGridView1.Rows(r).Cells(DG_ouvrir_dossier_patient.Index).Value = "dossier patient"
                Application.DoEvents()
            Catch ex As Exception
            End Try
        Next
        For r = 0 To Factures_Sans_Retour_AMO.Rows.Count - 1
            Dim found As Boolean = False
            Dim Dtgrdvw_RwIndx As Integer
            Try



                'For rr = 0 To Dtgrdvw_RwIndx
                For rr = 0 To DataGridView1.Rows.Count - 1
                    If Val(DataGridView1.Item(DG_Numfact.Index, rr).Value) = Val(Factures_Sans_Retour_AMO(r).Num) Then
                        found = True
                        DataGridView1.Rows(rr).Cells(DG_Retour_AMO.Index).Value = "Non"
                        Exit For
                    End If
                Next
                If found = False Then
                    DataGridView1.Rows.Add()
                    Dtgrdvw_RwIndx = DataGridView1.Rows.Count - 1
                    DataGridView1.Item(DG_Numfact.Index, Dtgrdvw_RwIndx).Value = Factures_Sans_Retour_AMO(r).Num
                    DataGridView1.Item(DG_Date.Index, Dtgrdvw_RwIndx).Value = CDate(Mid(Factures_Sans_Retour_AMO(r).G1110C3, 7, 2) & "/" & Mid(Factures_Sans_Retour_AMO(r).G1110C3, 5, 2) & "/" & Mid(Factures_Sans_Retour_AMO(r).G1110C3, 1, 4)).ToShortDateString
                    DataGridView1.Item(DG_Lot.Index, Dtgrdvw_RwIndx).Value = Val(Factures_Sans_Retour_AMO(r).Num_Lot_FSE)
                    DataGridView1.Item(DG_Date_Lot.Index, r).Value = CDate(Mid(Factures_Sans_Retour_AMO(r).Date_Lot_FSE, 7, 2) & "/" & Mid(Factures_Sans_Retour_AMO(r).Date_Lot_FSE, 5, 2) & "/" & Mid(Factures_Sans_Retour_AMO(r).Date_Lot_FSE, 1, 4)).ToShortDateString
                    DataGridView1.Item(DG_Retour_AMO.Index, Dtgrdvw_RwIndx).Value = "Non"
                    DataGridView1.Item(DG_Béneficiaire.Index, Dtgrdvw_RwIndx).Value = Factures_Sans_Retour_AMO(r).Bénéficiaire
                    DataGridView1.Item(DG_ouvrir_dossier_patient.Index, Dtgrdvw_RwIndx).Value = "dossier patient"
                End If
                Application.DoEvents()
            Catch ex As Exception
      
            End Try
        Next
        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
        Me.Enabled = True
        Me.BringToFront()
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = DG_ouvrir_dossier_patient.Index Then
            Me.Visible = False
             Bandeau_info_bas_show("Ouverture dossier patient...")
            COMPTOIR_MAIN.LECTURE_VITALE.BackgroundImage = officine.My.Resources.carte_vitale
            DP_Carte_Vitale_lue = False
            If Not COMPTOIR_MAIN.DP_list_Alrt_Prdt_deja_diffusées Is Nothing Then
                COMPTOIR_MAIN.DP_list_Alrt_Prdt_deja_diffusées.Clear()
            End If
            COMPTOIR_MAIN.DP_affichage_statut("Statut DP inconnu")

            ReDim tab_current_formule_STS(0)
            current_formule_STS.Clear()
            CurrentConvention.clear()

            SGD_LibererSession(NumeroSessionVitale)
            CurrentBenéficiaire.clear()
            COMPTOIR_MAIN.CurrentBeneficiaireRichTextBox.Clear()
            ClearGroupBox(COMPTOIR_MAIN.AssuréGroupBox, True)
            ClearTabPage(COMPTOIR_MAIN.Dossier_Patient_AMC_TabPage, False)
            COMPTOIR_MAIN.PrescripteurMyCombobox.Text = ""
            COMPTOIR_MAIN.TextBox_Prescripteur_NumID.Clear()
            COMPTOIR_MAIN.TextBox_Prescripteur_NumRPPS.Clear()
            COMPTOIR_MAIN.Prescripteur_Spécialité_TextBox.Clear()
            COMPTOIR_MAIN.Médecin_Traitant_Label.Text = ""
            'Choix_Bénéficiaire.ShowDialog() ' = True
            COMPTOIR_MAIN.DatePrecription.Enabled = True
            COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = 0
            '''''''''''''''
            Dim facture = BDD.FacturesTableAdapter.GetFactureDataByNum1(CDec(DataGridView1.Item(DG_Numfact.Index, DataGridView1.CurrentRow.Index).Value)).Item(0)

            CurrentBenéficiaire.G101C8 = facture.G1130C2
            CurrentBenéficiaire.G104C12 = facture.G1130C4
            CurrentBenéficiaire.G104C13 = facture.G1130C5
            CurrentBenéficiaire.G104C14 = facture.G1130C6
            COMPTOIR_MAIN.GetDataBénéficiaire()
            COMPTOIR_MAIN.ModeSecur = "5"
            'COMPTOIR_MAIN.CheckBoxDRE.Enabled = True
            'COMPTOIR_MAIN.CheckBoxFSE.Enabled = True
            COMPTOIR_MAIN.CheckBoxModeDegradé.Enabled = True
            COMPTOIR_MAIN.CheckBoxModeDegradé.Checked = True
            COMPTOIR_MAIN.ventetype_defaut = "assurance"
            'COMPTOIR_MAIN.Détail_Bénéficiaire_Button.Text = Choix_BénéficiaireDataGridView.Item(Nom.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value + " " + Choix_BénéficiaireDataGridView.Item(Prénom.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value
            COMPTOIR_MAIN.afficheDonnéesAssuré()
            'If CurrentBenéficiaire.AMC_Piecejustificative <> "4" Then
            '    COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = Val(CurrentBenéficiaire.AMC_Piecejustificative)
            'Else
            'COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = 0
            'End If
            'COMPTOIR_MAIN.Assuré_détail_Button.Visible = True
            COMPTOIR_MAIN.DP_form = New DP
            COMPTOIR_MAIN.DP_form.Affichage_histo_local()
            COMPTOIR_MAIN.Affiche_dus()
            COMPTOIR_MAIN.affiche_Avances(False)
            COMPTOIR_MAIN.Dossier_Patient_Load()
            If COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Visible = False Then
                'COMPTOIR_MAIN.Vente.Visible = False
                'COMPTOIR_MAIN.Panel_Prescripteur.Visible = False
                'COMPTOIR_MAIN.Panel_Facturation.Visible = False
                COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Visible = True
                COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.BringToFront()
                COMPTOIR_MAIN.Dossier_Patient_LinkLabel.Text = "Fermer Dossier patient"
                COMPTOIR_MAIN.Dossier_Patient_TabControl.SelectedTab = COMPTOIR_MAIN.Dossier_Patient_Historique_TabPage
            End If
            Me.Close()
            Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
        End If

    End Sub

   

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        affichage()
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = DG_Retour_AMC.Index Then
            If DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = "Non" Then
                MyMsg_Box.Presentation("Officine ?", "Acquitter tiers payant AMC?" & vbCrLf & vbCrLf & "Le logiciel ne signalera plus l'absence de retour.", True, "Oui", True, "Non", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    If BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_NumLot_Part(CInt(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value), DataGridView1.Item(DG_Lot.Index, e.RowIndex).Value, "AMC").Rows.Count = 0 Then
                        Dim NumFacture = DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value.ToString
                        While NumFacture.Length < 9
                            NumFacture = "0" & NumFacture
                        End While
                        BDD.RSP_PaiementsTableAdapter.Insert(Format(Now, "yyMMdd"), "Règlement RC acquitté manu", current_User.ID, "", "AMC", "0", "P", "0", NumFacture, DataGridView1.Item(DG_Lot.Index, e.RowIndex).Value, "", "", "")
                        BDD.FacturesTableAdapter.Set_TP_AMC(False, CDec(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value))
                        affichage()
                    End If
                End If
            End If
        ElseIf e.ColumnIndex = DG_Retour_AMO.Index Then
            If DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = "Non" Then
                MyMsg_Box.Presentation("Officine ?", "Acquitter tiers payant AMO?" & vbCrLf & vbCrLf & "Le logiciel ne signalera plus l'absence de retour.", True, "Oui", True, "Non", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    If BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_NumLot_Part(CInt(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value), DataGridView1.Item(DG_Lot.Index, e.RowIndex).Value, "AMO").Rows.Count = 0 Then
                        Dim NumFacture = DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value.ToString
                        While NumFacture.Length < 9
                            NumFacture = "0" & NumFacture
                        End While
                        BDD.RSP_PaiementsTableAdapter.Insert(Format(Now, "yyMMdd"), "Règlement RO acquitté manu", current_User.ID, "", "AMO", "0", "P", "0", NumFacture, DataGridView1.Item(DG_Lot.Index, e.RowIndex).Value, "", "", "")
                        BDD.FacturesTableAdapter.Set_TP_AMO(False, CDec(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value))
                        affichage()
                    End If
                End If
            End If
        End If
    End Sub
End Class