From e3a9d21d6e0e19827adb3ffaec368c3bad93e0d9 Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT Date: Mon, 17 Aug 2009 16:27:53 +0200 Subject: [PATCH] Improving and bug fixing admin/aqbudgets Adding FormatData and FormatNumber in C4::Output Using them Adding duplication detection for budget periods Factorizing some code --- C4/Budgets.pm | 199 ++++++------------ C4/Output.pm | 44 +++- admin/aqbudgetperiods.pl | 161 +++++--------- admin/aqbudgets.pl | 147 ++++--------- .../prog/en/includes/duplicates.inc | 30 +++ .../en/modules/admin/aqbudgetperiods.tmpl | 19 +- .../prog/en/modules/admin/aqbudgets.tmpl | 2 +- t/db_dependent/Budgets.t | 123 +++++++++++ 8 files changed, 355 insertions(+), 370 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/duplicates.inc create mode 100755 t/db_dependent/Budgets.t diff --git a/C4/Budgets.pm b/C4/Budgets.pm index a07afe842a..a78bafe204 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -20,6 +20,7 @@ package C4::Budgets; use strict; use C4::Context; use C4::Dates qw(format_date format_date_in_iso); +use C4::SQLHelper qw<:all>; use C4::Debug; use vars qw($VERSION @ISA @EXPORT); @@ -43,6 +44,7 @@ BEGIN { &GetBudgetPeriod &GetBudgetPeriods &ModBudgetPeriod + &AddBudgetPeriod &DelBudgetPeriod &GetBudgetPeriodsDropbox @@ -51,11 +53,13 @@ BEGIN { &GetBudgetPermDropbox &ModBudgetPlan + &GetCurrency &GetCurrencies &ModCurrencies &ConvertCurrency - &GetBudgetsPlanCell + + &GetBudgetsPlanCell &AddBudgetPlanValue &GetBudgetAuthCats &BudgetHasChildren @@ -74,15 +78,6 @@ sub HideCols { my ( $authcat, @hide_cols ) = @_; my $dbh = C4::Context->dbh; -=c - my $sth = $dbh->prepare( - qq| - UPDATE aqbudgets_planning - SET display = 1 where authcat = ? | - ); - $sth->execute( $authcat ); -=cut - my $sth1 = $dbh->prepare( qq| UPDATE aqbudgets_planning SET display = 0 @@ -129,6 +124,10 @@ sub CheckBudgetParentPerm { return 0; } +sub AddBudgetPeriod { + my ($budgetperiod) = @_; + return InsertInTable("aqbudgetperiods",$budgetperiod); +} # ------------------------------------------------------------------- sub GetPeriodsCount { my $dbh = C4::Context->dbh; @@ -173,7 +172,6 @@ sub BudgetHasChildren { WHERE budget_parent_id = ? | ); $sth->execute( $budget_id ); my $sum = $sth->fetchrow_hashref; - $sth->finish; return $sum->{'sum'}; } @@ -227,7 +225,6 @@ sub GetBudgetsPlanCell { ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR (aqbudgets.sort2_authcat = ? AND sort2 =?)) | ); - $sth->{TraceLevel} = 2; $sth->execute( $cell->{'budget_id'}, $budget->{'sort1_authcat'}, $cell->{'authvalue'}, @@ -310,7 +307,6 @@ sub GetBudgetSpent { $sth->execute($budget_id); my $sum = $sth->fetchrow_array; -# $sum = sprintf "%.2f", $sum; return $sum; } @@ -412,25 +408,9 @@ sub GetBudgetPeriodsDropbox { # ------------------------------------------------------------------- sub GetBudgetPeriods { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(qq| - SELECT * - FROM aqbudgetperiods - ORDER BY budget_period_startdate, budget_period_enddate | - ); - $sth->execute(); - my @results; - my $active; - while (my $data = $sth->fetchrow_hashref) { - if ($data->{'budget_period_active'} == 1) { - $active = $data->{'budget_period_id'}; - } - push(@results, $data); - } - $sth->finish; - return ($active, \@results); + my ($filters,$orderby) = @_; + return SearchInTable("aqbudgetperiods",$filters, $orderby); } - # ------------------------------------------------------------------- sub GetBudgetPeriod { my ($budget_period_id) = @_; @@ -439,7 +419,7 @@ sub GetBudgetPeriod { my $total = 0; ## get information about the record that will be deleted my $sth; - if ($budget_period_id gt 0) { + if ($budget_period_id) { $sth = $dbh->prepare( qq| SELECT * FROM aqbudgetperiods @@ -455,12 +435,11 @@ sub GetBudgetPeriod { $sth->execute(); } my $data = $sth->fetchrow_hashref; - $sth->finish; return $data; } # ------------------------------------------------------------------- -sub DelBudgetPeriod() { +sub DelBudgetPeriod{ my ($budget_period_id) = @_; my $dbh = C4::Context->dbh; ; ## $total = number of records linked to the record that must be deleted @@ -476,34 +455,9 @@ sub DelBudgetPeriod() { } # ------------------------------------------------------------------- -sub ModBudgetPeriod() { +sub ModBudgetPeriod { my ($budget_period_information) = @_; - my $dbh = C4::Context->dbh ; ## $total = number of records linked to the record that must be deleted my $total = 0; - - ## get information about the record that will be deleted - my $budget_period_id=$$budget_period_information{'budget_period_id'}; - delete $$budget_period_information{'budget_period_id'}; - my @values; - my @keys; - - while ( my ($k,$v) = each %$budget_period_information ) { - next if (not $v and $k!~/sort1|note/); - #next if any { $_ eq $k } qw( sort1 note); - push @values, $v; - push @keys, "$k=?"; - } - - my $query = do { local $"=','; - qq{ - UPDATE aqbudgetperiods - SET @keys - WHERE budget_period_id=? - } - }; - - my $sth=$dbh->prepare($query); - my $data = $sth->execute(@values,$budget_period_id); - return $data; + return UpdateInTable("aqbudgetperiods",$budget_period_information); } # ------------------------------------------------------------------- @@ -513,30 +467,30 @@ sub GetBudgetHierarchy { my $dbh = C4::Context->dbh; my $query = qq| SELECT aqbudgets.* - FROM aqbudgets - JOIN aqbudgetperiods USING (budget_period_id) - WHERE budget_period_active=1 |; + FROM aqbudgets |; # show only period X if requested + my @where_strings; if ($budget_period_id) { - $query .= "AND aqbudgets.budget_period_id = ?"; + push @where_strings," aqbudgets.budget_period_id = ?"; push @bind_params, $budget_period_id; } # show only budgets owned by me, my branch or everyone if ($owner) { if ($branchcode) { - $query .= " AND (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL AND budget_owner_id IS NULL))"; - push @bind_params, $owner; - push @bind_params, $branchcode; + push @where_strings,qq{ (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL or budget_branchcode="" AND (budget_owner_id IS NULL OR budget_owner_id=""))}; + push @bind_params, ($owner, $branchcode); } else { - $query .= ' AND budget_owner_id = ? OR budget_owner_id IS NULL'; + push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") '; push @bind_params, $owner; } } else { if ($branchcode) { - $query .= " AND (budget_branchcode =? or budget_branchcode is NULL)"; + push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)"; push @bind_params, $branchcode; } } + $query.=" WHERE ".join(' AND ', @where_strings); + $debug && warn $query,join(",",@bind_params); my $sth = $dbh->prepare($query); $sth->execute(@bind_params); my $results = $sth->fetchall_arrayref({}); @@ -643,58 +597,16 @@ sub GetBudgetHierarchy { } # ------------------------------------------------------------------- -sub AddBudget { -my ($budget) = @_; -my $dbh = C4::Context->dbh; - my @keys; my @values; - - while ( my ($k,$v) = each %$budget ) { - next unless $v; - push @values, $v; - push @keys , "$k = ?"; - } - my $query = do { - local $" = ','; - qq{ - INSERT INTO aqbudgets - SET @keys - }; - }; - - #warn $query; - my $sth = $dbh->prepare($query); - $sth->execute(@values); - return $dbh->{'mysql_insertid'}; +sub AddBudget { + my ($budget) = @_; + return InsertInTable("aqbudgets",$budget); } # ------------------------------------------------------------------- sub ModBudget { my ($budget) = @_; - my $dbh = C4::Context->dbh; - my $budgetid=$$budget{'budgetid'}; - delete $$budget{'budget_id'}; - - my @values; - my @keys; - - while ( my ($k,$v) = each %$budget ) { - next if (not $v and $k!~/sort1|note/); - #next if any { $_ eq $k } qw( sort1 note); - push @values, $v; - push @keys, "$k=?"; - } - - my $query = do { local $"=','; - qq{ - UPDATE aqbudgets - SET @keys - WHERE budget_id=? - }; - }; - - my $sth = $dbh->prepare($query); - $sth->execute( @values,$budgetid); + return UpdateInTable("aqbudgets",$budget); } # ------------------------------------------------------------------- @@ -703,7 +615,6 @@ sub DelBudget { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?"); my $rc = $sth->execute($budget_id); - $sth->finish; return $rc; } @@ -747,7 +658,7 @@ sub GetBudget { =over 4 -&GetBudget($budget_id); +&GetBudgets($filter, $order_by); gets all budgets @@ -757,26 +668,8 @@ gets all budgets # ------------------------------------------------------------------- sub GetBudgets { - my ($active) = @_; - my $dbh = C4::Context->dbh; - my $q = "SELECT * from aqbudgets"; - my $row; - my $sth; - unless ($active) { - $sth = $dbh->prepare($q); - $sth->execute(); - } else { - $q = "select budget_period_id from aqbudgetperiods where budget_period_active = 1 "; - $sth = $dbh->prepare($q); - $sth->execute(); - $row = $sth->fetchrow_hashref(); - $q = "select * from aqbudgets WHERE budget_period_id =? "; - $sth = $dbh->prepare($q); - $sth->execute( $row->{'budget_period_id'} ); - } - my $results = $sth->fetchall_arrayref( {} ); - $sth->finish; - return $results; + my ($filters,$orderby) = @_; + return SearchInTable("aqbudgets",$filters,$orderby); } # ------------------------------------------------------------------- @@ -804,7 +697,6 @@ sub GetCurrencies { while ( my $data = $sth->fetchrow_hashref ) { push( @results, $data ); } - $sth->finish; return @results; } @@ -817,7 +709,6 @@ sub GetCurrency { my $sth = $dbh->prepare($query); $sth->execute; my $r = $sth->fetchrow_hashref; - $sth->finish; return $r; } @@ -871,6 +762,34 @@ sub ConvertCurrency { return ( $price / $cur ); } +=item + returns an array containing fieldname followed by PRI as value if PRIMARY Key +=cut +sub _columns(;$) { + my $tablename=shift||"aqbudgets"; + return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})}; +} + +sub _filter_fields{ + my $budget=shift; + my $tablename=shift; + my @keys; + my @values; + my %columns= _columns($tablename); + #Filter Primary Keys of table + my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns; + foreach my $field (grep {/\b($elements)\b/} keys %$budget){ + $$budget{$field}=format_date_in_iso($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4::Dates->regexp("iso")); + my $strkeys= " $field = ? "; + if ($field=~/branch/){ + $strkeys="( $strkeys OR $field='' OR $field IS NULL) "; + } + push @values, $$budget{$field}; + push @keys, $strkeys; + } + return (\@keys,\@values); +} + END { } # module clean-up code here (global destructor) 1; diff --git a/C4/Output.pm b/C4/Output.pm index 731cb7e39d..fde73f1be0 100644 --- a/C4/Output.pm +++ b/C4/Output.pm @@ -29,6 +29,8 @@ use strict; use C4::Context; use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language ); +use C4::Dates qw(format_date); +use C4::Budgets qw(GetCurrency); use HTML::Template::Pro; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -48,7 +50,7 @@ BEGIN { &themelanguage &gettemplate setlanguagecookie getlanguagecookie pagination_bar ); push @EXPORT, qw( - &output_html_with_http_headers &output_with_http_headers + &output_html_with_http_headers &output_with_http_headers FormatData FormatNumber ); } @@ -216,6 +218,46 @@ sub getlanguagecookie { return $lang; } +=item FormatNumber +=cut +sub FormatNumber{ +my $cur = GetCurrency; +my $cur_format = C4::Context->preference("CurrencyFormat"); +my $num; + +if ( $cur_format eq 'FR' ) { + $num = new Number::Format( + 'decimal_fill' => '2', + 'decimal_point' => ',', + 'int_curr_symbol' => $cur->{symbol}, + 'mon_thousands_sep' => ' ', + 'thousands_sep' => ' ', + 'mon_decimal_point' => ',' + ); +} else { # US by default.. + $num = new Number::Format( + 'int_curr_symbol' => '', + 'mon_thousands_sep' => ',', + 'mon_decimal_point' => '.' + ); +} +return $num; +} + +=item FormatData + +FormatData($data_hashref) +C<$data_hashref> is a ref to data to format + +Format dates of data those dates are assumed to contain date in their noun +Could be used in order to centralize all the formatting for HTML output +=cut + +sub FormatData{ + my $data_hashref=shift; + $$data_hashref{$_} = format_date( $$data_hashref{$_} ) for grep{/date/} keys (%$data_hashref); +} + =item pagination_bar pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name) diff --git a/admin/aqbudgetperiods.pl b/admin/aqbudgetperiods.pl index 8a01fad61f..326385f894 100755 --- a/admin/aqbudgetperiods.pl +++ b/admin/aqbudgetperiods.pl @@ -60,10 +60,10 @@ my $input = new CGI; my $searchfield = $input->param('searchfield'); my $budget_period_id = $input->param('budget_period_id'); -my $budget_period_active = $input->param('budget_period_active'); -my $budget_period_locked = $input->param('budget_period_locked'); -my $op = $input->param('op'); +my $op = $input->param('op')||"else"; +my $check_duplicate = $input->param('confirm_not_duplicate')||0; +my $budget_period_hashref= $input->Vars; #my $sort1_authcat = $input->param('sort1_authcat'); #my $sort2_authcat = $input->param('sort2_authcat'); @@ -80,11 +80,6 @@ my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user( } ); -my $script_name = "/cgi-bin/koha/admin/aqbudgetperiods.pl"; # ??? - -my ( $count, $results ) = GetBudgetPeriods(); -### $count -$template->param( period_button_only => 1 ) if ($count == 0) ; my $cur = GetCurrency(); $template->param( cur => $cur->{symbol} ); @@ -108,96 +103,63 @@ if ( $cur_format eq 'US' ) { ); } -if ($op) { $template->param( $op => 1 ); } -else { $template->param( 'else' => 1 ); } # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN if ( $op eq 'add_form' ) { ## add or modify a budget period (preparation) ## get information about the budget period that must be modified -# my ( $default, $sort1_authcat_dropbox, $sort1_default, $sort2_default ); -# my ( $default, t ); if ($budget_period_id) { # MOD - my $data; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(qq| - SELECT * FROM aqbudgetperiods - WHERE budget_period_id=? | ); - $sth->execute($budget_period_id); - $data = $sth->fetchrow_hashref; - $sth->finish; - + my $budgetperiod_hash=GetBudgetPeriod($budget_period_id); # get dropboxes + FormatData($budgetperiod_hash); + $$budgetperiod_hash{budget_period_total}= $num->format_price($$budgetperiod_hash{'budget_period_total'}); $template->param( - budget_period_id => $budget_period_id, - budget_period_startdate => format_date( $data->{'budget_period_startdate'} ), - budget_period_enddate => format_date( $data->{'budget_period_enddate'} ), - budget_period_description => $data->{'budget_period_description'}, - budget_period_total => sprintf ("%.2f", $data->{'budget_period_total'} ), - budget_period_active => $data->{'budget_period_active'}, - budget_period_locked => $data->{'budget_period_locked'}, + %$budgetperiod_hash ); } # IF-MOD - $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), ); + $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),); + $template->param( confirm_not_duplicate => $check_duplicate ); } elsif ( $op eq 'add_validate' ) { ## add or modify a budget period (confirmation) - ## update budget period data - if ( $budget_period_id ne '' ) { - my $query = ' - UPDATE aqbudgetperiods - SET budget_period_startdate = ? - , budget_period_enddate = ? - , budget_period_description = ? - , budget_period_total = ? - , budget_period_locked = ? - , budget_period_active = ? - WHERE budget_period_id = ? - '; - - my $sth = $dbh->prepare($query); - $sth->execute( - $input->param('budget_period_startdate') ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef, - $input->param('budget_period_enddate') ? format_date_in_iso( $input->param('budget_period_enddate') ) : undef, - $input->param('budget_period_description') ? $input->param('budget_period_description') : undef, - $input->param('budget_period_total') ? $input->param('budget_period_total') : undef, - $input->param('budget_period_locked') ? $input->param('budget_period_locked') : undef, - $input->param('budget_period_active') ? $input->param('budget_period_active') : undef, - $input->param('budget_period_id'), - ); - - } else { # ELSE ITS AN ADD - my $query = " - INSERT INTO aqbudgetperiods ( - budget_period_id - , budget_period_startdate - , budget_period_enddate - , budget_period_total - , budget_period_description - , budget_period_locked - , budget_period_active) - VALUES (?,?,?,?,?,?,? ); - "; - my $sth = $dbh->prepare($query); - $sth->execute( - $budget_period_id, - $input->param('budget_period_startdate') ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef, - $input->param('budget_period_enddate') ? format_date_in_iso( $input->param('budget_period_enddate') ) : undef, - $input->param('budget_period_total') ? $input->param('budget_period_total') : undef, - $input->param('budget_period_description') ? $input->param('budget_period_description') : undef, - $input->param('budget_period_locked') ? $input->param('budget_period_locked') : undef, - $input->param('budget_period_active') ? $input->param('budget_period_active') : undef, - ); - $budget_period_id = $dbh->last_insert_id( undef, undef, 'aqbudgetperiods', undef ); - } - - print "Content-Type: text/html\n\n"; #YUCK - # output_html_with_http_headers $input, $cookie, $template->output; # FIXME: THIS WOULD BE NICER THAN THE PREVIOUS PRINT - exit; + ## update budget period data + if ( $budget_period_id ne '' ) { + $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked); + my $status=ModBudgetPeriod($budget_period_hashref); + } + else { # ELSE ITS AN ADD + unless ($check_duplicate){ + my $candidates=GetBudgetPeriods({ + budget_period_startdate => $$budget_period_hashref{budget_period_startdate} + , budget_period_enddate => $$budget_period_hashref{budget_period_enddate} + }); + if (@$candidates){ + my @duplicates=map{ + { dupid => $$_{budget_period_id} + , duplicateinformation => + $$_{budget_period_description}." ".$$_{budget_period_startdate}." ".$$_{budget_period_enddate} + } + } @$candidates; + $template->param(url => "aqbudgetperiods.pl", + field_name => "budget_period_id", + action_dup_yes_url=> "aqbudgets.pl", + action_dup_no_url => "aqbudgetperiods.pl?op=add_validate", + confirm_not_duplicate => 0 + ); + delete $$budget_period_hashref{budget_period_id}; + $template->param(duplicates=>\@duplicates,%$budget_period_hashref); + $template->param("add_form"=>1); + output_html_with_http_headers $input, $cookie, $template->output; + exit; + } + } + my $budget_period_id=AddBudgetPeriod($budget_period_hashref); + } + $op='else'; } #-------------------------------------------------- @@ -208,39 +170,28 @@ elsif ( $op eq 'delete_confirm' ) { my $total = 0; my $data = GetBudgetPeriod( $budget_period_id); + FormatData($data); + $$data{'budget_period_total'}=$num->format_price( $data->{'budget_period_total'}); $template->param( - budget_period_id => $budget_period_id, - budget_period_startdate => format_date($data->{'budget_period_startdate'}), - budget_period_enddate => format_date($data->{'budget_period_enddate'}), - budget_period_total => $num->format_price( $data->{'budget_period_total'} ) - -# budget_period_active => $data->{'budget_period_active'}, -# budget_period_description => $data->{'budget_period_description'}, -# template => C4::Context->preference('template'), ## ??!? + %$data ); } elsif ( $op eq 'delete_confirmed' ) { ## delete the budget period record - my $dbh = C4::Context->dbh; - my $budget_period_id = uc( $input->param('budget_period_id') ); - my $sth = $dbh->prepare("DELETE FROM aqbudgetperiods WHERE budget_period_id=?"); - $sth->execute($budget_period_id); - $sth->finish; - print "Content-Type: text/html\n\n"; - exit; + my $data = GetBudgetPeriod( $budget_period_id); + DelBudgetPeriod($budget_period_id); + $op='else'; } -else { - # DEFAULT - DISPLAY AQPERIODS TABLE # ------------------------------------------------------------------- # display the list of budget periods - my ( $count, $results ) = GetBudgetPeriods(); + my $results = GetBudgetPeriods(); + $template->param( period_button_only => 1 ) unless (@$results) ; my $page = $input->param('page') || 1; my $first = ( $page - 1 ) * $pagesize; - # if we are on the last page, the number of the last word to display # must not exceed the length of the results array my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, ); @@ -248,10 +199,8 @@ else { my @period_loop; foreach my $result ( @{$results}[ $first .. $last ] ) { my $budgetperiod = $result; - $budgetperiod->{'budget_period_startdate'} = format_date( $budgetperiod->{'budget_period_startdate'} ); - $budgetperiod->{'budget_period_enddate'} = format_date( $budgetperiod->{'budget_period_enddate'} ); + FormatData($budgetperiod); $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} ); - $budgetperiod->{toggle} = ( $toggle++ % 2 eq 0 ? 1 : 0 ); $budgetperiod->{budget_active} = 1; push( @period_loop, $budgetperiod ); } @@ -260,10 +209,8 @@ else { $template->param( budget_period_dropbox => $budget_period_dropbox, period_loop => \@period_loop, -# pagination_bar => pagination_bar( $script_name, -# getnbpages( scalar @{$results}, -# $pagesize ), $page, 'page' ) + pagination_bar => pagination_bar("aqbudgetperiods.pl",getnbpages(scalar(@$results),$pagesize),$page), ); -} +$template->param($op=>1); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/admin/aqbudgets.pl b/admin/aqbudgets.pl index 33c99dab7d..17926c270d 100755 --- a/admin/aqbudgets.pl +++ b/admin/aqbudgets.pl @@ -62,60 +62,24 @@ if ( not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del $op = ''; } -my $cur = GetCurrency; -my $cur_format = C4::Context->preference("CurrencyFormat"); -my $num; - -if ( $cur_format eq 'FR' ) { - $num = new Number::Format( - 'decimal_fill' => '2', - 'decimal_point' => ',', - 'int_curr_symbol' => '', - 'mon_thousands_sep' => ' ', - 'thousands_sep' => ' ', - 'mon_decimal_point' => ',' - ); -} else { # US by default.. - $num = new Number::Format( - 'int_curr_symbol' => '', - 'mon_thousands_sep' => ',', - 'mon_decimal_point' => '.' - ); -} +my $num=FormatNumber; my $script_name = "/cgi-bin/koha/admin/aqbudgets.pl"; -my $budget_id = $input->param('budget_id'); -my $budget_code = $input->param('budget_code'); -my $budget_name = $input->param('budget_name'); -my $budget_amount = $input->param('budget_amount'); -my $budget_amount_sublevel = $input->param('budget_amount_sublevel'); -my $budget_encumb = $input->param('budget_encumb'); -my $budget_expend = $input->param('budget_expend'); -my $budget_notes = $input->param('budget_notes'); -my $sort1_authcat = $input->param('sort1_authcat'); -my $sort2_authcat = $input->param('sort2_authcat'); -my $budget_description = $input->param('budget_description'); -my $budget_branchcode = $input->param('budget_branchcode'); -my $budget_owner_id = $input->param('budget_owner_id'); -my $budget_parent_id = $input->param('budget_parent_id'); +my $budget_hash=$input->Vars; +my $budget_id = $$budget_hash{budget_id}; my $budget_permission = $input->param('budget_permission'); my $budget_period_dropbox = $input->param('budget_period_dropbox'); +#filtering non budget keys +delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash; my $filter_budgetname = $input->param('filter_budgetname'); my $filter_budgetbranch = $input->param('filter_budgetbranch'); # ' ------- get periods stuff ------------------' # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT -my $budget_period_id = $input->param('budget_period_id'); -my $period = GetBudgetPeriod($budget_period_id); -my $budget_period_id = $period->{'budget_period_id'}; -my $budget_period_locked = $period->{'budget_period_locked'}; -my $budget_period_description = $period->{'budget_period_description'}; -my $budget_period_total = $period->{'budget_period_total'}; +my $period = GetBudgetPeriod($$budget_hash{budget_period_id}); $template->param( - budget_period_id => $budget_period_id, - budget_period_locked => $budget_period_locked, - budget_period_description => $budget_period_description, + %$period ); # ------- get periods stuff ------------------ @@ -133,9 +97,9 @@ $template->param( # retrieve branches -my ( $budget, $period, $query, $sth ); +my ( $budget, ); -my $branches = GetBranches; +my $branches = GetBranches($show_mine); my @branchloop2; foreach my $thisbranch (keys %$branches) { my %row = ( @@ -146,7 +110,7 @@ foreach my $thisbranch (keys %$branches) { push @branchloop2, \%row; } -$template->param(auth_cats_loop => GetBudgetAuthCats($budget_period_id) ); +$template->param(auth_cats_loop => GetBudgetAuthCats($$period{budget_period_id}) ); # Used to create form to add or modify a record if ($op eq 'add_form') { @@ -155,17 +119,18 @@ if ($op eq 'add_form') { # if no buget_id is passed then its an add # pass the period_id to build the dropbox - because we only want to show budgets from this period my $dropbox_disabled; - if ( defined $budget_id ) { ### MOD + if (defined $budget_id ) { ### MOD $budget = GetBudget($budget_id); $dropbox_disabled = BudgetHasChildren($budget_id); - my $borrower = &GetMember( $budget->{budget_owner_id} ); + my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} ); $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'}; + $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget; } # build budget hierarchy my %labels; my @values; - my $hier = GetBudgetHierarchy($budget_period_id); + my $hier = GetBudgetHierarchy($$period{budget_period_id}); foreach my $r (@$hier) { $r->{budget_code_indent} =~ s/ /\~/g; # $labels{"$r->{budget_id}"} = $r->{budget_code_indent}; @@ -220,28 +185,13 @@ if ($op eq 'add_form') { # if no buget_id is passed then its an add $template->param( - add_form => 1, + add_validate => 1, dateformat => C4::Dates->new()->visual(), - budget_id => $budget->{'budget_id'}, - budget_parent_id => $budget->{'budget_parent_id'}, - budget_dropbox => $budget_period_dropbox, + budget_dropbox => $budget_period_dropbox, budget_perm_dropbox => $budget_perm_dropbox, - budget_code => $budget->{'budget_code'}, - budget_code_indent => $budget->{'budget_code_indent'}, - budget_name => $budget->{'budget_name'}, - budget_branchcode => $budget->{'budget_branchcode'}, - budget_amount => sprintf("%.2f", $budget->{'budget_amount'}), - budget_amount_sublevel => sprintf("%.2f", $budget->{'budget_amount_sublevel'}), - budget_encumb => $budget->{'budget_encumb'}, - budget_expend => $budget->{'budget_expend'}, - budget_notes => $budget->{'budget_notes'}, - budget_description => $budget->{'budget_description'}, - budget_owner_id => $budget->{'budget_owner_id'}, - budget_owner_name => $budget->{'budget_owner_name'}, - budget_permission => $budget->{'budget_permission'}, - budget_period_id => $budget_period_id, - budget_period_description => $budget_period_description, branchloop_select => \@branchloop_select, + %$period, + %$budget, ); # END $OP eq ADD_FORM #---------------------- DEFAULT DISPLAY BELOW --------------------- @@ -263,39 +213,20 @@ if ($op eq 'add_form') { my $rc = DelBudget($budget_id); } if ( $op eq 'add_validate' ) { - my %budget_hash = ( - budget_id => $budget_id, - budget_parent_id => $budget_parent_id, - budget_period_id => $budget_period_id, - budget_code => $budget_code, - budget_name => $budget_name, - budget_branchcode => $budget_branchcode, - budget_amount => $budget_amount, - budget_amount_sublevel => $budget_amount_sublevel, - budget_encumb => $budget_encumb, - budget_expend => $budget_expend, - budget_notes => $budget_notes, - sort1_authcat => $sort1_authcat, - sort2_authcat => $sort2_authcat, - budget_description => $budget_description, - budget_owner_id => $budget_owner_id, - budget_permission => $budget_permission, - ); - if ( defined $budget_id ) { - ModBudget( \%budget_hash ); + if ( defined $$budget_hash{budget_id} ) { + ModBudget( $budget_hash ); } else { - AddBudget( \%budget_hash ); + AddBudget( $budget_hash ); } } my $branches = GetBranches(); - my $budget_period_dropbox = GetBudgetPeriodsDropbox($budget_period_id ); + my $budget_period_dropbox = GetBudgetPeriodsDropbox($$period{budget_period_id} ); $template->param( budget_period_dropbox => $budget_period_dropbox, budget_id => $budget_id, - budget_period_startdate => $period->{'budget_period_startdate'}, - budget_period_enddate => $period->{'budget_period_enddate'}, + %$period, ); - my $moo = GetBudgetHierarchy($budget_period_id, $template->{param_map}->{'USER_INFO'}[0]->{'branchcode'}, $show_mine?$borrower_id:''); + my $moo = GetBudgetHierarchy($$period{budget_period_id}, C4::Context->userenv->{branchcode}, $show_mine?$borrower_id:''); my @budgets = @$moo; #FIXME my $toggle = 0; @@ -303,16 +234,20 @@ if ($op eq 'add_form') { my $period_total = 0; my ( $period_alloc_total, $base_alloc_total, $sub_alloc_total, $base_spent_total, $base_remaining_total ); + use YAML; + $debug && warn Dump(@budgets); + #This Looks WEIRD to me : should budgets be filtered in such a way ppl who donot own it would not see the amount spent on the budget by others ? + foreach my $budget (@budgets) { # PERMISSIONS - unless($staffflags->{'superlibrarian'} == 1 ) { + unless($staffflags->{'superlibrarian'} % 2 == 1 ) { #IF NO PERMS, THEN DISABLE EDIT/DELETE unless ( $template->{param_map}->{'CAN_user_acquisition_budget_modify'} ) { $budget->{'budget_lock'} = 1; } # check budget permission - if ( $budget_period_locked == 1 ) { + if ( $$period{budget_period_locked} == 1 ) { $budget->{'budget_lock'} = 1; } elsif ( $budget->{budget_permission} == 1 ) { @@ -356,22 +291,16 @@ if ($op eq 'add_form') { $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0; $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0; - $budget->{'budget_amount'} = $num->format_price( $budget->{'budget_amount'} ); - $budget->{'budget_spent'} = $num->format_price( $budget->{'budget_spent'} ); - $budget->{'budget_remaining'} = $num->format_price( $budget->{'budget_remaining'} ); - $budget->{'budget_amount_total'} = $num->format_price( $budget->{'budget_amount_total'} ); - $budget->{'budget_amount_sublevel'} = $num->format_price( $budget->{'budget_amount_sublevel'} ) if defined $budget->{'budget_amount_sublevel'}; - $budget->{'budget_unalloc_sublevel'} = $num->format_price( $budget->{'budget_unalloc_sublevel'} ) if defined $budget->{'budget_unalloc_sublevel'}; + for (grep {/budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){ + $$budget{$_} = $num->format_price( $$budget{$_} ) if defined($$budget{$_}) + } - my $borrower = &GetMember( $budget->{budget_owner_id} ); - $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'}; - $budget->{budget_borrowernumber} = $borrower->{'borrowernumber'}; + my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} ); + $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'}; + $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'}; push( @loop, { %{$budget}, - toggle => $toggle++%2, branchname => $branches->{ $budget->{branchcode} }->{branchname}, - startdate => format_date($budget->{startdate}), - enddate => format_date($budget->{enddate}), } ); } @@ -379,7 +308,7 @@ if ($op eq 'add_form') { $template->param( else => 1, budget => \@loop, - budget_period_total => $num->format_price($budget_period_total), + budget_period_total => $num->format_price($$period{budget_period_total}), period_alloc_total => $num->format_price($period_alloc_total), base_alloc_total => $num->format_price($base_alloc_total), sub_alloc_total => $num->format_price($sub_alloc_total), @@ -387,8 +316,6 @@ if ($op eq 'add_form') { base_remaining_total => $num->format_price($base_remaining_total), period_remaining_total => $num->format_price( $period_alloc_total - $base_alloc_total ), branchloop => \@branchloop2, - cur => $cur->{symbol}, - cur_format => $cur_format, ); } #---- END $OP eq DEFAULT diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/duplicates.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/duplicates.inc new file mode 100644 index 0000000000..32fc105f7b --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/duplicates.inc @@ -0,0 +1,30 @@ +
+ + + +

Duplicate suspected

+

Is this a duplicate of +

+

+
" method="get"> + +
+
+ diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tmpl index fb7b44c643..b3c3e5c5b9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgetperiods.tmpl @@ -119,23 +119,22 @@
-  --> +

Modify budget period

Add budget period

- - -
+ +
- + " /> " />
  1. @@ -190,16 +189,14 @@ - checked /> +
  2. - - checked /> + +
@@ -285,7 +282,7 @@ - + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tmpl index 6ddbf8d9f0..64115d5bcc 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqbudgets.tmpl @@ -347,7 +347,7 @@
- Cancel + Cancel
diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t new file mode 100755 index 0000000000..4d7da5ee74 --- /dev/null +++ b/t/db_dependent/Budgets.t @@ -0,0 +1,123 @@ +use strict; +use warnings; +use Test::More tests=>18; + +BEGIN {use_ok('C4::Budgets') } +use C4::Budgets; +use C4::Dates; + +print "ok 1\n"; +use YAML; + +# +# Budget Periods : +# +my $bpid; +my $budgetperiod; +my $active_period; +my $mod_status; +my $del_status; +ok($bpid=AddBudgetPeriod( + { budget_period_startdate => '2008-01-01' + , budget_period_enddate => '2008-12-31' + , budget_description => "MAPERI"}), + "AddBudgetPeriod with iso dates OK"); + +if (C4::Context->preference('dateformat') eq "metric"){ + ok($bpid=AddBudgetPeriod( + { budget_period_startdate => '01-01-2008' + , budget_period_enddate => '31-12-2008' + , budget_description => "MAPERI"}), + "AddBudgetPeriod returned $bpid"); +} +elsif (C4::Context->preference('dateformat') eq "us"){ + ok($bpid=AddBudgetPeriod( + { budget_period_startdate => '01-01-2008' + , budget_period_enddate => '12-31-2008' + , budget_description => "MAPERI"}), + "AddBudgetPeriod returned $bpid"); +} + +ok($budgetperiod=GetBudgetPeriod($bpid), + "GetBudgetPeriod($bpid) returned ".Dump($budgetperiod)); +ok(!GetBudgetPeriod(0) ,"GetBudgetPeriod(0) returned undef : noactive BudgetPeriod"); +$$budgetperiod{budget_period_active}=1; +ok($mod_status=ModBudgetPeriod($budgetperiod),"ModBudgetPeriod OK"); +ok($active_period=GetBudgetPeriod(0),"GetBudgetPeriod(0) returned".Dump($active_period)); +ok(scalar(GetBudgetPeriods())>0,"GetBudgetPeriods OK");#Should at least return the Budget inserted +ok($del_status=DelBudgetPeriod($bpid),"DelBudgetPeriod returned $del_status"); + +# +# Budget : +# + +# Add A budget Period +if (C4::Context->preference('dateformat') eq "metric"){ +ok($bpid=AddBudgetPeriod( + { budget_period_startdate =>'01-01-2008' + , budget_period_enddate =>'31-12-2008' + , budget_description =>"MAPERI"}), + "AddBudgetPeriod returned $bpid"); +} elsif (C4::Context->preference('dateformat') eq "us"){ +ok($bpid=AddBudgetPeriod( + { budget_period_startdate =>'01-01-2008' + , budget_period_enddate =>'12-31-2008' + , budget_description =>"MAPERI"}), + "AddBudgetPeriod returned $bpid"); +} +else{ +ok($bpid=AddBudgetPeriod( + {budget_period_startdate=>'2008-01-01' + ,budget_period_enddate =>'2008-12-31' + ,budget_description =>"MAPERI" + }), + "AddBudgetPeriod returned $bpid"); + +} +my $budget_id; +ok($budget_id=AddBudget( + { budget_code => "ABCD" + , budget_amount => "123.132" + , budget_name => "Périodiques" + , budget_notes => "This is a note" + , budget_description=> "Serials" + , budget_active => 1 + , budget_period_id => $bpid + } + ), + "AddBudget returned $budget_id"); +#budget_code | varchar(30) | YES | | NULL | | +#| budget_amount | decimal(28,6) | NO | | 0.000000 | | +#| budget_amount_sublevel | decimal(28,6) | YES | | NULL | | +#| budget_id | int(11) | NO | PRI | NULL | | +#| budget_branchcode | varchar(10) | YES | | NULL | | +#| budget_parent_id | int(11) | YES | | NULL | | +#| budget_name | varchar(80) | YES | | NULL | | +#| budget_encumb | decimal(28,6) | YES | | 0.000000 | | +#| budget_expend | decimal(28,6) | YES | | 0.000000 | | +#| budget_notes | mediumtext | YES | | NULL | | +#| budget_description | mediumtext | YES | | NULL | | +#| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | | +#| budget_period_id | int(11) | YES | MUL | NULL | | +#| sort1_authcat | varchar(80) | YES | | NULL | | +#| sort2_authcat | varchar(80) | YES | | NULL | | +#| budget_owner_id | int(11) | YES | | NULL | | +#| budget_permission | int(1) | YES | | 0 | | + +my $budget; +ok($budget=GetBudget($budget_id) ,"GetBudget OK"); +$$budget{budget_permission}=1; +ok($mod_status=ModBudget($budget),"ModBudget OK"); +ok(GetBudgets()>0, + "GetBudgets OK"); +ok(GetBudgets({budget_period_id=>$bpid})>0, + "GetBudgets With Filter OK"); +ok(GetBudgets({budget_period_id=>$bpid},{"budget_description"=>0})>0, + "GetBudgets With Order OK"); +ok(GetBudgets({budget_period_id=>GetBudgetPeriod(0)->{budget_period_id}},{"budget_description"=>0})>0, + "GetBudgets With Order + Getting Active budgetPeriod OK"); +ok($del_status=DelBudget($budget_id), + "DelBudget returned $del_status"); + + -- 2.20.1