00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 require_once('class_own.php');
00021 require_once('class_acc_account_ledger.php');
00022 require_once('class_follow_up.php');
00023 require_once('class_acc_tva.php');
00024 require_once('class_user.php');
00025 require_once('class_zip_extended.php');
00026
00027
00028
00029
00030
00031
00032 class Document
00033 {
00034 var $db;
00035 var $d_id;
00036 var $ag_id;
00037 var $d_mimetype;
00038 var $d_filename;
00039 var $d_lob;
00040 var $d_description;
00041 var $d_number;
00042 var $md_id;
00043
00044
00045
00046 function Document($p_cn,$p_d_id=0)
00047 {
00048 $this->db=$p_cn;
00049 $this->d_id=$p_d_id;
00050 $this->counter=0;
00051 }
00052
00053
00054 function blank()
00055 {
00056 $this->d_id=$this->db->get_next_seq("document_d_id_seq");
00057
00058 $this->d_number=$this->db->get_next_seq("seq_doc_type_".$this->md_type);
00059 $sql=sprintf('insert into document(d_id,ag_id,d_number) values(%d,%d,%d)',
00060 $this->d_id,
00061 $this->ag_id,
00062 $this->d_number);
00063 $this->db->exec_sql($sql);
00064
00065 }
00066 function compute_filename($pj,$filename)
00067 {
00068 foreach (array('/','*','<','>',';',',','\\','.',':') as $i) {
00069 $pj= str_replace($i, "-",$pj);
00070 }
00071
00072 $pos_prefix=strrpos($filename,".");
00073 if ($pos_prefix == 0) $pos_prefix=strlen($filename);
00074 $filename_no=substr($filename,0,$pos_prefix);
00075 $filename_suff=substr($filename,$pos_prefix,strlen($filename));
00076 $new_filename= strtolower($filename_no."-".$pj.$filename_suff);
00077 return $new_filename;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 function Generate($p_array,$p_filename="")
00088 {
00089
00090 $dirname=tempnam($_ENV['TMP'],'doc_');
00091
00092
00093 unlink($dirname);
00094 mkdir ($dirname);
00095
00096 $this->db->start();
00097 $dm_info="select md_name,md_type,md_lob,md_filename,md_mimetype
00098 from document_modele where md_id=".$this->md_id;
00099 $Res=$this->db->exec_sql($dm_info);
00100
00101 $row=Database::fetch_array($Res,0);
00102 $this->d_lob=$row['md_lob'];
00103 $this->d_filename=$row['md_filename'];
00104 $this->d_mimetype=$row['md_mimetype'];
00105 $this->d_name=$row['md_name'];
00106
00107
00108 chdir($dirname);
00109 $filename=$row['md_filename'];
00110 $exp=$this->db->lo_export($row['md_lob'],$dirname.DIRECTORY_SEPARATOR.$filename);
00111 if ( $exp === false ) echo_warning( __FILE__.":".__LINE__."Export NOK $filename");
00112
00113 $type="n";
00114
00115
00116 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00117 {
00118 ob_start();
00119 $zip = new Zip_Extended;
00120 if ($zip->open($filename) === TRUE) {
00121 $zip->extractTo($dirname.DIRECTORY_SEPARATOR);
00122 $zip->close();
00123 } else {
00124 echo __FILE__.":".__LINE__."cannot unzip model ".$filename;
00125 }
00126
00127
00128 unlink($filename);
00129 ob_end_clean();
00130 $file_to_parse="content.xml";
00131 $type="OOo";
00132 }
00133 else
00134 $file_to_parse=$filename;
00135
00136 $this->d_number=$this->db->get_next_seq("seq_doc_type_".$row['md_type']);
00137
00138
00139 $this->ParseDocument($dirname,$file_to_parse,$type,$p_array);
00140
00141 $this->db->commit();
00142
00143 if ( strpos($row['md_mimetype'],'vnd.oasis') != 0 )
00144 {
00145 ob_start();
00146 $zip = new Zip_Extended;
00147 $res = $zip->open($filename, ZipArchive::CREATE);
00148 if($res !== TRUE)
00149 {
00150 throw new Exception ( __FILE__.":".__LINE__."cannot recreate zip");
00151 }
00152 $zip->add_recurse_folder($dirname.DIRECTORY_SEPARATOR);
00153 $zip->close();
00154
00155 ob_end_clean();
00156
00157 $file_to_parse=$filename;
00158 }
00159 if ( $p_filename !="") {
00160
00161 $this->d_filename=$this->compute_filename($p_filename, $this->d_filename);
00162 }
00163 $this->SaveGenerated($dirname.DIRECTORY_SEPARATOR.$file_to_parse);
00164
00165 $ret='<A class="mtitle" HREF="show_document.php?d_id='.$this->d_id.'&'.dossier::get().'">Document généré</A>';
00166 @rmdir($dirname);
00167 return $ret;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 function ParseDocument($p_dir,$p_file,$p_type,$p_array)
00182 {
00183
00184
00185
00186
00187
00188
00189
00190
00191 $infile_name=$p_dir.DIRECTORY_SEPARATOR.$p_file;
00192 $h=fopen($infile_name,"r");
00193
00194
00195 $temp_dir=$_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR.'tmp';
00196 if ( is_dir($temp_dir) == false )
00197 {
00198 if ( mkdir($temp_dir) == false )
00199 {
00200 $msg=_("Ne peut pas créer le répertoire ".$temp_dir);
00201 throw new Exception($msg);
00202 }
00203 }
00204
00205 $output_name=tempnam($temp_dir,"gen_doc_");
00206 $output_file=fopen($output_name,"w+");
00207
00208 if ( $h === false )
00209 {
00210 echo __FILE__.":".__LINE__."cannot open $p_dir $p_file ";
00211 $msg=_("Ne peut pas ouvrir $p_dir $p_file");
00212 throw new Exception($msg);
00213 }
00214 if ( $output_file == false)
00215 {
00216 $msg=_("Ne peut pas ouvrir $p_dir $p_file");
00217 echo $msg;
00218 throw new Exception($msg);
00219 }
00220
00221 if ( $p_type=='OOo')
00222 {
00223 $regex="/=*<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[A-Z]*_*[0-9]*>>/i";
00224 $lt="<";
00225 $gt=">";
00226 }
00227 else
00228 {
00229 $regex="/=*<<[A-Z]+_*[A-Z]*_*[A-Z]*_*[A-Z]*_*[0-9]*>>/i";
00230 $lt="<";
00231 $gt=">";
00232 }
00233
00234
00235 while(! feof($h))
00236 {
00237
00238 $buffer=fgets($h);
00239
00240
00241 while ( preg_match_all ($regex,$buffer,$f) >0 )
00242 {
00243
00244
00245 foreach ( $f as $apattern )
00246 {
00247
00248
00249 foreach($apattern as $pattern)
00250 {
00251
00252
00253 $to_remove=$pattern;
00254
00255 $tag=str_replace($lt,'',$pattern);
00256 $tag=str_replace($gt,'',$tag);
00257
00258
00259
00260 $value=$this->Replace($tag,$p_array);
00261 if ( strpos($value,'ERROR') != false ) $value="";
00262
00263
00264
00265
00266 if ( is_numeric($value) && $p_type=='OOo')
00267 {
00268 $searched='/office:value-type="string"><text:p>'.$pattern.'/';
00269 $replaced='office:value-type="float" office:value="'.$value.'"><text:p>'.$pattern;
00270 $buffer=preg_replace($searched, $replaced, $buffer,1);
00271 }
00272
00273
00274 $pos=strpos($buffer,$to_remove);
00275
00276 $len=strlen($to_remove);
00277 if ( $p_type=='OOo' )
00278 {
00279 $value=str_replace('&','&',$value);
00280 $value=str_replace('<','<',$value);
00281 $value=str_replace('>','>',$value);
00282 $value=str_replace('"','"',$value);
00283 $value=str_replace("'",''',$value);
00284 }
00285 $buffer=substr_replace($buffer,$value,$pos,$len);
00286
00287
00288 }
00289 }
00290 }
00291
00292 fwrite($output_file,$buffer);
00293
00294 }
00295 fclose($h);
00296 fclose($output_file);
00297 if ( ($ret=copy ($output_name,$infile_name)) == FALSE )
00298 {
00299 echo _('Ne peut pas sauver '.$output_name.' vers '.$infile_name.' code d\'erreur ='.$ret);
00300 }
00301
00302
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 function SaveGenerated($p_file)
00312 {
00313
00314 $doc=new Document($this->db);
00315 $this->db->start();
00316 $this->d_lob=$this->db->lo_import($p_file);
00317 if ( $this->d_lob == false )
00318 {
00319 echo "ne peut pas importer [$p_file]";
00320 return 1;
00321 }
00322
00323 $sql="insert into document(ag_id,d_lob,d_number,d_filename,d_mimetype)
00324 values ($1,$2,$3,$4,$5)";
00325
00326 $this->db->exec_sql($sql, array($this->ag_id,
00327 $this->d_lob,
00328 $this->d_number,
00329 $this->d_filename,
00330 $this->d_mimetype));
00331 $this->d_id=$this->db->get_current_seq("document_d_id_seq");
00332
00333 unlink ($p_file);
00334 $this->db->commit();
00335 return 0;
00336 }
00337
00338
00339
00340
00341
00342
00343
00344 function Upload($p_ag_id)
00345 {
00346
00347 if ( sizeof($_FILES) == 0 ) return;
00348
00349
00350
00351
00352 $this->db->start();
00353 $name=$_FILES['file_upload']['name'];
00354 for ($i = 0; $i < sizeof($name);$i++)
00355 {
00356 $new_name=tempnam($_ENV['TMP'],'doc_');
00357
00358 if ( strlen($_FILES['file_upload']['tmp_name'][$i]) != 0 )
00359 {
00360
00361 if ( move_uploaded_file($_FILES['file_upload']['tmp_name'][$i],$new_name))
00362 {
00363 $oid=$this->db->lo_import($new_name);
00364
00365 if ( $oid == false )
00366 {
00367 $this->db->rollback();
00368 return 1;
00369 }
00370 }
00371
00372 $this->d_lob=$oid;
00373 $this->d_filename=$_FILES['file_upload']['name'][$i];
00374 $this->d_mimetype=$_FILES['file_upload']['type'][$i];
00375 $this->d_description= strip_tags($_POST['input_desc'][$i]);
00376
00377 $sql="insert into document (ag_id, d_lob,d_filename,d_mimetype,d_number,d_description) values ($1,$2,$3,$4,$5,$6)";
00378 $this->db->exec_sql($sql,array($p_ag_id,$this->d_lob,$this->d_filename,$this->d_mimetype,1,$this->d_description));
00379 }
00380 }
00381 $this->db->commit();
00382
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 static function insert_existing_document($p_ag_id, $p_lob, $p_filename, $p_mimetype, $p_description = "")
00394 {
00395 global $cn;
00396
00397 $sql = "insert into document (ag_id, d_lob,d_filename,d_mimetype,d_number,d_description) values ($1,$2,$3,$4,$5,$6)";
00398 $cn->exec_sql($sql, array($p_ag_id, $p_lob, $p_filename, $p_mimetype, 1, $p_description));
00399 }
00400
00401
00402
00403
00404
00405
00406 function anchor()
00407 {
00408 if ( $this->d_id == 0 )
00409 return '';
00410 $image='<IMG SRC="image/insert_table.gif" title="'.$this->d_filename.'" border="0">';
00411 $r="";
00412 $r='<A class="mtitle" HREF="show_document.php?d_id='.$this->d_id.'&'.dossier::get().'">'.$image.'</A>';
00413 return $r;
00414 }
00415
00416
00417
00418 function Send()
00419 {
00420
00421 $this->db->start();
00422 $ret=$this->db->exec_sql(
00423 "select d_id,d_lob,d_filename,d_mimetype from document where d_id=".$this->d_id );
00424 if ( Database::num_row ($ret) == 0 )
00425 return;
00426 $row=Database::fetch_array($ret,0);
00427
00428 $tmp=tempnam($_ENV['TMP'],'document_');
00429 $this->db->lo_export($row['d_lob'],$tmp);
00430 $this->d_mimetype=$row['d_mimetype'];
00431 $this->d_filename=$row['d_filename'];
00432
00433
00434 ini_set('zlib.output_compression','Off');
00435 header("Pragma: public");
00436 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00437 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00438 header("Cache-Control: must-revalidate");
00439 header('Content-type: '.$this->d_mimetype);
00440 header('Content-Disposition: attachment;filename="'.$this->d_filename.'"',FALSE);
00441 header("Accept-Ranges: bytes");
00442 $file=fopen($tmp,'r');
00443 while ( !feof ($file) )
00444 {
00445 echo fread($file,8192);
00446 }
00447 fclose($file);
00448
00449 unlink ($tmp);
00450
00451 $this->db->commit();
00452
00453 }
00454
00455
00456
00457
00458 function get_all($ag_id)
00459 {
00460 $res=$this->db->get_array('select d_id, ag_id, d_lob, d_number, d_filename,'.
00461 ' d_mimetype,d_description from document where ag_id=$1',array($ag_id));
00462 $a=array();
00463 for ($i=0;$i<sizeof($res); $i++ )
00464 {
00465 $doc=new Document($this->db);
00466 $doc->d_id=$res[$i]['d_id'];
00467 $doc->ag_id=$res[$i]['ag_id'];
00468 $doc->d_lob=$res[$i]['d_lob'];
00469 $doc->d_number=$res[$i]['d_number'];
00470 $doc->d_filename=$res[$i]['d_filename'];
00471 $doc->d_mimetype=$res[$i]['d_mimetype'];
00472 $doc->d_description=$row['d_description'];
00473 $a[$i]=clone $doc;
00474 }
00475 return $a;
00476 }
00477
00478
00479
00480 function get()
00481 {
00482 $sql="select * from document where d_id=".$this->d_id;
00483 $ret=$this->db->exec_sql($sql);
00484 if ( Database::num_row($ret) == 0 )
00485 return;
00486 $row=Database::fetch_array($ret,0);
00487 $this->ag_id=$row['ag_id'];
00488 $this->d_mimetype=$row['d_mimetype'];
00489 $this->d_filename=$row['d_filename'];
00490 $this->d_lob=$row['d_lob'];
00491 $this->d_number=$row['d_number'];
00492 $this->d_description=$row['d_description'];
00493
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 function Replace($p_tag,$p_array)
00553 {
00554 global $g_parameter;
00555 $p_tag=strtoupper($p_tag);
00556 $p_tag=str_replace('=','',$p_tag);
00557 $r="Tag inconnu";
00558
00559 switch ($p_tag)
00560 {
00561 case 'DATE':
00562 $r=(isset ($p_array['ag_timestamp']))?$p_array['ag_timestamp']:$p_array['e_date'];
00563 break;
00564 case 'DATE_CALC':
00565 $r=' Date inconnue ';
00566
00567
00568 if ( isset ($p_array['ag_timestamp'])) {
00569 $date=format_date($p_array['ag_timestamp'],'DD.MM.YYYY','YYYY-MM-DD');
00570 $r=$date;
00571 }
00572 if ( isset ($p_array['e_date'])) {
00573 $date=format_date($p_array['e_date'],'DD.MM.YYYY','YYYY-MM-DD');
00574 $r=$date;
00575 }
00576 break;
00577
00578
00579
00580 case 'MY_NAME':
00581 $r=$g_parameter->MY_NAME;
00582 break;
00583 case 'MY_CP':
00584 $r=$g_parameter->MY_CP;
00585 break;
00586 case 'MY_COMMUNE':
00587 $r=$g_parameter->MY_COMMUNE;
00588 break;
00589 case 'MY_TVA':
00590 $r=$g_parameter->MY_TVA;
00591 break;
00592 case 'MY_STREET':
00593 $r=$g_parameter->MY_STREET;
00594 break;
00595 case 'MY_NUMBER':
00596 $r=$g_parameter->MY_NUMBER;
00597 break;
00598 case 'MY_TEL':
00599 $r=$g_parameter->MY_TEL;
00600 break;
00601 case 'MY_FAX':
00602 $r=$g_parameter->MY_FAX;
00603 break;
00604 case 'MY_PAYS':
00605 $r=$g_parameter->MY_PAYS;
00606 break;
00607
00608
00609
00610
00611
00612 case 'SOLDE':
00613 $tiers=new Fiche($this->db);
00614 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00615 $tiers->get_by_qcode($qcode,false);
00616 $p=$tiers->strAttribut(ATTR_DEF_ACCOUNT);
00617 $poste=new Acc_Account_Ledger($this->db,$p);
00618 $r=$poste->get_solde(' true' );
00619 break;
00620 case 'CUST_NAME':
00621 $tiers=new Fiche($this->db);
00622 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00623 $tiers->get_by_qcode($qcode,false);
00624 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00625 break;
00626 case 'CUST_ADDR_1':
00627 $tiers=new Fiche($this->db);
00628 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00629 $tiers->get_by_qcode($qcode,false);
00630 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00631
00632 break ;
00633 case 'CUST_CP':
00634 $tiers=new Fiche($this->db);
00635
00636 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00637 $tiers->get_by_qcode($qcode,false);
00638 $r=$tiers->strAttribut(ATTR_DEF_CP);
00639
00640 break;
00641 case 'CUST_CITY':
00642 $tiers=new Fiche($this->db);
00643
00644 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00645 $tiers->get_by_qcode($qcode,false);
00646 $r=$tiers->strAttribut(ATTR_DEF_CITY);
00647
00648 break;
00649
00650 case 'CUST_CO':
00651 $tiers=new Fiche($this->db);
00652
00653 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00654 $tiers->get_by_qcode($qcode,false);
00655 $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00656
00657 break;
00658
00659
00660 case 'CUST_VAT':
00661 $tiers=new Fiche($this->db);
00662
00663 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00664 $tiers->get_by_qcode($qcode,false);
00665 $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00666 break;
00667 case 'CUST_NUM':
00668 $tiers=new Fiche($this->db);
00669 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00670 $tiers->get_by_qcode($qcode,false);
00671 $r=$tiers->strAttribut(ATTR_DEF_NUMBER_CUSTOMER);
00672 break;
00673 case 'CUST_BANQUE_NO':
00674 $tiers=new Fiche($this->db);
00675 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00676 $tiers->get_by_qcode($qcode,false);
00677 $r=$tiers->strAttribut(ATTR_DEF_BQ_NO);
00678 break;
00679 case 'CUST_BANQUE_NAME':
00680 $tiers=new Fiche($this->db);
00681 $qcode=isset($p_array['qcode_dest'])?$p_array['qcode_dest']:$p_array['e_client'];
00682 $tiers->get_by_qcode($qcode,false);
00683 $r=$tiers->strAttribut(ATTR_DEF_BQ_NAME);
00684 break;
00685
00686
00687 case 'BENEF_NAME':
00688 $tiers=new Fiche($this->db);
00689 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00690 if ( $qcode=='')
00691 {
00692 $r='';
00693 break;
00694 }
00695 $tiers->get_by_qcode($qcode,false);
00696 $r=$tiers->strAttribut(ATTR_DEF_NAME);
00697 break;
00698 case 'BENEF_ADDR_1':
00699 $tiers=new Fiche($this->db);
00700 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00701 if ( $qcode=='')
00702 {
00703 $r='';
00704 break;
00705 }
00706 $tiers->get_by_qcode($qcode,false);
00707 $r=$tiers->strAttribut(ATTR_DEF_ADRESS);
00708
00709 break ;
00710 case 'BENEF_CP':
00711 $tiers=new Fiche($this->db);
00712
00713 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00714 if ( $qcode=='')
00715 {
00716 $r='';
00717 break;
00718 }
00719 $tiers->get_by_qcode($qcode,false);
00720 $r=$tiers->strAttribut(ATTR_DEF_CP);
00721
00722 break;
00723 case 'BENEF_CITY':
00724 $tiers=new Fiche($this->db);
00725
00726 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00727 if ( $qcode=='')
00728 {
00729 $r='';
00730 break;
00731 }
00732 $tiers->get_by_qcode($qcode,false);
00733 $r=$tiers->strAttribut(ATTR_DEF_CITY);
00734
00735 break;
00736
00737 case 'BENEF_CO':
00738 $tiers=new Fiche($this->db);
00739
00740 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00741 if ( $qcode=='')
00742 {
00743 $r='';
00744 break;
00745 }
00746 $tiers->get_by_qcode($qcode,false);
00747 $r=$tiers->strAttribut(ATTR_DEF_PAYS);
00748
00749 break;
00750
00751
00752 case 'BENEF_VAT':
00753 $tiers=new Fiche($this->db);
00754
00755 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00756 if ( $qcode=='')
00757 {
00758 $r='';
00759 break;
00760 }
00761 $tiers->get_by_qcode($qcode,false);
00762 $r=$tiers->strAttribut(ATTR_DEF_NUMTVA);
00763 break;
00764 case 'BENEF_NUM':
00765 $tiers=new Fiche($this->db);
00766 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00767 if ( $qcode=='')
00768 {
00769 $r='';
00770 break;
00771 }
00772 $tiers->get_by_qcode($qcode,false);
00773 $r=$tiers->strAttribut(ATTR_DEF_NUMBER_CUSTOMER);
00774 break;
00775 case 'BENEF_BANQUE_NO':
00776 $tiers=new Fiche($this->db);
00777 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00778 if ( $qcode=='')
00779 {
00780 $r='';
00781 break;
00782 }
00783 $tiers->get_by_qcode($qcode,false);
00784 $r=$tiers->strAttribut(ATTR_DEF_BQ_NO);
00785 break;
00786 case 'BENEF_BANQUE_NAME':
00787 $tiers=new Fiche($this->db);
00788 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
00789 if ( $qcode=='')
00790 {
00791 $r='';
00792 break;
00793 }
00794 $tiers->get_by_qcode($qcode,false);
00795 $r=$tiers->strAttribut(ATTR_DEF_BQ_NAME);
00796 break;
00797
00798
00799
00800 case 'NUMBER':
00801 $r=$this->d_number;
00802 break;
00803
00804 case 'USER' :
00805 return $_SESSION['use_name'].', '.$_SESSION['use_first_name'];
00806
00807 break;
00808 case 'REFERENCE':
00809 $act=new Follow_Up($this->db);
00810 $act->ag_id=$this->ag_id;
00811 $act->get();
00812 $r=$act->ag_ref;
00813 break;
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827 case 'DATE_LIMIT_CALC':
00828 extract ($p_array);
00829 $id='e_ech' ;
00830 if ( !isset (${$id}) ) return "";
00831 $r=format_date(${$id},'DD.MM.YYYY','YYYY-MM-DD');
00832 break;
00833 case 'DATE_LIMIT':
00834 extract ($p_array);
00835 $id='e_ech' ;
00836 if ( !isset (${$id}) ) return "";
00837 $r=${$id};
00838 break;
00839 case 'MARCH_NEXT':
00840 $this->counter++;
00841 $r='';
00842 break;
00843
00844 case 'VEN_ART_NAME':
00845 extract ($p_array);
00846 $id='e_march'.$this->counter;
00847
00848 if ( ! isset (${$id})) return "";
00849
00850 if ( ${'e_march'.$this->counter.'_price'} != 0 && ${'e_quant'.$this->counter} != 0 )
00851 {
00852 $f=new Fiche($this->db);
00853 $f->get_by_qcode(${$id},false);
00854 $r=$f->strAttribut(ATTR_DEF_NAME);
00855 }
00856 else $r = "";
00857 break;
00858 case 'VEN_ART_LABEL':
00859 extract ($p_array);
00860 $id='e_march'.$this->counter."_label";
00861
00862
00863 if (! isset (${$id}) || (isset (${$id}) && strlen(trim(${$id})) == 0))
00864 {
00865 $id = 'e_march' . $this->counter;
00866
00867 if (!isset(${$id}))
00868 $r= "";
00869 else
00870 {
00871
00872 if (${'e_march' . $this->counter . '_price'} != 0 && ${'e_quant' . $this->counter} != 0)
00873 {
00874 $f = new Fiche($this->db);
00875 $f->get_by_qcode(${$id}, false);
00876 $r = $f->strAttribut(ATTR_DEF_NAME);
00877 } else
00878 $r = "";
00879 }
00880 }
00881 else
00882 $r=${'e_march'.$this->counter.'_label'};
00883 break;
00884 case 'VEN_ART_STOCK_CODE':
00885 extract ($p_array);
00886 $id = 'e_march' . $this->counter;
00887
00888 if (!isset(${$id}))
00889 $r= "";
00890 else
00891 {
00892
00893 if (${'e_march' . $this->counter . '_price'} != 0 && ${'e_quant' . $this->counter} != 0)
00894 {
00895 $f = new Fiche($this->db);
00896 $f->get_by_qcode(${$id}, false);
00897 $r = $f->strAttribut(ATTR_DEF_STOCK);
00898 $r=($r == NOTFOUND)?'':$r;
00899 }
00900 }
00901 break;
00902 case 'VEN_ART_PRICE':
00903 extract ($p_array);
00904 $id='e_march'.$this->counter.'_price' ;
00905 if ( !isset (${$id}) ) return "";
00906 if (${$id} == 0 ) return "";
00907 $r=${$id};
00908 break;
00909
00910 case 'TVA_RATE':
00911 case 'VEN_ART_TVA_RATE':
00912 extract ($p_array);
00913 $id='e_march'.$this->counter.'_tva_id';
00914 if ( !isset (${$id}) ) return "";
00915 if ( ${$id} == -1 || ${$id}=='' ) return "";
00916 $march_id='e_march'.$this->counter.'_price' ;
00917 if ( ! isset (${$march_id})) return '';
00918 $tva=new Acc_Tva($this->db);
00919 $tva->set_parameter("id",${$id});
00920 if ( $tva->load() == -1) return '';
00921 return $tva->get_parameter("rate");
00922 break;
00923
00924 case 'TVA_CODE':
00925 case 'VEN_ART_TVA_CODE':
00926 extract ($p_array);
00927 $id='e_march'.$this->counter.'_tva_id';
00928 if ( !isset (${$id}) ) return "";
00929 if ( ${$id} == -1 ) return "";
00930 $qt='e_quant'.$this->counter;
00931 $price='e_march'.$this->counter.'_price' ;
00932 if ( ${$price} == 0 || ${$qt} == 0
00933 || strlen(trim( $price )) ==0
00934 || strlen(trim($qt)) ==0)
00935 return "";
00936
00937 $r=${$id};
00938 break;
00939
00940 case 'TVA_LABEL':
00941 extract ($p_array);
00942 $id='e_march'.$this->counter.'_tva_id';
00943 if ( !isset (${$id}) ) return "";
00944 $march_id='e_march'.$this->counter.'_price' ;
00945 if ( ! isset (${$march_id})) return '';
00946 if ( ${$march_id} == 0) return '';
00947 $tva=new Acc_Tva($this->db,${$id});
00948 if ($tva->load() == -1 ) return "";
00949 $r=$tva->get_parameter('label');
00950
00951 break;
00952
00953
00954 case 'TVA_AMOUNT':
00955 case 'VEN_TVA':
00956 extract ($p_array);
00957 $qt='e_quant'.$this->counter;
00958 $price='e_march'.$this->counter.'_price' ;
00959 $tva='e_march'.$this->counter.'_tva_id';
00960
00961 if ( !isset(${$tva}) ) return '';
00962 if ( !isset (${'e_march'.$this->counter}) ) return "";
00963
00964 if ( ${$price} == 0 || ${$qt} == 0
00965 || strlen(trim( $price )) ==0
00966 || strlen(trim($qt)) ==0)
00967 return "";
00968 $r=${'e_march'.$this->counter.'_tva_amount'};
00969 break;
00970
00971 case 'VEN_ART_TVA':
00972
00973 extract ($p_array);
00974 $qt='e_quant'.$this->counter;
00975 $price='e_march'.$this->counter.'_price' ;
00976 $tva='e_march'.$this->counter.'_tva_id';
00977 if ( !isset (${'e_march'.$this->counter}) ) return "";
00978
00979 if ( ${$price} == 0 || ${$qt} == 0
00980 || strlen(trim( $price )) ==0
00981 || strlen(trim($qt)) ==0)
00982 return "";
00983 $oTva=new Acc_Tva($this->db,${$tva});
00984 if ($oTva->load() == -1 ) return "";
00985 $r=round(${$price},2)*$oTva->get_parameter('rate');
00986 $r=round($r,2);
00987 break;
00988
00989 case 'VEN_ART_TVAC':
00990 extract ($p_array);
00991 $qt='e_quant'.$this->counter;
00992 $price='e_march'.$this->counter.'_price' ;
00993 $tva='e_march'.$this->counter.'_tva_id';
00994 if ( !isset (${'e_march'.$this->counter}) ) return "";
00995
00996 if ( ${$price} == 0 || ${$qt} == 0
00997 || strlen(trim( $price )) ==0
00998 || strlen(trim($qt)) ==0)
00999 return "";
01000 if ( ! isset (${$tva}) ) return '';
01001 $tva=new Acc_Tva($this->db,${$tva});
01002 if ($tva->load() == -1 )
01003 {
01004 $r=round(${$price},2);
01005 }
01006 else
01007 {
01008 $r=round(${$price}*$tva->get_parameter('rate')+${$price},2);
01009 }
01010
01011 break;
01012
01013 case 'VEN_ART_QUANT':
01014 extract ($p_array);
01015 $id='e_quant'.$this->counter;
01016 if ( !isset (${$id}) ) return "";
01017
01018 if ( ${'e_march'.$this->counter.'_price'} == 0
01019 || ${'e_quant'.$this->counter} == 0
01020 || strlen(trim( ${'e_march'.$this->counter.'_price'} )) ==0
01021 || strlen(trim(${'e_quant'.$this->counter})) ==0 )
01022 return "";
01023 $r=${$id};
01024 break;
01025
01026 case 'VEN_HTVA':
01027 extract ($p_array);
01028 $id='e_march'.$this->counter.'_price' ;
01029 $quant='e_quant'.$this->counter;
01030 if ( !isset (${$id}) ) return "";
01031
01032
01033 if ( ${'e_march'.$this->counter.'_price'} == 0 || ${'e_quant'.$this->counter} == 0
01034 || strlen(trim( ${'e_march'.$this->counter.'_price'} )) ==0
01035 || strlen(trim(${'e_quant'.$this->counter})) ==0)
01036 return "";
01037 bcscale(4);
01038 $r=bcmul(${$id},${$quant});
01039 $r=round($r,2);
01040 break;
01041
01042 case 'VEN_TVAC':
01043 extract ($p_array);
01044 $id='e_march'.$this->counter.'_tva_amount' ;
01045 $price='e_march'.$this->counter.'_price' ;
01046 $quant='e_quant'.$this->counter;
01047 if ( ! isset(${'e_march'.$this->counter.'_price'})|| !isset(${'e_quant'.$this->counter})) return "";
01048
01049 if ( ${'e_march'.$this->counter.'_price'} == 0 || ${'e_quant'.$this->counter} == 0 ) return "";
01050 bcscale(4);
01051
01052 if ( ! isset(${$id}))
01053 $r= bcmul(${$price},${$quant});
01054 else{
01055 $r= bcmul(${$price},${$quant});
01056 $r=bcadd($r,${$id});
01057 }
01058 $r=round($r,2);
01059 return $r;
01060 break;
01061
01062 case 'TOTAL_VEN_HTVA':
01063 extract($p_array);
01064 bcscale(4);
01065 $sum=0.0;
01066 for ($i=0;$i<$nb_item;$i++)
01067 {
01068 $sell='e_march'.$i.'_price';
01069 $qt='e_quant'.$i;
01070
01071 if ( ! isset (${$sell}) ) break;
01072
01073 if ( strlen(trim(${$sell})) == 0 ||
01074 strlen(trim(${$qt})) == 0 ||
01075 ${$qt}==0 || ${$sell}==0)
01076 continue;
01077 $tmp1=bcmul(${$sell},${$qt});
01078 $sum=bcadd($sum,$tmp1);
01079
01080
01081 }
01082 $r=round($sum,2);
01083 break;
01084 case 'TOTAL_VEN_TVAC':
01085 extract($p_array);
01086 $sum=0.0;
01087 bcscale(4);
01088 for ($i=0;$i<$nb_item;$i++)
01089 {
01090 $tva='e_march'.$i.'_tva_amount';
01091 $tva_amount=0;
01092
01093 if ( isset(${$tva}) )
01094 {
01095 $tva_amount=${$tva};
01096 }
01097 $sell=${'e_march'.$i.'_price'};
01098 $qt=${'e_quant'.$i};
01099 $tot=bcmul($sell,$qt);
01100 $tot=bcadd($tot,$tva_amount);
01101 $sum=bcadd($sum,$tot);
01102 }
01103 $r=round($sum,2);
01104
01105 break;
01106 case 'TOTAL_TVA':
01107 extract($p_array);
01108 $sum=0.0;
01109 for ($i=0;$i<$nb_item;$i++)
01110 {
01111 $tva='e_march'.$i.'_tva_amount';
01112 if (! isset(${$tva})) $tva_amount=0.0;
01113 else $tva_amount=$
01114 {
01115 $tva
01116 };
01117 $sum+=$tva_amount;
01118 $sum=round($sum,2);
01119 }
01120 $r=$sum;
01121
01122 break;
01123 case 'BON_COMMANDE':
01124 if ( isset($p_array['bon_comm']))
01125 return $p_array['bon_comm'];
01126 else
01127 return "";
01128 break;
01129 case 'PJ':
01130 if ( isset($p_array['e_pj']))
01131 return $p_array['e_pj'];
01132 else
01133 return "";
01134
01135 case 'OTHER_INFO':
01136 if ( isset($p_array['other_info']))
01137 return $p_array['other_info'];
01138 else
01139 return "";
01140 break;
01141 case 'COMMENT':
01142 if ( isset($p_array['e_comm']))
01143 return $p_array['e_comm'];
01144 break;
01145 case 'ACOMPTE':
01146 if ( isset($p_array['acompte']))
01147 return $p_array['acompte'];
01148 return "0";
01149 break;
01150 case 'STOCK_NAME':
01151 if ( ! isset ($p_array['repo'])) return "";
01152 $ret=$this->db->get_value('select r_name from public.stock_repository where r_id=$1',array($p_array['repo']));
01153 return $ret;
01154 case 'STOCK_ADRESS':
01155 if ( ! isset ($p_array['repo'])) return "";
01156 $ret=$this->db->get_value('select r_adress from public.stock_repository where r_id=$1',array($p_array['repo']));
01157 return $ret;
01158 case 'STOCK_COUNTRY':
01159 if ( ! isset ($p_array['repo'])) return "";
01160 $ret=$this->db->get_value('select r_country from public.stock_repository where r_id=$1',array($p_array['repo']));
01161 return $ret;
01162 case 'STOCK_CITY':
01163 if ( ! isset ($p_array['repo'])) return "";
01164 $ret=$this->db->get_value('select r_city from public.stock_repository where r_id=$1',array($p_array['repo']));
01165 return $ret;
01166 case 'STOCK_PHONE':
01167 if ( ! isset ($p_array['repo'])) return "";
01168 $ret=$this->db->get_value('select r_phone from public.stock_repository where r_id=$1',array($p_array['repo']));
01169 return $ret;
01170 case 'TITLE':
01171 $title=HtmlInput::default_value_request("ag_title", "");
01172 return $title;
01173
01174 }
01175
01176
01177
01178 if (preg_match('/^ATTR/', $p_tag) == 1)
01179 {
01180
01181 if ( isset ($p_array['e_march'.$this->counter]))
01182 {
01183 $id = $p_array['e_march' . $this->counter];
01184 $r=$this->replace_special_tag($id,$p_tag);
01185 }
01186 }
01187
01188
01189
01190 if (preg_match('/^BENEFATTR/', $p_tag) == 1)
01191 {
01192 $qcode=isset($p_array['qcode_benef'])?$p_array['qcode_benef']:'';
01193
01194 $r=$this->replace_special_tag($qcode,$p_tag);
01195 }
01196 if (preg_match('/^CUSTATTR/', $p_tag) == 1)
01197 {
01198 if ( isset($p_array['qcode_dest']) || isset($p_array['e_client']) )
01199 {
01200 $qcode=(isset($p_array['qcode_dest']))?$p_array['qcode_dest']:$p_array['e_client'];
01201 $r=$this->replace_special_tag($qcode,$p_tag);
01202 }
01203 }
01204 return $r;
01205 }
01206
01207
01208
01209 function remove()
01210 {
01211 $d_lob=$this->db->get_value('select d_lob from document where d_id=$1',
01212 array($this->d_id));
01213 $sql='delete from document where d_id='.$this->d_id;
01214 $this->db->exec_sql($sql);
01215 if ( $d_lob != 0 )
01216 $this->db->lo_unlink($d_lob);
01217 }
01218
01219
01220
01221
01222
01223
01224 function MoveDocumentPj($p_internal)
01225 {
01226 $sql="update jrn set jr_pj=$1,jr_pj_name=$2,jr_pj_type=$3 where jr_internal=$4";
01227
01228 $this->db->exec_sql($sql,array($this->d_lob,$this->d_filename,$this->d_mimetype,$p_internal));
01229
01230 $sql='delete from document where d_id='.$this->d_id;
01231 $this->db->exec_sql($sql);
01232
01233
01234 }
01235
01236
01237
01238
01239
01240
01241
01242
01243 function replace_special_tag($p_qcode, $p_tag)
01244 {
01245
01246 if ($p_qcode == "")
01247 return "";
01248
01249 $f = new Fiche($this->db);
01250 $found = $f->get_by_qcode($p_qcode, false);
01251
01252 if ($found == 1)
01253 return "";
01254
01255
01256 $attr=preg_replace("/^.*ATTR/","",$p_tag);
01257
01258 if (isNumber($attr) == 0) return "";
01259 $ad_type=$this->db->get_value("select ad_type from attr_def where ad_id=$1",array($attr));
01260
01261
01262 $ad_value=$this->db->get_value("select ad_value from fiche_detail where f_id=$1 and ad_id=$2",array($f->id,$attr));
01263
01264
01265 if ( $ad_type=="select")
01266 {
01267 $sql=$this->db->get_value("select ad_extra from attr_def where ad_id=$1",array($attr));
01268 $array= $this->db->make_array($sql);
01269 for ($a=0;$a<count ($array);$a++)
01270 {
01271 if ($array[$a]['value']==$ad_value)
01272 return $array[$a]['label'];
01273 }
01274
01275 }
01276
01277 return $ad_value;
01278 }
01279 function update_description ($p_desc)
01280 {
01281 $this->db->exec_sql('update document set d_description = $1 where d_id=$2',
01282 array($p_desc,$this->d_id));
01283 }
01284
01285 }