00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 include_once("class_fiche_attr.php");
00022 require_once("class_itext.php");
00023 require_once('class_fiche_def_ref.php');
00024 require_once('class_fiche.php');
00025 require_once('user_common.php');
00026 require_once('class_iradio.php');
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Fiche_Def
00037 {
00038 var $cn;
00039 var $id;
00040 var $label;
00041 var $class_base;
00042 var $fiche_def;
00043 var $create_account;
00044 var $all;
00045 var $attribut;
00046 function __construct($p_cn,$p_id = 0)
00047 {
00048 $this->cn=$p_cn;
00049 $this->id=$p_id;
00050 }
00051
00052
00053 function input ()
00054 {
00055 $ref=$this->cn->get_array("select * from fiche_def_ref order by frd_text");
00056 $iradio=new IRadio();
00057
00058 $class_base=new IPoste('class_base');
00059 $class_base->set_attribute('ipopup','ipop_account');
00060 $class_base->set_attribute('account','class_base');
00061 $class_base->set_attribute('label','acc_label');
00062 $f_class_base=$class_base->input();
00063 require_once ('template/fiche_def_input.php');
00064 return;
00065 }
00066
00067
00068
00069
00070
00071
00072 function getAttribut()
00073 {
00074 $sql="select * from jnt_fic_attr ".
00075 " natural join attr_def where fd_id=".$this->id.
00076 " order by jnt_order";
00077
00078 $Ret=$this->cn->exec_sql($sql);
00079
00080 if ( ($Max=Database::num_row($Ret)) == 0 )
00081 return ;
00082 for ($i=0;$i < $Max;$i++)
00083 {
00084 $row=Database::fetch_array($Ret,$i);
00085 $t = new Fiche_Attr($this->cn);
00086 $t->ad_id=$row['ad_id'];
00087 $t->ad_text=$row['ad_text'];
00088 $t->jnt_order=$row['jnt_order'];
00089 $t->ad_size=$row['ad_size'];
00090 $t->ad_type=$row['ad_type'];
00091 $t->ad_extra=$row['ad_extra'];
00092 $this->attribut[$i]=clone $t;
00093 }
00094 return $this->attribut;
00095 }
00096
00097
00098
00099
00100
00101 function Get()
00102 {
00103 if ( $this->id == 0 )
00104 return 0;
00105
00106
00107
00108 $sql="select * from fiche_def ".
00109 " where fd_id=".$this->id;
00110 $Ret=$this->cn->exec_sql($sql);
00111 if ( ($Max=Database::num_row($Ret)) == 0 )
00112 return ;
00113 $row=Database::fetch_array($Ret,0);
00114 $this->label=$row['fd_label'];
00115 $this->class_base=$row['fd_class_base'];
00116 $this->fiche_def=$row['frd_id'];
00117 $this->create_account=$row['fd_create_account'];
00118 }
00119
00120
00121
00122
00123
00124
00125 function GetAll()
00126 {
00127 $sql="select * from fiche_def ";
00128
00129 $Ret=$this->cn->exec_sql($sql);
00130 if ( ($Max=Database::num_row($Ret)) == 0 )
00131 return ;
00132
00133 for ( $i = 0; $i < $Max;$i++)
00134 {
00135 $row=Database::fetch_array($Ret,$i);
00136 $this->all[$i]=new Fiche_Def($this->cn,$row['fd_id']);
00137 $this->all[$i]->label=$row['fd_label'];
00138 $this->all[$i]->class_base=$row['fd_class_base'];
00139 $this->all[$i]->fiche_def=$row['frd_id'];
00140 $this->all[$i]->create_account=$row['fd_create_account'];
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 function HasAttribute($p_attr)
00153 {
00154 return ($this->cn->count_sql("select * from vw_fiche_def where ad_id=$p_attr and fd_id=".$this->id)>0)?true:false;
00155
00156 }
00157
00158
00159
00160
00161
00162
00163 function Display()
00164 {
00165 $tab = new Sort_Table();
00166
00167 $url = HtmlInput::get_to_string(array('ac', 'gDossier'));
00168 $tab->add("Nom de fiche", $url, "order by fd_label asc", "order by fd_label desc", "na", "nd");
00169 $tab->add("Basé sur le poste comptable", $url, "order by fd_class_base asc", "order by fd_class_base desc", "pa", "pd");
00170 $tab->add("Calcul automatique du poste comptable", $url, "order by fd_create_account asc", "order by fd_create_account desc", "ca", "cd");
00171 $tab->add("Basé sur le modèle", $url, "order by frd_text asc", "order by frd_text desc", "ma", "md");
00172
00173 $order = (isset($_GET['ord'])) ? $tab->get_sql_order($_GET["ord"]) : $tab->get_sql_order("na");
00174
00175
00176 $res = $this->cn->exec_sql("SELECT fd_id, fd_class_base, fd_label, fd_create_account, fiche_def_ref.frd_id,
00177 frd_text FROM fiche_def join fiche_def_ref on (fiche_def.frd_id=fiche_def_ref.frd_id)
00178 $order
00179 ");
00180
00181 require_once 'template/fiche_def_list.php';
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 function Add($array)
00194 {
00195 foreach ( $array as $key=>$element )
00196 {
00197 ${"p_$key"}=$element;
00198 }
00199
00200 $p_nom_mod=sql_string($p_nom_mod);
00201
00202
00203
00204 if ( strlen(trim($p_nom_mod)) == 0 )
00205 {
00206 alert (_('Le nom de la catégorie ne peut pas être vide'));
00207 return 1;
00208 }
00209
00210 if (! isset ($p_FICHE_REF) || strlen($p_FICHE_REF) == 0 )
00211 {
00212 echo alert (_('Vous devez choisir une categorie'));
00213 return 1;
00214 }
00215 $fiche_Def_ref=new Fiche_Def_Ref($this->cn,$p_FICHE_REF);
00216 $fiche_Def_ref->Get();
00217
00218
00219
00220
00221
00222 if ( sql_string($p_class_base) == null )
00223 {
00224
00225 $p_class_base=$fiche_Def_ref->frd_class_base;
00226 }
00227
00228 $sql="select count(*) from fiche_Def where upper(fd_label)=upper($1)";
00229 $count=$this->cn->get_value($sql,array(trim($p_nom_mod)));
00230
00231 if ($count != 0 ) {
00232 echo alert (_('Catégorie existante'));
00233 return 1;
00234 }
00235
00236
00237 if ( isset($p_create) && strlen(trim($p_class_base)) != 0)
00238 $p_create='true';
00239 else
00240 $p_create='false';
00241
00242
00243 if ( sql_string($p_class_base) != null || strpos(',',$p_class_base) != 0 )
00244 {
00245
00246 $sql="insert into fiche_def(fd_label,fd_class_base,frd_id,fd_create_account)
00247 values ($1,$2,$3,$4) returning fd_id";
00248
00249 $fd_id=$this->cn->get_value($sql,array($p_nom_mod,$p_class_base,$p_FICHE_REF,$p_create));
00250
00251
00252 if ( strpos(',',$p_class_base) ==0)
00253 {
00254 $sql="select account_add($1,$2)";
00255 $Res=$this->cn->exec_sql($sql,array($p_class_base,$p_nom_mod));
00256 }
00257
00258
00259 $fd_id=$this->cn->get_current_seq('s_fdef');
00260
00261
00262 $sql=sprintf("insert into jnt_fic_attr(fd_id,ad_id,jnt_order)
00263 values (%d,%d,10)",$fd_id,ATTR_DEF_ACCOUNT);
00264 $Res=$this->cn->exec_sql($sql);
00265
00266 }
00267 else
00268 {
00269
00270 $sql=sprintf("insert into fiche_def(fd_label,frd_id,fd_create_account) values ('%s',%d,'%s') returning fd_id",
00271 $p_nom_mod,$p_FICHE_REF,$p_create);
00272
00273 $this->id=$this->cn->get_value($sql);
00274
00275
00276 $fd_id=$this->cn->get_current_seq('s_fdef');
00277
00278 }
00279
00280
00281 $def_attr=$this->get_attr_min($p_FICHE_REF);
00282
00283
00284
00285 if (sizeof($def_attr) != 0 )
00286 {
00287
00288 $jnt_order=10;
00289 foreach ( $def_attr as $i=>$v)
00290 {
00291 $order=$jnt_order;
00292 if ( $v['ad_id'] == ATTR_DEF_NAME )
00293 $order=0;
00294 $sql=sprintf("insert into jnt_fic_Attr(fd_id,ad_id,jnt_order)
00295 values (%d,%s,%d)",
00296 $fd_id,$v['ad_id'],$order);
00297 $this->cn->exec_sql($sql);
00298 $jnt_order+=10;
00299 }
00300 }
00301 $this->id=$fd_id;
00302 return 0;
00303
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313 function get_by_type($step=0)
00314 {
00315 $sql="select f_id,ad_value
00316 from
00317 fiche join fiche_detail using(f_id)
00318 where ad_id=1 and fd_id=$1 order by 2";
00319
00320
00321 if ($step == 1 && $_SESSION['g_pagesize'] != -1 )
00322 {
00323 $offset=(isset($_GET['offset']))?$_GET['offset']:0;
00324 $step=$_SESSION['g_pagesize'];
00325 $sql.=" offset $offset limit $step";
00326 }
00327
00328 $Ret=$this->cn->get_array($sql,array($this->id));
00329
00330 return $Ret;
00331 }
00332
00333
00334
00335
00336 function get_by_category($p_cat)
00337 {
00338 $sql="select f_id,ad_value
00339 from
00340 fiche join fiche_def using(fd_id)
00341 join fiche_detail using(f_id)
00342 where ad_id=1 and frd_id=$1 order by 2 ";
00343
00344 $Ret=$this->cn->exec_sql($sql,array($p_cat));
00345 if ( ($Max=Database::num_row($Ret)) == 0 )
00346 return null;
00347 $all[0]=new Fiche($this->cn);
00348
00349 for ($i=0;$i<$Max;$i++)
00350 {
00351 $row=Database::fetch_array($Ret,$i);
00352 $t=new Fiche($this->cn,$row['f_id']);
00353 $t->getAttribut();
00354 $all[$i]=$t;
00355
00356 }
00357 return $all;
00358 }
00359
00360
00361
00362 function myList()
00363 {
00364 $this->Get();
00365 echo '<H2 class="info">'.$this->id." ".$this->label.'</H2>';
00366
00367 $step=$_SESSION['g_pagesize'];
00368 $sql_limit="";
00369 $sql_offset="";
00370 $bar="";
00371 if ( $step != -1 )
00372 {
00373
00374 $page=(isset($_GET['page']))?$_GET['page']:1;
00375 $offset=(isset($_GET['offset']))?$_GET['offset']:0;
00376 $max_line=$this->cn->count_sql("select f_id,ad_value from
00377 fiche join fiche_detail using (f_id)
00378 where fd_id='".$this->id."' and ad_id=".ATTR_DEF_NAME." order by f_id");
00379 $sql_limit=" limit ".$step;
00380 $sql_offset=" offset ".$offset;
00381 $bar=navigation_bar($offset,$max_line,$step,$page);
00382 }
00383
00384
00385
00386 $Res=$this->cn->exec_sql("select f_id,vw_name,quick_code from ".
00387 " vw_fiche_attr ".
00388 " where fd_id='".$this->id.
00389 "' order by f_id $sql_offset $sql_limit ");
00390 $Max=Database::num_row($Res);
00391 echo $bar;
00392 $str="";
00393
00394
00395 if ( $_SESSION['g_pagesize'] != -1)
00396 {
00397 $str=sprintf("&offset=%s&step=%s&page=%s&size=%s",
00398 $offset,
00399 $step,
00400 $page,
00401 $max_line);
00402 }
00403
00404
00405 echo '<FORM METHOD="POST" action="?p_action=fiche&action=vue'.$str.'">';
00406 echo HtmlInput::hidden('ac',$_REQUEST['ac']);
00407 echo dossier::hidden();
00408 echo HtmlInput::hidden("fiche",$this->id);
00409 echo HtmlInput::submit('add','Ajout fiche');
00410 echo '</FORM>';
00411 $str_dossier=dossier::get();
00412 echo '<table>';
00413 for ( $i = 0; $i < $Max; $i++)
00414 {
00415 $l_line=Database::fetch_array($Res,$i);
00416 if ( $i%2 == 0)
00417 echo '<TR class="odd">';
00418 else
00419 echo '<TR class="even">';
00420
00421 $span_mod='<TD><A href="?p_action=fiche&'.$str_dossier.
00422 '&action=detail&fiche_id='.$l_line['f_id'].$str.'&fiche='.
00423 $_REQUEST['fiche'].'&ac='.$_REQUEST['ac'].'">'.$l_line['quick_code']
00424 .'</A></TD>';
00425
00426 echo $span_mod.'<TD>'.h($l_line['vw_name'])."</TD>";
00427 echo '</tr>';
00428 }
00429 echo '</table>';
00430 echo '<FORM METHOD="POST" action="?p_action=fiche&action=vue'.$str.'">';
00431 echo HtmlInput::hidden('ac',$_REQUEST['ac']);
00432 echo dossier::hidden();
00433 echo HtmlInput::hidden("fiche",$this->id);
00434 echo HtmlInput::submit('add','Ajout fiche');
00435 echo '</FORM>';
00436 echo $bar;
00437
00438 }
00439
00440
00441
00442
00443
00444 function input_base()
00445 {
00446 $r="";
00447 $r.=_('Label');
00448 $label=new IText('label',$this->label);
00449 $r.=$label->input();
00450 $r.='<br>';
00451
00452 $class_base=new IPoste('class_base',$this->class_base);
00453 $class_base->set_attribute('ipopup','ipop_account');
00454 $class_base->set_attribute('account','class_base');
00455 $class_base->set_attribute('label','acc_label');
00456 $r.=_('Poste Comptable de base').' : ';
00457 $r.=$class_base->input();
00458 $r.='<span id="acc_label"></span><br>';
00459
00460 $ck=new ICheckBox('create');
00461 $ck->selected=($this->create_account=='f')?false:true;
00462 $r.=_('Chaque fiche aura automatiquement son propre poste comptable : ');
00463 $r.=$ck->input();
00464 return $r;
00465 }
00466
00467
00468
00469 function DisplayAttribut($str="")
00470 {
00471 if ( $this->id == 0 )
00472 return ;
00473 $this->cn->exec_sql('select fiche_attribut_synchro($1)',array($this->id));
00474
00475 $MaxLine=sizeof($this->attribut);
00476 $r="<TABLE>";
00477 $r.="<tr>".th('Nom attribut').th('').th('Ordre','style="text-align:right"').'</tr>';
00478
00479 $add_action="";
00480 for ($i=0;$i<$MaxLine;$i++)
00481 {
00482 $class="even";
00483 if ( $i % 2 == 0 )
00484 $class="odd";
00485
00486 $r.='<TR class="'.$class.'"><td>';
00487
00488 if ( $this->attribut[$i]->ad_id == ATTR_DEF_NAME )
00489 {
00490 continue;
00491 }
00492 else
00493 {
00494 if ( $str == "remove" )
00495 {
00496
00497 if ( $this->cn->count_sql("select * from attr_min where frd_id=".
00498 $this->fiche_def." and ad_id = ".$this->attribut[$i]->ad_id) == 0
00499 && $this->attribut[$i]->ad_id != ATTR_DEF_QUICKCODE
00500 && $this->attribut[$i]->ad_id != ATTR_DEF_ACCOUNT
00501 )
00502 {
00503 $add_action=sprintf( '</TD><TD> Supprimer <input type="checkbox" name="chk_remove[]" value="%d">',
00504 $this->attribut[$i]->ad_id);
00505 }
00506 else
00507 $add_action="</td><td>";
00508 }
00509
00510 $a=sprintf('%s ', $this->attribut[$i]->ad_text);
00511 $r.=$a.$add_action;
00512
00513
00514
00515 $order=new IText();
00516 $order->name='jnt_order'.$this->attribut[$i]->ad_id;
00517 $order->size=3;
00518 $order->value=$this->attribut[$i]->jnt_order;
00519 $r.='</td><td> '.$order->input();
00520 }
00521 $r.= '</td></tr>';
00522 }
00523
00524
00525
00526 $Res=$this->cn->exec_sql("select ad_id,ad_text from attr_def
00527 where
00528 ad_id not in (select ad_id from fiche_def natural join jnt_fic_attr
00529 where fd_id=$1) order by ad_text",array($this->id) );
00530 $M=Database::num_row($Res);
00531
00532
00533 $r.='<TR> <TD>';
00534 $r.= '<SELECT NAME="ad_id">';
00535 for ($i=0;$i<$M;$i++)
00536 {
00537 $l=Database::fetch_array($Res,$i);
00538 $a=sprintf('<OPTION VALUE="%s"> %s',
00539 $l['ad_id'],$l['ad_text']);
00540 $r.=$a;
00541 }
00542 $r.='</SELECT>';
00543
00544 $r.="</TABLE>";
00545 return $r;
00546 }
00547
00548
00549
00550 function SaveLabel($p_label)
00551 {
00552 if ( $this->id == 0 ) return;
00553 $p_label=sql_string($p_label);
00554 if (strlen(trim ($p_label)) == 0 )
00555 {
00556 return;
00557 }
00558 $sql=sprintf("update fiche_def set fd_label='%s' ".
00559 "where fd_id=%d",
00560 $p_label,$this->id);
00561 $Res=$this->cn->exec_sql($sql);
00562
00563 }
00564
00565
00566
00567
00568 function set_autocreate($p_label)
00569 {
00570 if ( $this->id == 0 ) return;
00571 if ($p_label==true)
00572 $t='t';
00573 if ($p_label==false)
00574 $t='f';
00575
00576 $sql="update fiche_def set fd_create_account=$1 ".
00577 "where fd_id=$2";
00578
00579 $Res=$this->cn->exec_sql($sql,array($t,$this->id));
00580
00581 }
00582
00583
00584
00585 function save_class_base($p_label)
00586 {
00587 if ( $this->id == 0 ) return;
00588 $p_label=sql_string($p_label);
00589
00590 $sql="update fiche_def set fd_class_base=$1 ".
00591 "where fd_id=$2";
00592
00593 $Res=$this->cn->exec_sql($sql,array($p_label,$this->id));
00594 }
00595
00596
00597
00598
00599
00600 function InsertAttribut($p_ad_id)
00601 {
00602 if ( $this->id == 0 ) return;
00603
00604 $this->GetAttribut();
00605 $max=sizeof($this->attribut)*15;
00606
00607
00608 $sql=sprintf("insert into jnt_fic_attr (fd_id,ad_id,jnt_order) values (%d,%d,%d)",
00609 $this->id,$p_ad_id,$max);
00610 $Res=$this->cn->exec_sql($sql);
00611
00612
00613 }
00614
00615
00616
00617
00618 function RemoveAttribut($array)
00619 {
00620 foreach ($array as $ch)
00621 {
00622 $this->cn->start();
00623 $sql="delete from jnt_fic_attr where fd_id=$1 ".
00624 " and ad_id=$2";
00625 $this->cn->exec_sql($sql,array($this->id,$ch));
00626
00627 $sql="delete from fiche_detail where jft_id in ( select ".
00628 " jft_id from fiche_Detail ".
00629 " join fiche using(f_id) ".
00630 " where ".
00631 "fd_id = $1 and ".
00632 "ad_id=$2)";
00633 $this->cn->exec_sql($sql,array($this->id,$ch));
00634
00635 $this->cn->commit();
00636 }
00637 }
00638
00639
00640
00641
00642 function save_order($p_array)
00643 {
00644 extract($p_array);
00645 $this->GetAttribut();
00646 foreach ($this->attribut as $row)
00647 {
00648 if ( $row->ad_id == 1 ) continue;
00649 if ( ${'jnt_order'.$row->ad_id} <= 0 ) continue;
00650 $sql='update jnt_fic_attr set jnt_order=$1 where fd_id=$2 and ad_id=$3';
00651 $this->cn->exec_sql($sql,array(${'jnt_order'.$row->ad_id},
00652 $this->id,
00653 $row->ad_id));
00654
00655 }
00656
00657 $this->cn->exec_sql('select attribute_correct_order()');
00658 }
00659
00660
00661
00662
00663
00664
00665
00666 function remove()
00667 {
00668 $remain=0;
00669
00670 $aFiche=fiche::get_fiche_def($this->cn,$this->id);
00671 if ( $aFiche != null )
00672 {
00673
00674 foreach ($aFiche as $dfiche)
00675 {
00676 $fiche=new Fiche($this->cn,$dfiche['f_id']);
00677
00678
00679 if ( $fiche->is_used() == false )
00680 {
00681 $fiche->delete();
00682 }
00683 else
00684 $remain++;
00685 }
00686 }
00687
00688 if ( $remain == 0 )
00689 {
00690 $sql='delete from jnt_fic_attr where fd_id=$1';
00691 $this->cn->exec_sql($sql,array($this->id));
00692 $sql='delete from fiche_def where fd_id=$1';
00693 $this->cn->exec_sql($sql,array($this->id));
00694 }
00695
00696 return $remain;
00697
00698 }
00699
00700
00701
00702
00703
00704
00705 function get_attr_min($p_fiche_def_ref)
00706 {
00707
00708
00709 $Sql="select ad_id,ad_text from attr_min natural join attr_def
00710 natural join fiche_def_ref
00711 where
00712 frd_id= $1";
00713 $Res=$this->cn->exec_sql($Sql,array($p_fiche_def_ref));
00714 $Num=Database::num_row($Res);
00715
00716
00717 if ($Num == 0 ) return null;
00718
00719
00720 for ($i=0;$i<$Num;$i++)
00721 {
00722 $f=Database::fetch_array($Res,$i);
00723 $array[$i]['ad_id']=$f['ad_id'];
00724 $array[$i]['ad_text']=$f['ad_text'];
00725 }
00726 return $array;
00727 }
00728
00729
00730
00731
00732
00733 function count_category($p_frd_id)
00734 {
00735 $ret=$this->cn->count_sql("select fd_id from fiche_def where frd_id=$1",array($p_frd_id));
00736 return $ret;
00737 }
00738 function input_detail()
00739 {
00740 $r = "";
00741
00742
00743 $this->Get();
00744 $this->GetAttribut();
00745 $r.= '<H2 class="info">' . $this->id . " " . h($this->label) . '</H2>';
00746 $r.='<fieldset><legend>Données générales</legend>';
00747
00748
00749 $r.='<form method="post">';
00750 $r.=dossier::hidden();
00751 $r.=HtmlInput::hidden("fd_id", $this->id);
00752 $r.=HtmlInput::hidden("p_action", "fiche");
00753 $r.= $this->input_base();
00754 $r.='<hr>';
00755 $r.=HtmlInput::submit('change_name', _('Sauver'));
00756 $r.='</form>';
00757 $r.='</fieldset>';
00758
00759 $r.='<fieldset><legend>Détails</legend>';
00760
00761 $r.= '<FORM method="POST">';
00762 $r.=dossier::hidden();
00763 $r.=HtmlInput::hidden("fd_id", $this->id);
00764 $r.= $this->DisplayAttribut("remove");
00765 $r.= HtmlInput::submit('add_line', _('Ajoutez cet élément'));
00766 $r.= HtmlInput::submit("save_line", _("Sauvez"));
00767 $r.=HtmlInput::submit('remove_cat', _('Effacer cette catégorie'), 'onclick="return confirm(\'' . _('Vous confirmez ?') . '\')"');
00768
00769 if (strpos($r, "chk_remove") != 0)
00770 {
00771 $r.=HtmlInput::submit('remove_line', _("Enleve les éléments cochés"), "onclick=\"return confirm('Vous confirmez?')\"");
00772 }
00773 $r.= "</form>";
00774 $r.=" <p class=\"notice\"> " . _("Attention : il n'y aura pas de demande de confirmation pour enlever les
00775 attributs sélectionnés. Il ne sera pas possible de revenir en arrière") . "</p>";
00776 $r.='</fieldset>';
00777 return $r;
00778 }
00779 function input_new()
00780 {
00781 $single=new Tool_Uos("dup");
00782 echo '<form method="post" style="display:inline">';
00783 echo $single->hidden();
00784 echo HtmlInput::hidden("p_action","fiche");
00785 echo dossier::hidden();
00786 echo $this->input();
00787 echo HtmlInput::submit("add_modele" ,"Sauve");
00788 echo '</FORM>';
00789 }
00790
00791 }
00792 ?>