Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

class_balance.php

Go to the documentation of this file.
00001 <?
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 1.3 $ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 include_once("poste.php");
00029 class Balance {
00030   var $db;       
00031   var $central; 
00032   var $row;     
00033   function Balance($p_cn) {
00034     $this->db=$p_cn;
00035     $this->central='N';
00036   }
00037 
00038 
00053   function GetRow($p_from_periode,$p_to_periode) {
00054     // compute periode
00055     if ( $p_from_periode==$p_to_periode ) {
00056       $per_sql=" j_tech_per = $p_from_periode ";
00057     } else {
00058       $per_sql = "j_tech_per >=  $p_from_periode and j_tech_per <= $p_to_periode ";
00059     }
00060 
00061 
00062     // if centralized
00063     $cent="";
00064 
00065     if ( $this->central=='Y' ) { $cent="j_centralized = true and "; }
00066 
00067     // build query
00068     $sql="select j_poste,sum(deb) as sum_deb, sum(cred) as sum_cred from 
00069           ( select j_poste,
00070              case when j_debit='t' then j_montant else 0 end as deb,
00071              case when j_debit='f' then j_montant else 0 end as cred
00072           from jrnx join tmp_pcmn on j_poste=pcm_val
00073               where 
00074              $cent
00075             $per_sql ) as m group by j_poste order by j_poste::text";
00076 
00077     $Res=ExecSql($this->db,$sql);
00078 
00079     $tot_cred=  0.0;
00080     $tot_deb=  0.0;
00081     $tot_deb_saldo=0.0;
00082     $tot_cred_saldo=0.0;
00083     $M=pg_NumRows($Res);
00084     // Load the array
00085     for ($i=0; $i <$M;$i++) {
00086       $r=pg_fetch_array($Res,$i);
00087       $a['poste']=$r['j_poste'];
00088       $a['label']=substr(GetPosteLibelle($this->db,$r['j_poste'],1),0,40);
00089       $a['sum_deb']=round($r['sum_deb'],2);
00090       $a['sum_cred']=round($r['sum_cred'],2);
00091       $a['solde_deb']=round(( $a['sum_deb']  >=  $a['sum_cred'] )? $a['sum_deb']- $a['sum_cred']:0,2);
00092       $a['solde_cred']=round(( $a['sum_deb'] <=  $a['sum_cred'] )?$a['sum_cred']-$a['sum_deb']:0,2);
00093       $array[$i]=$a;
00094       $tot_cred+=  $a['sum_cred'];
00095       $tot_deb+= $a['sum_deb']; 
00096       $tot_deb_saldo+= $a['solde_deb'];
00097       $tot_cred_saldo+= $a['solde_cred'];
00098       
00099       
00100     }//for i
00101     // Add the saldo
00102     $i+=1;
00103     $a['poste']="";
00104     $a['label']="<b> Totaux </b>";
00105     $a['sum_deb']=$tot_deb;
00106     $a['sum_cred']=$tot_cred;
00107     $a['solde_deb']=$tot_deb_saldo;
00108     $a['solde_cred']=$tot_cred_saldo;
00109     $array[$i]=$a;
00110     $this->row=$array;
00111     return $array;
00112 
00113   }
00114 }