noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anc_group.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 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  * \brief class for the group of the analytic account
00024  *
00025  */
00026 require_once ('class_database.php');
00027 require_once ('constant.php');
00028 require_once ('class_dossier.php');
00029 require_once('class_anc_print.php');
00030 
00031 /*! \brief class for the group of the analytic account
00032  *
00033  */
00034 class Anc_Group extends Anc_Print
00035 {
00036     var $db;
00037     var $ga_id;
00038     var $ga_description;
00039     var $pa_id;
00040 
00041     function __construct ( $p_cn )
00042     {
00043         $this->db=$p_cn;
00044         $this->ga_id=null;
00045         $this->ga_description=null;
00046         $this->pa_id=null;
00047     }
00048     /*!
00049      * \brief insert into the database  an object
00050      * \return message with error otherwise an empty string
00051      */
00052 
00053     function insert()
00054     {
00055         $sql=" insert into groupe_analytique (ga_id,ga_description,pa_id) values ('%s','%s',%d)";
00056         $sql=sprintf($sql,Database::escape_string($this->ga_id),
00057                      Database::escape_string($this->ga_description),
00058                      $this->pa_id);
00059         try
00060         {
00061             $this->db->exec_sql($sql);
00062         }
00063         catch (Exception $a)
00064         {
00065             return '<span class="notice">Doublon !!</span>';
00066         }
00067         return "";
00068     }
00069     /*!
00070      * \brief remove from the database
00071      */
00072 
00073     function remove()
00074     {
00075         $this->ga_id=str_replace(' ','',$this->ga_id);
00076         $this->ga_id=strtoupper($this->ga_id);
00077         $sql=" delete from groupe_analytique where ga_id='".Database::escape_string($this->ga_id)."'";
00078 
00079         $this->db->exec_sql($sql);
00080     }
00081 
00082     /*!
00083      * \brief load from the database and make an object
00084      */
00085     function load()
00086     {
00087         $sql="select ga_id, ga_description,pa_id from groupe_analytique where".
00088              " ga_id = ".$this->ga_id;
00089         $res=$this->db->exec_sql($sql);
00090         $array=Database::fetch_all($res);
00091         if ( ! empty($array) )
00092         {
00093             $this->ga_id=$array['ga_id'];
00094             $this->ga_description=$array['ga_description'];
00095             $this->pa_id=$array['pa_id'];
00096         }
00097     }
00098 
00099     /*!
00100      * \brief fill the object thanks an array
00101      * \param array
00102      */
00103     function get_from_array($p_array)
00104     {
00105         $this->ga_id=$p_array['ga_id'];
00106         $this->pa_id=$p_array['pa_id'];
00107         $this->ga_description=$p_array['ga_description'];
00108     }
00109     function myList()
00110     {
00111         $sql=" select ga_id,groupe_analytique.pa_id,pa_name,ga_description ".
00112              " from groupe_analytique ".
00113              " join plan_analytique using (pa_id)";
00114         $r=$this->db->exec_sql($sql);
00115         $array=Database::fetch_all($r);
00116         $res=array();
00117         if ( ! empty($array))
00118         {
00119             foreach ($array as $m )
00120             {
00121                 $obj= new Anc_Group($this->db);
00122                 $obj->get_from_array($m);
00123                 $obj->pa_name=$m['pa_name'];
00124                 $res[]=clone $obj;
00125             }
00126         }
00127         return $res;
00128     }
00129 
00130     function set_sql_filter()
00131     {
00132         $sql="";
00133         $and="and ";
00134         if ( $this->from != "" )
00135         {
00136             $sql.=" $and  oa_date >= to_date('".$this->from."','DD.MM.YYYY')";
00137             $and=" and ";
00138         }
00139         if ( $this->to != "" )
00140         {
00141             $sql.=" $and oa_date <= to_date('".$this->to."','DD.MM.YYYY')";
00142             $and=" and ";
00143         }
00144         if ( $this->from_poste != "" )
00145         {
00146             $sql.=" $and upper(po_name)>= upper('".$this->from_poste."')";
00147             $and=" and ";
00148         }
00149         if ( $this->to_poste != "" )
00150         {
00151             $sql.=" $and upper(po_name)<= upper('".$this->to_poste."')";
00152             $and=" and ";
00153         }
00154         return $sql;
00155 
00156     }
00157 
00158     function get_result()
00159     {
00160       $filter_date=$this->set_sql_filter();
00161 
00162       $sql="with m as (select po_id,
00163         po_name,
00164         ga_id,
00165         case when  oa_debit = 't' then oa_amount
00166         else 0
00167         end  as amount_deb,
00168         case when oa_debit = 'f' then oa_amount
00169         else 0
00170         end as amount_cred,
00171         oa_date
00172         from operation_analytique
00173 join poste_analytique using (po_id)
00174 where pa_id=$1 $filter_date )
00175 select sum(amount_cred) as sum_cred, sum(amount_deb)as sum_deb,po_name,ga_id,ga_description
00176 from m left join groupe_analytique using (ga_id)
00177 group by ga_id,po_name,ga_description
00178 order by ga_description,po_name";
00179       $ret=$this->db->get_array($sql,array($this->pa_id));
00180 
00181       return $ret;
00182     }
00183 
00184     function display_html()
00185     {
00186       if ( $this->check()  != 0)
00187         {
00188           alert('Désolé mais une des dates données n\'est pas valide');
00189           return;
00190         }
00191 
00192       $array=$this->get_result();
00193       if ( empty ($array) ) return "";
00194       require_once('template/anc_balance_group.php');
00195 
00196 
00197     }
00198   /**
00199    *@brief display the button export CSV
00200    *@param $p_hidden is a string containing hidden items
00201    *@return html string
00202    */
00203   function show_button($p_hidden="")
00204   {
00205     $r="";
00206     $r.= '<form method="GET" action="export.php"  style="display:inline">';
00207     $r.= HtmlInput::hidden("act","CSV:AncBalGroup");
00208     $r.= HtmlInput::hidden("to",$this->to);
00209     $r.= HtmlInput::hidden("from",$this->from);
00210     $r.= HtmlInput::hidden("pa_id",$this->pa_id);
00211     $r.= HtmlInput::hidden("from_poste",$this->from_poste);
00212     $r.= HtmlInput::hidden("to_poste",$this->to_poste);
00213     $r.= $p_hidden;
00214     $r.= dossier::hidden();
00215     $r.=HtmlInput::submit('bt_csv',"Export en CSV");
00216     $r.= '</form>';
00217     return $r;
00218   }
00219   function export_csv()
00220   {
00221     $array=$this->get_result();
00222     printf('"groupe";"activité";"débit";"credit";"solde"');
00223     printf("\r\n");
00224     bcscale(2);
00225     for ($i=0;$i<count($array);$i++)
00226       {
00227         printf('"%s";"%s";%s;%s;%s',
00228                $array[$i]['ga_id'],
00229                $array[$i]['po_name'],
00230                nb($array[$i]['sum_deb']),
00231                nb($array[$i]['sum_cred']),
00232                nb(bcsub($array[$i]['sum_cred'],$array[$i]['sum_deb']))
00233                );
00234         printf("\r\n");
00235       }
00236   }
00237     static function test_me()
00238     {
00239 
00240         $cn=new Database(dossier::id());
00241         print_r($cn);
00242         $o=new Anc_Group($cn);
00243         $r=$o->myList();
00244         print_r($r);
00245         echo '<hr>';
00246         print_r($o);
00247         $o->ga_id="DD' dd dDD";
00248         $o->ga_description="Test 1";
00249         $o->remove();
00250         //    $o->insert();
00251         $o->ga_id="DD";
00252         $o->ga_description="Test 1";
00253         $o->remove();
00254 
00255         $r=$o->myList();
00256         print_r($r);
00257     }
00258 }
 All Data Structures Namespaces Files Functions Variables Enumerations