noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_impress.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 /*!\file
00022  * \brief contains function for the printing
00023 */
00024 require_once('class_periode.php');
00025 
00026 
00027 class Impress
00028 {
00029     /*! \brief   Purpose Parse a formula
00030      *
00031      * \param $p_cn connexion
00032      * \param $p_label
00033      * \param $p_formula
00034      * \param $p_eval  true if we eval here otherwise the function returns
00035      *                 a string which must be evaluated
00036      * \param $p_type_date : type of the date 0 for accountant period or 1
00037      * for calendar
00038      * \return array
00039      *
00040      *
00041      */
00042     static function parse_formula($p_cn,$p_label,$p_formula,$p_start,$p_end,$p_eval=true,$p_type_date=0,$p_sql="")
00043     {
00044         global $g_user;
00045         if ( Impress::check_formula($p_formula) == false)
00046         {
00047             if ( $p_eval == true)
00048                 return array('desc'=>$p_label.'  Erreur Formule!',
00049                              'montant'=>0);
00050             else
00051                 return $p_formula;
00052 
00053         }
00054         if ( $p_type_date == 0 )
00055             $cond=sql_filter_per($p_cn,$p_start,$p_end,'p_id','j_tech_per');
00056         else
00057             $cond="( j_date >= to_date('$p_start','DD.MM.YYYY') and j_date <= to_date('$p_end','DD.MM.YYYY'))";
00058 
00059         include_once("class_acc_account_ledger.php");
00060 
00061         while (preg_match_all("(\[[0-9]*[A-Z]*%*c*d*s*\])",$p_formula,$e) == true)
00062           {
00063 
00064             // remove the [ ]
00065             $x=$e[0];
00066             foreach ($x as $line)
00067               {
00068                 $compute='all';
00069                 if ( strpos($line,'d') != 0 )
00070                   $compute='deb';
00071                 if ( strpos($line,'c') != 0 )
00072                   $compute='cred';
00073                 if ( strpos($line,'s') != 0 )
00074                   $compute='signed';
00075                 $line=str_replace ("[","",$line);
00076                 $line=str_replace ("]","",$line);
00077                 $line=str_replace ("d","",$line);
00078                 $line=str_replace ("c","",$line);
00079                 $line=str_replace ("s","",$line);
00080                 // If there is a FROM clause we must recompute
00081                 // the time cond
00082 
00083                 if ($p_type_date == 0 && preg_match ("/FROM=[0-9]+\.[0-9]+/", $p_formula,$afrom) == 1 )
00084                   {
00085                     // There is a FROM clause
00086                     // then we must modify the cond for the periode
00087                     $from=str_replace("FROM=","",$afrom[0]);
00088 
00089                     // Get the periode
00090                     /*! \note special value for the clause FROM=00.0000
00091                      */
00092                     if ( $from == '00.0000' )
00093                       {
00094 
00095                         // retrieve the first month of this periode
00096                         $user_periode=$g_user->get_periode();
00097                         $oPeriode=new Periode($p_cn);
00098                         $periode=$oPeriode->get_exercice($user_periode);
00099                         list($first,$last)=$oPeriode->get_limit($periode);
00100                         $ret=$first->get_date_limit();
00101                         $end_date=$oPeriode->get_date_limit($p_end);
00102                         if ($ret == null ) throw new Exception ('Pas de limite à cette période',1);
00103                         $cond=sql_filter_per($p_cn,$ret['p_start'],$end_date['p_end'],'date','j_tech_per');
00104 
00105 
00106                       }
00107                     else
00108                       {
00109                         $oPeriode=new Periode($p_cn);
00110                         try
00111                           {
00112                             $pfrom=$oPeriode->find_periode('01.'.$from);
00113                             $cond=  sql_filter_per($p_cn, $pfrom, $p_end,'p_id','j_tech_per');
00114                           }
00115                         catch (Exception $exp)
00116                           {
00117                             /* if none periode is found
00118                                then we take the first periode of the year
00119                             */
00120                             $user_periode=$g_user->get_periode();
00121 
00122                             $year=$oPeriode->get_exercice($user_periode);
00123                             list($first,$last)=$oPeriode->get_limit($year);
00124                             $ret=$first->get_date_limit();
00125                             $end_date=$oPeriode->get_date_limit($p_end);
00126                             if ($ret == null ) throw new Exception ('Pas de limite à cette période',1);
00127                             $cond=sql_filter_per($p_cn,$ret['p_start'],$end_date['p_end'],'date','j_tech_per');
00128                           }
00129                       }
00130                   }
00131 
00132                 if ( strpos($p_formula,"FROM") != 0)
00133                   {
00134                     // We remove FROM out of the p_formula
00135                     $p_formula=substr_replace($p_formula,"",strpos($p_formula,"FROM"));
00136                   }
00137 
00138                 // Get sum of account
00139                 $P=new Acc_Account_Ledger($p_cn,$line);
00140                 $detail=$P->get_solde_detail($cond.$p_sql);
00141 
00142 
00143                 if ( $compute=='all')
00144                   $i=$detail['solde'];
00145                 if ( $compute=='deb')
00146                   $i=$detail['debit'];
00147                 if ( $compute=='cred')
00148                   $i=$detail['credit'];
00149                 if ( $compute=='signed')
00150                   $i=$detail['debit']-$detail['credit'];
00151                 $p_formula=str_replace($x[0],$i,$p_formula);
00152               }
00153           }
00154 
00155         // $p_eval is true then we eval and returns result
00156         if ( $p_eval == true)
00157         {
00158             $p_formula="\$result=".$p_formula.";";
00159             eval("$p_formula");
00160 
00161             while (preg_match("/\[([0-9]+)([Tt]*)\]/",trim($p_label),$e) == 1)
00162             {
00163                 $nom = "!!".$e[1]."!!";
00164                 if (Impress::check_formula($e[0]))
00165                 {
00166                     $nom = $p_cn->get_value ( "SELECT pcm_lib AS acct_name FROM tmp_pcmn WHERE pcm_val::text LIKE $1||'%' ORDER BY pcm_val ASC LIMIT 1",array($e[1]));
00167                     if($nom)
00168                     {
00169                         if($e[2] == 'T') $nom = strtoupper($nom);
00170                         if($e[2] == 't') $nom = strtolower($nom);
00171                     }
00172                 }
00173                 $p_label = str_replace($e[0], $nom, $p_label);
00174             }
00175 
00176             $aret=array('desc'=>$p_label,
00177                         'montant'=>$result);
00178             return $aret;
00179         }
00180         else
00181         {
00182             // $p_eval is false we returns only the string
00183             return $p_formula;
00184         }
00185     }
00186     /*!
00187      * \brief  Check if formula doesn't contain
00188      *           php injection
00189      * \param string
00190      *
00191      * \return true if the formula is good otherwise false
00192      */
00193     static function check_formula($p_string)
00194     {
00195         // the preg_match gets too complex if we want to add a test
00196         // for parenthesis, math function...
00197         // So I prefer remove them before testing
00198         $p_string=str_replace("round","",$p_string);
00199         $p_string=str_replace("abs","",$p_string);
00200         $p_string=str_replace("(","",$p_string);
00201         $p_string=str_replace(")","",$p_string);
00202         // for  the inline test like $a=(cond)?value:other;
00203         $p_string=str_replace("?","+",$p_string);
00204         $p_string=str_replace(":","+",$p_string);
00205         $p_string=str_replace(">=","+",$p_string);
00206         $p_string=str_replace("<=","+",$p_string);
00207         $p_string=str_replace(">","+",$p_string);
00208         $p_string=str_replace("<","+",$p_string);
00209         // eat Space + comma
00210         $p_string=str_replace(" ","",$p_string);
00211         $p_string=str_replace(",","",$p_string);
00212         // Remove D/C/S
00213         $p_string=str_replace("c","",$p_string);
00214         $p_string=str_replace("d","",$p_string);
00215         $p_string=str_replace("s","",$p_string);
00216         // Remove T,t
00217         $p_string=str_replace("t","",$p_string);
00218 
00219                 // remove date
00220                 $p_string=  preg_replace("/FROM*=*[0-9]+/", "", $p_string);
00221                 // remove comment
00222                 $p_string=  preg_replace("/#.*/", "", $p_string);
00223                 // remove $C=
00224                 $p_string=  preg_replace('/\$[a-z]*[A-Z]*[0-9]*[A-Z]*[a-z]*/', "", $p_string);
00225                 $p_string=  preg_replace('/=/', "", $p_string);
00226 
00227                 // remove account
00228                 $p_string=  preg_replace("/\[[0-9]*[A-Z]*%*\]/", "", $p_string);
00229 
00230                 $p_string=  preg_replace("/\+|-|\/|\*/", "", $p_string);
00231                 $p_string=  preg_replace("/[0-9]*\.*[0-9]/", "", $p_string);
00232 
00233                 //********************************************************************************************************************
00234                 // If the string is empty then formula should be good
00235                 //
00236                 //********************************************************************************************************************
00237                 if ($p_string == '')
00238         {
00239             return true;
00240         }
00241         else
00242         {
00243             return false;
00244         }
00245     }
00246      /**
00247      * with the handle of a successull query, echo each row into CSV and
00248      * send it directly
00249      * @param type $array of data
00250      * @param type $aheader  double array, each item of the array contains
00251      * a key type (num) and a key title
00252      */
00253     static function array_to_csv($array,$aheader)
00254     {
00255         $seq="";
00256         for ($i=0;$i<count($aheader);$i++)
00257         {
00258             echo $seq.'"'.$aheader[$i]['title'].'"';
00259             $seq=";";
00260         }
00261         printf("\r");
00262 
00263         $seq="";
00264         // fetch all the rows
00265         for ($i=0;$i<count($array);$i++)
00266         {
00267             $row=$array[$i];
00268             $sep2="";
00269                         $e=0;
00270             // for each rows, for each value
00271             foreach ($array[$i] as $key=>$value)
00272             {
00273                                 if ($e > count($aheader)) $e=0;
00274 
00275                                 if ( isset ($aheader[$e]['type']))
00276                                 {
00277                                         switch ($aheader[$e]['type'])
00278                                         {
00279                                                 case 'num':
00280                                                         echo $sep2.nb($value);
00281                                                         break;
00282                                                 default:
00283                                                         echo $sep2.'"'.$value.'"';
00284                                         }
00285                                 } else {
00286                                         echo '"'.$value.'"'.$sep2;
00287                                 }
00288                 $sep2=";";$e++;
00289             }
00290             printf("\r");
00291         }
00292     }
00293 }
 All Data Structures Namespaces Files Functions Variables Enumerations