cn=$p_cn;
}
function set_jrn($p_jrn) {
$this->jrn_def_id=$p_jrn;
}
function set_periode($pp_id){
$this->p_id=$pp_id;
}
/*!\brief return the p_id of the start and the end of the exercice
*into an array
*\param $p_exercice
*\return array [start]=>,[end]=>
*/
function limit_year($p_exercice) {
$sql_start="select p_id from parm_periode where p_exercice=$1 order by p_start ASC";
$start=getDbValue($this->cn,$sql_start,array($p_exercice));
$sql_end="select p_id from parm_periode where p_exercice=$1 order by p_end DESC";
$end=getDbValue($this->cn,$sql_end,array($p_exercice));
return array("start"=>$start,"end"=>$end);
}
function is_closed() {
if ( $this->jrn_def_id != 0 )
$sql="select status from jrn_periode ".
" where jrn_def_id=".$this->jrn_def_id.
" and p_id =".$this->p_id;
else
$sql="select p_closed as status from parm_periode ".
" where ".
" p_id =".$this->p_id;
$res=ExecSql($this->cn,$sql);
$status=pg_fetch_result($res,0,0);
echo_debug(__FILE__.':'.__LINE__.'- is_closed','return ',$status);
if ( $status == 'CL' || $status=='t' ||$status=='CE')
return 1;
return 0;
}
function is_open() {
/* if jrn_Def_id == 0 then we check the global otherwise we check
a ledger */
if ( $this->jrn_def_id != 0 )
$sql="select status from jrn_periode ".
" where jrn_def_id=".$this->jrn_def_id.
" and p_id =".$this->p_id;
else
$sql="select p_closed as status from parm_periode ".
" where ".
" p_id =".$this->p_id;
$res=ExecSql($this->cn,$sql);
$status=pg_fetch_result($res,0,0);
if ( $status == 'OP' || $status=='f' )
return 1;
return 0;
}
function is_centralized() {
if ( $this->jrn_def_id != 0 )
$sql="select status from jrn_periode ".
" where jrn_def_id=".$this->jrn_def_id.
" and p_id =".$this->p_id;
else
$sql="select p_centralized as status from parm_periode ".
" where ".
" p_id =".$this->p_id;
$res=ExecSql($this->cn,$sql);
$status=pg_fetch_result($res,0,0);
if ( $status == 'CE' || $status=='t' )
return 1;
return 0;
}
function close() {
if ( $this->jrn_def_id == 0 ) {
ExecSql($this->cn,"update parm_periode set p_closed=true where p_id=".
$this->p_id);
ExecSql($this->cn,"update jrn_periode set status='CL' ".
" where p_id = ".$this->p_id);
return;
}else {
ExecSql($this->cn,"update jrn_periode set status='CL' ".
" where jrn_def_id=".$this->jrn_def_id." and ".
" p_id = ".$this->p_id);
/* if all ledgers have this periode closed then synchro with
the table parm_periode
*/
$nJrn=CountSql( $this->cn,"select * from jrn_periode where ".
" p_id=".$this->p_id);
$nJrnPeriode=CountSql( $this->cn,"select * from jrn_periode where ".
" p_id=".$this->p_id." and status='CL'");
if ( $nJrnPeriode==$nJrn)
ExecSql($this->cn,"update parm_periode set p_closed=true where p_id=".$this->p_id);
return;
}
}
function centralized() {
if ( $this->jrn_def_id == 0 ) {
ExecSql($this->cn,"update parm_periode set p_central=true");
return;
}else {
ExecSql($this->cn,"update jrn_periode set status='CE' ".
" where ".
" p_id = ".$this->p_id);
return;
}
}
/*!
* \brief Display all the periode and their status
*
*/
function display_form_periode() {
$str_dossier=dossier::get();
if ( $this->jrn_def_id==0 ) {
$Res=ExecSql($this->cn,"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
from parm_periode order by p_start,p_end");
$Max=pg_NumRows($Res);
echo '
';
echo "";
echo '| Date début | ';
echo ' Date fin | ';
echo ' Exercice | ';
echo "";
for ($i=0;$i<$Max;$i++) {
$l_line=pg_fetch_array($Res,$i);
echo '';
echo '| '.$l_line['date_start'].' | ';
echo ' '.$l_line['date_end'].' | ';
echo ' '.$l_line['p_exercice'].' | ';
if ( $l_line['p_closed'] == 't' ) {
$closed=($l_line['p_central']=='t')?'Centralisée | ':'Fermée | ';
$change=' | ';
$remove=' | ';
} else {
$closed='';
$closed.=' Cloturer';
$change=' | ';
$change.=' Changer";
$remove=' | ';
$remove.=' Efface";
}
echo "$closed";
echo $change;
echo $remove;
echo ' |
';
}
echo '
';
echo '
';
} else {
$Res=ExecSql($this->cn,"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
from parm_periode join jrn_periode using (p_id) where jrn_def_id=".$this->jrn_def_id."
order by p_start,p_end");
$Max=pg_NumRows($Res);
$r=ExecSql($this->cn,'select jrn_Def_name from jrn_Def where jrn_Def_id='.
$this->jrn_def_id);
$jrn_name=pg_fetch_result($r,0,0);
echo ' Journal '.$jrn_name.'
';
echo '';
echo "";
echo '| Date début | ';
echo ' Date fin | ';
echo ' Exercice | ';
echo "";
for ($i=0;$i<$Max;$i++) {
$l_line=pg_fetch_array($Res,$i);
echo '';
echo '| '.$l_line['date_start'].' | ';
echo ' '.$l_line['date_end'].' | ';
echo ' '.$l_line['p_exercice'].' | ';
if ( $l_line['status'] != 'OP' ) {
$closed=($l_line['status']=='CE')?'Centralisee | ':'Fermée | ';
} else {
$closed='';
$closed.=' Cloturer';
$closed.=' | ';
}
echo "$closed";
echo '
';
}
echo '
';
}
}
function insert($p_date_start,$p_date_end,$p_exercice) {
if (isDate($p_date_start) == null ||
isDate($p_date_end) == null ||
strlen (trim($p_exercice)) == 0 ||
(string) $p_exercice != (string)(int) $p_exercice)
{
return 1;
}
$p_id=NextSequence($this->cn,'s_periode');
$sql=sprintf(" insert into parm_periode(p_id,p_start,p_end,p_closed,p_exercice)".
"values (%d,to_date('%s','DD.MM.YYYY'),to_date('%s','DD.MM.YYYY')".
",'f','%s')",
$p_id,
$p_date_start,
$p_date_end,
$p_exercice);
try {
StartSql($this->cn);
$Res=ExecSql($this->cn,$sql);
$Res=ExecSql($this->cn,"insert into jrn_periode (jrn_def_id,p_id,status) ".
"select jrn_def_id,$p_id,'OP' from jrn_def");
Commit($this->cn);
} catch (Exception $e) {
Rollback($this->cn);
echo_debug(__FILE__.':'.__LINE__.'- Periode insert','Exception ',$e);
echo_debug(__FILE__.':'.__LINE__.'- Periode insert','Exception ',$e->getMessage());
return 1;
}
return 0;
}
/*!\brief load data from database
*/
function load() {
$row=get_array($this->cn,"select p_start,p_end,p_exercice,p_closed,p_central from parm_periode where p_id=$1",
array($this->p_id));
if ($row == null ) return;
$this->p_start=$row[0]['p_start'];
$this->p_end=$row[0]['p_end'];
$this->p_exercice=$row[0]['p_exercice'];
$this->p_closed=$row[0]['p_closed'];
$this->p_central=$row[0]['p_central'];
}
/*!\brief return the max and the min periode of the exercice given
*in parameter
*\param $p_exercice is the exercice
*\return an array of Periode object
*/
function get_limit($p_exercice) {
$max=getDbValue($this->cn,"select p_id from parm_periode where p_exercice=$1 order by p_start asc",array($p_exercice));
$min=getDbValue($this->cn,"select p_id from parm_periode where p_exercice=$1 order by p_start desc",array($p_exercice));
$rMax=new Periode($this->cn);
$rMax->p_id=$max;
$rMax->load();
$rMin=new Periode($this->cn);
$rMin->p_id=$min;
$rMin->load();
return array($rMax,$rMin);
}
/*!
* \brief Give the start & end date of a periode
*
* \return array containing the start date & the end date
*
*
*/
public function get_date_limit($p_periode) {
$sql="select to_char(p_start,'DD.MM.YYYY') as p_start,
to_char(p_end,'DD.MM.YYYY') as p_end
from parm_periode
where p_id=".$p_periode;
$Res=ExecSql($this->cn,$sql);
if ( pg_NumRows($Res) == 0) return null;
return pg_fetch_array($Res,0);
}
public function first_day() {
list($p_start,$p_end)=$this->get_date_limit($this->p_id);
return $p_start;
}
function get_exercice() {
$sql="select p_exercice from parm_periode where p_id=".$this->id;
$Res=ExecSql($this->cn,$sql);
if ( pg_NumRows($Res) == 0) return null;
return pg_fetch_result($Res,0,0);
}
static function test_me() {
$cn=DbConnect(dossier::id());
$obj=new Periode($cn);
$obj->set_jrn(1);
$obj->display_form_periode();
}
}