noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_compute.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  * \brief to compute the different amount of an invoice of an expense,
00024  */
00025 
00026 /**
00027  * @brief  this class aims to compute different amount
00028  *
00029  * This class compute without decimal error the following amount
00030  * - vat
00031  * - amount without vat
00032  * - no deductible vat
00033  * - personal part
00034  * - no deductible amount
00035  * Nothing won't be saved to the database, this class will just
00036  * compute and complete the object
00037  * if you need to compute the vat and in another place all the
00038  * details you'll have to use the clone function
00039  private static $variable=array( 'amount'=>'amount',
00040                   'amount_vat'=>'amount_vat',
00041                   'amount_vat_rate'=>'amount_vat_rate',
00042                   'nd_vat'=>'nd_vat',
00043                   'nd_vat_rate'=>'nd_vat_rate',
00044                   'nd_ded_vat'=>'nd_ded_vat',
00045                   'nd_ded_vat_rate'=>'nd_ded_vat_rate',
00046                   'amount_nd'=>'amount_nd',
00047                   'amount_nd_rate'=>'amount_nd_rate',
00048                   'nd_vat_rate'=>'nd_vat_rate',
00049                   'amount_perso'=>'amount_perso',
00050                   'amount_perso_rate'=>'amount_perso_rate'                                );
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;                    // check that the compute
00072     // function are  called in the
00073     // good order
00074 
00075     var $check;                         // activate the check of the
00076     // order, valid value are
00077     // false or true
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     /*!\brief Compute the no deductible part of the amount, it reduce
00121      *also the vat
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         // the nd part for the vat
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         // correct the others amount
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     /*!\brief verify that all the amount are positive or null
00183      * otherwise throw a exception and the sum of amount + vat must
00184      * equal to the sum of all the amount of the current object
00185      * so you have to copy the object before computing anything and pass
00186      * it as parameter
00187      * \param compare with a object copied before computing, if null
00188      * there is no comparison
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         // Compute some operation to see if the computed amount are
00222         // correct
00223 
00224         //Test VAT
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         // Test VAT + perso
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         // TEST VAT + ND
00252         // Test VAT + perso
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         // TEST VAT + ND
00268         // Test VAT + perso
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         // TEST VAT + ND
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         // TEST VAT + ND
00306         // + Perso
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 // TEST VAT + ND
00326         // + Perso
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 }
 All Data Structures Namespaces Files Functions Variables Enumerations