00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 require_once('class_own.php');
00022 require_once('class_poste.php');
00028 class Document
00029 {
00030 var $db;
00032 var $ag_id;
00033 var $d_mimetype;
00034 var $d_filename;
00035 var $d_lob;
00036 var $d_number;
00037 var $md_id;
00038 var $d_state;
00039
00040
00041
00042 function Document($p_cn,$p_d_id=0)
00043 {
00044 $this->db=$p_cn;
00045 $this->d_id=$p_d_id;
00046 }
00049 function blank()
00050 {
00051 $this->d_id=NextSequence($this->db,"document_d_id_seq");
00052
00053 $this->d_number=NextSequence($this->db,"seq_doc_type_".$this->md_type);
00054 $sql=sprintf('insert into document(d_id,ag_id,d_number) values(%d,%d,%d)',
00055 $this->d_id,
00056 $this->ag_id,
00057 $this->d_number);
00058 ExecSql($this->db,$sql);
00059
00060 }
00071 function Generate()
00072 {
00073
00074 $dirname=tempnam('/tmp','doc_');
00075 echo $dirname;
00076 unlink($dirname);
00077 mkdir ($dirname);
00078 echo_debug('class_action',__LINE__,"Dirname is $dirname");
00079
00080 StartSql($this->db);
00081 $dm_info="select md_type,md_lob,md_filename,md_mimetype
00082 from document_modele where md_id=".$this->md_id;
00083 $Res=ExecSql($this->db,$dm_info);
00084
00085 $row=pg_fetch_array($Res,0);
00086 $this->d_lob=$row['md_lob'];
00087 $this->d_filename=$row['md_filename'];
00088 $this->d_mimetype=$row['md_mimetype'];
00089
00090 echo_debug('class_document',__LINE__,"OID is ".$row['md_lob']);
00092 chdir($dirname);
00093 $filename=$row['md_filename'];
00094 pg_lo_export($this->db,$row['md_lob'],$filename);
00095 $type="n";
00096 echo_debug('class_document',__LINE__,'The document type is '.$row['md_mimetype']);
00097
00098
00099 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00100 {
00101 echo_debug('class_document',__LINE__,'Unzip the OOo');
00102 system("unzip ".$filename);
00103
00104 unlink($filename);
00105 $file_to_parse="content.xml";
00106 $type="OOo";
00107 }
00108 else
00109 $file_to_parse=$filename;
00110
00111 $this->d_number=NextSequence($this->db,"seq_doc_type_".$row['md_type']);
00112
00113
00114
00115 $this->ParseDocument($dirname,$file_to_parse,$type);
00116
00117 Commit($this->db);
00118
00119 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00120 {
00121 system ("zip -r ".$filename." *");
00122 $file_to_parse=$filename;
00123 }
00124
00125 mkdir ($_SERVER['DOCUMENT_ROOT'].$dirname);
00126
00127
00128 rename($dirname."/".$file_to_parse,$_SERVER['DOCUMENT_ROOT'].$dirname.'/'.$file_to_parse);
00129 $ret=sprintf('<A HREF="%s">Document généré</A>',
00130 $dirname.'/'.$file_to_parse);
00131 $this->SaveGenerated($_SERVER['DOCUMENT_ROOT'].$dirname."/".$file_to_parse);
00132 return $ret;
00133 }
00134
00145 function ParseDocument($p_dir,$p_file,$p_type)
00146 {
00147
00148 echo_debug('class_document',__LINE__,'Begin parsing of '.$p_dir.' '.$p_file.'Type = '.$p_type);
00149
00156
00157 $infile_name=$p_dir."/".$p_file;
00158 echo_debug("class_document.php",__LINE__,"Open the document $p_dir/$p_file");
00159 $h=fopen($infile_name,"r");
00160 $output_name=tempnam($_SERVER["DOCUMENT_ROOT"]."/tmp","gen_doc_");
00161 $output_file=fopen($output_name,"w+");
00162
00163 if ( $h == false )
00164 {
00165 echo "cannot open $p_dir $p_file ";
00166 exit();
00167 }
00168 if ( $output_file == false)
00169 {
00170 echo "ne peut pas ouvrir le fichier de sortie";
00171 exit();
00172 }
00173
00174 if ( $p_type=='OOo')
00175 {
00176 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00177 $lt="<";
00178 $gt=">";
00179 }
00180 else
00181 {
00182 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00183 $lt="<";
00184 $gt=">";
00185 }
00186
00187 while(! feof($h))
00188 {
00189
00190 $buffer=fgets($h);
00191
00192
00193 while ( ereg ($regex,$buffer,$f) )
00194 {
00195
00196 echo_debug('class_document',__LINE__,'var_export '.var_export( $f,true));
00197 foreach ( $f as $pattern )
00198 {
00199 echo_debug('class_document',__LINE__, "pattern");
00200 echo_debug('class_document',__LINE__, var_export($pattern,true));
00201 $to_remove=$pattern;
00202
00203 $pattern=str_replace($lt,'',$pattern);
00204 $pattern=str_replace($gt,'',$pattern);
00205
00206
00207
00208 $value=$this->Replace($pattern);
00209
00210
00211 $pos=strpos($buffer,$to_remove);
00212
00213 $len=strlen($to_remove);
00214 $buffer=substr_replace($buffer,$value,$pos,$len);
00215
00216
00217
00218 echo_debug('class_document',__LINE__,"Transform $pattern by $value");
00219 }
00220 }
00221
00222 fwrite($output_file,$buffer);
00223
00224 }
00225 fclose($h);
00226 fclose($output_file);
00227 rename ($output_name,$infile_name);
00228
00229
00230 }
00238 function SaveGenerated($p_file)
00239 {
00240 echo_debug('class_document',__LINE__,'Save generated');
00241
00242 $doc=new Document($this->db);
00243 StartSql($this->db);
00244 $this->d_lob=pg_lo_import($this->db,$p_file);
00245 if ( $this->d_lob == false ) {
00246 Rollback($this->db); echo_debug('class_document',__LINE__,"can't save file $p_file");
00247 return 1; }
00248
00249 $sql=sprintf("insert into document(ag_id,d_lob,d_number,d_filename,d_mimetype,d_state)
00250 values (%d,%s,%d,'%s','%s',%d)",
00251 $this->ag_id,
00252 $this->d_lob,
00253 $this->d_number,
00254 $this->d_filename,
00255 $this->d_mimetype,
00256 $this->d_state
00257 );
00258 ExecSql($this->db,$sql);
00259 $this->d_id=GetSequence($this->db,"document_d_id_seq");
00260 echo_debug('class_document',__LINE__,'document sauvé : d_id'.$this->d_id);
00261 Commit($this->db);
00262 return 0;
00263 }
00271 function Upload()
00272 {
00273
00274
00275 StartSql($this->db);
00276 $new_name=tempnam('/tmp','doc_');
00277
00278
00279 if ( sizeof($_FILES) == 0 ) return;
00280
00281 if ( strlen($_FILES['file_upload']['tmp_name']) != 0 )
00282 {
00283
00284 if ( move_uploaded_file($_FILES['file_upload']['tmp_name'],$new_name))
00285 {
00286 $oid=pg_lo_import($this->db,$new_name);
00287
00288 if ( $oid == false )
00289 {
00290 Rollback($this->db);
00291 return 1;
00292 }
00293 }
00294
00295 $this->d_lob=$oid;
00296 $this->d_filename=$_FILES['file_upload']['name'];
00297 $this->d_mimetype=$_FILES['file_upload']['type'];
00298
00299
00300 $sql="select d_lob from document where d_id=".$this->d_id;
00301 $ret=ExecSql($this->db,$sql);
00302
00303 if (pg_num_rows($ret) != 0)
00304 {
00305
00306
00307 $r=pg_fetch_array($ret,0) ;
00308 $old_oid=$r['d_lob'] ;
00309 if (strlen($old_oid) != 0) { pg_lo_unlink ($this->db,$old_oid);}
00310 }
00311
00312 $sql=sprintf("update document set d_lob=%s,d_filename='%s',d_mimetype='%s' where d_id=%d",
00313 $this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id);
00314 ExecSql($this->db,$sql);
00315 Commit($this->db);
00316 }
00317
00318 }
00324 function a_ref()
00325 {
00326 if ( $this->d_id == 0 )
00327 return '';
00328 $r="";
00329 $r='<A HREF="show_document.php?d_id='.$this->d_id.'">Document</A>';
00330 return $r;
00331 }
00332
00333
00334
00335 function Send()
00336 {
00337
00338 StartSql($this->db);
00339 $ret=ExecSql($this->db,
00340 "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00341 if ( pg_num_rows ($ret) == 0 )
00342 return;
00343 $row=pg_fetch_array($ret,0);
00344
00345 $tmp=tempnam('/tmp/','document_');
00346 pg_lo_export($this->db,$row['d_lob'],$tmp);
00347 $this->d_mimetype=$row['d_mimetype'];
00348 $this->d_filename=$row['d_filename'];
00349
00350
00351 ini_set('zlib.output_compression','Off');
00352 header("Pragma: public");
00353 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00354 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00355 header("Cache-Control: must-revalidate");
00356 header('Content-type: "'.$this->d_mimetype.'"');
00357 header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00358 header("Accept-Ranges: bytes");
00359 $file=fopen($tmp,'r');
00360 while ( !feof ($file) )
00361 {
00362 echo fread($file,8192);
00363 }
00364 fclose($file);
00365
00366 unlink ($tmp);
00367
00368 Commit($this->db);
00369
00370 }
00373 function get()
00374 {
00375 $sql="select * from document where d_id=".$this->d_id;
00376 $ret=Exec($this->db,$sql);
00377 if ( pg_num_rows($ret) == 0 )
00378 return;
00379 $row=pg_fetch_array($ret,0);
00380 $this->ag_id=$row['ag_id'];
00381 $this->d_mimetype=$row['d_mimetype'];
00382 $this->d_filename=$row['d_filename'];
00383 $this->d_lob=$row['d_lob'];
00384 $this->d_number=$row[''];
00385 $this->d_state=$row['ag_id'];
00386 $this->d_number=$row['d_number'];
00387
00388 }
00421 function Replace($p_tag)
00422 {
00423 $r="Tag inconnu";
00424 static $counter=0;
00425 switch ($p_tag)
00426 {
00427 case 'DATE':
00428 $r=' Date inconnue ';
00429
00430
00431 if ( isset ($_REQUEST['ag_date']))
00432 $r=$_REQUEST['ag_date'];
00433 if ( isset ($_REQUEST['e_date']))
00434 $r=$_REQUEST['e_date'];
00435
00436 break;
00437
00438
00439
00440 case 'MY_NAME':
00441 $my=new own($this->db);
00442 $r=$my->MY_NAME;
00443 break;
00444 case 'MY_CP':
00445 $my=new own($this->db);
00446 $r=$my->MY_CP;
00447 break;
00448 case 'MY_COMMUNE':
00449 $my=new own($this->db);
00450 $r=$my->MY_COMMUNE;
00451 break;
00452 case 'MY_TVA':
00453 $my=new own($this->db);
00454 $r=$my->MY_TVA;
00455 break;
00456 case 'MY_STREET':
00457 $my=new own($this->db);
00458 $r=$my->MY_STREET;
00459 break;
00460 case 'MY_NUMBER':
00461 $my=new own($this->db);
00462 $r=$my->MY_NUMBER;
00463 break;
00464 case 'MY_TEL':
00465 $my=new own($this->db);
00466 $r=$my->MY_TEL;
00467 break;
00468 case 'MY_FAX':
00469 $my=new own($this->db);
00470 $r=$my->MY_FAX;
00471 break;
00472 case 'MY_PAYS':
00473 $my=new own($this->db);
00474 $r=$my->MY_PAYS;
00475 break;
00476
00477
00478
00479
00480
00481 case 'SOLDE':
00482 $tiers=new fiche($this->db);
00483 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00484 $tiers->Getbyqcode($qcode,false);
00485 $p=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
00486 $poste=new Poste($this->db,$p);
00487 $r=$poste->GetSolde(' true' );
00488 break;
00489 case 'CUST_NAME':
00490 $tiers=new fiche($this->db);
00491 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00492 $tiers->getByQcode($qcode,false);
00493 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00494 break;
00495 case 'CUST_ADDR_1':
00496 $tiers=new fiche($this->db);
00497 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00498 $tiers->getByQcode($qcode,false);
00499 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00500
00501 break ;
00502 case 'CUST_CP':
00503 $tiers=new fiche($this->db);
00504 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00505 $tiers->getByQcode($qcode,false);
00506 $r=$tiers->strAttribut(ATTR_DEF_CP);
00507
00508 break;
00509 case 'CUST_CO':
00510 $tiers=new fiche($this->db);
00511 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00512 $tiers->getByQcode($qcode,false);
00513 $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00514
00515 break;
00516 case 'CUST_VAT':
00517 $tiers=new fiche($this->db);
00518 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00519 $tiers->getByQcode($qcode,false);
00520 $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00521 break;
00522
00523
00524 case 'NUMBER':
00525 $r=$this->d_number;
00526 break;
00527 case 'REFERENCE':
00528 $act=new action($this->db);
00529 $act->ag_id=$this->ag_id;
00530 $act->get();
00531 $r=$act->ag_ref;
00532 break;
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 case 'MARCH_NEXT':
00547 $counter++;
00548 $r='';
00549 break;
00550
00551 case 'VEN_ART_NAME':
00552 extract ($_POST);
00553 $id='e_march'.$counter;
00554
00555 if ( ! isset (${$id})) return "";
00556
00557 if ( ${'e_march'.$counter.'_sell'} != 0 && ${'e_quant'.$counter} != 0 )
00558 {
00559 $f=new fiche($this->db);
00560 $f->GetByQCode(${$id},false);
00561 $r=$f->strAttribut(ATTR_DEF_NAME);
00562 } else $r = "";
00563 break;
00564
00565 case 'VEN_ART_PRICE':
00566 extract ($_POST);
00567 $id='e_march'.$counter.'_sell' ;
00568 if ( !isset (${$id}) ) return "";
00569 $r=${$id};
00570 break;
00571
00572 case 'TVA_CODE':
00573 extract ($_POST);
00574 $id='e_march'.$counter.'_tva_id';
00575 if ( !isset (${$id}) ) return "";
00576 if ( ${$id} == -1 ) return "";
00577 $qt='e_quant'.$counter;
00578 $price='e_march'.$counter.'_sell' ;
00579 if ( ${$price} == 0 || ${$qt} == 0
00580 || strlen(trim( $price )) ==0
00581 || strlen(trim($qt)) ==0)
00582 return "";
00583
00584 $r=${$id};
00585 break;
00586
00587 case 'TVA_LABEL':
00588 extract ($_POST);
00589 $id='e_march'.$counter.'_tva_id';
00590 if ( !isset (${$id}) ) return "";
00591 $tva=GetTvaRate($this->db,${$id});
00592 if ( $tva == null || $tva==0 ) return "";
00593 $r=$tva['tva_label'];
00594 break;
00595
00596
00597 case 'TVA_AMOUNT':
00598 extract ($_POST);
00599 $qt='e_quant'.$counter;
00600 $price='e_march'.$counter.'_sell' ;
00601 $tva='e_march'.$counter.'_tva_id';
00602 if ( !isset (${'e_march'.$counter}) ) return "";
00603
00604 if ( ${$price} == 0 || ${$qt} == 0
00605 || strlen(trim( $price )) ==0
00606 || strlen(trim($qt)) ==0)
00607 return "";
00608 $a_tva=GetTvaRate($this->db,${$tva});
00609 echo_debug('class_document',__LINE__,'Tva :'.var_export($a_tva,true));
00610
00611 if ( sizeof($a_tva) == 0 ) return "";
00612 $r=round(${$price},2)*${$qt}*$a_tva['tva_rate'];
00613 $r=round($r,2);
00614 break;
00615
00616 case 'VEN_ART_QUANT':
00617 extract ($_POST);
00618 $id='e_quant'.$counter;
00619 if ( !isset (${$id}) ) return "";
00620
00621 if ( ${'e_march'.$counter.'_sell'} == 0
00622 || ${'e_quant'.$counter} == 0
00623 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00624 || strlen(trim(${'e_quant'.$counter})) ==0 )
00625 return "";
00626 $r=${$id};
00627 break;
00628
00629 case 'VEN_HTVA':
00630 extract ($_POST);
00631 $id='e_march'.$counter.'_sell' ;
00632 $quant='e_quant'.$counter;
00633 if ( !isset (${$id}) ) return "";
00634
00635
00636 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0
00637 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00638 || strlen(trim(${'e_quant'.$counter})) ==0)
00639 return "";
00645 $r=round(${$id}*${$quant},2);
00646 break;
00647
00648 case 'VEN_TVAC':
00649 extract ($_POST);
00650 $id='e_march'.$counter.'_sell' ;
00651 $quant='e_quant'.$counter;
00652
00653 if ( ! isset(${$id}))
00654 return "";
00655
00656 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0 )
00657 return "";
00662 $r=${$id}*${$quant};
00663 $tva=GetTvaRate($this->db,${'e_march'.$counter.'_tva_id'});
00664
00665 if ( $tva == null || $tva == 0 ) return $r;
00666
00667 $r=$r+$r*$tva['tva_rate'];
00668 $r=round($r,2);
00669 break;
00670 case 'TOTAL_VEN_HTVA':
00671 extract($_POST);
00672 echo_debug('class_document',__LINE__,'TOTAL_VEN_TVA item :'.$nb_item);
00673
00674 $sum=0.0;
00675 for ($i=0;$i<$nb_item;$i++)
00676 {
00677 $sell='e_march'.$i.'_sell';
00678 $qt='e_quant'.$i;
00679 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00680
00681 echo_debug('class_document',__LINE__,'counter :'.$i.' sur '.$nb_item);
00682 if ( ! isset (${$sell}) ) break;
00683 echo_debug('class_document',__LINE__,'sell :'.${$sell}.' qt = '.${$qt});
00684
00685 if ( strlen(trim(${$sell})) == 0 ||
00686 strlen(trim(${$qt})) == 0 ||
00687 ${$qt}==0 || ${$sell}==0)
00688 continue;
00689 $sum+=${$sell}*${$qt};
00690 echo_debug('class_document',__LINE__,'sum :'.$sum);
00691
00692 }
00693 $r=round($sum,2);
00694 break;
00695 case 'TOTAL_VEN_TVAC':
00696 extract($_POST);
00697 $sum=0.0;
00698 for ($i=0;$i<$nb_item;$i++)
00699 {
00700 $tva=GetTvaRate($this->db,${'e_march'.$i.'_tva_id'});
00701 $tva_rate=( $tva == null || $tva == 0 )?0.0:$tva['tva_rate'];
00702 echo_debug('class_document',__LINE__,' :'.$i.' sur '.$nb_item);
00703 $sell=${'e_march'.$i.'_sell'};
00704 $qt=${'e_quant'.$i};
00705 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00706
00707 $sum+=$sell*$qt*(1+$tva_rate);
00708
00709 }
00710 $r=round($sum,2);
00711
00712 break;
00713
00714 }
00715 return $r;
00716 }
00720 function remove()
00721 {
00722 $sql='delete from document where d_id='.$this->d_id;
00723 ExecSql($this->db,$sql);
00724 }
00731 function MoveDocumentPj($p_internal)
00732 {
00733
00734 $sql=sprintf("update jrn set jr_pj=%s,jr_pj_name='%s',jr_pj_type='%s' where jr_internal='%s'",
00735 $this->d_lob,$this->d_filename,$this->d_mimetype,$p_internal);
00736 ExecSql($this->db,$sql);
00737
00738 $this->remove();
00739 }
00740
00741
00742 }