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