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


    Private Sub SCOR_Documents_externes_Load1(sender As Object, e As EventArgs) Handles Me.Load
        CheckedListBox1.Items.Clear()
        Dim fact_table = COMPTOIR_MAIN.FacturesTableAdapter.GetDataBy_Bénéficiaire(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, CurrentBenéficiaire.G101C8)
        For r = 0 To fact_table.Rows.Count - 1
            Dim file = Dir(Config.LocalNetwork_ServerName & "\scor\doc\" & fact_table.Item(r).G1110C1 & "_*_*.pdf")
            While file <> ""

                Dim numlot = COMPTOIR_MAIN.FacturesTableAdapter.GetFactureDataByNum1(Val(Mid(file, 1, InStr(file, "_") - 1))).Item(0).Num_Lot_FSE
                Dim SCOR_lots_table = Teletransmission_MAIN.SCOR_lotsTableAdapter.GetDataBy_NumLot(numlot)
                Dim ajout As Boolean = True
                If SCOR_lots_table.Rows.Count = 0 Then
                    ajout = False

                Else
                    For rr = 0 To SCOR_lots_table.Rows.Count - 1
                        If SCOR_lots_table.Item(rr).Statut <> "ACQUITE" Then
                            ajout = False
                        End If
                    Next
                End If


                If ajout = True And numlot <> "" Then
                    If Not Teletransmission_MAIN.LotsTableAdapter.GetDataByRéférence(Val(numlot), "@@@@@@").Item(0).IsARLG31C9Null Then
                        If Replace(Teletransmission_MAIN.LotsTableAdapter.GetDataByRéférence(Val(numlot), "@@@@@@").Item(0).ARLG31C9, " ", "") <> "" Then
                            ajout = False
                        End If
                    End If
                End If
                If ajout = True Then
                    CheckedListBox1.Items.Add(file)
                End If
                file = Dir()

            End While
        Next

        For i = 0 To CheckedListBox1.Items.Count - 1
            If VenteForm.SCOR_Documents_Externes_list.Contains(CheckedListBox1.Items.Item(i).ToString) Then
                CheckedListBox1.SetItemCheckState(i, CheckState.Checked)
            End If
        Next
    End Sub

    Private Sub CheckedListBox1_DoubleClick(sender As Object, e As EventArgs) Handles CheckedListBox1.DoubleClick
        Process.Start(Config.LocalNetwork_ServerName & "\scor\doc\" & CheckedListBox1.SelectedItem.ToString)
    End Sub

    Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
        If Not CheckedListBox1.SelectedItem Is Nothing Then
            If e.CurrentValue = CheckState.Unchecked Then

                If Not VenteForm.SCOR_Documents_Externes_list.Contains(CheckedListBox1.SelectedItem.ToString) Then
                    VenteForm.SCOR_Documents_Externes_list.Add(CheckedListBox1.SelectedItem.ToString)
                    VenteForm.ListBox_Doc_archive.Items.Add(CheckedListBox1.SelectedItem.ToString)
                End If
            Else
                If VenteForm.SCOR_Documents_Externes_list.Contains(CheckedListBox1.SelectedItem.ToString) Then
                    VenteForm.SCOR_Documents_Externes_list.Remove(CheckedListBox1.SelectedItem.ToString)
                    VenteForm.ListBox_Doc_archive.Items.Remove(CheckedListBox1.SelectedItem.ToString)
                    VenteForm.ListBox_Doc_archive.Refresh()
                End If
            End If
        End If
    End Sub

    Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged

    End Sub
End Class
  
