Location: PHPKode > projects > MyPhpMoney > myphpmoney_2.0/public_html/simulate.php
<?php
/**
 * $Id: simulate.php 369 2003-04-06 12:52:20Z courou $
 *
 * Author     : courou&#64;users.sourceforge.net
 * Website    : http://allreponse.ath.cx
 *
 * Support    : http://sourceforge.net/projects/myphpmoney/
 * CVS        : http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/myphpmoney/
 */

/**
 * INCLUDE FILE
 */
     require_once 'config/settings.inc.php';

/**
 * ENTER IN THE SESSION OR BUILD THE LOGIN FORM
 */
     if (SQL_VerifSession()) {

/**
 * BUILD THE HEADER
 */
     XHTML_header(1,0,1,1,1,1,1,1);

/**
 * DEFINE VARIABLE GLOBALS
 */
      $Formule = '';
      $Val     = '';

/**
 * THE SWITCH POST
 */
     if (!isset($_POST['opt'])) $_POST['opt'] = '';
       switch($_POST['opt']) {

            ## THE TEMPLATES -- BUILD THE DEFAULT SIMULATION PAGE
            default :

                 $_MPM['BeginMixed'] = array
                 (
                           'SIMUL_S_TITRE'            => $_VAR['SIMUL_S_TITRE'],
                           'SIMUL_BODY'               => $_VAR['SIMUL_BODY'],
                           'SIMULATION'               => $_VAR['SIMULATION']
                 );
                 XHTML_SetFile('header_simulation.tpl',$_MPM['BeginMixed'],2);
        break;

            ## AFFICHE LE TABLEAU POUR FAIRE LA SIMULATION
            case $_VAR['SIMULATION'] :
            case $_VAR['CALCULE'] :

               if ($_POST['opt'] == $_VAR['CALCULE']) {

                  if ($_POST['MT'] == '')            $Formule = 1;
                  if ($_POST['taux'] == '')          $Formule = 2;
                  if ($_POST['duree'] == '')         $Formule = 3;
                  if ($_POST['REMB'] == '')          $Formule = 4;
                  if ($_POST['choix_duree'] == 1)    $Val = 12;
                  if ($_POST['choix_duree'] == 2)    $Val = 1;

                  $_POST['MT']   = str_replace(' ', '', $_POST['MT']);
                  $_POST['REMB'] = str_replace(' ', '', $_POST['REMB']);

                  switch($Formule) {

                    ## Recherche du Montant de l'emprunt ==> MT = REMB * (1 - (1 + i )^-n) / i
                    case '1' :
                         $i = ($_POST['taux'] / 100) / 12;
                         $n = $_POST['duree'] * $Val;
                         if ($i != 0 && $n != 0 && $_POST['REMB'] != 0) $_POST['MT'] = $tools->formatage(round($_POST['REMB'] * (1-pow((1+$i),-$n)) / $i)); else $_POST['MT'] = $_VAR['AMORTISS_REPONSE'];
                    break;

                    ## Recherche du Taux d'intêret
                    case '2' :
                         $n = $_POST['duree'] * $Val;
                         $b2 = 1; ## au lieu de prendre 25% (0.25) comme valeur maxi , je prend 100% (1.00)
                         if ($_POST['REMB'] != 0 && $_POST['MT'] != 0 && $n != 0) {
                              if ((($n * $_POST['REMB']) < $_POST['MT'])) $trier = 1;
                                 for ($t=0; $t < 10000000; $t++) {
                                     if ($t == 0) $b1 = 0.005; else $b1 = $c;
                                      $a1 = ((pow((1 + ($b1 / 12)),$n)) * (((($b1 * $_POST['MT']) / 12) - $_POST['REMB']) / $_POST['REMB']) + 1);
                                      $a2 = ((pow((1 + ($b2 / 12)),$n)) * (((($b2 * $_POST['MT']) / 12) - $_POST['REMB']) / $_POST['REMB']) + 1);
                                 if ($a2 <= 0) $trier = 1;
                                    if ($a1 != 0 && $a2 != 0) {
                                     $c =  (($a2 * $b1) - ($a1 * $b2)) / ($a2 - $a1);
                                     } else {
                                      $trier = 1;
                                       break;
                                     }
                                      if ((($c - $b1) < 0.00000001) && ($t > 9)) break;
                                  }

                              $i = ((100000 * $c) + 15) / 1000;
                              $_POST['taux'] = $i;

                              $cherche = explode('.',$i);
                              $reste   = substr($cherche[1], 0, 2);

                              if (isset($trier) && $trier == 1) {
                               $_POST['taux'] = $_VAR['AMORTISS_REPONSE'];
                              } else {
                               $_POST['taux'] = "$cherche[0].$reste";
                              }
                          } else {
                              $_POST['taux'] = $_VAR['AMORTISS_REPONSE'];
                          }
                    break;

                    ## Recherche de la duree ==> n = - log((REMB - (MT * i)) / REMB) / log(1+i)
                    case '3' :
                          $i = ($_POST['taux'] / 100) / 12;
                             if ($i != 0 && $_POST['REMB'] != 0 && $_POST['MT'] != 0) {
                              $_POST['duree'] = round(- log(($_POST['REMB'] - ($_POST['MT'] * $i)) / $_POST['REMB']) / log(1+$i));
                                 if ($_POST['duree'] % 12 == 0) {
                                  $_POST['duree'] = $_POST['duree'] / 12;
                                  $_POST['choix_duree'] = 1;
                                 } else {
                                  $_POST['choix_duree'] = 2;
                                 }
                              } else {
                               $_POST['duree'] = $_VAR['AMORTISS_REPONSE'];
                              }
                    break;

                    ## Recherche du Montant de l'emprunt ==> REMB = MT * i / 1 - (i+1)^-n
                    case '4' :
                          $i = ($_POST['taux'] / 100) / 12;
                          $n = $_POST['duree'] * $Val;

                          if ($i != 0 && $n != 0)
                                $_POST['REMB'] = $tools->formatage(($_POST['MT'] * $i)/(1-pow(($i+1),-$n)));
                          else
                                $_POST['REMB'] = $_VAR['AMORTISS_REPONSE'];
                    break;
                  } ## end of switch $Formule

                   $_MPM['BeginMixed'] = array
                   (
                             'AMORTISSEMENT'     => $_VAR['AMORTISSEMENT'],
                             'AMORTISSEMENT_URL' => urlencode($_VAR['AMORTISSEMENT']),
                             'L_LISTING'         => $_MPM['file_listing'],
                             'MT_STR'            => str_replace(' ','',$_POST['MT']),
                             'TAUX_STR'          => str_replace(' ','',$_POST['taux']),
                             'DUREE_STR'         => str_replace(' ','',$_POST['duree']),
                             'REMB_STR'          => str_replace(' ','',$_POST['REMB']),
                             'CHOIX_DUREE'       => $_POST['choix_duree']
                   );
                   $w = XHTML_SetFile('opt_simulation_1.tpl',$_MPM['BeginMixed'],1);
             } ## end of $_POST['opt'] == $_VAR['CALCULE']

       $_MPM['BeginMixed'] = array
       (
          'SIMUL_ENTER_TITLE'     => sprintf($_VAR['SIMUL_ENTER_TITLE'],$_MPM['name']),
          'SIMUL_ENTER_FORM_1'    => $_VAR['SIMUL_ENTER_FORM_1'],
          'SIMUL_ENTER_FORM_2'    => $_VAR['SIMUL_ENTER_FORM_2'],
          'SIMUL_ENTER_FORM_3'    => $_VAR['SIMUL_ENTER_FORM_3'],
          'SIMUL_ENTER_FORM_4'    => $_VAR['SIMUL_ENTER_FORM_4'],
          'SIMUL_ENTER_FORM_5'    => $_VAR['SIMUL_ENTER_FORM_5'],
          'SIMUL_ENTER_FORM_6'    => $_VAR['SIMUL_ENTER_FORM_6'],
          'CALCULE'               => $_VAR['CALCULE'],
          'MT_SIMUL'              => isset($_POST['MT']) ? $_POST['MT'] : '',
          'TAUX_SIMUL'            => isset($_POST['taux']) ? $_POST['taux'] : '',
          'DUREE_SIMUL'           => isset($_POST['duree']) ? $_POST['duree'] : '',
          'REMB_SIMUL'            => isset($_POST['REMB']) ? $_POST['REMB'] : '',
          'SELECTED_1'            => isset($_POST['choix_duree']) && $_POST['choix_duree'] == 1 ? ' selected="selected"' : '',
          'SELECTED_2'            => isset($_POST['choix_duree']) && $_POST['choix_duree'] == 2 ? ' selected="selected"' : '',
          'OPTION_SIMULATION'     => $_POST['opt'] == $_VAR['CALCULE'] ? $w : ''
       );
       XHTML_SetFile('form_simulation_2.tpl',$_MPM['BeginMixed'],2);

       break;
       } ## end of switch $_POST['opt']
     } else {
          XHTML_DisplayLogin();
     } ## end if (SQL_VerifSession())

/**
 * BUILD THE FOOTER
 */
     XHTML_footer();
Return current item: MyPhpMoney