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

    Private en_cours_affichage As Boolean

    Private Sub MAJ_Global()
         Bandeau_info_bas_show("Traitement en cours...")

        'Action1
        Dim factures = BDD.FacturesTableAdapter.GetData()
        For r = 0 To factures.Rows.Count - 1
            If Not factures(r).IsReprise_Facture_NumNull Then
                If factures(r).Reprise_Facture_Num > 0 Then
                    BDD.FacturesTableAdapter.Set_Recyclée(True, factures(r).Reprise_Facture_Num)
                End If
            End If
        Next
        'Action1 fin

        'Action2
        Dim rejets = BDD.RSP_LIGNE_REJETTableAdapter.GetData
        For r = 0 To rejets.Rows.Count - 1
            If Not rejets(r).IsAcquitementNull Then
                If rejets(r).Acquitement = True Then
                    BDD.FacturesTableAdapter.Set_Recyclée(True, CDec(rejets(r).NumFacture))
                End If
            End If
        Next
        'Action2 fin


        Dim factures_AMO = BDD.FacturesTableAdapter.GetDataBy_Part_AMO_payée(False, False)
        For r = 0 To factures_AMO.Rows.Count - 1

            Dim Num_Fact_strg = factures_AMO(r).Num.ToString
            While Num_Fact_strg.Length < 9
                Num_Fact_strg = "0" & Num_Fact_strg
            End While
            Dim paiement = BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMO")
            If paiement.Rows.Count > 0 Then
                If paiement(0).Montant_Demandé = paiement(0).Montant_Payé And paiement(0).Etat_Paiement = "P" Then
                    BDD.FacturesTableAdapter.Set_Part_AMO_payée(True, factures_AMO(r).Num)
                End If
            End If

        Next

        Dim factures_AMC = BDD.FacturesTableAdapter.GetDataBy_Part_AMC_payée(False, False)
        For r = 0 To factures_AMC.Rows.Count - 1
            Dim Num_Fact_strg = factures_AMC(r).Num.ToString
            While Num_Fact_strg.Length < 9
                Num_Fact_strg = "0" & Num_Fact_strg
            End While
            Dim paiement = BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMC")
            If paiement.Rows.Count > 0 Then
                If paiement(0).Montant_Demandé = paiement(0).Montant_Payé And paiement(0).Etat_Paiement = "P" Then
                    BDD.FacturesTableAdapter.Set_Part_AMC_payée(True, factures_AMC(r).Num)
                End If
            End If
        Next

        


        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)

    End Sub

    Private Sub affichage()
        en_cours_affichage = True
        Me.Enabled = False
        Bandeau_info_bas_show("Préparation de l'affichage...")

        Try
            CInt(Jours_Factures_TextBox1.Text).ToString()
        Catch ex As Exception
            Jours_Factures_TextBox1.Text = "14"
        End Try
        Dim datemax_fact = DateAdd(DateInterval.Day, (CDbl(Jours_Factures_TextBox1.Text) + 1) * (-1), Now)
        Dim datemax_fact_int = CInt(Format(datemax_fact, "yyyyMMdd"))
        Dim datemax_lots = DateAdd(DateInterval.Day, (CDbl(Jours_Lots_TextBox1.Text)) * (-1), Now)
        Dim datemax_lots_int = CInt(Format(datemax_lots, "yyyyMMdd"))
        Dim factures = BDD.FacturesTableAdapter.Factures_sans_paiement(datemax_fact_int, datemax_lots_int)
        Dim total_amo As Decimal = 0
        Dim total_amc As Decimal = 0

        DataGridView1.Rows.Clear()
        For r = 0 To factures.Rows.Count - 1
            DataGridView1.Rows.Add()
        Next

        For r = 0 To factures.Rows.Count - 1
            Bandeau_info_bas_change_text("Préparation de l'affichage..." & (r + 1).ToString & "/" & factures.Rows.Count.ToString)
            Application.DoEvents()

            DataGridView1.Item(DG_Date.Index, r).Value = CDate(Mid(factures(r).G1110C3, 7, 2) & "/" & Mid(factures(r).G1110C3, 5, 2) & "/" & Mid(factures(r).G1110C3, 1, 4))
            DataGridView1.Item(DG_Numfact.Index, r).Value = factures(r).Num
            DataGridView1.Item(DG_Lot.Index, r).Value = Val(factures(r).Num_Lot_FSE)

            If factures(r).TP_AMO = True Then
                Dim montant_amo As Decimal = 0
                If Mid(factures(r).G1321C5, 4, 1) = "5" Then
                    DataGridView1.Item(DG_TPAMO.Index, r).Value = "N C"
                Else
                    DataGridView1.Item(DG_TPAMO.Index, r).Value = "OUI"
                End If

                If factures(r).Part_AMO_payée = True Then
                    DataGridView1.Item(DG_Part_AMO_payée.Index, r).Value = "OUI"
                    DataGridView1.Rows(r).Cells(DG_Part_AMO_payée.Index).Style.BackColor = Color.LightGreen
                    DataGridView1.Rows(r).Cells(DG_TPAMO.Index).Style.BackColor = Color.LightGreen
                Else
                    DataGridView1.Item(DG_Part_AMO_payée.Index, r).Value = "aucun retour"
                    DataGridView1.Rows(r).Cells(DG_Part_AMO_payée.Index).Style.BackColor = Color.Yellow
                    DataGridView1.Rows(r).Cells(DG_TPAMO.Index).Style.BackColor = Color.Yellow
                    Dim Num_Fact_strg = factures(r).Num.ToString
                    montant_amo = CDec(factures(r).G1910C3)
                    While Num_Fact_strg.Length < 9
                        Num_Fact_strg = "0" & Num_Fact_strg
                    End While
                    Dim paiement = BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMO")
                    If paiement.Rows.Count > 0 Then
                        DataGridView1.Item(DG_Part_AMO_payée.Index, r).Value = "demandé " & (CDec(paiement(0).Montant_Demandé) / 100).ToString & " payé " & (CDec(paiement(0).Montant_Payé) / 100).ToString
                        DataGridView1.Rows(r).Cells(DG_Part_AMO_payée.Index).Style.BackColor = Color.Orange
                        DataGridView1.Rows(r).Cells(DG_TPAMO.Index).Style.BackColor = Color.Orange
                        'montant_amo = CDec(paiement(0).Montant_Demandé) - CDec(paiement(0).Montant_Payé)
                    End If
                    Dim rejet = BDD.RSP_LIGNE_REJETTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMO")
                    If rejet.Rows.Count > 0 Then
                        DataGridView1.Item(DG_Part_AMO_payée.Index, r).Value = "demandé " & (CDec(factures(r).G1910C3) / 100).ToString & " rejeté"
                        DataGridView1.Rows(r).Cells(DG_Part_AMO_payée.Index).Style.BackColor = Color.Red
                        DataGridView1.Rows(r).Cells(DG_TPAMO.Index).Style.BackColor = Color.Red
                        montant_amo = CDec(factures(r).G1910C3)
                    End If
                End If
                total_amo = total_amo + montant_amo
            Else
                DataGridView1.Item(DG_TPAMO.Index, r).Value = "NON"
                DataGridView1.Rows(r).Cells(DG_Part_AMO_payée.Index).Style.BackColor = Color.LightGreen
                DataGridView1.Rows(r).Cells(DG_TPAMO.Index).Style.BackColor = Color.LightGreen
            End If

            If factures(r).TP_AMC = True Then
                Dim montant_amc As Decimal = 0
                DataGridView1.Item(DG_TPAMC.Index, r).Value = "OUI"
                If factures(r).Part_AMC_payée = True Then
                    DataGridView1.Item(DG_Part_AMC_payée.Index, r).Value = "OUI"
                    DataGridView1.Rows(r).Cells(DG_Part_AMC_payée.Index).Style.BackColor = Color.LightGreen
                    DataGridView1.Rows(r).Cells(DG_TPAMC.Index).Style.BackColor = Color.LightGreen
                Else
                    DataGridView1.Item(DG_Part_AMC_payée.Index, r).Value = "aucun retour"
                    DataGridView1.Rows(r).Cells(DG_Part_AMC_payée.Index).Style.BackColor = Color.Yellow
                    DataGridView1.Rows(r).Cells(DG_TPAMC.Index).Style.BackColor = Color.Yellow
                    Dim Num_Fact_strg = factures(r).Num.ToString
                    montant_amc = CDec(factures(r).G1910C3)
                    While Num_Fact_strg.Length < 9
                        Num_Fact_strg = "0" & Num_Fact_strg
                    End While
                    Dim paiement = BDD.RSP_PaiementsTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMC")
                    If paiement.Rows.Count > 0 Then
                        DataGridView1.Item(DG_Part_AMC_payée.Index, r).Value = "demandé " & (CDec(paiement(0).Montant_Demandé) / 100).ToString & " payé " & (CDec(paiement(0).Montant_Payé) / 100).ToString
                        DataGridView1.Rows(r).Cells(DG_Part_AMC_payée.Index).Style.BackColor = Color.Orange
                        DataGridView1.Rows(r).Cells(DG_TPAMC.Index).Style.BackColor = Color.Orange
                        'montant_amc = CDec(paiement(0).Montant_Demandé) - CDec(paiement(0).Montant_Payé)
                    End If
                    Dim rejet = BDD.RSP_LIGNE_REJETTableAdapter.GetDataBy_NumFacture_Part(Num_Fact_strg, "AMC")
                    If rejet.Rows.Count > 0 Then
                        DataGridView1.Item(DG_Part_AMC_payée.Index, r).Value = "demandé " & (CDec(factures(r).G1910C3) / 100).ToString & " rejeté"
                        DataGridView1.Rows(r).Cells(DG_Part_AMC_payée.Index).Style.BackColor = Color.Red
                        DataGridView1.Rows(r).Cells(DG_TPAMC.Index).Style.BackColor = Color.Red
                        montant_amc = CDec(factures(r).G1910C3)
                    End If
                End If
                total_amc = total_amc + montant_amc
            Else
                DataGridView1.Item(DG_TPAMC.Index, r).Value = "NON"
                DataGridView1.Rows(r).Cells(DG_Part_AMC_payée.Index).Style.BackColor = Color.LightGreen
                DataGridView1.Rows(r).Cells(DG_TPAMC.Index).Style.BackColor = Color.LightGreen
            End If
            DataGridView1.Item(DG_Set_Part_AMO_payée.Index, r).Value = "Acquit AMO"
            DataGridView1.Item(DG_Set_Part_AMC_payée.Index, r).Value = "Acquit AMC"
        Next
        RichTextBox1.Clear()
        RichTextBox1.AppendText("attente AMO: " & (total_amo / 100).ToString & "€   attente AMC: " & (total_amc / 100).ToString & "€   attente totale: " & ((total_amo + total_amc) / 100).ToString & "€")
        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
        Me.Enabled = True
        en_cours_affichage = False
        Me.BringToFront()
    End Sub

  

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Select Case e.ColumnIndex
            Case DG_Set_Part_AMO_payée.Index
                MyMsg_Box.Presentation("Officine ?", "Voulez-vous acquitter l'attente de paiement part AMO pour la facture " & DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value.ToString & " ?" & vbCrLf & vbCrLf & "Le logiciel n'affichera plus cette part en attente de paiement.", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    BDD.FacturesTableAdapter.Set_Part_AMO_payée(True, CDec(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value))
                    affichage()
                End If
            Case DG_Set_Part_AMC_payée.Index
                MyMsg_Box.Presentation("Officine ?", "Voulez-vous acquitter l'attente de paiement part AMC pour la facture " & DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value.ToString & " ?" & vbCrLf & vbCrLf & "Le logiciel n'affichera plus cette part en attente de paiement.", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    BDD.FacturesTableAdapter.Set_Part_AMC_payée(True, CDec(DataGridView1.Item(DG_Numfact.Index, e.RowIndex).Value))
                    affichage()
                End If
        End Select
    End Sub

   

 

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        If en_cours_affichage = True Then Exit Sub
        Dim parameters As String = "&Num=" & DataGridView1.Item(DG_Numfact.Index, DataGridView1.CurrentRow.Index).Value
        WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fFactures%2fFacture+Sesam-Vitale&rs:Command=Render" & parameters)
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

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

    Private Sub DataGridView1_CellContextMenuStripChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContextMenuStripChanged

    End Sub
End Class