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

    Private Sub Rejets_Paiements_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Bandeau_info_bas_show("Récuperation liste rejets...")



        DataGridView_Rejets_Paiements.Rows.Clear()
        Dim mydatatable = Teletransmission_MAIN.RSP_LIGNE_REJETTableAdapter.GetDataBy_Acquitement(False, 0)
        If mydatatable.Rows.Count > 0 Then
            DataGridView_Rejets_Paiements.Columns.Item(0).DefaultCellStyle.Format = "dd/MM/yyyy"
            For r = 0 To mydatatable.Rows.Count - 1
                DataGridView_Rejets_Paiements.Rows.Add()
                DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Num_facture.Index, r).Value = Trim(mydatatable(r).NumFacture)
                DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Libellé.Index, r).Value = Trim(mydatatable(r).LIBELLE)
                DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Part_Concernée.Index, r).Value = Trim(mydatatable(r).PART_CONCERNE)
                Try
                    Dim facture = BDD.FacturesTableAdapter.GetFactureDataByNum1(CDec(Trim(mydatatable(r).NumFacture))).Item(0)
                    DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Bénéficiaire.Index, r).Value = facture.Bénéficiaire
                Catch ex As Exception

                End Try
               
                Dim paiement = Teletransmission_MAIN.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_Part(Trim(mydatatable(r).NumFacture), Trim(mydatatable(r).PART_CONCERNE))
                If paiement.Rows.Count > 0 Then
                    DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Paiement.Index, r).Value = CDec(paiement.Item(0).Montant_Payé) / 100
                Else
                    DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Paiement.Index, r).Value = CDec("0")
                End If
                Try
                    DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Date_soins.Index, r).Value = CDate(Mid(Trim(mydatatable(r).DATE_DES_SOINS), 5, 2) & "/" & Mid(Trim(mydatatable(r).DATE_DES_SOINS), 3, 2) & "/20" & Mid(Trim(mydatatable(r).DATE_DES_SOINS), 1, 2))
                    If InStr(mydatatable(r).INFORMATION_COMPLEMENTAIRE, "Montant facturé R") = 1 Then
                        DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Info_Compl.Index, r).Value = CDec(Mid(Trim(mydatatable(r).INFORMATION_COMPLEMENTAIRE), 20)) / 100
                    Else
                        DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Info_Compl.Index, r).Value = Trim(mydatatable(r).INFORMATION_COMPLEMENTAIRE)
                    End If
                    If DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Paiement.Index, r).Value <> 0 Then
                        If DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Info_Compl.Index, r).Value = DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Paiement.Index, r).Value Then
                            DataGridView_Rejets_Paiements.Rows.Item(r).DefaultCellStyle.BackColor = Color.LightGreen
                        Else
                            DataGridView_Rejets_Paiements.Rows.Item(r).DefaultCellStyle.BackColor = Color.Orange
                        End If
                    End If
                Catch ex As Exception

                End Try
            Next
        End If


        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Teletransmission_MAIN.Affiche_Rejets_Paiement(Me.DataGridView_Rejets_Paiements)
    End Sub



    Private Sub DataGridView_Rejets_Paiements_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView_Rejets_Paiements.CellContentClick
        If e.RowIndex < 0 Then Exit Sub
        If e.ColumnIndex = Rejets_Paiement_Traitement.Index Then
            Me.Visible = False
            Dim facture = BDD.FacturesTableAdapter.GetFactureDataByNum1(CDec(DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Num_facture.Index, DataGridView_Rejets_Paiements.CurrentRow.Index).Value)).Item(0)
            COMPTOIR_MAIN.Chargement_CurrentBenéficiaire_degradé(facture.G1130C2, facture.G1130C4, facture.G1130C5, facture.G1130C6)

            If COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Visible = False Then

                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()

        ElseIf e.ColumnIndex = Rejets_Paiement_Acquittement.Index Then
            MyMsg_Box.Presentation("Officine ?", "Acquitter le rejet ?" & vbCrLf & vbCrLf & "Ce rejet n'apparaitra plus dans la liste des rejets.", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                Teletransmission_MAIN.RSP_LIGNE_REJETTableAdapter.Update_Acquitement(True, DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Num_facture.Index, DataGridView_Rejets_Paiements.CurrentRow.Index).Value, DataGridView_Rejets_Paiements.Item(Rejets_Paiement_Part_Concernée.Index, DataGridView_Rejets_Paiements.CurrentRow.Index).Value)
                DataGridView_Rejets_Paiements.Rows.RemoveAt(DataGridView_Rejets_Paiements.CurrentRow.Index)
            End If

        End If

    End Sub

    Private Sub DataGridView_Rejets_Paiements_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView_Rejets_Paiements.CellPainting
        If e.ColumnIndex = Rejets_Paiement_Traitement.Index And e.RowIndex <> -1 Then
            e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
            e.Graphics.DrawImage(officine.My.Resources.magnifying_glass2, e.CellBounds)
            e.Handled = True
        ElseIf e.ColumnIndex = Rejets_Paiement_Acquittement.Index And e.RowIndex <> -1 Then
            e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
            e.Graphics.DrawImage(officine.My.Resources.QUITTER2, e.CellBounds)
            e.Handled = True
        End If

    End Sub
End Class