diff --git a/html/ajax_misc.php b/html/ajax_misc.php index 1eb27c9b1..438ce8a2d 100644 --- a/html/ajax_misc.php +++ b/html/ajax_misc.php @@ -122,8 +122,8 @@ $path = array( "todo_list"=>"ajax_todo_list", "history"=>"ajax_history", "mod_doc"=>"ajax_mod_document", - "input_per"=>"ajax_mod_periode", - "save_per"=>"ajax_mod_periode", + // Periode menu: PERIODE + 'periode'=>"ajax_periode", "mod_predf"=>"ajax_mod_predf_op", "save_predf"=>"ajax_save_predf_op", "search_action"=>"ajax_search_action", diff --git a/html/js/scripts.js b/html/js/scripts.js index 3f087243e..766792083 100644 --- a/html/js/scripts.js +++ b/html/js/scripts.js @@ -3032,4 +3032,339 @@ function show_all_row(p_table_id) mRow.show(); } -} \ No newline at end of file +} +/** + * @class + * Periode handling + * Variables : + * - id of the row of the periode row_per_(p_periode_id) , attribute exercice =per_exercice,periode_id=p_id + * - (this.dialog) + * - id of the table with the rows : periode_tbl + * + * Members : + * - periode_id the concerned Periode , 0 none + * - p_ledger : the id of ledger (jrn_def.jrn_def_id), 0 for global + * - pcallback : default ajax_misc.php (this.callback) with the parameter { op:'periode',gDossier,[action:display,remove,save],p_id:p_periode_id} + * - dossier + * - js_obj_name : name of the js object (this.js_obj_name) + * - ajax_test : file to include for debugging + * - dialog : id of the dialog box (update / add ) periode_box + * + */ +var Periode=function (p_ledger) { + this.periode_id=0; + this.p_ledger=p_ledger; + this.dialog='periode_box'; + this.pcallback='ajax_misc.php'; + this.dossier=0; + this.js_obj_name=""; + this.ajax_test=""; + this.set_callback=function (p_phpfile) { this.pcallback=p_phpfile;}; + this.set_dossier=function (p_dosid) { this.dossier=p_dosid;}; + /** + * set_js_obj_name (p_js_obj_name) + * We need to know the javascript variable name , to pass it to ajax and + * create a HTML containing the right variable + * @param p_js_obj_name name of the variable js we use on caller side + */ + this.set_js_obj_name=function (p_js_obj_name) { this.js_obj_name=p_js_obj_name;}; + + /** + * Remove the periode , so call new Ajax and hide the row if successful + * otherwise show dialog box. + * @parameter p_periode_id is the id of periode + */ + this.remove=function(p_periode_id) { + + var js_param={"gDossier":this.dossier, + "op":"periode", + "act":"remove", + "p_id":p_periode_id, + "ledger_id":0, + "js_var":this.js_obj_name}; + if ( this.ajax_test !="") { + js_param["TestAjaxFile"]=this.ajax_test; + } + here=this; + smoke.confirm("Confirmer ?",function(e) { + if (e ) { + waiting_box(); + new Ajax.Request(here.pcallback, + { + method:"POST", + parameters:js_param, + onSuccess:function(req) { + var answer=req.responseText.evalJSON(); + remove_waiting_box(); + if ( answer.status=="OK" ) + { + $("row_per_"+p_periode_id).remove(); + alternate_row_color("periode_tbl"); + } else { + smoke.alert(answer.content); + } + } + }); + } + }); + }; + + /** + * display a dialog box to update a periode, call save either display + * an error box or update the row. + * the name of variable is requested + * to build the right button , javascript in the html of answer + * @parameter p_periode_id is the id of periode + */ + this.box_display=function(p_periode_id) { + if ( this.js_obj_name == "") { + smoke.alert("ERROR BOX_ADD") + } + + var js_param={"gDossier":this.dossier, + "op":"periode", + "act":"show", + "p_id":p_periode_id, + "ledger_id":this.p_ledger, + "js_var":this.js_obj_name}; + if ( this.ajax_test !="") { + js_param["TestAjaxFile"]=this.ajax_test; + } + var here=this; + new Ajax.Request(here.pcallback, + { + method:"POST", + parameters:js_param, + onSuccess:function(req) { + remove_waiting_box(); + var json=req.responseText.evalJSON(); + var y=calcy(100); + add_div({"id":"mod_periode","style":"position:fixed;top:"+y+"px;width:50%","cssclass":"inner_box",'html':"wait"}); + $('mod_periode').update(json.content); + } + }); + }; + /** + * close the periode, call ajax and receive a json object with the attribute + * status, content + * @parameter p_periode_id is the id of periode + */ + this.close_periode=function(p_periode_id) { + if ( this.js_obj_name == "") { + smoke.alert("ERROR BOX_ADD") + } + + if ( this.ajax_test !="") { + js_param["TestAjaxFile"]=this.ajax_test; + } + var here=this; + smoke.confirm("Confirmer ?",function(e) { + if (e ) { + here._close(p_periode_id); + } + }); + }; + /** + * Internal function to close without confirming + * @param {type} p_periode_id + * @returns {undefined} + */ + this._close=function(p_periode_id) { + if ( this.js_obj_name == "") { + smoke.alert("ERROR BOX_ADD") + } + var js_param={"gDossier":this.dossier, + "op":"periode", + "act":"close", + "ledger_id":this.p_ledger, + "p_id":p_periode_id, + "js_var":this.js_obj_name + }; + if ( this.ajax_test !="") { + js_param["TestAjaxFile"]=this.ajax_test; + } + var here=this; + waiting_box(); + new Ajax.Request(here.pcallback, + { + method:"POST", + parameters:js_param, + onSuccess:function(req) { + remove_waiting_box(); + var json=req.responseText.evalJSON(); + if ( json.status == 'OK') + { + $('row_per_'+p_periode_id).update(json.content); + new Effect.Highlight('row_per_'+p_periode_id ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); + } else { + smoke.alert(json.content); + } + } + }); + }; + /** + * reopen the periode + * @parameter p_periode_id is the SQL id of parm_periode or the id of + * jrn_periode + */ + this.open_periode=function(p_periode_id) { + if ( this.js_obj_name == "") { + smoke.alert("ERROR BOX_ADD") + } + var js_param={"gDossier":this.dossier, + "op":"periode", + "act":"reopen", + "ledger_id":this.p_ledger, + "p_id":p_periode_id, + "js_var":this.js_obj_name + }; + if ( this.ajax_test !="") { + js_param["TestAjaxFile"]=this.ajax_test; + } + var here=this; + smoke.confirm("Confirmer ?",function(e) { + if (e ) { + waiting_box(); + new Ajax.Request(here.pcallback, + { + method:"POST", + parameters:js_param, + onSuccess:function(req) { + remove_waiting_box(); + var json=req.responseText.evalJSON(); + if ( json.status == 'OK') + { + $('row_per_'+p_periode_id).update(json.content); + new Effect.Highlight('row_per_'+p_periode_id ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); + } else { + smoke.alert(json.content); + } + } + }); + } + }); + }; + /** + * This DOMID of the DIV containing the form is mod_periode + * @param {type} p_frm + * @returns {Boolean} + */ + this.save=function(p_frm) { + var js_param=$(p_frm).serialize(true); + waiting_box(); + js_param["js_var"]=this.js_obj_name; + js_param["act"]="save"; + js_param["op"]="periode"; + var here=this; + new Ajax.Request(this.pcallback,{ + method:"POST", + parameters:js_param, + onSuccess:function (req) { + + var answer=req.responseText.evalJSON(); + remove_waiting_box(); + if ( answer.status == "OK") { + $('row_per_'+js_param['periode_id']).update(answer.content); + removeDiv('mod_periode'); + new Effect.Highlight('row_per_'+js_param['periode_id'] ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); + } else { + smoke.alert(answer.content); + } + } + }); + return false; + }; + /** + * Thanks the object DOMID sel_per_closed[] the selected periodes are + * closed + * @see Periode._close + */ + this.close_selected = function () { + var here = this; + var a_selected = document.getElementsByName('sel_per_close[]'); + var count=0; + var i = 0; + for (i = 0; i < a_selected.length; i++) { + if (a_selected[i].checked == true) { + // Close the selected periode + count++; + } + } + if ( count==0){ + smoke.signal("Sélectionner au moins une période",function(){},{duration:1500}); + return; + } + smoke.confirm("Confirmer fermeture de "+count+" periode", function (e) { + if (e) { + var a_selected = document.getElementsByName('sel_per_close[]'); + var i = 0; + for (i = 0; i < a_selected.length; i++) { + if (a_selected[i].checked == true) { + // Close the selected periode + here._close(a_selected[i].value); + } + } + } + } + ); + }; + /** + * @brief Insert a periode into the list, always at the bottom ! + * DomId : + * # FORM id :insert_periode_frm + * # DIV id = periode_add + * # table id = periode_tbl + */ + this.insert_periode=function() { + var p_frm='insert_periode_frm'; + var js_param=$(p_frm).serialize(true); + waiting_box(); + js_param["js_var"]=this.js_obj_name; + js_param["act"]="insert_periode"; + js_param["op"]="periode"; + js_param["p_id"]="-1"; + js_param["ledger_id"]="0"; + var here=this; + new Ajax.Request(this.pcallback,{ + method:"POST", + parameters:js_param, + onSuccess:function (req) { + var answer=req.responseText.evalJSON(); + remove_waiting_box(); + if ( answer.status == "OK") { + var new_row=document.createElement("tr"); + $('periode_tbl').append(new_row); + new_row.replace(answer.content); + + // hide the form + $('periode_add').hide(); + new Effect.Highlight('row_per_'+answer.p_id ,{startcolor: '#FAD4D4',endcolor: '#F78082' }); + alternate_row_color('periode_tbl'); + } else { + smoke.alert(answer.content); + } + } + }); + return false; +} + +} +/** + * Show the periodes from the exercice contained into the id (p_exercice_sel) + * @param p_table_id DOM ID of the table + */ +Periode.filter_exercice=function (p_table_id) { + var rows=$(p_table_id).rows; + var selected_value=$('p_exercice_sel').value; + for (var i=1;iload(); - $limit = $per->get_date_limit($_GET['p_id']); - - $p_start = new IDate('p_start'); - $p_start->value = $limit['p_start']; - $p_end = new IDate('p_end'); - $p_end->value = $limit['p_end']; - $p_exercice = new INum('p_exercice'); - $p_exercice->value = $per->p_exercice; - - $html = ''; - $html.=HtmlInput::anchor_close('mod_periode'); - $html.=h2info(_('Modification période')); - $html.='

'._('Modifier les dates de début et fin de période').'

'; - $html.='

'._('Cela pourrait avoir un impact sur les opérations déjà existantes').'

'; - $html.='
'; - $html.=dossier::hidden(); - $html.=''; - - $html.=tr(td(_(' Début période : ')) . td($p_start->input())); - $html.=tr(td(_(' Fin période : ')) . td($p_end->input())); - $html.=tr(td(_(' Exercice : ')) . td($p_exercice->input())); - $html.='
'; - $html.=HtmlInput::submit('sauver', _('sauver')); - $html.=HtmlInput::button('close', _('fermer'), 'onclick="removeDiv(\'mod_periode\')"'); - $html.=HtmlInput::hidden('p_id', $_GET['p_id']); - $html.='
'; - break; - case 'save_per': - $per = new Periode($cn, $_POST['p_id']); - $per->load(); - if (isDate($_POST['p_start']) == null || - isDate($_POST['p_end'] == null) || - isNumber($_POST['p_exercice']) == 0 || - $_POST['p_exercice'] > 2099 || - $_POST['p_exercice'] < 2000) - { - $html = ''; - $html.=HtmlInput::anchor_close('mod_periode'); - $html.='

'._('Modifier les dates de début et fin de période').'

'; - $html.="
"._('Erreur date invalide')."
"; - - $html.=HtmlInput::button('close', _('fermer'), 'onclick="removeDiv(\'mod_periode\')"'); - } - else - { - $sql = "update parm_periode set p_start=to_date($1,'DD.MM.YYYY'),p_end=to_date($2,'DD.MM.YYYY'),p_exercice=$3 where p_id=$4"; - try - { - $cn->exec_sql($sql, array($_POST['p_start'], $_POST['p_end'], $_POST['p_exercice'], $_POST['p_id'])); - $html = '

Modifier les dates de début et fin de période

'; - $html.='

Sauvé

'; - - $html.=HtmlInput::button('close', _('Fermer'), 'onclick=" refresh_window();"'); - } - catch (Exception $e) - { - record_log($e->getTraceAsString()); - $html = alert($e->getTrace(), true); - } - } - break; -} - -$html = escape_xml($html); -header('Content-type: text/xml; charset=UTF-8'); -echo ''; -echo ''; -echo '' . $html . ''; -echo ''; diff --git a/include/ajax/ajax_periode.php b/include/ajax/ajax_periode.php new file mode 100644 index 000000000..d21722543 --- /dev/null +++ b/include/ajax/ajax_periode.php @@ -0,0 +1,297 @@ + + +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); + +/** + * @file + * @brief display or save a periode variable received $op, $cn $g_user + * variable : + * act + * # close : close a periode + * # reopen : reopen a periode + * # show : display a form for modifying / adding a period + * # remove : delete a period IF not used + * + * ledger_id is the SQL id of ledger + * + * p_id is either the SQL id of parm_periode of jrn_periode, depending if + * ledger_id == 0 or not + * The answer must be in JSON + */ +require_once NOALYSS_INCLUDE.'/class/periode.class.php'; +require_once NOALYSS_INCLUDE.'/class/periode_ledger.class.php'; + +$err=0; +$a_answer=[]; +$a_answer['status']="NOK"; +$http=new HttpInput(); +try +{ +// action to perform + $act=$http->request("act"); +// Periode id + $periode_id=$http->request("p_id", "number"); +// Ledger id + $ledger_id=$http->request("ledger_id", "number"); +// Name of the javascript variable + $js_var=$http->request("js_var"); +} +catch (Exception $ex) +{ + $a_answer['content']=$ex->getMessage(); + $jsson=json_encode($a_answer, + JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); + header('Content-Type: application/json;charset=utf-8'); + echo $jsson; + return; +} +/* we check the security */ +if ($g_user->check_module("PERIODE")==0) +{ + return; +} + +switch ($act) +{ +// Reopen a periode for specific ledger if ledger_id != 0, or all +// the periodes if ledger_id=0 + case 'reopen': + try + { + if ($ledger_id==0) + { + $per=new Periode($cn, $periode_id); + $per->set_ledger(0); + $per->reopen(); + $parm_periode=new Parm_periode_SQL($cn, $periode_id); + ob_start(); + Periode::display_row_global($parm_periode, 0, $js_var); + $a_answer['content']=ob_get_clean(); + } + else + { + $id=$http->post("p_id", "number"); + $jrn_periode=new Jrn_periode_SQL($cn, $p_id); + $per_led=new Periode_Ledger($jrn_periode); + $per_led->reopen(); + ob_start(); + $per_led_table=new Periode_Ledger_Table($periode_id); + $per_led_table->display_row($per_led_table->get_a_member(), 0, + $js_var); + $a_answer['content']=ob_get_clean(); + } + $a_answer["status"]="OK"; + } + catch (Exception $ex) + { + $a_answer['content']=$ex->getMessage(); + } + break; +// Close a periode for specific ledger if ledger_id != 0, or all +// the periodes if ledger_id=0 + case 'close': + try + { + if ($ledger_id==0) + { + $per=new Periode($cn, $periode_id); + $per->close(); + $parm_periode=new Parm_periode_SQL($cn, $periode_id); + ob_start(); + Periode::display_row_global($parm_periode, 0, $js_var); + $a_answer['content']=ob_get_clean(); + } + else + { + $jrn_periode=new Jrn_periode_SQL($cn, $periode_id); + $per_led=new Periode_Ledger($jrn_periode); + $per_led->close(); + ob_start(); + $per_led_table=new Periode_Ledger_Table($periode_id); + $per_led_table->display_row($per_led_table->get_a_member(), 0, + $js_var); + $a_answer['content']=ob_get_clean(); + } + $a_answer["status"]="OK"; + } + catch (Exception $ex) + { + $a_answer['content']=$ex->getMessage(); + } + break; +// Add a new periode , only if ledger_id == 0 + case 'show': + $per=new Periode($cn, $periode_id); + $per->load(); + + $p_exercice=new INum('p_exercice'); + $limit=$per->get_date_limit($periode_id); + $p_exercice->value=$per->p_exercice; + $title=_('Modification période'); + $title_par="

"._('Modifier les dates de début et fin de période'). + "

"; + $title_par.='

'._('Cela pourrait avoir un impact sur les opérations déjà existantes').'

'; + + $p_start=new IDate('p_start'); + $p_start->value=$limit['p_start']; + $p_end=new IDate('p_end'); + $p_end->value=$limit['p_end']; + + $html=''; + $html.=HtmlInput::title_box($title, 'mod_periode'); + $html.=$title_par; + $html.=sprintf('
', + $js_var); ; + $html.=HtmlInput::hidden("js_var", $js_var); + $html.=HtmlInput::hidden("periode_id", $periode_id); + $html.=HtmlInput::hidden("ledger_id", $ledger_id); + $html.=dossier::hidden(); + $html.=''; + + $html.=tr(td(_(' Début période : ')).td($p_start->input())); + $html.=tr(td(_(' Fin période : ')).td($p_end->input())); + $html.=tr(td(_(' Exercice : ')).td($p_exercice->input())); + $html.='
'; + $html.=HtmlInput::submit('sauver', _('sauver')); + $html.=HtmlInput::button('close', _('fermer'), + 'onclick="removeDiv(\'mod_periode\')"'); + $html.=HtmlInput::hidden('p_id', $periode_id); + $html.='
'; + $a_answer['content']=$html; + break; +// Save a modification of a periode +// @todo must be adapted + case 'save': + $per=new Periode($cn, $periode_id); + $per->load(); + try + { + $p_start=$http->post("p_start", "date"); + $p_end=$http->post("p_end", "date"); + $p_exercice=$http->post("p_exercice", "number"); + if ($p_exercice>2099||$p_exercice<1980) + { + $html=''; + $html.=_('Erreur exercice invalide'); + } + else + { + $sql="update parm_periode set p_start=to_date($1,'DD.MM.YYYY'),p_end=to_date($2,'DD.MM.YYYY'),p_exercice=$3 where p_id=$4"; + try + { + $cn->exec_sql($sql, + array($p_start, $p_end, $p_exercice, $periode_id)); + $a_answer["status"]="OK"; + } + catch (Exception $e) + { + record_log($e->getTraceAsString()); + $html=$e->getTrace(); + throw $e; + } + } + $parm_periode=new Parm_periode_SQL($cn, $periode_id); + ob_start(); + Periode::display_row_global($parm_periode, 0, $js_var); + $a_answer['content']=ob_get_clean(); + } + catch (Exception $ex) + { + $html=$ex->getTrace(); + $a_answer['content']=$html; + } + break; + case "remove": + try + { + $per=new Periode($cn, $periode_id); + $per->verify_delete(); + $per->delete(); + $a_answer['status']="OK"; + } + catch (Exception $ex) + { + $a_answer["content"]=$ex->getMessage(); + } + break; +// case "add_per": +// $per=new Periode($cn, $periode_id); +// $per->load(); +// +// $p_exercice=new ISelect('p_exercice'); +// $p_exercice->value=$cn->make_array("select distinct p_exercice,p_exercice from parm_periode order by 1 desc"); +// $title=_('Ajout période'); +// $title_par="

"._('On ne peut ajouter une période que sur un exercice qui existe'). +// "

"; +// +// $p_start=new IDate('p_start'); +// $p_end=new IDate('p_end'); +// +// $html=''; +// $html.=HtmlInput::title_box($title, 'mod_periode'); +// $html.=$title_par; +// $html.='
' ; +// $html.=HtmlInput::hidden("ac", $http->post("ac")); +// $html.=Dossier::hidden(); +// $html.=''; +// +// $html.=tr(td(_(' Début période : ')).td($p_start->input())); +// $html.=tr(td(_(' Fin période : ')).td($p_end->input())); +// $html.=tr(td(_(' Exercice : ')).td($p_exercice->input())); +// $html.='
'; +// $html.=HtmlInput::submit('add_per', _('sauver')); +// $html.=HtmlInput::button('close', _('fermer'), +// 'onclick="removeDiv(\'mod_periode\')"'); +// $html.='
'; +// $a_answer['content']=$html; +// break; + case 'insert_periode': + try + { + $p_start=$http->post("p_start", "date"); + $p_end=$http->post("p_end", "date"); + $p_exercice=$http->post("p_exercice", "number"); + $obj=new Periode($cn); + $p_id=$obj->insert($p_start, $p_end, $p_exercice); + $parm_periode=new Parm_periode_SQL($cn, $p_id); + ob_start(); + Periode::display_row_global($parm_periode, 0, $js_var); + $a_answer['content']=ob_get_clean(); + $a_answer['status']="OK"; + $a_answer['p_id']=$p_id; + } + catch (Exception $e) + { + $a_answer['content']=$e->getMessage(); + } + break; + default: + $a_answer['content']=_("Invalid command")."[$act]"; + break; +} + +$jsson=json_encode($a_answer, + JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); +if (!headers_sent()) + header('Content-Type: application/json;charset=utf-8'); +echo $jsson; diff --git a/include/class/periode.class.php b/include/class/periode.class.php index 92315bb5d..63ad8da6d 100644 --- a/include/class/periode.class.php +++ b/include/class/periode.class.php @@ -1,4 +1,5 @@ p_id=$p_id; $this->cn=$p_cn; + $this->jrn_def_id=0; } - function set_jrn($p_jrn) + + function set_ledger($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]=> + + /* !\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 limit 1"; - $start=$this->cn->get_value($sql_start,array($p_exercice)); + $start=$this->cn->get_value($sql_start, array($p_exercice)); $sql_end="select p_id from parm_periode where p_exercice=$1 order by p_end DESC limit 1"; - $end=$this->cn->get_value($sql_end,array($p_exercice)); - return array("start"=>$start,"end"=>$end); + $end=$this->cn->get_value($sql_end, array($p_exercice)); + return array("start"=>$start, "end"=>$end); } - /*!\brief check if a periode is closed. If jrn_def_id is set to a no zero value then check only for this ledger - *\return 1 is the periode is closed otherwise return 0 + + /* !\brief check if a periode is closed. If jrn_def_id is set to a no zero value then check only for this ledger + * @see Periode::set_ledger + * \return 1 is the periode is closed otherwise return 0 */ + function is_closed() { - if ( $this->jrn_def_id != 0 ) + 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; + " 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; + " where ". + " p_id =".$this->p_id; $res=$this->cn->exec_sql($sql); - $status=Database::fetch_result($res,0,0); - if ( $status == 'CL' || $status=='t' ||$status=='CE') + $status=Database::fetch_result($res, 0, 0); + if ($status=='CL'||$status=='t'||$status=='CE') return 1; return 0; } + ///Return 1 if the periode is open otherwise 0 //!\note For only a ledger you must set Periode::jrn_def_id to the ledger id + ///@see Periode::set_ledger function is_open() { /* if jrn_Def_id == 0 then we check the global otherwise we check - a ledger */ - if ( $this->jrn_def_id != 0 ) + 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; + " 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; + " where ". + " p_id =".$this->p_id; $res=$this->cn->exec_sql($sql); - $status=Database::fetch_result($res,0,0); - if ( $status == 'OP' || $status=='f' ) + $status=Database::fetch_result($res, 0, 0); + if ($status=='OP'||$status=='f') return 1; return 0; } + ///Return 1 if periode is centralized + ///@deprecated //!\note deprecated , centralization not used anymore function is_centralized() { - if ( $this->jrn_def_id != 0 ) + 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; + " 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; + " where ". + " p_id =".$this->p_id; $res=$this->cn->exec_sql($sql); - $status=Database::fetch_result($res,0,0); - if ( $status == 'CE' || $status=='t' ) + $status=Database::fetch_result($res, 0, 0); + if ($status=='CE'||$status=='t') return 1; return 0; } + function reopen() { - if ( $this->jrn_def_id == 0 ) + if ($this->jrn_def_id==0) { - $this->cn->exec_sql("update parm_periode set p_closed='f',p_central='f' where p_id=$1", - array($_GET['p_per'])); + $this->cn->exec_sql("update parm_periode set p_closed='f',p_central='f' where p_id=$1", + array($this->p_id)); - $this->cn->exec_sql("update jrn_periode set status='OP' ". - " where p_id = ".$this->p_id); + $this->cn->exec_sql("update jrn_periode set status='OP' ". + " where p_id = $1", [$this->p_id]); - return; + return; } else { - $this->cn->exec_sql("update jrn_periode set status='OP' ". - " where jrn_def_id=".$this->jrn_def_id." and ". - " p_id = ".$this->p_id); - /* if one ledger is open then the periode is open */ - $this->cn->exec_sql("update parm_periode set p_closed=false where p_id=".$this->p_id); + $this->cn->exec_sql("update jrn_periode set status='OP' + where jrn_def_id=$1 and + p_id = $2 ", [$this->jrn_def_id, $this->p_id]); + /* if one ledger is open then the periode is open */ + $this->cn->exec_sql("update parm_periode set p_closed=false where p_id=".$this->p_id); return; } - } + ///Close a periode , if Periode::jrn_def_id is set to a different value /// than 0 , it close only for this ledger id ; otherwise close for all /// periode function close() { - if ( $this->jrn_def_id == 0 ) + if ($this->jrn_def_id==0) { - $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=". - $this->p_id); - $this->cn->exec_sql("update jrn_periode set status='CL' ". - " where p_id = ".$this->p_id); + $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=$1", + [$this->p_id]); + $this->cn->exec_sql("update jrn_periode set status='CL' + where p_id = $1", [$this->p_id]); return; } else { $this->cn->exec_sql("update jrn_periode set status='CL' ". - " where jrn_def_id=".$this->jrn_def_id." and ". - " p_id = ".$this->p_id); + " where jrn_def_id=$1 and + p_id = $2", [$this->jrn_def_id, $this->p_id]); /* if all ledgers have this periode closed then synchro with - the table parm_periode - */ - $nJrn=$this->cn->count_sql( "select * from jrn_periode where ". - " p_id=".$this->p_id); - $nJrnPeriode=$this->cn->count_sql( "select * from jrn_periode where ". - " p_id=".$this->p_id." and status='CL'"); + the table parm_periode + */ + $nJrn=$this->cn->count_sql("select * from jrn_periode where ". + " p_id=$1", [$this->p_id]); + $nJrnPeriode=$this->cn->count_sql("select * from jrn_periode where ". + " p_id=$1 and status='CL'", [$this->p_id]); - if ( $nJrnPeriode==$nJrn) - $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=".$this->p_id); + if ($nJrnPeriode==$nJrn) + $this->cn->exec_sql("update parm_periode set p_closed=true where p_id=$1", + [$this->p_id]); return; } - } + /** + * @deprecated since version 5 + * @return type + */ function centralized() { - if ( $this->jrn_def_id == 0 ) + if ($this->jrn_def_id==0) { $this->cn->exec_sql("update parm_periode set p_central=true"); return; @@ -185,260 +207,87 @@ class Periode else { $this->cn->exec_sql("update jrn_periode set status='CE' ". - " where ". - " p_id = ".$this->p_id); + " where ". + " p_id = $1", [$this->p_id]); return; } - } - /*! - * \brief Display all the periode and their status - * + /** + * Add new periode + * @param date $p_date_start + * @param date $p_date_end + * @param int $p_exercice + * @return int p_id of the new periode + * @exception Exception 10 Invalide date or exercice */ - - function display_form_periode() - { - $str_dossier=dossier::get(); - - if ( $this->jrn_def_id==0 ) - { - $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, - (select count(jr_id) as count_op from jrn where jr_tech_per = p_id) as count_op - from parm_periode - order by p_start,p_end"); - $Max=Database::num_row($Res); - echo '
'; - echo HtmlInput::array_to_hidden(array('ac','gDossier','jrn_def_id','choose'), $_REQUEST); - echo ''; - echo ""; - echo '"; - echo ''; - echo ''; - echo ''; - echo ""; - - for ($i=0;$i<$Max;$i++) - { - $l_line=Database::fetch_array($Res,$i); - $class="even"; - if ( $i % 2 == 0 ) - $class="odd"; - $style=''; - if ( $l_line['p_closed'] == 't') - $style="color:red"; - echo ''; - echo ''; - - echo ''; - echo ''; - echo ''; - - if ( $l_line['p_closed'] == 't' ) - { - $closed=($l_line['p_central']=='t')?'':''; - $change=''; - $remove=sprintf(_('Nombre opérations %d'),$l_line['count_op']); - $remove=td($remove,' class="mtitle" '); - $change=td (' Réouverture',' class="mtitle"'); - - } - else - { - if ($l_line['count_op'] == 0 ) - { - $change=HtmlInput::display_periode($l_line['p_id']); - } - else - { - $change="Non modifiable"; - } - $change=td($change,' class="mtitle" '); - $reopen=td(""); - - - $remove=''; - } - echo $change; - - echo $remove; - - echo ''; - - } - echo '
'.ICheckBox::toggle_checkbox("per_toggle", "periode_frm")." Date début Date fin Exercice
'; - if ( $l_line['p_closed'] == 'f') { - $per_to_close=new ICheckBox('sel_per_close[]'); - $per_to_close->value=$l_line['p_id']; - echo $per_to_close->input(); - } - echo ' '.$l_line['date_start'].' '.$l_line['date_end'].' '.$l_line['p_exercice'].'CentraliséeFermée'; - - - if ($l_line['count_op'] == 0 ) - { - $go='do.php?'.http_build_query(array('ac'=>$_REQUEST['ac'], - 'action'=>'delete_per', - 'p_per'=>$l_line['p_id'], - 'gDossier'=>Dossier::id())); - - $remove.='' - . ' Efface'; - } - else - { - $remove.=sprintf(_('Nombre opérations %d'),$l_line['count_op']); - } - $remove.='
'; - echo '

'; - echo HtmlInput::hidden("close_per", 1); - echo HtmlInput::submit('close_per_bt','Fermeture des périodes sélectionnées'); - echo '

'; - echo '
'; - $but=new IButton('show_per_add','Ajout d\'une période'); - $but->javascript="$('periode_add_div').show();"; - echo $but->input(); - echo '
'; - echo HtmlInput::title_box("Ajout d'une période","periode_add_div","hide"); - echo '
'; - echo dossier::hidden(); - $istart=new IDate('p_date_start'); - $iend=new IDate('p_date_end'); - $iexercice=new INum('p_exercice'); - $iexercice->size=5; - echo ''; - echo ' '; - echo td('Date de début'); - echo td($istart->input()); - echo ''; - echo td('Date de fin'); - echo td($iend->input()); - - echo ''; - echo td('Exercice'); - echo td($iexercice->input()); - - echo '
'; - - echo HtmlInput::submit('add_per','Valider'); - echo '
'; - echo '
'; - echo create_script("$('periode_add_div').hide();new Draggable('periode_add_div',{starteffect:function() - { - new Effect.Highlight(obj.id,{scroll:window,queue:'end'}); - }} - );"); - } - else - { - $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 - from parm_periode join jrn_periode using (p_id) where jrn_def_id=".$this->jrn_def_id." - order by p_start,p_end"); - $Max=Database::num_row($Res); - $r=$this->cn->exec_sql('select jrn_Def_name from jrn_Def where jrn_Def_id='. - $this->jrn_def_id); - $jrn_name=Database::fetch_result($r,0,0); - echo '

Journal '.$jrn_name.'

'; - echo '
'; - echo HtmlInput::array_to_hidden(array('ac','gDossier','jrn_def_id','choose'), $_REQUEST); - - echo ''; - echo ""; - echo '"; - echo ''; - echo ''; - echo ''; - echo ""; - - for ($i=0;$i<$Max;$i++) - { - $l_line=Database::fetch_array($Res,$i); - if ( $l_line['status'] != 'OP' ) - echo ''; - else - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - $closed=""; - if ( $l_line['status'] != 'OP' ) - { - $go='do.php?'.http_build_query(array('ac'=>$_REQUEST['ac'], - 'action'=>'reopen', - 'p_per'=>$l_line['p_id'], - 'gDossier'=>Dossier::id(), - 'jrn_def_id'=>$this->jrn_def_id)); - - $closed=td (' Réouverture',' class="mtitle"'); - } - - echo "$closed"; - - echo ''; - - } - echo '
'.ICheckBox::toggle_checkbox("per_toggle", "periode_frm")." Date début Date fin Exercice
'; - if ( $l_line['status'] == 'OP') { - $per_to_close=new ICheckBox('sel_per_close[]'); - $per_to_close->value=$l_line['p_id']; - echo $per_to_close->input(); - } - echo ' '.$l_line['date_start'].' '.$l_line['date_end'].' '.$l_line['p_exercice'].'
'; - echo '

'; - echo HtmlInput::submit('close_per','Fermeture des périodes sélectionnées'); - echo '

'; - echo '
'; - - } - } - function insert($p_date_start,$p_date_end,$p_exercice) + function insert($p_date_start, $p_date_end, $p_exercice) { try { - if (isDate($p_date_start) == null || - isDate($p_date_end) == null || - strlen (trim($p_exercice)) == 0 || - (string) $p_exercice != (string)(int) $p_exercice - ||$p_exercice < COMPTA_MIN_YEAR || $p_exercice > COMPTA_MAX_YEAR) - - { - throw new Exception ("Paramètre invalide"); - } - $p_id=$this->cn->get_next_seq('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); + if (isDate($p_date_start)==null || + isDate($p_date_end)==null || + strlen(trim($p_exercice))==0 || + isNumber($p_exercice) ==0 || + $p_exerciceCOMPTA_MAX_YEAR) + { + throw new Exception(_("Paramètre invalide"),10); + } + $overlap_start=$this->cn->get_value("select count(*) from parm_periode + where + p_start <= to_date($1,'DD-MM-YYYY') + and p_end >= to_date($1,'DD-MM-YYYY') + + ",[$p_date_start]); + $overlap_end=$this->cn->get_value("select count(*) from parm_periode + where + p_start <= to_date($1,'DD-MM-YYYY') + and p_end >= to_date($1,'DD-MM-YYYY') + + ",[$p_date_end]); + if ( $overlap_start > 0 || $overlap_end > 0) + { + throw new Exception (_("Période chevauchant une autre")); + } + $p_id=$this->cn->get_next_seq('s_periode'); + $sql=" insert into parm_periode(p_id,p_start,p_end,p_closed,p_exercice) + values + ($1, + to_date($2,'DD.MM.YYYY'), + to_date($3,'DD.MM.YYYY'), + 'f', + $4)"; + $this->cn->start(); - $Res=$this->cn->exec_sql($sql); + $Res=$this->cn->exec_sql($sql,[$p_id, $p_date_start, $p_date_end, $p_exercice]); $Res=$this->cn->exec_sql("insert into jrn_periode (jrn_def_id,p_id,status) ". - "select jrn_def_id,$p_id,'OP' from jrn_def"); + "select jrn_def_id,$p_id,'OP' from jrn_def"); $this->cn->commit(); + return $p_id; } catch (Exception $e) { record_log($e->getTraceAsString()); $this->cn->rollback(); + throw $e; return 1; } return 0; } - /*!\brief load data from database - *\return 0 on success and -1 on error + + /* !\brief load data from database + * \return 0 on success and -1 on error */ + function load() { - if ($this->p_id == '') $this->p_id=-1; + if ($this->p_id=='') + $this->p_id=-1; $row=$this->cn->get_array("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 -1; + array($this->p_id)); + if ($row==null) + return -1; $this->p_start=$row[0]['p_start']; $this->p_end=$row[0]['p_end']; @@ -448,138 +297,436 @@ class Periode return 0; } - /*!\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 + /* !\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=$this->cn->get_value("select p_id from parm_periode where p_exercice=$1 order by p_start asc limit 1",array($p_exercice)); - $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)); + $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)); + $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)); $rMax=new Periode($this->cn); $rMax->p_id=$max; - if ( $rMax->load() ) throw new Exception('Periode n\'existe pas'); + if ($rMax->load()) + throw new Exception('Periode n\'existe pas'); $rMin=new Periode($this->cn); $rMin->p_id=$min; - if ( $rMin->load() ) throw new Exception('Periode n\'existe pas'); - return array($rMax,$rMin); + if ($rMin->load()) + throw new Exception('Periode n\'existe pas'); + return array($rMax, $rMin); } - /*! + + /* ! * \brief Give the start & end date of a periode * \param $p_periode is the periode id, if omitted the value is the current object * \return array containing the start date & the end date, index are p_start and p_end or NULL if * nothing is found - \verbatim - $ret['p_start']=>'01.01.2009' - $ret['p_end']=>'31.01.2009' - \endverbatim + \verbatim + $ret['p_start']=>'01.01.2009' + $ret['p_end']=>'31.01.2009' + \endverbatim */ - public function get_date_limit($p_periode = 0) + + public function get_date_limit($p_periode=0) { - if ( $p_periode == 0 ) $p_periode=$this->p_id; + if ($p_periode==0) + $p_periode=$this->p_id; $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=$1"; - $Res=$this->cn->exec_sql($sql,array($p_periode)); - if ( Database::num_row($Res) == 0) return null; - return Database::fetch_array($Res,0); - + $Res=$this->cn->exec_sql($sql, array($p_periode)); + if (Database::num_row($Res)==0) + return null; + return Database::fetch_array($Res, 0); } - /*!\brief return the first day of periode - *the this->p_id must be set - *\return a string with the date (DD.MM.YYYY) + + /* !\brief return the first day of periode + * the this->p_id must be set + * \return a string with the date (DD.MM.YYYY) */ + public function first_day($p=0) { - if ($p==0) $p=$this->p_id; - list($p_start,$p_end)=$this->get_date_limit($p); + if ($p==0) + $p=$this->p_id; + list($p_start, $p_end)=$this->get_date_limit($p); return $p_start; } - /*!\brief return the last day of periode - *the this->p_id must be set - *\return a string with the date (DD.MM.YYYY) + + /* !\brief return the last day of periode + * the this->p_id must be set + * \return a string with the date (DD.MM.YYYY) */ + public function last_day($p=0) { - if ($p==0) $p=$this->p_id; - list($p_start,$p_end)=$this->get_date_limit($p); + if ($p==0) + $p=$this->p_id; + list($p_start, $p_end)=$this->get_date_limit($p); return $p_end; } function get_exercice($p_id=0) { - if ( $p_id == 0 ) $p_id=$this->p_id; + if ($p_id==0) + $p_id=$this->p_id; $sql="select p_exercice from parm_periode where p_id=".$p_id; $Res=$this->cn->exec_sql($sql); - if ( Database::num_row($Res) == 0) return null; - return Database::fetch_result($Res,0,0); - + if (Database::num_row($Res)==0) + return null; + return Database::fetch_result($Res, 0, 0); } - /*!\brief retrieve the periode thanks the date_end - *\param $p_date format DD.MM.YYYY + + /* !\brief retrieve the periode thanks the date_end + * \param $p_date format DD.MM.YYYY * \return the periode id - *\exception if not periode is found or if more than one periode is found + * \exception if not periode is found or if more than one periode is found */ + function find_periode($p_date) { $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') "; - $ret=$this->cn->exec_sql($sql,array($p_date)); + $ret=$this->cn->exec_sql($sql, array($p_date)); $nb_periode=Database::num_row($ret); - if ( $nb_periode == 0 ) - throw (new Exception('Aucune période trouvée',101)); - if ( $nb_periode > 1 ) - throw (new Exception("Trop de périodes trouvées $nb_periode pour $p_date",100)); - $per=Database::fetch_result($ret,0); + if ($nb_periode==0) + throw (new Exception('Aucune période trouvée', 101)); + if ($nb_periode>1) + throw (new Exception("Trop de périodes trouvées $nb_periode pour $p_date", + 100)); + $per=Database::fetch_result($ret, 0); $this->p_id=$per; return $per; } + /** - *add a exerice of 13 periode + * add a exercice starting in year p_year with p_month month, with a starting + * and a closing + * @param $p_exercice the exercice + * @param $p_year the starting year + * @param $p_from_month starting month + * @param $p_month number of month of the exercice + * @param $p_opening 1 if we create a one-day periode for opening writings + * @param $p_closing 1 if we create a one-day periode for closing writings */ - function insert_exercice($p_exercice,$nb_periode) + function insert_exercice($p_exercice, $p_year, $p_from_month, $p_month, + $p_opening, $p_closing) { - try - { - if ( $nb_periode != 12 && $nb_periode != 13) throw new Exception ('Nombre de période incorrectes'); - $this->cn->start(); - for ($i=1;$i < 12;$i++) - { - $date_start=sprintf('01.%02d.%d',$i,$p_exercice); - $date_end=$this->cn->get_value("select to_char(to_date($1,'DD.MM.YYYY')+interval '1 month'-interval '1 day','DD.MM.YYYY')",array($date_start)); - if ( $this->insert($date_start,$date_end,$p_exercice) != 0) - { - throw new Exception('Erreur insertion période'); - } - } - if ( $nb_periode==12 && $this->insert('01.12.'.$p_exercice,'31.12.'.$p_exercice,$p_exercice) != 0 ) - { - throw new Exception('Erreur insertion période'); - } - if ( $nb_periode==13) - { - if ($this->insert('01.12.'.$p_exercice,'30.12.'.$p_exercice,$p_exercice) != 0 ) - throw new Exception('Erreur insertion période'); - if ($this->insert('31.12.'.$p_exercice,'31.12.'.$p_exercice,$p_exercice) != 0 ) - throw new Exception('Erreur insertion période'); - } + try + { + if (isNumber($p_exercice)==0) + throw new Exception(_("Exercice n'est pas un nombre")); + if ($p_exercice>COMPTA_MAX_YEAR||$p_exercicecn->commit(); - } - catch (Exception $e) - { - record_log($e->getTraceAsString()); - $this->cn->rollback(); - } + if ($p_year>COMPTA_MAX_YEAR||$p_year60) + throw new Exception(_("Nombre de mois doit être compris entre 1 & 60 ")); + if (isNumber($p_month)==0) + throw new Exception(_("Mois de début n'existe pas ")); + if ($p_from_month>13||$p_from_month<1) + throw new Exception(_("Mois de début n'existe pas ")); + + $this->cn->start(); + $year=$p_year; + $month=$p_from_month; + for ($i=1; $i<=$p_month; $i++) + { + + // create first a periode of day + if ($i==1&&$p_opening==1) + { + $fdate_start=sprintf('01.%02d.%d', $month, $year); + $this->insert($fdate_start, $fdate_start, $p_exercice); + + $date_start=sprintf('02.%02d.%d', $month, $year); + $date_end=$this->cn->get_value("select to_char(to_date($1,'DD.MM.YYYY')+interval '1 month'-interval '1 day','DD.MM.YYYY')", + array($fdate_start)); + + $date_end=sprintf('02.%02d.%d', $month, $year); + $this->insert($date_start, $date_end, $p_exercice); + } + // The last month, we create a one-day periode for closing + elseif ($i==$p_month && $p_closing ==1 ) + { + $fdate_start=sprintf('01.%02d.%d', $month, $year); + $date_end=$this->cn->get_value("select to_char(to_date($1,'DD.MM.YYYY')+interval '1 month'-interval '2 day','DD.MM.YYYY')", + array($fdate_start)); + $this->insert($fdate_start, $date_end, $p_exercice); + + $date_end=$this->cn->get_value("select to_char(to_date($1,'DD.MM.YYYY')+interval '1 month'-interval '1 day','DD.MM.YYYY')", + array($fdate_start)); + + $this->insert($date_end, $date_end, $p_exercice); + + } + else + { + $date_start=sprintf('01.%02d.%d', $month, $year); + $date_end=$this->cn->get_value("select to_char(to_date($1,'DD.MM.YYYY')+interval '1 month'-interval '1 day','DD.MM.YYYY')", + array($date_start)); + if ($this->insert($date_start, $date_end, $p_exercice)!=0) + { + throw new Exception('Erreur insertion période'); + } + } + $month++; + if ($month == 13 ) + { + $year++; + $month=1; + } + } + + $this->cn->commit(); + } + catch (Exception $e) + { + record_log($e->getTraceAsString()); + $this->cn->rollback(); + throw $e; + } } - static function test_me() + + /** + * Display a table with all the periode + * @param $p_js javascript variable + * @see scripts.js + */ + static function display_periode_global($p_js) { $cn=Dossier::connect(); - $obj=new Periode($cn); - $obj->set_jrn(1); - $obj->display_form_periode(); + $periode=new Parm_Periode_SQL($cn); + $ret=$periode->seek(" order by p_start asc"); + $nb_periode=Database::num_row($ret); + + if ($nb_periode==0) + return; + echo ''; + echo ""; + echo ""; + echo th(""); + echo th(_("Date Début")); + echo th(_("Date Fin")); + echo th(_("Exercice")); + echo th(_("nb opérations")); + echo th(_("Status")); + echo ""; + echo ""; + echo ''; + + for ($i=0; $i<$nb_periode; $i++) + { + $obj=$periode->next($ret, $i); + Periode::display_row_global($obj, $i, $p_js); + } + echo ''; + echo '
'; + } + + /** + * count the number of operation of a Periode + * @return integer + */ + function count_operation() + { + $count=$this->cn->get_value(" + select count(*) + from + jrn + where + jr_tech_per = $1", [$this->p_id]); + return $count; + } + + /** + * @brief Display each row for the global + * @param $obj Parm_Periode_SQL + * @param $p_nb not used so far + * @param $p_js javascript variable + */ + static function display_row_global(Parm_Periode_SQL $obj, $p_nb, $p_js) + { + $periode=new Periode($obj->cn, $obj->p_id); + $class=($p_nb%2==0)?"even":"odd"; + printf(' ', + $obj->getp("p_id"), + $obj->getp("p_start"), + $obj->getp("p_exercice"), + $obj->getp("p_id"), $class); + /** + * Display a checkbox to select several month to close + */ + if ($obj->getp("p_closed")=="f") + { + $checkbox=new ICheckBox("sel_per_close[]"); + $checkbox->set_attribute("per_id", $obj->getp("p_id")); + $checkbox->value=$obj->getp("p_id"); + echo "".$checkbox->input().""; + } + else + { + echo td(""); + } + echo td(format_date($obj->getp("p_start"), "YYYY-MM-DD", "DD.MM.YYYY")); + echo td(format_date($obj->getp("p_end"), "YYYY-MM-DD", "DD.MM.YYYY")); + echo td($obj->getp("p_exercice")); + $nb_operation=$periode->count_operation(); + echo td($nb_operation); + $closed=$obj->getp('p_closed'); + $status=($closed=='t')?_("Fermée"):_("Ouvert"); + echo td($status); + + // if no operation then this periode can be removed or updated + if ($nb_operation==0) + { + // Updatable + $js=sprintf("%s.box_display('%d')", $p_js, $obj->p_id); + echo ""; + echo HtmlInput::image_click("crayon-mod-b24.png", $js, _("Effacer")); + echo ""; + //removable + $js=sprintf("%s.remove('%d')", $p_js, $obj->p_id); + echo ""; + echo HtmlInput::image_click("trash-24.gif", $js, _("Effacer")); + echo ""; + } + else + { + echo td(""), td(""); + } + + /// Can close if open + echo ""; + if ($obj->getp("p_closed")=='f') + { + $javascript=sprintf("%s.close_periode('%d')", $p_js, $obj->p_id); + echo HtmlInput::anchor_action(_("Fermeture"), $javascript, NULL, + "line"); + } + else + { + $javascript=sprintf("%s.open_periode('%d')", $p_js, $obj->p_id); + echo HtmlInput::anchor_action(_("Réouverture"), $javascript, NULL, + "line"); + } + echo ""; + echo ""; + } + + /** + * @brief display a form (method POST) to input a new exercice + * variable in the FORM + * - p_exercice + * - p_year + * - nb_month + * - from_month + * - day_opening + * - day_closing + */ + static function form_exercice_add() + { + $cn=Dossier::connect(); + $exercice=new INum('p_exercice'); + $exercice->prec=0; + $exercice->value=$cn->get_value('select max(p_exercice::float)+1 from parm_periode'); + $year=new INum('p_year'); + $year->prec=0; + $year->value=$exercice->value; + $nb_month=new INum('nb_month'); + $nb_month->prec=0; + $nb_month->value=12; + $from=new ISelect('from_month'); + $amonth=array(); + $month=[_('Janvier'), _('Février'), _('Mars'), _('Avril'), + _('Mai'), _('Juin'), _('Juillet'), _('Août'), _('Septembre'), + _('Octobre'), _('Novembre'), _('Décembre')]; + for ($i=1; $i<13; $i++) + { + $strMonth=$month[($i-1)]; + $amonth[]=array("value"=>$i, "label"=>$strMonth); + } + $from->value=$amonth; + $day_opening=new ICheckBox("day_opening"); + $day_closing=new ICheckBox("day_closing"); + $day_closing->value=1; + $day_opening->value=1; + $day_closing->set_check(1); + require_once NOALYSS_TEMPLATE.'/periode_add_exercice.php'; + } + function delete() { + $this->cn->exec_sql("delete from parm_periode where p_id=$1",[$this->p_id]); + } + /** + * Verify before delete that the month is not used + * @exception Exception code 1 if periode used + */ + function verify_delete() { + try { + if ( $this->cn->get_value("select count(*) from jrn where jr_tech_per =$1 ",[$this->p_id]) > 0) { + throw new Exception(_("Effacement impossible"), 1); + } + } catch (Exception $ex) { + throw $ex; + } + } + /** + * @brief Display a form for the input of a new periode + */ + static function form_periode_add($p_js_var) + { + $cn=Dossier::connect(); + $p_exercice=new ISelect('p_exercice'); + $p_exercice->value=$cn->make_array("select distinct p_exercice,p_exercice from parm_periode order by 1 desc"); + $title=_('Ajout période'); + $title_par="

"._('On ne peut ajouter une période que sur un exercice qui existe déjà'). + "

"; + + $p_start=new IDate('p_start'); + $p_end=new IDate('p_end'); + + $html=''; + $html.=HtmlInput::title_box($title, 'periode_add','hide'); + $html.=$title_par; + $html.='
' ; + $html.=HtmlInput::hidden("ac", $_REQUEST['ac']); + $html.=Dossier::hidden(); + $html.=''; + + $html.=tr(td(_(' Début période : ')).td($p_start->input())); + $html.=tr(td(_(' Fin période : ')).td($p_end->input())); + $html.=tr(td(_(' Exercice : ')).td($p_exercice->input())); + $html.='
'; + $html.=HtmlInput::submit('add_per', _('sauver')); + $html.=HtmlInput::button('close', _('fermer'), + 'onclick="$(\'periode_add\').hide()"'); + $html.='
'; + echo $html; + } + /** + *@brief + */ + static function filter_exercice($p_sel) + { + $cn=Dossier::connect(); + $i_exercice=new ISelect("p_exercice_sel"); + $i_exercice->value=$cn->make_array("select distinct p_exercice,p_exercice from parm_periode order by 1 desc", 1); + $i_exercice->javascript="onchange=\"Periode.filter_exercice('periode_tbl')\""; + $i_exercice->selected=$p_sel; + echo $i_exercice->input(); } } diff --git a/include/class/periode_ledger.class.php b/include/class/periode_ledger.class.php new file mode 100644 index 000000000..f172df57a --- /dev/null +++ b/include/class/periode_ledger.class.php @@ -0,0 +1,76 @@ + + +/** + * @file + * @brief Manage the periode of a specific ledger + */ +require_once NOALYSS_INCLUDE."/class/periode_ledger_table.class.php"; +require_once NOALYSS_INCLUDE."/class/periode.class.php"; +/** + * @brief Manage the periode of a specif ledger, wrap the SQL Class Jrn_Periode_SQL + * @see Periode + * @see Periode_Ledger_Table + * + */ +class Periode_Ledger +{ + private $m_jrn_periode_sql; + function __construct(Jrn_periode_SQL $p_jrn_periode_sql) + { + $this->m_jrn_periode_sql=$p_jrn_periode_sql; + } + public function get_jrn_periode_sql() + { + return $this->m_jrn_periode_sql; + } + + public function set_jrn_periode_sql($m_jrn_periode_sql) + { + $this->m_jrn_periode_sql=$m_jrn_periode_sql; + } + /** + * Close the month / periode for the ledger , + * Call Periode->close() + * @see Periode::close + */ + function close() + { + $cn=Dossier::connect(); + $jrn_periode_sql=$this->m_jrn_periode_sql; + $periode=new Periode($cn,$jrn_periode_sql->getp("p_id")); + $periode->set_ledger($this->m_jrn_periode_sql->getp("jrn_def_id")); + $periode->close(); + } + /** + * Reopen the month / periode for the ledger , + * Call Periode->reopen() + * @see Periode::reopen + */ + function reopen() + { + $cn=Dossier::connect(); + $periode=new Periode($cn,$this->m_jrn_periode_sql->getp("p_id")); + $periode->jrn_def_id=$this->m_jrn_periode_sql->getp("jrn_def_id"); + $periode->reopen(); + } +} + diff --git a/include/class/periode_ledger_table.class.php b/include/class/periode_ledger_table.class.php new file mode 100644 index 000000000..d4c4da513 --- /dev/null +++ b/include/class/periode_ledger_table.class.php @@ -0,0 +1,162 @@ + + + +/** + * @file + * @brief Manage the periode per ledger, used the table jrn_periode + * @example periode-ledger-test.php + */ +require_once NOALYSS_INCLUDE.'/database/jrn_periode_sql.class.php'; + +/** + * @brief Display the periode per ledger: close : reopen ... + * the close , open must be done thanks Periode + * @see Periode + */ +class Periode_Ledger_Table +{ + + private $sql; + private $a_member; + private $cn; + + function __construct($p_id) + { + $this->a_member["id"]=$p_id; + $this->sql="select id,p_id,status,p_id,p_start,p_end,p_closed,p_exercice,jrn_def_id + from + jrn_periode + join parm_periode using (p_id)"; + $this->cn=Dossier::connect(); + $this->a_member=$this->cn->get_row($this->sql." where id = $1", + [$this->a_member["id"]]); + + } + public function get_a_member() + { + return $this->a_member; + } + + + function set_id($p_id) + { + $a_member['id']=$p_id; + } + /** + * Load a row and return it as an array + * @return type + */ + function load() + { + $this->a_member=$this->cn->get_row($this->sql." where id = $1", + [$this->a_member["id"]]); + return $this->a_member; + } + function get_resource_periode_ledger($p_ledger_id) + { + $ret=$this->cn->exec_sql($this->sql." where jrn_def_id = $1 order by p_start desc",[$p_ledger_id]); + return $ret; + } + /** + * Display all the periode for a specific ledger + * the ledger is in m_jrn_periode_sql + * + * @param type $p_resource_psql resource SQL + * @param type $p_js + * @return type + * @see Periode_Ledger_Table::get_resource_periode_ledger + */ + function display_table($p_resource_psql, $p_js) + { + $nb_periode=Database::num_row($p_resource_psql); + + if ($nb_periode==0) + return; + echo ''; + echo ""; + echo ""; + echo th(""); + echo th(_("Date Début")); + echo th(_("Date Fin")); + echo th(_("Exercice")); + echo th(""); + echo ""; + echo ""; + echo ''; + + for ($i=0; $i<$nb_periode; $i++) + { + $obj=Database::fetch_array($p_resource_psql, $i); + $this->display_row($obj, $i, $p_js); + } + echo ''; + echo '
'; + } + /** + * Display one row from jrn_periode with supplemental info + * + * @param type $pa_row is an array corresponding to a_member + * @param type $p_num number of rows , for color + * @param type $p_js name of the js variable passed to ajax + */ + function display_row($pa_row, $p_num, $p_js) + { + $class=($p_num%2==0)?"even":"odd"; + printf(' ', + $pa_row["id"], $pa_row["jrn_def_id"], $pa_row["p_exercice"], + $pa_row["id"], $class + ); + /** + * Display a checkbox to select several month to close + */ + if ($pa_row["status"] == "OP") { + $checkbox=new ICheckBox("sel_per_close[]"); + $checkbox->set_attribute("per_id", $pa_row['id']); + $checkbox->value=$pa_row['id']; + echo "".$checkbox->input().""; + }else { + echo td(""); + } + echo td(format_date($pa_row["p_start"], "YYYY-MM-DD", "DD.MM.YYYY")); + echo td(format_date($pa_row["p_end"], "YYYY-MM-DD", "DD.MM.YYYY")); + echo td($pa_row["p_exercice"]); + $status=($pa_row['p_closed']=='t')?_("Fermée"):_("Ouvert"); + + /// Can close if open + echo ""; + if ($pa_row['status']=='OP') + { + $javascript=sprintf("%s.close_periode('%d')", $p_js, $pa_row['id']); + echo HtmlInput::anchor_action(_("Fermeture"), $javascript, NULL, + "line"); + } + elseif ($pa_row['status']=='CL') + { + $javascript=sprintf("%s.open_periode('%d')", $p_js, $pa_row['id']); + echo HtmlInput::anchor_action(_("Réouverture"), $javascript, NULL, + "line"); + } + echo ""; + echo ""; + } + +} diff --git a/include/database/jrn_periode_sql.class.php b/include/database/jrn_periode_sql.class.php new file mode 100644 index 000000000..eb50639fa --- /dev/null +++ b/include/database/jrn_periode_sql.class.php @@ -0,0 +1,69 @@ +table="public.jrn_periode"; + $this->primary_key="id"; + /* + * List of columns + */ + $this->name=array( + "id"=>"id", + "jrn_def_id"=>"jrn_def_id" + , "p_id"=>"p_id" + , "status"=>"status" + ); + /* + * Type of columns + */ + $this->type=array( + "id"=>"numeric", + "jrn_def_id"=>"numeric" + , "p_id"=>"numeric" + , "status"=>"text" + ); + + + $this->default=array( + "id"=>"auto" + ); + + $this->date_format="DD.MM.YYYY"; + parent::__construct($p_cn, $p_id); + } + +} diff --git a/include/database/parm_periode_sql.class.php b/include/database/parm_periode_sql.class.php new file mode 100644 index 000000000..2cbd0806b --- /dev/null +++ b/include/database/parm_periode_sql.class.php @@ -0,0 +1,75 @@ +table = "public.parm_periode"; + $this->primary_key = "p_id"; +/* + * List of columns + */ + $this->name=array( + "p_id"=>"p_id" + ,"p_start"=>"p_start" + ,"p_end"=>"p_end" + ,"p_exercice"=>"p_exercice" + ,"p_closed"=>"p_closed" + ,"p_central"=>"p_central" + ); +/* + * Type of columns + */ + $this->type = array( + "p_id"=>"numeric" + ,"p_start"=>"date" + ,"p_end"=>"date" + ,"p_exercice"=>"text" + ,"p_closed"=>"boolean" + ,"p_central"=>"boolean" + ); + + + $this->default = array( + "p_id" => "auto" + ); + + $this->date_format = "DD.MM.YYYY"; + parent::__construct($p_cn,$p_id); + } + + +} \ No newline at end of file diff --git a/include/periode.inc.php b/include/periode.inc.php index d938ad3ea..fb7e62cd9 100644 --- a/include/periode.inc.php +++ b/include/periode.inc.php @@ -15,133 +15,124 @@ * You should have received a copy of the GNU General Public License * along with NOALYSS; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ + */ // Copyright Author Dany De Bontridder danydb@aevalys.eu -/*! \file +/* ! \file * \brief add, modify, close or delete a period */ -if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis'); +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); $gDossier=dossier::id(); require_once NOALYSS_INCLUDE.'/lib/iselect.class.php'; -require_once NOALYSS_INCLUDE.'/class/periode.class.php'; +require_once NOALYSS_INCLUDE.'/class/periode.class.php'; echo '
'; $cn=Dossier::connect(); //----------------------------------------------------- // Periode //----------------------------------------------------- -$action=""; -if ( isset($_REQUEST['action'])) - $action=$_REQUEST['action']; -$choose=(isset ($_GET['choose']))?$_GET['choose']:"no"; +require_once NOALYSS_INCLUDE."/class/periode.class.php"; +require_once NOALYSS_INCLUDE."/class/periode_ledger.class.php"; +$http=new HttpInput(); -if ($choose=='Valider') $choose='yes'; +$p_ledger_id=$http->request("jrn_def_id", "number", 0); +?> + +'; +echo dossier::hidden(); +$sel_jrn=$cn->make_array("select jrn_def_id, jrn_def_name from ". + " jrn_def order by jrn_def_name"); +$sel_jrn[]=array('value'=>0, 'label'=>_('Global : periode pour tous les journaux')); +$wSel=new ISelect(); +$wSel->value=$sel_jrn; +$wSel->name='jrn_def_id'; +$wSel->selected=$p_ledger_id; +echo _("Choisissez global ou uniquement le journal à fermer").$wSel->input(); +echo HtmlInput::submit('choose', 'Valider'); +echo HtmlInput::hidden('ac', $_REQUEST['ac']); +echo ""; +// display a filter by exercice +echo _("Montrer l'exercice"); +$max_exercice=$cn->get_value("select max(p_exercice) from parm_periode"); +$p_exercice=$http->request("p_exercice","string",$max_exercice); +Periode::filter_exercice($p_exercice); -} +$js_close_selected="jsper.close_selected()"; +echo HtmlInput::button_action(_("Fermer les périodes sélectionnées"), + $js_close_selected); +//-------------------------------------------------------------------- +// Add an exercice +// receive nb_exercice +//-------------------------------------------------------------------- if (isset($_POST['add_exercice'])) - { +{ $obj=new Periode($cn); - $exercice=$cn->get_value('select max(p_exercice::float)+1 from parm_periode'); - if ( $obj->insert_exercice($exercice,$_POST['nb_exercice']) == 1 ) + try { - alert(_('Valeurs invalides')); + $p_exercice=$http->post("p_exercice", "number"); + $p_year=$http->post("p_year", "number"); + $nb_month=$http->post("nb_month", "number"); + $from_month=$http->post("from_month", "number"); + $day_opening=$http->post("day_opening", "string", 0); + $day_closing=$http->post("day_closing", "string", 0); + $exercice=new Periode($cn); + $exercice->insert_exercice($p_exercice, $p_year, $from_month, $nb_month, + $day_opening, $day_closing); } - - $choose="yes"; - } + catch (Exception $ex) + { + echo_warning($ex->getMessage()); + } +} /* - * Close selected periode - */ -if ( isset($_POST['close_per']) ) + * Display all the periode for all ledgers + */ +if ($p_ledger_id==0) { - if (isset($_POST['sel_per_close'] ) ) { - $a_per_to_close=$_POST['sel_per_close']; - for ($i=0;$i < count($a_per_to_close);$i++) { - $per=new Periode($cn); - $jrn_def_id=(isset($_GET['jrn_def_id']))?$_GET['jrn_def_id']:0; - $per->jrn_def_id=$jrn_def_id; - $per->set_periode($a_per_to_close[$i]); - $per->close(); - - } - } - $choose="yes"; -} + echo HtmlInput::button_action(_("Ajout exercice"), + "\$('exercice_add').show()"); +//------------------------------------------------------------------- +// Add a new Exercice +//------------------------------------------------------------------- + echo ''; +//------------------------------------------------------------------- +// Add a new Periode +//------------------------------------------------------------------- + echo HtmlInput::button_action(_("Ajout période"), "\$('periode_add').show()"); + echo ''; - -if ( $action== "delete_per" ) -{ - $p_per=$_GET["p_per"]; -// Check if the periode is not used - if ( $cn->count_sql("select * from jrnx where j_tech_per=$p_per") != 0 ) - { - alert(_(' Désolé mais cette période est utilisée')); - } - else - { - $count=$cn->get_value("select count(*) from parm_periode;"); - if ( $count > 1 ) { - $Res=$cn->exec_sql("delete from parm_periode where p_id=$p_per"); - } else - { - alert(_('Désolé mais vous devez avoir au moins une période')); - } - } - $choose="yes"; -} -if ( $action == 'reopen') - { - $jrn_def_id=(isset($_GET['jrn_def_id']))?$_GET['jrn_def_id']:0; - $per=new Periode($cn); - $jrn_def_id=(isset($_GET['jrn_def_id']))?$_GET['jrn_def_id']:0; - $per->set_jrn($jrn_def_id); - $per->set_periode($_GET['p_per']); - $per->reopen(); - - $choose="yes"; - } -if ( $choose=="yes" ) -{ - echo '

