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 throw new Exception (__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 throw new Exception (__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
00191 function verify($p_obj=null)
00192 {
00193 foreach (self::$variable as $key=>$value)
00194 if ( $this->$value < 0 )
00195 throw new Exception (_("Montant invalide"));
00196
00197 if ( $p_obj != null )
00198 {
00199 $sum=0;
00200 foreach ( array( 'amount','amount_vat','amount_nd','nd_vat','amount_perso','nd_ded_vat') as $value)
00201 $sum=bcadd($sum,$this->$value);
00202 if ( $p_obj->amount_vat == 0 ) $p_obj->compute_vat();
00203 $cmp=bcadd($p_obj->amount,$p_obj->amount_vat);
00204 $diff=bcsub($sum,$cmp);
00205 if ( $diff != 0.0 )
00206 throw new Exception (_("ECHEC VERIFICATION : valeur totale = $sum valeur attendue = $cmp diff = $diff"));
00207 }
00208 }
00209 function display()
00210 {
00211 foreach (self::$variable as $key=>$value)
00212 {
00213 echo 'key '.$key.' Description '.$value.' value is '.$this->$key.'<br>';
00214 }
00215 }
00216 public static function test_me ()
00217 {
00218 $a=new Acc_Compute();
00219 echo $a->get_info();
00220 echo '<hr>';
00221
00222
00223
00224
00225
00226 $a->set_parameter('amount',1.23);
00227 $a->set_parameter('amount_vat_rate',0.21);
00228
00229 echo '<h1> Test VAT </h1>';
00230 echo '<h2> Data </h2>';
00231 $a->display();
00232
00233 echo '<h2> Result </h2>';
00234 $a->compute_vat();
00235 $a->display();
00236 $a->verify();
00237
00238 $a=new Acc_Compute();
00239 $a->set_parameter('amount',1.23);
00240 $a->set_parameter('amount_vat_rate',0.21);
00241 $a->set_parameter('amount_perso_rate',0.5);
00242 echo '<h1> Test VAT + Perso</h1>';
00243 echo '<h2> Data </h2>';
00244 $a->display();
00245 $b=clone $a;
00246 $a->compute_vat();
00247 $a->compute_perso();
00248 $a->correct();
00249 echo '<h2> Result </h2>';
00250 $a->display();
00251 $a->verify($b);
00252
00253
00254 $a=new Acc_Compute();
00255 $a->set_parameter('amount',1.23);
00256 $a->set_parameter('amount_vat_rate',0.21);
00257 $a->set_parameter('nd_vat_rate',0.5);
00258 $b=clone $a;
00259 echo '<h1> Test VAT + ND VAT</h1>';
00260 echo '<h2> Data </h2>';
00261 $a->display();
00262 $a->compute_vat();
00263 $a->compute_nd_vat();
00264 $a->correct();
00265 echo '<h2> Result </h2>';
00266 $a->display();
00267 $a->verify($b);
00268
00269
00270 $a=new Acc_Compute();
00271 $a->set_parameter('amount',1.23);
00272 $a->set_parameter('amount_vat_rate',0.21);
00273 $a->set_parameter('nd_vat_rate',0.5);
00274 $a->set_parameter('amount_perso_rate',0.5);
00275
00276 $b=clone $a;
00277 echo '<h1> Test VAT + ND VAT + perso</h1>';
00278 echo '<h2> Data </h2>';
00279 $a->display();
00280 $a->compute_vat();
00281 $a->compute_perso();
00282 $a->compute_nd_vat();
00283 $a->correct();
00284 echo '<h2> Result </h2>';
00285 $a->display();
00286 $a->verify($b);
00287
00288 $a=new Acc_Compute();
00289 $a->set_parameter('amount',1.23);
00290 $a->set_parameter('amount_vat_rate',0.21);
00291 $a->set_parameter('amount_nd_rate',0.5);
00292
00293 $b=clone $a;
00294 echo '<h1> Test VAT + ND </h1>';
00295 echo '<h2> Data </h2>';
00296 $a->display();
00297 $a->compute_vat();
00298 $a->compute_nd();
00299
00300 $a->compute_perso();
00301 $a->compute_nd_vat();
00302 $a->correct();
00303 echo '<h2> Result </h2>';
00304 $a->display();
00305 $a->verify($b);
00306
00307
00308 $a=new Acc_Compute();
00309 $a->set_parameter('amount',1.23);
00310 $a->set_parameter('amount_vat_rate',0.21);
00311 $a->set_parameter('amount_nd_rate',0.5);
00312 $a->set_parameter('amount_perso_rate',0.2857);
00313 $b=clone $a;
00314 echo '<h1> Test VAT + ND + Perso</h1>';
00315 echo '<h2> Data </h2>';
00316 $a->display();
00317 $a->compute_vat();
00318 $a->compute_nd();
00319
00320 $a->compute_perso();
00321 $a->compute_nd_vat();
00322 $a->correct();
00323 echo '<h2> Result </h2>';
00324 $a->display();
00325 $a->verify($b);
00326
00327
00328 $a=new Acc_Compute();
00329 $a->set_parameter('amount',1.23);
00330 $a->set_parameter('amount_vat_rate',0.21);
00331 $a->set_parameter('nd_ded_vat_rate',0.5);
00332
00333 $b=clone $a;
00334 echo '<h1> Test VAT + TVA ND DED</h1>';
00335 echo '<h2> Data </h2>';
00336 $a->display();
00337 $a->compute_vat();
00338 $a->compute_nd();
00339
00340 $a->compute_perso();
00341 $a->compute_nd_vat();
00342 $a->compute_ndded_vat();
00343 $a->correct();
00344 echo '<h2> Result </h2>';
00345 $a->display();
00346 $a->verify($b);
00347
00348
00349 }
00350 }