Génération d'un tableau d'amortissement
Répondre à la discussion
Affichage des résultats 1 à 2 sur 2

Génération d'un tableau d'amortissement



  1. #1
    lordgodgiven

    Génération d'un tableau d'amortissement


    ------

    Bonjour,

    Je suis entrains de développer une application en php avec le framework laravel, cette application permet de gérer les emprunts des mutualistes. Je souhaite donc générer un tableau d'amortissement en fonction du montant de l'emprunt, le taux d'intérêt et la durée de remboursement. Le script que voici fonctionne mais le hic c'est qu'il génère lui même le tableau, mon souhait est que le calcul se fasse dans une classe service qui sera en suite utiliser par le contrôleur qui va à son tour envoyé les résultats sur une vue en blade mais je n'y arrive pas, un coup de main serais le bienvenu.

    Merci

    Code:
    <?php
    
    // Description
    //
    // This class will calculate and display an amortization schedule given the
    // amount of the loan, the interest rate of the loan, and the length in years
    // of the loan.
    //
    // Optionally, it will either display the entire schedule
    // or just the following calculated amounts:
    //    Total amount paid over the life of the loan.
    //    Total interest paid over the life of the loan.
    //    Total number of monthly payments.
    //    The monthly payment.
    //
    
    class amort{
     var $amount;      //amount of the loan
     var $rate;        //percentage rate of the loan
     var $years;        //number of years of the loan
     var $npmts;        //number of payments of the loan
     var $mrate;        //monthly interest rate
     var $tpmnt;        //total amount paid on the loan
     var $tint;         //total interest paid on the loan
     var $pmnt;         //monthly payment of the loan
    
    //amort is the constructor and sets up the variable values
    //using the three values passed to it.
    
    function amort($amount=0,$rate=0,$years=0){
     $this->amount=$amount;   //amount of the loan
     $this->rate=$rate;       //yearly interest rate in percent
     $this->years=$years;     //length of loan in years
    if($amount*$rate*$years > 0){
     $this->npmts=$years*12;  //number of payments (12 per year)
     $this->mrate=$rate/1200; //monthly interest rate
     $this->pmnt=$amount*($this->mrate/(1-pow(1+$this->mrate,-$this->npmts))); //monthly payment
     $this->tpmnt=$this->pmnt * $this->npmts;  //total amount paid at end of loan
     $this->tint=$this->tpmnt-$amount;         //total amount of interest paid at end of loan
    }else{
     $this->pmnt=0;
     $this->npmts=0;
     $this->mrate=0;
     $this->tpmnt=0;
     $this->tint=0;
    }
    }
    //returns the monthly payment in dolars (two decimal places)
    function payment(){
    return sprintf("%01.2f",$this->pmnt);
    }
    
    //returns the total amount paid at the end of the loan in dolars
    function totpayment(){
    return sprintf("%01.2f",$this->tpmnt);
    }
    
    //returns the total interest paid at the end of the loan in dolars
    function totinterest(){
    return sprintf("%01.2f",$this->tint);
    }
    
    //displays the form to enter amount, rate and length of loan in years
    function showForm(){
    print "<h1 align='center'>Amortization Schedule</h1>";
    print "<p align='center'> </p>";
    print "<form action='$PHP_SELF' method='GET' name='amort'>";
    print "<table border='1' width='100%' height='40'>";
    print "<tr><td width='33%' align='center' height='35'>";
    print "<dl><dt>Amount of Loan</dt><dt>(in dollars, no commas)</dt>";
    print "<dt><input type='text' name='amount' value='$this->amount' align='top' maxlength='6' size='20'>";
    print "</dt></dl></td>";
    print "<td width='33%' height='35' align='center'>";
    print "<dl><dt>Annual Interest Rate</dt><dt>(in percent)</dt>";
    print "<dt><input type='text' name='rate' value='$this->rate' align='top' maxlength='5' size='20'>";
    print "</dt></dl></td>";
    print "<td width='34%' height='35' align='center'>";
    print "<dl><dt>Length of Loan</dt><dt>(in years)</dt>";
    print "<dt><input type='text' name='years' value='$this->years' align='top' maxlength='2' size='20'>";
    print "</dt></dl></td></tr></table>";
    print "<p><input type='submit' value='Click to submit.' align='middle'></form>";
    }
    
    //if $show is false:
    //     displays:
    //               monthly payment
    //               number of payments in the loan
    //               total paid at end of loan
    //               total interest paid at end of loan
    //if $show is true:
    //    displays: everything for false case plus the amortization table
    
    function showTable($show){
    print "<table border='1' width='100%'>";
        print "<td width='25%' align='center'><dt>Total Payments</dt>";
          print "<dt>";
           print sprintf("$%01.2f",$this->tpmnt);
             print "</dt></td>";
        print "<td width='25%' align='center'><dt>Total Interest</dt>";
          print "<dt>";
           print sprintf("$%01.2f",$this->tint);
             print "</dt></td>";
    //print "</tr></table>";
    //print "<table border='1' width='100%'>";
    //  print "<tr>";
    //    print "<td width='33%' align='center'><dt>Monthly Interest Rate</dt>";
    //      print "<dt>";
    //       print sprintf("$%01.2f",$this->mrate*100);
    //         print "</dt></td>";
    
        print "<td width='25%' align='center'><dt>Number of Monthly Payments</dt>";
          print "<dt>";
           print $this->npmts;
             print "</dt></td>";
    
        print "<td width='25%' align='center'><dt>Monthly Payment</dt>";
          print "<dt>";
          print sprintf("$%01.2f",$this->pmnt);
             print "</dt>";
      print "</td></tr>";
    if($show){
      print "</table>";
      print "<table border='1' width='100%'><tr>";
      print "<td width='14%' align='center'>Payment Number</td>";
      print "<td width='14%' align='center'>Beginning Balance</td>";
      print "<td width='14%' align='center'>Interest Payment</td>";
      print "<td width='14%' align='center'>Principal Payment</td>";
      print "<td width='14%' align='center'>Ending Balance</td>";
      print "<td width='14%' align='center'>Cumulative Interest</td>";
      print "<td width='14%' align='center'>Cumulative Payments</td>";
     print "</tr>";
    
    $ebal = $this->amount;
    $ccint =0.0;
    $cpmnt = 0.0;
    
    for ($pnum = 1; $pnum <= $this->npmts; $pnum++){
      print "<tr>";
      print "<td width='14%' align='center'>$pnum</td>";
      $bbal = $ebal;
      print "<td width='14%' align='right'>$". sprintf("%01.2f",$bbal) . "</td>";
      $ipmnt = $bbal * $this->mrate;
      print "<td width='14%' align='right'>$" . sprintf("%01.2f",$ipmnt) . "</td>";
      $ppmnt = $this->pmnt - $ipmnt;
      print "<td width='14%' align='right'>$" . sprintf("%01.2f",$ppmnt) . "</td>";
      $ebal = $bbal - $ppmnt;
      print "<td width='14%' align='right'>$" . sprintf("%01.2f",$ebal) . "</td>";
      $ccint = $ccint + $ipmnt;
      print "<td width='14%' align='right'>$" . sprintf("%01.2f",$ccint) . "</td>";
      $cpmnt = $cpmnt + $this->pmnt;
      print "<td width='14%' align='right'>$" . sprintf("%01.2f",$cpmnt) . "</td>";
      print "</tr>";
     }
     print "</table>";
     }
    }
    }
    //End of amort class
    ?>

    -----

  2. #2
    umfred

    Re : Génération d'un tableau d'amortissement

    bah, ta classe t'affiche si tu demandes l'affichage via showTable ou showForm. Tu récupères tes infos le constructeur amort()
    Code:
    monAmort=new amort(montant, taux, duree)
    et tu utilises cette variable monAmort ensuite qui contient toutes les infos, et tu transposes ce qui se fait dans les fonctions d'affichage dans ta vue
    Sinon tu peux aussi remplacer les print pour remplir une variable de type chaine, qui contiendra la tableau (code html) à afficher
    Dernière modification par umfred ; 26/05/2021 à 14h35.

Discussions similaires

  1. Réponses: 5
    Dernier message: 11/02/2020, 16h53
  2. Réponses: 2
    Dernier message: 22/10/2017, 12h53
  3. pilule de 3ème génération et 4ème génération : danger ?
    Par haricotnain dans le forum Contraception, gynécologie, grossesse et sexualité
    Réponses: 4
    Dernier message: 31/01/2013, 07h42
  4. Génération bornée (Bounded Generation)
    Par inviteececec2d dans le forum Mathématiques du supérieur
    Réponses: 0
    Dernier message: 05/03/2009, 10h41
  5. Amortissement
    Par inviteb98e2557 dans le forum Physique
    Réponses: 2
    Dernier message: 04/05/2008, 13h39