noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_acc_compute.php
Go to the documentation of this file.
1 <?php
2 /*
3  * This file is part of NOALYSS.
4  *
5  * NOALYSS is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * NOALYSS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with NOALYSS; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 // Copyright Author Dany De Bontridder danydb@aevalys.eu
21 
22 /*!\file
23  * \brief to compute the different amount of an invoice of an expense,
24  */
25 
26 /**
27  * @brief this class aims to compute different amount
28  *
29  * This class compute without decimal error the following amount
30  * - vat
31  * - amount without vat
32  * - no deductible vat
33  * - personal part
34  * - no deductible amount
35  * Nothing won't be saved to the database, this class will just
36  * compute and complete the object
37  * if you need to compute the vat and in another place all the
38  * details you'll have to use the clone function
39  private static $variable=array( 'amount'=>'amount',
40  'amount_vat'=>'amount_vat',
41  'amount_vat_rate'=>'amount_vat_rate',
42  'nd_vat'=>'nd_vat',
43  'nd_vat_rate'=>'nd_vat_rate',
44  'nd_ded_vat'=>'nd_ded_vat',
45  'nd_ded_vat_rate'=>'nd_ded_vat_rate',
46  'amount_nd'=>'amount_nd',
47  'amount_nd_rate'=>'amount_nd_rate',
48  'nd_vat_rate'=>'nd_vat_rate',
49  'amount_perso'=>'amount_perso',
50  'amount_perso_rate'=>'amount_perso_rate' );
51 
52  */
53 
54 
56 {
57  private static $variable=array( 'amount'=>'amount',
58  'amount_vat'=>'amount_vat',
59  'amount_vat_rate'=>'amount_vat_rate',
60  'nd_vat'=>'nd_vat',
61  'nd_vat_rate'=>'nd_vat_rate',
62  'nd_ded_vat'=>'nd_ded_vat',
63  'nd_ded_vat_rate'=>'nd_ded_vat_rate',
64  'amount_nd'=>'amount_nd',
65  'amount_nd_rate'=>'amount_nd_rate',
66  'nd_vat_rate'=>'nd_vat_rate',
67  'amount_perso'=>'amount_perso',
68  'amount_perso_rate'=>'amount_perso_rate'
69  );
70 
71  private $order; // check that the compute
72  // function are called in the
73  // good order
74 
75  var $check; // activate the check of the
76  // order, valid value are
77  // false or true
78  function __construct ()
79  {
80  bcscale(4);
81  foreach (self::$variable as $key=>$value) $this->$key=0;
82  $this->order=0;
83  $this->check=true;
84  }
85 
86  public function get_parameter($p_string)
87  {
88  if ( array_key_exists($p_string,self::$variable) )
89  {
90  $idx=self::$variable[$p_string];
91  return $this->$idx;
92  }
93  else
94  throw new Exception (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
95  }
96  public function set_parameter($p_string,$p_value)
97  {
98  if ( array_key_exists($p_string,self::$variable) )
99  {
100  $idx=self::$variable[$p_string];
101  $this->$idx=$p_value;
102  }
103  else
104  throw new Exception (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
105 
106 
107  }
108  public function get_info()
109  {
110  return var_export(self::$variable,true);
111  }
112 
113  function compute_vat()
114  {
115  if ( $this->check && $this->order != 0 ) throw new Exception ('ORDER NOT RESPECTED');
116  $this->amount_vat=bcmul($this->amount,$this->amount_vat_rate);
117  $this->amount_vat=round($this->amount_vat,2);
118  $this->order=1;
119  }
120  /*!\brief Compute the no deductible part of the amount, it reduce
121  *also the vat
122  */
123  function compute_nd()
124  {
125  if ( $this->check && $this->order > 2 ) throw new Exception ('ORDER NOT RESPECTED');
126 
127  $this->amount_nd=bcmul($this->amount,$this->amount_nd_rate);
128  $this->amount_nd=bcdiv($this->amount_nd,100);
129  $this->amount_nd=round($this->amount_nd,2);
130  // the nd part for the vat
131  $nd_vat=bcmul($this->amount_vat,$this->amount_nd_rate);
132  $nd_vat=bcdiv($nd_vat,100);
133  $nd_vat=round($nd_vat,2);
134 
135  }
136  function compute_nd_vat()
137  {
138  if ( $this->check && $this->order > 3 ) throw new Exception ('ORDER NOT RESPECTED');
139  $this->order=4;
140 
141  if ($this->amount_vat == 0 ) $this->compute_vat();
142  $this->nd_vat=bcmul($this->amount_vat,$this->nd_vat_rate);
143  $this->nd_vat=bcdiv($this->nd_vat,100);
144  $this->nd_vat=round($this->nd_vat,2);
145  }
146 
147  function compute_ndded_vat()
148  {
149  if ( $this->check && $this->order > 4 ) throw new Exception ('ORDER NOT RESPECTED');
150  $this->order=5;
151 
152  if ($this->amount_vat == 0 ) $this->compute_vat();
153  $this->nd_ded_vat=bcmul($this->amount_vat,$this->nd_ded_vat_rate);
154  $this->nd_ded_vat=bcdiv($this->nd_ded_vat,100);
155  $this->nd_ded_vat=round($this->nd_ded_vat,2);
156  }
157 
158  function compute_perso()
159  {
160  if ( $this->check && $this->order != 1 ) throw new Exception ('ORDER NOT RESPECTED');
161  $this->order=2;
162  if ( $this->amount == 0 ) return;
163  $this->amount_perso=bcmul($this->amount,$this->amount_perso_rate);
164  $this->amount_perso=bcdiv($this->amount_perso,100);
165  $this->amount_perso=round($this->amount_perso,2);
166 
167 
168 
169  }
170  function correct()
171  {
172  $this->amount=bcsub($this->amount,$this->amount_perso);
173  // correct the others amount
174  $this->amount=bcsub($this->amount,$this->amount_nd);
175  $this->amount_vat=bcsub($this->amount_vat,$this->nd_ded_vat);
176  $this->amount_vat=round($this->amount_vat,2);
177  $this->amount_vat=bcsub($this->amount_vat,$this->nd_vat);
178  $this->amount_vat=round($this->amount_vat,2);
179 
180  }
181 
182  /**!
183  * \brief verify that all the amount are positive or null
184  * otherwise throw a exception and the sum of amount + vat must
185  * equal to the sum of all the amount of the current object
186  * so you have to copy the object before computing anything and pass
187  * it as parameter
188  * \param compare with a object copied before computing, if null
189  * there is no comparison
190  */
191  function verify($p_obj=null)
192  {
193  foreach (self::$variable as $key=>$value)
194  if ( $this->$value < 0 )
195  throw new Exception (_("Montant invalide"));
196 
197  if ( $p_obj != null )
198  {
199  $sum=0;
200  foreach ( array( 'amount','amount_vat','amount_nd','nd_vat','amount_perso','nd_ded_vat') as $value)
201  $sum=bcadd($sum,$this->$value);
202  if ( $p_obj->amount_vat == 0 ) $p_obj->compute_vat();
203  $cmp=bcadd($p_obj->amount,$p_obj->amount_vat);
204  $diff=bcsub($sum,$cmp);
205  if ( $diff != 0.0 )
206  throw new Exception (_("ECHEC VERIFICATION : valeur totale = $sum valeur attendue = $cmp diff = $diff"));
207  }
208  }
209  function display()
210  {
211  foreach (self::$variable as $key=>$value)
212  {
213  echo 'key '.$key.' Description '.$value.' value is '.$this->$key.'<br>';
214  }
215  }
216  public static function test_me ()
217  {
218  $a=new Acc_Compute();
219  echo $a->get_info();
220  echo '<hr>';
221 
222  // Compute some operation to see if the computed amount are
223  // correct
224 
225  //Test VAT
226  $a->set_parameter('amount',1.23);
227  $a->set_parameter('amount_vat_rate',0.21);
228 
229  echo '<h1> Test VAT </h1>';
230  echo '<h2> Data </h2>';
231  $a->display();
232 
233  echo '<h2> Result </h2>';
234  $a->compute_vat();
235  $a->display();
236  $a->verify();
237  // Test VAT + perso
238  $a=new Acc_Compute();
239  $a->set_parameter('amount',1.23);
240  $a->set_parameter('amount_vat_rate',0.21);
241  $a->set_parameter('amount_perso_rate',0.5);
242  echo '<h1> Test VAT + Perso</h1>';
243  echo '<h2> Data </h2>';
244  $a->display();
245  $b=clone $a;
246  $a->compute_vat();
247  $a->compute_perso();
248  $a->correct();
249  echo '<h2> Result </h2>';
250  $a->display();
251  $a->verify($b);
252  // TEST VAT + ND
253  // Test VAT + perso
254  $a=new Acc_Compute();
255  $a->set_parameter('amount',1.23);
256  $a->set_parameter('amount_vat_rate',0.21);
257  $a->set_parameter('nd_vat_rate',0.5);
258  $b=clone $a;
259  echo '<h1> Test VAT + ND VAT</h1>';
260  echo '<h2> Data </h2>';
261  $a->display();
262  $a->compute_vat();
263  $a->compute_nd_vat();
264  $a->correct();
265  echo '<h2> Result </h2>';
266  $a->display();
267  $a->verify($b);
268  // TEST VAT + ND
269  // Test VAT + perso
270  $a=new Acc_Compute();
271  $a->set_parameter('amount',1.23);
272  $a->set_parameter('amount_vat_rate',0.21);
273  $a->set_parameter('nd_vat_rate',0.5);
274  $a->set_parameter('amount_perso_rate',0.5);
275 
276  $b=clone $a;
277  echo '<h1> Test VAT + ND VAT + perso</h1>';
278  echo '<h2> Data </h2>';
279  $a->display();
280  $a->compute_vat();
281  $a->compute_perso();
282  $a->compute_nd_vat();
283  $a->correct();
284  echo '<h2> Result </h2>';
285  $a->display();
286  $a->verify($b);
287  // TEST VAT + ND
288  $a=new Acc_Compute();
289  $a->set_parameter('amount',1.23);
290  $a->set_parameter('amount_vat_rate',0.21);
291  $a->set_parameter('amount_nd_rate',0.5);
292 
293  $b=clone $a;
294  echo '<h1> Test VAT + ND </h1>';
295  echo '<h2> Data </h2>';
296  $a->display();
297  $a->compute_vat();
298  $a->compute_nd();
299 
300  $a->compute_perso();
301  $a->compute_nd_vat();
302  $a->correct();
303  echo '<h2> Result </h2>';
304  $a->display();
305  $a->verify($b);
306  // TEST VAT + ND
307  // + Perso
308  $a=new Acc_Compute();
309  $a->set_parameter('amount',1.23);
310  $a->set_parameter('amount_vat_rate',0.21);
311  $a->set_parameter('amount_nd_rate',0.5);
312  $a->set_parameter('amount_perso_rate',0.2857);
313  $b=clone $a;
314  echo '<h1> Test VAT + ND + Perso</h1>';
315  echo '<h2> Data </h2>';
316  $a->display();
317  $a->compute_vat();
318  $a->compute_nd();
319 
320  $a->compute_perso();
321  $a->compute_nd_vat();
322  $a->correct();
323  echo '<h2> Result </h2>';
324  $a->display();
325  $a->verify($b);
326 // TEST VAT + ND
327  // + Perso
328  $a=new Acc_Compute();
329  $a->set_parameter('amount',1.23);
330  $a->set_parameter('amount_vat_rate',0.21);
331  $a->set_parameter('nd_ded_vat_rate',0.5);
332 
333  $b=clone $a;
334  echo '<h1> Test VAT + TVA ND DED</h1>';
335  echo '<h2> Data </h2>';
336  $a->display();
337  $a->compute_vat();
338  $a->compute_nd();
339 
340  $a->compute_perso();
341  $a->compute_nd_vat();
342  $a->compute_ndded_vat();
343  $a->correct();
344  echo '<h2> Result </h2>';
345  $a->display();
346  $a->verify($b);
347 
348 
349  }
350 }
$value
$idx
verify($p_obj=null)
!
function clone(object)
get_parameter($p_string)
set_parameter($p_string, $p_value)
this class aims to compute different amount
compute_nd()
Compute the no deductible part of the amount, it reduce also the vat.