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