' 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 DETAIL_Ligne_Vente
    Public Mystring As String
    Public Numéro_Prestation_SV As Integer

    Private Sub DETAIL_Ligne_Vente_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    End Sub



    Private Sub DETAIL_Ligne_Vente_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For r = 0 To COMPTOIR_MAIN.Vente.CurrentRow.Index
            If UCase(COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteType.Index, r).Value) <> "VENTE DIRECTE" Then
                Numéro_Prestation_SV = Numéro_Prestation_SV + 1
            End If
        Next
        Label1.Text = Mystring
        For i = 0 To CurrentFactureVitale.PEC.Montants_Actes.Length - 1
            If CurrentFactureVitale.PEC.Montants_Actes(i).Num_Acte = Numéro_Prestation_SV Then
                PEC_TextBox.Text = CurrentFactureVitale.PEC.Montants_Actes(i).Montant.ToString
            End If
        Next
        For i = 0 To CurrentFactureVitale.G1840.Length - 1
            If CurrentFactureVitale.G1840(i).G1840C1 = Numéro_Prestation_SV Then
                If CurrentFactureVitale.G1840(i).G1840C3 <> "" Then
                    Entente_préalable_DateTimePicker.Value = Convert.ToDateTime(Mid(CurrentFactureVitale.G1840(i).G1840C3, 7, 2) + "/" + Mid(CurrentFactureVitale.G1840(i).G1840C3, 5, 2) + "/" + Mid(CurrentFactureVitale.G1840(i).G1840C3, 1, 4))
                End If
                For j = 0 To Entente_préalable_ComboBox.Items.Count - 1
                    If Mid(Entente_préalable_ComboBox.Items.Item(j).ToString, 1, 1) = CurrentFactureVitale.G1840(i).G1840C2 Then
                        Entente_préalable_ComboBox.SelectedIndex = j
                        Entente_préalable_ComboBox.Text = Entente_préalable_ComboBox.Items.Item(j).ToString
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
        If CurrentFactureVitale.G1740C7.Length > 1 Then
            Subtitution_ComboBox.Text = CurrentFactureVitale.G1740C7(Numéro_Prestation_SV)
        End If

        For i = 0 To CurrentFactureVitale.Forçage_Part_AMO.Length - 1
            If CurrentFactureVitale.Forçage_Part_AMO(i).Num_Acte = Numéro_Prestation_SV Then
                Forçage_Part_AMO_TextBox.Text = CurrentFactureVitale.Forçage_Part_AMO(i).Montant.ToString
            End If
        Next

        If CurrentFactureVitale.G1741.Length > 1 Then
            For i = 0 To CurrentFactureVitale.G1741.Length - 1
                If CurrentFactureVitale.G1741(i).G1741C1 = Numéro_Prestation_SV Then
                    Prest_Det_Lot_CodeCIP_DataGridView.Rows.Add()
                End If
            Next
            For i = 0 To CurrentFactureVitale.G1741.Length - 1
                If CurrentFactureVitale.G1741(i).G1741C1 = Numéro_Prestation_SV Then
                    Dim rang_du_lot As Integer = CurrentFactureVitale.G1741(i).G1741C2
                    Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C3.Index, rang_du_lot - 1).Value = CurrentFactureVitale.G1741(i).G1741C3
                    Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C4.Index, rang_du_lot - 1).Value = CurrentFactureVitale.G1741(i).G1741C4
                    Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C5.Index, rang_du_lot - 1).Value = CurrentFactureVitale.G1741(i).G1741C5
                    Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C6.Index, rang_du_lot - 1).Value = CurrentFactureVitale.G1741(i).G1741C6
                End If
            Next
        End If
    End Sub

    Private Sub PEC_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PEC_TextBox.TextChanged
        '______________________________________
        If PEC_TextBox.Text = "" Then
            For i = 0 To CurrentFactureVitale.PEC.Montants_Actes.Length - 1
                If CurrentFactureVitale.PEC.Montants_Actes(i).Num_Acte = Numéro_Prestation_SV Then
                    CurrentFactureVitale.PEC.Montants_Actes(i).Montant = Nothing
                    CurrentFactureVitale.PEC.Montants_Actes(i).Num_Acte = Nothing
                End If
            Next
            '______________________________________
        ElseIf Not IsNumeric(PEC_TextBox.Text) Then
            PEC_TextBox.Text = ""
            Beep()
            Exit Sub
            '______________________________________
        Else
            ReDim Preserve CurrentFactureVitale.PEC.Montants_Actes(COMPTOIR_MAIN.Vente.RowCount)
            CurrentFactureVitale.PEC.Montants_Actes(Numéro_Prestation_SV).Montant = Val(PEC_TextBox.Text)
            CurrentFactureVitale.PEC.Montants_Actes(Numéro_Prestation_SV).Num_Acte = Numéro_Prestation_SV
            CurrentFactureVitale.PEC.MontantGlobal = Nothing
            COMPTOIR_MAIN.MontantGlobalPriseEnchargeToolStripTextBox.Text = ""
        End If
    End Sub

    Private Sub Entente_préalable_ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Entente_préalable_ComboBox.SelectedIndexChanged
        ReDim Preserve CurrentFactureVitale.G1840(COMPTOIR_MAIN.Vente.RowCount)
        If Entente_préalable_ComboBox.Text = "" Then
            For i = 0 To CurrentFactureVitale.G1840.Length - 1
                If CurrentFactureVitale.G1840(i).G1840C1 = Numéro_Prestation_SV Then
                    CurrentFactureVitale.G1840(i).G1840C2 = ""
                    CurrentFactureVitale.G1840(i).G1840C3 = ""
                    Exit Sub
                End If
            Next
        Else
            CurrentFactureVitale.G1840(COMPTOIR_MAIN.Vente.CurrentRow.Index + 1).G1840C1 = Numéro_Prestation_SV
            CurrentFactureVitale.G1840(COMPTOIR_MAIN.Vente.CurrentRow.Index + 1).G1840C2 = Mid(Entente_préalable_ComboBox.Text, 1, 1)
            CurrentFactureVitale.G1840(COMPTOIR_MAIN.Vente.CurrentRow.Index + 1).G1840C3 = Format(Entente_préalable_DateTimePicker.Value, "yyyyMMdd") + "0000"
        End If
    End Sub

    Private Sub Entente_préalable_DateTimePicker_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Entente_préalable_DateTimePicker.ValueChanged
        ReDim Preserve CurrentFactureVitale.G1840(COMPTOIR_MAIN.Vente.RowCount)
        CurrentFactureVitale.G1840(Numéro_Prestation_SV).G1840C3 = Format(Entente_préalable_DateTimePicker.Value, "yyyyMMdd") + "0000"
    End Sub

    Private Sub Subtitution_ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Subtitution_ComboBox.SelectedIndexChanged
        ReDim Preserve CurrentFactureVitale.G1740C7(COMPTOIR_MAIN.Vente.RowCount)
        CurrentFactureVitale.G1740C7(Numéro_Prestation_SV) = Subtitution_ComboBox.Text
    End Sub

    Private Sub Forçage_Part_AMO_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Forçage_Part_AMO_TextBox.TextChanged
        '______________________________________
        If Forçage_Part_AMO_TextBox.Text = "" Then
            For i = 0 To CurrentFactureVitale.Forçage_Part_AMO.Length - 1
                If CurrentFactureVitale.Forçage_Part_AMO(i).Num_Acte = Numéro_Prestation_SV Then
                    CurrentFactureVitale.Forçage_Part_AMO(i).Montant = Nothing
                    CurrentFactureVitale.Forçage_Part_AMO(i).Num_Acte = Nothing
                End If
            Next
            '______________________________________
        ElseIf Not IsNumeric(Forçage_Part_AMO_TextBox.Text) Then
            Forçage_Part_AMO_TextBox.Text = ""
            Beep()
            Exit Sub
            '______________________________________
        Else
            ReDim Preserve CurrentFactureVitale.Forçage_Part_AMO(COMPTOIR_MAIN.Vente.RowCount)
            CurrentFactureVitale.Forçage_Part_AMO(Numéro_Prestation_SV).Montant = Val(Forçage_Part_AMO_TextBox.Text)
            CurrentFactureVitale.Forçage_Part_AMO(Numéro_Prestation_SV).Num_Acte = Numéro_Prestation_SV
        End If
    End Sub


    Private Sub Prest_Det_Lot_CodeCIP_DataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Prest_Det_Lot_CodeCIP_DataGridView.CellValidated
        ReDim Preserve CurrentFactureVitale.G1741((COMPTOIR_MAIN.Vente.RowCount - 1) * 30)
        Dim found As Boolean = False
        Dim indice_libre_G1740 As Integer = 0
        For i = 0 To CurrentFactureVitale.G1741.Length - 1
            If CurrentFactureVitale.G1741(i).G1741C1 = Numéro_Prestation_SV And CurrentFactureVitale.G1741(i).G1741C2 = Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index + 1 Then
                CurrentFactureVitale.G1741(i).G1741C3 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C3.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
                CurrentFactureVitale.G1741(i).G1741C4 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C4.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
                CurrentFactureVitale.G1741(i).G1741C5 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C5.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
                CurrentFactureVitale.G1741(i).G1741C6 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C6.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
                found = True
            End If
            If Val(CurrentFactureVitale.G1741(i).G1741C1) = 0 Then
                indice_libre_G1740 = i
            End If
        Next
        If found = False Then
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C1 = Numéro_Prestation_SV
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C2 = Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index + 1
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C3 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C3.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C4 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C4.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C5 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C5.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
            CurrentFactureVitale.G1741(indice_libre_G1740).G1741C6 = Prest_Det_Lot_CodeCIP_DataGridView.Item(G1741C6.Index, Prest_Det_Lot_CodeCIP_DataGridView.CurrentRow.Index).Value
        End If


    End Sub


    Private Sub Prest_Det_Lot_CodeCIP_DataGridView_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles Prest_Det_Lot_CodeCIP_DataGridView.RowsRemoved
        For j = 0 To CurrentFactureVitale.G1741.Length - 1
            If CurrentFactureVitale.G1741(j).G1741C1 = Numéro_Prestation_SV Then
                CurrentFactureVitale.G1741(j).G1741C1 = 0
                CurrentFactureVitale.G1741(j).G1741C2 = Nothing
                CurrentFactureVitale.G1741(j).G1741C3 = Nothing
                CurrentFactureVitale.G1741(j).G1741C4 = Nothing
                CurrentFactureVitale.G1741(j).G1741C5 = Nothing
                CurrentFactureVitale.G1741(j).G1741C6 = Nothing
            End If
        Next
        While Prest_Det_Lot_CodeCIP_DataGridView.RowCount > 1
            Prest_Det_Lot_CodeCIP_DataGridView.Rows.RemoveAt(0)
        End While
    End Sub

   
    Private Sub Prest_Det_Lot_CodeCIP_DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Prest_Det_Lot_CodeCIP_DataGridView.CellContentClick

    End Sub
End Class