'; - echo HtmlInput::button_anchor(_('Autre Journal ?'),'?choose=no&ac='.$_REQUEST['ac'].'&gDossier='.dossier::id()); - echo '

'; - $per=new Periode($cn); - $jrn=(isset($_GET['jrn_def_id']))?$_GET['jrn_def_id']:0; - $per->set_jrn($jrn); - - $per->display_form_periode(); - $nb_exercice=new ISelect("nb_exercice"); - $nb_exercice->value=array( - array('value'=>12,'label'=>_("12 périodes)")), - array('value'=>13,'label'=>_("13 périodes")) - ); - - require_once NOALYSS_TEMPLATE.'/periode_add_exercice.php'; +//------------------------------------------------------------------- +// List of Periode +//------------------------------------------------------------------- + $periode=new Parm_Periode_SQL($cn); + Periode::display_periode_global("jsper"); } else { - echo '
'; - echo dossier::hidden(); - $sel_jrn=$cn->make_array("select jrn_def_id, jrn_def_name from ". - " jrn_def order by jrn_def_name"); - $sel_jrn[]=array('value'=>0,'label'=>_('Global : periode pour tous les journaux')); - $wSel=new ISelect(); - $wSel->value=$sel_jrn; - $wSel->name='jrn_def_id'; - echo _("Choisissez global ou uniquement le journal à fermer").$wSel->input(); - echo HtmlInput::submit('choose','Valider'); - echo HtmlInput::hidden('ac',$_REQUEST['ac']); - echo "
"; - echo '

