00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 class Acc_Compute
00056 {
00057 private static $variable=array( 'amount'=>'amount',
00058 'amount_vat'=>'amount_vat',
00059 'amount_vat_rate'=>'amount_vat_rate',
00060 'nd_vat'=>'nd_vat',
00061 'nd_vat_rate'=>'nd_vat_rate',
00062 'nd_ded_vat'=>'nd_ded_vat',
00063 'nd_ded_vat_rate'=>'nd_ded_vat_rate',
00064 'amount_nd'=>'amount_nd',
00065 'amount_nd_rate'=>'amount_nd_rate',
00066 'nd_vat_rate'=>'nd_vat_rate',
00067 'amount_perso'=>'amount_perso',
00068 'amount_perso_rate'=>'amount_perso_rate'
00069 );
00070
00071 private $order;
00072
00073
00074
00075 var $check;
00076
00077
00078 function __construct ()
00079 {
00080 bcscale(4);
00081 foreach (self::$variable as $key=>$value) $this->$key=0;
00082 $this->order=0;
00083 $this->check=true;
00084 }
00085
00086 public function get_parameter($p_string)
00087 {
00088 if ( array_key_exists($p_string,self::$variable) )
00089 {
00090 $idx=self::$variable[$p_string];
00091 return $this->$idx;
00092 }
00093 else
00094 exit (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
00095 }
00096 public function set_parameter($p_string,$p_value)
00097 {
00098 if ( array_key_exists($p_string,self::$variable) )
00099 {
00100 $idx=self::$variable[$p_string];
00101 $this->$idx=$p_value;
00102 }
00103 else
00104 exit (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
00105
00106
00107 }
00108 public function get_info()
00109 {
00110 return var_export(self::$variable,true);
00111 }
00112
00113 function compute_vat()
00114 {
00115 if ( $this->check && $this->order != 0 ) throw new Exception ('ORDER NOT RESPECTED');
00116 $this->amount_vat=bcmul($this->amount,$this->amount_vat_rate);
00117 $this->amount_vat=round($this->amount_vat,2);
00118 $this->order=1;
00119 }
00120
00121
00122
00123 function compute_nd()
00124 {
00125 if ( $this->check && $this->order > 2 ) throw new Exception ('ORDER NOT RESPECTED');
00126
00127 $this->amount_nd=bcmul($this->amount,$this->amount_nd_rate);
00128 $this->amount_nd=bcdiv($this->amount_nd,100);
00129 $this->amount_nd=round($this->amount_nd,2);
00130
00131 $nd_vat=bcmul($this->amount_vat,$this->amount_nd_rate);
00132 $nd_vat=bcdiv($nd_vat,100);
00133 $nd_vat=round($nd_vat,2);
00134
00135 }
00136 function compute_nd_vat()
00137 {
00138 if ( $this->check && $this->order > 3 ) throw new Exception ('ORDER NOT RESPECTED');
00139 $this->order=4;
00140
00141 if ($this->amount_vat == 0 ) $this->compute_vat();
00142 $this->nd_vat=bcmul($this->amount_vat,$this->nd_vat_rate);
00143 $this->nd_vat=bcdiv($this->nd_vat,100);
00144 $this->nd_vat=round($this->nd_vat,2);
00145 }
00146
00147 function compute_ndded_vat()
00148 {
00149 if ( $this->check && $this->order > 4 ) throw new Exception ('ORDER NOT RESPECTED');
00150 $this->order=5;
00151
00152 if ($this->amount_vat == 0 ) $this->compute_vat();
00153 $this->nd_ded_vat=bcmul($this->amount_vat,$this->nd_ded_vat_rate);
00154 $this->nd_ded_vat=bcdiv($this->nd_ded_vat,100);
00155 $this->nd_ded_vat=round($this->nd_ded_vat,2);
00156 }
00157
00158 function compute_perso()
00159 {
00160 if ( $this->check && $this->order != 1 ) throw new Exception ('ORDER NOT RESPECTED');
00161 $this->order=2;
00162 if ( $this->amount == 0 ) return;
00163 $this->amount_perso=bcmul($this->amount,$this->amount_perso_rate);
00164 $this->amount_perso=bcdiv($this->amount_perso,100);
00165 $this->amount_perso=round($this->amount_perso,2);
00166
00167
00168
00169 }
00170 function correct()
00171 {
00172 $this->amount=bcsub($this->amount,$this->amount_perso);
00173
00174 $this->amount=bcsub($this->amount,$this->amount_nd);
00175 $this->amount_vat=bcsub($this->amount_vat,$this->nd_ded_vat);
00176 $this->amount_vat=round($this->amount_vat,2);
00177 $this->amount_vat=bcsub($this->amount_vat,$this->nd_vat);
00178 $this->amount_vat=round($this->amount_vat,2);
00179
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 function verify($p_obj=null)
00191 {
00192 foreach (self::$variable as $key=>$value)
00193 if ( $this->$value < 0 )
00194 throw new Exception (_("Montant invalide"));
00195
00196 if ( $p_obj != null )
00197 {
00198 $sum=0;
00199 foreach ( array( 'amount','amount_vat','amount_nd','nd_vat','amount_perso','nd_ded_vat') as $value)
00200 $sum=bcadd($sum,$this->$value);
00201 if ( $p_obj->amount_vat == 0 ) $p_obj->compute_vat();
00202 $cmp=bcadd($p_obj->amount,$p_obj->amount_vat);
00203 $diff=bcsub($sum,$cmp);
00204 if ( $diff != 0.0 )
00205 throw new Exception (_("ECHEC VERIFICATION : valeur totale = $sum valeur attendue = $cmp diff = $diff"));
00206 }
00207 }
00208 function display()
00209 {
00210 foreach (self::$variable as $key=>$value)
00211 {
00212 echo 'key '.$key.' Description '.$value.' value is '.$this->$key.'<br>';
00213 }
00214 }
00215 public static function test_me ()
00216 {
00217 $a=new Acc_Compute();
00218 echo $a->get_info();
00219 echo '<hr>';
00220
00221
00222
00223
00224
00225 $a->set_parameter('amount',1.23);
00226 $a->set_parameter('amount_vat_rate',0.21);
00227
00228 echo '<h1> Test VAT </h1>';
00229 echo '<h2> Data </h2>';
00230 $a->display();
00231
00232 echo '<h2> Result </h2>';
00233 $a->compute_vat();
00234 $a->display();
00235 $a->verify();
00236
00237 $a=new Acc_Compute();
00238 $a->set_parameter('amount',1.23);
00239 $a->set_parameter('amount_vat_rate',0.21);
00240 $a->set_parameter('amount_perso_rate',0.5);
00241 echo '<h1> Test VAT + Perso</h1>';
00242 echo '<h2> Data </h2>';
00243 $a->display();
00244 $b=clone $a;
00245 $a->compute_vat();
00246 $a->compute_perso();
00247 $a->correct();
00248 echo '<h2> Result </h2>';
00249 $a->display();
00250 $a->verify($b);
00251
00252
00253 $a=new Acc_Compute();
00254 $a->set_parameter('amount',1.23);
00255 $a->set_parameter('amount_vat_rate',0.21);
00256 $a->set_parameter('nd_vat_rate',0.5);
00257 $b=clone $a;
00258 echo '<h1> Test VAT + ND VAT</h1>';
00259 echo '<h2> Data </h2>';
00260 $a->display();
00261 $a->compute_vat();
00262 $a->compute_nd_vat();
00263 $a->correct();
00264 echo '<h2> Result </h2>';
00265 $a->display();
00266 $a->verify($b);
00267
00268
00269 $a=new Acc_Compute();
00270 $a->set_parameter('amount',1.23);
00271 $a->set_parameter('amount_vat_rate',0.21);
00272 $a->set_parameter('nd_vat_rate',0.5);
00273 $a->set_parameter('amount_perso_rate',0.5);
00274
00275 $b=clone $a;
00276 echo '<h1> Test VAT + ND VAT + perso</h1>';
00277 echo '<h2> Data </h2>';
00278 $a->display();
00279 $a->compute_vat();
00280 $a->compute_perso();
00281 $a->compute_nd_vat();
00282 $a->correct();
00283 echo '<h2> Result </h2>';
00284 $a->display();
00285 $a->verify($b);
00286
00287 $a=new Acc_Compute();
00288 $a->set_parameter('amount',1.23);
00289 $a->set_parameter('amount_vat_rate',0.21);
00290 $a->set_parameter('amount_nd_rate',0.5);
00291
00292 $b=clone $a;
00293 echo '<h1> Test VAT + ND </h1>';
00294 echo '<h2> Data </h2>';
00295 $a->display();
00296 $a->compute_vat();
00297 $a->compute_nd();
00298
00299 $a->compute_perso();
00300 $a->compute_nd_vat();
00301 $a->correct();
00302 echo '<h2> Result </h2>';
00303 $a->display();
00304 $a->verify($b);
00305
00306
00307 $a=new Acc_Compute();
00308 $a->set_parameter('amount',1.23);
00309 $a->set_parameter('amount_vat_rate',0.21);
00310 $a->set_parameter('amount_nd_rate',0.5);
00311 $a->set_parameter('amount_perso_rate',0.2857);
00312 $b=clone $a;
00313 echo '<h1> Test VAT + ND + Perso</h1>';
00314 echo '<h2> Data </h2>';
00315 $a->display();
00316 $a->compute_vat();
00317 $a->compute_nd();
00318
00319 $a->compute_perso();
00320 $a->compute_nd_vat();
00321 $a->correct();
00322 echo '<h2> Result </h2>';
00323 $a->display();
00324 $a->verify($b);
00325
00326
00327 $a=new Acc_Compute();
00328 $a->set_parameter('amount',1.23);
00329 $a->set_parameter('amount_vat_rate',0.21);
00330 $a->set_parameter('nd_ded_vat_rate',0.5);
00331
00332 $b=clone $a;
00333 echo '<h1> Test VAT + TVA ND DED</h1>';
00334 echo '<h2> Data </h2>';
00335 $a->display();
00336 $a->compute_vat();
00337 $a->compute_nd();
00338
00339 $a->compute_perso();
00340 $a->compute_nd_vat();
00341 $a->compute_ndded_vat();
00342 $a->correct();
00343 echo '<h2> Result </h2>';
00344 $a->display();
00345 $a->verify($b);
00346
00347
00348 }
00349 }