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

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If CDec(Montant_TextBox1.Text) < CDec(crédit_TextBox2.Text) + CDec(Remise_TextBox3.Text) Then
            MyMsg_Box.Presentation("Officine !", "Crédit + Remise superieur au montant de la facture.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        Try
            VenteForm.Encaissement_partiel_montant_crédit = CDec(crédit_TextBox2.Text)
            VenteForm.Encaissement_partiel_montant_remise = CDec(Remise_TextBox3.Text)
        Catch ex As Exception
            VenteForm.Encaissement_partiel_montant_crédit = 0
            VenteForm.Encaissement_partiel_montant_remise = 0
        End Try
        Me.Close()
    End Sub

    Private Sub crédit_TextBox2_TextChanged(sender As Object, e As EventArgs) Handles crédit_TextBox2.TextChanged
        Replace(crédit_TextBox2.Text, ".", ",")
        If InStr(crédit_TextBox2.Text, ",") <> 0 Then
            If crédit_TextBox2.Text.Length > InStr(crédit_TextBox2.Text, ",") + 2 Then
                crédit_TextBox2.Text = Mid(crédit_TextBox2.Text, 1, InStr(crédit_TextBox2.Text, ",") + 2)
            End If
        End If
        Try
            CDec(crédit_TextBox2.Text).ToString()
        Catch ex As Exception
            crédit_TextBox2.Text = 0
        End Try
        If crédit_TextBox2.Text <> 0 Then
            Dim bénéficiaire As String = COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text
            'If bénéficiaire = "" Then
            '    bénéficiaire = Ventes_enchainées_Last_Bénéficiaire
            'End If
            If bénéficiaire = "" Then
                MyMsg_Box.Presentation("Officine !", "Vous devez selectionner un beneficiaire avant d'accorder un crédit.", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                crédit_TextBox2.Text = 0
            End If
        End If
    End Sub

    Private Sub Remise_TextBox3_TextChanged(sender As Object, e As EventArgs) Handles Remise_TextBox3.TextChanged
        Replace(Remise_TextBox3.Text, ".", ",")
        If InStr(Remise_TextBox3.Text, ",") <> 0 Then
            If Remise_TextBox3.Text.Length > InStr(Remise_TextBox3.Text, ",") + 2 Then
                Remise_TextBox3.Text = Mid(Remise_TextBox3.Text, 1, InStr(Remise_TextBox3.Text, ",") + 2)
            End If
        End If

        Try
            CDec(Remise_TextBox3.Text).ToString()
        Catch ex As Exception
            Remise_TextBox3.Text = 0
        End Try
    End Sub
End Class