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