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