' 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.Xml
Public Class Sécuriser_Facture
    Private Sub Afficher_FSE_à_sécuriser()
        DataGridView_FSE.Rows.Clear()
        Dim myfile As String
        myfile = Dir("C:\Factures\Desync\*.fse")
        Dim nombrefichier As Integer = 0
        While myfile <> ""
            Dim num As String
            num = Mid(myfile, InStr(myfile, "@") + 1, 9)
            Dim mydatatable As DataTable
            mydatatable = Me.FacturesTableAdapter1.GetFactureDataByNum1(Val(num))
            Select Case mydatatable.Rows.Count
                Case 0
                    MsgBox("Il existe un fichier pour la facture numéro: " + num + Chr(10) + "Il n'y a acune inforùation sur cette facture dans la BDD", MsgBoxStyle.Critical)
                Case Is > 1
                    MsgBox("Il existe plusieurs enregistrement pour la facture numéro: " + num + " dans la BDD", MsgBoxStyle.Critical)
                Case Else
                    DataGridView_FSE.Rows.Add()
                    Dim NumFacturation As String = ""
                    DataGridView_FSE.Item(DataGridView_FSE_Num.Index, nombrefichier).Value = num
                    DataGridView_FSE.Item(DataGridView_FSE_Fichier.Index, nombrefichier).Value = myfile
                    For c = 0 To mydatatable.Columns.Count - 1
                        Select Case mydatatable.Columns.Item(c).ColumnName
                            Case "Bénéficiaire"
                                DataGridView_FSE.Item(DataGridView_FSE_Bénéficiaire.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "Assuré"
                                DataGridView_FSE.Item(DataGridView_FSE_Assuré.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "CRLdeFSE"
                                DataGridView_FSE.Item(DataGridView_FSE_CRL.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "G1910C2"
                                DataGridView_FSE.Item(DataGridView_FSE_G1910C2.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "G1120C1"
                                NumFacturation = mydatatable(0).Item(c) + NumFacturation
                            Case "G1120C2"
                                NumFacturation = NumFacturation + mydatatable(0).Item(c)
                        End Select
                    Next
                    DataGridView_FSE.Item(DataGridView_FSE_NumFacturation.Index, nombrefichier).Value = NumFacturation
            End Select
            nombrefichier = nombrefichier + 1
            myfile = Dir()
        End While
    End Sub
    Private Sub Afficher_DRE_à_sécuriser()
        DataGridView_DRE.Rows.Clear()
        Dim myfile As String
        myfile = Dir("C:\Factures\Desync\*.dre")
        Dim nombrefichier As Integer = 0
        While myfile <> ""
            Dim num As String
            num = Mid(myfile, InStr(myfile, "@") + 1, 9)
            Dim mydatatable As DataTable
            mydatatable = Me.FacturesTableAdapter1.GetFactureDataByNum1(Val(num))
            Select Case mydatatable.Rows.Count
                Case 0
                    MsgBox("Il existe un fichier pour la DRE associée à la facture numéro: " + num + Chr(10) + "Il n'y a acune information sur cette facture dans la BDD", MsgBoxStyle.Critical)
                Case Else
                    DataGridView_DRE.Rows.Add()
                    Dim NumFacturation As String = ""
                    DataGridView_DRE.Item(DataGridView_DRE_Num.Index, nombrefichier).Value = num
                    DataGridView_DRE.Item(DataGridView_DRE_Fichier.Index, nombrefichier).Value = myfile
                    For c = 0 To mydatatable.Columns.Count - 1
                        Select Case mydatatable.Columns.Item(c).ColumnName
                            Case "Bénéficiaire"
                                DataGridView_DRE.Item(DataGridView_DRE_Bénéficiaire.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "Assuré"
                                DataGridView_DRE.Item(DataGridView_DRE_Assuré.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "CRLdeDRE"
                                DataGridView_DRE.Item(DataGridView_DRE_CRL.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "G1910C2"
                                DataGridView_DRE.Item(DataGridView_DRE_Total.Index, nombrefichier).Value = mydatatable(0).Item(c)
                            Case "G1120C1"
                                NumFacturation = mydatatable(0).Item(c) + NumFacturation
                            Case "G1120C2"
                                NumFacturation = NumFacturation + mydatatable(0).Item(c)
                        End Select
                    Next
                    DataGridView_DRE.Item(DataGridView_DRE_NumFacturation.Index, nombrefichier).Value = NumFacturation
            End Select
            nombrefichier = nombrefichier + 1
            myfile = Dir()
        End While
    End Sub
    Private Sub Sécuriser_Facture_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Afficher_FSE_à_sécuriser()
        Afficher_DRE_à_sécuriser()
    End Sub


    Private Sub DataGridView_FSE_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView_FSE.CellContentClick
        Select Case DataGridView_FSE.CurrentCellAddress.X

            Case DataGridView_FSE_SECURISER.Index
                '____________________________________________________________________________________________________
                'Trace
                Trace_Reset_compteur()
                FichierTrace = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\")) + "traces\Sécuriser_FSE_in.txt"
                '____________________________________________________________________________________________________
                Dim erreur As Integer
                SGD_LibererSession(NumeroSessionCPS)
                SGD_ReserverSession(0, NumeroSessionCPS)
                erreur = SIS_LireCartePS(NomRessourcePS, NomRessourceLecteur, "", NumeroSessionCPS)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub
                Dim NologSituation As String = "@"
                TailleMax = 1
                SGD_LireChamp(NumeroSessionCPS, 2, Occurence_Grp2, 1, NologSituation, TailleMax)
                SGD_LibererSession(NumeroSessionFacture)
                SGD_ReserverSession(10000, NumeroSessionFacture)
                SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 151, Visu_API, FichierTrace)
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, _
                Mid(DataGridView_FSE.Item(DataGridView_FSE_Fichier.Index, DataGridView_FSE.CurrentCellAddress.Y).Value, 1, InStr(DataGridView_FSE.Item(DataGridView_FSE_Fichier.Index, DataGridView_FSE.CurrentCellAddress.Y).Value, "@") - 1) _
                , Visu_API, FichierTrace)
                SGD_AjouterChampFichier(NumeroSessionFacture, "C:\Factures\Desync\" + DataGridView_FSE.Item(DataGridView_FSE_Fichier.Index, DataGridView_FSE.CurrentCellAddress.Y).Value)


                'MsgBox(NomRessourcePS)
                'MsgBox(NomRessourceLecteur)
                'MsgBox(NologSituation)
                'MsgBox(DataGridView_FSE.Item(DataGridView_FSE_NumFacturation.Index, DataGridView_FSE.CurrentCellAddress.Y).Value)
                'MsgBox(NumeroSessionFacture.ToString)

                Dim NumFacturation As String = DataGridView_FSE.Item(DataGridView_FSE_NumFacturation.Index, DataGridView_FSE.CurrentCellAddress.Y).Value.ToString
                erreur = SIS_SecuriserFacture(NomRessourcePS, NomRessourceLecteur, "", NologSituation, NumFacturation, NumeroSessionFacture)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub


                'sauvegarde FSE
                Dim ConfigXml As New XmlDocument
                Dim ConfigXMLelementliste As XmlNodeList
                ConfigXml.Load("config.xml")
                ConfigXMLelementliste = ConfigXml.GetElementsByTagName("Idposte")
                Dim idposte = ConfigXMLelementliste.Item(0).InnerText
                ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
                TailleMax = 40
                SGD_LireChamp(NumeroSessionFacture, 150, 1, 1, ValeurChamp, TailleMax) 'ValeurChamp=CRL de FSE
                Me.FacturesTableAdapter1.UpdateCRLdeFSE(ValeurChamp, Val(DataGridView_FSE.Item(DataGridView_FSE_Num.Index, DataGridView_FSE.CurrentCellAddress.Y).Value))
                Dim fichierFSE As String = "c:\factures\" + ValeurChamp + "@" + DataGridView_FSE.Item(DataGridView_FSE_Num.Index, DataGridView_FSE.CurrentCellAddress.Y).Value + "@" + idposte + "@" + Format(Now, "yyyyMMddhhmmss") + ".fse"
                erreur = SGD_LireChampFichier(NumeroSessionFacture, 150, 1, 2, fichierFSE)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub
                My.Computer.FileSystem.DeleteFile("C:\Factures\Desync\" + DataGridView_FSE.Item(DataGridView_FSE_Fichier.Index, DataGridView_FSE.CurrentCellAddress.Y).Value)
                '____________________________________________________________________________________________________
                'Visu_API agrement CNDA
                If Visu_API = True Then
                    Technique_MAIN.LireSession_Out_VersFichier2(NumeroSessionFacture, Application.StartupPath + "\traces\Sécuriser_FSE_out.txt")
                End If
                '____________________________________________________________________________________________________
    
                Afficher_FSE_à_sécuriser()
        End Select

    End Sub

    Private Sub DataGridView_DRE_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView_DRE.CellContentClick
        Select Case DataGridView_DRE.CurrentCellAddress.X

            Case DataGridView_DRE_SECURISER.Index
                '____________________________________________________________________________________________________
                'Trace
                Trace_Reset_compteur()
                FichierTrace = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\")) + "traces\Sécuriser_DRE_in.txt"
                '____________________________________________________________________________________________________
                Dim erreur As Integer
                SGD_LibererSession(NumeroSessionCPS)
                SGD_ReserverSession(0, NumeroSessionCPS)
                erreur = SIS_LireCartePS(NomRessourcePS, NomRessourceLecteur, "", NumeroSessionCPS)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub
                Dim NologSituation As String = "@"
                TailleMax = 1
                SGD_LireChamp(NumeroSessionCPS, 2, Occurence_Grp2, 1, NologSituation, TailleMax)
                SGD_LibererSession(NumeroSessionFacture)
                SGD_ReserverSession(10000, NumeroSessionFacture)
                SGD_AjouterGroupe_Tracé(NumeroSessionFacture, 161, Visu_API, FichierTrace)
                SGD_AjouterChamp_Tracé(NumeroSessionFacture, _
                 Mid(DataGridView_DRE.Item(DataGridView_DRE_Fichier.Index, DataGridView_DRE.CurrentCellAddress.Y).Value, 1, InStr(DataGridView_DRE.Item(DataGridView_DRE_Fichier.Index, DataGridView_DRE.CurrentCellAddress.Y).Value, "@") - 1) _
                , Visu_API, FichierTrace)
                SGD_AjouterChampFichier(NumeroSessionFacture, "C:\Factures\Desync\" + DataGridView_DRE.Item(DataGridView_DRE_Fichier.Index, DataGridView_DRE.CurrentCellAddress.Y).Value)
                erreur = SIS_SecuriserFacture(NomRessourcePS, NomRessourceLecteur, "", NologSituation, DataGridView_DRE.Item(DataGridView_DRE_NumFacturation.Index, DataGridView_DRE.CurrentCellAddress.Y).Value, NumeroSessionFacture)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub


                'sauvegarde DRE
                Dim ConfigXml As New XmlDocument
                Dim ConfigXMLelementliste As XmlNodeList
                ConfigXml.Load("config.xml")
                ConfigXMLelementliste = ConfigXml.GetElementsByTagName("Idposte")
                Dim idposte = ConfigXMLelementliste.Item(0).InnerText
                ValeurChamp = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
                TailleMax = 71
                SGD_LireChamp(NumeroSessionFacture, 160, 1, 1, ValeurChamp, TailleMax) 'ValeurChamp=CRL de DRE
                Me.FacturesTableAdapter1.UpdateCRLdeDRE(ValeurChamp, Val(DataGridView_DRE.Item(DataGridView_DRE_Num.Index, DataGridView_DRE.CurrentCellAddress.Y).Value))
                Dim fichierDRE As String = "c:\factures\" + ValeurChamp + "@" + DataGridView_DRE.Item(DataGridView_DRE_Num.Index, DataGridView_DRE.CurrentCellAddress.Y).Value + "@" + idposte + "@" + Format(Now, "yyyyMMddhhmmss") + ".dre"
                erreur = SGD_LireChampFichier(NumeroSessionFacture, 160, 1, 2, fichierDRE)
                AfficheErreur(erreur)
                If erreur <> 0 Then Exit Sub
                My.Computer.FileSystem.DeleteFile("C:\Factures\Desync\" + DataGridView_DRE.Item(DataGridView_DRE_Fichier.Index, DataGridView_DRE.CurrentCellAddress.Y).Value)
                '____________________________________________________________________________________________________
                'Visu_API agrement CNDA
                If Visu_API = True Then
                    Technique_MAIN.LireSession_Out_VersFichier2(NumeroSessionFacture, Application.StartupPath + "\traces\Sécuriser_DRE_out.txt")
                End If
                '____________________________________________________________________________________________________
                Afficher_DRE_à_sécuriser()
        End Select
    End Sub
End Class