noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_balance.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS 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  *   NOALYSS 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 NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 /*! \file
00021  * \brief Class for manipulating data to print the balance of account
00022  */
00023 /*!
00024  * \brief Class for manipulating data to print the balance of account
00025  */
00026 require_once('class_acc_account.php');
00027 
00028 class Acc_Balance
00029 {
00030     var $db;       /*! < database connection */
00031     var $row;     /*! < row for ledger*/
00032     var $jrn;                                           /*!< idx of a table of ledger create by user->get_ledger */
00033     var $from_poste;                            /*!< from_poste  filter on the post */
00034     var $to_poste;                              /*!< to_poste filter on the post*/
00035     var $unsold;                                /**= 0) */
00036     function Acc_Balance($p_cn)
00037     {
00038         $this->db=$p_cn;
00039         $this->jrn=null;
00040         $from_poste="";
00041         $to_poste="";
00042         $unsold=false;
00043     }
00044 
00045 
00046     /*!
00047      * \brief retrieve all the row from the ledger in the range of a periode
00048      * \param $p_from_periode start periode (p_id)
00049      * \param $p_to_periode end periode (p_id)
00050      * \param $p_previous_exc previous exercice 1= yes default =0
00051      *
00052      * \return a double array
00053      *     array of
00054      *         - $a['poste']
00055      *         - $a['label']
00056      *         - $a['sum_deb']
00057      *         - $a['sum_cred']
00058      *         - $a['solde_deb']
00059      *         - $a['solde_cred']
00060      */
00061     function get_row($p_from_periode,$p_to_periode,$p_previous_exc=0)
00062     {
00063         global $g_user;
00064         // filter on requested periode
00065         $per_sql=sql_filter_per($this->db,$p_from_periode,$p_to_periode,'p_id','j_tech_per');
00066 
00067 
00068         $and="";
00069         $jrn="";
00070         $from_poste="";
00071         $to_poste="";
00072         /* if several ledgers are asked then we filter here  */
00073         if ($this->jrn!== null)
00074         {
00075             /**
00076              *@file
00077              *@bug the get_ledger here is not valid and useless we just need a list of the 
00078              * asked ledgers
00079              */
00080 
00081             $jrn="  j_jrn_def in (";
00082             $comma='';
00083             for ($e=0;$e<count($this->jrn);$e++)
00084             {
00085                 $jrn.=$comma.$this->jrn[$e];
00086                 $comma=',';
00087             }
00088             $jrn.=')';
00089             $and=" and ";
00090         }
00091 
00092         if ( strlen(trim($this->from_poste)) != 0 && $this->from_poste!=-1  )
00093         {
00094             $from_poste=" $and j_poste::text >= '".$this->from_poste."'";
00095             $and=" and ";
00096         }
00097         if ( strlen(trim($this->to_poste)) != 0   && $this->to_poste!=-1 )
00098         {
00099             $to_poste=" $and j_poste::text <= '".$this->to_poste."'";
00100             $and=" and ";
00101         }
00102         $filter_sql=$g_user->get_ledger_sql('ALL',3);
00103         
00104         switch ($p_previous_exc)
00105         {
00106             case 0:
00107                 // build query
00108                 $sql="select j_poste as poste,sum(deb) as sum_deb, sum(cred) as sum_cred from
00109                      ( select j_poste,
00110                      case when j_debit='t' then j_montant else 0 end as deb,
00111                      case when j_debit='f' then j_montant else 0 end as cred
00112                      from jrnx join tmp_pcmn on (j_poste=pcm_val)
00113                      left join parm_periode on (j_tech_per = p_id)
00114                      join jrn_def on (j_jrn_def=jrn_def_id)
00115                      where
00116                      $jrn $from_poste $to_poste
00117                      $and $filter_sql
00118                      and
00119                      $per_sql ) as m group by 1 order by 1";
00120                 break;
00121             case 1:
00122                 /*
00123                  * retrieve balance previous exercice
00124                  */
00125                 $periode=new Periode($this->db);
00126                 $previous_exc=$periode->get_exercice($p_from_periode)-1;
00127                 try {
00128                     list($previous_start,$previous_end)=$periode->get_limit($previous_exc);
00129                
00130                         $per_sql_previous=sql_filter_per($this->db,$previous_start->p_id,$previous_end->p_id,'p_id','j_tech_per');
00131                         $sql="
00132                             with m as 
00133                                 ( select j_poste,sum(deb) as sdeb,sum(cred) as scred
00134                                 from 
00135                                 (select j_poste, 
00136                                     case when j_debit='t' then j_montant else 0 end as deb, 
00137                                     case when j_debit='f' then j_montant else 0 end as cred 
00138                                     from jrnx 
00139                                     join tmp_pcmn on (j_poste=pcm_val) 
00140                                     left join parm_periode on (j_tech_per = p_id) 
00141                                     join jrn_def on (j_jrn_def=jrn_def_id) 
00142                                     where
00143                                                              $jrn $from_poste $to_poste
00144                                     $and $filter_sql and $per_sql
00145                                     ) as sub_m group by j_poste order by j_poste ) , 
00146                             p as ( select j_poste,sum(deb) as sdeb,sum(cred) as scred 
00147                                 from 
00148                                     (select j_poste, 
00149                                         case when j_debit='t' then j_montant else 0 end as deb, 
00150                                         case when j_debit='f' then j_montant else 0 end as cred 
00151                                         from jrnx join tmp_pcmn on (j_poste=pcm_val) 
00152                                         left join parm_periode on (j_tech_per = p_id) 
00153                                         join jrn_def on (j_jrn_def=jrn_def_id) 
00154                                         where 
00155                                        $jrn $from_poste $to_poste
00156                                     $and $filter_sql and $per_sql_previous)  as sub_p group by j_poste order by j_poste)
00157                             select coalesce(m.j_poste,p.j_poste) as poste
00158                                                                 ,coalesce(m.sdeb,0) as sum_deb
00159                                                                 , coalesce(m.scred,0) as sum_cred 
00160                                                                 ,coalesce(p.sdeb,0) as sum_deb_previous
00161                                                                 , coalesce(p.scred,0) as sum_cred_previous from m full join p on (p.j_poste=m.j_poste)
00162                                              order by poste";
00163                        
00164                  } catch (Exception $exc) {
00165                     $p_previous_exc=0;
00166                     /*
00167                      * no previous exercice
00168                      */
00169                      $sql="select upper(j_poste::text) as poste,sum(deb) as sum_deb, sum(cred) as sum_cred from
00170                      ( select j_poste,
00171                      case when j_debit='t' then j_montant else 0 end as deb,
00172                      case when j_debit='f' then j_montant else 0 end as cred
00173                      from jrnx join tmp_pcmn on (j_poste=pcm_val)
00174                      left join parm_periode on (j_tech_per = p_id)
00175                      join jrn_def on (j_jrn_def=jrn_def_id)
00176                      where
00177                      $jrn $from_poste $to_poste
00178                      $and $filter_sql
00179                      and
00180                      $per_sql ) as m group by poste order by poste";
00181                 }
00182                 break;
00183                            
00184         }
00185         $cn=clone $this->db;
00186         $Res=$this->db->exec_sql($sql);
00187         $tot_cred=  0.0;
00188         $tot_deb=  0.0;
00189         $tot_deb_saldo=0.0;
00190         $tot_cred_saldo=0.0;
00191         $tot_cred_previous=  0.0;
00192         $tot_deb_previous=  0.0;
00193         $tot_deb_saldo_previous=0.0;
00194         $tot_cred_saldo_previous=0.0;
00195         $M=$this->db->size();
00196 
00197         // Load the array
00198         for ($i=0; $i <$M;$i++)
00199         {
00200             $r=$this->db->fetch($i);
00201             $poste=new Acc_Account($cn,$r['poste']);
00202 
00203             $a['poste']=$r['poste'];
00204             $a['label']=mb_substr($poste->get_lib(),0,40);
00205             $a['sum_deb']=round($r['sum_deb'],2);
00206             $a['sum_cred']=round($r['sum_cred'],2);
00207             $a['solde_deb']=round(( $a['sum_deb']  >=  $a['sum_cred'] )? $a['sum_deb']- $a['sum_cred']:0,2);
00208             $a['solde_cred']=round(( $a['sum_deb'] <=  $a['sum_cred'] )?$a['sum_cred']-$a['sum_deb']:0,2);
00209             if ($p_previous_exc==1)
00210             {
00211                 $a['sum_deb_previous']=round($r['sum_deb_previous'],2);
00212                 $a['sum_cred_previous']=round($r['sum_cred_previous'],2);
00213                 $a['solde_deb_previous']=round(( $a['sum_deb_previous']  >=  $a['sum_cred_previous'] )? $a['sum_deb_previous']- $a['sum_cred_previous']:0,2);
00214                 $a['solde_cred_previous']=round(( $a['sum_deb_previous'] <=  $a['sum_cred_previous'] )?$a['sum_cred_previous']-$a['sum_deb_previous']:0,2);
00215                 $tot_cred_previous+=  $a['sum_cred_previous'];
00216                 $tot_deb_previous+= $a['sum_deb_previous'];
00217                 $tot_deb_saldo_previous+= $a['solde_deb_previous'];
00218                 $tot_cred_saldo_previous+= $a['solde_cred_previous'];
00219             }
00220             if ($p_previous_exc==0 && $this->unsold==true && $a['solde_cred']==0 && $a['solde_deb']==0) continue;
00221             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;
00222             $array[$i]=$a;
00223             $tot_cred+=  $a['sum_cred'];
00224             $tot_deb+= $a['sum_deb'];
00225             $tot_deb_saldo+= $a['solde_deb'];
00226             $tot_cred_saldo+= $a['solde_cred'];
00227 
00228 
00229         }//for i
00230         // Add the saldo
00231         $i+=1;
00232         $a['poste']="";
00233         $a['label']="Totaux ";
00234         $a['sum_deb']=$tot_deb;
00235         $a['sum_cred']=$tot_cred;
00236         $a['solde_deb']=$tot_deb_saldo;
00237         $a['solde_cred']=$tot_cred_saldo;
00238         if ($p_previous_exc==1) {
00239             $a['sum_deb_previous']=$tot_deb_previous;
00240             $a['sum_cred_previous']=$tot_cred_previous;
00241             $a['solde_deb_previous']=$tot_deb_saldo_previous;
00242             $a['solde_cred_previous']=$tot_cred_saldo_previous;
00243         }
00244         $array[$i]=$a;
00245         $this->row=$array;
00246         return $array;
00247 
00248     }
00249     /**
00250      * set the $this->jrn to the cat
00251      */
00252     function filter_cat($p_array)
00253     {
00254         if ( empty($p_array) )
00255         {
00256             $bal->jn=null;
00257             return;
00258         }
00259         /* get the list of jrn of the cat. */
00260 
00261         $array=Acc_Ledger::array_cat();
00262         $jrn=array();
00263         for ($e=0;$e<count($array);$e++)
00264         {
00265             if ( isset($p_array[$e]))
00266             {
00267                 $t_a=$this->db->get_array('select jrn_def_id from jrn_def where jrn_def_type=$1',array($array[$e]['cat']));
00268                 for ( $f=0;$f < count($t_a);$f++) $this->jrn[]=$t_a[$f]['jrn_def_id'];
00269             }
00270         }
00271 
00272     }
00273     static function test_me ()
00274     {
00275         require 'class_user.php';
00276         global $g_user;
00277         $cn=new Database(dossier::id());
00278         $g_user=new User($cn);
00279         $a=new Acc_Balance($cn);
00280         $a->get_row(163, 175, 1);
00281         var_dump($a);
00282     }
00283 }
 All Data Structures Namespaces Files Functions Variables Enumerations