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

    Private Structure facture
        Dim NumFacture As String
        Dim file As String
        Dim CRL As String
        Dim traitée As Boolean
        Dim à_traiter As Boolean
        Dim BaseRemboursement As Integer
        Dim MontantAMO As Integer
        Dim MontantAMC As Integer
        Dim Datef As String
        Dim CodeRégime As String
        Dim CaisseGestionnaire As String
        Dim CentreGestionnaire As String
        Dim NomPrénom As String
    End Structure
    Private FacturesTab() As facture
    Private Structure Lot
        Dim CRL As String
        Dim NbreFactures As Integer
        Dim Total As Double
        Dim Organisme As String
        Dim Constitution As Boolean
    End Structure
    Private LotsTab() As Lot

    Private Sub LoadFactures()
        ReDim FacturesTab(0)
        Dim file As String = Dir("c:\factures\*.fse")
        Dim compteurfactures As Integer = 0
        Do Until file = ""
            ReDim Preserve FacturesTab(FacturesTab.Length)
            FacturesTab(compteurfactures).file = file
            FacturesTab(compteurfactures).traitée = False
            FacturesTab(compteurfactures).à_traiter = True
            FacturesTab(compteurfactures).CRL = Mid(file, 1, 40)
            file = Mid(file, InStr(file, "@") + 1)
            FacturesTab(compteurfactures).NumFacture = Val(Mid(file, 1, InStr(file, "@") - 1)).ToString
            FacturesTab(compteurfactures).CodeRégime = Mid(FacturesTab(compteurfactures).CRL, InStr(FacturesTab(compteurfactures).CRL, "PH") + 2, 2)
            FacturesTab(compteurfactures).CaisseGestionnaire = Mid(FacturesTab(compteurfactures).CRL, InStr(FacturesTab(compteurfactures).CRL, "PH") + 4, 3)
            FacturesTab(compteurfactures).CentreGestionnaire = Me.FacturesTableAdapter1.SelectCentreGestionnairebyNumFacture(FacturesTab(compteurfactures).NumFacture)
            FacturesTab(compteurfactures).BaseRemboursement = Me.Facture_LigneFactureTableAdapter1.SelectSUMBaseRemboursementByNumFacture(FacturesTab(compteurfactures).NumFacture)
            FacturesTab(compteurfactures).MontantAMO = Me.Facture_LigneFactureTableAdapter1.SelectSUMMontantAMOByNumFacture(FacturesTab(compteurfactures).NumFacture)
            FacturesTab(compteurfactures).MontantAMC = Me.Facture_LigneFactureTableAdapter1.SelectSUMMonstantAMCByNumFacture(FacturesTab(compteurfactures).NumFacture)
            FacturesTab(compteurfactures).Datef = Me.FacturesTableAdapter1.SelectDatebyNum(FacturesTab(compteurfactures).NumFacture)
            Dim IdBenef As String = Me.FacturesTableAdapter1.SelectIdBenef(FacturesTab(compteurfactures).NumFacture)
            Dim G101C8, G101C9, G104C12, G104C13, G104C14 As String
            G101C8 = Mid(IdBenef, 1, InStr(IdBenef, "@/@/@") - 1)
            IdBenef = Mid(IdBenef, InStr(IdBenef, "@/@/@") + 5)
            G101C9 = Mid(IdBenef, 1, InStr(IdBenef, "@/@/@") - 1)
            IdBenef = Mid(IdBenef, InStr(IdBenef, "@/@/@") + 5)
            G104C12 = Mid(IdBenef, 1, InStr(IdBenef, "@/@/@") - 1)
            IdBenef = Mid(IdBenef, InStr(IdBenef, "@/@/@") + 5)
            G104C13 = Mid(IdBenef, 1, InStr(IdBenef, "@/@/@") - 1)
            IdBenef = Mid(IdBenef, InStr(IdBenef, "@/@/@") + 5)
            G104C14 = IdBenef
            FacturesTab(compteurfactures).NomPrénom = Me.VitaleTableAdapter1.SelectNomPrénom(G101C8, G101C9, G104C12, G104C13, G104C14)
            file = Dir()
            compteurfactures = compteurfactures + 1
        Loop
        ReDim Preserve FacturesTab(FacturesTab.Length - 2)
    End Sub
    Private Sub LoadLots()
        ReDim LotsTab(0)
        For i = 0 To FacturesTab.Length - 1
            If FacturesTab(i).à_traiter = True Then
                Dim Found As Boolean = False
                For j = 0 To LotsTab.Length - 1
                    If FacturesTab(i).CRL = LotsTab(j).CRL Then
                        Found = True
                        LotsTab(j).NbreFactures = LotsTab(j).NbreFactures + 1
                        LotsTab(j).Total = LotsTab(j).Total + FacturesTab(i).MontantAMO + FacturesTab(i).MontantAMC
                    End If
                    If Found = False Then
                        ReDim Preserve LotsTab(j + 1)
                        LotsTab(j).CRL = FacturesTab(i).CRL
                        LotsTab(j).Constitution = True
                        LotsTab(j).NbreFactures = 1
                        LotsTab(j).Total = FacturesTab(i).MontantAMO + FacturesTab(i).MontantAMC
                        For k = 0 To taborgdest.Length - 1
                            If (FacturesTab(i).CodeRégime = taborgdest(k).Coderégime) And (FacturesTab(i).CaisseGestionnaire = taborgdest(k).CaisseGestionnaire) And (FacturesTab(i).CentreGestionnaire = taborgdest(k).CentreGestionnaire) Then
                                LotsTab(j).Organisme = taborgdest(k).Intitulé
                            End If
                        Next

                    End If
                Next
            End If
        Next
    End Sub
    Private Sub Affiche_Lots_à_constituer()
        LotDataGridView.Rows.Clear()
        Dim j = 0
        For i = 0 To LotsTab.Length - 2
            LotDataGridView.Rows.Add()
            If LotsTab(i).Constitution = False Then
                LotDataGridView(Constitution.Index, j).Value = False
            Else
                LotDataGridView(Constitution.Index, j).Value = True
            End If
            LotDataGridView(Organisme.Index, j).Value = LotsTab(i).Organisme
            LotDataGridView(FacturesNombre.Index, j).Value = LotsTab(i).NbreFactures
            LotDataGridView(Total.Index, j).Value = (LotsTab(i).Total / 100).ToString + "€"
            LotDataGridView(CRL.Index, j).Value = LotsTab(i).CRL
            j = j + 1
        Next
    End Sub
    Private Sub Affiche_Factures(ByVal CRL As String)
        FacturesDataGridView.Rows.Clear()
        Dim j = 0
        For i = 0 To FacturesTab.Length - 1
            If FacturesTab(i).CRL = CRL Then
                FacturesDataGridView.Rows.Add()
                If FacturesTab(i).à_traiter = False Then
                    FacturesDataGridView(à_traiter.Index, j).Value = False
                Else
                    FacturesDataGridView(à_traiter.Index, j).Value = True
                End If
                FacturesDataGridView(NumFacture.Index, j).Value = FacturesTab(i).NumFacture
                FacturesDataGridView(NomPrénom.Index, j).Value = FacturesTab(i).NomPrénom
                FacturesDataGridView(Datef.Index, j).Value = Mid(FacturesTab(i).Datef, 7, 2) + "/" + Mid(FacturesTab(i).Datef, 5, 2) + "/" + Mid(FacturesTab(i).Datef, 1, 4)
                FacturesDataGridView(BaseRemboursement.Index, j).Value = FacturesTab(i).BaseRemboursement / 100
                FacturesDataGridView(MontantAMO.Index, j).Value = FacturesTab(i).MontantAMO / 100
                FacturesDataGridView(MontantAMC.Index, j).Value = FacturesTab(i).MontantAMC / 100
                j = j + 1
            End If
        Next
    End Sub

    Private Sub ConstitutionLots2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadFactures()
        LoadLots()
        Affiche_Lots_à_constituer()

    End Sub

    Private Sub LotDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles LotDataGridView.CellContentClick

        Select Case LotDataGridView.CurrentCellAddress.X

            Case Détail.Index
                Affiche_Factures(LotDataGridView(CRL.Index, LotDataGridView.CurrentRow.Index).Value)

            Case Constitution.Index
                If LotsTab(LotDataGridView.CurrentCellAddress.Y).Constitution = True Then
                    LotsTab(LotDataGridView.CurrentCellAddress.Y).Constitution = False
                Else
                    LotsTab(LotDataGridView.CurrentCellAddress.Y).Constitution = True

                End If
        End Select

    End Sub

    Private Sub FacturesDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles FacturesDataGridView.CellContentClick

        Select Case FacturesDataGridView.CurrentCellAddress.X

            Case à_traiter.Index
                If FacturesTab(FacturesDataGridView.CurrentCellAddress.Y).à_traiter = True Then
                    FacturesTab(FacturesDataGridView.CurrentCellAddress.Y).à_traiter = False
                Else
                    FacturesTab(FacturesDataGridView.CurrentCellAddress.Y).à_traiter = True
                End If
                LoadLots()
                Affiche_Lots_à_constituer()

        End Select
    End Sub

    Private Sub ConstitutionLotsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConstitutionLotsButton.Click
        For i = 0 To LotsTab.Length - 2
            If LotsTab(i).Constitution = True Then


                SGD_ReserverSession(10000000, NumeroSessionLot)

                'Groupe7
                SGD_AjouterGroupe(NumeroSessionLot, 7)
                'Champ1
                ValeurChamp = "12345678"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 9, ValeurChamp, 8)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                'Champ2
                ValeurChamp = "1"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 10, ValeurChamp, 1)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)

                'Groupe8
                SGD_AjouterGroupe(NumeroSessionLot, 8)
                'Champ1
                SGD_AjouterChamp(NumeroSessionLot, "TESTCNDA1407")
                'Champ2
                ValeurChamp = "12345678"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 9, ValeurChamp, 8)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                'Champ3
                ValeurChamp = "1"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 10, ValeurChamp, 1)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                'Champ4
                ValeurChamp = "1234567890123456789012345678901234567890"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 8, ValeurChamp, 40)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                'Champ5
                ValeurChamp = "1"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 1, ValeurChamp, 1)
                SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                'Champ6
                SGD_AjouterChamp(NumeroSessionLot, "TP") 'Professionnel de Santé hors établissement
                'Champ7
                Dim numemetteur As String
                ValeurChamp = "12345678"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 9, ValeurChamp, 8)
                numemetteur = ValeurChamp
                ValeurChamp = "1"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 10, ValeurChamp, 1)
                numemetteur = "00000" + numemetteur + ValeurChamp
                SGD_AjouterChamp(NumeroSessionLot, numemetteur)
                'Champ8
                Dim datelot As String = Format(Now, "yyyyMMdd") + "0000"
                SGD_AjouterChamp(NumeroSessionLot, datelot)
                'Champ9
                SGD_AjouterChamp(NumeroSessionLot, "7") 'Lot unique à formater
                'champ10
                Dim RéférenceLotFSE As Integer = Me.LotsTableAdapter.SelectMaxRéférence + 1
                SGD_AjouterChamp(NumeroSessionLot, RéférenceLotFSE.ToString)
                'champ11
                SGD_AjouterChamp(NumeroSessionLot, LotsTab(i).NbreFactures.ToString)
                'champ12,13,14
                ValeurChamp = "XXXXXXXXXXXXXX"
                SGD_LireChamp(NumeroSessionCPS, 2, 1, 6, ValeurChamp, 14)
                If ValeurChamp <> "XXXXXXXXXXXXXX" Then
                    ValeurChamp = "X"
                    SGD_LireChamp(NumeroSessionCPS, 2, 1, 5, ValeurChamp, 1)
                    SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                    ValeurChamp = "XXXXXXXXXXXXXX"
                    SGD_LireChamp(NumeroSessionCPS, 2, 1, 6, ValeurChamp, 14)
                    SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                    ValeurChamp = "X"
                    SGD_LireChamp(NumeroSessionCPS, 2, 1, 7, ValeurChamp, 1)
                    SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                Else
                    SGD_AjouterChamp(NumeroSessionLot, "9")
                    SGD_AjouterChamp(NumeroSessionLot, "9")
                    ValeurChamp = "X"
                    SGD_LireChamp(1, 2, 1, 10, ValeurChamp, 1)
                    SGD_AjouterChamp(NumeroSessionLot, ValeurChamp)
                End If

                'groupe6
                SGD_AjouterGroupe(NumeroSessionLot, 6)
                SGD_AjouterChamp(NumeroSessionLot, LotsTab(i).CRL)

                'groupe9
                SGD_AjouterGroupe(NumeroSessionLot, 9)
                For j = 0 To FacturesTab.Length - 1
                    If FacturesTab(j).CRL = LotsTab(i).CRL And FacturesTab(j).traitée = False And FacturesTab(j).à_traiter = True Then
                        Dim bytes = My.Computer.FileSystem.ReadAllBytes("c:\factures\" + FacturesTab(j).file)
                        My.Computer.FileSystem.WriteAllBytes("c:\factures\lots\temp", bytes, True)
                        FacturesTab(j).traitée = True
                        'deplacement fichier FSE apres traitement
                        My.Computer.FileSystem.MoveFile("c:\factures\" + FacturesTab(j).file, "C:\Factures\Traitées\" + FacturesTab(j).file)
                    End If
                Next
                SGD_AjouterChampFichier(NumeroSessionLot, "c:\factures\lots\temp")
                My.Computer.FileSystem.DeleteFile("c:\factures\lots\temp")

                'SIS_FormaterLot
                Dim out As Integer = SIS_FormaterLot(NomRessourcePS, NomRessourceLecteur, CodePorteurPS, NumeroSessionLot)
                If out <> 0 Then
                    MsgBox("erreur constitution lot " + LotsTab(i).Organisme + Chr(10) + out.ToString)
                    Exit Sub
                End If

                Dim CRF As String = "123456789012345678901234567890"
                SGD_LireChamp(NumeroSessionLot, 10, 1, 1, CRF, 30)
                Dim TailleLot = 0
                SGD_LireChampBin(NumeroSessionLot, 11, 1, 1, ValeurChamp, TailleLot)
                'enregistrement lot dans bdd
                Me.LotsTableAdapter.Insert(RéférenceLotFSE, LotsTab(i).CRL, CRF, datelot, LotsTab(i).NbreFactures, TailleLot)
                'enregistrement lot dans fichier
                SGD_LireChampFichier(NumeroSessionLot, 11, 1, 1, "C:\Factures\Lots\" + RéférenceLotFSE.ToString + ".lotFSE")
                SGD_LireChampFichier(NumeroSessionLot, 91, 1, 1, "C:\Factures\Lots\" + RéférenceLotFSE.ToString + ".lotDRE")
            End If
        Next

        LoadFactures()
        LoadLots()
        Affiche_Lots_à_constituer()
        FacturesDataGridView.Rows.Clear()
    End Sub
End Class