' 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 Reservation_Produit_Reception_Commande
    Public Structure Manquant
        'Dim code_prod As String
        Dim info As String
        Dim commande_ref As Integer
        Dim commande_NumLigne as integer
    End Structure
    Public Manquants As List(Of Manquant)

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        For r = 0 To DataGridView1.Rows.Count - 1
            Try
                CInt(DataGridView1.Item(Commande_Quantité.Index, r).Value).ToString()
            Catch ex As Exception
                Bandeau_info_bas_show("???" & DataGridView1.Item(Commande_Quantité.Index, r).Value & "???...")
                System.Threading.Thread.Sleep(3000)
                Bandeau_info_bas_hide()
                Exit Sub
            End Try
        Next
        Bandeau_info_bas_show("Enregistrement...")

        For r = 0 To DataGridView1.Rows.Count - 1
            Try
                Dim old_qtt_résérvée = 0
                Dim old_data = BDD.Produits_ReservésTableAdapter.GetData_ligne(CInt(DataGridView1.Item(Patient_Num.Index, r).Value), DataGridView1.Item(Produit_Désignation.Index, r).Value, DataGridView1.Item(Produit_CIP.Index, r).Value, DataGridView1.Item(Produit_EAN13.Index, r).Value, DataGridView1.Item(Produit_CAB.Index, r).Value)
                If old_data.Rows.Count > 0 Then
                    For rr = 0 To old_data.Rows.Count - 1
                        old_qtt_résérvée = old_qtt_résérvée + old_data(r).Quantité_Reservée
                    Next
                    BDD.Produits_ReservésTableAdapter.Delete_ligne(CInt(DataGridView1.Item(Patient_Num.Index, r).Value), DataGridView1.Item(Produit_Désignation.Index, r).Value, DataGridView1.Item(Produit_CIP.Index, r).Value, DataGridView1.Item(Produit_EAN13.Index, r).Value, DataGridView1.Item(Produit_CAB.Index, r).Value)
                End If
                BDD.Produits_ReservésTableAdapter.Insert(DataGridView1.Item(Produit_Désignation.Index, r).Value, DataGridView1.Item(Produit_CIP.Index, r).Value, DataGridView1.Item(Produit_EAN13.Index, r).Value, _
                DataGridView1.Item(Produit_CAB.Index, r).Value, CInt(DataGridView1.Item(Commande_Quantité.Index, r).Value), Now, DataGridView1.Item(Date_Renouvellement.Index, r).Value, Now, _
                CInt(DataGridView1.Item(Patient_Num.Index, r).Value), DataGridView1.Item(ALD.Index, r).Value)

                If DataGridView1.Item(Produit_EAN13.Index, r).Value <> "" Then
                    BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB((CInt(DataGridView1.Item(Commande_Quantité.Index, r).Value) * (-1)) + old_qtt_résérvée, DataGridView1.Item(Produit_EAN13.Index, r).Value, DataGridView1.Item(Produit_CAB.Index, r).Value)
                Else
                    BDD.ProduitsTableAdapter.UpdateStock_By_CIP_CAB((CInt(DataGridView1.Item(Commande_Quantité.Index, r).Value) * (-1)) + old_qtt_résérvée, DataGridView1.Item(Produit_CIP.Index, r).Value, DataGridView1.Item(Produit_CAB.Index, r).Value)
                End If
            Catch ex As Exception

            End Try
        Next
        Bandeau_info_bas_hide()
        Me.Close()
    End Sub


    Private Sub impr_LinkLabel_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles impr_LinkLabel.LinkClicked
        MyDataGridViewPrinter = New DataGridViewPrinter.DataGridViewPrinter()
        If MyDataGridViewPrinter.SetupThePrinting(DataGridView1, "", 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(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles MyDataGridViewPrinter_PrintDocument.PrintPage
        Dim more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If (more = True) Then
            e.HasMorePages = True
        End If
    End Sub
End Class