00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once('class_periode.php');
00025
00026
00027 class Impress
00028 {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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
00081
00082
00083 if ($p_type_date == 0 && preg_match ("/FROM=[0-9]+\.[0-9]+/", $p_formula,$afrom) == 1 )
00084 {
00085
00086
00087 $from=str_replace("FROM=","",$afrom[0]);
00088
00089
00090
00091
00092 if ( $from == '00.0000' )
00093 {
00094
00095
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
00118
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
00135 $p_formula=substr_replace($p_formula,"",strpos($p_formula,"FROM"));
00136 }
00137
00138
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
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
00183 return $p_formula;
00184 }
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 static function check_formula($p_string)
00194 {
00195
00196
00197
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
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
00210 $p_string=str_replace(" ","",$p_string);
00211 $p_string=str_replace(",","",$p_string);
00212
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
00217 $p_string=str_replace("t","",$p_string);
00218
00219
00220 $p_string= preg_replace("/FROM*=*[0-9]+/", "", $p_string);
00221
00222 $p_string= preg_replace("/#.*/", "", $p_string);
00223
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
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
00235
00236
00237 if ($p_string == '')
00238 {
00239 return true;
00240 }
00241 else
00242 {
00243 return false;
00244 }
00245 }
00246
00247
00248
00249
00250
00251
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
00265 for ($i=0;$i<count($array);$i++)
00266 {
00267 $row=$array[$i];
00268 $sep2="";
00269 $e=0;
00270
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 }