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 }
00063 function save()
00064 {
00065 $sql="update document set d_state=".$this->d_state.
00066 " where d_id=".$this->d_id;
00067 ExecSql($this->db,$sql);
00068 }
00079 function Generate()
00080 {
00081
00082 $dirname=tempnam('/tmp','doc_');
00083
00084
00085 unlink($dirname);
00086 mkdir ($dirname);
00087 echo_debug('class_action',__LINE__,"Dirname is $dirname");
00088
00089 StartSql($this->db);
00090 $dm_info="select md_type,md_lob,md_filename,md_mimetype
00091 from document_modele where md_id=".$this->md_id;
00092 $Res=ExecSql($this->db,$dm_info);
00093
00094 $row=pg_fetch_array($Res,0);
00095 $this->d_lob=$row['md_lob'];
00096 $this->d_filename=$row['md_filename'];
00097 $this->d_mimetype=$row['md_mimetype'];
00098
00099 echo_debug('class_document',__LINE__,"OID is ".$row['md_lob']);
00101 chdir($dirname);
00102 $filename=$row['md_filename'];
00103 pg_lo_export($this->db,$row['md_lob'],$filename);
00104 $type="n";
00105 echo_debug('class_document',__LINE__,'The document type is '.$row['md_mimetype']);
00106
00107
00108 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00109 {
00110 echo_debug('class_document',__LINE__,'Unzip the OOo');
00111 echo '<span id="gen_msg">';
00112 echo '<blink><font color="red">Un moment de patience, le document se prépare...</font></blink>';
00113 echo '</span>';
00114 ob_start();
00115 system("unzip ".$filename);
00116
00117 unlink($filename);
00118 ob_end_clean();
00119 $file_to_parse="content.xml";
00120 $type="OOo";
00121 }
00122 else
00123 $file_to_parse=$filename;
00124
00125 $this->d_number=NextSequence($this->db,"seq_doc_type_".$row['md_type']);
00126
00127
00128
00129 $this->ParseDocument($dirname,$file_to_parse,$type);
00130
00131 Commit($this->db);
00132
00133 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00134 {
00135 ob_start();
00136 system ("zip -r ".$filename." *");
00137 ob_end_clean();
00138 echo "le document est prêt ";
00139
00140 $file_to_parse=$filename;
00141 }
00142
00143 mkdir ($_SERVER['DOCUMENT_ROOT'].$dirname);
00144
00145
00146 rename($dirname."/".$file_to_parse,$_SERVER['DOCUMENT_ROOT'].$dirname.'/'.$file_to_parse);
00147 $ret=sprintf('<A HREF="%s">Document généré</A>',
00148 $dirname.'/'.$file_to_parse);
00149 $this->SaveGenerated($_SERVER['DOCUMENT_ROOT'].$dirname."/".$file_to_parse);
00150 return $ret;
00151 }
00152
00163 function ParseDocument($p_dir,$p_file,$p_type)
00164 {
00165
00166 echo_debug('class_document',__LINE__,'Begin parsing of '.$p_dir.' '.$p_file.'Type = '.$p_type);
00167
00174
00175 $infile_name=$p_dir."/".$p_file;
00176 echo_debug("class_document.php",__LINE__,"Open the document $p_dir/$p_file");
00177 $h=fopen($infile_name,"r");
00178 $output_name=tempnam($_SERVER["DOCUMENT_ROOT"]."/tmp","gen_doc_");
00179 $output_file=fopen($output_name,"w+");
00180
00181 if ( $h == false )
00182 {
00183 echo "cannot open $p_dir $p_file ";
00184 exit();
00185 }
00186 if ( $output_file == false)
00187 {
00188 echo "ne peut pas ouvrir le fichier de sortie";
00189 exit();
00190 }
00191
00192 if ( $p_type=='OOo')
00193 {
00194 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00195 $lt="<";
00196 $gt=">";
00197 }
00198 else
00199 {
00200 $regex="<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[0-9]*>>";
00201 $lt="<";
00202 $gt=">";
00203 }
00204
00205 while(! feof($h))
00206 {
00207
00208 $buffer=fgets($h);
00209
00210
00211 while ( ereg ($regex,$buffer,$f) )
00212 {
00213
00214 echo_debug('class_document',__LINE__,'var_export '.var_export( $f,true));
00215 foreach ( $f as $pattern )
00216 {
00217 echo_debug('class_document',__LINE__, "pattern");
00218 echo_debug('class_document',__LINE__, var_export($pattern,true));
00219 $to_remove=$pattern;
00220
00221 $pattern=str_replace($lt,'',$pattern);
00222 $pattern=str_replace($gt,'',$pattern);
00223
00224
00225
00226 $value=$this->Replace($pattern);
00227
00228
00229 $pos=strpos($buffer,$to_remove);
00230
00231 $len=strlen($to_remove);
00232 $buffer=substr_replace($buffer,$value,$pos,$len);
00233
00234
00235
00236 echo_debug('class_document',__LINE__,"Transform $pattern by $value");
00237 }
00238 }
00239
00240 fwrite($output_file,$buffer);
00241
00242 }
00243 fclose($h);
00244 fclose($output_file);
00245 rename ($output_name,$infile_name);
00246
00247
00248 }
00256 function SaveGenerated($p_file)
00257 {
00258 echo_debug('class_document',__LINE__,'Save generated');
00259
00260 $doc=new Document($this->db);
00261 StartSql($this->db);
00262 $this->d_lob=pg_lo_import($this->db,$p_file);
00263 if ( $this->d_lob == false ) {
00264 Rollback($this->db); echo_debug('class_document',__LINE__,"can't save file $p_file");
00265 return 1; }
00266
00267 $sql=sprintf("insert into document(ag_id,d_lob,d_number,d_filename,d_mimetype,d_state)
00268 values (%d,%s,%d,'%s','%s',%d)",
00269 $this->ag_id,
00270 $this->d_lob,
00271 $this->d_number,
00272 $this->d_filename,
00273 $this->d_mimetype,
00274 $this->d_state
00275 );
00276 ExecSql($this->db,$sql);
00277 $this->d_id=GetSequence($this->db,"document_d_id_seq");
00278 echo_debug('class_document',__LINE__,'document sauvé : d_id'.$this->d_id);
00279 Commit($this->db);
00280 return 0;
00281 }
00289 function Upload()
00290 {
00291
00292
00293 if ( sizeof($_FILES) == 0 ) return;
00294
00295
00296 StartSql($this->db);
00297 $new_name=tempnam('/tmp','doc_');
00298
00299
00300
00301 if ( strlen($_FILES['file_upload']['tmp_name']) != 0 )
00302 {
00303
00304 if ( move_uploaded_file($_FILES['file_upload']['tmp_name'],$new_name))
00305 {
00306 $oid=pg_lo_import($this->db,$new_name);
00307
00308 if ( $oid == false )
00309 {
00310 Rollback($this->db);
00311 return 1;
00312 }
00313 }
00314
00315 $this->d_lob=$oid;
00316 $this->d_filename=$_FILES['file_upload']['name'];
00317 $this->d_mimetype=$_FILES['file_upload']['type'];
00318
00319
00320 $sql="select d_lob from document where d_id=".$this->d_id;
00321 $ret=ExecSql($this->db,$sql);
00322
00323 if (pg_num_rows($ret) != 0)
00324 {
00325
00326
00327 $r=pg_fetch_array($ret,0) ;
00328 $old_oid=$r['d_lob'] ;
00329 if (strlen($old_oid) != 0) { pg_lo_unlink ($this->db,$old_oid);}
00330 }
00331
00332 $sql=sprintf("update document set d_lob=%s,d_filename='%s',d_mimetype='%s' where d_id=%d",
00333 $this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id);
00334 ExecSql($this->db,$sql);
00335 }
00336 Commit($this->db);
00337
00338 }
00344 function a_ref()
00345 {
00346 if ( $this->d_id == 0 )
00347 return '';
00348 $r="";
00349 $r='<A class="mtitle" HREF="show_document.php?d_id='.$this->d_id.'">Document</A>';
00350 return $r;
00351 }
00352
00353
00354
00355 function Send()
00356 {
00357
00358 StartSql($this->db);
00359 $ret=ExecSql($this->db,
00360 "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00361 if ( pg_num_rows ($ret) == 0 )
00362 return;
00363 $row=pg_fetch_array($ret,0);
00364
00365 $tmp=tempnam('/tmp/','document_');
00366 pg_lo_export($this->db,$row['d_lob'],$tmp);
00367 $this->d_mimetype=$row['d_mimetype'];
00368 $this->d_filename=$row['d_filename'];
00369
00370
00371 ini_set('zlib.output_compression','Off');
00372 header("Pragma: public");
00373 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00374 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00375 header("Cache-Control: must-revalidate");
00376 header('Content-type: "'.$this->d_mimetype.'"');
00377 header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00378 header("Accept-Ranges: bytes");
00379 $file=fopen($tmp,'r');
00380 while ( !feof ($file) )
00381 {
00382 echo fread($file,8192);
00383 }
00384 fclose($file);
00385
00386 unlink ($tmp);
00387
00388 Commit($this->db);
00389
00390 }
00393 function get()
00394 {
00395 $sql="select * from document where d_id=".$this->d_id;
00396 $ret=Exec($this->db,$sql);
00397 if ( pg_num_rows($ret) == 0 )
00398 return;
00399 $row=pg_fetch_array($ret,0);
00400 $this->ag_id=$row['ag_id'];
00401 $this->d_mimetype=$row['d_mimetype'];
00402 $this->d_filename=$row['d_filename'];
00403 $this->d_lob=$row['d_lob'];
00404 $this->d_number=$row[''];
00405 $this->d_state=$row['ag_id'];
00406 $this->d_number=$row['d_number'];
00407
00408 }
00441 function Replace($p_tag)
00442 {
00443 $r="Tag inconnu";
00444 static $counter=0;
00445 switch ($p_tag)
00446 {
00447 case 'DATE':
00448 $r=' Date inconnue ';
00449
00450
00451 if ( isset ($_REQUEST['ag_date']))
00452 $r=$_REQUEST['ag_date'];
00453 if ( isset ($_REQUEST['e_date']))
00454 $r=$_REQUEST['e_date'];
00455
00456 break;
00457
00458
00459
00460 case 'MY_NAME':
00461 $my=new own($this->db);
00462 $r=$my->MY_NAME;
00463 break;
00464 case 'MY_CP':
00465 $my=new own($this->db);
00466 $r=$my->MY_CP;
00467 break;
00468 case 'MY_COMMUNE':
00469 $my=new own($this->db);
00470 $r=$my->MY_COMMUNE;
00471 break;
00472 case 'MY_TVA':
00473 $my=new own($this->db);
00474 $r=$my->MY_TVA;
00475 break;
00476 case 'MY_STREET':
00477 $my=new own($this->db);
00478 $r=$my->MY_STREET;
00479 break;
00480 case 'MY_NUMBER':
00481 $my=new own($this->db);
00482 $r=$my->MY_NUMBER;
00483 break;
00484 case 'MY_TEL':
00485 $my=new own($this->db);
00486 $r=$my->MY_TEL;
00487 break;
00488 case 'MY_FAX':
00489 $my=new own($this->db);
00490 $r=$my->MY_FAX;
00491 break;
00492 case 'MY_PAYS':
00493 $my=new own($this->db);
00494 $r=$my->MY_PAYS;
00495 break;
00496
00497
00498
00499
00500
00501 case 'SOLDE':
00502 $tiers=new fiche($this->db);
00503 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00504 $tiers->Getbyqcode($qcode,false);
00505 $p=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
00506 $poste=new Poste($this->db,$p);
00507 $r=$poste->GetSolde(' true' );
00508 break;
00509 case 'CUST_NAME':
00510 $tiers=new fiche($this->db);
00511 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00512 $tiers->getByQcode($qcode,false);
00513 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00514 break;
00515 case 'CUST_ADDR_1':
00516 $tiers=new fiche($this->db);
00517 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00518 $tiers->getByQcode($qcode,false);
00519 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00520
00521 break ;
00522 case 'CUST_CP':
00523 $tiers=new fiche($this->db);
00524 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00525 $tiers->getByQcode($qcode,false);
00526 $r=$tiers->strAttribut(ATTR_DEF_CP);
00527
00528 break;
00529 case 'CUST_CO':
00530 $tiers=new fiche($this->db);
00531 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00532 $tiers->getByQcode($qcode,false);
00533 $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00534
00535 break;
00536 case 'CUST_VAT':
00537 $tiers=new fiche($this->db);
00538 $qcode=isset($_REQUEST['qcode_dest'])?$_REQUEST['qcode_dest']:$_REQUEST['e_client'];
00539 $tiers->getByQcode($qcode,false);
00540 $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00541 break;
00542
00543
00544 case 'NUMBER':
00545 $r=$this->d_number;
00546 break;
00547 case 'REFERENCE':
00548 $act=new action($this->db);
00549 $act->ag_id=$this->ag_id;
00550 $act->get();
00551 $r=$act->ag_ref;
00552 break;
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 case 'MARCH_NEXT':
00567 $counter++;
00568 $r='';
00569 break;
00570
00571 case 'VEN_ART_NAME':
00572 extract ($_POST);
00573 $id='e_march'.$counter;
00574
00575 if ( ! isset (${$id})) return "";
00576
00577 if ( ${'e_march'.$counter.'_sell'} != 0 && ${'e_quant'.$counter} != 0 )
00578 {
00579 $f=new fiche($this->db);
00580 $f->GetByQCode(${$id},false);
00581 $r=$f->strAttribut(ATTR_DEF_NAME);
00582 } else $r = "";
00583 break;
00584
00585 case 'VEN_ART_PRICE':
00586 extract ($_POST);
00587 $id='e_march'.$counter.'_sell' ;
00588 if ( !isset (${$id}) ) return "";
00589 $r=${$id};
00590 break;
00591
00592 case 'TVA_CODE':
00593 extract ($_POST);
00594 $id='e_march'.$counter.'_tva_id';
00595 if ( !isset (${$id}) ) return "";
00596 if ( ${$id} == -1 ) return "";
00597 $qt='e_quant'.$counter;
00598 $price='e_march'.$counter.'_sell' ;
00599 if ( ${$price} == 0 || ${$qt} == 0
00600 || strlen(trim( $price )) ==0
00601 || strlen(trim($qt)) ==0)
00602 return "";
00603
00604 $r=${$id};
00605 break;
00606
00607 case 'TVA_LABEL':
00608 extract ($_POST);
00609 $id='e_march'.$counter.'_tva_id';
00610 if ( !isset (${$id}) ) return "";
00611 $tva=GetTvaRate($this->db,${$id});
00612 if ( $tva == null || $tva==0 ) return "";
00613 $r=$tva['tva_label'];
00614 break;
00615
00616
00617 case 'TVA_AMOUNT':
00618 extract ($_POST);
00619 $qt='e_quant'.$counter;
00620 $price='e_march'.$counter.'_sell' ;
00621 $tva='e_march'.$counter.'_tva_id';
00622 if ( !isset (${'e_march'.$counter}) ) return "";
00623
00624 if ( ${$price} == 0 || ${$qt} == 0
00625 || strlen(trim( $price )) ==0
00626 || strlen(trim($qt)) ==0)
00627 return "";
00628 $a_tva=GetTvaRate($this->db,${$tva});
00629 echo_debug('class_document',__LINE__,'Tva :'.var_export($a_tva,true));
00630
00631 if ( sizeof($a_tva) == 0 ) return "";
00632 $r=round(${$price},2)*${$qt}*$a_tva['tva_rate'];
00633 $r=round($r,2);
00634 break;
00635
00636 case 'VEN_ART_QUANT':
00637 extract ($_POST);
00638 $id='e_quant'.$counter;
00639 if ( !isset (${$id}) ) return "";
00640
00641 if ( ${'e_march'.$counter.'_sell'} == 0
00642 || ${'e_quant'.$counter} == 0
00643 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00644 || strlen(trim(${'e_quant'.$counter})) ==0 )
00645 return "";
00646 $r=${$id};
00647 break;
00648
00649 case 'VEN_HTVA':
00650 extract ($_POST);
00651 $id='e_march'.$counter.'_sell' ;
00652 $quant='e_quant'.$counter;
00653 if ( !isset (${$id}) ) return "";
00654
00655
00656 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0
00657 || strlen(trim( ${'e_march'.$counter.'_sell'} )) ==0
00658 || strlen(trim(${'e_quant'.$counter})) ==0)
00659 return "";
00665 $r=round(${$id}*${$quant},2);
00666 break;
00667
00668 case 'VEN_TVAC':
00669 extract ($_POST);
00670 $id='e_march'.$counter.'_sell' ;
00671 $quant='e_quant'.$counter;
00672
00673 if ( ! isset(${$id}))
00674 return "";
00675
00676 if ( ${'e_march'.$counter.'_sell'} == 0 || ${'e_quant'.$counter} == 0 )
00677 return "";
00682 $r=${$id}*${$quant};
00683 $tva=GetTvaRate($this->db,${'e_march'.$counter.'_tva_id'});
00684
00685 if ( $tva == null || $tva == 0 ) return $r;
00686
00687 $r=$r+$r*$tva['tva_rate'];
00688 $r=round($r,2);
00689 break;
00690 case 'TOTAL_VEN_HTVA':
00691 extract($_POST);
00692 echo_debug('class_document',__LINE__,'TOTAL_VEN_TVA item :'.$nb_item);
00693
00694 $sum=0.0;
00695 for ($i=0;$i<$nb_item;$i++)
00696 {
00697 $sell='e_march'.$i.'_sell';
00698 $qt='e_quant'.$i;
00699 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00700
00701 echo_debug('class_document',__LINE__,'counter :'.$i.' sur '.$nb_item);
00702 if ( ! isset (${$sell}) ) break;
00703 echo_debug('class_document',__LINE__,'sell :'.${$sell}.' qt = '.${$qt});
00704
00705 if ( strlen(trim(${$sell})) == 0 ||
00706 strlen(trim(${$qt})) == 0 ||
00707 ${$qt}==0 || ${$sell}==0)
00708 continue;
00709 $sum+=${$sell}*${$qt};
00710 echo_debug('class_document',__LINE__,'sum :'.$sum);
00711
00712 }
00713 $r=round($sum,2);
00714 break;
00715 case 'TOTAL_VEN_TVAC':
00716 extract($_POST);
00717 $sum=0.0;
00718 for ($i=0;$i<$nb_item;$i++)
00719 {
00720 $tva=GetTvaRate($this->db,${'e_march'.$i.'_tva_id'});
00721 $tva_rate=( $tva == null || $tva == 0 )?0.0:$tva['tva_rate'];
00722 echo_debug('class_document',__LINE__,' :'.$i.' sur '.$nb_item);
00723 $sell=${'e_march'.$i.'_sell'};
00724 $qt=${'e_quant'.$i};
00725 echo_debug('class_document',__LINE__,'sell :'.$sell.' qt = '.$qt);
00726
00727 $sum+=$sell*$qt*(1+$tva_rate);
00728
00729 }
00730 $r=round($sum,2);
00731
00732 break;
00733
00734 }
00735 return $r;
00736 }
00740 function remove()
00741 {
00742 $sql='delete from document where d_id='.$this->d_id;
00743 ExecSql($this->db,$sql);
00744 }
00751 function MoveDocumentPj($p_internal)
00752 {
00753
00754 $sql=sprintf("update jrn set jr_pj=%s,jr_pj_name='%s',jr_pj_type='%s' where jr_internal='%s'",
00755 $this->d_lob,$this->d_filename,$this->d_mimetype,$p_internal);
00756 ExecSql($this->db,$sql);
00757
00758 $this->remove();
00759 }
00760
00761
00762 }