' 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 Afficher_commandes
    Public prods_scannés() As Commande_réception.prod_scanné 'pour la réception groupée de commande
    Public reception_groupée_compteur_commande_à_traiter As Integer
    Public reception_groupée_compteur_commandes_traitées As Integer


    Private Sub Afficher_commandes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CommandesDataGridView.DefaultCellStyle.ForeColor = Color.Black
        Commandes_LignesDataGridView.DefaultCellStyle.ForeColor = Color.Black
        Me.Icon = Icon.FromHandle(officine.My.Resources.Commandes2.GetHicon)

        CommandesDataGridView.DataSource = Nothing
        CommandesDataGridView.Rows.Clear()
        Dim fournisseurs = Commandes_PharmaML.CommandesTableAdapter.Select_distinct_fournisseur
        For r = 0 To fournisseurs.Rows.Count - 1
            Fournisseur_ComboBox.Items.Add(fournisseurs.Item(r).Fournisseur)
        Next
        Dim statuts = Commandes_PharmaML.CommandesTableAdapter.SELECT_distinct_Statut
        For r = 0 To statuts.Rows.Count - 1
            Statut_ComboBox.Items.Add(statuts.Item(r).Statut)
        Next
        CommandesDataGridView.DataSource = Commandes_PharmaML.CommandesTableAdapter.GetData_TopX(20, "")
    End Sub

    Private Sub CommandesDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles CommandesDataGridView.CellClick
        affichage_lignes_commande()
    End Sub
    Private Sub Manquants_CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles Manquants_CheckBox.CheckedChanged
        affichage_lignes_commande()
    End Sub
    Private Sub affichage_lignes_commande()
        If CommandesDataGridView.CurrentRow.Index >= 0 Then
            Commandes_LignesDataGridView.DataSource = Commandes_PharmaML.Commandes_LignesTableAdapter.GetDataBy_ref_commande_manquants(CommandesDataGridView.Item(0, CommandesDataGridView.CurrentRow.Index).Value, Manquants_CheckBox.Checked)
        End If
    End Sub
    Private Sub Reception_Button_Click(sender As Object, e As EventArgs) Handles Reception_Button.Click


        Dim reception As New Commande_réception
        reception.reception_hors_commande = False
        reception.reception_groupée = False
        reception.Ref_Commande = CInt(CommandesDataGridView.Item(0, CommandesDataGridView.CurrentRow.Index).Value)
        reception.ShowDialog()
        CommandesDataGridView.DataSource = Commandes_PharmaML.CommandesTableAdapter.GetData_TopX(100, "")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If CommandesDataGridView.SelectedRows.Count > 1 Then
            Dim REF_MESSAGE_Fournisseur_index As Integer = 0
            Dim Statut_index As Integer = 0
            For c = 0 To CommandesDataGridView.Columns.Count - 1
                If CommandesDataGridView.Columns.Item(c).Name = "REF_MESSAGE_Fournisseur" Then
                    REF_MESSAGE_Fournisseur_index = c
                ElseIf CommandesDataGridView.Columns.Item(c).Name = "Statut" Then
                    Statut_index = c
                End If
            Next

            For Each row As DataGridViewRow In CommandesDataGridView.SelectedRows
                If row.Cells.Item(Statut_index).Value = "COMMANDE MANUELLE" Then
                    MyMsg_Box.Presentation("Officine !", "Vous ne pouvez pas recevoir de manière groupée les commandes manuelles.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
                End If
            Next
            For Each row As DataGridViewRow In CommandesDataGridView.SelectedRows
                If row.Cells.Item(REF_MESSAGE_Fournisseur_index).Value = "" Then
                    MyMsg_Box.Presentation("Officine !", "Vous ne pouvez pas recevoir de manière groupée des commandes n'ayant pas reçu de retour Pharma-ML.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Exit Sub
                End If
            Next
            For Each row As DataGridViewRow In CommandesDataGridView.SelectedRows
                If row.Cells.Item(Statut_index).Value <> "EN COURS FOURNISSEUR" Then
                    MyMsg_Box.Presentation("Officine !", "Vous ne pouvez pas recevoir de manière groupée des commandes qui n'ont pas le statut EN COURS FOURNISSEUR.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                End If
            Next
        ElseIf CommandesDataGridView.SelectedRows.Count = 1 Then
            Reception_Button.PerformClick()
            Exit Sub
        Else
            Exit Sub
        End If

        reception_groupée_compteur_commande_à_traiter = CommandesDataGridView.SelectedRows.Count
        reception_groupée_compteur_commandes_traitées = 0
        For Each row As DataGridViewRow In CommandesDataGridView.SelectedRows
            reception_groupée_compteur_commandes_traitées = reception_groupée_compteur_commandes_traitées + 1
            Dim reception As New Commande_réception
            reception.reception_hors_commande = False
            reception.reception_groupée = True
            reception.Ref_Commande = CInt(CommandesDataGridView.Item(0, row.Index).Value)
            reception.ShowDialog()
        Next
        prods_scannés = Nothing
        CommandesDataGridView.DataSource = Commandes_PharmaML.CommandesTableAdapter.GetData_TopX(100, "")
       

    End Sub



    Private Sub CommandesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles CommandesDataGridView.CellContentClick

    End Sub

    Private Sub CommandesDataGridView_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles CommandesDataGridView.DataBindingComplete

        Try
      
            'Dim période_column_index As Integer = 999
            'For c = 0 To CommandesDataGridView.ColumnCount - 1
            '    If CommandesDataGridView.Columns(c).Name = "période_column" Then
            '        période_column_index = c
            '    End If
            'Next
            'If période_column_index = 999 Then
            '    CommandesDataGridView.Columns.Add("période_column", "période")
            '    For c = 0 To CommandesDataGridView.ColumnCount - 1
            '        If CommandesDataGridView.Columns(c).Name = "période_column" Then
            '            période_column_index = c
            '            Exit For
            '        End If
            '    Next
            'End If
            For r = 0 To CommandesDataGridView.RowCount - 1
                For c = 0 To CommandesDataGridView.ColumnCount - 1
                    If CommandesDataGridView.Columns(c).DataPropertyName = "ref" Then
                        Dim chroniques = BDD.ChroniqueTableAdapter.GetDataBy_Commande_Ref(CommandesDataGridView.Item(c, r).Value.ToString)
                        If chroniques.Rows.Count > 0 Then
                            Dim vitales = BDD.VitaleTableAdapter.GetDataBy_Num(chroniques.Item(0).Vitale_Num)
                            If vitales.Rows.Count > 0 Then
                                For rr = 0 To vitales.Rows.Count - 1
                                    If vitales(rr).Chronique_Periode > 0 Then
                                        'CommandesDataGridView.Item(période_column_index, r).Value = vitales(rr).Chronique_Periode

                                        '''''''''''''''''''''
                                        Dim color_ = System.Drawing.ColorTranslator.FromHtml( _
                                            "#" & BDD.Chronique_PériodeTableAdapter.GetDataBy_Id(vitales(rr).Chronique_Periode).Item(0).Color_hex)

                                        CommandesDataGridView.Rows.Item(r).DefaultCellStyle.BackColor = color_
                                        ' CommandesDataGridView.Rows.Item(r).DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb((color_.R) + 128) Mod 256, (CInt(Color_tab(r).Back_color.G) + 128) Mod 256, (CInt(Color_tab(r).Back_color.B) + 128) Mod 256)
                                        ''''''''''''''''''
                                        Exit For
                                    End If
                                Next
                            End If
                        End If
                        Exit For
                    End If
                Next
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub CommandesDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles CommandesDataGridView.SelectionChanged
        If CommandesDataGridView.SelectedRows.Count > 1 Then
            Reception_Button.Visible = False
            Button2.Visible = True
        Else
            Reception_Button.Visible = True
            Button2.Visible = False
        End If
    End Sub

    Private Sub tri_commandes()
        Bandeau_info_bas_show("Recherche de commandes...")
        Try
            Dim année As String = Début_DateTimePicker1.Value.Year.ToString
            Dim mois As String = Début_DateTimePicker1.Value.Month.ToString
            If mois.Length < 2 Then mois = "0" & mois
            Dim jour As String = Début_DateTimePicker1.Value.Day.ToString
            If jour.Length < 2 Then jour = "0" & jour
            Dim date_debut As String = année & "-" & mois & "-" & jour & "T00:00:00"
            année = Fin_DateTimePicker2.Value.Year.ToString
            mois = Fin_DateTimePicker2.Value.Month.ToString
            If mois.Length < 2 Then mois = "0" & mois
            jour = Fin_DateTimePicker2.Value.Day.ToString
            If jour.Length < 2 Then jour = "0" & jour
            Dim date_fin As String = année & "-" & mois & "-" & jour & "T99:99:99"
            Commandes_PharmaML.CommandesTableAdapter.affichage_commande(CommandesDataGridView.DataSource, CInt(Top_X_TextBox.Text), Fournisseur_ComboBox.Text, date_debut, date_fin, Pas_de_date_CheckBox.Checked.ToString, Statut_ComboBox.Text)
        Catch ex As Exception
        End Try
   
        Bandeau_info_bas_hide()
    End Sub

    Private Sub Fournisseur_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Fournisseur_ComboBox.SelectedIndexChanged
        tri_commandes()
    End Sub

    Private Sub Statut_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Statut_ComboBox.SelectedIndexChanged
        tri_commandes()
    End Sub

    Private Sub Top_X_TextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles Top_X_TextBox.KeyDown
        If e.KeyCode = Keys.Enter Then
            Top_X_TextBox_Validated(Nothing, Nothing)
        End If
    End Sub


    Private Sub Top_X_TextBox_Validated(sender As Object, e As EventArgs) Handles Top_X_TextBox.Validated
        Try
            CInt(Top_X_TextBox.Text).ToString()
        Catch ex As Exception
            Top_X_TextBox.Text = "20"
        End Try
        tri_commandes()
    End Sub

    Private Sub Début_DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles Début_DateTimePicker1.ValueChanged
        Pas_de_date_CheckBox.Checked = False
        tri_commandes()
    End Sub

    Private Sub Fin_DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles Fin_DateTimePicker2.ValueChanged
        Pas_de_date_CheckBox.Checked = False
        tri_commandes()
    End Sub

    Private Sub Pas_de_date_CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles Pas_de_date_CheckBox.CheckedChanged
        tri_commandes()
    End Sub

    Private Sub Commandes_LignesDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Commandes_LignesDataGridView.CellClick
        If e.RowIndex < 0 Then Exit Sub

        Select Case e.ColumnIndex
            Case 3, 4
                Produits.Visible = True
                Dim code As String
                If Commandes_LignesDataGridView.Item(3, e.RowIndex).Value <> "" Then
                    code = Commandes_LignesDataGridView.Item(3, e.RowIndex).Value
                Else
                    code = Commandes_LignesDataGridView.Item(4, e.RowIndex).Value
                End If
                Produits.Recherche_TextBox.Text = code
                Produits.Recherche_TextBox_Validated(Nothing, Nothing)
                Produits.BringToFront()
                Produits.Select()
        End Select
    End Sub


    Private Sub Impression_Button_Click(sender As Object, e As EventArgs) Handles Impression_Button.Click
        If CommandesDataGridView.CurrentRow Is Nothing Then Exit Sub
        If CommandesDataGridView.CurrentRow.Index < 0 Then Exit Sub
        
        Dim parameters As String = ""
        If Not CommandesDataGridView.Item(0, CommandesDataGridView.CurrentRow.Index) Is Nothing Then
            parameters = "&Ref_Commande=" & CommandesDataGridView.Item(0, CommandesDataGridView.CurrentRow.Index).Value.ToString
        End If
        Web_browser.Visible = True
        Web_browser.BringToFront()
        Web_browser.WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fCommande%2fBon_Commande&rs:Command=Render" & parameters)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
            MyDataGridViewPrinter = New DataGridViewPrinter.DataGridViewPrinter() 'Reception_DataGridView, PrintDocument1, True, True, "Commandes", New Font("Ariel", 10, FontStyle.Regular), Color.Black, New Font("Ariel", 10, FontStyle.Regular), Color.Black, True)
            If MyDataGridViewPrinter.SetupThePrinting(CommandesDataGridView, "", MyDataGridViewPrinter_PrintDocument) = True Then
                MyDataGridViewPrinter_PrintDocument.Print()

            End If
        Try
            System.Threading.Thread.Sleep(1000)
            Process.Start(Application.StartupPath & "\temp.XPS")
        Catch ex As Exception
        End Try

    End Sub


    Private Sub MyDataGridViewPrinter_PrintDocument_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles MyDataGridViewPrinter_PrintDocument.PrintPage

        Dim more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If (more = True) Then
            e.HasMorePages = True
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        MyDataGridViewPrinter = New DataGridViewPrinter.DataGridViewPrinter()
        If MyDataGridViewPrinter.SetupThePrinting(Commandes_LignesDataGridView, "", MyDataGridViewPrinter_PrintDocument) = True Then
            MyDataGridViewPrinter_PrintDocument.Print()
        End If
        Try
            System.Threading.Thread.Sleep(1000)
            Process.Start(Application.StartupPath & "\temp.XPS")
        Catch ex As Exception
        End Try
    End Sub

 
End Class