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             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     /*!\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     /**!
00183      * \brief verify that all the amount are positive or null
00184      * otherwise throw a exception and the sum of amount + vat must
00185      * equal to the sum of all the amount of the current object
00186      * so you have to copy the object before computing anything and pass
00187      * it as parameter
00188      * \param compare with a object copied before computing, if null
00189      * there is no comparison
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         // Compute some operation to see if the computed amount are
00223         // correct
00224 
00225         //Test VAT
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         // Test VAT + perso
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         // TEST VAT + ND
00253         // Test VAT + perso
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         // TEST VAT + ND
00269         // Test VAT + perso
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         // TEST VAT + ND
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         // TEST VAT + ND
00307         // + Perso
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 // TEST VAT + ND
00327         // + Perso
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 }
 All Data Structures Namespaces Files Functions Variables Enumerations