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


    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        calcul_TVA_AChat(DateTimePicker1.Value, DateTimePicker2.Value)
        Me.Close()
    End Sub
    Private Sub calcul_TVA_AChat(ByVal datedebut As Date, ByVal datefin As Date)
        LinkLabel1.Text = "Calcul..."
         Bandeau_info_bas_show("Calcul TVA...")
        Try


            Me.Produits_Achats_Temp_calcul_TVATableAdapter.DeleteQuery()
            Dim current_date = New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, 0, 0, 0, 0)
            Dim date_fin = New DateTime(DateTimePicker2.Value.Year, DateTimePicker2.Value.Month, DateTimePicker2.Value.Day, 23, 59, 59, 999)
            While DateTime.Compare(current_date, date_fin) < 0
                Dim achats As DataTable = BDD.Produits_AchatsTableAdapter.Calcul_Tva_By_Date(current_date, DateAdd(DateInterval.Day, 1, current_date))
                For r = 0 To achats.Rows.Count - 1
                    Bandeau_info_bas_change_text("Calcul TVA... " & current_date.ToShortDateString)
                    Application.DoEvents()
                    Dim tva As Decimal
                    Dim total As Decimal
                    For c = 0 To achats.Columns.Count - 1
                        Select Case achats.Columns(c).ColumnName
                            Case "TVA"
                                tva = achats(r).Item(c)
                            Case "Total"
                                total = achats(r).Item(c)
                        End Select
                    Next
                    Me.Produits_Achats_Temp_calcul_TVATableAdapter.Insert(tva, total, current_date)
                Next
                current_date = DateAdd(DateInterval.DayOfYear, 1, current_date)
            End While
        Catch ex As Exception
            Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
            LinkLabel1.Text = "Afficher"
        End Try

        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
        LinkLabel1.Text = "Afficher"
        Dim parameters As String = "&Début=" & Format(DateTimePicker1.Value, "yyyy-MM-dd") & "T00:00:00" & "&Fin=" & Format(DateTimePicker2.Value, "yyyy-MM-dd") & "T23:59:59"
        Web_browser.Visible = True
        Web_browser.BringToFront()
        Web_browser.WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fAdministratif%2fCompta%2fBalance&rs:Command=Render" & parameters)

    End Sub
End Class