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

import_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 Olivier Dzwoniarkiewicz
00021 // Modified Dany De Bontridder ddebontridder@yahoo.fr
00022 // $Revision$
00023 include_once("jrn.php");
00024 include_once("preference.php");
00025 include_once("user_common.php");
00026 require_once('class_user.php');
00027 require_once('class_widget.php');
00028 require_once('class_fiche.php');
00043 function ImportCSV($p_cn,$file,$p_bq_account,$p_format_csv,$p_jrn)
00044 {
00045         if(!$handle = fopen($file, "r")) {
00046                 print 'could not open file. quitting';
00047                 die;
00048         }
00049 
00050 StartSql($p_cn);
00051 
00052         
00053 // include the right format for CSV --> given by the <form
00054                 include($p_format_csv);
00055 
00056         
00057         echo "Importation terminée.";
00058 
00059 // if importation succeeds then we can commit the change
00060 Commit($p_cn);
00061 
00062 }
00066 function UpdateCSV($p_cn){
00067   $code=$_POST['code'];
00068   $count=$_POST['count'];
00069   $poste=$_POST['poste'.$count];
00070   $sql = "update import_tmp set poste_comptable='".$poste."' where code='".$code."'";
00071   $Res=ExecSql($p_cn,$sql);
00072 }
00073 
00074 
00075 
00076 
00084 function ShowBox($p_val,$counter,$p_form='form'){
00085 
00086   $w=new widget('js_search_only');
00087   $w->name='poste'.$counter;
00088   $w->extra='cred';
00089   $w->extra2=$p_val['jrn'];
00090   $w->label='';
00091   $w->table=0;
00092   if ( $p_form == 'remove' )
00093     $w->readonly=true;
00094 
00095   $s=new widget('span');
00096   
00097   if ( isset($p_val['poste_comptable']))
00098     {
00099       $w->value=$p_val['poste_comptable'];
00100       $cn=DbConnect($_SESSION['g_dossier']);
00101       $f=new fiche($cn);
00102       $f->GetByQCode($p_val['poste_comptable']);
00103       $s->value=$f->strAttribut(ATTR_DEF_NAME);
00104   }
00105   echo '<input type="hidden" name="code" value="'.$p_val['code'].'">';
00106   echo '<input type="hidden" name="count" value="'.$counter.'">';
00107   echo '<table border="1" width="500">';
00108   echo '<tr><td width="200">'.$p_val['code'].'</td><td width="200">'.$p_val['date_exec'].'</td><td width="100">'.$p_val['montant'].' EUR</td><tr/>';
00109   echo '<tr><td height="50" colspan="3">'.$p_val['detail'].'</td><tr/>';
00110   echo '<tr><td>'.$w->IOValue().' '.$s->IOValue('poste'.$counter.'_label').'</td>';
00111   echo "<td>n° compte : ".$p_val['num_compte']."</td>";
00112   if ( $p_form == 'form')
00113     echo '<td><input type="submit" value="modifier"></td><tr/>';
00114   if ($p_form == 'remove' )
00115     echo '<td><input type="submit" value="enlever"></td><tr/>';
00116 
00117   echo '</table>';
00118   
00119 }
00123 function RemoveCSV($cn)
00124 {
00125   var_dump($_POST);
00126   $sql="update import_tmp set poste_comptable=null where code='".$_POST['code']."'";
00127   ExecSql($cn,$sql);
00128 }
00129 
00133 function VerifImport($p_cn){
00134         $sql = "select * from import_tmp where poste_comptable='' or poste_comptable is null order by date_exec,code";
00135         $Res=ExecSql($p_cn,$sql);
00136         $Num=pg_NumRows($Res);
00137         echo $Num." opérations à complèter.<br/><br/>";
00138         $i=1;
00139         // include javascript for popup 
00140         echo JS_SEARCH_CARD;
00141         while($val = pg_fetch_array($Res)){
00142           echo '<form METHOD="POST" action="import.php?action=verif">'; 
00143           ShowBox($val,$i);
00144           echo '</form>';
00145           $i++;
00146         }
00147 
00148 }
00154 function ConfirmTransfert($p_cn,$periode){
00155   $sql = "select to_char(p_start,'DD-MM-YYYY') as p_start,to_char(p_end,'DD-MM-YYYY') as p_end".
00156     " from parm_periode where p_id = '".$periode."'";
00157   $Res=ExecSql($p_cn,$sql);
00158   $val = pg_fetch_array($Res);
00159   if ( $val == false )
00160     {
00161       echo "<script>".
00162         "alert ('Vous devez selectionner votre période dans vos préférences');".
00163         "</script>";
00164       exit();
00165     }
00166   $start ="to_date('".$val['p_start']."','DD-MM-YYYY')";   
00167   $end = "to_date('".$val['p_end']."','DD-MM-YYYY')";
00168 
00169   $sql = "select code,to_char(date_exec,'DD.MM.YYYY') as date_exec, ".
00170     " montant,num_compte,poste_comptable,bq_account,jrn,detail ".
00171     " from import_tmp where poste_comptable is not null and  poste_comptable <> '' 
00172           and   ok <> TRUE AND date_exec BETWEEN ".$start." and ".$end;
00173   
00174 
00175         
00176   $Res=ExecSql($p_cn,$sql);
00177   $Num=pg_NumRows($Res);
00178   echo $Num." opérations à complèter.<br/><br/>";
00179   if ( $Num == 0 ) return;
00180   $i=1;
00181   while($val = pg_fetch_array($Res)){
00182 
00183     echo '<form method="post" action="import.php">';
00184     echo '<input type="hidden" name="action" value="remove">';
00185     ShowBox($val,$i,'remove');
00186     echo '</form>';
00187     $i++;
00188   }
00189   echo '<form method="post" action="import.php">';
00190   echo '<input type="hidden" name="action" value="transfer">';
00191   echo '<input type="submit" name="sub" value="Commencer le transfert">';
00192   echo '</form>';
00193 
00194 }
00195 
00201 function TransferCSV($p_cn, $periode){
00202         //on obtient la période courante
00203   $User=new cl_user($p_cn);
00204   $periode = $User->GetPeriode();
00205   // on trouve les dates frontières de cette période
00206   $sql = "select to_char(p_start,'DD-MM-YYYY') as p_start,to_char(p_end,'DD-MM-YYYY') as p_end".
00207     " from parm_periode where p_id = '".$periode."'";
00208   $Res=ExecSql($p_cn,$sql);
00209   $val = pg_fetch_array($Res);
00210   if ( $val == false )
00211     {
00212       echo "<script>".
00213         "alert ('Vous devez selectionner votre période dans vos préférences');".
00214         "</script>";
00215       exit();
00216     }
00217   $start ="to_date('".$val['p_start']."','DD-MM-YYYY')";   
00218   $end = "to_date('".$val['p_end']."','DD-MM-YYYY')";
00219   // var_dump($val);
00220   $sql = "select code,to_char(date_exec,'DD.MM.YYYY') as date_exec, ".
00221     " montant,num_compte,poste_comptable,bq_account,jrn,detail ".
00222     " from import_tmp where poste_comptable is not null and  poste_comptable <> '' 
00223           and   ok <> TRUE AND date_exec BETWEEN ".$start." and ".$end;
00224   $Res=ExecSql($p_cn,$sql);
00225   //echo "boucle: ".sizeof($Res)."<br/>";
00226   //while($val = pg_fetch_array($Res)){
00227   $Max=pg_NumRows($Res);
00228   echo $Max." opérations à transférer.<br/>";
00229   StartSql($p_cn);
00230 
00231   for ($i = 0;$i < $Max;$i++) {
00232     $val=pg_fetch_array($Res,$i);
00233     
00234     $code=$val['code']; $date_exec=$val['date_exec']; $montant=$val['montant']; $num_compte=$val['num_compte']; 
00235     $poste_comptable=$val['poste_comptable'];$bq_account=$val['bq_account'];
00236     $jrn=$val['jrn']; $detail=$val['detail'];
00237     
00238 // Retrieve the account thx the quick code    
00239     $f=new fiche($p_cn);
00240     $f->GetByQCode($poste_comptable,false);
00241     $poste_comptable=$f->strAttribut(ATTR_DEF_ACCOUNT);
00242 
00243     // Vérification que le poste comptable trouvé existe
00244     if ( $poste_comptable == '- ERROR -')
00245       $test=0;
00246       else
00247         {
00248           $sqltest = "select * from tmp_pcmn WHERE pcm_val='".$poste_comptable."'";
00249           
00250           $Restest=ExecSql($p_cn,$sqltest);
00251           $test=pg_NumRows($Restest);
00252         }
00253 
00254     // Test it
00255     if($test == 0) {
00256       $sqlupdate = "update import_tmp set poste_comptable=null,  ok='f' WHERE code='".$code."' AND num_compte='".$num_compte."' or num_compte is null";
00257       $Resupdate=ExecSql($p_cn,$sqlupdate);
00258       echo "Poste comptable erronné pour l'opération ".$num_compte."-".$code.", réinitialisation du poste comptable<br/>";
00259       continue;
00260     }
00261          
00262       
00263       // Finances
00264       
00265       $seq=NextSequence($p_cn,'s_grpt');
00266       $p_user = $_SESSION['g_user'];
00267 
00268       $r=InsertJrnx($p_cn,"d",$p_user,$jrn,$bq_account,$date_exec,$montant,$seq,$periode);
00269       if ( $r == false) { $Rollback($p_cn);exit("error 'import_inc.php' __LINE__");}
00270       
00271       $r=InsertJrnx($p_cn,"c",$p_user,$jrn,$poste_comptable,$date_exec,$montant,$seq,$periode);
00272       if ( $r == false) { $Rollback($p_cn);exit("error 'import_inc.php' __LINE__");}
00273       
00274       //remove annoying double-quote
00275       $num_compte=str_replace('"','',$num_compte);
00276       $code=str_replace('\"','',$code);
00277       if ( strlen(trim($num_compte)) == 0 )
00278         $num_compte=$val['detail'];
00279 
00280       $r=InsertJrn($p_cn,$date_exec,NULL,$jrn,$detail.$num_compte." ".$code,$montant,$seq,$periode);
00281       if ( $r == false ) { Rollback($p_cn); exit(" Error 'import_inc.php' __LINE__");}
00282       
00283       SetInternalCode($p_cn,$seq,$jrn);
00284       
00285       
00286       echo "Tranfer de l'opération ".$code." effectué<br/>";
00287       $sql2 = "update import_tmp set ok=TRUE where code='".$code."'";
00288       $Res2=ExecSql($p_cn,$sql2);
00289       
00290         
00291   }
00292   Commit($p_cn);
00293 
00294 }
00305 function ShowFormTransfert($p_cn){
00306 $w=new widget("select");
00307   echo '<FORM METHOD="POST" action="import.php?action=import" enctype="multipart/form-data">';
00308   echo '<INPUT TYPE="file" name="fupload" size="20"><br>';
00309   // ask for the journal target 
00310   $jrn=make_array ($p_cn,"select jrn_def_id,jrn_def_name from jrn_def where jrn_def_type='FIN';");
00311   $w->label='Journal';
00312   echo $w->label." :".$w->IOValue('import_jrn',$jrn)."<br>";
00313   // choose the bank account
00314   $bq=make_array($p_cn,"select pcm_val,pcm_lib from tmp_pcmn where pcm_val like '550%'");
00315   $w->label='Banque';
00316   echo "Compte en banque :".$w->IOValue('import_bq',$bq)."<br>";
00317   $format_csv=make_array($p_cn,"select include_file,name from format_csv_banque;");
00318   $w->label="Format import";
00319   echo $w->label.$w->IOValue('format_csv',$format_csv).'<br>';
00320   echo '<INPUT TYPE="SUBMIT" Value="Import fiche">';
00321   echo '</FORM>';
00322 }
00323 ?>