' 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 PhML_Auto_Sender
    Public Sub Auto_sender()
        If UCase(Config.Logiciel_ModeFonctionement) = "TEST" Then Exit Sub
DEBUT:
        Dim envois = Me.Commandes_auto_senderTableAdapter.GetDataBy_Jour_Heure(Now.DayOfWeek.ToString, Now.Hour.ToString & ":" & Now.Minute.ToString)
        For r = 0 To envois.Rows.Count - 1
            Dim commandes_A_TRANSMETTRE = Commandes_PharmaML.CommandesTableAdapter.GetDataBy_Fournisseur_Statut(envois(r).fournisseur, "A TRANSMETTRE")
            For rr = 0 To commandes_A_TRANSMETTRE.Rows.Count - 1
                Bandeau_info_bas_show("Transmission commande " & commandes_A_TRANSMETTRE(rr).ref.ToString & " " & commandes_A_TRANSMETTRE(rr).Fournisseur)
                Commandes_PharmaML.Transmettre_Commande(commandes_A_TRANSMETTRE(rr).ref, False)
                Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
            Next
            'auto_réassort_auto_command(envois(r).fournisseur)
        Next
        envois = Nothing
        System.Threading.Thread.Sleep(30000)
        envois = Nothing
        GoTo DEBUT


    End Sub

   


    Private Sub auto_réassort_auto_command(ByVal fournisseur As String)
        Dim produits_auto_réassort = COMPTOIR_MAIN.ProduitsTableAdapter.GetData_Réassort_auto(fournisseur)
        If produits_auto_réassort.Rows.Count > 0 Then

            Bandeau_info_bas_show("Auto-Réassort " & fournisseur)
            Dim ref_commande = Commandes_PharmaML.CommandesTableAdapter.Select_Max_ref + 1
            Commandes_PharmaML.CommandesTableAdapter.Insert(ref_commande, fournisseur, "", Format(Now, "yyyy-MM-dd"), "A TRANSMETTRE", "", "", "")
            Dim num_ligne As Integer = 0
            For r = 0 To produits_auto_réassort.Rows.Count - 1


                num_ligne = num_ligne + 1
                Dim quantité As Single = Val(produits_auto_réassort.Item(r).Réassort_auto_seuil_max) - Val(produits_auto_réassort.Item(r).Stock)
                If produits_auto_réassort.Item(r).EAN13 <> "" Then
                    Commandes_PharmaML.Commandes_LignesTableAdapter.Insert(ref_commande, num_ligne, "EAN13", produits_auto_réassort.Item(r).EAN13, produits_auto_réassort.Item(r).Designation, _
                                                         quantité, True, False, False, "", "", "", 0, 0, 0)
                    COMPTOIR_MAIN.ProduitsTableAdapter.Update_en_commande(quantité, produits_auto_réassort.Item(r).EAN13)


                ElseIf produits_auto_réassort.Item(r).CIP <> "" Then
                    Commandes_PharmaML.Commandes_LignesTableAdapter.Insert(ref_commande, num_ligne, "CIP39", produits_auto_réassort.Item(r).CIP, produits_auto_réassort.Item(r).Designation, _
                                              quantité, True, False, False, "", "", "", 0, 0, 0)
                    COMPTOIR_MAIN.ProduitsTableAdapter.Update_en_commande(quantité, produits_auto_réassort.Item(r).CIP)

                Else
                    Commandes_PharmaML.Commandes_LignesTableAdapter.Insert(ref_commande, num_ligne, "CLAIR", "", produits_auto_réassort.Item(r).Designation, _
                                              quantité, True, False, False, "", "", "", 0, 0, 0)
                    COMPTOIR_MAIN.ProduitsTableAdapter.Update_en_commande(quantité, produits_auto_réassort.Item(r).Designation)

                End If


            Next

            Commandes_PharmaML.Transmettre_Commande(ref_commande, True)
            Bandeau_info_bas_hide() 'Bandeau_info_bas_show_thread)
        End If


    End Sub

  

    Private Sub PhML_Auto_Sender_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.Icon = Icon.FromHandle(officine.My.Resources.Commandes2.GetHicon)

        affiche_data()

        Dim fournisseurs = BDD.FournisseurTableAdapter.GetData_OrderByNom
        For r = 0 To fournisseurs.Rows.Count - 1
            If fournisseurs.Item(r).URL <> "" Then
                Fournisseurs_ComboBox.Items.Add(fournisseurs.Item(r).Nom)
            End If
        Next
    End Sub

    Private Sub affiche_data()
        Lundi.Rows.Clear()
        Mardi.Rows.Clear()
        Mercredi.Rows.Clear()
        Jeudi.Rows.Clear()
        Vendredi.Rows.Clear()
        Samedi.Rows.Clear()
        Dimanche.Rows.Clear()
        Dim datas = Me.Commandes_auto_senderTableAdapter.GetData_orderby_heure
        For r = 0 To datas.Rows.Count - 1
            Select Case datas(r).Jour
                Case "Monday"
                    Lundi.Rows.Add()
                Case "Tuesday"
                    Mardi.Rows.Add()
                Case "Wednesday"
                    Mercredi.Rows.Add()
                Case "Thursday"
                    Jeudi.Rows.Add()
                Case "Friday"
                    Vendredi.Rows.Add()
                Case "Saturday"
                    Samedi.Rows.Add()
                Case "Sunday"
                    Dimanche.Rows.Add()
            End Select
        Next
        Dim idx_Lun, idx_Mar, idx_Mer, idx_Jeu, idx_Ven, idx_Sam, idx_Dim As Integer
        For r = 0 To datas.Rows.Count - 1
            Select Case datas(r).Jour
                Case "Monday"
                    Lundi.Item(0, idx_Lun).Value = datas(r).fournisseur
                    Lundi.Item(1, idx_Lun).Value = datas(r).heure
                    idx_Lun = idx_Lun + 1
                Case "Tuesday"
                    Mardi.Item(0, idx_Mar).Value = datas(r).fournisseur
                    Mardi.Item(1, idx_Mar).Value = datas(r).heure
                    idx_Mar = idx_Mar + 1
                Case "Wednesday"
                    Mercredi.Item(0, idx_Mer).Value = datas(r).fournisseur
                    Mercredi.Item(1, idx_Mer).Value = datas(r).heure
                    idx_Mer = idx_Mer + 1
                Case "Thursday"
                    Jeudi.Item(0, idx_Jeu).Value = datas(r).fournisseur
                    Jeudi.Item(1, idx_Jeu).Value = datas(r).heure
                    idx_Jeu = idx_Jeu + 1
                Case "Friday"
                    Vendredi.Item(0, idx_Ven).Value = datas(r).fournisseur
                    Vendredi.Item(1, idx_Ven).Value = datas(r).heure
                    idx_Ven = idx_Ven + 1
                Case "Saturday"
                    Samedi.Item(0, idx_Sam).Value = datas(r).fournisseur
                    Samedi.Item(1, idx_Sam).Value = datas(r).heure
                    idx_Sam = idx_Sam + 1
                Case "Sunday"
                    Dimanche.Item(0, idx_Dim).Value = datas(r).fournisseur
                    Dimanche.Item(1, idx_Dim).Value = datas(r).heure
                    idx_Dim = idx_Dim + 1
            End Select
        Next
    End Sub

    Private Sub Lundi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Lundi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Lundi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Lundi"
        Heure_TextBox.Text = Mid(Lundi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Lundi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Mardi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Mardi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Mardi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Mardi"
        Heure_TextBox.Text = Mid(Mardi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Mardi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Mercredi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Mercredi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Mercredi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Mercredi"
        Heure_TextBox.Text = Mid(Mercredi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Mercredi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Jeudi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Jeudi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Jeudi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Jeudi"
        Heure_TextBox.Text = Mid(Jeudi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Jeudi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Vendredi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Vendredi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Vendredi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Vendredi"
        Heure_TextBox.Text = Mid(Vendredi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Vendredi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Samedi_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Samedi.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Samedi.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Samedi"
        Heure_TextBox.Text = Mid(Samedi.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Samedi.Item(1, e.RowIndex).Value, 4, 2)
    End Sub
    Private Sub Dimanche_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Dimanche.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Fournisseurs_ComboBox.SelectedItem = Dimanche.Item(0, e.RowIndex).Value
        Jours_ComboBox.SelectedItem = "Dimanche"
        Heure_TextBox.Text = Mid(Dimanche.Item(1, e.RowIndex).Value, 1, 2)
        Minute_TextBox.Text = Mid(Dimanche.Item(1, e.RowIndex).Value, 4, 2)
    End Sub

    Private Function FR_to_ENG(ByVal jour As String) As String
        Select Case jour
            Case "Lundi"
                Return "Monday"
            Case "Mardi"
                Return "Tuesday"
            Case "Mercredi"
                Return "Wednesday"
            Case "Jeudi"
                Return "Thursday"
            Case "Vendredi"
                Return "Friday"
            Case "Samedi"
                Return "Saturday"
            Case "Dimanche"
                Return "Sunday"
        End Select
        Return ""
    End Function

    Private Sub Add_Button_Click(sender As Object, e As EventArgs) Handles Add_Button.Click
        If Heure_TextBox.Text.Length > 2 Or Heure_TextBox.Text = "" Or Not IsNumeric(Heure_TextBox.Text) Or Minute_TextBox.Text.Length > 2 Or Minute_TextBox.Text = "" Or Not IsNumeric(Minute_TextBox.Text) Or _
            Fournisseurs_ComboBox.SelectedItem Is Nothing Or Jours_ComboBox.SelectedItem Is Nothing Then
            MyMsg_Box.Presentation("Officine !", "Enregistrement impossible", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        While Heure_TextBox.Text.Length < 2
            Heure_TextBox.Text = "0" & Heure_TextBox.Text
        End While
        While Minute_TextBox.Text.Length < 2
            Minute_TextBox.Text = "0" & Minute_TextBox.Text
        End While
        If Me.Commandes_auto_senderTableAdapter.GetDataBy_All(FR_to_ENG(Jours_ComboBox.SelectedItem.ToString), Heure_TextBox.Text & ":" & Minute_TextBox.Text, Fournisseurs_ComboBox.SelectedItem.ToString).Rows.Count = 0 Then
            Me.Commandes_auto_senderTableAdapter.Insert(FR_to_ENG(Jours_ComboBox.SelectedItem.ToString), Heure_TextBox.Text & ":" & Minute_TextBox.Text, Fournisseurs_ComboBox.SelectedItem.ToString)
        End If
        affiche_data()
    End Sub

    Private Sub Suppr_Button_Click(sender As Object, e As EventArgs) Handles Suppr_Button.Click
        If Heure_TextBox.Text.Length > 2 Or Heure_TextBox.Text = "" Or Not IsNumeric(Heure_TextBox.Text) Or Minute_TextBox.Text.Length > 2 Or Minute_TextBox.Text = "" Or Not IsNumeric(Minute_TextBox.Text) Or _
     Fournisseurs_ComboBox.SelectedItem Is Nothing Or Jours_ComboBox.SelectedItem Is Nothing Then
            MyMsg_Box.Presentation("Officine !", "Suppression impossible", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
        Me.Commandes_auto_senderTableAdapter.Delete_One(FR_to_ENG(Jours_ComboBox.SelectedItem.ToString), Heure_TextBox.Text & ":" & Minute_TextBox.Text, Fournisseurs_ComboBox.SelectedItem.ToString)
        affiche_data()
    End Sub

 
End Class