'._("Pour ajouter, effacer ou modifier une période, il faut choisir global").'

'; + echo '

'._("Pour ajouter, effacer ou modifier une période, il faut choisir global").'

'; + $ledger=new Acc_Ledger($cn, $p_ledger_id); + echo h2($ledger->get_name()); + + $periode_ledger=new Periode_Ledger_Table(0); + $ret=$periode_ledger->get_resource_periode_ledger($p_ledger_id); + $periode_ledger->display_table($ret, "jsper"); } + echo '
'; ?> + diff --git a/include/template/periode_add_exercice.php b/include/template/periode_add_exercice.php index bab22985c..0d139d817 100644 --- a/include/template/periode_add_exercice.php +++ b/include/template/periode_add_exercice.php @@ -1,26 +1,104 @@
Ajout d'un exercice -
    -
  • -Exercice en 12 périodes : Ajout d'un exercice comptable de 12 périodes, commençant le 1 janvier et terminant le 31 décembre.
  • -
  • Exercice en 13 périodes: Ajout d'une période d'un jour le 31/12. Cette période est utilisée -pour faire toutes les écritures de fin d'exercice: amortissements, régulations de compte... Avec une 13ième période, cela simplifie les prévisions, les rapports...
  • -
  • - Pour ajouter des années, ne commençant pas en janvier ou comptant un nombre de mois supérieur à 12, utilisez le plugin "Outils Comptables" -
  • -
