class_acc_report.php

Go to the documentation of this file.
00001 <?php
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: 4709 $ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 /*! \file
00022  * \brief Create, view, modify and parse report
00023  */
00024 
00025 require_once("class_itext.php");
00026 require_once("class_ibutton.php");
00027 require_once('class_acc_report_row.php');
00028 require_once('class_impress.php');
00029 
00030 /*!
00031  * \brief Class rapport  Create, view, modify and parse report
00032  */
00033 
00034 class Acc_Report
00035 {
00036 
00037     var $db;    /*!< $db database connx */
00038     var $id;    /*!< $id formdef.fr_id */
00039     var $name;  /*!< $name report's name */
00040     var $aAcc_Report_row;               /*!< array of rapport_row */
00041     var $nb;
00042     /*!\brief  Constructor */
00043     function __construct($p_cn,$p_id=0)
00044     {
00045         $this->db=$p_cn;
00046         $this->id=$p_id;
00047         $this->name='Nouveau';
00048         $this->aAcc_Report_row=null;
00049     }
00050     /*!\brief Return the report's name
00051      */
00052     function get_name()
00053     {
00054         $ret=$this->db->exec_sql("select fr_label from formdef where fr_id=".$this->id);
00055         if (Database::num_row($ret) == 0) return $this->name;
00056         $a=Database::fetch_array($ret,0);
00057         $this->name=$a['fr_label'];
00058         return $this->name;
00059     }
00060     /*!\brief return all the row and parse formula
00061      *        from a report
00062      * \param $p_start start periode
00063      * \param $p_end end periode
00064      * \param $p_type_date type of the date : periode or calendar
00065      */
00066     function get_row($p_start,$p_end,$p_type_date)
00067     {
00068 
00069         $Res=$this->db->exec_sql("select fo_id ,
00070                                  fo_fr_id,
00071                                  fo_pos,
00072                                  fo_label,
00073                                  fo_formula,
00074                                  fr_label from form
00075                                  inner join formdef on fr_id=fo_fr_id
00076                                  where fr_id =".$this->id.
00077                                  "order by fo_pos");
00078         $Max=Database::num_row($Res);
00079         if ($Max==0)
00080         {
00081             $this->row=0;
00082             return null;
00083         }
00084         $col=array();
00085         for ($i=0;$i<$Max;$i++)
00086         {
00087             $l_line=Database::fetch_array($Res,$i);
00088             $col[]=Impress::parse_formula($this->db,
00089                                 $l_line['fo_label'],
00090                                 $l_line['fo_formula'],
00091                                 $p_start,
00092                                 $p_end,
00093                                 true,
00094                                 $p_type_date
00095                                );
00096 
00097         } //for ($i
00098         $this->row=$col;
00099         return $col;
00100     }
00101     /*!
00102      * \brief  Display a form for encoding a new report or update one
00103      *
00104      * \param $p_line number of line
00105      *
00106      */
00107     function form($p_line=0)
00108     {
00109 
00110         $r="";
00111         if ($p_line == 0 ) $p_line=count($this->aAcc_Report_row);
00112         $r.= dossier::hidden();
00113         $r.= HtmlInput::hidden('line',$p_line);
00114         $r.= HtmlInput::hidden('fr_id',$this->id);
00115         $wForm=new IText();
00116         $r.="Nom du rapport : ";
00117         $r.=$wForm->input('form_nom',$this->name);
00118 
00119         $r.= '<TABLE id="rap1" width="100%">';
00120         $r.= "<TR>";
00121         $r.= "<TH> Position </TH>";
00122         $r.= "<TH> Texte </TH>";
00123         $r.= "<TH> Formule</TH>";
00124 
00125         $r.= '</TR>';
00126         $wName=new IText();
00127         $wName->size=40;
00128         $wPos=new IText();
00129         $wPos->size=3;
00130         $wForm=new IText();
00131         $wForm->size=35;
00132         for ( $i =0 ; $i < $p_line;$i++)
00133         {
00134 
00135             $r.= "<TR>";
00136 
00137             $r.= "<TD>";
00138             $wPos->value=( isset($this->aAcc_Report_row[$i]->fo_pos))?$this->aAcc_Report_row[$i]->fo_pos:$i+1;
00139             $r.=$wPos->input("pos".$i);
00140             $r.= '</TD>';
00141 
00142 
00143             $r.= "<TD>";
00144             $wName->value=( isset($this->aAcc_Report_row[$i]->fo_label))?$this->aAcc_Report_row[$i]->fo_label:"";
00145             $r.=$wName->input("text".$i);
00146             $r.= '</TD>';
00147 
00148             $r.='<td>';
00149             $search=new IPoste("form".$i);
00150             $search->size=50;
00151             $search->value=( isset($this->aAcc_Report_row[$i]->fo_formula))?$this->aAcc_Report_row[$i]->fo_formula:"";
00152             $search->label=_("Recherche poste");
00153             $search->set_attribute('gDossier',dossier::id());
00154             $search->set_attribute('bracket',1);
00155             $search->set_attribute('no_overwrite',1);
00156             $search->set_attribute('noquery',1);
00157             $search->set_attribute('account',$search->name);
00158             $search->set_attribute('ipopup','ipop_card');
00159 
00160             $r.=$search->input();
00161             $r.='</td>';
00162 
00163 
00164             $r.= "</TR>";
00165         }
00166 
00167         $r.= "</TABLE>";
00168         $wButton=new IButton();
00169         $wButton->javascript=" rapport_add_row('".dossier::id()."')";
00170         $wButton->label="Ajout d'une ligne";
00171         $r.=$wButton->input();
00172         return $r;
00173 
00174     }
00175     /*!\brief save into form and form_def
00176      */
00177     function save()
00178     {
00179 
00180         if ( strlen(trim($this->name)) == 0 )
00181             return;
00182         if ( $this->id == 0 )
00183             $this->insert();
00184         else
00185             $this->update();
00186 
00187     }
00188     function insert()
00189     {
00190         try
00191         {
00192             $this->db->start();
00193             $ret_sql=$this->db->exec_sql(
00194                          "insert into formdef (fr_label) values($1) returning fr_id",
00195                          array($this->name)
00196                      );
00197             $this->id=Database::fetch_result($ret_sql,0,0);
00198             $ix=1;
00199             foreach ( $this->aAcc_Report_row as $row)
00200             {
00201                 if ( strlen(trim($row->get_parameter("name"))) != 0 &&
00202                         strlen(trim($row->get_parameter("formula"))) != 0 )
00203                 {
00204                     $ix=($row->get_parameter("position")!="")?$row->get_parameter("position"):$ix;
00205                     $row->set_parameter("position",$ix);
00206                     $ret_sql=$this->db->exec_sql(
00207                                  "insert into form (fo_fr_id,fo_pos,fo_label,fo_formula)".
00208                                  " values($1,$2,$3,$4)",
00209                                  array($this->id,
00210                                        $row->fo_pos,
00211                                        $row->fo_label,
00212                                        $row->fo_formula)
00213                              );
00214                 }
00215             }
00216 
00217         }
00218         catch (Exception $e)
00219         {
00220             $this->db->rollback();
00221             echo $e->getMessage();
00222         }
00223         $this->db->commit();
00224 
00225     }
00226     function update()
00227     {
00228         try
00229         {
00230             $this->db->start();
00231             $ret_sql=$this->db->exec_sql(
00232                          "update formdef set fr_label=$1 where fr_id=$2",
00233                          array($this->name,$this->id));
00234             $ret_sql=$this->db->exec_sql(
00235                          "delete from form where fo_fr_id=$1",
00236                          array($this->id));
00237             $ix=0;
00238 
00239             foreach ( $this->aAcc_Report_row as $row)
00240             {
00241                 if ( strlen(trim($row->get_parameter("name"))) != 0 &&
00242                         strlen(trim($row->get_parameter("formula"))) != 0 )
00243                 {
00244                     $ix=($row->get_parameter("position")!="")?$row->get_parameter("position"):$ix;
00245                     $row->set_parameter("position",$ix);
00246                     $ret_sql=$this->db->exec_sql(
00247                                  "insert into form (fo_fr_id,fo_pos,fo_label,fo_formula)".
00248                                  " values($1,$2,$3,$4)",
00249                                  array($this->id,
00250                                        $row->fo_pos,
00251                                        $row->fo_label,
00252                                        $row->fo_formula)
00253                              );
00254                 }
00255             }
00256 
00257 
00258         }
00259         catch (Exception $e)
00260         {
00261             $this->db->rollback();
00262             echo $e->getMessage();
00263         }
00264         $this->db->commit();
00265     }
00266     /*!\brief fill a form thanks an array, usually it is $_POST
00267      *\param $p_array keys = fr_id, form_nom,textXX, formXX, posXX where
00268         XX is an number
00269      */
00270     function from_array($p_array)
00271     {
00272         $this->id=(isset($p_array['fr_id']))?$p_array['fr_id']:0;
00273         $this->name=(isset($p_array['form_nom']))?$p_array['form_nom']:"";
00274         $ix=0;
00275 
00276         $rr=new Acc_Report_Row();
00277         $rr->set_parameter("form_id",$this->id);
00278         $rr->set_parameter('database',$this->db);
00279 
00280         $this->aAcc_Report_row=$rr->from_array($p_array);
00281 
00282 
00283     }
00284     /*!\brief the fr_id MUST be set before calling
00285      */
00286 
00287 
00288     function load()
00289     {
00290         $sql=$this->db->exec_sql(
00291                  "select fr_label from formdef where fr_id=$1",
00292                  array($this->id));
00293         if ( Database::num_row($sql) == 0 ) return;
00294         $this->name=Database::fetch_result($sql,0,0);
00295         $sql=$this->db->exec_sql(
00296                  "select fo_id,fo_pos,fo_label,fo_formula ".
00297                  " from form ".
00298                  " where fo_fr_id=$1 order by fo_pos",
00299                  array($this->id));
00300         $f=Database::fetch_all($sql);
00301         $array=array();
00302         if ( ! empty($f) )
00303         {
00304             foreach ($f as $r)
00305             {
00306                 $obj=new Acc_Report_Row();
00307                 $obj->set_parameter("name",$r['fo_label']);
00308                 $obj->set_parameter("id",$r['fo_id']);
00309                 $obj->set_parameter("position",$r['fo_pos']);
00310                 $obj->set_parameter("formula",$r['fo_formula']);
00311                 $obj->set_parameter('database',$this->db);
00312                 $obj->set_parameter('form_id',$this->id);
00313                 $array[]=clone $obj;
00314             }
00315         }
00316         $this->aAcc_Report_row=$array;
00317 
00318     }
00319     function delete()
00320     {
00321         $ret=$this->db->exec_sql(
00322                  "delete from formdef where fr_id=$1",
00323                  array($this->id)
00324              );
00325     }
00326     /*!\brief get a list from formdef of all defined form
00327      *
00328      *\return array of object rapport
00329      *
00330      */
00331     function get_list()
00332     {
00333         $sql="select fr_id,fr_label from formdef order by fr_label";
00334         $ret=$this->db->exec_sql($sql);
00335         if ( Database::num_row($ret) == 0 ) return array();
00336         $array=Database::fetch_all($ret);
00337         $obj=array();
00338         foreach ($array as $row)
00339         {
00340             $tmp=new Acc_Report($this->db);
00341             $tmp->id=$row['fr_id'];
00342             $tmp->name=$row['fr_label'];
00343             $obj[]=clone $tmp;
00344         }
00345         return $obj;
00346     }
00347     /*!\brief To make a SELECT button with the needed value, it is used
00348      *by the SELECT widget
00349      *\return string with html code
00350      */
00351     function make_array()
00352     {
00353         $sql=$this->db->make_array("select fr_id,fr_label from formdef order by fr_label");
00354         return $sql;
00355     }
00356 
00357 
00358     /*!\brief write to a file the definition of a report
00359      * \param p_file is the file name (default php://output)
00360      */
00361     function export_csv($p_file)
00362     {
00363         $this->load();
00364 
00365         fputcsv($p_file,array($this->name));
00366 
00367         foreach ($this->aAcc_Report_row as $row)
00368         {
00369             fputcsv($p_file,array($row->get_parameter("name"),
00370                                   $row->get_parameter('position'),
00371                                   $row->get_parameter('formula'))
00372                    );
00373         }
00374 
00375     }
00376     /*!\brief upload a definition of a report and insert it into the
00377      * database
00378      */
00379     function upload()
00380     {
00381         if ( empty ($_FILES) ) return;
00382         if ( strlen(trim($_FILES['report']['tmp_name'])) == 0 )
00383         {
00384             alert("Nom de fichier est vide");
00385             return;
00386         }
00387         $file_report=tempnam('tmp','file_report');
00388         if (  move_uploaded_file($_FILES['report']['tmp_name'],$file_report))
00389         {
00390             // File is uploaded now we can try to parse it
00391             $file=fopen($file_report,'r');
00392             $data=fgetcsv($file);
00393             if ( empty($data) ) return;
00394             $this->name=$data[0];
00395             $array=array();
00396             while($data=fgetcsv($file))
00397             {
00398                 $obj=new Acc_Report_Row();
00399                 $obj->set_parameter("name",$data[0]);
00400                 $obj->set_parameter("id",0);
00401                 $obj->set_parameter("position",$data[1]);
00402                 $obj->set_parameter("formula",$data[2]);
00403                 $obj->set_parameter('database',$this->db);
00404                 $obj->set_parameter('form_id',0);
00405                 $array[]=clone $obj;
00406             }
00407             $this->aAcc_Report_row=$array;
00408             $this->insert();
00409         }
00410     }
00411     /**
00412      *@brief check if a report exist
00413      *@param $p_id, optional, if given check the report with this fr_id
00414      *@return return true if the report exist otherwise false
00415      */
00416     function exist($p_id=0)
00417     {
00418         $c=$this->id;
00419         if ( $p_id != 0 ) $c=$p_id;
00420         $ret=$this->db->exec_sql("select fr_label from formdef where fr_id=$1",array($c));
00421         if (Database::num_row($ret) == 0) return false;
00422         return true;
00423     }
00424     static function test_me()
00425     {
00426         $cn=new Database(dossier::id());
00427         $a=new Acc_Report($cn);
00428         print_r($a->get_list());
00429         $array=array("text0"=>"test1",
00430                      "form0"=>"7%",
00431                      "text1"=>"test2",
00432                      "form1"=>"6%",
00433                      "fr_id"=>110,
00434                      "form_nom"=>"Tableau"
00435                     );
00436         $a->from_array($array);
00437         print_r($a);
00438         echo '<form method="post">';
00439         echo $a->form(10);
00440 
00441         echo HtmlInput::submit('update','Enregistre');
00442         /* Add a line should be a javascript see comptanalytic */
00443         //  $r.= '<INPUT TYPE="submit" value="Ajoute une ligne" name="add_line">';
00444         echo HtmlInput::submit('del_form','Efface ce rapport');
00445         echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
00446         echo "</FORM>";
00447         if ( isset ($_POST['update']))
00448         {
00449             $b=new Acc_Report($cn);
00450             $b->from_array($_POST);
00451             echo '<hr>';
00452             print_r($b);
00453         }
00454     }
00455 }
00456 
00457 ?>