' 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 Retrocession_Choix_Prix
    Public lignevente As Integer
    Public prix_non_remisé As Decimal
    Public remise As Decimal
    Public prix_remisé As Decimal

    Public Sub fill_datagridview()
        DataGridView1.Rows.Clear()
        Dim achats As PharmaDataSet.Produits_AchatsDataTable
        achats = BDD.Produits_AchatsTableAdapter.GetDataBy_code_designation_ordby_date_desc(COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.VenteEAN13.Index, lignevente).Value, _
                                                                                            COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.Vente_produits.Index, lignevente).Value)

        For r = 0 To Math.Min(achats.Rows.Count - 1, CInt(n_derniers_TextBox.Text) - 1)
            DataGridView1.Rows.Add()
        Next
        For r = 0 To Math.Min(achats.Rows.Count - 1, CInt(n_derniers_TextBox.Text) - 1)
            DataGridView1.Item(Histo_Commande_Date.Index, r).Value = achats(r)._Date.ToShortDateString
            Try
                DataGridView1.Item(Histo_Commande_Fournisseur.Index, r).Value = BDD.CommandesTableAdapter.GetDataBy_ref(achats(r).Ref_Commande).Item(0).Fournisseur.ToString
            Catch ex As Exception
                DataGridView1.Item(Histo_Commande_Fournisseur.Index, r).Value = ""
            End Try


            DataGridView1.Item(Histo_Commande_prixnonremisé.Index, r).Value = achats(r).Prix_achat
            DataGridView1.Item(Histo_Commande_Prixremisé.Index, r).Value = achats(r).Prix_achat_remisé
            DataGridView1.Item(Histo_Commande_ref.Index, r).Value = achats(r).Ref_Commande
            DataGridView1.Item(Histo_Commande_Remise.Index, r).Value = achats(r).Remise
        Next
        If achats.Rows.Count > 0 Then
            prix_non_remisé_TextBox.Text = achats(0).Prix_achat
            remise_TextBox.Text = achats(0).Remise
            prix_remisé_TextBox.Text = achats(0).Prix_achat_remisé
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            COMPTOIR_MAIN.Retrocession_prix_non_remisé = CDec(Replace(prix_non_remisé_TextBox.Text, ".", ","))
            COMPTOIR_MAIN.Retrocession_prix_remisé = CDec(Replace(prix_remisé_TextBox.Text, ".", ","))
            COMPTOIR_MAIN.Retrocession_remise = CDec(Replace(remise_TextBox.Text, ".", ","))
            Me.Dispose()
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine: retrocession", "Renseignez les valeurs.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End Try
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex >= 0 Then
            prix_non_remisé_TextBox.Text = DataGridView1.Item(Histo_Commande_prixnonremisé.Index, e.RowIndex).Value
            prix_remisé_TextBox.Text = DataGridView1.Item(Histo_Commande_Prixremisé.Index, e.RowIndex).Value
            remise_TextBox.Text = DataGridView1.Item(Histo_Commande_Remise.Index, e.RowIndex).Value
        End If
       
    End Sub

   
    Private Sub n_derniers_TextBox_TextChanged(sender As Object, e As EventArgs) Handles n_derniers_TextBox.TextChanged
        Try
            CInt(n_derniers_TextBox.Text).ToString()
            If CInt(n_derniers_TextBox.Text) < 1 Then
                n_derniers_TextBox.Text = "1"
            End If
        Catch ex As Exception
            n_derniers_TextBox.Text = "1"
        End Try
        fill_datagridview()
    End Sub
End Class