diff --git a/html/bud_ajax.php b/html/bud_ajax.php index 59e2f65e1..ca2c57606 100644 --- a/html/bud_ajax.php +++ b/html/bud_ajax.php @@ -22,42 +22,49 @@ /*!\file * \brief Ajax action for budget remove or save record + *\todo the security must be added here * */ - //print_r($_POST); extract ($_POST); +require_once ('class_dossier.php'); +require_once ('class_bud_data.php'); +require_once ('debug.php'); + +echo_debug(__FILE__.':'.__LINE__,' $POST ',$_POST); -require_once ('class_bud_detail.php'); $cn=DbConnect(dossier::id()); -if ( $action == 'delete') { - if ( $bd_id == 0 ) return; - $obj=new Bud_Detail($cn,$bd_id); - $obj->delete(); +if ( $_POST['action'] == 'delete') { + if ( $bd_id != 0 ) { + $obj=new Bud_Data($cn); + $obj->bd_id=$bd_id; + $obj->delete_by_bd_id(); + } + header("Content-type: application/json charset=\"ISO8859-1\"",true); + echo '{"bd_id":"0","form_id":"'.$form_id.'"}'; + +} + +if ( $_POST['action'] == 'save' ) { + $a=$_POST; + $a['bc_id']=${"bc_id".$form_id}; + $a['pcm_val']=${"account_".$form_id."_hidden"}; + + $obj=new Bud_Data($cn); + $obj->get_from_array($a); + + if ( $bd_id == 0 ) { + $obj->add(); + } else { + $obj->update(); + + } header("Content-type: application/json charset=\"ISO8859-1\"",true); echo '{"bd_id":"'.$obj->bd_id.'","form_id":"'.$form_id.'"}'; - } +} -if ( $action == 'save' ) { - if ( $bd_id == 0 ) { - $obj=new Bud_Detail($cn); - $obj->po_id=$po_id; - $obj->bc_id=${"bc_id".$form_id}; - $obj->pcm_val=${"account_".$form_id."_hidden"}; - $obj->bh_id=$bh_id; - $obj->add(); - header("Content-type: application/json charset=\"ISO8859-1\"",true); - echo '{"bd_id":"'.$obj->bd_id.'","form_id":"'.$form_id.'"}'; - exit() - } else { - $obj=new Bud_Detail($cn); - $obj->po_id=$po_id; - $obj->bc_id=${"bc_id".$form_id}; - $obj->pcm_val=${"account_".$form_id."_hidden"}; - $obj->bh_id=$bh_id; - $obj->update(); - } - - } + + + diff --git a/html/js/bud_script.js b/html/js/bud_script.js index 4dfa9c522..b95f60c55 100644 --- a/html/js/bud_script.js +++ b/html/js/bud_script.js @@ -50,15 +50,21 @@ function bud_form_enable(p_ctl) { $('button_save'+p_ctl).show(); $('button_change'+p_ctl).hide(); $('button_delete'+p_ctl).show(); + $('button_escape'+p_ctl).show(); } +function bud_form_disable(p_ctl) { + $("form_"+p_ctl).disable(); + $('button_save'+p_ctl).hide(); + $('button_change'+p_ctl).show(); + $('button_delete'+p_ctl).hide(); + $('button_escape'+p_ctl).hide(); + +} + function bud_form_save(p_ctl,p_sessid,p_dossier) { query=$("form_"+p_ctl).serialize(); query+="&action=save"; - $("form_"+p_ctl).disable(); - - $('button_change'+p_ctl).show(); - $('button_save'+p_ctl).hide(); - $('button_delete'+p_ctl).hide(); + bud_form_disable(p_ctl); bud_action(query,p_ctl); } function bud_form_delete(p_ctl,p_sessid,p_dossier) { @@ -66,17 +72,15 @@ function bud_form_delete(p_ctl,p_sessid,p_dossier) { query=$("form_"+p_ctl).serialize(); query+="&action=delete"; var elt=$("form_"+p_ctl).getElements(); - alert (elt); + // alert (elt); for (i=0;i < elt.length;i++) { if ( elt[i].name.search(/amount_/)!= -1 ) { elt[i].value="0"; } if ( elt[i].name.search(/account_/) != -1 ) { elt[i].value=" ";} + if ( elt[i].name.search(/bd_id/) != -1 ) { elt[i].value="0";} } - $('button_change'+p_ctl).show(); - $('button_save'+p_ctl).hide(); - $('button_delete'+p_ctl).hide(); - $("form_"+p_ctl).disable(); + bud_form_disable(p_ctl); bud_action(query,p_ctl); } function display_error(request) { @@ -84,13 +88,15 @@ function display_error(request) { } function display_success(request,json) { - alert ('ok'); var answer=request.responseText.evalJSON(true); var form=$('form_'+answer.form_id); var elt=form.getElements(); for (i = 0;i < elt.length;i++) { - if ( elt[i].name.search(/bd_id/) != -1 ) {elt[i].value=answer.bd_id;} + if ( elt[i].name.search(/bd_id/) != -1 ) { + + elt[i].value=answer.bd_id; + } } diff --git a/html/test.php b/html/test.php index 3fae38ace..0adc94b83 100644 --- a/html/test.php +++ b/html/test.php @@ -67,7 +67,13 @@ Bud_Card::testme(); require_once('class_bud_hypo.php'); Bud_Hypo::testme(); + + +require_once ('class_bud_detail_periode.php'); +Bud_Detail_Periode::test_me(); + */ + require_once ('class_bud_data.php'); Bud_Data::test_me(); diff --git a/include/class_bud_data.php b/include/class_bud_data.php index 699ebd620..e24d81d88 100644 --- a/include/class_bud_data.php +++ b/include/class_bud_data.php @@ -30,6 +30,9 @@ require_once ('postgres.php'); require_once ('constant.php'); require_once ('debug.php'); require_once ('ac_common.php'); +require_once ('class_bud_detail.php'); +require_once ('class_bud_detail_periode.php'); + class Bud_Data { @@ -37,29 +40,30 @@ class Bud_Data { var $bc_id; var $bd_id; var $pcm_val; - var $bdp_id; + var $amount; var $cn; - function __construct($p_cn,$p_bh_id,$p_po_id) { - echo "constructor"; + var $bud_detail_periode; + + function __construct($p_cn,$p_bh_id=0,$p_po_id=0) { $this->cn=$p_cn; $this->bh_id=$p_bh_id; $this->po_id=$p_po_id; - /* + - pg_prepare($this->cn,"sql_periode",$sql_periode); - */ + + } function load() { $sql=" select bd_id,bc_id,bc_code,bd.bh_id,bd.bh_name,". - " tmp_pcmn.pcm_val,pcm_lib,bud_detail_periode.bdp_id ". - " from bud_detail join bud_detail_periode using (bd_id)". + " tmp_pcmn.pcm_val as pcm_val,pcm_lib ". + " from bud_detail ". " join bud_hypothese as bd using (bh_id) ". " join bud_card using (bc_id) ". " join tmp_pcmn using (pcm_val) ". " where bd.bh_id=".$this->bh_id. " and po_id=".$this->po_id; - + echo_debug(__FILE__.':'.__LINE__.'- load',' SQL ',$sql); $res1=ExecSql($this->cn,$sql); $array=pg_fetch_all($res1); @@ -67,10 +71,8 @@ class Bud_Data { $sql_periode="select coalesce(bdp_amount,0) as bdp_amount,a.p_id as p_id ". " from bud_detail_periode left join parm_periode as a using (p_id) ". - " where bdp_id=$1 order by p_start,p_end "; + " where bd_id=$1 order by p_start,p_end "; - print_r($sql_periode); - $ret=array(); @@ -79,24 +81,36 @@ class Bud_Data { foreach ($array as $row) { $obj=new Bud_Data($this->cn,$this->bh_id,$this->po_id); $obj->load_from_array($row); - - $res2=ExecSqlParam($this->db,"sql_periode",array($row['bdp_id'])); - $per=pg_fetch_all($r); + echo_debug(__FILE__,__LINE__," load "); + echo_debug(__FILE__,__LINE__,$obj); + try { + $arg=array($obj->bd_id); + $res2=ExecSqlParam($this->cn,$sql_periode,$arg); + $per=pg_fetch_all($res2); + } catch (Exception $e) { + echo __FILE__.__LINE__."Erreur lors du chargement "; + echo_debug(__FILE__,__LINE__,$arg); + echo_debug(__FILE__,__LINE__,$e); + echo $e->getMessage(); + exit(); + } foreach ($per as $row2) { - $p_id=$row['p_id']; - $obj->amount[$p_id]=$row['bdp_amount']; + echo_debug(__FILE__.':'.__LINE__.'- row','',$row2); + $p_id=$row2['p_id']; + $obj->amount[$p_id]=$row2['bdp_amount']; } } - $ret=clone $obj; + $ret[]=clone $obj; } + echo_debug(__FILE__.':'.__LINE__,'Return load',$ret); return $ret; } function create_empty_row() { $ret=new Bud_Data($this->cn,$this->bh_id,$this->po_id); $ret->bd_id=0; - $ret->bdp_id=0; + $ret->pcm_lib=""; // populate the periode with 0 $res_empty=get_array($this->cn, "select 0,p_id from parm_periode order by p_start,p_end"); @@ -108,19 +122,21 @@ class Bud_Data { } private function load_from_array($p_array) { - foreach (array('bd_id','bh_id','bc_id','pcm_val','bdp_id','pcm_lib','bh_name','bc_code') as $key) { + foreach (array('bd_id','bh_id','bc_id','pcm_val','pcm_lib','bh_name','bc_code') as $key) { $this->{$key}=$p_array[$key]; } } function form() { $r=""; $array=$this->load(); + echo_debug(__FILE__.':'.__LINE__,'form : $array',$array); $a=0; if ( ! empty ($array) ) { foreach ($array as $row) { $a++; $style=($a%2==0)?"even":"odd"; - $r.=$this->create_row($style); + echo_debug(__FILE__.':'.__LINE__,'function form :row ',$row); + $r.=$row->create_row($style); } } for ($i=count($array);$i< MAX_BUD_DETAIL;$i++) { @@ -137,6 +153,7 @@ class Bud_Data { private function create_row($p_style="odd") { static $p_number=0; + echo_debug(__FILE__.':'.__LINE__.'- create_row','',$this); $p_number++; $tot=0; @@ -147,7 +164,7 @@ class Bud_Data { $wAccount=new widget('js_bud_search_poste'); $wAccount->table=0; $wAccount->disabled=true; - + $wAccount->value=$this->pcm_val.' - '.$this->pcm_lib; $wBudCard=new widget('select'); $wBudCard->value=$this->load_bud_card(); $wBudCard->selected=$this->bc_id; @@ -161,18 +178,18 @@ class Bud_Data { $r.=widget::hidden('po_id',$this->po_id); $r.=widget::hidden('bh_id',$this->bh_id); $r.=widget::hidden('bd_id',$this->bd_id); - $r.=widget::hidden('bdp_id',$this->bdp_id); $r.=widget::hidden('form_id',$p_number); $r.="Compte d'exploitation ".$wAccount->IOValue('account_'.$p_number); - + // $r.=widget::hidden('account_'.$p_number.'_hidden',$this->pcm_val); $r.="Fiche Budget ".$wBudCard->IOValue('bc_id'.$p_number); $r.='Total : '.$tot.' '; $r.=''; $r.=$this->header_table(); $r.=" "; foreach ($this->amount as $p_id=>$amount){ + echo_debug(__FILE__.':'.__LINE__,' p_id '.$p_id.' - amount '.$amount); $tot+=$amount; $r.='"; } @@ -185,6 +202,11 @@ class Bud_Data { $button_change->javascript='bud_form_enable('.$p_number.')'; $r.=$button_change->IOValue('button_change'.$p_number); + $button_escape=new widget('button','Echapper'); + $button_escape->javascript='bud_form_disable('.$p_number.')'; + $button_escape->extra='style="display:none"'; + $r.=$button_escape->IOValue('button_escape'.$p_number); + $button_save=new widget('button','Sauve'); $button_save->javascript='bud_form_save('.$p_number.')'; $button_save->extra='style="display:none"'; @@ -198,6 +220,7 @@ class Bud_Data { $r.="
"; return $r; } + private function load_bud_card() { if ( !isset ($this->array_bud_card) ) $this->array_bud_card=make_array($this->cn, @@ -219,6 +242,70 @@ class Bud_Data { } return $this->header; } + + function get_from_array($p_array) { + foreach (array('bd_id','po_id','bc_id','pcm_val','bh_id') as $attr) { + if ( isset ($p_array[$attr])) { + $this->$attr=$p_array[$attr]; + } + } + + $this->get_form_detail_periode($p_array); + echo_debug(__FILE__.':'.__LINE__.'- get_from_array','',$this); + } + function add() { + $r=$this->extract_bud_detail(); + $r->add(); + $this->bd_id=$r->bd_id; + + foreach ( $this->bud_detail_periode as $r ) { + $r->bd_id=$this->bd_id; + $r->add(); + } + + } + + function extract_bud_detail() { + foreach ( array('bd_id','po_id','bc_id','pcm_val','bh_id') as $r1) { + $a[$r1]=$this->$r1; + } + $r=new Bud_Detail($this->cn); + + $r->get_from_array($a); + return $r; + } + + function update() { + $r=$this->extract_bud_detail(); + $r->update(); + ExecSql($this->cn,"delete from bud_detail_periode where bd_id =".$this->bd_id); + echo_debug(__FILE__.':'.__LINE__.'- update ',$this->bud_detail_periode); + foreach ( $this->bud_detail_periode as $obj ) { + $obj->add(); + } + + } + function delete_by_bd_id() { + ExecSql($this->cn,'delete from bud_detail where bd_id='.$this->bd_id); + } + private function get_form_detail_periode($p_array){ + echo_debug(__FILE__.':'.__LINE__.'- get_form_detail_periode arg:','',$p_array); + extract ($p_array); + $periode=get_array($this->cn,"select p_id from parm_periode"); + + foreach ($periode as $key=>$value ) { + $row=$value['p_id']; + + if ( isset (${'amount_'.$row} )) { + $obj=new Bud_Detail_Periode($this->cn); + $obj->p_id=$row; + $obj->bd_id=$this->bd_id; + $obj->bdp_amount=${'amount_'.$row}; + $this->bud_detail_periode[]=clone $obj; + } + + } + } static function test_me() { echo JS_PROTOTYPE_JS; echo JS_BUD_SCRIPT; diff --git a/include/class_bud_detail.php b/include/class_bud_detail.php index c8de6a7a1..27c182fbc 100644 --- a/include/class_bud_detail.php +++ b/include/class_bud_detail.php @@ -102,6 +102,11 @@ class Bud_Detail { " right join parm_periode using (p_id) ". " where bh_id= $bh_id $pa_filter "; } + function get_from_array($p_array) { + foreach (array('bd_id','po_id','bc_id','pcm_val','bh_id') as $attr) + if ( isset ( $p_array[$attr] )) + $this->$attr=$p_array[$attr]; + } static function test_me() { $cn=DbConnect(dossier::id()); $a=new Bud_Detail($cn); diff --git a/include/class_bud_detail_period.php b/include/class_bud_detail_period.php deleted file mode 100644 index 82da53f34..000000000 --- a/include/class_bud_detail_period.php +++ /dev/null @@ -1,33 +0,0 @@ -cn=$p_cn; + $this->bdp_id=0; + $this->p_id=0; + $this->bdp_amount=0; + } + + function delete () { + $sql="delete from bud_detail_periode where bdp_id=".$this->bdp_id; + ExecSql($this->cn,$sql); + } + + function add(){ + $sql="insert into bud_detail_periode(bdp_amount,p_id,bd_id) values ($1,$2,$3)". + " returning bdp_id"; + $value=array($this->bdp_amount,$this->p_id,$this->bd_id); + try { + $r=ExecSqlParam($this->cn,$sql,$value); + $this->bdp_id=pg_fetch_result($r,0,0); + } catch (Exception $e) { + echo "Erreur : ".$e->getMessage(); + } + } + + static function test_me() { + $cn=DbConnect(dossier::id()); + + print_r($cn); + + $obj=new Bud_Detail_Periode($cn); + + $obj->bdp_amount = 12; + $obj->p_id=54; + $obj->bd_id=10; + $obj->add(); + print_r($obj); + echo '
'; + } +} + + diff --git a/include/constant.php b/include/constant.php index bf3677804..98ecbf0c0 100644 --- a/include/constant.php +++ b/include/constant.php @@ -36,7 +36,7 @@ define ("domaine",""); define ("MAX_COMPTE",4); define ('MAX_BUD_DETAIL',3); -define ("DEBUG","false"); +define ("DEBUG","true"); // securite correspond a la table // action
'.$wAmount->IOValue('amount_'.$p_id,sprintf("%08.2f",$amount))."