From c4f2b6d9dd9cb06ac01f76017f6bf0c65d476ee5 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Sun, 30 Dec 2007 22:20:50 +0000 Subject: [PATCH] Add export CSV - HTML to Bud_Synthese_anc & acc --- html/test.php | 12 +- include/class_bud_synthese_acc.php | 197 +++++++++++++++++++---------- include/class_bud_synthese_anc.php | 175 ++++++++++++++++--------- 3 files changed, 252 insertions(+), 132 deletions(-) diff --git a/html/test.php b/html/test.php index e0c80ac91..738f64081 100644 --- a/html/test.php +++ b/html/test.php @@ -84,13 +84,13 @@ Bud_Synthese_Hypo::test_me(); -require_once ('class_bud_synthese_acc.php'); -Bud_Synthese_Acc::test_me(); +require_once ('class_bud_synthese_group.php'); +Bud_Synthese_Group::test_me(); + require_once ('class_bud_synthese_anc.php'); Bud_Synthese_Anc::test_me(); + */ - - -require_once ('class_bud_synthese_group.php'); -Bud_Synthese_Group::test_me(); +require_once ('class_bud_synthese_acc.php'); +Bud_Synthese_Acc::test_me(); diff --git a/include/class_bud_synthese_acc.php b/include/class_bud_synthese_acc.php index 648014115..8cb540553 100644 --- a/include/class_bud_synthese_acc.php +++ b/include/class_bud_synthese_acc.php @@ -39,12 +39,6 @@ class Bud_Synthese_Acc extends Bud_Synthese { foreach (array('acc_from','acc_to') as $r) { $this->$r=(isset($p_array[$r]))?$p_array[$r]:""; } - // swap po_from and po_to if po_from is > to - if ( $this->acc_from > $this->acc_to ){ - $swap=$this->acc_to; - $this->acc_to=$this->acc_from; - $this->acc_from=$swap; - } } function select_hypo() { @@ -66,7 +60,7 @@ class Bud_Synthese_Acc extends Bud_Synthese { $hypo=new Bud_Hypo($this->cn); $hypo->bh_id=$this->bh_id; $hypo->load(); - $acc_value=make_array($this->cn,'select pcm_val,pcm_val from bud_detail where bh_id='.$this->bh_id." order by pcm_val::text "); + $acc_value=make_array($this->cn,'select distinct pcm_val::text,pcm_val from bud_detail where bh_id='.$this->bh_id." order by pcm_val::text "); $wAcc_from=new widget("select"); $wAcc_from->name="acc_from"; $wAcc_from->value=$acc_value; @@ -92,47 +86,49 @@ class Bud_Synthese_Acc extends Bud_Synthese { return $r; } /*!\brief return all the data (raw format) in a array - * \return Array - -Array -( - [0] => Array + * \return Array key = Account + [6040002] => Array ( - [bc_id] => 4 - [price_unit] => 1.0000 - [bc_code] => FICHE_1 - [bc_description] => - [pcm_val] => 6040002 - [unit] => 1 - [amount] => 1 - [amount_unit] => Array - ( - [79] => 1.0000 - ) - [acc_name] => Loyer [acc_amount] => 0 - ) - - [1] => Array - ( - [bc_id] => 7 - [price_unit] => 1.0000 - [bc_code] => FICHE4 - [bc_description] => - [pcm_val] => 6510 - [unit] => 2 - [amount] => 2 - [amount_unit] => Array + [detail] => Array ( - [79] => 2.0000 + [0] => Array + ( + [bc_id] => 4 + [bc_code] => FICHE_1 + [unit] => 6 + [amount] => 6 + [amount_unit] => Array + ( + [79] => 1.0000 + [80] => 2.0000 + [81] => 3.0000 + [82] => 0.0000 + ) + + ) + + [1] => Array + ( + [bc_id] => 5 + [bc_code] => FICHE_2 + [unit] => 10 + [amount] => 10 + [amount_unit] => Array + ( + [79] => 1.0000 + [80] => 2.0000 + [81] => 3.0000 + [82] => 4.0000 + ) + + ) + ) - [acc_name] => Dotations - [acc_amount] => 0 ) -) * */ function load() { @@ -140,16 +136,19 @@ Array $per_acc=sql_filter_per($this->cn,$this->from,$this->to,'p_id','j_tech_per'); // get all the bud_card.bc_id - $sql="select distinct bc_id,bc_code,bc_description,bc_price_unit ". + $sql="select distinct pcm_val ". " from bud_card join bud_detail using (bc_id) ". - "join poste_analytique using(po_id) where pcm_val >= $1 and ". - "pcm_val <=$2 and bud_card.bh_id=$3"; + "join poste_analytique using(po_id) where pcm_val::text >= $1 and ". + "pcm_val::text <= $2 and bud_card.bh_id=$3"; + $res=ExecSqlParam($this->cn,$sql,array($this->acc_from,$this->acc_to,$this->bh_id)); $aBudCard=pg_fetch_all($res); + echo_debug(__FILE__.':'.__LINE__.'- load','aBudCard',$aBudCard); $array=array(); $cn=DbConnect(dossier::id()); - pg_prepare($cn,"sql_detail","select distinct pcm_val from bud_detail ". - " where bc_id=$1"); + pg_prepare($cn,"sql_detail","select distinct bc_id,bc_code,bc_description,bc_price_unit ". + " from bud_detail join bud_card using (bc_id)". + " where pcm_val=$1"); pg_prepare($cn,"sql_detail_periode","select sum(bdp_amount) as amount,". "p_id from bud_card join bud_detail using (bc_id)". " join bud_detail_periode using (bd_id) ". @@ -159,39 +158,104 @@ Array // foreach card get the detail per pcm_val and periode foreach ($aBudCard as $rBudCard) { $line=array(); - $line['bc_id']=$rBudCard['bc_id']; - $line['price_unit']=$rBudCard['bc_price_unit']; - $line['bc_code']=$rBudCard['bc_code']; - $line['bc_description']=$rBudCard['bc_description']; - $res=pg_execute("sql_detail",array($line['bc_id'])); + echo_debug(__FILE__.':'.__LINE__.'- load ','bud_card',$rBudCard); + $acc_account=new Acc_Account($this->cn,$rBudCard['pcm_val']); + $acc_account->load(); + $line['acc_name']=$acc_account->label; + $line['acc_amount']=$acc_account->get_solde($per_acc); + + $res=pg_execute("sql_detail",array($rBudCard['pcm_val'])); $row=pg_fetch_all($res); + $idx=0; foreach ($row as $col) { - $pcm_val=$col['pcm_val']; - $line['pcm_val']=$pcm_val; + $sub=array(); + echo_debug(__FILE__.':'.__LINE__.'- ','load : pcm_val',$rBudCard['pcm_val']); + $bc_id=$col['bc_id']; + $sub['bc_id']=$bc_id; + $sub['bc_code']=$col['bc_code']; + $sub['bc_description']=$col['bc_description']; + $sub['bc_price_unit']=$col['bc_price_unit']; $periode=array(); - $res2=pg_execute("sql_detail_periode",array($rBudCard['bc_id'],$pcm_val)); + $res2=pg_execute("sql_detail_periode",array($bc_id,$rBudCard['pcm_val'])); $col_per=pg_fetch_all($res2); - $line['unit']=0; + $sub['unit']=0; + // fill the periode array foreach ($col_per as $cPer) { $p_id=$cPer['p_id']; $periode[$p_id]=$cPer['amount']; - $line['unit']+=$cPer['amount']; + $sub['unit']+=$cPer['amount']; } - $line['amount']=$line['unit']*$line['price_unit']; - $line['amount_unit']=$periode; + $sub['amount']=$sub['unit']*$col['bc_price_unit']; + $sub['amount_unit']=$periode; - $acc_account=new Acc_Account($this->cn,$pcm_val); - $acc_account->load(); - $line['acc_name']=$acc_account->label; - $line['acc_amount']=$acc_account->get_solde($per_acc); + $line['detail'][$idx]=$sub; + $idx++; } - $array[]=$line; + $array[$rBudCard['pcm_val']]=$line; } - + echo_debug(__FILE__.':'.__LINE__.'- load ','return ',$array); return $array; } + function display_csv ($p_array){ + if (empty($p_array)) return; + $r=""; + foreach ($p_array as $key=>$value) { + echo_debug(__FILE__.':'.__LINE__.'- display_csv','$key',$key); + echo_debug(__FILE__.':'.__LINE__.'- display_csv','$value',$value); + $r.=sprintf('"%s","%s"',$key,$value['acc_name']); + $r.=sprintf("\r\n"); + foreach ($value['detail'] as $v) { + $r.=sprintf('"%s","%s","%s",' + ,$v['bc_code'], + $v['bc_description'], + $v['bc_price_unit']); + echo_debug(__FILE__.':'.__LINE__.'- display_csv','$value[amount_unit]',$v['amount_unit']); + foreach ($v['amount_unit'] as $a=>$v2 ){ + $r.=sprintf('%10.4f,',$v2); + } + $r.=sprintf('%10.4f,%10.4f',$v['unit'],$v['amount']); + $r.=sprintf("\r\n"); + } + } + return $r; + } + function display_html($p_array) { + $r=""; + if (empty($p_array)) return; + $per=get_array($this->cn,"select to_char(p_start,'MM.YYYY') as d". + " from parm_periode ". + " where p_id between ".$this->from.' and '. + $this->to." order by p_start" ); + + $heading=" CE "; + foreach( $per as $c) { $heading.=''.$c['d'].'';} + $heading.="Total Unite"; + $heading.="Total Cout"; + + foreach ($p_array as $key=>$value) { + echo_debug(__FILE__.':'.__LINE__.'- display_html','$key',$key); + echo_debug(__FILE__.':'.__LINE__.'- display_html','$value',$value); + $r.=$key.'-'.$value['acc_name']; + + $r.=""; + $r.=$heading; + foreach ($value['detail'] as $v) { + $r.=''; + $r.=''; + foreach ($v['amount_unit'] as $a=>$v2) { + $r.=sprintf('',$v2); + } + $r.=sprintf('',$v['unit']); + $r.=sprintf('',$v['amount']); + $r.=''; + } + $r.="
'.$v['bc_code'].' '.$v['bc_description'].'%10.2f% 10.2f% 10.2f
"; + } + + return $r; + } static function test_me() { $cn=DbConnect(dossier::id()); @@ -213,7 +277,12 @@ Array $obj->from_array($_GET); echo 'ICI'; - print_r($obj->load()); + $res=$obj->load(); + print_r($res); + echo '
'; + echo $obj->display_csv($res); + echo '
'; + echo $obj->display_html($res); } } diff --git a/include/class_bud_synthese_anc.php b/include/class_bud_synthese_anc.php index c0b6502a3..a0ac4a623 100644 --- a/include/class_bud_synthese_anc.php +++ b/include/class_bud_synthese_anc.php @@ -39,12 +39,6 @@ class Bud_Synthese_Anc extends Bud_Synthese { foreach (array('po_from','po_to') as $r) { $this->$r=(isset($p_array[$r]))?$p_array[$r]:""; } - // swap po_from and po_to if po_from is > to - if ( $this->po_from > $this->po_to ){ - $swap=$this->po_to; - $this->po_to=$this->po_from; - $this->po_from=$swap; - } } function select_hypo() { @@ -93,70 +87,56 @@ class Bud_Synthese_Anc extends Bud_Synthese { } /*!\brief return all the data (raw format) in a array \return Array structure -ICIArray -( + [0] => Array ( [bc_id] => 4 [price_unit] => 1.0000 [bc_code] => FICHE_1 [bc_description] => - [pcm_val] => 6040002 - [unit] => 25 - [amount] => 25 - [amount_unit] => Array + [detail] => Array ( - [79] => 1.0000 - [80] => 2.0000 - [81] => 3.0000 - [82] => 0.0000 - [83] => 0.0000 - [84] => 0.0000 - [85] => 0.0000 - [86] => 0.0000 - [87] => 9.0000 - [88] => 10.0000 + [0] => Array + ( + [pcm_val] => 6040001 + [unit] => 27 + [amount] => 27 + [amount_unit] => Array + ( + [79] => 13.0000 + [80] => 14.0000 + ) + + [acc_name] => Electricité + [acc_amount] => 0 + ) + + [1] => Array + ( + [pcm_val] => 6040002 + [unit] => 317 + [amount] => 317 + [amount_unit] => Array + ( + [79] => 108.0000 + [80] => 209.0000 + ) + + [acc_name] => Loyer + [acc_amount] => 0 + ) + ) - [acc_name] => Loyer - [acc_amount] => 0 ) - [1] => Array - ( - [bc_id] => 7 - [price_unit] => 1.0000 - [bc_code] => FICHE4 - [bc_description] => - [pcm_val] => 6510 - [unit] => 74 - [amount] => 74 - [amount_unit] => Array - ( - [79] => 2.0000 - [80] => 43.0000 - [81] => 4.0000 - [82] => 5.0000 - [83] => 0.0000 - [84] => 6.0000 - [85] => 7.0000 - [86] => 7.0000 - [87] => 0.0000 - [88] => 0.0000 - ) - - [acc_name] => Dotations - [acc_amount] => 0 - ) - -) */ function load() { $per=sql_filter_per($this->cn,$this->from,$this->to,'p_id','p_id'); $per_acc=sql_filter_per($this->cn,$this->from,$this->to,'p_id','j_tech_per'); // get all the bud_card.bc_id - $sql="select distinct bc_id,bc_price_unit,bc_code,bc_description ". + $sql="select distinct bc_id,bc_price_unit,bc_code,bc_description,bc_unit ". " from bud_card join bud_detail using (bc_id) ". "join poste_analytique using(po_id) where po_name >= $1 and ". "po_name <=$2 and bud_card.bh_id=$3"; @@ -179,34 +159,100 @@ ICIArray $line['price_unit']=$rBudCard['bc_price_unit']; $line['bc_code']=$rBudCard['bc_code']; $line['bc_description']=$rBudCard['bc_description']; + $line['bc_unit']=$rBudCard['bc_unit']; $res=pg_execute("sql_detail",array($line['bc_id'])); $row=pg_fetch_all($res); + $idx=0; foreach ($row as $col) { + $sub=array(); $pcm_val=$col['pcm_val']; - $line['pcm_val']=$pcm_val; + $sub['pcm_val']=$pcm_val; $periode=array(); $res2=pg_execute("sql_detail_periode",array($rBudCard['bc_id'],$pcm_val)); $col_per=pg_fetch_all($res2); - $line['unit']=0; + $sub['unit']=0; foreach ($col_per as $cPer) { $p_id=$cPer['p_id']; $periode[$p_id]=$cPer['amount']; - $line['unit']+=$cPer['amount']; + $sub['unit']+=$cPer['amount']; } - $line['amount']=$line['unit']*$line['price_unit']; - $line['amount_unit']=$periode; + $sub['amount']=$sub['unit']*$line['price_unit']; + $sub['amount_unit']=$periode; $acc_account=new Acc_Account($this->cn,$pcm_val); $acc_account->load(); - $line['acc_name']=$acc_account->label; - $line['acc_amount']=$acc_account->get_solde($per_acc); + $sub['acc_name']=$acc_account->label; + $sub['acc_amount']=$acc_account->get_solde($per_acc); + $line['detail'][$idx]=$sub; + $idx++; } $array[]=$line; } - pg_close($cn); + return $array; } + function display_csv ($p_array){ + if (empty($p_array)) return; + $r=""; + foreach ($p_array as $key=>$value) { + echo_debug(__FILE__.':'.__LINE__.'- display_csv','$key',$key); + echo_debug(__FILE__.':'.__LINE__.'- display_csv','$value',$value); + $r.=sprintf('"%s","%s"',$value['bc_code'],$value['bc_description']); + $r.=sprintf("\r\n"); + + foreach ($value['detail'] as $v) { + $r.=sprintf('"%s","%s",' + ,$v['pcm_val'], + $v['acc_name']); + + foreach ($v['amount_unit'] as $a=>$v2 ){ + $r.=sprintf('%10.4f,',$v2); + } + $r.=sprintf('%10.4f,%10.4f,%10.4f', + $v['unit'],$v['amount'],$v['acc_amount']); + $r.=sprintf("\r\n"); + } + } + return $r; + } + function display_html($p_array) { + $r=""; + if (empty($p_array)) return; + $per=get_array($this->cn,"select to_char(p_start,'MM.YYYY') as d". + " from parm_periode ". + " where p_id between ".$this->from.' and '. + $this->to." order by p_start" ); + $heading=" CE "; + foreach( $per as $c) { $heading.=''.$c['d'].'';} + $heading.="Total Unite"; + $heading.="Total Prix"; + $heading.="Total CE"; + + foreach ($p_array as $key=>$value) { + echo_debug(__FILE__.':'.__LINE__.'- display_html','$key',$key); + echo_debug(__FILE__.':'.__LINE__.'- display_html','$value',$value); + $r.=$value['bc_code'].'-'.$value['bc_description']. + ' PU: '.$value['price_unit'].$value['bc_unit']; + + $r.=""; + $r.=$heading; + foreach ($value['detail'] as $v) { + $r.=''; + $r.=''; + foreach ($v['amount_unit'] as $a=>$v2) { + $r.=sprintf('',$v2); + } + $r.=sprintf('',$v['unit']); + $r.=sprintf('',$v['amount']); + $r.=sprintf('',$v['acc_amount']); + $r.=''; + } + $r.="
'.$v['pcm_val'].' '.$v['acc_name'].'% 10.2f% 10.2f% 10.2f% 10.2f
"; + } + + return $r; + } static function test_me() { @@ -229,7 +275,12 @@ ICIArray $obj->from_array($_GET); echo 'ICI'; - print_r($obj->load()); + $res=$obj->load(); + echo '
'; + echo $obj->display_csv($res); + echo '
'; + + echo $obj->display_html($res); } }