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

class_document.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$ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 require_once('class_own.php');
00027 class Document 
00028 {
00029   var $db;          
00031   var $ag_id;       
00032   var $d_mimetype;  
00033   var $d_filename;  
00034   var $d_lob;       
00035   var $d_number;    
00036   var $md_id;       
00037   /* Constructor
00038    * \param $p_cn Database connection
00039    */
00040   function Document($p_cn,$p_d_id=0)
00041     {
00042       $this->db=$p_cn;
00043       $this->d_id=$p_d_id;
00044     } 
00055   function Generate() 
00056     {
00057       // create a temp directory in /tmp to unpack file and to parse it
00058       $dirname=tempnam('/tmp','doc_');
00059       echo $dirname;
00060       unlink($dirname);
00061       mkdir ($dirname);
00062       echo_debug('class_action',__LINE__,"Dirname is $dirname");
00063       // Retrieve the lob and save it into $dirname
00064       StartSql($this->db);
00065       $dm_info="select md_lob,md_filename,md_mimetype 
00066                    from document_modele where md_id=".$this->md_id;
00067       $Res=ExecSql($this->db,$dm_info);
00068 
00069       $row=pg_fetch_array($Res,0);
00070       $this->d_lob=$row['md_lob'];
00071       $this->d_filename=$row['md_filename'];
00072       $this->d_mimetype=$row['md_mimetype'];
00073 
00074       echo_debug('class_document',__LINE__,"OID is ".$row['md_lob']);
00076       chdir($dirname);
00077       $filename=$row['md_filename'];
00078       pg_lo_export($this->db,$row['md_lob'],$filename);
00079 
00080       // if the doc is a OOo, we need to unzip it first
00081       // and the name of the file to change is always content.xml
00082       if ( substr($row['md_mimetype'],'vnd.oasis') != 0 )
00083         {
00084           system("unzip ".$filename);
00085           // Remove the file we do  not need anymore
00086           unlink($filename);
00087           $file_to_parse="content.xml";
00088         }
00089       else 
00090         $file_to_parse=$filename;
00091       // affect a number
00092       $this->d_number=NextSequence($this->db,"seq_doc_type_".$this->md_id);
00093 
00094 
00095       // parse the document - return the doc number ?
00096       $this->ParseDocument($dirname,$file_to_parse);
00097 
00098       Commit($this->db);
00099       // if the doc is a OOo, we need to re-zip it 
00100       if ( substr($row['md_mimetype'],'vnd.oasis') != 0 )
00101         {
00102           system ("zip -r ".$filename." *");
00103           $file_to_parse=$filename;
00104         }
00105       // Create a directory 
00106       mkdir ($_SERVER['DOCUMENT_ROOT'].$dirname);
00107 
00108       // we need to rename the new generated file
00109       rename($dirname."/".$file_to_parse,$_SERVER['DOCUMENT_ROOT'].$dirname.'/'.$file_to_parse);
00110       $ret=sprintf('<A HREF="%s">Document généré</A>',
00111                    $dirname.'/'.$file_to_parse);
00112       $this->SaveGenerated($_SERVER['DOCUMENT_ROOT'].$dirname."/".$file_to_parse);
00113       return $ret;
00114     }
00115     
00125   function ParseDocument($p_dir,$p_file)
00126     {
00127 
00128       
00129       
00136       // open the document
00137       $infile_name=$p_dir."/".$p_file;
00138       echo_debug("class_document.php",__LINE__,"Open the document $p_dir/$p_file");
00139       $h=fopen($infile_name,"r");
00140       $output_name=tempnam($_SERVER["DOCUMENT_ROOT"]."/tmp","gen_doc_");
00141       $output_file=fopen($output_name,"w+");
00142       // check if the opening is sucessfull
00143       if (  $h == false ) 
00144         {
00145           echo "cannot open $p_dir $p_file ";
00146           exit();
00147         }
00148       if ( $output_file == false) 
00149         {
00150           echo "ne peut pas ouvrir le fichier de sortie";
00151           exit();
00152         }
00153       //read the file
00154       while(! feof($h)) 
00155         {
00156           // replace the tag
00157           $buffer=fgets($h);
00158           // search in the buffer the magic << and >>
00159           // while ereg finds something to replace
00160           while ( ereg ("<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>",$buffer,$f) )
00161             {
00162 
00163             echo_debug('class_document',__LINE__,var_export( $f,true));
00164             foreach ( $f as $pattern )
00165               {
00166                 echo_debug('class_document',__LINE__, "pattern");
00167                 echo_debug('class_document',__LINE__, var_export($pattern,true));
00168                 $to_remove=$pattern;
00169                 // we remove the < and > from the pattern
00170                 $pattern=str_replace('<','',$pattern);
00171                 $pattern=str_replace('>','',$pattern);
00172 
00173 
00174                 // if the pattern if found we replace it
00175                 $value=$this->Replace($pattern);
00176                 // replace into the $buffer
00177                 $buffer=str_replace($to_remove,$value,$buffer);
00178                 echo_debug('class_document',__LINE__, $buffer);
00179                 // if the pattern if found we replace it
00180                 echo_debug('class_document',__LINE__,"Transform $pattern by $value");
00181               }
00182           }
00183           // write into the output_file
00184           fwrite($output_file,$buffer);
00185 
00186         }
00187       fclose($h);
00188       fclose($output_file);
00189       rename ($output_name,$infile_name);
00190       // Save the document into the database
00191 
00192     }
00200   function SaveGenerated($p_file) 
00201     {
00202       // We save the generated file
00203       $doc=new Document($this->db);
00204       StartSql($this->db);
00205       $this->d_lob=pg_lo_import($this->db,$p_file);
00206       if ( $this->d_lob == false ) { 
00207         Rollback($this->db); echo_debug('class_document',__LINE__,"can't save file $p_file");
00208         return 1; }
00209     
00210       $sql=sprintf("insert into document(ag_id,d_lob,d_number,d_filename,d_mimetype) 
00211                         values (%d,%s,%d,'%s','%s')",
00212                    $this->ag_id,
00213                    $this->d_lob,
00214                    $this->d_number,
00215                    $this->d_filename,
00216                    $this->d_mimetype
00217                    );
00218       ExecSql($this->db,$sql);
00219       $this->d_id=GetSequence($this->db,"document_d_id_seq");
00220 
00221       Commit($this->db);
00222       return 0;
00223     }
00231   function Upload() 
00232     {
00233 
00234       // Start Transaction
00235       StartSql($this->db);
00236       $new_name=tempnam('/tmp','doc_');
00237       var_dump($_FILES);
00238       // nothing to save
00239       if ( sizeof($_FILES) == 0 ) return;
00240       // check if a file is submitted
00241       if ( strlen($_FILES['file_upload']['tmp_name']) != 0 )
00242         {
00243           // upload the file and move it to temp directory
00244           if ( move_uploaded_file($_FILES['file_upload']['tmp_name'],$new_name))
00245           {
00246             $oid=pg_lo_import($this->db,$new_name);
00247             // check if the lob is in the database
00248             if ( $oid == false ) 
00249               {
00250                 Rollback($this->db);
00251                 return 1;
00252               }
00253           }
00254           // the upload in the database is successfull
00255           $this->d_lob=$oid;
00256           $this->d_filename=$_FILES['file_upload']['name'];
00257           $this->d_mimetype=$_FILES['file_upload']['type'];
00258           // now we have to update the col.
00259           // We retrieve the row to remove a possible existing lob (replace)
00260           $sql="select d_lob from document where d_id=".$this->d_id;
00261           $ret=ExecSql($this->db,$sql);
00262 
00263           if (pg_num_rows($ret) != 0)  
00264             {
00265               // a result is found, the old oid is keept in order to
00266               // remove it later
00267               $r=pg_fetch_array($ret,0) ;
00268               $old_oid=$r['d_lob'] ;
00269               if (strlen($old_oid) != 0) { pg_lo_unlink ($this->db,$old_oid);}
00270             }
00271           // Update the table
00272           $sql=sprintf("update document set d_lob=%s,d_filename='%s',d_mimetype='%s' where d_id=%d",
00273                        $this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id);
00274           ExecSql($this->db,$sql);
00275           Commit($this->db);
00276         }
00277 
00278     }
00284   function a_ref() 
00285     {
00286       if ( $this->d_id == 0 )
00287         return '';
00288       $r="";
00289       $r='<A HREF="show_document.php?d_id='.$this->d_id.'">Document</A>';
00290       return $r;
00291     }
00292   /* ! Get
00293    * \brief Retrieve the document
00294    */
00295   function Get() 
00296     {
00297       // retrieve the template and generate document
00298       StartSql($this->db);
00299       $ret=ExecSql($this->db,
00300                    "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00301       if ( pg_num_rows ($ret) == 0 )
00302         return;
00303       $row=pg_fetch_array($ret,0);
00304       //the document  is saved into file $tmp
00305       $tmp=tempnam('/tmp/','document_');
00306       pg_lo_export($this->db,$row['d_lob'],$tmp);
00307       $this->d_mimetype=$row['d_mimetype'];
00308       $this->d_filename=$row['d_filename'];
00309 
00310       // send it to stdout
00311       ini_set('zlib.output_compression','Off');
00312       header("Pragma: public");
00313       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00314       header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00315       header("Cache-Control: must-revalidate");
00316       header('Content-type: "'.$this->d_mimetype.'"');
00317       header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00318       header("Accept-Ranges: bytes");
00319       $file=fopen($tmp,'r');
00320       while ( !feof ($file) )
00321         {
00322           echo fread($file,8192);
00323         }
00324       fclose($file);
00325       
00326       unlink ($tmp);
00327       
00328       Commit($this->db);
00329       
00330     }
00363   function Replace($p_tag)
00364     {
00365       $r="Tag inconnu";
00366       static $counter=0;
00367       switch ($p_tag)
00368         {
00369         case 'DATE':
00370           $r=' Date inconnue ';
00371           // Date are in $_REQUEST['ag_date'] 
00372           // or $_POST['e_date']
00373           if ( isset ($_REQUEST['ag_date']))
00374             $r=$_REQUEST['ag_date'];
00375           if ( isset ($_REQUEST['e_date']))
00376             $r=$_REQUEST['e_date'];
00377 
00378           break; 
00379           //
00380           //  the company priv
00381 
00382         case 'MY_NAME':
00383           $my=new own($this->db);
00384           $r=$my->MY_NAME;
00385           break;
00386         case 'MY_CP':
00387           $my=new own($this->db);
00388           $r=$my->MY_CP;
00389           break;
00390         case 'MY_COMMUNE':
00391           $my=new own($this->db);
00392           $r=$my->MY_COMMUNE;
00393           break;
00394         case 'MY_TVA':
00395           $my=new own($this->db);
00396           $r=$my->MY_TVA;
00397           break;
00398         case 'MY_STREET':
00399           $my=new own($this->db);
00400           $r=$my->MY_STREET;
00401           break;
00402         case 'MY_NUMBER':
00403           $my=new own($this->db);
00404           $r=$my->MY_NUMBER;
00405           break;
00406           // customer 
00407           /*\note The CUST_* are retrieved thx the $_REQUEST['tiers'] 
00408            * which contains the quick_code
00409            */
00410         case 'CUST_NAME':
00411           $tiers=new fiche($this->db);
00412           $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00413           $tiers->getByQcode($qcode,false);
00414           $r=$tiers->strAttribut(ATTR_DEF_NAME);
00415           break;
00416         case 'CUST_ADDR_1':
00417           $tiers=new fiche($this->db);
00418           $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00419           $tiers->getByQcode($qcode,false);
00420           $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00421           
00422           break ;
00423         case 'CUST_CP':
00424           $tiers=new fiche($this->db);
00425           $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00426           $tiers->getByQcode($qcode,false);
00427           $r=$tiers->strAttribut(ATTR_DEF_CP);
00428 
00429           break;
00430         case 'CUST_CO':
00431           $tiers=new fiche($this->db);
00432           $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00433           $tiers->getByQcode($qcode,false);
00434           $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00435 
00436           break; 
00437         case 'CUST_VAT':
00438           $tiers=new fiche($this->db);
00439           $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00440           $tiers->getByQcode($qcode,false);
00441           $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00442           break; 
00443           // Marchandise in $_POST['e_march*']
00444           // \see user_form_achat.php or user_form_ven.php
00445         case 'NUMBER':
00446           $r=$this->d_number;
00447           break;
00448           /*
00449            *  - [VEN_ART_NAME]
00450            *  - [VEN_ART_PRICE]
00451            *  - [VEN_ART_QUANT]
00452            *  - [VEN_ART_TVA_CODE]
00453            *  - [VEN_ART_TVA_AMOUNT]
00454            *  - [VEN_ART_STOCK_CODE]
00455            *  - [VEN_HTVA]
00456            *  - [VEN_TVAC]
00457            *  - [VEN_TVA]
00458            *  - [TOTAL_VEN_HTVA]
00459            */
00460         case 'MARCH_NEXT':
00461           $counter++;
00462           $r='';
00463           break;
00464           
00465         case 'VEN_ART_NAME':
00466           extract ($_POST);
00467           $id='e_march'.$counter;
00468           // check if the march exists
00469           if ( ! isset (${$id})) return "";
00470           // check that something is sold
00471           if ( ${'e_march'.$counter.'_sell'} != 0 && ${'e_quant'.$counter} != 0 )
00472             {
00473               $f=new fiche($this->db);
00474               $f->GetByQCode(${$id},false);
00475               $r=$f->strAttribut(ATTR_DEF_NAME);
00476             } else $r = "";
00477           break;
00478 
00479         case 'VEN_ART_PRICE':
00480           extract ($_POST);
00481           $id='e_march'.$counter.'_sell' ;
00482           if ( !isset (${$id}) ) return "";
00483           $r=${$id};
00484           break;
00485 
00486         case 'VEN_ART_TVA_CODE':
00487           extract ($_POST);
00488           $id='e_march'.$counter.'_tva_id';
00489           if ( !isset (${$id}) ) return "";
00490           $r=${$id};
00491           break;
00492 
00493         case 'VEN_ART_TVA_LABEL':
00494           extract ($_POST);
00495           $id='e_march'.$counter.'_tva_id';
00496           if ( !isset (${$id}) ) return "";
00497           $tva=GetTvaRate($this->db,${$id});
00498           if ( $tva == null || $tva==0 ) return "";
00499           $r=$tva['tva_label'];
00500           break;
00501 
00502 
00503         case 'VEN_ART_TVA_AMOUNT':
00504           extract ($_POST);
00505           $qt='e_quant'.$counter;
00506           $price='e_march'.$counter.'_sell' ;
00507           $tva='e_march'.$counter.'_tva_id';
00508           if ( !isset (${'e_march'.$counter}) ) return "";
00509           // check that something is sold
00510           if ( ${'e_march'.$counter.'_sell'} == 0 && ${'e_quant'.$counter} == 0 )
00511             return "";
00512           $a_tva=GetTvaRate($this->db,${$tva});
00513           // if no vat returns 0
00514           if ( $a_tva == null || $a_tva == 0 ) return 0;
00515           $r=round($price,2)*$qt*$a_tva['tva_rate'];
00516           $r=round($r,2);
00517           break;
00518 
00519         case 'VEN_ART_QUANT':
00520           extract ($_POST);
00521           $id='e_quant'.$counter;
00522           if ( !isset (${$id}) ) return "";
00523           // check that something is sold
00524           if ( ${'e_march'.$counter.'_sell'} == 0 && ${'e_quant'.$counter} == 0 )
00525             return "";
00526           $r=${$id};
00527           break;
00528 
00529         case 'VEN_HTVA':
00530           extract ($_POST);
00531           $id='e_march'.$counter.'_sell' ;
00532           $quant='e_quant'.$counter;
00533           // check that something is sold
00534           if ( ${'e_march'.$counter.'_sell'} == 0 && ${'e_quant'.$counter} == 0 )
00535             return "";
00541           $r=round(${$id}*${$quant},2); 
00542           break;
00543 
00544         case 'VEN_TVAC':
00545           extract ($_POST);
00546           $id='e_march'.$counter.'_sell' ;
00547           $quant='e_quant'.$counter;
00548           // check that something is sold
00549           if ( ${'e_march'.$counter.'_sell'} == 0 && ${'e_quant'.$counter} == 0 )
00550             return "";
00555           $r=${$id}*${$quant}; 
00556           $tva=GetTvaRate($this->db,${'e_march'.$counter.'_tva_id'});
00557           // if there is no vat we return now
00558           if ( $tva == null || $tva == 0 ) return $r;
00559           // we compute with the vat included
00560           $r=$r+$tva['tva_rate'];
00561           $r=round($r,2);
00562           break;
00563         case 'TOTAL_VEN_HTVA':
00564           extract($_POST);
00565           $sum=0.0;
00566           for ($i=0;$i<$nb_item;$i++)
00567             {
00568               $sum+=${'e_march'.$i.'_sell'}*$e{'e_quant'.$i};
00569             }
00570           $r=round($sum,2);
00571           break;
00572         case 'TOTAL_VEN_TVAC':
00573           extract($_POST);
00574           $sum=0.0;
00575           for ($i=0;$i<$nb_item;$i++)
00576             {
00577               $tva=GetTvaRate($this->db,${'e_march'.$i.'_tva_id'});
00578               $tva_rate=( $tva == null || $tva == 0 )?0.0:$tva['tva_rate'];
00579               $sum+=${'e_march'.$i.'_sell'}*${'e_quant'.$i}*(1+$tva_rate);
00580             }
00581           $r=round($sum,2);
00582 
00583           break;
00584 
00585         }
00586       return $r;
00587     }
00588 
00589 
00590 }

Generated on Fri Jun 23 00:12:45 2006 for phpcompta by  doxygen 1.4.4