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