noalyss Version-9
acc_compute.class.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 'amount_currency'=>'amount_currency',
70 'amount_vat_currency'=>'amount_vat_currency',
71 'currency_rate'=>'currency_rate'
72 );
73
74 private $order; // check that the compute
75 // function are called in the
76 // good order
77
78 var $check; // activate the check of the
79 // order, valid value are
80 // false or true
81 function __construct ()
82 {
83 bcscale(4);
84 foreach (self::$variable as $key=>$value) $this->$key=0;
85 $this->order=0;
86 $this->check=true;
87 }
88
89 function convert_euro()
90 {
91 $local_amount=$this->amount;
92 $this->amount=bcdiv($this->amount,$this->currency_rate,6);
93 $this->amount_currency=$local_amount;
94 }
96 {
97 $local_amount=$this->amount_vat;
98 $this->amount_vat=bcdiv($this->amount_vat,$this->currency_rate,6);
99 $this->amount_vat_currency=$local_amount;
100 }
101 public function get_parameter($p_string)
102 {
103 if ( array_key_exists($p_string,self::$variable) )
104 {
105 $idx=self::$variable[$p_string];
106 return $this->$idx;
107 }
108 else
109 throw new Exception (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
110 }
111 public function set_parameter($p_string,$p_value)
112 {
113 if ( array_key_exists($p_string,self::$variable) )
114 {
115 $idx=self::$variable[$p_string];
116 $this->$idx=$p_value;
117 }
118 else
119 throw new Exception (__FILE__.":".__LINE__._('Erreur attribut inexistant'));
120
121
122 }
123 public function get_info()
124 {
125 return var_export(self::$variable,true);
126 }
127 function compute_vat()
128 {
129 if ( $this->check && $this->order != 0 ) throw new Exception ('ORDER NOT RESPECTED');
130 $this->amount_vat=bcmul($this->amount,$this->amount_vat_rate);
131 $this->amount_vat=round($this->amount_vat,2);
132 $this->amount_vat_currency=bcmul($this->amount_vat,$this->currency_rate);
133 $this->order=1;
134 }
135 /*!\brief Compute the no deductible part of the amount, it reduce
136 *also the vat
137 */
138 function compute_nd()
139 {
140 if ( $this->check && $this->order > 2 ) throw new Exception ('ORDER NOT RESPECTED');
141
142 $this->amount_nd=bcmul($this->amount,$this->amount_nd_rate);
143 $this->amount_nd=bcdiv($this->amount_nd,100);
144 $this->amount_nd=round($this->amount_nd,2);
145 // the nd part for the vat
146 $nd_vat=bcmul($this->amount_vat,$this->amount_nd_rate);
147 $nd_vat=bcdiv($nd_vat,100);
148 $nd_vat=round($nd_vat,2);
149
150 }
151 function compute_nd_vat()
152 {
153 if ( $this->check && $this->order > 3 ) throw new Exception ('ORDER NOT RESPECTED');
154 $this->order=4;
155
156 if ($this->amount_vat == 0 ) $this->compute_vat();
157 $this->nd_vat=bcmul($this->amount_vat,$this->nd_vat_rate);
158 $this->nd_vat=bcdiv($this->nd_vat,100);
159 $this->nd_vat=round($this->nd_vat,2);
160 }
161
163 {
164 if ( $this->check && $this->order > 4 ) throw new Exception ('ORDER NOT RESPECTED');
165 $this->order=5;
166
167 if ($this->amount_vat == 0 ) $this->compute_vat();
168 $this->nd_ded_vat=bcmul($this->amount_vat,$this->nd_ded_vat_rate);
169 $this->nd_ded_vat=bcdiv($this->nd_ded_vat,100);
170 $this->nd_ded_vat=round($this->nd_ded_vat,2);
171 }
172
173 function compute_perso()
174 {
175 if ( $this->check && $this->order != 1 ) throw new Exception ('ORDER NOT RESPECTED');
176 $this->order=2;
177 if ( $this->amount == 0 ) return;
178 $this->amount_perso=bcmul($this->amount,$this->amount_perso_rate);
179 $this->amount_perso=bcdiv($this->amount_perso,100);
180 $this->amount_perso=round($this->amount_perso,2);
181
182
183
184 }
185 function correct()
186 {
187 $this->amount=bcsub($this->amount,$this->amount_perso);
188 // correct the others amount
189 $this->amount=bcsub($this->amount,$this->amount_nd);
190 $this->amount_vat=bcsub($this->amount_vat,$this->nd_ded_vat);
191 $this->amount_vat=round($this->amount_vat,2);
192 $this->amount_vat=bcsub($this->amount_vat,$this->nd_vat);
193 $this->amount_vat=round($this->amount_vat,2);
194
195 }
196
197 /**!
198 * \brief verify that all the amount are positive or null
199 * otherwise throw a exception and the sum of amount + vat must
200 * equal to the sum of all the amount of the current object
201 * so you have to copy the object before computing anything and pass
202 * it as parameter
203 * \param compare with a object copied before computing, if null
204 * there is no comparison
205 */
206 function verify($p_obj=null)
207 {
208 foreach (self::$variable as $key=>$value)
209 if ( $this->$value < 0 )
210 throw new Exception (_("Montant invalide"));
211
212 if ( $p_obj != null )
213 {
214 $sum=0;
215 foreach ( array( 'amount','amount_vat','amount_nd','nd_vat','amount_perso','nd_ded_vat') as $value)
216 $sum=bcadd($sum,$this->$value);
217 if ( $p_obj->amount_vat == 0 ) $p_obj->compute_vat();
218 $cmp=bcadd($p_obj->amount,$p_obj->amount_vat);
219 $diff=bcsub($sum,$cmp);
220 if ( $diff != 0.0 )
221 throw new Exception (_("ECHEC VERIFICATION : valeur totale = $sum valeur attendue = $cmp diff = $diff"));
222 }
223 }
224 function display()
225 {
226 foreach (self::$variable as $key=>$value)
227 {
228 echo 'key '.$key.' Description '.$value.' value is '.$this->$key.'<br>';
229 }
230 }
231 public static function test_me ()
232 {
233 $a=new Acc_Compute();
234 echo $a->get_info();
235 echo '<hr>';
236
237 // Compute some operation to see if the computed amount are
238 // correct
239
240 //Test VAT
241 $a->set_parameter('amount',1.23);
242 $a->set_parameter('amount_vat_rate',0.21);
243
244 echo '<h1> Test VAT </h1>';
245 echo '<h2> Data </h2>';
246 $a->display();
247
248 echo '<h2> Result </h2>';
249 $a->compute_vat();
250 $a->display();
251 $a->verify();
252 // Test VAT + perso
253 $a=new Acc_Compute();
254 $a->set_parameter('amount',1.23);
255 $a->set_parameter('amount_vat_rate',0.21);
256 $a->set_parameter('amount_perso_rate',0.5);
257 echo '<h1> Test VAT + Perso</h1>';
258 echo '<h2> Data </h2>';
259 $a->display();
260 $b=clone $a;
261 $a->compute_vat();
262 $a->compute_perso();
263 $a->correct();
264 echo '<h2> Result </h2>';
265 $a->display();
266 $a->verify($b);
267 // TEST VAT + ND
268 // Test VAT + perso
269 $a=new Acc_Compute();
270 $a->set_parameter('amount',1.23);
271 $a->set_parameter('amount_vat_rate',0.21);
272 $a->set_parameter('nd_vat_rate',0.5);
273 $b=clone $a;
274 echo '<h1> Test VAT + ND VAT</h1>';
275 echo '<h2> Data </h2>';
276 $a->display();
277 $a->compute_vat();
278 $a->compute_nd_vat();
279 $a->correct();
280 echo '<h2> Result </h2>';
281 $a->display();
282 $a->verify($b);
283 // TEST VAT + ND
284 // Test VAT + perso
285 $a=new Acc_Compute();
286 $a->set_parameter('amount',1.23);
287 $a->set_parameter('amount_vat_rate',0.21);
288 $a->set_parameter('nd_vat_rate',0.5);
289 $a->set_parameter('amount_perso_rate',0.5);
290
291 $b=clone $a;
292 echo '<h1> Test VAT + ND VAT + perso</h1>';
293 echo '<h2> Data </h2>';
294 $a->display();
295 $a->compute_vat();
296 $a->compute_perso();
297 $a->compute_nd_vat();
298 $a->correct();
299 echo '<h2> Result </h2>';
300 $a->display();
301 $a->verify($b);
302 // TEST VAT + ND
303 $a=new Acc_Compute();
304 $a->set_parameter('amount',1.23);
305 $a->set_parameter('amount_vat_rate',0.21);
306 $a->set_parameter('amount_nd_rate',0.5);
307
308 $b=clone $a;
309 echo '<h1> Test VAT + ND </h1>';
310 echo '<h2> Data </h2>';
311 $a->display();
312 $a->compute_vat();
313 $a->compute_nd();
314
315 $a->compute_perso();
316 $a->compute_nd_vat();
317 $a->correct();
318 echo '<h2> Result </h2>';
319 $a->display();
320 $a->verify($b);
321 // TEST VAT + ND
322 // + Perso
323 $a=new Acc_Compute();
324 $a->set_parameter('amount',1.23);
325 $a->set_parameter('amount_vat_rate',0.21);
326 $a->set_parameter('amount_nd_rate',0.5);
327 $a->set_parameter('amount_perso_rate',0.2857);
328 $b=clone $a;
329 echo '<h1> Test VAT + ND + Perso</h1>';
330 echo '<h2> Data </h2>';
331 $a->display();
332 $a->compute_vat();
333 $a->compute_nd();
334
335 $a->compute_perso();
336 $a->compute_nd_vat();
337 $a->correct();
338 echo '<h2> Result </h2>';
339 $a->display();
340 $a->verify($b);
341// TEST VAT + ND
342 // + Perso
343 $a=new Acc_Compute();
344 $a->set_parameter('amount',1.23);
345 $a->set_parameter('amount_vat_rate',0.21);
346 $a->set_parameter('nd_ded_vat_rate',0.5);
347
348 $b=clone $a;
349 echo '<h1> Test VAT + TVA ND DED</h1>';
350 echo '<h2> Data </h2>';
351 $a->display();
352 $a->compute_vat();
353 $a->compute_nd();
354
355 $a->compute_perso();
356 $a->compute_nd_vat();
357 $a->compute_ndded_vat();
358 $a->correct();
359 echo '<h2> Result </h2>';
360 $a->display();
361 $a->verify($b);
362
363
364 }
365}
$idx
this class aims to compute different amount
set_parameter($p_string, $p_value)
compute_nd()
Compute the no deductible part of the amount, it reduce also the vat.
verify($p_obj=null)
!
get_parameter($p_string)