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