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

ecrit_ouv.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 /* $Revision: 1.8 $ */
00024 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00025 include_once ("ac_common.php");
00026 require_once('class_widget.php');
00027 require_once('class_poste.php');
00028 html_page_start($_SESSION['g_theme']);
00029 if ( ! isset ( $_SESSION['g_dossier'] ) ) {
00030   echo "You must choose a Dossier ";
00031   exit -2;
00032 }
00033 include_once ("postgres.php");
00034 /* Admin. Dossier */
00035 include_once ("class_user.php");
00036 $cn=DbConnect($_SESSION['g_dossier']);
00037 $User=new cl_user($cn);
00038 $User->Check();
00039 
00040 include_once ("check_priv.php");
00041 
00042 include_once ("user_menu.php");
00043 echo '<div class="u_tmenu">';
00044 echo ShowMenuCompta($_SESSION['g_dossier'],"user_advanced.php");
00045 echo '</div>';
00046 // \todo add a check for permission
00047 if ( $User->CheckAction($cn,EXP_IMP_ECR) == 0 ) {
00048   /* Cannot Access */
00049   NoAccess();
00050   exit -1;                      
00051  }
00052 
00053 echo ShowMenuAdvanced("ecrit_ouv.php");
00054 echo '<div class="lmenu">';
00055 
00056 echo ShowItem ( array (
00057                         array ("ecrit_ouv.php?export","Export"),
00058                         array ("ecrit_ouv.php?import","Import")
00059                         ),'V');
00060 echo '</div>';
00061 echo '<div class="redcontent">';
00062 //----------------------------------------------------- EXPORT ////////////////////////////////////////////
00063 if ( isset ($_GET['export'])) {
00064   // if the year is not set, ask it
00065   // ask the exercice and do the export
00066   $periode=make_array($cn,"select distinct p_exercice,p_exercice from parm_periode order by p_exercice");
00067   echo '<form method="GET" ACTION="export_ouv.php">';
00068   $w=new widget('select');
00069   $w->table=0;
00070   $w->label='Periode';
00071   $w->readonly=false;
00072   $w->value=$periode;
00073   $w->name="p_periode";
00074   echo 'P&eacute;riode : '.$w->IOValue();
00075   echo $w->Submit('export','Export');
00076   echo "</form>";
00077   exit(0);
00078 }
00079 //----------------------------------------------------- IMPORT ////////////////////////////////////////////
00080 if ( isset ($_GET['import'])) {
00081         // show a form to upload the file
00082         // that form will parse the file, create an ods operation
00083         // and ask you to validate it
00084   // if no file is given
00085   if ( ! isset ($_REQUEST['p_submit']) ) {
00086 ?>
00087 <FORM NAME="form_detail" enctype="multipart/form-data" ACTION="ecrit_ouv.php?import" METHOD="POST">
00088 <?
00089   // TODO propose  ODS ledger 
00090   $ods=make_array($cn,"select jrn_def_id,jrn_def_name from jrn_def where jrn_def_type = 'OD'");
00091   $x=new widget("select");
00092   $x->name='p_jrn';
00093   $x->value=$ods;
00094   echo "Choississez votre journal ".$x->IOValue();
00095 
00096         $w=new widget("file");
00097   $w->name='import_file';
00098   $w->label='p_file';
00099   echo $w->IOValue();
00100   echo $w->Submit('p_submit','Charger le fichier');
00101 ?>
00102 </FORM>
00103 <?
00104         exit(0);
00105   } else { 
00106     require_once("user_form_ods.php");
00107     require_once("jrn.php");
00108     // a file is given, so we upload it
00109     $new_name=tempnam('/tmp','import');
00110     if ( strlen ( $_FILES['import_file']['tmp_name']) != 0 ) {
00111       if ( move_uploaded_file($_FILES['import_file']['tmp_name'],$new_name) ) {
00112         // upload succeed
00113         $h_file=fopen($new_name,'r') ;
00114         // test if the file is opened
00115         if ( $h_file == false) { echo 'Je ne peux ouvrir pas ce fichier';exit(-1);}
00116         // Analyze the file and store result into array
00117         $valid=false;
00118         $idx=0;
00119         while ( !feof($h_file) ) {
00120 
00121           $line=fgets($h_file);
00122           // check if the first line contains the signature
00123           if ( $valid  ) {
00124             // skip blank line
00125             if (strlen (trim($line)) == 0 ) continue;
00126             // put the line into several array with the same index
00127             list($sign,$poste,$label,$amount)=explode(";",$line);
00128             $asign[$idx]=$sign; $aposte[$idx]=$poste;$aamount[$idx]=$amount;
00129             $alabel[$idx]=$label;
00130             $idx++;
00131             }
00132           $valid=(($line=="OUVERTURE\n" && $valid==false) || $valid)?true:false;
00133           
00134         } // read the file
00135         // if valid is still false then there is nothing to do
00136         if ( ! $valid) { echo 'Aucun enregistrement valide'; return ;}
00137         // compose the array for the function FormODS
00138         $array_ods['e_comm']='Ecriture d\'ouverture';
00139         for ($i=0;$i<$idx;$i++) {
00140                 $n="e_account$i";
00141                 $array_ods[$n]=$aposte[$i];
00142                 $n="e_account".$i."_type";
00143                 $array_ods[$n]=$asign[$i];
00144                 $n="e_account".$i."_amount";
00145                 $array_ods[$n]=$aamount[$i];
00146         }
00147         // Check if all the poste exist
00148         // otherwise create it
00149         for ($i=0;$i<$idx;$i++)
00150           {
00151             
00152             $p=new Poste($cn,$aposte[$i]);
00153 
00154             // if the poste exists then check the next one
00155             if ( $p->get() == true ) continue;
00156             echo 'Attention creation de '.$p->id.' '.$alabel[$i].'<br>';
00157             $sql=sprintf("select account_add(%d,'%s')",
00158                         $p->id,$alabel[$i]);
00159 
00160             ExecSql($cn,$sql);
00161           }
00162 
00163         // submit button in the form    
00164         $submit='<INPUT TYPE="SUBMIT" NAME="add_item" VALUE="Ajout Poste">
00165                     <INPUT TYPE="SUBMIT" NAME="view_invoice" VALUE="Sauver">';
00166          $r=FormODS($cn,$_POST['p_jrn'],$User->GetPeriode(),$submit,
00167                         $array_ods,false,$idx,$p_saved=false);
00168         
00169           echo $r;
00170           echo "<div><h4>On-line calculator</h4>".JS_CALC_LINE."<div>";
00171         
00172       } 
00173     }
00174   } // else -> a file is given
00175  }// if import
00176 // IF import and export are not set then the choice is proposed
00177 echo '</div>';
00178 html_page_stop(); 
00179 ?>