- -
')"> -input(); -echo HtmlInput::hidden("jrn_def_id","0"); -echo HtmlInput::hidden("add_exercice","1"); -echo Dossier::hidden(); -echo HtmlInput::submit("add_exercicebt",_("Ajout d'un exercice comptable")); ?> + +

+ + +

+

+ /p> +'))"> + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +input() ?> +
+ + +input() ?> +
+ + +input() ?> +
+ + +input() ?> +
+ + + input();?> +
+ + + input();?> +
+

-
+ diff --git a/sql/upgrade.sql b/sql/upgrade.sql index d8ff69b9e..217dfc02e 100644 --- a/sql/upgrade.sql +++ b/sql/upgrade.sql @@ -58,3 +58,14 @@ CREATE TABLE public.user_filter ( unpaid varchar NULL, PRIMARY KEY (id) ); + + + + +alter table jrn_periode drop constraint jrn_periode_pk; +create sequence jrn_periode_id_seq; +alter table jrn_periode add id bigint; +alter table jrn_periode alter column id set default nextval('jrn_periode_id_seq'); +update jrn_periode set id=nextval('jrn_periode_id_seq'); +alter table jrn_periode add constraint jrn_periode_pk primary key (id); +alter table jrn_periode add constraint jrn_periode_periode_ledger unique (jrn_def_id,p_id);