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 if ( sizeof($_FILES) == 0 ) return;
00276
00277
00278 StartSql($this->db);
00279 $new_name=tempnam('/tmp','doc_');
00280
00281
00282
00283 if ( strlen($_FILES['file_upload']['tmp_name']) != 0 )
00284 {
00285
00286 if ( move_uploaded_file($_FILES['file_upload']['tmp_name'],$new_name))
00287 {
00288 $oid=pg_lo_import($this->db,$new_name);
00289
00290 if ( $oid == false )
00291 {
00292 Rollback($this->db);
00293 return 1;
00294 }
00295 }
00296
00297 $this->d_lob=$oid;
00298 $this->d_filename=$_FILES['file_upload']['name'];
00299 $this->d_mimetype=$_FILES['file_upload']['type'];
00300
00301
00302 $sql="select d_lob from document where d_id=".$this->d_id;
00303 $ret=ExecSql($this->db,$sql);
00304
00305 if (pg_num_rows($ret) != 0)
00306 {
00307
00308
00309 $r=pg_fetch_array($ret,0) ;
00310 $old_oid=$r['d_lob'] ;
00311 if (strlen($old_oid) != 0) { pg_lo_unlink ($this->db,$old_oid);}
00312 }
00313
00314 $sql=sprintf("update document set d_lob=%s,d_filename='%s',d_mimetype='%s' where d_id=%d",
00315 $this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id);
00316 ExecSql($this->db,$sql);
00317 }
00318 Commit($this->db);
00319
00320 }
00326 function a_ref()
00327 {
00328 if ( $this->d_id == 0 )
00329 return '';
00330 $r="";
00331 $r='<A HREF="show_document.php?d_id='.$this->d_id.'">Document</A>';
00332 return $r;
00333 }
00334
00335
00336
00337 function Send()
00338 {
00339
00340 StartSql($this->db);
00341 $ret=ExecSql($this->db,
00342 "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00343 if ( pg_num_rows ($ret) == 0 )
00344 return;
00345 $row=pg_fetch_array($ret,0);
00346
00347 $tmp=tempnam('/tmp/','document_');
00348 pg_lo_export($this->db,$row['d_lob'],$tmp);
00349 $this->d_mimetype=$row['d_mimetype'];
00350 $this->d_filename=$row['d_filename'];
00351
00352
00353 ini_set('zlib.output_compression','Off');
00354 header("Pragma: public");
00355 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00356 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00357 header("Cache-Control: must-revalidate");
00358 header('Content-type: "'.$this->d_mimetype.'"');
00359 header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00360 header("Accept-Ranges: bytes");
00361 $file=fopen($tmp,'r');
00362 while ( !feof ($file) )
00363 {
00364 echo fread($file,8192);
00365 }
00366 fclose($file);
00367
00368 unlink ($tmp);
00369
00370 Commit($this->db);
00371
00372 }
00375 function get()
00376 {
00377 $sql="select * from document where d_id=".$this->d_id;
00378 $ret=Exec($this->db,$sql);
00379 if ( pg_num_rows($ret) == 0 )
00380 return;
00381 $row=pg_fetch_array($ret,0);
00382 $this->ag_id=$row['ag_id'];
00383 $this->d_mimetype=$row['d_mimetype'];
00384 $this->d_filename=$row['d_filename'];
00385 $this->d_lob=$row['d_lob'];
00386 $this->d_number=$row[''];
00387 $this->d_state=$row['ag_id'];
00388 $this->d_number=$row['d_number'];
00389
00390 }
00423 function Replace($p_tag)
00424 {
00425 $r="Tag inconnu";
00426 static $counter=0;
00427 switch ($p_tag)
00428 {
00429 case 'DATE':
00430 $r=' Date inconnue ';
00431
00432
00433 if ( isset ($_REQUEST['ag_date']))
00434 $r=$_REQUEST['ag_date'];
00435 if ( isset ($_REQUEST['e_date']))
00436 $r=$_REQUEST['e_date'];
00437
00438 break;
00439
00440
00441
00442 case 'MY_NAME':
00443 $my=new own($this->db);
00444 $r=$my->MY_NAME;
00445 break;
00446 case 'MY_CP':
00447 $my=new own($this->db);
00448 $r=$my->MY_CP;
00449 break;
00450 case 'MY_COMMUNE':
00451 $my=new own($this->db);
00452 $r=$my->MY_COMMUNE;
00453 break;
00454 case 'MY_TVA':
00455 $my=new own($this->db);
00456 $r=$my->MY_TVA;
00457 break;
00458 case 'MY_STREET':
00459 $my=new own($this->db);
00460 $r=$my->MY_STREET;
00461 break;
00462 case 'MY_NUMBER':
00463 $my=new own($this->db);
00464 $r=$my->MY_NUMBER;
00465 break;
00466 case 'MY_TEL':
00467 $my=new own($this->db);
00468 $r=$my->MY_TEL;
00469 break;
00470 case 'MY_FAX':
00471 $my=new own($this->db);
00472 $r=$my->MY_FAX;
00473 break;
00474 case 'MY_PAYS':
00475 $my=new own($this->db);
00476 $r=$my->MY_PAYS;
00477 break;
00478
00479
00480
00481
00482
00483 case 'SOLDE':
00484 $tiers=new fiche($this->db);
00485 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00486 $tiers->Getbyqcode($qcode,false);
00487 $p=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
00488 $poste=new Poste($this->db,$p);
00489 $r=$poste->GetSolde(' true' );
00490 break;
00491 case 'CUST_NAME':
00492 $tiers=new fiche($this->db);
00493 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00494 $tiers->getByQcode($qcode,false);
00495 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00496 break;
00497 case 'CUST_ADDR_1':
00498 $tiers=new fiche($this->db);
00499 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00500 $tiers->getByQcode($qcode,false);
00501 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00502
00503 break ;
00504 case 'CUST_CP':
00505 $tiers=new fiche($this->db);
00506 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00507 $tiers->getByQcode($qcode,false);
00508 $r=$tiers->strAttribut(ATTR_DEF_CP);
00509
00510 break;
00511 case 'CUST_CO':
00512 $tiers=new fiche($this->db);
00513 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00514 $tiers->getByQcode($qcode,false);
00515 $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00516
00517 break;
00518 case 'CUST_VAT':
00519 $tiers=new fiche($this->db);
00520 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00521 $tiers->getByQcode($qcode,false);
00522 $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00523 break;
00524
00525
00526 case 'NUMBER':
00527 $r=$this->d_number;
00528 break;
00529 case 'REFERENCE':
00530 $act=new action($this->db);
00531 $act->ag_id=$this->ag_id;
00532 $act->get();
00533 $r=$act->ag_ref;
00534 break;
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 case 'MARCH_NEXT':
00549 $counter++;
00550 $r='';
00551 break;
00552
00553 case 'VEN_ART_NAME':
00554 extract ($_POST);
00555 $id='e_march'.$counter;
00556
00557 if ( ! isset (${$id})) return "";
00558
00559 if ( ${'e_march'.$counter.'_sell'} != 0 && ${'e_quant'.$counter} != 0 )
00560 {
00561 $f=new fiche($this->db);
00562 $f->GetByQCode(${$id},false);
00563 $r=$f->strAttribut(ATTR_DEF_NAME);
00564 } else $r = "";
00565 break;
00566
00567 case 'VEN_ART_PRICE':
00568 extract ($_POST);
00569 $id='e_march'.$counter.'_sell' ;
00570 if ( !isset (${$id}) ) return "";
00571 $r=${$id};
00572 break;
00573
00574 case 'TVA_CODE':
00575 extract ($_POST);
00576 $id='e_march'.$counter.'_tva_id';
00577 if ( !isset (${$id}) ) return "";
00578 if ( ${$id} == -1 ) return "";
00579 $qt='e_quant'.$counter;
00580 $price='e_march'.$counter.'_sell' ;
00581 if ( ${$price} == 0 || ${$qt} == 0
00582 || strlen(trim( $price )) ==0
00583 || strlen(trim($qt)) ==0)
00584 return "";
00585
00586 $r=${$id};
00587 break;
00588
00589 case 'TVA_LABEL':
00590 extract ($_POST);
00591 $id='e_march'.$counter.'_tva_id';
00592 if ( !isset (${$id}) ) return "";
00593 $tva=GetTvaRate($this->db,${$id});
00594 if ( $tva == null || $tva==0 ) return "";
00595 $r=$tva['tva_label'];
00596 break;
00597
00598
00599 case 'TVA_AMOUNT':
00600 extract ($_POST);
00601 $qt='e_quant'.$counter;
00602 $price='e_march'.$counter.'_sell' ;
00603 $tva='e_march'.$counter.'_tva_id';
00604 if ( !isset (${'e_march'.$counter}) ) return "";
00605
00606 if ( ${$price} == 0 || ${$qt} == 0
00607 || strlen(trim( $price )) ==0
00608 || strlen(trim($qt)) ==0)
00609 return "";
00610 $a_tva=GetTvaRate($this->db,${$tva});
00611 echo_debug('class_document',__LINE__,'Tva :'.var_export($a_tva,true));
00612
00613 if ( sizeof($a_tva) == 0 ) return "";
00614 $r=round(${$price},2)*${$qt}*$a_tva['tva_rate'];
00615 $r=round($r,2);
00616 break;
00617
00618 case 'VEN_ART_QUANT':
00619 extract ($_POST);
00620 $id='e_quant'.$counter;
00621 if ( !isset (${$id}) ) return "";
00622
00623 if ( ${'e_march'.$counter.'_sell'} == 0
00624 || ${'e_quant'.$counter} == 0
00625 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00626 || strlen(trim(${'e_quant'.$counter})) ==0 )
00627 return "";
00628 $r=${$id};
00629 break;
00630
00631 case 'VEN_HTVA':
00632 extract ($_POST);
00633 $id='e_march'.$counter.'_sell' ;
00634 $quant='e_quant'.$counter;
00635 if ( !isset (${$id}) ) return "";
00636
00637
00638 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0
00639 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00640 || strlen(trim(${'e_quant'.$counter})) ==0)
00641 return "";
00647 $r=round(${$id}*${$quant},2);
00648 break;
00649
00650 case 'VEN_TVAC':
00651 extract ($_POST);
00652 $id='e_march'.$counter.'_sell' ;
00653 $quant='e_quant'.$counter;
00654
00655 if ( ! isset(${$id}))
00656 return "";
00657
00658 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0 )
00659 return "";
00664 $r=${$id}*${$quant};
00665 $tva=GetTvaRate($this->db,${'e_march'.$counter.'_tva_id'});
00666
00667 if ( $tva == null || $tva == 0 ) return $r;
00668
00669 $r=$r+$r*$tva['tva_rate'];
00670 $r=round($r,2);
00671 break;
00672 case 'TOTAL_VEN_HTVA':
00673 extract($_POST);
00674 echo_debug('class_document',__LINE__,'TOTAL_VEN_TVA item :'.$nb_item);
00675
00676 $sum=0.0;
00677 for ($i=0;$i<$nb_item;$i++)
00678 {
00679 $sell='e_march'.$i.'_sell';
00680 $qt='e_quant'.$i;
00681 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00682
00683 echo_debug('class_document',__LINE__,'counter :'.$i.' sur '.$nb_item);
00684 if ( ! isset (${$sell}) ) break;
00685 echo_debug('class_document',__LINE__,'sell :'.${$sell}.' qt = '.${$qt});
00686
00687 if ( strlen(trim(${$sell})) == 0 ||
00688 strlen(trim(${$qt})) == 0 ||
00689 ${$qt}==0 || ${$sell}==0)
00690 continue;
00691 $sum+=${$sell}*${$qt};
00692 echo_debug('class_document',__LINE__,'sum :'.$sum);
00693
00694 }
00695 $r=round($sum,2);
00696 break;
00697 case 'TOTAL_VEN_TVAC':
00698 extract($_POST);
00699 $sum=0.0;
00700 for ($i=0;$i<$nb_item;$i++)
00701 {
00702 $tva=GetTvaRate($this->db,${'e_march'.$i.'_tva_id'});
00703 $tva_rate=( $tva == null || $tva == 0 )?0.0:$tva['tva_rate'];
00704 echo_debug('class_document',__LINE__,' :'.$i.' sur '.$nb_item);
00705 $sell=${'e_march'.$i.'_sell'};
00706 $qt=${'e_quant'.$i};
00707 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00708
00709 $sum+=$sell*$qt*(1+$tva_rate);
00710
00711 }
00712 $r=round($sum,2);
00713
00714 break;
00715
00716 }
00717 return $r;
00718 }
00722 function remove()
00723 {
00724 $sql='delete from document where d_id='.$this->d_id;
00725 ExecSql($this->db,$sql);
00726 }
00733 function MoveDocumentPj($p_internal)
00734 {
00735
00736 $sql=sprintf("update jrn set jr_pj=%s,jr_pj_name='%s',jr_pj_type='%s' where jr_internal='%s'",
00737 $this->d_lob,$this->d_filename,$this->d_mimetype,$p_internal);
00738 ExecSql($this->db,$sql);
00739
00740 $this->remove();
00741 }
00742
00743
00744 }