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 }
00057 function Generate()
00058 {
00059
00060 $dirname=tempnam('/tmp','doc_');
00061 echo $dirname;
00062 unlink($dirname);
00063 mkdir ($dirname);
00064 echo_debug('class_action',__LINE__,"Dirname is $dirname");
00065
00066 StartSql($this->db);
00067 $dm_info="select md_type,md_lob,md_filename,md_mimetype
00068 from document_modele where md_id=".$this->md_id;
00069 $Res=ExecSql($this->db,$dm_info);
00070
00071 $row=pg_fetch_array($Res,0);
00072 $this->d_lob=$row['md_lob'];
00073 $this->d_filename=$row['md_filename'];
00074 $this->d_mimetype=$row['md_mimetype'];
00075
00076 echo_debug('class_document',__LINE__,"OID is ".$row['md_lob']);
00078 chdir($dirname);
00079 $filename=$row['md_filename'];
00080 pg_lo_export($this->db,$row['md_lob'],$filename);
00081 $type="n";
00082 echo_debug('class_document',__LINE__,'The document type is '.$row['md_mimetype']);
00083
00084
00085 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00086 {
00087 echo_debug('class_document',__LINE__,'Unzip the OOo');
00088 system("unzip ".$filename);
00089
00090 unlink($filename);
00091 $file_to_parse="content.xml";
00092 $type="OOo";
00093 }
00094 else
00095 $file_to_parse=$filename;
00096
00097 $this->d_number=NextSequence($this->db,"seq_doc_type_".$row['md_type']);
00098
00099
00100
00101 $this->ParseDocument($dirname,$file_to_parse,$type);
00102
00103 Commit($this->db);
00104
00105 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00106 {
00107 system ("zip -r ".$filename." *");
00108 $file_to_parse=$filename;
00109 }
00110
00111 mkdir ($_SERVER['DOCUMENT_ROOT'].$dirname);
00112
00113
00114 rename($dirname."/".$file_to_parse,$_SERVER['DOCUMENT_ROOT'].$dirname.'/'.$file_to_parse);
00115 $ret=sprintf('<A HREF="%s">Document généré</A>',
00116 $dirname.'/'.$file_to_parse);
00117 $this->SaveGenerated($_SERVER['DOCUMENT_ROOT'].$dirname."/".$file_to_parse);
00118 return $ret;
00119 }
00120
00131 function ParseDocument($p_dir,$p_file,$p_type)
00132 {
00133
00134 echo_debug('class_document',__LINE__,'Begin parsing of '.$p_dir.' '.$p_file.'Type = '.$p_type);
00135
00142
00143 $infile_name=$p_dir."/".$p_file;
00144 echo_debug("class_document.php",__LINE__,"Open the document $p_dir/$p_file");
00145 $h=fopen($infile_name,"r");
00146 $output_name=tempnam($_SERVER["DOCUMENT_ROOT"]."/tmp","gen_doc_");
00147 $output_file=fopen($output_name,"w+");
00148
00149 if ( $h == false )
00150 {
00151 echo "cannot open $p_dir $p_file ";
00152 exit();
00153 }
00154 if ( $output_file == false)
00155 {
00156 echo "ne peut pas ouvrir le fichier de sortie";
00157 exit();
00158 }
00159
00160 if ( $p_type=='OOo')
00161 {
00162 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00163 $lt="<";
00164 $gt=">";
00165 }
00166 else
00167 {
00168 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00169 $lt="<";
00170 $gt=">";
00171 }
00172
00173 while(! feof($h))
00174 {
00175
00176 $buffer=fgets($h);
00177
00178
00179 while ( ereg ($regex,$buffer,$f) )
00180 {
00181
00182 echo_debug('class_document',__LINE__,'var_export '.var_export( $f,true));
00183 foreach ( $f as $pattern )
00184 {
00185 echo_debug('class_document',__LINE__, "pattern");
00186 echo_debug('class_document',__LINE__, var_export($pattern,true));
00187 $to_remove=$pattern;
00188
00189 $pattern=str_replace($lt,'',$pattern);
00190 $pattern=str_replace($gt,'',$pattern);
00191
00192
00193
00194 $value=$this->Replace($pattern);
00195
00196
00197 $pos=strpos($buffer,$to_remove);
00198
00199 $len=strlen($to_remove);
00200 $buffer=substr_replace($buffer,$value,$pos,$len);
00201
00202
00203
00204 echo_debug('class_document',__LINE__,"Transform $pattern by $value");
00205 }
00206 }
00207
00208 fwrite($output_file,$buffer);
00209
00210 }
00211 fclose($h);
00212 fclose($output_file);
00213 rename ($output_name,$infile_name);
00214
00215
00216 }
00224 function SaveGenerated($p_file)
00225 {
00226 echo_debug('class_document',__LINE__,'Save generated');
00227
00228 $doc=new Document($this->db);
00229 StartSql($this->db);
00230 $this->d_lob=pg_lo_import($this->db,$p_file);
00231 if ( $this->d_lob == false ) {
00232 Rollback($this->db); echo_debug('class_document',__LINE__,"can't save file $p_file");
00233 return 1; }
00234
00235 $sql=sprintf("insert into document(ag_id,d_lob,d_number,d_filename,d_mimetype,d_state)
00236 values (%d,%s,%d,'%s','%s',%d)",
00237 $this->ag_id,
00238 $this->d_lob,
00239 $this->d_number,
00240 $this->d_filename,
00241 $this->d_mimetype,
00242 $this->d_state
00243 );
00244 ExecSql($this->db,$sql);
00245 $this->d_id=GetSequence($this->db,"document_d_id_seq");
00246 echo_debug('class_document',__LINE__,'document sauvé : d_id'.$this->d_id);
00247 Commit($this->db);
00248 return 0;
00249 }
00257 function Upload()
00258 {
00259
00260
00261 StartSql($this->db);
00262 $new_name=tempnam('/tmp','doc_');
00263
00264
00265 if ( sizeof($_FILES) == 0 ) return;
00266
00267 if ( strlen($_FILES['file_upload']['tmp_name']) != 0 )
00268 {
00269
00270 if ( move_uploaded_file($_FILES['file_upload']['tmp_name'],$new_name))
00271 {
00272 $oid=pg_lo_import($this->db,$new_name);
00273
00274 if ( $oid == false )
00275 {
00276 Rollback($this->db);
00277 return 1;
00278 }
00279 }
00280
00281 $this->d_lob=$oid;
00282 $this->d_filename=$_FILES['file_upload']['name'];
00283 $this->d_mimetype=$_FILES['file_upload']['type'];
00284
00285
00286 $sql="select d_lob from document where d_id=".$this->d_id;
00287 $ret=ExecSql($this->db,$sql);
00288
00289 if (pg_num_rows($ret) != 0)
00290 {
00291
00292
00293 $r=pg_fetch_array($ret,0) ;
00294 $old_oid=$r['d_lob'] ;
00295 if (strlen($old_oid) != 0) { pg_lo_unlink ($this->db,$old_oid);}
00296 }
00297
00298 $sql=sprintf("update document set d_lob=%s,d_filename='%s',d_mimetype='%s' where d_id=%d",
00299 $this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id);
00300 ExecSql($this->db,$sql);
00301 Commit($this->db);
00302 }
00303
00304 }
00310 function a_ref()
00311 {
00312 if ( $this->d_id == 0 )
00313 return '';
00314 $r="";
00315 $r='<A HREF="show_document.php?d_id='.$this->d_id.'">Document</A>';
00316 return $r;
00317 }
00318
00319
00320
00321 function Send()
00322 {
00323
00324 StartSql($this->db);
00325 $ret=ExecSql($this->db,
00326 "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00327 if ( pg_num_rows ($ret) == 0 )
00328 return;
00329 $row=pg_fetch_array($ret,0);
00330
00331 $tmp=tempnam('/tmp/','document_');
00332 pg_lo_export($this->db,$row['d_lob'],$tmp);
00333 $this->d_mimetype=$row['d_mimetype'];
00334 $this->d_filename=$row['d_filename'];
00335
00336
00337 ini_set('zlib.output_compression','Off');
00338 header("Pragma: public");
00339 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00340 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00341 header("Cache-Control: must-revalidate");
00342 header('Content-type: "'.$this->d_mimetype.'"');
00343 header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00344 header("Accept-Ranges: bytes");
00345 $file=fopen($tmp,'r');
00346 while ( !feof ($file) )
00347 {
00348 echo fread($file,8192);
00349 }
00350 fclose($file);
00351
00352 unlink ($tmp);
00353
00354 Commit($this->db);
00355
00356 }
00359 function get()
00360 {
00361 $sql="select * from document where d_id=".$this->d_id;
00362 $ret=Exec($this->db,$sql);
00363 if ( pg_num_rows($ret) == 0 )
00364 return;
00365 $row=pg_fetch_array($ret,0);
00366 $this->ag_id=$row['ag_id'];
00367 $this->d_mimetype=$row['d_mimetype'];
00368 $this->d_filename=$row['d_filename'];
00369 $this->d_lob=$row['d_lob'];
00370 $this->d_number=$row[''];
00371 $this->d_state=$row['ag_id'];
00372 $this->d_number=$row['d_number'];
00373
00374 }
00407 function Replace($p_tag)
00408 {
00409 $r="Tag inconnu";
00410 static $counter=0;
00411 switch ($p_tag)
00412 {
00413 case 'DATE':
00414 $r=' Date inconnue ';
00415
00416
00417 if ( isset ($_REQUEST['ag_date']))
00418 $r=$_REQUEST['ag_date'];
00419 if ( isset ($_REQUEST['e_date']))
00420 $r=$_REQUEST['e_date'];
00421
00422 break;
00423
00424
00425
00426 case 'MY_NAME':
00427 $my=new own($this->db);
00428 $r=$my->MY_NAME;
00429 break;
00430 case 'MY_CP':
00431 $my=new own($this->db);
00432 $r=$my->MY_CP;
00433 break;
00434 case 'MY_COMMUNE':
00435 $my=new own($this->db);
00436 $r=$my->MY_COMMUNE;
00437 break;
00438 case 'MY_TVA':
00439 $my=new own($this->db);
00440 $r=$my->MY_TVA;
00441 break;
00442 case 'MY_STREET':
00443 $my=new own($this->db);
00444 $r=$my->MY_STREET;
00445 break;
00446 case 'MY_NUMBER':
00447 $my=new own($this->db);
00448 $r=$my->MY_NUMBER;
00449 break;
00450 case 'MY_TEL':
00451 $my=new own($this->db);
00452 $r=$my->MY_TEL;
00453 break;
00454 case 'MY_FAX':
00455 $my=new own($this->db);
00456 $r=$my->MY_FAX;
00457 break;
00458 case 'MY_PAYS':
00459 $my=new own($this->db);
00460 $r=$my->MY_PAYS;
00461 break;
00462
00463
00464
00465
00466
00467 case 'SOLDE':
00468 $tiers=new fiche($this->db);
00469 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00470 $tiers->Getbyqcode($qcode,false);
00471 $p=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
00472 $poste=new Poste($this->db,$p);
00473 $r=$poste->GetSolde(' true' );
00474 break;
00475 case 'CUST_NAME':
00476 $tiers=new fiche($this->db);
00477 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00478 $tiers->getByQcode($qcode,false);
00479 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00480 break;
00481 case 'CUST_ADDR_1':
00482 $tiers=new fiche($this->db);
00483 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00484 $tiers->getByQcode($qcode,false);
00485 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00486
00487 break ;
00488 case 'CUST_CP':
00489 $tiers=new fiche($this->db);
00490 $qcode=isset($_REQUEST['tiers'])?$_REQUEST['tiers']:$_REQUEST['e_client'];
00491 $tiers->getByQcode($qcode,false);
00492 $r=$tiers->strAttribut(ATTR_DEF_CP);
00493
00494 break;
00495 case 'CUST_CO':
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_PAYS);
00500
00501 break;
00502 case 'CUST_VAT':
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_NUMTVA);
00507 break;
00508
00509
00510 case 'NUMBER':
00511 $r=$this->d_number;
00512 break;
00513 case 'REFERENCE':
00514 $act=new action($this->db);
00515 $act->ag_id=$this->ag_id;
00516 $act->get();
00517 $r=$act->ag_ref;
00518 break;
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 case 'MARCH_NEXT':
00533 $counter++;
00534 $r='';
00535 break;
00536
00537 case 'VEN_ART_NAME':
00538 extract ($_POST);
00539 $id='e_march'.$counter;
00540
00541 if ( ! isset (${$id})) return "";
00542
00543 if ( ${'e_march'.$counter.'_sell'} != 0 && ${'e_quant'.$counter} != 0 )
00544 {
00545 $f=new fiche($this->db);
00546 $f->GetByQCode(${$id},false);
00547 $r=$f->strAttribut(ATTR_DEF_NAME);
00548 } else $r = "";
00549 break;
00550
00551 case 'VEN_ART_PRICE':
00552 extract ($_POST);
00553 $id='e_march'.$counter.'_sell' ;
00554 if ( !isset (${$id}) ) return "";
00555 $r=${$id};
00556 break;
00557
00558 case 'TVA_CODE':
00559 extract ($_POST);
00560 $id='e_march'.$counter.'_tva_id';
00561 if ( !isset (${$id}) ) return "";
00562 if ( ${$id} == -1 ) return "";
00563 $qt='e_quant'.$counter;
00564 $price='e_march'.$counter.'_sell' ;
00565 if ( ${$price} == 0 || ${$qt} == 0
00566 || strlen(trim( $price )) ==0
00567 || strlen(trim($qt)) ==0)
00568 return "";
00569
00570 $r=${$id};
00571 break;
00572
00573 case 'TVA_LABEL':
00574 extract ($_POST);
00575 $id='e_march'.$counter.'_tva_id';
00576 if ( !isset (${$id}) ) return "";
00577 $tva=GetTvaRate($this->db,${$id});
00578 if ( $tva == null || $tva==0 ) return "";
00579 $r=$tva['tva_label'];
00580 break;
00581
00582
00583 case 'TVA_AMOUNT':
00584 extract ($_POST);
00585 $qt='e_quant'.$counter;
00586 $price='e_march'.$counter.'_sell' ;
00587 $tva='e_march'.$counter.'_tva_id';
00588 if ( !isset (${'e_march'.$counter}) ) return "";
00589
00590 if ( ${$price} == 0 || ${$qt} == 0
00591 || strlen(trim( $price )) ==0
00592 || strlen(trim($qt)) ==0)
00593 return "";
00594 $a_tva=GetTvaRate($this->db,${$tva});
00595 echo_debug('class_document',__LINE__,'Tva :'.var_export($a_tva,true));
00596
00597 if ( sizeof($a_tva) == 0 ) return "";
00598 $r=round(${$price},2)*${$qt}*$a_tva['tva_rate'];
00599 $r=round($r,2);
00600 break;
00601
00602 case 'VEN_ART_QUANT':
00603 extract ($_POST);
00604 $id='e_quant'.$counter;
00605 if ( !isset (${$id}) ) return "";
00606
00607 if ( ${'e_march'.$counter.'_sell'} == 0
00608 || ${'e_quant'.$counter} == 0
00609 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00610 || strlen(trim(${'e_quant'.$counter})) ==0 )
00611 return "";
00612 $r=${$id};
00613 break;
00614
00615 case 'VEN_HTVA':
00616 extract ($_POST);
00617 $id='e_march'.$counter.'_sell' ;
00618 $quant='e_quant'.$counter;
00619 if ( !isset (${$id}) ) return "";
00620
00621
00622 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0
00623 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00624 || strlen(trim(${'e_quant'.$counter})) ==0)
00625 return "";
00631 $r=round(${$id}*${$quant},2);
00632 break;
00633
00634 case 'VEN_TVAC':
00635 extract ($_POST);
00636 $id='e_march'.$counter.'_sell' ;
00637 $quant='e_quant'.$counter;
00638
00639 if ( ! isset(${$id}))
00640 return "";
00641
00642 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0 )
00643 return "";
00648 $r=${$id}*${$quant};
00649 $tva=GetTvaRate($this->db,${'e_march'.$counter.'_tva_id'});
00650
00651 if ( $tva == null || $tva == 0 ) return $r;
00652
00653 $r=$r+$r*$tva['tva_rate'];
00654 $r=round($r,2);
00655 break;
00656 case 'TOTAL_VEN_HTVA':
00657 extract($_POST);
00658 echo_debug('class_document',__LINE__,'TOTAL_VEN_TVA item :'.$nb_item);
00659
00660 $sum=0.0;
00661 for ($i=0;$i<$nb_item;$i++)
00662 {
00663 $sell='e_march'.$i.'_sell';
00664 $qt='e_quant'.$i;
00665 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00666
00667 echo_debug('class_document',__LINE__,'counter :'.$i.' sur '.$nb_item);
00668 if ( ! isset (${$sell}) ) break;
00669 echo_debug('class_document',__LINE__,'sell :'.${$sell}.' qt = '.${$qt});
00670
00671 if ( strlen(trim(${$sell})) == 0 ||
00672 strlen(trim(${$qt})) == 0 ||
00673 ${$qt}==0 || ${$sell}==0)
00674 continue;
00675 $sum+=${$sell}*${$qt};
00676 echo_debug('class_document',__LINE__,'sum :'.$sum);
00677
00678 }
00679 $r=round($sum,2);
00680 break;
00681 case 'TOTAL_VEN_TVAC':
00682 extract($_POST);
00683 $sum=0.0;
00684 for ($i=0;$i<$nb_item;$i++)
00685 {
00686 $tva=GetTvaRate($this->db,${'e_march'.$i.'_tva_id'});
00687 $tva_rate=( $tva == null || $tva == 0 )?0.0:$tva['tva_rate'];
00688 echo_debug('class_document',__LINE__,' :'.$i.' sur '.$nb_item);
00689 $sell=${'e_march'.$i.'_sell'};
00690 $qt=${'e_quant'.$i};
00691 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00692
00693 $sum+=$sell*$qt*(1+$tva_rate);
00694
00695 }
00696 $r=round($sum,2);
00697
00698 break;
00699
00700 }
00701 return $r;
00702 }
00706 function remove()
00707 {
00708 $sql='delete from document where d_id='.$this->d_id;
00709 ExecSql($this->db,$sql);
00710 }
00717 function MoveDocumentPj($p_internal)
00718 {
00719
00720 $sql=sprintf("update jrn set jr_pj=%s,jr_pj_name='%s',jr_pj_type='%s' where jr_internal='%s'",
00721 $this->d_lob,$this->d_filename,$this->d_mimetype,$p_internal);
00722 ExecSql($this->db,$sql);
00723
00724 $this->remove();
00725 }
00726
00727
00728 }