00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 require_once ('ac_common.php');
00030 require_once ('class_database.php');
00031 class Periode
00032 {
00033 var $cn;
00034 var $jrn_def_id;
00035 var $p_id;
00036 var $status;
00037
00038 var $p_start;
00039 var $p_end;
00040 function __construct($p_cn,$p_id=0)
00041 {
00042 $this->p_id=$p_id;
00043 $this->cn=$p_cn;
00044 }
00045 function set_jrn($p_jrn)
00046 {
00047 $this->jrn_def_id=$p_jrn;
00048 }
00049 function set_periode($pp_id)
00050 {
00051 $this->p_id=$pp_id;
00052 }
00053
00054
00055
00056
00057
00058 function limit_year($p_exercice)
00059 {
00060 $sql_start="select p_id from parm_periode where p_exercice=$1 order by p_start ASC limit 1";
00061 $start=$this->cn->get_value($sql_start,array($p_exercice));
00062 $sql_end="select p_id from parm_periode where p_exercice=$1 order by p_end DESC limit 1";
00063 $end=$this->cn->get_value($sql_end,array($p_exercice));
00064 return array("start"=>$start,"end"=>$end);
00065 }
00066
00067
00068
00069 function is_closed()
00070 {
00071 if ( $this->jrn_def_id != 0 )
00072 $sql="select status from jrn_periode ".
00073 " where jrn_def_id=".$this->jrn_def_id.
00074 " and p_id =".$this->p_id;
00075 else
00076 $sql="select p_closed as status from parm_periode ".
00077 " where ".
00078 " p_id =".$this->p_id;
00079 $res=$this->cn->exec_sql($sql);
00080 $status=Database::fetch_result($res,0,0);
00081 if ( $status == 'CL' || $status=='t' ||$status=='CE')
00082 return 1;
00083 return 0;
00084 }
00085 function is_open()
00086 {
00087
00088
00089 if ( $this->jrn_def_id != 0 )
00090 $sql="select status from jrn_periode ".
00091 " where jrn_def_id=".$this->jrn_def_id.
00092 " and p_id =".$this->p_id;
00093 else
00094 $sql="select p_closed as status from parm_periode ".
00095 " where ".
00096 " p_id =".$this->p_id;
00097 $res=$this->cn->exec_sql($sql);
00098 $status=Database::fetch_result($res,0,0);
00099 if ( $status == 'OP' || $status=='f' )
00100 return 1;
00101 return 0;
00102 }
00103 function is_centralized()
00104 {
00105 if ( $this->jrn_def_id != 0 )
00106 $sql="select status from jrn_periode ".
00107 " where jrn_def_id=".$this->jrn_def_id.
00108 " and p_id =".$this->p_id;
00109 else
00110 $sql="select p_centralized as status from parm_periode ".
00111 " where ".
00112 " p_id =".$this->p_id;
00113 $res=$this->cn->exec_sql($sql);
00114 $status=Database::fetch_result($res,0,0);
00115 if ( $status == 'CE' || $status=='t' )
00116 return 1;
00117 return 0;
00118 }
00119 function reopen()
00120 {
00121 if ( $this->jrn_def_id == 0 )
00122 {
00123 $this->cn->exec_sql("update parm_periode set p_closed='f',p_central='f' where p_id=$1",
00124 array($_GET['p_per']));
00125
00126 $this->cn->exec_sql("update jrn_periode set status='OP' ".
00127 " where p_id = ".$this->p_id);
00128
00129 return;
00130 }
00131 else
00132 {
00133 $this->cn->exec_sql("update jrn_periode set status='OP' ".
00134 " where jrn_def_id=".$this->jrn_def_id." and ".
00135 " p_id = ".$this->p_id);
00136
00137 $this->cn->exec_sql("update parm_periode set p_closed=false where p_id=".$this->p_id);
00138 return;
00139 }
00140
00141 }
00142
00143 function close()
00144 {
00145 if ( $this->jrn_def_id == 0 )
00146 {
00147 $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=".
00148 $this->p_id);
00149 $this->cn->exec_sql("update jrn_periode set status='CL' ".
00150 " where p_id = ".$this->p_id);
00151
00152 return;
00153 }
00154 else
00155 {
00156 $this->cn->exec_sql("update jrn_periode set status='CL' ".
00157 " where jrn_def_id=".$this->jrn_def_id." and ".
00158 " p_id = ".$this->p_id);
00159
00160
00161
00162 $nJrn=$this->cn->count_sql( "select * from jrn_periode where ".
00163 " p_id=".$this->p_id);
00164 $nJrnPeriode=$this->cn->count_sql( "select * from jrn_periode where ".
00165 " p_id=".$this->p_id." and status='CL'");
00166
00167 if ( $nJrnPeriode==$nJrn)
00168 $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=".$this->p_id);
00169 return;
00170 }
00171
00172 }
00173 function centralized()
00174 {
00175 if ( $this->jrn_def_id == 0 )
00176 {
00177 $this->cn->exec_sql("update parm_periode set p_central=true");
00178 return;
00179 }
00180 else
00181 {
00182 $this->cn->exec_sql("update jrn_periode set status='CE' ".
00183 " where ".
00184 " p_id = ".$this->p_id);
00185 return;
00186 }
00187
00188 }
00189
00190
00191
00192
00193
00194 function display_form_periode()
00195 {
00196 $str_dossier=dossier::get();
00197
00198 if ( $this->jrn_def_id==0 )
00199 {
00200 $Res=$this->cn->exec_sql("select p_id,to_char(p_start,'DD.MM.YYYY') as date_start,to_char(p_end,'DD.MM.YYYY') as date_end,p_central,p_closed,p_exercice,
00201 (select count(jr_id) as count_op from jrn where jr_tech_per = p_id) as count_op
00202 from parm_periode
00203 order by p_start,p_end");
00204 $Max=Database::num_row($Res);
00205 echo '<TABLE ALIGN="CENTER">';
00206 echo "</TR>";
00207 echo '<TH> Date début </TH>';
00208 echo '<TH> Date fin </TH>';
00209 echo '<TH> Exercice </TH>';
00210 echo "</TR>";
00211
00212 for ($i=0;$i<$Max;$i++)
00213 {
00214 $l_line=Database::fetch_array($Res,$i);
00215 $class="even";
00216 if ( $i % 2 == 0 )
00217 $class="odd";
00218 $style='';
00219 if ( $l_line['p_closed'] == 't')
00220 $style="color:red";
00221 echo '<TR class="'.$class.'" style="'.$style.'">';
00222 echo '<TD ALIGN="CENTER"> '.$l_line['date_start'].'</TD>';
00223 echo '<TD ALIGN="CENTER"> '.$l_line['date_end'].'</TD>';
00224 echo '<TD ALIGN="CENTER"> '.$l_line['p_exercice'].'</TD>';
00225
00226 if ( $l_line['p_closed'] == 't' )
00227 {
00228 $closed=($l_line['p_central']=='t')?'<TD>Centralisée</TD>':'<TD>Fermée</TD>';
00229 $change='<TD></TD>';
00230 $remove=sprintf(_('Nombre opérations %d'),$l_line['count_op']);
00231 $remove=td($remove,' class="mtitle" ');
00232 $change=td ('<A class="mtitle" HREF="?ac='.$_REQUEST['ac'].'&action=reopen&p_per='.$l_line['p_id'].'&'.$str_dossier.'" onclick="return confirm(\''._('Confirmez Réouverture').' ?\')"> Réouverture</A>',' class="mtitle"');
00233
00234 }
00235 else
00236 {
00237 $closed='<TD class="mtitle">';
00238 $closed.='<A class="mtitle" HREF="?ac='.$_REQUEST['ac'].'&action=closed&p_per='.$l_line['p_id'].'&'.$str_dossier.'" onclick="return confirm(\''._('Confirmez cloture').' ?\')"> Cloturer</A></td>';
00239
00240 if ($l_line['count_op'] == 0 )
00241 {
00242 $change=HtmlInput::display_periode($l_line['p_id']);
00243 }
00244 else
00245 {
00246 $change="Non modifiable";
00247 }
00248 $change=td($change,' class="mtitle" ');
00249
00250
00251
00252
00253
00254
00255 $reopen=td("");
00256
00257
00258 $remove='<TD class="mtitle">';
00259
00260
00261 if ($l_line['count_op'] == 0 )
00262 {
00263 $remove.='<A class="mtitle" HREF="?ac='.$_REQUEST['ac'].'&action=delete_per&p_per='.
00264 $l_line['p_id']."&$str_dossier\" onclick=\"return confirm('"._('Confirmez effacement ?')."')\" > Efface</A>";
00265 }
00266 else
00267 {
00268 $remove.=sprintf(_('Nombre opérations %d'),$l_line['count_op']);
00269 }
00270 $remove.='</td>';
00271 }
00272 echo "$closed";
00273 echo $change;
00274
00275 echo $remove;
00276
00277 echo '</TR>';
00278
00279 }
00280 echo '<TR> <FORM METHOD="POST">';
00281 echo dossier::hidden();
00282 $istart=new IDate('p_date_start');
00283 $iend=new IDate('p_date_end');
00284 $iexercice=new INum('p_exercice');
00285 $iexercice->size=5;
00286 echo td($istart->input());
00287 echo td($iend->input());
00288 echo td($iexercice->input());
00289
00290 echo '<TD> <INPUT TYPE="SUBMIT" NAME="add_per" Value="Ajout"</TD>';
00291 echo '<TD></TD>';
00292 echo '<TD></TD>';
00293 echo '</FORM></TR>';
00294
00295 echo '</TABLE>';
00296
00297 }
00298 else
00299 {
00300 $Res=$this->cn->exec_sql("select p_id,to_char(p_start,'DD.MM.YYYY') as date_start,to_char(p_end,'DD.MM.YYYY') as date_end,status,p_exercice
00301 from parm_periode join jrn_periode using (p_id) where jrn_def_id=".$this->jrn_def_id."
00302 order by p_start,p_end");
00303 $Max=Database::num_row($Res);
00304 $r=$this->cn->exec_sql('select jrn_Def_name from jrn_Def where jrn_Def_id='.
00305 $this->jrn_def_id);
00306 $jrn_name=Database::fetch_result($r,0,0);
00307 echo '<h2> Journal '.$jrn_name.'</h2>';
00308 echo '<TABLE ALIGN="CENTER">';
00309 echo "</TR>";
00310 echo '<TH> Date début </TH>';
00311 echo '<TH> Date fin </TH>';
00312 echo '<TH> Exercice </TH>';
00313 echo "</TR>";
00314
00315 for ($i=0;$i<$Max;$i++)
00316 {
00317 $l_line=Database::fetch_array($Res,$i);
00318 if ( $l_line['status'] != 'OP' )
00319 echo '<TR style="COLOR:RED">';
00320 else
00321 echo '<TR>';
00322 echo '<TD ALIGN="CENTER"> '.$l_line['date_start'].'</TD>';
00323 echo '<TD ALIGN="CENTER"> '.$l_line['date_end'].'</TD>';
00324 echo '<TD ALIGN="CENTER"> '.$l_line['p_exercice'].'</TD>';
00325
00326 if ( $l_line['status'] != 'OP' )
00327 {
00328 $closed=td ('<A class="mtitle" HREF="?ac='.$_REQUEST['ac'].'&action=reopen&p_per='.$l_line['p_id'].'&'.$str_dossier.'&jrn_def_id='.$this->jrn_def_id.'" onclick="return confirm(\''._('Confirmez Réouverture').' ?\')"> Réouverture</A>',' class="mtitle"');
00329
00330 }
00331 else
00332 {
00333 $closed='<TD class="mtitle">';
00334 $closed.='<A class="mtitle" HREF="?ac='.$_REQUEST['ac'].'&action=closed&p_per='.$l_line['p_id'].'&'.$str_dossier.'&jrn_def_id='.$this->jrn_def_id.'" onclick="return confirm(\''._('Confirmez Cloture').' ?\')"> Cloturer</A>';
00335 $closed.='</td>';
00336 }
00337 echo "$closed";
00338
00339 echo '</TR>';
00340
00341 }
00342 echo '</TABLE>';
00343
00344 }
00345 }
00346 function insert($p_date_start,$p_date_end,$p_exercice)
00347 {
00348 try
00349 {
00350
00351 if (isDate($p_date_start) == null ||
00352 isDate($p_date_end) == null ||
00353 strlen (trim($p_exercice)) == 0 ||
00354 (string) $p_exercice != (string)(int) $p_exercice
00355 ||$p_exercice < COMPTA_MIN_YEAR || $p_exercice > COMPTA_MAX_YEAR)
00356
00357 {
00358 throw new Exception ("Paramètre invalide");
00359 }
00360 $p_id=$this->cn->get_next_seq('s_periode');
00361 $sql=sprintf(" insert into parm_periode(p_id,p_start,p_end,p_closed,p_exercice)".
00362 "values (%d,to_date('%s','DD.MM.YYYY'),to_date('%s','DD.MM.YYYY')".
00363 ",'f','%s')",
00364 $p_id,
00365 $p_date_start,
00366 $p_date_end,
00367 $p_exercice);
00368 $this->cn->start();
00369 $Res=$this->cn->exec_sql($sql);
00370 $Res=$this->cn->exec_sql("insert into jrn_periode (jrn_def_id,p_id,status) ".
00371 "select jrn_def_id,$p_id,'OP' from jrn_def");
00372 $this->cn->commit();
00373 }
00374 catch (Exception $e)
00375 {
00376 $this->cn->rollback();
00377 return 1;
00378 }
00379 return 0;
00380 }
00381
00382
00383
00384 function load()
00385 {
00386
00387 $row=$this->cn->get_array("select p_start,p_end,p_exercice,p_closed,p_central from parm_periode where p_id=$1",
00388 array($this->p_id));
00389 if ($row == null ) return -1;
00390
00391 $this->p_start=$row[0]['p_start'];
00392 $this->p_end=$row[0]['p_end'];
00393 $this->p_exercice=$row[0]['p_exercice'];
00394 $this->p_closed=$row[0]['p_closed'];
00395 $this->p_central=$row[0]['p_central'];
00396 return 0;
00397 }
00398
00399
00400
00401
00402
00403
00404 function get_limit($p_exercice)
00405 {
00406
00407 $max=$this->cn->get_value("select p_id from parm_periode where p_exercice=$1 order by p_start asc limit 1",array($p_exercice));
00408 $min=$this->cn->get_value("select p_id from parm_periode where p_exercice=$1 order by p_start desc limit 1",array($p_exercice));
00409 $rMax=new Periode($this->cn);
00410 $rMax->p_id=$max;
00411 if ( $rMax->load() ) throw new Exception('Periode n\'existe pas');
00412 $rMin=new Periode($this->cn);
00413 $rMin->p_id=$min;
00414 if ( $rMin->load() ) throw new Exception('Periode n\'existe pas');
00415 return array($rMax,$rMin);
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 public function get_date_limit($p_periode = 0)
00428 {
00429 if ( $p_periode == 0 ) $p_periode=$this->p_id;
00430 $sql="select to_char(p_start,'DD.MM.YYYY') as p_start,
00431 to_char(p_end,'DD.MM.YYYY') as p_end
00432 from parm_periode
00433 where p_id=$1";
00434 $Res=$this->cn->exec_sql($sql,array($p_periode));
00435 if ( Database::num_row($Res) == 0) return null;
00436 return Database::fetch_array($Res,0);
00437
00438 }
00439
00440
00441
00442
00443 public function first_day($p=0)
00444 {
00445 if ($p==0) $p=$this->p_id;
00446 list($p_start,$p_end)=$this->get_date_limit($p);
00447 return $p_start;
00448 }
00449
00450
00451
00452
00453 public function last_day($p=0)
00454 {
00455 if ($p==0) $p=$this->p_id;
00456 list($p_start,$p_end)=$this->get_date_limit($p);
00457 return $p_end;
00458 }
00459
00460 function get_exercice($p_id=0)
00461 {
00462 if ( $p_id == 0 ) $p_id=$this->p_id;
00463 $sql="select p_exercice from parm_periode where p_id=".$p_id;
00464 $Res=$this->cn->exec_sql($sql);
00465 if ( Database::num_row($Res) == 0) return null;
00466 return Database::fetch_result($Res,0,0);
00467
00468 }
00469
00470
00471
00472
00473
00474 function find_periode($p_date)
00475 {
00476 $sql="select p_id from parm_periode where p_start <= to_date($1,'DD.MM.YYYY') and p_end >= to_date($1,'DD.MM.YYYY') ";
00477 $ret=$this->cn->exec_sql($sql,array($p_date));
00478 $nb_periode=Database::num_row($ret);
00479 if ( $nb_periode == 0 )
00480 throw (new Exception('Aucune période trouvée',101));
00481 if ( $nb_periode > 1 )
00482 throw (new Exception("Trop de périodes trouvées $nb_periode pour $p_date",100));
00483 $per=Database::fetch_result($ret,0);
00484 $this->p_id=$per;
00485 return $per;
00486 }
00487
00488
00489
00490 function insert_exercice($p_exercice,$nb_periode)
00491 {
00492 try
00493 {
00494 if ( $nb_periode != 12 && $nb_periode != 13) throw new Exception ('Nombre de période incorrectes');
00495 $this->cn->start();
00496 for ($i=1;$i < 12;$i++)
00497 {
00498 $date_start=sprintf('01.%02d.%d',$i,$p_exercice);
00499 $date_end=$this->cn->get_value("select to_char(to_date('$date_start','DD.MM.YYYY')+interval '1 month'-interval '1 day','DD.MM.YYYY')");
00500 if ( $this->insert($date_start,$date_end,$p_exercice) != 0)
00501 {
00502 throw new Exception('Erreur insertion période');
00503 }
00504 }
00505 if ( $nb_periode==12 && $this->insert('01.12.'.$p_exercice,'31.12.'.$p_exercice,$p_exercice) != 0 )
00506 {
00507 throw new Exception('Erreur insertion période');
00508 }
00509 if ( $nb_periode==13)
00510 {
00511 if ($this->insert('01.12.'.$p_exercice,'30.12.'.$p_exercice,$p_exercice) != 0 ) throw new Exception('Erreur insertion période');
00512 if ($this->insert('31.12.'.$p_exercice,'31.12.'.$p_exercice,$p_exercice) != 0 ) throw new Exception('Erreur insertion période');
00513 }
00514
00515
00516 $this->cn->commit();
00517 }
00518 catch (Exception $e)
00519 {
00520 $this->cn->rollback();
00521 }
00522 }
00523 static function test_me()
00524 {
00525 $cn=new Database(dossier::id());
00526 $obj=new Periode($cn);
00527 $obj->set_jrn(1);
00528 $obj->display_form_periode();
00529 }
00530 }