
Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Public Property CalculettePaiement As CalculettePaiementNOSP2
        Get
            Return CType(ViewState("CalculettePaiement"), CalculettePaiementNOSP2)
        End Get
        Set(value As CalculettePaiementNOSP2)
            ViewState("CalculettePaiement") = value
        End Set
    End Property

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            InitCalcul()
        End If
    End Sub

    Private Sub InitCalcul()
        Dim HasGestionCumul As Boolean = True
        Dim IsCumulSituationEditable As Boolean = True

        CalculettePaiement = New CalculettePaiementNOSP2(HasGestionCumul, IsCumulSituationEditable)
        CalculettePaiement.DesactivationDeductionRG()

        CalculettePaiement.CumulAnterieur = 48000D

        InitAffichageCalcul()
        UpdateAffichageCalcul()
    End Sub

    Private Sub InitAffichageCalcul()
        rntbCumulAnterieur.Visible = CalculettePaiement.IsGestionCumul
        lCumulSituationAnterieur.Visible = CalculettePaiement.IsGestionCumul

        TextBox5.Enabled = CalculettePaiement.IsCumulSituationEditable
        TextBox5.AutoPostBack = CalculettePaiement.IsCumulSituationEditable

        TextBox6.Enabled = Not CalculettePaiement.IsGestionCumul
        TextBox6.AutoPostBack = Not CalculettePaiement.IsGestionCumul

        TextBox5.Visible = CalculettePaiement.IsGestionCumul
        lCumulSituationEnCours.Visible = CalculettePaiement.IsGestionCumul

        If CalculettePaiement.Deductions IsNot Nothing Then
            TextBox2.Enabled = CalculettePaiement.Deductions.EditionProrata
            TextBox2.AutoPostBack = CalculettePaiement.Deductions.EditionProrata

            TextBox3.Enabled = CalculettePaiement.Deductions.EditionPenalite
            TextBox3.AutoPostBack = CalculettePaiement.Deductions.EditionPenalite

            TextBox4.Enabled = CalculettePaiement.Deductions.EditionAutre
            TextBox4.AutoPostBack = CalculettePaiement.Deductions.EditionAutre
        End If

        lTitreDeduction.Visible = CalculettePaiement.Deductions IsNot Nothing

        lDeductionRG.Visible = CalculettePaiement.Deductions IsNot Nothing
        lDeductionProrata.Visible = CalculettePaiement.Deductions IsNot Nothing
        lDeductionPenalite.Visible = CalculettePaiement.Deductions IsNot Nothing
        lDeductionAutre.Visible = CalculettePaiement.Deductions IsNot Nothing

        TextBox1.Visible = CalculettePaiement.Deductions IsNot Nothing
        TextBox2.Visible = CalculettePaiement.Deductions IsNot Nothing
        TextBox3.Visible = CalculettePaiement.Deductions IsNot Nothing
        TextBox4.Visible = CalculettePaiement.Deductions IsNot Nothing

        lTotalDeduction.Visible = CalculettePaiement.Deductions IsNot Nothing
        rntbTotalDeduction.Visible = CalculettePaiement.Deductions IsNot Nothing
    End Sub

    Private Sub UpdateAffichageCalcul()
        rntbCumulAnterieur.Value = CType(CalculettePaiement.CumulAnterieur, Double?)
        TextBox5.Value = CType(CalculettePaiement.CumulEnCours, Double?)
        TextBox6.Value = CType(CalculettePaiement.MontantDemande, Double?)

        If CalculettePaiement.Deductions IsNot Nothing Then
            TextBox1.DbValue = CalculettePaiement.Deductions.RG
            TextBox2.Value = CType(CalculettePaiement.Deductions.Prorata, Double?)
            TextBox3.Value = CType(CalculettePaiement.Deductions.Penalite, Double?)
            TextBox4.Value = CType(CalculettePaiement.Deductions.Autre, Double?)
            rntbTotalDeduction.Value = CType(CalculettePaiement.Deductions.Total, Double?)
        End If

        rntbMontantHTVA.Value = CType(CalculettePaiement.MontantHTVA, Double?)
        rntbMontantTVA.Value = CType(CalculettePaiement.MontantTVA, Double?)
        TextBox7.Value = CType(CalculettePaiement.MontantTTC, Double?)
        lMontantTVA.Text = $"Montant TVA (Taux : {CalculettePaiement.TauxTVA:#0.##} %) :"
    End Sub

#Region "Event Calculs"

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        CalculettePaiement.Deductions.RG = CType(TextBox1.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        CalculettePaiement.Deductions.Prorata = CType(TextBox2.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        CalculettePaiement.Deductions.Penalite = CType(TextBox3.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        CalculettePaiement.Deductions.Autre = CType(TextBox4.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
        CalculettePaiement.CumulEnCours = CType(TextBox5.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
        CalculettePaiement.MontantDemande = CType(TextBox6.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

    Protected Sub TextBox7_TextChanged(sender As Object, e As EventArgs) Handles TextBox7.TextChanged
        CalculettePaiement.MontantTTC = CType(TextBox7.Value, Decimal)
        CalculettePaiement.Calcul()
        UpdateAffichageCalcul()
    End Sub

#End Region

End Class
