Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

impress_inc.php

Go to the documentation of this file.
00001 <?
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 
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 // $Revision: 1.25 $
00022 
00036 function ViewImp($p_array,$p_cn) {
00037   include_once("preference.php");
00038   require_once("class_user.php");
00039 
00040   $periode=FormPeriodeMult($p_cn);
00041   foreach ( $p_array as $key=>$element) {
00042     echo_debug('impress_inc.php',__LINE__,"VIEWIMP $key $element");
00043     ${"$key"}=$element;
00044   }
00045   if ( ! isset($type) ) return;
00046   $centr="";
00047   if ( $action=="viewhtml")  {
00048     echo '<FORM ACTION=impress.php METHOD="GET">';
00049     if ( $type=="jrn")
00050           $centr='<BR>Centralisé : 
00051              <INPUT TYPE="CHECKBOX" NAME="central" unchecked><BR>'; 
00052   }
00053   else {
00054     if ( $type=="jrn") {
00055       echo '<FORM ACTION=send_jrn_pdf.php METHOD="GET">';
00056           $centr='<BR>Centralisé : 
00057              <INPUT TYPE="CHECKBOX" NAME="central" unchecked><BR>'; 
00058     }
00059     if ( $type=="poste")
00060       echo '<FORM ACTION=send_poste_pdf.php METHOD="GET">';
00061   }
00062   echo $periode;
00063   echo '<INPUT TYPE="HIDDEN" NAME="type" value="'.$type.'">';
00064   echo '<INPUT TYPE="HIDDEN" NAME="action" value="'.$action.'">';
00065 
00066   echo $centr;
00067 
00068   if ( isset($filter))
00069     echo '<INPUT TYPE="HIDDEN" NAME="filter" value="'.$filter.'">';
00070   if ( isset ($p_id)) {
00071     echo '<INPUT TYPE="HIDDEN" NAME="p_id" value="'.$p_id.'">';
00072   } else {
00073     include_once("poste.php");
00074     echo "<BR>";
00075     echo PosteForm($p_cn);
00076     echo ' <BR>Tous les postes ';
00077     echo '<INPUT TYPE="checkbox" NAME="all_poste"><BR>';
00078   }
00079   echo '<INPUT TYPE="SUBMIT" name="print" Value="Executer">';
00080 
00081   echo '</FORM>';
00082   echo "</DIV>";
00083 }
00097 function Imp($p_array,$p_cn) {
00098   if ( ! isset($p_array['action'])) {
00099     echo_error ("IMP no action specified"); return;
00100   }
00101   if ( $p_array['action']=="viewhtml") {
00102     return ImpHtml($p_array,$p_cn);
00103   }
00104   echo_error ("IMP no action specified"); return;
00105 }
00119 function ImpHtml($p_array,$p_cn) 
00120 {
00121   foreach($p_array as $key=>$element) {
00122     ${"$key"}=$element;
00123     echo_debug('impress_inc.php',__LINE__,"ImpHtml $key => $element");
00124   }
00125 
00126 
00127   $colvide="<TD></TD>";
00128   // formulaire
00129   if ( $type == "form" ) {
00130     if ( !isset ($periode)) return NO_PERIOD_SELECTED;
00131     $cond=CreatePeriodeCond($periode);
00132     $Res=ExecSql($p_cn,"select fo_id , 
00133                      fo_fr_id, 
00134                      fo_pos, 
00135                      fo_label, 
00136                      fo_formula,
00137                      fr_label from form 
00138                       inner join formdef on fr_id=fo_fr_id
00139                      where fo_fr_id=$p_id
00140                      order by fo_pos");
00141     $Max=pg_NumRows($Res);
00142     if ($Max==0) return $ret="";
00143     for ($i=0;$i<$Max;$i++) {
00144       $l_line=pg_fetch_array($Res,$i);
00145       $col=GetFormulaValue($p_cn,
00146                    $l_line['fo_label'],
00147                    $l_line['fo_formula'],$cond);
00148       echo "<div>";
00149       foreach ($col as $key=> $element) {
00150         echo "$element ";
00151       }
00152       echo "</div>";
00153     } //for ($i
00154 
00155   }//form
00156   if ($type=="poste") { 
00157     if ( ! isset ( $all_poste) && ! isset ( $poste )) return NO_POST_SELECTED;
00158     if ( !isset ($periode)) return NO_PERIOD_SELECTED;
00159     include_once("poste.php");
00160     $cond=CreatePeriodeCond($periode);
00161     $ret="" ;
00162     if ( isset ( $all_poste) ){ //choisit de voir tous les postes
00163       $r_poste=ExecSql($p_cn,"select pcm_val from tmp_pcmn");
00164       $nPoste=pg_numRows($r_poste);
00165       for ( $i=0;$i<$nPoste;$i++) {
00166         $t_poste=pg_fetch_array($r_poste,$i);
00167         $poste[]=$t_poste['pcm_val'];
00168       } 
00169     }      
00170     for ( $i =0;$i<count($poste);$i++) {
00171       list ($array,$tot_deb,$tot_cred)=GetDataPoste($p_cn,$poste[$i],$cond);
00172       if ( count($array) == 0) continue;
00173       $ret.=sprintf("<H2 class=\"info\">%d %s</H2>",
00174                     $poste[$i],GetPosteLibelle($p_cn,$poste[$i],1));
00175       $ret.="<TABLE style=\"border-bottom-style:solid; border-width:2px\" >";
00176       $i=0;
00177       foreach ($array as $col=>$element) {
00178         $i++;
00179         if ( $i %2 == 0) 
00180           $ret.="<tr class=\"even\">";
00181         else
00182           $ret.="<TR class=\"odd\">";
00183         $ret.=sprintf("<TD>%s</TD>",$element['j_date']);
00184         $ret.=sprintf("<TD>%s</TD>",$element['jr_internal']);
00185         //      $ret.=sprintf("<TD>jrn:%s</TD>",$element['jrn_name']);
00186         $ret.=sprintf("<TD>%s</TD>",$element['description']);
00187         if ( $element['j_debit']=='t') {
00188           $ret.=sprintf("<TD> debit</TD><TD ALIGN=\"right\">   % 8.2f</TD> $colvide",
00189                         $element['deb_montant']);
00190         } else {
00191           $ret.=sprintf("<TD>credit</TD> $colvide <TD ALIGN=\"right\">  % 8.2f</TD>",
00192                         $element['cred_montant']);
00193           
00194         }
00195         $ret.="</TR>";
00196       }//foreach
00197       
00198       $ret.=sprintf("$colvide $colvide $colvide $colvide ".
00199                     "<TD ALIGN=\"right\">% 8.2f</TD>".
00200                     "<TD ALIGN=\"right\">% 8.2f</TD>",
00201                     $tot_deb,
00202                     $tot_cred);
00203       $ret.="</TABLE>";
00204       $ret.="<p>Total débit :".$tot_deb."   Total Crédit:".$tot_cred."</p>";     
00205       if ( $tot_deb > $tot_cred ) {
00206         $solde_t="D"; 
00207         $solde=$tot_deb-$tot_cred;
00208         }else {
00209         $solde_t="C";
00210         $solde=$tot_cred-$tot_deb;
00211         }
00212       $ret.=" <p><B> Solde  $solde_t = ".$solde."</B></p>";
00213     }// for i
00214     return $ret;
00215   }//poste
00216   if ($type=="jrn") {
00217     if ( !isset ($periode)) return NO_PERIOD_SELECTED;
00218 
00219     echo_debug('impress_inc.php',__LINE__,"imp html journaux");
00220     $ret="";
00221     if (isset($filter)) {
00222       $array=GetDataJrn($p_cn,$p_array,$filter);
00223     }
00224     $cass="";
00225     $c=0;
00226 
00227     foreach ($array as $a=>$e2) {
00228       //      echo_debug($ret);
00229       
00230       //cassure entre op
00231       if ( $cass!=$e2['grp'] ) {
00232         $cass=$e2['grp'];
00233         $ret.='<TR style="background-color:#89BEFF"><TD>'.$e2['j_date']."</TD>";
00234         $ret.="<TD>".$e2['jr_internal']."</TD><TD COLSPAN=4> ".$e2['comment']."</TD></TR>";     
00235       }
00236       $ret.="<TR>";
00237       $ret.=$colvide;
00238       
00239       if ($e2['debit']=='f') $ret.=$colvide;
00240       $ret.="<TD>".$e2['poste']."</TD>";
00241       if ($e2['debit']=='t') $ret.=$colvide;
00242       $ret.="<TD>".$e2['description']."</TD>";
00243       if ($e2['debit']=='f') $ret.=$colvide;
00244       $ret.="<TD>".$e2['montant']."</TD>";
00245       if ($e2['debit']=='t') $ret.=$colvide;
00246       $ret.="</TR>";
00247     }
00248     echo_debug($ret);
00249 
00250     return $ret ;
00251   }//jrn
00252 
00253 }
00267 function GetDataPoste($p_cn,$p_poste,$p_condition)
00268 {
00269   $Res=ExecSql($p_cn,"select to_char(j_date,'DD.MM.YYYY') as j_date,".
00270                "case when j_debit='t' then to_char(j_montant,'999999999.99') else ' ' end as deb_montant,".
00271                "case when j_debit='f' then to_char(j_montant,'999999999.99') else ' ' end as cred_montant,".
00272                " jr_comment as description,jrn_def_name as jrn_name,".
00273                "j_debit, jr_internal ".
00274 //             " case when j_debit='t' then 'debit' else 'credit' end as debit".
00275                " from jrnx left join jrn_def on jrn_def_id=j_jrn_def ".
00276                " left join jrn on jr_grpt_id=j_grpt".
00277                " where j_poste=".$p_poste." and ".$p_condition.
00278                " order by j_date::date");
00279   $array=array();
00280   $tot_cred=0;
00281   $tot_deb=0;
00282   $Max=pg_NumRows($Res);
00283   if ( $Max == 0 ) return null;
00284   for ($i=0;$i<$Max;$i++) {
00285     $array[]=pg_fetch_array($Res,$i);
00286     if ($array[$i]['j_debit']=='t') {
00287       $tot_deb+=$array[$i]['deb_montant'] ;
00288     } else {
00289       $tot_cred+=$array[$i]['cred_montant'] ;
00290     }
00291   }
00292   return array($array,$tot_deb,$tot_cred);
00293 }
00308 function GetDataJrn($p_cn,$p_array,$filter=YES)
00309 {
00310   if ( !isset ($p_array['periode']) ) return NO_PERIOD_SELECTED;
00311 
00312   if ( $filter==YES) {
00313     if ( ! isset ( $p_array['central'])){
00314       $cond=CreatePeriodeCond($p_array['periode']);
00315       $Res=ExecSql($p_cn,"select to_char(j_date,'DD.MM.YYYY') as j_date,
00316                 j_montant as montant,j_debit as debit,j_poste as poste,".
00317                "j_text as description,j_grpt as grp,jr_comment as comment,
00318                 j_rapt as oc,jr_internal from jrnx left join jrn on ".
00319                "jr_grpt_id=j_grpt where j_jrn_def=".$p_array['p_id'].
00320                " and ".$cond." order by j_date::date,j_grpt,j_debit desc");
00321     } else {
00322       // create 
00323       $cond=CreatePeriodeCond($p_array['periode'],"c_periode");
00324 
00325       $Res=ExecSql($p_cn,"select to_char(c_date,'DD.MM.YYYY') as j_date,
00326                 c_montant as montant,c_debit as debit,c_poste as poste,".
00327                    "c_description as description,c_grp as grp,c_comment as comment,
00328                 c_rapt as oc,c_internal as jr_internal from centralized left join jrn on ".
00329                    "jr_grpt_id=c_grp where c_jrn_def=".$p_array['p_id']." and ".
00330                    $cond." order by c_id,c_date,c_grp,c_debit desc");
00331     }
00332     
00333   } // if filter == YES
00334   if ( $filter == NO) {
00335     if ( ! isset ($p_array['central']) ) {
00336       $cond=CreatePeriodeCond($p_array['periode']);
00337       $Res=ExecSql($p_cn,"select to_char(j_date,'DD.MM.YYYY') as j_date,
00338                 j_montant as montant,j_debit as debit,j_poste as poste,".
00339                    "j_text as description,j_grpt as grp,jr_comment as comment,
00340                 j_rapt as oc,jr_internal from jrnx left join jrn on ".
00341                    "jr_grpt_id=j_grpt where ".
00342                    $cond." order by j_date::date,j_grpt,j_debit desc");
00343     } else {
00344       $cond=CreatePeriodeCond($p_array['periode'],"c_periode");
00345 
00346       $Res=ExecSql($p_cn,"select to_char(c_date,'DD.MM.YYYY') as j_date,
00347                 c_montant as montant,c_debit as debit,c_poste as poste,".
00348                    "c_description as description,c_grp as grp,c_comment as comment,
00349                 c_rapt as oc,c_internal as jr_internal from centralized left join jrn on ".
00350                    "jr_grpt_id=c_grp where ".
00351                    $cond." order by c_id,c_date,c_grp,c_debit desc");
00352     
00353     } 
00354   }// filter == no
00355   $array=array();
00356   $Max=pg_NumRows($Res);
00357   for ($i=0;$i<$Max;$i++) {
00358     $array[]=pg_fetch_array($Res,$i);
00359   }
00360   return $array;
00361 }
00374 function CreatePeriodeCond($p_periode,$p_field=" j_tech_per") {
00375   if ( count($p_periode) == 1) {
00376     return $p_field."=".$p_periode[0];
00377   }
00378 
00379   $cond_periode=" $p_field in (";
00380   // condition periode
00381   $old=0;
00382   $follow=0;
00383   foreach ( $p_periode as $per) {
00384     if ( $old == 0) { 
00385       $old=$per;
00386       $follow=1;
00387       continue;
00388     }
00389     if ( $per == $old+1 ) { 
00390       $old=$per;
00391       $follow++;
00392     }
00393     
00394   }// foreach
00395   if ( count($p_periode) == $follow) {
00396     $cond=$p_field." >= ".$p_periode[0].' and '.$p_field.' <= '.$p_periode[count($p_periode)-1];
00397     return $cond;
00398   }
00399 
00400   // condition periode
00401   foreach ( $p_periode as $per) {
00402     $cond_periode.=$per.",";
00403   }
00404   $cond_periode=substr($cond_periode,0,strlen($cond_periode)-1);
00405   $cond_periode.=")";
00406   return $cond_periode;
00407 }
00423 function GetDataJrnPdf($p_cn,$p_array,$p_limit,$p_offset)
00424 {
00425   echo_debug('impress_inc.php',__LINE__,"GetDataJrnPdf");
00426 
00427   if ( !isset ($p_array['periode']) ) return NO_PERIOD_SELECTED;
00428 
00429   if ( $p_array['filter']==YES) {
00430     $cond=CreatePeriodeCond($p_array['periode']);
00431     if ( ! isset ($p_array['central']) ) {
00432       // Journaux non centralisés
00433     $Res=ExecSql($p_cn,"select j_id,to_char(j_date,'DD.MM.YYYY') as j_date,
00434                       jr_internal,
00435                 case j_debit when 't' then j_montant::text else '   ' end as deb_montant,
00436                 case j_debit when 'f' then j_montant::text else '   ' end as cred_montant,
00437                 j_debit as debit,j_poste as poste,jr_montant , ".
00438                "pcm_lib as description,j_grpt as grp,jr_comment ,
00439                 jr_rapt as oc, j_tech_per as periode from jrnx left join jrn on ".
00440                  "jr_grpt_id=j_grpt ".
00441                  " left join tmp_pcmn on pcm_val=j_poste ".
00442                 " where j_jrn_def=".$p_array['p_id'].
00443                " and ".$cond." order by j_date::date asc,jr_internal,j_debit desc".
00444                " limit ".$p_limit." offset ".$p_offset);
00445     }else {
00446       // Journaux centralisés
00447       $cond=CreatePeriodeCond($p_array['periode'],"c_periode");
00448       $Sql="select c_id as j_id,
00449             c_j_id,
00450             to_char (c_date,'DD.MM.YYYY') as j_date ,
00451             c_internal as jr_internal,
00452             case c_debit when 't' then c_montant::text else '   ' end as deb_montant,
00453             case c_debit when 'f' then c_montant::text else '   ' end as cred_montant,
00454             c_debit as j_debit,
00455             c_poste as poste,
00456             pcm_lib as description,
00457             jr_comment,
00458             jr_montant,
00459             c_grp as grp,
00460             c_comment as comment,
00461             c_rapt as oc,
00462             c_periode as periode 
00463             from centralized left join jrn on ".
00464                 "jr_grpt_id=c_grp left join tmp_pcmn on pcm_val=c_poste where ".
00465                 " c_jrn_def=".$p_array['p_id']." and ".
00466                 $cond." order by c_id ";
00467     $Res=ExecSql($p_cn,$Sql." limit ".$p_limit." offset ".$p_offset);
00468 
00469     }
00470   } else {
00471     // Grand Livre
00472     if (! isset($p_array['central'])) {
00473       // Non centralisé
00474       $cond=CreatePeriodeCond($p_array['periode']);
00475       $Res=ExecSql($p_cn,"select j_id,to_char(j_date,'DD.MM.YYYY') as j_date,
00476                       jr_internal,
00477                 case j_debit when 't' then j_montant::text else '   ' end as deb_montant,
00478                 case j_debit when 'f' then j_montant::text else '   ' end as cred_montant,
00479                 j_debit as debit,j_poste as poste,".
00480                "pcm_lib as description,j_grpt as grp,jr_comment as jr_comment,
00481                 jr_montant,
00482                 jr_rapt as oc, j_tech_per as periode from jrnx left join jrn on ".
00483                  "jr_grpt_id=j_grpt left join tmp_pcmn on pcm_val=j_poste where ".
00484                "  ".$cond." order by j_date::date,j_grpt,j_debit desc".
00485                " limit ".$p_limit." offset ".$p_offset);
00486 
00487     } else {
00488       // Centralisé
00489       $cond=CreatePeriodeCond($p_array['periode'],"c_periode");
00490       $Sql="select c_id as j_id,
00491             c_j_id,
00492             to_char (c_date,'DD.MM.YYYY') as j_date ,
00493             c_internal as jr_internal,
00494             case c_debit when 't' then c_montant::text else '   ' end as deb_montant,
00495             case c_debit when 'f' then c_montant::text else '   ' end as cred_montant,
00496             c_debit as j_debit,
00497             c_poste as poste,
00498             pcm_lib as description,
00499             jr_comment,
00500             jr_montant,
00501             c_grp as grp,
00502             c_comment as comment,
00503             c_rapt as oc,
00504             c_periode as periode 
00505             from centralized left join jrn on ".
00506                 "jr_grpt_id=c_grp left join tmp_pcmn on pcm_val=c_poste where ".
00507                 $cond." order by c_id ";
00508     $Res=ExecSql($p_cn,$Sql." limit ".$p_limit." offset ".$p_offset);
00509     } // Grand Livre
00510   }
00511 
00512 
00513   $array=array();
00514   $Max=pg_NumRows($Res);
00515   if ($Max==0) return null;
00516   $case="";
00517   $tot_deb=0;
00518   $tot_cred=0;
00519   for ($i=0;$i<$Max;$i++) {
00520     $line=pg_fetch_array($Res,$i);
00521     $mont_deb=($line['deb_montant']!=0)?sprintf("% 8.2f",$line['deb_montant']):"";
00522     $mont_cred=($line['cred_montant']!=0)?sprintf("% 8.2f",$line['cred_montant']):"";
00523     $jr_montant=($line['jr_montant']!=0)?sprintf("% 8.2f",$line['jr_montant']):"";
00524     $tot_deb+=$line['deb_montant'];
00525     $tot_cred+=$line['cred_montant'];
00526     echo_debug('impress_inc.php',__LINE__," GetJrnDataPdf : mont_Deb ".$mont_deb);
00527     echo_debug('impress_inc.php',__LINE__," GetJrnDataPdf : mont_cred ".$mont_cred);
00528 
00529     if ( $case != $line['grp'] ) {
00530       $case=$line['grp'];
00531       $array[]=array (
00532                       'j_id'=>$line['j_id'],
00533                       'j_date' => $line['j_date'],
00534                       'internal'=>$line['jr_internal'],
00535                       'deb_montant'=>'',
00536                       'cred_montant'=>'<b><i>'.$jr_montant.'</i></b>',
00537                       'description'=>'<b><i>'.$line['jr_comment'].'</i></b>',
00538                       'poste' => $line['oc'],
00539                       'periode' =>$line['periode'] );
00540 
00541       $array[]=array (
00542                       'j_id'=>$line['j_id'], 
00543                       'j_date' => '',
00544                       'internal'=>'',
00545                       'deb_montant'=>$mont_deb,
00546                       'cred_montant'=>$mont_cred,
00547                       'description'=>$line['description'],
00548                       'poste' => $line['poste'],
00549                       'periode' => $line['periode']
00550                       );
00551     
00552     }else {
00553       $array[]=array (
00554                       'j_id'=>$line['j_id'],
00555                       'j_date' => '',
00556                       'internal'=>'',
00557                       'deb_montant'=>$mont_deb,
00558                       'cred_montant'=>$mont_cred,
00559                       'description'=>$line['description'],
00560                       'poste' => $line['poste'],
00561                       'periode' => $line['periode']);
00562 
00563     }
00564       
00565 
00566   }
00567   echo_debug('impress_inc.php',__LINE__,"Total debit $tot_deb,credit $tot_cred");
00568   $a=array($array,$tot_deb,$tot_cred);
00569  return $a;
00570 }
00582 function GetDataGrpt($p_cn,$p_array)
00583 {
00584   if ( !isset ($p_array['periode']) ) return NO_PERIOD_SELECTED;
00585   $cond=CreatePeriodeCond($p_array['periode']);
00586   $Res=ExecSql($p_cn,"select distinct ".
00587                " j_grpt as grp".
00588                " from jrnx ".
00589                " where j_jrn_def=".$p_array['p_id'].
00590                " and ".$cond." order by j_grpt");
00591   $array=array();
00592   $Max=pg_NumRows($Res);
00593   $case="";
00594   for ($i=0;$i<$Max;$i++) {
00595     $array[]=pg_fetch_array($Res,$i);
00596   }
00597   return $array;
00598 }
00599 
00616 function GetRappel($p_cn,$p_jrnx_id,$p_jrn_id,$p_exercice,$which,$p_type,$p_central) 
00617 {
00618   include_once("preference.php");
00619 
00620   if ( $which == LAST) 
00621     $cmp="<="; 
00622   else
00623     $cmp="<";
00624 
00625   if ( $p_type == 1 ) {
00626     // Vue filtree => Journaux
00627     if ( $p_central == 0 ) { // Vue non centralisée
00628           return array(0,0);
00629     } // p_central == 0
00630 
00631     //     Vue filtree => Journaux & Jrn centralisé 
00632     if ( $p_central == 1 ) {
00633       $c_line=CountSql($p_cn,"select * from centralized left join parm_periode on c_periode=p_id ".
00634                        " where c_jrn_def=$p_jrn_id and  p_exercice='".$p_exercice."'".
00635                        " and c_order $cmp $p_jrnx_id ");
00636       
00637       if ($c_line == 0 ) { return array (0,0); }
00638       $sql="select sum(c_montant) as tot_amount ".
00639         " from centralized ".
00640         " left join parm_periode on c_periode=p_id ".
00641         " inner join jrn on jr_grpt_id=c_grp ".
00642         " where c_jrn_def=$p_jrn_id and ".
00643         " p_exercice='".$p_exercice."'".
00644         " and c_order $cmp $p_jrnx_id " ;
00645       $Res=ExecSql($p_cn,$sql." and c_debit='t' ");
00646       if ( pg_NumRows($Res) == 0 ) 
00647         $deb=0;
00648       else {
00649         $line=pg_fetch_array($Res,0);
00650         $deb=$line['tot_amount'];
00651       }
00652       
00653       $Res=ExecSql($p_cn,$sql." and c_debit='f' ");
00654       if ( pg_NumRows($Res) == 0 ) 
00655         $cred=0;
00656       else {
00657         
00658         $line=pg_fetch_array($Res,0);
00659         $cred=$line['tot_amount'];
00660       }
00661       echo_debug('impress_inc.php',__LINE__,"MONTANT $deb,$cred");
00662       $a=array($deb,$cred);
00663       return $a;
00664 
00665     }
00666   } // Type = jrn
00667   if ($p_type==0 ) { // Si Grand Livre, prendre donnée centralisée{
00668     if ( $p_central == 1) {
00669       $c_line=CountSql($p_cn,"select * from centralized left join parm_periode on c_periode=p_id ".
00670                        "where p_exercice='".$p_exercice."'".
00671                        " and c_id $cmp $p_jrnx_id ");
00672       
00673       if ($c_line == 0 ) { return array (0,0); }
00674       $sql="select sum(c_montant) as tot_amount ".
00675         " from centralized ".
00676         " left join parm_periode on c_periode=p_id ".
00677         " where ".
00678         " p_exercice='".$p_exercice."'".
00679         " and c_order $cmp $p_jrnx_id " ;
00680       $Res=ExecSql($p_cn,$sql." and c_debit='t' ");
00681       if ( pg_NumRows($Res) == 0 ) 
00682         $deb=0;
00683       else {
00684         $line=pg_fetch_array($Res,0);
00685         $deb=$line['tot_amount'];
00686       }
00687       
00688       $Res=ExecSql($p_cn,$sql." and c_debit='f' ");
00689       if ( pg_NumRows($Res) == 0 ) 
00690         $cred=0;
00691       else {
00692         
00693         $line=pg_fetch_array($Res,0);
00694         $cred=$line['tot_amount'];
00695       }
00696       echo_debug('impress_inc.php',__LINE__,"MONTANT $deb,$cred");
00697       $a=array($deb,$cred);
00698       return $a;
00699     } // central == 1
00700     else // Donnée non centralisée => pas de rappel
00701       {
00702         if ($p_central== 0) { // Si Grand Livre, prendre donnée non centralisée{
00703           return array(0,0);
00704         }
00705       }//else
00706   } // if type==0
00707   
00708 }
00709 
00726 function ParseFormula($p_cn,$p_label,$p_formula,$p_start,$p_end,$p_eval=true) {
00727   if ( CheckFormula($p_formula) == false) {
00728     if ( $p_eval == true)
00729       return array('desc'=>$p_label.'  Erreur Formule!',
00730                 'montant'=>0);
00731     else
00732       return $p_formula;
00733     
00734   }
00735 
00736   if ( $p_start == $p_end ) 
00737     $cond=" j_tech_per = $p_start ";
00738   else
00739     $cond = "(j_tech_per >= $p_start and j_tech_per <= $p_end) ";
00740   
00741   while (ereg("(\[[0-9]*%*\])",$p_formula,$e) == true) {
00742     include_once("class_poste.php");
00743     // remove the [ ] 
00744     $x=$e;
00745     $e[0]=str_replace ("[","",$e[0]);
00746     $e[0]=str_replace ("]","",$e[0]);
00747 
00748     // If there is a FROM clause we must recompute 
00749     // the time cond
00750     if ( ereg ("FROM=[0-9]+\.[0-9]+", $p_formula,$afrom) == true ){
00751       // There is a FROM clause 
00752       // then we must modify the cond for the periode
00753       $from=str_replace("FROM=","",$afrom[0]);
00754       // Get the periode 
00757       $from=getPeriodeFromMonth($p_cn,$from);
00758       if ( $from == '0000.00' ) {
00759         $User=new cl_user(DbConnect());
00760         $user_periode=$User-getPeriode();
00761         $sql_per="select min(p_id) from parm_periode where ".
00762           " p_exercice='".$user_periode."'";
00763         $ret=ExecSql($p_cn);
00764         $per="01".$user_periode;
00765         $cond="";
00766       } 
00767       else 
00768         {
00769           // the clause from is something else
00770           //  Compute the cond
00771           if ( $from == $p_end ) 
00772             $cond=" j_tech_per = $from ";
00773           else
00774             $cond = "(j_tech_per >= $from and j_tech_per <= $p_end) ";
00775         }
00776       // We remove FROM out of the p_formula
00777       $p_formula=substr_replace($p_formula,"",strpos($p_formula,"FROM"));
00778     }
00779       // Get sum of account
00780     $P=new poste($p_cn,$e[0]);
00781     $i=$P->GetSolde($cond);
00782     $p_formula=str_replace($x,$i,$p_formula);
00783   }
00784 
00785   // $p_eval is true then we eval and returns result
00786   if ( $p_eval == true) {
00787     $p_formula="\$result=".$p_formula.";";
00788     echo_debug('impress_inc.php',__LINE__, $p_formula);
00789     
00790     eval("$p_formula");
00791     $aret=array('desc'=>$p_label,
00792                 'montant'=>$result);
00793     return $aret;
00794   } else {
00795     // $p_eval is false we returns only the string
00796     return $p_formula;
00797   }
00798 }
00814 function GetFormulaValue($p_cn,$p_label,$p_formula,$p_cond) 
00815 {
00816   $aret=array();
00817   $l_debit=0;
00818   $l_credit=0;
00819     // somme debit
00820   $Res=ExecSql($p_cn,"select sum (j_montant) as montant from
00821                       jrnx where $p_cond and j_debit='t' and j_poste like '$p_formula'");
00822   if (pg_NumRows($Res)==0){
00823     $l_debit=0;                   
00824   } else {
00825     $l=pg_fetch_array($Res,0);
00826     $l_debit=$l['montant'];
00827       }
00828   // somme credit
00829   $Res=ExecSql($p_cn,"select sum (j_montant) as montant from
00830                       jrnx where $p_cond and j_debit='f' and j_poste like '$p_formula'");
00831   if (pg_NumRows($Res)==0) {
00832     $l_credit=0;                   
00833   } else {
00834     $l=pg_fetch_array($Res,0);
00835     $l_credit=$l['montant'];
00836   }
00837 
00838   if ( $l_credit==$l_debit) {
00839     $aret=array('desc' => $p_label,
00840                 'montant' => '0',
00841                 'cmontant'=>0);
00842   }
00843   if ( $l_credit < $l_debit) {
00844     $l2=sprintf("% .2f",$l_debit-$l_credit);
00845     $aret=array('desc' => $p_label,
00846                 'montant' => $l2,
00847                 'cmontant'=>$l2);
00848   }
00849   if ( $l_credit>$l_debit) {
00850     $l2=sprintf("(% .2f)",$l_credit-$l_debit);
00851     $aret=array('desc' => $p_label,
00852                 'montant' => $l2,
00853                 'cmontant'=> $l_debit-$l_credit);
00854 
00855   }
00856   return $aret;
00857 }
00869 function CheckFormula($p_string) {
00870   // the ereg gets too complex if we want to add a test
00871   // for parenthesis, math function...
00872   // So I prefer remove them before testing
00873   $p_string=str_replace("round","",$p_string);
00874   $p_string=str_replace("abs","",$p_string);
00875   $p_string=str_replace("(","",$p_string);
00876   $p_string=str_replace(")","",$p_string);
00877   // for  the inline test like $a=(cond)?value:other;
00878   $p_string=str_replace("?","+",$p_string);
00879   $p_string=str_replace(":","+",$p_string);
00880   $p_string=str_replace(">=","+",$p_string);
00881   $p_string=str_replace("<=","+",$p_string);
00882   $p_string=str_replace(">","+",$p_string);
00883   $p_string=str_replace("<","+",$p_string);
00884   // eat Space 
00885   $p_string=str_replace(" ","",$p_string);
00886 
00887   if ( ereg ("^(\\$[a-zA-Z]*[0-9]*=){0,1}((\[{0,1}[0-9]+\.*[0-9]*%{0,1}\]{0,1})+ *([+-\*/])* *(\[{0,1}[0-9]+\.*[0-9]*%{0,1}\]{0,1})*)*(([+-\*/])*\\$([a-zA-Z])+[0-9]*([+-\*/])*)* *( *FROM=[0-9][0-0].20[0-9][0-9]){0,1}$",$p_string) == false)
00888     {
00889       return false;
00890     } else {
00891       return true;
00892   }
00893 }
00894 
00895 ?>