1<?php
2/**
3 * $Id: simulate.php,v 1.12 2003/04/02 12:16:54 courou Exp $
4 *
5 * Author     : courou&#64;users.sourceforge.net
6 * Website    : http://allreponse.ath.cx
7 *
8 * Support    : http://sourceforge.net/projects/myphpmoney/
9 * CVS        : http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/myphpmoney/
10 */
11
12/**
13 * INCLUDE FILE
14 */
15     require_once 'config/settings.inc.php';
16
17/**
18 * ENTER IN THE SESSION OR BUILD THE LOGIN FORM
19 */
20     if (SQL_VerifSession()) {
21
22/**
23 * BUILD THE HEADER
24 */
25     XHTML_header(1,0,1,1,1,1,1,1);
26
27/**
28 * DEFINE VARIABLE GLOBALS
29 */
30      $Formule = '';
31      $Val     = '';
32
33/**
34 * THE SWITCH POST
35 */
36     if (!isset($_POST['opt'])) $_POST['opt'] = '';
37       switch($_POST['opt']) {
38
39            ## THE TEMPLATES -- BUILD THE DEFAULT SIMULATION PAGE
40            default :
41
42                 $_MPM['BeginMixed'] = array
43                 (
44                           'SIMUL_S_TITRE'            => $_VAR['SIMUL_S_TITRE'],
45                           'SIMUL_BODY'               => $_VAR['SIMUL_BODY'],
46                           'SIMULATION'               => $_VAR['SIMULATION']
47                 );
48                 XHTML_SetFile('header_simulation.tpl',$_MPM['BeginMixed'],2);
49        break;
50
51            ## AFFICHE LE TABLEAU POUR FAIRE LA SIMULATION
52            case $_VAR['SIMULATION'] :
53            case $_VAR['CALCULE'] :
54
55               if ($_POST['opt'] == $_VAR['CALCULE']) {
56
57                  if ($_POST['MT'] == '')            $Formule = 1;
58                  if ($_POST['taux'] == '')          $Formule = 2;
59                  if ($_POST['duree'] == '')         $Formule = 3;
60                  if ($_POST['REMB'] == '')          $Formule = 4;
61                  if ($_POST['choix_duree'] == 1)    $Val = 12;
62                  if ($_POST['choix_duree'] == 2)    $Val = 1;
63
64                  $_POST['MT']   = str_replace(' ', '', $_POST['MT']);
65                  $_POST['REMB'] = str_replace(' ', '', $_POST['REMB']);
66
67                  switch($Formule) {
68
69                    ## Recherche du Montant de l'emprunt ==> MT = REMB * (1 - (1 + i )^-n) / i
70                    case '1' :
71                         $i = ($_POST['taux'] / 100) / 12;
72                         $n = $_POST['duree'] * $Val;
73                         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'];
74                    break;
75
76                    ## Recherche du Taux d'int�ret
77                    case '2' :
78                         $n = $_POST['duree'] * $Val;
79                         $b2 = 1; ## au lieu de prendre 25% (0.25) comme valeur maxi , je prend 100% (1.00)
80                         if ($_POST['REMB'] != 0 && $_POST['MT'] != 0 && $n != 0) {
81                              if ((($n * $_POST['REMB']) < $_POST['MT'])) $trier = 1;
82                                 for ($t=0; $t < 10000000; $t++) {
83                                     if ($t == 0) $b1 = 0.005; else $b1 = $c;
84                                      $a1 = ((pow((1 + ($b1 / 12)),$n)) * (((($b1 * $_POST['MT']) / 12) - $_POST['REMB']) / $_POST['REMB']) + 1);
85                                      $a2 = ((pow((1 + ($b2 / 12)),$n)) * (((($b2 * $_POST['MT']) / 12) - $_POST['REMB']) / $_POST['REMB']) + 1);
86                                 if ($a2 <= 0) $trier = 1;
87                                    if ($a1 != 0 && $a2 != 0) {
88                                     $c =  (($a2 * $b1) - ($a1 * $b2)) / ($a2 - $a1);
89                                     } else {
90                                      $trier = 1;
91                                       break;
92                                     }
93                                      if ((($c - $b1) < 0.00000001) && ($t > 9)) break;
94                                  }
95
96                              $i = ((100000 * $c) + 15) / 1000;
97                              $_POST['taux'] = $i;
98
99                              $cherche = explode('.',$i);
100                              $reste   = substr($cherche[1], 0, 2);
101
102                              if (isset($trier) && $trier == 1) {
103                               $_POST['taux'] = $_VAR['AMORTISS_REPONSE'];
104                              } else {
105                               $_POST['taux'] = "$cherche[0].$reste";
106                              }
107                          } else {
108                              $_POST['taux'] = $_VAR['AMORTISS_REPONSE'];
109                          }
110                    break;
111
112                    ## Recherche de la duree ==> n = - log((REMB - (MT * i)) / REMB) / log(1+i)
113                    case '3' :
114                          $i = ($_POST['taux'] / 100) / 12;
115                             if ($i != 0 && $_POST['REMB'] != 0 && $_POST['MT'] != 0) {
116                              $_POST['duree'] = round(- log(($_POST['REMB'] - ($_POST['MT'] * $i)) / $_POST['REMB']) / log(1+$i));
117                                 if ($_POST['duree'] % 12 == 0) {
118                                  $_POST['duree'] = $_POST['duree'] / 12;
119                                  $_POST['choix_duree'] = 1;
120                                 } else {
121                                  $_POST['choix_duree'] = 2;
122                                 }
123                              } else {
124                               $_POST['duree'] = $_VAR['AMORTISS_REPONSE'];
125                              }
126                    break;
127
128                    ## Recherche du Montant de l'emprunt ==> REMB = MT * i / 1 - (i+1)^-n
129                    case '4' :
130                          $i = ($_POST['taux'] / 100) / 12;
131                          $n = $_POST['duree'] * $Val;
132
133                          if ($i != 0 && $n != 0)
134                                $_POST['REMB'] = $tools->formatage(($_POST['MT'] * $i)/(1-pow(($i+1),-$n)));
135                          else
136                                $_POST['REMB'] = $_VAR['AMORTISS_REPONSE'];
137                    break;
138                  } ## end of switch $Formule
139
140                   $_MPM['BeginMixed'] = array
141                   (
142                             'AMORTISSEMENT'     => $_VAR['AMORTISSEMENT'],
143                             'AMORTISSEMENT_URL' => urlencode($_VAR['AMORTISSEMENT']),
144                             'L_LISTING'         => $_MPM['file_listing'],
145                             'MT_STR'            => str_replace(' ','',$_POST['MT']),
146                             'TAUX_STR'          => str_replace(' ','',$_POST['taux']),
147                             'DUREE_STR'         => str_replace(' ','',$_POST['duree']),
148                             'REMB_STR'          => str_replace(' ','',$_POST['REMB']),
149                             'CHOIX_DUREE'       => $_POST['choix_duree']
150                   );
151                   $w = XHTML_SetFile('opt_simulation_1.tpl',$_MPM['BeginMixed'],1);
152             } ## end of $_POST['opt'] == $_VAR['CALCULE']
153
154       $_MPM['BeginMixed'] = array
155       (
156          'IMG_TXT_EUROS'         => (__DISPLAY_EURO__ == 2) ? $_MPM['lang_money_country'] : $_VAR['IMG_TXT_EUROS'],
157          'IMG_ALT_EUROS'         => (__DISPLAY_EURO__ == 2) ? $_MPM['lang_money_country'] : $_VAR['IMG_ALT_EUROS'],
158          'SIMUL_ENTER_TITLE'     => sprintf($_VAR['SIMUL_ENTER_TITLE'],$_MPM['name']),
159          'SIMUL_ENTER_FORM_1'    => $_VAR['SIMUL_ENTER_FORM_1'],
160          'SIMUL_ENTER_FORM_2'    => $_VAR['SIMUL_ENTER_FORM_2'],
161          'SIMUL_ENTER_FORM_3'    => $_VAR['SIMUL_ENTER_FORM_3'],
162          'SIMUL_ENTER_FORM_4'    => $_VAR['SIMUL_ENTER_FORM_4'],
163          'SIMUL_ENTER_FORM_5'    => $_VAR['SIMUL_ENTER_FORM_5'],
164          'SIMUL_ENTER_FORM_6'    => $_VAR['SIMUL_ENTER_FORM_6'],
165          'CALCULE'               => $_VAR['CALCULE'],
166          'MT_SIMUL'              => isset($_POST['MT']) ? $_POST['MT'] : '',
167          'TAUX_SIMUL'            => isset($_POST['taux']) ? $_POST['taux'] : '',
168          'DUREE_SIMUL'           => isset($_POST['duree']) ? $_POST['duree'] : '',
169          'REMB_SIMUL'            => isset($_POST['REMB']) ? $_POST['REMB'] : '',
170          'SELECTED_1'            => isset($_POST['choix_duree']) && $_POST['choix_duree'] == 1 ? ' selected="selected"' : '',
171          'SELECTED_2'            => isset($_POST['choix_duree']) && $_POST['choix_duree'] == 2 ? ' selected="selected"' : '',
172          'OPTION_SIMULATION'     => $_POST['opt'] == $_VAR['CALCULE'] ? $w : ''
173       );
174       XHTML_SetFile('form_simulation_2.tpl',$_MPM['BeginMixed'],2);
175
176       break;
177       } ## end of switch $_POST['opt']
178     } else {
179          XHTML_DisplayLogin();
180     } ## end if (SQL_VerifSession())
181
182/**
183 * BUILD THE FOOTER
184 */
185     XHTML_footer();