noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
acc_balance.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// Copyright Author Dany De Bontridder danydb@aevalys.eu
20/*! \file
21 * \brief Class for manipulating data to print the balance of account
22 */
23/*!
24 * \brief Class for manipulating data to print the balance of account
25 */
26
28{
29 var $db; /*! < database connection */
30 var $row; /*! < row for ledger*/
31 var $jrn; /*!< idx of a table of ledger create by user->get_ledger */
32 var $from_poste; /*!< from_poste filter on the post */
33 var $to_poste; /*!< to_poste filter on the post*/
34 var $unsold; /**= 0) */
35 function __construct($p_cn)
36 {
37 $this->db=$p_cn;
38 $this->jrn=array();
39 $this->from_poste="";
40 $this->to_poste="";
41 $this->unsold=false;
42 }
43
44
45 /*!
46 * \brief retrieve all the row from the ledger in the range of a periode
47 * \param $p_from_periode start periode (p_id)
48 * \param $p_to_periode end periode (p_id)
49 * \param $p_previous_exc previous exercice 1= yes default =0
50 *
51 * \return a double array
52 * array of
53 * - $a['poste']
54 * - $a['label']
55 * - $a['type'] string pcm_type actif, passif...
56 * - $a['sum_deb']
57 * - $a['sum_cred']
58 * - $a['solde_deb']
59 * - $a['solde_cred']
60 */
61 function get_row($p_from_periode,$p_to_periode,$p_previous_exc=0)
62 {
63 global $g_user;
64 // filter on requested periode
65 $per_sql=sql_filter_per($this->db,$p_from_periode,$p_to_periode,'p_id','j_tech_per');
66 bcscale(2);
67
68 $and="";
69 $jrn="";
70 $from_poste="";
71 $to_poste="";
72 /* if several ledgers are asked then we filter here */
73 if ( !empty($this->jrn) )
74 {
75 /**
76 *@file
77 *@bug the get_ledger here is not valid and useless we just need a list of the
78 * asked ledgers
79 */
80
81 $jrn=" j_jrn_def in (";
82 $comma='';
83 for ($e=0;$e<count($this->jrn);$e++)
84 {
85 $jrn.=$comma.$this->jrn[$e];
86 $comma=',';
87 }
88 $jrn.=')';
89 $and=" and ";
90 }
91
92 if ( strlen(noalyss_trim($this->from_poste)) != 0 && $this->from_poste!=-1 )
93 {
94 $from_poste=" $and j_poste::text >= '".$this->from_poste."'";
95 $and=" and ";
96 }
97 if ( strlen(noalyss_trim($this->to_poste)) != 0 && $this->to_poste!=-1 )
98 {
99 $to_poste=" $and j_poste::text <= '".$this->to_poste."'";
100 $and=" and ";
101 }
102 $filter_sql=$g_user->get_ledger_sql('ALL',3);
103
104 switch ($p_previous_exc)
105 {
106 case 0:
107 // build query
108 $sql="select j_poste as poste,
109 sum(deb) as sum_deb,
110 sum(cred) as sum_cred,
111 sum(deb_op) as sum_deb_ope ,
112 sum(cred_op) as sum_cred_ope
113 from
114 ( select j_poste,
115 case when j_debit='t' then j_montant else 0 end as deb,
116 case when j_debit='f' then j_montant else 0 end as cred,
117 case when j_debit='t' and jr_optype='OPE' then j_montant else 0 end as deb_op,
118 case when j_debit='f' and jr_optype='OPE' then j_montant else 0 end as cred_op
119 from jrnx join tmp_pcmn on (j_poste=pcm_val)
120 left join parm_periode on (j_tech_per = p_id)
121 join jrn_def on (j_jrn_def=jrn_def_id)
122 join jrn on (j_grpt=jr_grpt_id)
123 where
124 $jrn $from_poste $to_poste
125 $and $filter_sql
126 and
127 $per_sql ) as m group by 1 order by 1";
128 break;
129 case 1:
130 /*
131 * retrieve balance previous exercice
132 */
133 $periode=new Periode($this->db);
134 $previous_exc=$periode->get_exercice($p_from_periode)-1;
135 try {
136 list($previous_start,$previous_end)=$periode->get_limit($previous_exc);
137
138 $per_sql_previous=sql_filter_per($this->db,$previous_start->p_id,$previous_end->p_id,'p_id','j_tech_per');
139 $sql="
140 with m as
141 ( select j_poste,sum(deb) as sdeb,sum(cred) as scred,
142 sum(deb_op) as sum_deb_ope ,
143 sum(cred_op) as sum_cred_ope
144 from
145 (select j_poste,
146 case when j_debit='t' then j_montant else 0 end as deb,
147 case when j_debit='f' then j_montant else 0 end as cred ,
148 case when j_debit='t' and jr_optype='OPE' then j_montant else 0 end as deb_op,
149 case when j_debit='f' and jr_optype='OPE' then j_montant else 0 end as cred_op
150 from jrnx
151 join jrn on (j_grpt=jr_grpt_id)
152 join tmp_pcmn on (j_poste=pcm_val)
153 left join parm_periode on (j_tech_per = p_id)
154 join jrn_def on (j_jrn_def=jrn_def_id)
155 where
156 $jrn $from_poste $to_poste
157 $and $filter_sql and $per_sql
158 ) as sub_m group by j_poste order by j_poste ) ,
159 p as ( select j_poste,sum(deb) as sdeb,
160 sum(cred) as scred
161 from
162 (select j_poste,
163 case when j_debit='t' then j_montant else 0 end as deb,
164 case when j_debit='f' then j_montant else 0 end as cred
165 from jrnx join tmp_pcmn on (j_poste=pcm_val)
166 left join parm_periode on (j_tech_per = p_id)
167 join jrn_def on (j_jrn_def=jrn_def_id)
168 join jrn on (j_grpt=jr_grpt_id)
169 where
170 $jrn $from_poste $to_poste
171 $and $filter_sql and $per_sql_previous) as sub_p group by j_poste order by j_poste)
172 select coalesce(m.j_poste,p.j_poste) as poste
173 ,coalesce(m.sdeb,0) as sum_deb
174 , coalesce(m.scred,0) as sum_cred
175 ,coalesce(p.sdeb,0) as sum_deb_previous
176 , coalesce(p.scred,0) as sum_cred_previous
177 ,coalesce(sum_deb_ope,0) as sum_deb_ope
178 ,coalesce(sum_cred_ope,0) as sum_cred_ope
179 from m full join p on (p.j_poste=m.j_poste)
180 order by poste";
181
182 } catch (Exception $exc) {
183 $p_previous_exc=0;
184 /*
185 * no previous exercice
186 */
187 $sql="select j_poste as poste,
188 sum(deb) as sum_deb,
189 sum(cred) as sum_cred,
190 sum(deb_op) as sum_deb_ope ,
191 sum(cred_op) as sum_cred_ope
192 from
193 ( select j_poste,
194 case when j_debit='t' then j_montant else 0 end as deb,
195 case when j_debit='f' then j_montant else 0 end as cred,
196 case when j_debit='t' and jr_optype='OPE' then j_montant else 0 end as deb_op,
197 case when j_debit='f' and jr_optype='OPE' then j_montant else 0 end as cred_op
198 from jrnx join tmp_pcmn on (j_poste=pcm_val)
199 left join parm_periode on (j_tech_per = p_id)
200 join jrn_def on (j_jrn_def=jrn_def_id)
201 join jrn on (j_grpt=jr_grpt_id)
202 where
203 $jrn $from_poste $to_poste
204 $and $filter_sql
205 and
206 $per_sql ) as m group by poste order by poste";
207 }
208 break;
209
210 }
211 $cn=clone $this->db;
212 $Res=$this->db->exec_sql($sql);
213 $tot_cred= 0.0;
214 $tot_deb= 0.0;
215 $tot_deb_saldo=0.0;
216 $tot_cred_saldo=0.0;
217 $tot_cred_ope= 0.0;
218 $tot_deb_ope= 0.0;
219 $tot_deb_saldo_ope=0.0;
220 $tot_cred_saldo_ope=0.0;
221 $tot_cred_previous= 0.0;
222 $tot_deb_previous= 0.0;
223 $tot_deb_saldo_previous=0.0;
224 $tot_cred_saldo_previous=0.0;
225 $M=$this->db->size();
226
227 // Load the array
228 for ($i=0; $i <$M;$i++)
229 {
230 $r=$this->db->fetch($i);
231 $poste=new Acc_Account($cn,$r['poste']);
232
233 $a['poste']=$r['poste'];
234 $a['label']=mb_substr($poste->get_lib(),0,40);
235 $a['type']=$poste->get_parameter("pcm_type");
236 $a['sum_deb']=round($r['sum_deb'],2);
237 $a['sum_cred']=round($r['sum_cred'],2);
238 $a['solde_deb']=round(( $a['sum_deb'] >= $a['sum_cred'] )? $a['sum_deb']- $a['sum_cred']:0,2);
239 $a['solde_cred']=round(( $a['sum_deb'] <= $a['sum_cred'] )?$a['sum_cred']-$a['sum_deb']:0,2);
240 // opening
241 $a['sum_deb_ope']=round($r['sum_deb_ope'],2);
242 $a['sum_cred_ope']=round($r['sum_cred_ope'],2);
243 $a['solde_deb_ope']=round(( $a['sum_deb_ope'] >= $a['sum_cred_ope'] )? $a['sum_deb_ope']- $a['sum_cred_ope']:0,2);
244 $a['solde_cred_ope']=round(( $a['sum_deb_ope'] <= $a['sum_cred_ope'] )?$a['sum_cred_ope']-$a['sum_deb_ope']:0,2);
245
246
247 if ($p_previous_exc==1)
248 {
249 $a['sum_deb_previous']=round($r['sum_deb_previous'],2);
250 $a['sum_cred_previous']=round($r['sum_cred_previous'],2);
251 $a['solde_deb_previous']=round(( $a['sum_deb_previous'] >= $a['sum_cred_previous'] )? $a['sum_deb_previous']- $a['sum_cred_previous']:0,2);
252 $a['solde_cred_previous']=round(( $a['sum_deb_previous'] <= $a['sum_cred_previous'] )?$a['sum_cred_previous']-$a['sum_deb_previous']:0,2);
253 $tot_cred_previous = bcadd ($tot_cred_previous, $a['sum_cred_previous']);
254 $tot_deb_previous = bcadd( $tot_deb_previous,$a['sum_deb_previous']);
255 $tot_deb_saldo_previous = bcadd ($tot_deb_saldo_previous ,$a['solde_deb_previous']);
256 $tot_cred_saldo_previous = bcadd ($tot_cred_saldo_previous,$a['solde_cred_previous']);
257 }
258 if ($p_previous_exc==0 && $this->unsold==true && $a['solde_cred']==0 && $a['solde_deb']==0) continue;
259 if ($p_previous_exc==1 && $this->unsold==true && $a['solde_cred']==0 && $a['solde_deb']==0 && $a['solde_cred_previous']==0 && $a['solde_deb_previous']==0) continue;
260 $array[$i]=$a;
261 // Normal op
262 $tot_cred= bcadd ($tot_cred,$a['sum_cred']);
263 $tot_deb= bcadd($tot_deb, $a['sum_deb']);
264 $tot_deb_saldo= bcadd($tot_deb_saldo, $a['solde_deb']);
265 $tot_cred_saldo= bcadd($tot_cred_saldo,$a['solde_cred']);
266 // Opening op.
267 $tot_cred_ope= bcadd ($tot_cred_ope,$a['sum_cred_ope']);
268 $tot_deb_ope= bcadd($tot_deb_ope, $a['sum_deb_ope']);
269 $tot_deb_saldo_ope= bcadd($tot_deb_saldo_ope, $a['solde_deb_ope']);
270 $tot_cred_saldo_ope= bcadd($tot_cred_saldo_ope,$a['solde_cred_ope']);
271
272
273 }//for i
274 // Add the saldo
275 $i+=1;
277 $side_delta=findSide($delta);
278
279 $a['poste']="";
280 $a['label']=sprintf(_("Totaux delta %s %s"),nbm(abs($delta)),$side_delta);
281 $a['sum_deb']=$tot_deb;
282 $a['sum_cred']=$tot_cred;
283 $a['solde_deb']=$tot_deb_saldo;
284 $a['solde_cred']=$tot_cred_saldo;
285 $a['sum_deb_ope']=$tot_deb_ope;
286 $a['sum_cred_ope']=$tot_cred_ope;
287 $a['solde_deb_ope']=$tot_deb_saldo_ope;
288 $a['solde_cred_ope']=$tot_cred_saldo_ope;
289 if ($p_previous_exc==1) {
290 $a['sum_deb_previous']=$tot_deb_previous;
291 $a['sum_cred_previous']=$tot_cred_previous;
292 $a['solde_deb_previous']=$tot_deb_saldo_previous;
293 $a['solde_cred_previous']=$tot_cred_saldo_previous;
294 }
295 $array[$i]=$a;
296 $this->row=$array;
297 return $array;
298
299 }
300 /**
301 * set the $this->jrn to the cat, filter the ledger thanks the type (key in the array)
302 * @param array of integer 0->Sale,1->Purchase,2->Financial,3->ODS
303 * @see Acc_Ledgger::array_cat
304 */
306 {
307 if ( empty($p_array) )
308 {
309 $this->jrn=null;
310 return;
311 }
312 /* get the list of jrn of the cat. */
313
315 $this->jrn=array();
316 for ($e=0;$e<count($array);$e++)
317 {
318 if ( isset($p_array[$e]))
319 {
320 $t_a=$this->db->get_array('select jrn_def_id from jrn_def where jrn_def_type=$1',array($array[$e]['cat']));
321 for ( $f=0;$f < count($t_a);$f++) $this->jrn[]=$t_a[$f]['jrn_def_id'];
322 }
323 }
324
325 }
326 /**
327 * @brief create an empty array for computing the summary
328 */
329 function summary_init()
330 {
331 $array=[];
332 $array["1_5"]=["deb"=>0,"cred"=>0];
333 $array["6"]=["deb"=>0,"cred"=>0];
334 $array["7"]=["deb"=>0,"cred"=>0];
335 return $array;
336 }
337 /**
338 * Add the current amount (d /c) to the right item in the array, in order
339 * to compute a summary (1 to 5 , 6 charge and 7 profit ),
340 * the return value is an array
341 * @see Acc_Balance::summary_init()
342 * @param array $p_array array with the result
343 * @param string $p_accounting accounting
344 * @param numeric $p_deb
345 * @param numeric $p_cred
346 * @return array
347 */
348 function summary_add($p_array,$p_accounting,$p_deb,$p_cred)
349 {
350 if (trim($p_accounting)=="") return $p_array;
351 // Summary
352 $first_digit=trim($p_accounting);
353 $first_digit_trim=$first_digit[0];
354 if ( $first_digit_trim >0 && $first_digit_trim < 6) {
355 $p_array["1_5"]["deb"]=bcadd( $p_array["1_5"]["deb"],$p_deb);
356 $p_array["1_5"]["cred"]=bcadd( $p_array["1_5"]["cred"],$p_cred);
357 }
358 elseif ($first_digit_trim == "6") {
359 $p_array["6"]["deb"]=bcadd( $p_array["6"]["deb"],$p_deb);
360 $p_array["6"]["cred"]=bcadd( $p_array["6"]["cred"],$p_cred);
361 }
362 elseif ($first_digit_trim=="7") {
363 $p_array["7"]["deb"]=bcadd( $p_array["7"]["deb"],$p_deb);
364 $p_array["7"]["cred"]=bcadd( $p_array["7"]["cred"],$p_cred);
365
366 }
367 return $p_array;
368 }
369 /**
370 * Display the summary of result in HTML
371 * @see Acc_Balance::summary_init()
372 * @param array $p_array
373 */
375 {
376 echo "<table>";
377 echo "<tr>";
378 echo td(_("Class 1-5"));
379 $diff=bcsub($p_array["1_5"]["deb"],$p_array["1_5"]["cred"]);
380 echo td(nbm(abs($diff),2),' class="num"');
381 $side=($diff < 0)?"C":"D";
382 $side=($diff == 0)?"=":$side;
383 echo td($side);
384 echo "</tr>";
385 echo "<tr>";
386 echo td(_("Class 6"));
387 $diff6=bcsub($p_array["6"]["deb"],$p_array["6"]["cred"]);
388 echo td(nbm(abs($diff6),2),' class="num"');
389 $side=($diff6 < 0)?"C":"D";
390 $side=($diff6 == 0)?"=":$side;
391 echo td($side);
392 echo "</tr>";
393 echo "<tr>";
394 echo td(_("Class 7"));
395 $diff7=bcsub($p_array["7"]["deb"],$p_array["7"]["cred"]);
396 echo td(nbm(abs($diff7),2),' class="num"');
397 $side=($diff7 < 0)?"C":"D";
398 $side=($diff7 == 0)?"=":$side;
399 echo td($side);
400 echo "</tr>";
401 echo "<tr>";
402 echo td(_("Solde 6/7"));
403 $diff=bcadd($diff6,$diff7);
404 echo td(nbm(abs($diff),2),' class="num"');
405 $side=($diff < 0)?"C":"D";
406 $side=($diff == 0)?"=":$side;
407 echo td($side);
408 echo "</tr>";
409 echo "</table>";
410 }
411 /**
412 * Display the summary of result in PDF
413 * @param array $p_array
414 * @param PDF $p_pdf
415 * @see Acc_Balance::summary_init()
416 */
418 {
419 $p_pdf->write_cell(30,6,_("Class 1-5"));
420 $diff=bcsub($p_array["1_5"]["deb"],$p_array["1_5"]["cred"]);
421 $p_pdf->write_cell(50,6,
422 nbm(abs($diff),2),0,0,'R');
423 $side=($diff < 0)?"C":"D";
424 $side=($diff == 0)?"=":$side;
425 $p_pdf->write_cell(10,6,$side);
426 $p_pdf->line_new();
427
428 $p_pdf->write_cell(30,6,_("Class 6"));
429 $diff6=bcsub($p_array["6"]["deb"],$p_array["6"]["cred"]);
430 $p_pdf->write_cell(50,6,nbm(abs($diff6),2),0,0,'R');
431 $side=($diff6 < 0)?"C":"D";
432 $side=($diff6 == 0)?"=":$side;
433 $p_pdf->write_cell(10,6,$side);
434 $p_pdf->line_new();
435
436 $p_pdf->write_cell(30,6,_("Class 7"));
437 $diff7=bcsub($p_array["7"]["deb"],$p_array["7"]["cred"]);
438 $p_pdf->write_cell(50,6,nbm(abs($diff7),2),0,0,'R');
439 $side=($diff7 < 0)?"C":"D";
440 $side=($diff7 == 0)?"=":$side;
441 $p_pdf->write_cell(10,6,$side);
442 $p_pdf->line_new();
443
444 $p_pdf->write_cell(30,6,_("Solde 6/7"));
445 $diff=bcadd($diff6,$diff7);
446 $p_pdf->write_cell(50,6,nbm(abs($diff),2),0,0,'R');
447 $side=($diff < 0)?"C":"D";
448 $side=($diff == 0)?"=":$side;
449 $p_pdf->write_cell(10,6,$side);
450 $p_pdf->line_new();
451 }
452
453}
sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
Create the condition to filter on the j_tech_per thanks a from and to date.
findSide($p_number)
return D if the number is smaller than 0 , C if bigger and an empty string if equal to 0.
td($p_string='', $p_extra='')
surround the string with td
Definition ac_common.php:83
nbm($p_number, $p_dec=2)
format the number with a sep.
noalyss_trim($p_string)
global $g_user
if no group available , then stop
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$anc_grandlivre from_poste
$anc_grandlivre to_poste
$from_poste
_("actif, passif,charge,...")
$to_poste
$previous_exc
Class for manipulating data to print the balance of account.
summary_add($p_array, $p_accounting, $p_deb, $p_cred)
Add the current amount (d /c) to the right item in the array, in order to compute a summary (1 to 5 ,...
summary_display_pdf($p_array, $p_pdf)
Display the summary of result in PDF.
filter_cat($p_array)
set the $this->jrn to the cat, filter the ledger thanks the type (key in the array)
__construct($p_cn)
= 0)
summary_init()
create an empty array for computing the summary
summary_display($p_array)
Display the summary of result in HTML.
static array_cat()
create an array of the existing cat, to be used in a checkbox form
$bal jrn
$SecUser db
if( $delta< 0) elseif( $delta==0)
$side