' 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/>.
Imports System.IO

Public Class Variation_Stock
    Private Structure produit_
        Dim code As String
        Dim designation As String
        Dim stock As Integer
        Dim stock_ini As Integer

    End Structure
    Private my_produit As produit_

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Bandeau_info_bas_show("Calcul en cours...")
        For r = 0 To DataGridView1.RowCount - 1
            Bandeau_info_bas_change_text("Calcul en cours... " & (r + 1).ToString & "/" & DataGridView1.RowCount)
            Bandeau_info_bas_text = ("Calcul en cours... " & (r + 1).ToString & "/" & DataGridView1.RowCount.ToString)
            DataGridView1.Item(Vente.Index, r).Value = BDD.ActesTableAdapter.Select_Sum_between_dates(DataGridView1(Code.Index, r).Value, DataGridView1(Code.Index, r).Value, DataGridView1(Produit.Index, r).Value, _
                                                                  Format(DateTimePicker1.Value, "yyyyMMddHHmmss"), Format(Now, "yyyyMMddHHmmss"))
            DataGridView1.Item(Achat.Index, r).Value = Commandes_PharmaML.Commandes_LignesTableAdapter.Select_Sum_between_dates(DataGridView1(Code.Index, r).Value, DataGridView1(Produit.Index, r).Value, _
                                                                           Format(DateTimePicker1.Value, "yyyy-MM-dd"), Format(Now, "yyyy-MM-dd"))
            DataGridView1.Item(variation.Index, r).Value = DataGridView1.Item(Achat.Index, r).Value - DataGridView1.Item(Vente.Index, r).Value
            DataGridView1.Item(Stock_théorique.Index, r).Value = DataGridView1.Item(Stock_ini.Index, r).Value + DataGridView1.Item(variation.Index, r).Value
            DataGridView1.Item(ecart.Index, r).Value = DataGridView1.Item(Stock_théorique.Index, r).Value - DataGridView1.Item(Stock_actuel.Index, r).Value
        Next
        Bandeau_info_bas_hide()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim data = BDD.ProduitsTableAdapter.GetDataBY_Cipean13_Or_Cab_Or_Designation(TextBox1.Text, TextBox1.Text, TextBox1.Text)
        If data.Rows.Count > 0 Then
            my_produit.code = data.Item(0).Code_PharmaML
            If my_produit.code = "" Then
                my_produit.code = data.Item(0).EAN13
            End If
            If my_produit.code = "" Then
                my_produit.code = data.Item(0).CIP
            End If
            If my_produit.code = "" Then
                my_produit.code = data.Item(0).CAB
            End If
            my_produit.designation = data.Item(0).Designation
            Label1.Text = data.Item(0).Designation
            my_produit.stock = data.Item(0).Stock
            my_produit.stock_ini = 0

        Else
            my_produit.code = ""
            my_produit.designation = ""
            Label1.Text = ""
            my_produit.stock_ini = 0
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If my_produit.code <> "" Or my_produit.designation <> "" Then
            DataGridView1.Rows.Add()
            DataGridView1.Item(Code.Index, DataGridView1.RowCount - 1).Value = my_produit.code
            DataGridView1.Item(Produit.Index, DataGridView1.RowCount - 1).Value = my_produit.designation
            DataGridView1.Item(Stock_actuel.Index, DataGridView1.RowCount - 1).Value = my_produit.stock_ini
            DataGridView1.Item(Stock_ini.Index, DataGridView1.RowCount - 1).Value = 0
        End If

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Bandeau_info_bas_show("Selection fichier")
        'OpenFileDialog1.ShowDialog()
        'Dim fichier = OpenFileDialog1.FileName
        RichTextBox1.Clear()
        Dim fichier = "C:\Users\julien\Desktop\Stock.txt"
        Dim sr As New StreamReader(fichier)
        Dim line = sr.ReadLine
        Dim i = 0
        While Not line Is Nothing
            Bandeau_info_bas_change_text(line)
            i = i + 1
            Try
                Dim code_ = Mid(line, 1, InStr(line, " ") - 1)
                Dim stock_ini_ = CInt(Mid(line, InStrRev(line, " ") + 1))
                DataGridView1.Rows.Add()
                DataGridView1.Item(Code.Index, DataGridView1.Rows.Count - 1).Value = code_
                DataGridView1.Item(Stock_ini.Index, DataGridView1.Rows.Count - 1).Value = stock_ini_

                Dim prod = BDD.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(code_)
                If prod.Rows.Count = 1 Then
                    DataGridView1.Item(Produit.Index, DataGridView1.Rows.Count - 1).Value = prod.Item(0).Designation
                    DataGridView1.Item(Stock_actuel.Index, DataGridView1.RowCount - 1).Value = CInt(prod.Item(0).Stock)
                ElseIf prod.Rows.Count > 1 Then
                    RichTextBox1.AppendText(i.ToString & " " & line & vbCrLf & "plusieurs produits pour ce code" & vbCrLf & vbCrLf)
                End If
                

            Catch ex As Exception
                RichTextBox1.AppendText(i.ToString & " " & line & vbCrLf & ex.Message & vbCrLf & vbCrLf)
            End Try


            line = sr.ReadLine
        End While
        Bandeau_info_bas_hide()
    End Sub
End Class