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

    Private Sub Quantité_TextBox_TextChanged(sender As Object, e As EventArgs) Handles Quantité_TextBox.TextChanged
        If Not IsNumeric(Quantité_TextBox.Text) Then
            Quantité_TextBox.Text = 0
        End If
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click


        COMPTOIR_MAIN.DusTableAdapter.Update_du(COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_Note.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
                                                DateTimePicker1.Value, code_TextBox1.Text, Designation_TextBox2.Text, Quantité_TextBox.Text, Solder_CheckBox1.Checked, _
 COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_CIP7.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
 COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_EAN13.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
         COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_Désignation.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
 COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_Quantité_Du.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value,
  COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_Bénéficiare.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
   COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_NIR.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
         COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_Date_création.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value, _
          COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_num_FSE.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value,
           COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.Item(COMPTOIR_MAIN.Dossier_Patient_Dus_NumVD.Index, COMPTOIR_MAIN.Dossier_Patient_Dus_DataGridView.CurrentRow.Index).Value)

        If Solder_CheckBox1.Checked = True Then
            If Code_Label.Text <> code_TextBox1.Text Or Quantité_Label1.Text <> Quantité_TextBox.Text Then
                COMPTOIR_MAIN.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(Quantité_Label1.Text), Code_Label.Text, Code_Label.Text)
                COMPTOIR_MAIN.ProduitsTableAdapter.UpdateStock_By_CIP_CAB(Val(Quantité_TextBox.Text) * (-1), code_TextBox1.Text, code_TextBox1.Text)
            End If
        End If
        COMPTOIR_MAIN.Affiche_dus()
        Me.Close()


    End Sub


    Private Sub Solder_CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles Solder_CheckBox1.CheckedChanged
        If Solder_CheckBox1.Checked = True Then
            code_TextBox1.ReadOnly = True
            Designation_TextBox2.ReadOnly = True
            Quantité_TextBox.ReadOnly = True
            DateTimePicker1.Enabled = False

        End If
    End Sub

    Private Sub gestion_du_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.Location = New Point(COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Location.X + (COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Width / 2) - (Me.Width / 2), _
                                  COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Location.Y + (COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Height / 2) - (Me.Height / 2))

        If Solder_CheckBox1.Checked = True Then
            Solder_CheckBox1.Enabled = False
            Solder_CheckBox1.Visible = False
            PictureBox1.Enabled = False
            PictureBox1.Visible = False
        End If
    End Sub



    Private Sub code_TextBox1_Validated(sender As Object, e As EventArgs) Handles code_TextBox1.Validated
        If code_TextBox1.Text <> "" Then

            If Mid(code_TextBox1.Text, 1, 2) = "01" And Mid(code_TextBox1.Text, 17, 2) = "17" And Mid(code_TextBox1.Text, 25, 2) = "10" Then
                Dim string_searched As String = code_TextBox1.Text
                Dim num_lot As String = "*"
                Dim date_peremption As Date

                num_lot = Mid(string_searched, 27)

                Dim date_string = (Mid(string_searched, 23, 2) & "/" & Mid(string_searched, 21, 2) & "/" & "20" & Mid(string_searched, 19, 2))
                While InStr(date_string, "00/") <> 0
                    date_string = Replace(date_string, "00/", "01/")
                End While
                date_peremption = Convert.ToDateTime(date_string, Nothing)
                string_searched = Mid(string_searched, 4, 13)

                code_TextBox1.Text = COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(string_searched).Item(0).EAN13
                Designation_TextBox2.Text = COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(string_searched).Item(0).Designation
                If date_peremption.Subtract(Now).TotalDays < 100 Then
                    MyMsg_Box.Presentation("Officine !", "DATE DE PEREMPTION" + Chr(10) + date_peremption.ToLongDateString, False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                End If
                Dim alertes = DP_Alerte.DP_ALERTE_PRODUITTableAdapter.GetDataBy_CIP_Lot("@@@@@@@@@@@@", string_searched, num_lot)
                If alertes.Rows.Count > 0 Then

                    MyMsg_Box.Presentation("Officine !", "ATTENTION : le produit " & Designation_TextBox2.Text & " a fait l’objet d’une alerte produit. Veuillez prendre les dispositions appropriées en respectant les directives associées à cette alerte.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    For r = 0 To alertes.Rows.Count - 1
                        'Dim alerte_show As New DP_Alerte
                        Dim alerte_show As New DP_Alerte2
                        alerte_show.affichage_Msg(alertes.Item(r).MsgRef)
                        If alertes.Item(r).Vue = True Then
                            alerte_show.Button1.Text = "Annuler"
                        End If
                        alerte_show.ShowDialog()
                    Next

                End If

            Else
                If COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(code_TextBox1.Text).Rows.Count = 1 Then
                    Designation_TextBox2.Text = COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(code_TextBox1.Text).Item(0).Designation
                Else
                    MyMsg_Box.Presentation("Officine !", "il y a " & COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByCIP_or_EAN13_or_CAB(code_TextBox1.Text).Rows.Count.ToString & " produit(s) trouvé(s)", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    Designation_TextBox2.Text = ""
                End If



            End If
        End If

    End Sub
End Class