' 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 Création_produits_LPPR

    Private Sub Code_LPP_TextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles Code_LPP_TextBox.KeyDown
        If e.KeyCode = Keys.Enter Then
            Code_LPP_TextBox_Validated(Nothing, Nothing)
        End If
    End Sub



    Private Sub Code_LPP_TextBox_Validated(sender As Object, e As EventArgs) Handles Code_LPP_TextBox.Validated
        If Code_LPP_TextBox.Text = "" Then Exit Sub

        Dim LPP_ = COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(Code_LPP_TextBox.Text, CInt(Format(Now, "yyyyMMdd")))
        If LPP_.Rows.Count = 0 Then

            Dim Vidal_LpprService As New Vidal_LpprService.LpprService
            Vidal_LpprService.Timeout = 60000 '3000
            Dim lpp_vidal = Vidal_LpprService.searchByCode(Code_LPP_TextBox.Text)
            If Not lpp_vidal Is Nothing And lpp_vidal.Count > 0 Then
                Dim prixlpp = (lpp_vidal(0).refundBase * 100).ToString
                While prixlpp.Length < 10
                    prixlpp = "0" & prixlpp
                End While
                Dim Type_prestation As String
                Select Case lpp_vidal(0).service.Value
                    Case officine.Vidal_LpprService.LpprServiceType.BUYING
                        Type_prestation = "A"
                    Case officine.Vidal_LpprService.LpprServiceType.DELIVERY
                        Type_prestation = "V"
                    Case officine.Vidal_LpprService.LpprServiceType.MAINTENANCE
                        Type_prestation = "E"
                    Case officine.Vidal_LpprService.LpprServiceType.RENTING
                        Type_prestation = "L"
                    Case officine.Vidal_LpprService.LpprServiceType.REPAIR
                        Type_prestation = "R"
                    Case officine.Vidal_LpprService.LpprServiceType.SERVICE
                        Type_prestation = "S"
                    Case Else
                        Type_prestation = "P"
                End Select
                COMPTOIR_MAIN.LPPTableAdapter.Insert(lpp_vidal(0).code, (lpp_vidal(0).refundBase * 100).ToString, "99999999", "00000000", False, lpp_vidal(0).actCode.ToString, "000", "", "", False, False, lpp_vidal(0).name, "", "", prixlpp, "000", prixlpp, Type_prestation)
                LPP_ = COMPTOIR_MAIN.LPPTableAdapter.GetData_CodeLPP_Date(Code_LPP_TextBox.Text, CInt(Format(Now, "yyyyMMdd")))
                Code_LPP_TextBox.ReadOnly = True
                Type_Service_TextBox.Text = LPP_.Item(0).Type_prestation
                Prestation_TextBox.Text = LPP_.Item(0).Prestation
                Designation_LPP_RichTextBox.Text = LPP_.Item(0).Libellé
                Tarif_LPP_TextBox.Text = (CDec(LPP_.Item(0).PrixLPP) / 100).ToString
                Quantité_TextBox.Text = "1"

            Else

                MyMsg_Box.Presentation("Officine", "Code LPP inconnu." & vbCrLf & "Création du code LPP?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                    LPP.ShowDialog()
                End If
                Exit Sub
            End If
        Else
            Code_LPP_TextBox.ReadOnly = True
            Type_Service_TextBox.Text = LPP_.Item(0).Type_prestation
            Prestation_TextBox.Text = LPP_.Item(0).Prestation
            Designation_LPP_RichTextBox.Text = LPP_.Item(0).Libellé
            Tarif_LPP_TextBox.Text = (CDec(LPP_.Item(0).PrixLPP) / 100).ToString
            Quantité_TextBox.Text = "1"
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Code_LPP_TextBox.Text = "" Then Exit Sub

        If COMPTOIR_MAIN.LPPTableAdapter.GetDataBy_CodeLPP(Code_LPP_TextBox.Text).Rows.Count = 0 Then
            MyMsg_Box.Presentation("Officine", "Code LPP inconnu", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        If COMPTOIR_MAIN.ProduitsTableAdapter.GetDataByInfo("@@@@@@@@@@@@", "@@@@@@@@@@@@@@@", Produit_TextBox.Text).Rows.Count > 0 Then
            MyMsg_Box.Presentation("Officine", "Désignation réservée", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        Try
            CDec(Replace(PVTTC_TextBox.Text, ".", ",")).ToString()
            CDec(Replace(TVA_TextBox.Text, ".", ",")).ToString()
            CInt(Quantité_TextBox.Text).ToString()
        Catch ex As Exception
            MyMsg_Box.Presentation("Officine", "Quantité ou PV TTC ou TVA erronée", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End Try

        Produits.ProduitsTableAdapter.Insert("", "", "", Produit_TextBox.Text, CDec(Replace(PVTTC_TextBox.Text, ".", ",")).ToString(), CDec(Replace(TVA_TextBox.Text, ".", ",")).ToString(), 0, _
                                             Code_LPP_TextBox.Text, "", "", Now, False, "", Prestation_TextBox.Text, "", "", CInt(Quantité_TextBox.Text), Type_Service_TextBox.Text, 100, False, 0, "", "", "", 0, _
                                             False, "", False, "", Now, False, Now, False, "", 0, False, "", False, False, 0, 0, "", "", False, True, True, "", "", 0, True, True, False, New Date(3000, 1, 1))
        Me.Close()

    End Sub



 
End Class