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

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If BDD.PromotionsTableAdapter.GetData.Rows.Count > 0 Then
                BDD.PromotionsTableAdapter.Insert(CIP_TextBox.Text, CAB_TextBox.Text, intitulé_produit_TextBox.Text, DateTimePicker1.Value, DateAdd(DateInterval.Day, 1, DateTimePicker2.Value), PPTTC_TextBox.Text, Rem_TextBox.Text, _
                                            CheckBox1.Checked, BDD.PromotionsTableAdapter.Select_Max_Num + 1, nom_prom_TextBox.Text)
            Else
                BDD.PromotionsTableAdapter.Insert(CIP_TextBox.Text, CAB_TextBox.Text, intitulé_produit_TextBox.Text, DateTimePicker1.Value, DateAdd(DateInterval.Day, 1, DateTimePicker2.Value), PPTTC_TextBox.Text, Rem_TextBox.Text, _
                                            CheckBox1.Checked, 1, nom_prom_TextBox.Text)
            End If
          
            affiche_histo()

        Catch ex As Exception
            MyMsg_Box.Presentation("Officine:", "Erreur d'enrengistrement de la promotion, verifiez vos valeurs.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()

        End Try
    End Sub


    Private Sub Promotions_Load(sender As Object, e As EventArgs) Handles Me.Load
        affiche_histo()
    End Sub
    Private Sub affiche_histo()
        DataGridView1.Rows.Clear()
        Dim promos = BDD.PromotionsTableAdapter.GetDataBy_prod(CIP_TextBox.Text, CAB_TextBox.Text, intitulé_produit_TextBox.Text)
        For r = 0 To promos.Rows.Count - 1
            DataGridView1.Rows.Add()
        Next
        For r = 0 To promos.Rows.Count - 1
            DataGridView1.Item(Num_Remise.Index, r).Value = promos.Item(r).Num
            DataGridView1.Item(intitulé.Index, r).Value = promos.Item(r).intitulé
            DataGridView1.Item(date_début.Index, r).Value = promos.Item(r).date_début.ToShortDateString
            DataGridView1.Item(date_fin.Index, r).Value = DateAdd(DateInterval.Day, -1, promos.Item(r).date_fin).ToShortDateString
            DataGridView1.Item(PPTTC.Index, r).Value = promos.Item(r).PPTTTC_Promo
            DataGridView1.Item(Pourcentage_Promo.Index, r).Value = promos.Item(r).Pourcentage_Promo
        Next
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = Suppr.Index Then
            MyMsg_Box.Presentation("Officine?", "Supprimmer la promotion?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                BDD.PromotionsTableAdapter.Delete_By_Num(DataGridView1.Item(Num_Remise.Index, e.RowIndex).Value)
                affiche_histo()
            End If
        End If
    End Sub

 


    Private Sub PPTTC_TextBox_Validated(sender As Object, e As EventArgs) Handles PPTTC_TextBox.Validated
        calcul_prix_remisé()
    End Sub


    Private Sub Rem_TextBox_Validated(sender As Object, e As EventArgs) Handles Rem_TextBox.Validated
        calcul_prix_remisé()
    End Sub
    Private Sub calcul_prix_remisé()
        Try
            Prix_rem_TextBox.Text = (CDec(PPTTC_TextBox.Text) * (100 - CDec(Rem_TextBox.Text)) / 100).ToString
        Catch ex As Exception

        End Try
    End Sub
End Class