class_acc_bilan.php

Go to the documentation of this file.
00001 <?php
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: 4456 $ */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /*!\file
00024  * \brief this class handle the different bilan, from the table bilan
00025  *
00026  */
00027 require_once("class_iselect.php");
00028 require_once ('class_database.php');
00029 require_once ('class_dossier.php');
00030 require_once ('class_impress.php');
00031 require_once ('header_print.php');
00032 require_once ('class_acc_account_ledger.php');
00033 
00034 /*!
00035  * \brief this class handle the different bilan, from the table bilan, parse the form and replace
00036  * in the template
00037  */
00038 class Acc_Bilan
00039 {
00040     var $db;                                            /*!< database connection */
00041     var $b_id;                                  /*!< id of the bilan (bilan.b_id) */
00042     var $from;                                  /*!< from periode */
00043     var $to;                                    /*!< end periode */
00044 
00045     function Acc_Bilan($p_cn)
00046     {
00047         $this->db=$p_cn;
00048     }
00049     /*!
00050      * \brief return a string with the form for selecting the periode and
00051      * the type of bilan
00052      * \param $p_filter_year filter on a year
00053      *
00054      * \return a string
00055      */
00056     function display_form($p_filter_year="")
00057     {
00058         $r="";
00059         $r.=dossier::hidden();
00060         $r.= '<TABLE>';
00061 
00062         $r.='<TR>';
00063 // filter on the current year
00064         $w=new ISelect();
00065         $w->table=1;
00066 
00067         $periode_start=$this->db->make_array("select p_id,to_char(p_start,'DD-MM-YYYY') from parm_periode $p_filter_year order by p_start,p_end");
00068 
00069         $periode_end=$this->db->make_array("select p_id,to_char(p_end,'DD-MM-YYYY') from parm_periode $p_filter_year order by p_end,p_start");
00070 
00071         $w->label=_("Depuis");
00072         $w->value=$this->from;
00073         $w->selected=$this->from;
00074         $r.= td($w->input('from_periode',$periode_start));
00075         $w->label=_(" jusque ");
00076         $w->value=$this->to;
00077         $w->selected=$this->to;
00078         $r.= td($w->input('to_periode',$periode_end));
00079         $r.= "</TR>";
00080         $r.="<tr>";
00081         $mod=new ISelect();
00082         $mod->table=1;
00083         $mod->value=$this->db->make_array("select b_id, b_name from bilan order by b_name");
00084         $mod->label=_("Choix du bilan");
00085         $r.=td($mod->input('b_id'));
00086         $r.="</tr>";
00087         $r.= '</TABLE>';
00088         return $r;
00089     }
00090     /*!\brief check and warn if an accound has the wrong saldo
00091      * \param $p_message legend of the fieldset
00092      * \param $p_type type of the Acccount ACT actif, ACTINV...
00093      * \param $p_type the saldo must debit or credit
00094      */
00095     private function warning($p_message,$p_type,$p_deb)
00096     {
00097         $sql="select pcm_val,pcm_lib from tmp_pcmn where pcm_type='$p_type'";
00098         $res=$this->db->exec_sql($sql);
00099         if ( Database::num_row($res) ==0 )
00100             return;
00101         $count=0;
00102         $nRow=Database::num_row($res);
00103 
00104         $ret="";
00105         $obj=new Acc_Account_Ledger($this->db,0);
00106         for ($i=0;$i<$nRow;$i++)
00107         {
00108 
00109             $line=Database::fetch_array($res,$i);
00110             /* set the periode filter */
00111             $sql=sql_filter_per($this->db,$this->from,$this->to,'p_id','j_tech_per');
00112             $obj->id=$line['pcm_val'];
00113 
00114             $solde=$obj->get_solde_detail($sql);
00115             $solde_signed=$solde['debit']-$solde['credit'];
00116 
00117             if (
00118                 ($solde_signed < 0 && $p_deb == 'D' ) ||
00119                 ($solde_signed > 0 && $p_deb == 'C' )
00120             )
00121             {
00122                 $ret.= '<li>Anomalie pour le compte '.$line['pcm_val'].
00123                        $line['pcm_lib'].
00124                        "  D: ".$solde['debit'].
00125                        "  C: ".$solde['credit']." diff ".$solde['solde'];
00126                 $count++;
00127             }
00128 
00129         }
00130 
00131         echo '<fieldset>';
00132         echo '<legend>'.$p_message.'</legend>';
00133         if ( $count <> 0 )
00134         {
00135             echo '<ol>'.$ret.'</ol>';
00136             echo '<span class="error">Nbres anomalies : '.$count.'</span>';
00137         }
00138         else
00139             echo " Pas d'anomalie d&eacute;tect&eacute;e";
00140         echo '</fieldset>';
00141 
00142 
00143     }
00144     /*!\brief verify that the saldo is good for the type of account */
00145     function verify()
00146     {
00147                 bcscale(2);
00148         echo '<h3> Comptes normaux </h3>';
00149         $this->warning('Actif avec un solde crediteur','ACT','D');
00150         $this->warning('Passif avec un solde debiteur','PAS','C');
00151         $this->warning('Compte de resultat : Charge avec un solde crediteur','CHA','D');
00152         $this->warning('Compte de resultat : produit avec un solde debiteur','PRO','C');
00153         echo '<hr>';
00154         echo '<h3> Comptes inverses </h3>';
00155         $this->warning('Compte inverse : actif avec un solde debiteur','ACTINV','C');
00156         $this->warning('Compte inverse : passif avec un solde crediteur','PASINV','D');
00157         $this->warning('Compte inverse : Charge avec un solde debiteur','CHAINV','C');
00158         $this->warning('Compte inverse : produit avec un solde crediteur','PROINV','D');
00159         echo '<h3>Solde </h3>';
00160         /* set the periode filter */
00161         $sql_periode=sql_filter_per($this->db,$this->from,$this->to,'p_id','j_tech_per');
00162         /* debit Actif */
00163         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00164              " where j_debit='t' and (pcm_type='ACT' or pcm_type='ACTINV')";
00165         $sql.="and $sql_periode";
00166         $debit_actif=$this->db->get_value($sql);
00167 
00168         /* Credit Actif */
00169         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00170              " where j_debit='f' and (pcm_type='ACT' or pcm_type='ACTINV')";
00171 
00172         $sql.="and $sql_periode";
00173 
00174         $credit_actif=$this->db->get_value($sql);
00175         $total_actif=abs(bcsub($debit_actif,$credit_actif));
00176         echo '<table >';
00177         echo tr(td( 'Total actif ').td($total_actif,'style="text-align:right"'));
00178 
00179         /* debit passif */
00180         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00181              " where j_debit='t' and (pcm_type='PAS' or pcm_type='PASINV') ";
00182         $sql.="and $sql_periode";
00183 
00184         $debit_passif=$this->db->get_value($sql);
00185 
00186         /* Credit Actif */
00187         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00188              " where j_debit='f' and (pcm_type='PAS' or pcm_type='PASINV') ";
00189         $sql.="and $sql_periode";
00190         $credit_passif=$this->db->get_value($sql);
00191         $total_passif=abs(bcsub($debit_passif,$credit_passif));
00192 
00193         /* diff actif / passif */
00194         echo tr(td('Total passif ').td($total_passif,'style="text-align:right"'));
00195         if ( $total_actif != $total_passif )
00196         {
00197             $diff=bcsub($total_actif,$total_passif);
00198             echo tr(td(' Difference Actif - Passif ').td($diff,'style="text-align:right"'),'style="font-weight:bolder"');
00199         }
00200 
00201         /* debit charge */
00202         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00203              " where j_debit='t' and (pcm_type='CHA' or pcm_type='CHAINV')";
00204         $sql.="and $sql_periode";
00205         $debit_charge=$this->db->get_value($sql);
00206 
00207         /* Credit charge */
00208         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00209              " where j_debit='f' and (pcm_type='CHA' or pcm_type='CHAINV')";
00210         $sql.="and $sql_periode";
00211         $credit_charge=$this->db->get_value($sql);
00212         $total_charge=abs(bcsub($debit_charge,$credit_charge));
00213         echo tr(td('Total charge ').td($total_charge,'style="text-align:right"'));
00214 
00215 
00216         /* debit prod */
00217         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00218              " where j_debit='t' and (pcm_type='PRO' or pcm_type='PROINV')";
00219         $sql.="and $sql_periode";
00220         $debit_pro=$this->db->get_value($sql);
00221 
00222         /* Credit prod */
00223         $sql="select sum(j_montant) from jrnx join tmp_pcmn on (j_poste=pcm_val)".
00224              " where j_debit='f' and (pcm_type='PRO' or pcm_type='PROINV')";
00225         $sql.="and $sql_periode";
00226         $credit_pro=$this->db->get_value($sql);
00227         $total_pro=abs(bcsub($debit_pro,$credit_pro));
00228         echo tr(td('Total produit ').td($total_pro,'style="text-align:right"'));
00229 
00230         $diff=bcsub($total_pro,$total_charge);
00231 
00232         echo tr( td("Difference Produit - Charge",'style="padding-right:20px"').td($diff,'style="text-align:right"'),'style="font-weight:bolder"');
00233         echo '</table>';
00234     }
00235     /*!
00236      * \brief get data from the $_GET
00237      *
00238      */
00239     function get_request_get()
00240     {
00241         $this->b_id=(isset($_GET['b_id']))?$_GET['b_id']:"";
00242         $this->from=( isset ($_GET['from_periode']))?$_GET['from_periode']:-1;
00243         $this->to=( isset ($_GET['to_periode']))?$_GET['to_periode']:-1;
00244     }
00245     /*!\brief load from the database the document data  */
00246     function load()
00247     {
00248         try
00249         {
00250             if ( $this->b_id=="")
00251                 throw new Exception("le formulaire id n'est pas donnee");
00252 
00253             $sql="select b_name,b_file_template,b_file_form,lower(b_type) as b_type from bilan where".
00254                  " b_id = ".$this->b_id;
00255             $res=$this->db->exec_sql($sql);
00256 
00257             if ( Database::num_row($res)==0)
00258                 throw new Exception ('Aucun enregistrement trouve');
00259             $array=Database::fetch_array($res,0);
00260             foreach ($array as $name=>$value)
00261             $this->$name=$value;
00262 
00263         }
00264         catch(Exception $Ex)
00265         {
00266             echo $Ex->getMessage();
00267             exit();
00268         }
00269     }
00270     /*!\brief open the file of the form */
00271     /*\return an handle to this file */
00272     function file_open_form()
00273     {
00274         $form=fopen($this->b_file_form,'r');
00275         if ( $form == false)
00276         {
00277             echo 'Cannot Open';
00278             exit();
00279         }
00280         return $form;
00281     }
00282     /*!\brief open the file with the template */
00283     /*\return an handle to this file */
00284     function file_open_template()
00285     {
00286         $templ=fopen($this->b_file_template,'r');
00287         if ( $templ == false)
00288         {
00289             echo 'Cannot Open';
00290             exit();
00291         }
00292         return $templ;
00293 
00294     }
00295     /*!
00296      * \brief Compute all the formula
00297      * \param $p_handle the handle to the file
00298      * \param
00299      * \param
00300      *
00301      *
00302      * \return
00303      */
00304     function compute_formula($p_handle)
00305     {
00306         while (! feof ($p_handle))
00307         {
00308             $buffer=trim(fgets($p_handle));
00309             // $a=(Impress::check_formula($buffer)  == true)?"$buffer ok<br>":'<font color="red">'.'Pas ok '.$buffer."</font><br>";
00310             // echo $a;
00311             // blank line are skipped
00312             if (strlen(trim($buffer))==0)
00313                 continue;
00314             // skip comment
00315             if ( strpos($buffer,'#') === true )
00316                 continue;
00317             // buffer contains a formula A$=....
00318             // We need to eval it
00319             $a=Impress::parse_formula($this->db,"$buffer",$buffer,$this->from,$this->to,false);
00320             $b=str_replace("$","\$this->",$a);
00321             if ( eval("$b;") === false )
00322                 echo_debug(__FILE__,__LINE__,"Code failed with $b");
00323 
00324 
00325         }// end read form line per line
00326     }
00327     /*!\brief generate the ods document
00328     * \param the handle to the template file
00329     * \return the xml
00330     *@note
00331     * Sur une seule ligne il y a plusieurs données, donc il y a plusieurs boucles, pour les autres documents
00332     * cela devrait être fait aussi, actuellement ces documents, n'acceptent qu'une formule par ligne.
00333     *@note
00334     * Pas de header dans les entêtes car n'est pas compris dans le document qu'on utilise
00335     */
00336     function generate_odt()
00337     {
00338         // create a temp directory in /tmp to unpack file and to parse it
00339         $dirname=tempnam($_ENV['TMP'],'bilan_');
00340 
00341 
00342         unlink($dirname);
00343         mkdir ($dirname);
00344         chdir($dirname);
00345 
00346         $file_base=dirname($_SERVER['SCRIPT_FILENAME']).DIRECTORY_SEPARATOR.$this->b_file_template;
00347         $work_file=basename($file_base);
00348         if ( copy ($file_base,$work_file) == false )
00349         {
00350             echo "Je ne peux pas ouvrir ce fichier ";
00351             exit();
00352         }
00353         ob_start();
00354         /* unzip the document */
00355         $zip = new Zip_Extended;
00356         if ($zip->open($work_file) === TRUE)
00357           {
00358             $zip->extractTo($dirname.DIRECTORY_SEPARATOR);
00359             $zip->close();
00360           } else
00361           {
00362             echo __FILE__.":".__LINE__."cannot unzip model ".$filename;
00363           }
00364 
00365         ob_end_clean();
00366         unlink($work_file);
00367         // remove the zip file
00368         $p_file=fopen('content.xml','r');
00369 
00370         if ( $p_file == false)
00371         {
00372             echo 'Cannot Open';
00373             exit();
00374         }
00375 
00376         $r="";
00377         $regex="/&lt;&lt;\\$[A-Z]*[0-9]*&gt;&gt;/";
00378         $lt="&lt;";
00379         $gt="&gt;";
00380         $header_txt=header_txt($this->db);
00381 
00382         while ( !feof($p_file) )
00383         {
00384             $line_rtf=fgets($p_file);
00385 
00386             /*
00387              * replace the header tag, doesn't work if inside header
00388              */
00389             $line_rtf=preg_replace('/&lt;&lt;header&gt;&gt;/',$header_txt,$line_rtf);
00390 
00391 
00392             // the line contains the magic <<
00393             $tmp="";
00394 
00395 
00396             while (preg_match_all($regex,$line_rtf,$f2) > 0 )
00397               {
00398                 // the f2 array contains all the magic << in the line
00399                 foreach ($f2 as $f2_array)
00400                   {
00401                     foreach ($f2_array as $f2_str)
00402                       {
00403                         $to_remove=$f2_str;
00404                         $f2_value=str_replace("&lt;","",$f2_str);
00405                         $f2_value=str_replace("&gt;","",$f2_value);
00406                         $f2_value=str_replace("$","",$f2_value);
00407 
00408 
00409 
00410                         // check for missing variables and labels (N vars)
00411                         if( ! isset($this->$f2_value))
00412                           {
00413 
00414                             $a = "!!".$f2_value."!!";
00415                             if( substr($f2_value, 0, 1) == "N" )
00416                               {
00417                                 $ret = $this->db->get_array("SELECT pcm_lib AS acct_name FROM tmp_pcmn WHERE pcm_val::text LIKE ".
00418                                                             " substr($1, 2)||'%' ORDER BY pcm_val ASC LIMIT 1",array($f2_value));
00419                                 if($ret[0]['acct_name'])
00420                                   {
00421                                     $a = $ret[0]['acct_name'];
00422                                     $a=str_replace('<','&lt;',$a);
00423                                     $a=str_replace('>','&gt;',$a);
00424                                   }
00425                               }
00426                           }
00427                         else
00428                           {
00429                             $a=$this->$f2_value;
00430                           }
00431                         if ( $a=='-0' ) $a=0;
00432 
00433                         /*  allow numeric cel in ODT for the formatting and formula */
00434                         if ( is_numeric($a) )
00435                           {
00436                             $searched='office:value-type="string"><text:p>'.$f2_str;
00437                             $replaced='office:value-type="float" office:value="'.$a.'"><text:p>'.$f2_str;
00438                             $line_rtf=str_replace($searched, $replaced, $line_rtf);
00439                           }
00440 
00441 
00442                         $line_rtf=str_replace($f2_str,$a,$line_rtf);
00443 
00444                       }// foreach end
00445                   } // foreach
00446               } // preg_match_all
00447             $r.=$line_rtf;
00448 
00449         }// odt file is read
00450 
00451         return $r;
00452 
00453     }
00454 
00455     /*!
00456      * \brief generate the plain  file (rtf,txt, or html)
00457      * \param the handle to the template file
00458      */
00459     function generate_plain($p_file)
00460     {
00461         $r="";
00462         if ( $this->b_type=='html')
00463         {
00464             $lt='&lt;';
00465             $gt='&gt;';
00466             $pattern='/&lt;&lt;header&gt;&gt;/';
00467         }
00468         else
00469         {
00470             $lt='<';
00471             $gt='>';
00472             $pattern='/<<header>>/';
00473         }
00474 
00475         $header_txt=header_txt($this->db);
00476 
00477         while ( !feof($p_file) )
00478         {
00479             $line_rtf=fgets($p_file);
00480 
00481             $line_rtf=preg_replace($pattern,$header_txt,$line_rtf);
00482 
00483 
00484             // the line contains the magic <<
00485             if (preg_match_all("/".$lt.$lt."\\$[a-zA-Z]*[0-9]*".$gt.$gt."/",$line_rtf,$f2) > 0)
00486             {
00487                 // DEBUG
00488                 //    echo $r.'<br>';
00489                 // the f2 array contains all the magic << in the line
00490                 foreach ($f2 as $f2_str)
00491                 {
00492                     // DEBUG
00493                     // echo "single_f2 = $f2_str <br>";
00494                     // replace single_f2 by its value
00495                     $f2_value=str_replace($lt,"",$f2_str);
00496                     $f2_value=str_replace($gt,"",$f2_value);
00497                     $f2_value=str_replace("$","",$f2_value);
00498                     $f2_value=$f2_value[0];
00499 
00500                     // check for missing variables and labels (N vars)
00501                     if( ! isset($this->$f2_value))
00502                     {
00503                         $a = "!!".$f2_value."!!";
00504                         if( substr($f2_value, 0, 1) == "N" )
00505                         {
00506                             $ret = $this->db->get_array("SELECT pcm_lib AS acct_name FROM tmp_pcmn WHERE ".
00507                                                         " pcm_val::text LIKE substr($1, 2)||'%' ORDER BY pcm_val ASC LIMIT 1",
00508                                                         array($f2_value));
00509                             if($ret[0]['acct_name'])
00510                             {
00511                                 /* for rtf we have the string to put it in latin1 */
00512                                 $a = utf8_decode($ret[0]['acct_name']);
00513                             }
00514                         }
00515                     }
00516                     else
00517                     {
00518                         // DEBUG
00519                         //echo "f2_value=$f2_value";
00520                         //                $a=${"$f2_value"};
00521                         $a=$this->$f2_value;
00522                     }
00523                     // DEBUG      echo " a = $a";
00524                     if ( $a=='-0' ) $a=0;
00525                     $line_rtf=str_replace($f2_str,$a,$line_rtf);
00526 
00527                 }// foreach end
00528             }
00529             $r.=$line_rtf;
00530 
00531         }// rtf file is read
00532         // DEBUG
00533         //  fwrite($out,$r);
00534 
00535         return $r;
00536 
00537 
00538 
00539 
00540     }
00541     /*!\brief generate the document and send it to the browser
00542      */
00543     function generate()
00544     {
00545         // Load the data
00546         $this->load();
00547         // Open the files
00548         $form=$this->file_open_form();
00549 
00550         // Compute all the formula and add the value to this
00551         $this->compute_formula($form);
00552         fclose($form);
00553         // open the form
00554         $templ=$this->file_open_template();
00555         switch ($this->b_type)
00556         {
00557         case 'rtf':
00558             $result=$this->generate_plain($templ);
00559             $this->send($result);
00560             break;
00561         case 'txt':
00562             $result=$this->generate_plain($templ);
00563             $this->send($result);
00564         case 'html':
00565             $result=$this->generate_plain($templ);
00566             $this->send($result);
00567 
00568             break;
00569         case 'odt':
00570         case 'ods':
00571             $result=$this->generate_odt($templ);
00572             $this->send($result);
00573             break;
00574 
00575         }
00576         fclose($templ);
00577     }
00578     /*!\brief send the result of generate plain to the browser
00579      * \param $p_result is the string returned by generate_...
00580      */
00581     function send($p_result)
00582     {
00583         switch ($this->b_type)
00584         {
00585         case 'rtf':
00586             // A rtf file is generated
00587             header('Content-type: application/rtf');
00588             header('Content-Disposition: attachment; filename="'.$this->b_name.'.rtf"');
00589             echo $p_result;
00590             break;
00591 
00592         case 'txt':
00593             // A txt file is generated
00594             header('Content-type: application/txt');
00595             header('Content-Disposition: attachment; filename="'.$this->b_name.'.txt"');
00596 
00597             echo $p_result;
00598             break;
00599         case 'html':
00600             // A txt file is generated
00601             header('Content-type: application/html');
00602             header('Content-Disposition: attachment; filename="'.$this->b_name.'.html"');
00603 
00604             echo $p_result;
00605             break;
00606         case 'odt':
00607         case 'ods':
00608             /*   header("Pragma: public");
00609             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00610             header("Cache-Control: must-revalidate");
00611             if ( $this->b_type == 'odt' )
00612             header('Content-type: application/vnd.oasis.opendocument.text');
00613             if ( $this->b_type == 'ods' )
00614             header('Content-type: application/vnd.oasis.opendocument.spreadsheet');
00615             header('Content-Disposition: attachment;filename="'.$this->b_name.'.odt"',FALSE);
00616             header("Accept-Ranges: bytes");
00617             */
00618             ob_start();
00619             // save the file in a temp folder
00620             // create a temp directory in /tmp to unpack file and to parse it
00621             $dirname=tempnam($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'tmp','bilan_');
00622 
00623 
00624             unlink($dirname);
00625             mkdir ($dirname);
00626             chdir($dirname);
00627             // create a temp directory in /tmp to unpack file and to parse it
00628             $file_base=dirname($_SERVER['SCRIPT_FILENAME']).DIRECTORY_SEPARATOR.$this->b_file_template;
00629             $work_file=basename($file_base);
00630             if ( copy ($file_base,$work_file) == false )
00631             {
00632                 echo "Je ne peux pas ouvrir ce fichier ";
00633                 exit();
00634             }
00635             /*
00636              * unzip the document
00637              */
00638             ob_start();
00639             $zip = new Zip_Extended;
00640             if ($zip->open($work_file) === TRUE)
00641               {
00642                 $zip->extractTo($dirname.DIRECTORY_SEPARATOR);
00643                 $zip->close();
00644               }
00645             else
00646               {
00647                 echo __FILE__.":".__LINE__."cannot unzip model ".$filename;
00648               }
00649 
00650             // Remove the file we do  not need anymore
00651             unlink ($work_file);
00652 
00653 
00654             // replace the file
00655             $p_file=fopen($dirname.DIRECTORY_SEPARATOR.'content.xml','wb');
00656             if ( $p_file == false )
00657             {
00658                 exit('Je ne peux pas ouvrir content.xml');
00659 
00660             }
00661             $a=fwrite($p_file,$p_result);
00662             if ( $a==false)
00663             {
00664                 echo "Je ne peux pas ecrire dans content.xml";
00665                 exit();
00666             }
00667             // repack
00668             $zip = new Zip_Extended;
00669             $res = $zip->open($this->b_name.".".$this->b_type, ZipArchive::CREATE);
00670             if($res !== TRUE)
00671               {
00672                 echo __FILE__.":".__LINE__."cannot recreate zip";
00673                 exit;
00674               }
00675             $zip->add_recurse_folder($dirname.DIRECTORY_SEPARATOR);
00676             $zip->close();
00677 
00678             ob_end_clean();
00679             fclose($p_file);
00680             $fdoc=fopen($dirname.DIRECTORY_SEPARATOR.$this->b_name.'.'.$this->b_type,'r');
00681             if ( $fdoc == false )
00682             {
00683                 exit('Je ne peux pas ouvrir ce document');
00684             }
00685             $buffer=fread ($fdoc,filesize($dirname.DIRECTORY_SEPARATOR.$this->b_name.'.'.$this->b_type));
00686             echo $buffer;
00687 
00688             break;
00689             // and send
00690         }
00691 
00692     }
00693     static function test_me()
00694     {
00695 
00696         if ( isset($_GET['result']))
00697         {
00698             ob_start();
00699             $cn=new Database(dossier::id());
00700             $a=new Acc_Bilan($cn);
00701             $a->get_request_get();
00702 
00703             $a->load();
00704             $form=$a->file_open_form();
00705             $a->compute_formula($form);
00706             fclose($form);
00707             // open the form
00708             $templ=$a->file_open_template();
00709             $r=$a->generate_odt($templ);
00710             fclose($templ);
00711             ob_end_clean();
00712 
00713             $a->send($r);
00714         }
00715         else
00716         {
00717             $cn=new Database(dossier::id());
00718             $a=new Acc_Bilan($cn);
00719             $a->get_request_get();
00720 
00721             echo '<form method="get">';
00722             echo $a->display_form();
00723             echo HtmlInput::hidden('test_select',$_GET['test_select']).dossier::hidden();
00724             echo HtmlInput::submit('result','Sauve');
00725             echo '</form>';
00726         }
00727     }
00728 }
00729