' 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 Vente_Du
    Private Sub Fill_datagridview()
        DataGridView1.Rows.Clear()
        For r = 0 To COMPTOIR_MAIN.Vente.RowCount - 2
            If COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteEAN13.Index, r).Value <> "" Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCIP.Index, r).Value <> "" _
                Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Ventecab.Index, r).Value <> "" Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteDésignation.Index, r).Value <> "" Then
                DataGridView1.Rows.Add()
            End If
        Next
        Dim rr = 0
        For r = 0 To COMPTOIR_MAIN.Vente.RowCount - 2
            If COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteEAN13.Index, r).Value <> "" Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCIP.Index, r).Value <> "" _
                Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Ventecab.Index, r).Value <> "" Or COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteDésignation.Index, r).Value <> "" Then
                DataGridView1.Item(CIP7.Index, rr).Value = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCIP.Index, r).Value
                If COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteEAN13.Index, r).Value <> "" Then
                    DataGridView1.Item(CIP13.Index, rr).Value = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteEAN13.Index, r).Value
                Else
                    DataGridView1.Item(CIP13.Index, rr).Value = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.Ventecab.Index, r).Value
                End If
                DataGridView1.Item(Quantité_Facture.Index, rr).Value = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteQuantité.Index, r).Value
                DataGridView1.Item(Désignation.Index, rr).Value = COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteDésignation.Index, r).Value
                DataGridView1.Item(Quantitée.Index, rr).Value = 0

                rr = rr + 1
            End If
        Next
        If Not VenteForm.liste_du Is Nothing Then
            For r = 0 To DataGridView1.RowCount - 1
                For Each du_ In VenteForm.liste_du
                    If du_.cip7 = DataGridView1.Item(CIP7.Index, r).Value And du_.cip13 = DataGridView1.Item(CIP13.Index, r).Value And du_.désignation = DataGridView1.Item(Désignation.Index, r).Value Then
                        DataGridView1.Item(Quantitée.Index, r).Value = du_.Quantité_Du
                        If Not du_.Note Is Nothing Then
                            DataGridView1.Item(Note.Index, r).Value = du_.Note
                        Else
                            DataGridView1.Item(Note.Index, r).Value = ""
                        End If
                    End If

                Next


            Next
        End If
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        If DataGridView1.RowCount > 0 Then
            DataGridView1.CurrentCell = DataGridView1.Item(0, 0)
            If VenteForm.liste_du Is Nothing Then
                VenteForm.liste_du = New List(Of VenteForm.du)
            Else
                VenteForm.liste_du.Clear()
            End If
            VenteForm.Produits_Dus_ListBox.Items.Clear()
            For r = 0 To DataGridView1.RowCount - 1
                If DataGridView1.Item(Quantitée.Index, r).Value > 0 Then
                    Dim the_du As VenteForm.du
                    the_du.cip7 = DataGridView1.Item(CIP7.Index, r).Value
                    the_du.cip13 = DataGridView1.Item(CIP13.Index, r).Value
                    the_du.désignation = DataGridView1.Item(Désignation.Index, r).Value
                    the_du.Quantité_Du = DataGridView1.Item(Quantitée.Index, r).Value
                    If DataGridView1.Item(Note.Index, r).Value Is Nothing Then
                        the_du.Note = ""
                    Else
                        the_du.Note = DataGridView1.Item(Note.Index, r).Value.ToString
                    End If
                    VenteForm.liste_du.Add(the_du)
                    VenteForm.Produits_Dus_ListBox.Items.Add(the_du.Quantité_Du.ToString & "  " & the_du.désignation)

                End If
            Next
        End If
        VenteForm.afficheinfovente()
        VenteForm.affiche_nombres_boites()
        Me.Close()
    End Sub



    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        If e.ColumnIndex = Quantitée.Index Then
            If Not IsNumeric(DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value) Then
                DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = 0
            End If
        End If
    End Sub

    Private Sub Vente_Du_Load(sender As Object, e As EventArgs) Handles Me.Load
        Fill_datagridview()
    End Sub

   

End Class