# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA 02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
+#use warnings; FIXME - Bug 2505
use CGI;
use List::Util qw/min/;
use Number::Format qw(format_price);
use C4::Dates qw/format_date format_date_in_iso/;
use C4::Auth;
use C4::Acquisition;
-use C4::Budgets; # 1ST ADD
+use C4::Budgets; #
use C4::Members; # calls GetSortDetails()
use C4::Context;
use C4::Output;
my $dbh = C4::Context->dbh;
my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
- { template_name => "admin/aqbudgets.tmpl",
- query => $input,
- type => "intranet",
- authnotrequired => 0,
- flagsrequired => { acquisition => 'budget_manage' },
- debug => 0,
- }
+ { template_name => "admin/aqbudgets.tmpl",
+ query => $input,
+ type => "intranet",
+ authnotrequired => 0,
+ flagsrequired => { acquisition => 'budget_manage' },
+ debug => 0,
+ }
);
+my $cur = GetCurrency();
+$template->param( symbol => $cur->{symbol},
+ currency => $cur->{currency}
+ );
+
my $op = $input->param('op');
# see if the user want to see all budgets or only owned ones
my $show = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
$show_mine = $input->param('show_mine') if $show == 1;
-
# IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
-if ( not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del'} && $op == 'add_form' ) {
+if ( not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'} && $op == 'add_form' ) {
$op = '';
}
+my $num=FormatNumber;
-my $count = GetPeriodsCount();
-$template->param( period_button_only => 1 ) if $count == 0 ;
-my $cur = GetCurrency;
-my $cur_format = C4::Context->preference("CurrencyFormat");
-my $num;
-
-=c
-# TODO: at some stage this currency handling should be done properly
-with locales and POSIX.pm - and a proper 'locales' lookup and syspref
-
- setlocale( LC_MONETARY , "french");
- my $num = new Number::Format();
- my $formatted_number = $num->format_price('123456789.123'):
-
-=cut
-
-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 $script_name = "/cgi-bin/koha/admin/aqbudgets.pl";
+my $budget_hash = $input->Vars;
+my $budget_id = $$budget_hash{budget_id};
+my $budget_permission = $input->param('budget_permission');
+my $filter_budgetbranch = $input->param('filter_budgetbranch');
+#filtering non budget keys
+delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
+my $filter_budgetname = $input->param('filter_budgetname');
+$template->param(
+ notree => ($filter_budgetbranch or $show_mine)
+);
# ' ------- 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);
-
-# authcats_loop populates the YUI planning button
-my @auth_cats_loop = GetBudgetAuthCats();
-
-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,
- auth_cats_loop => \@auth_cats_loop,
+ %$period
);
# ------- get periods stuff ------------------
-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_permission = $input->param('budget_permission');
-my $budget_period_dropbox = $input->param('budget_period_dropbox');
-my $filter_budgetname = $input->param('filter_budgetname');
-my $filter_budgetbranch = $input->param('filter_budgetbranch');
-
-my $pagesize = 20; # del
-
-# USED FOR PERMISSION COMPARSION LATER
-my $borrower_id = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
+# USED FOR PERMISSION COMPARISON LATER
+my $borrower_id = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
my $user = GetMemberDetails($borrower_id);
my $user_branchcode = $user->{'branchcode'};
$op || else => 1,
);
-my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
-$sthtemp->execute($borrowernumber);
-my ( $flags, $homebranch ) = $sthtemp->fetchrow;
-my ( $budget, $period, $query, $sth );
+# retrieve branches
+my ( $budget, );
-my $branches = GetBranches;
+my $branches = GetBranches($show_mine);
my @branchloop2;
foreach my $thisbranch (keys %$branches) {
- my %row = (
- value => $thisbranch,
- branchname => $branches->{$thisbranch}->{'branchname'},
- );
- $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
- push @branchloop2, \%row;
+ my %row = (
+ value => $thisbranch,
+ branchname => $branches->{$thisbranch}->{'branchname'},
+ );
+ $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
+ push @branchloop2, \%row;
}
-#$template->param(
-# budget_id => $budget->{'budget_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') {
#### ------------------- ADD_FORM -------------------------
-
-#---- if primkey exists, it's a modify action, so read values to modify...
-# pass the period_id to build the dropbox - because we only want to show budgets from this period
-
+ # 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;
}
- my $sort1_authcat_dropbox = GetAuthcatDropbox( 'sort1_authcat', $budget->{'sort1_authcat'} );
- my $sort2_authcat_dropbox = GetAuthcatDropbox( 'sort2_authcat', $budget->{'sort2_authcat'} );
-
+ # 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};
}
push @values, '';
# if no buget_id is passed then its an add
- my $budget_parent_dropbox;
- my $budget_parent_id = $budget->{'budget_parent_id'} if $budget;
- $budget_parent_dropbox = CGI::scrolling_list(
- -name => 'budget_parent_id',
- -values => \@values,
- -default => $budget_parent_id ? $budget_parent_id : undef,
- -size => 10,
- -style => "min-width:100px;",
- -labels => \%labels,
- );
+ my $budget_parent;
+ my $budget_parent_id;
+ if ($budget){
+ $budget_parent_id = $budget->{'budget_parent_id'} ;
+ }else{
+ $budget_parent_id = $input->param('budget_parent_id');
+ }
+ $budget_parent = GetBudget($budget_parent_id);
+ # build branches select
my $branches = GetBranches;
my @branchloop_select;
- foreach my $thisbranch ( keys %$branches ) {
+ foreach my $thisbranch ( sort keys %$branches ) {
my %row = (
value => $thisbranch,
branchname => $branches->{$thisbranch}->{'branchname'},
$row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
push @branchloop_select, \%row;
}
+
+ # populates the YUI planning button
+ my $categories = GetAuthorisedValueCategories();
+ my @auth_cats_loop1 = ();
+ foreach my $category (@$categories) {
+ my $entry = { category => $category,
+ selected => $budget->{sort1_authcat} eq $category ?1:0,
+ };
+ push @auth_cats_loop1, $entry;
+ }
+ my @auth_cats_loop2 = ();
+ foreach my $category (@$categories) {
+ my $entry = { category => $category,
+ selected => $budget->{sort2_authcat} eq $category ?1:0,
+ };
+ push @auth_cats_loop2, $entry;
+ }
+ $template->param(authorised_value_categories1 => \@auth_cats_loop1);
+ $template->param(authorised_value_categories2 => \@auth_cats_loop2);
+
+ if($budget->{'budget_permission'}){
+ my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
+ $template->param($budget_permission => 1);
+ }
-# $period = GetBudgetPeriod($budget->{'budget_period_id'});
-# $budget_period_description = $period->{'budget_period_description'};
-
- my $budget_perm_dropbox =
- GetBudgetPermDropbox($budget->{'budget_permission'});
-
- # if no buget_id is passed then its an add
- $template->param(
- add_form => 1,
- dateformat => C4::Dates->new()->visual(),
- budget_id => $budget->{'budget_id'},
- budget_parent_id => $budget->{'budget_parent_id'},
- budget_parent_dropbox => $budget_parent_dropbox,
- sort1_authcat_dropbox => $sort1_authcat_dropbox,
- sort2_authcat_dropbox => $sort2_authcat_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,
- );
- # END $OP eq ADD_FORM
-#---------------------- DEFAULT DISPLAPY BELOW ---------------------
+ # if no buget_id is passed then its an add
+ $template->param(
+ add_validate => 1,
+ dateformat => C4::Dates->new()->visual(),
+ budget_parent_id => $budget_parent->{'budget_id'},
+ budget_parent_name => $budget_parent->{'budget_name'},
+ branchloop_select => \@branchloop_select,
+ %$period,
+ %$budget,
+ );
+ # END $OP eq ADD_FORM
+#---------------------- DEFAULT DISPLAY BELOW ---------------------
# called by default form, used to confirm deletion of data in DB
} elsif ($op eq 'delete_confirm') {
my $budget = GetBudget($budget_id);
- $template->param(
- budget_id => $budget->{'budget_id'},
- budget_code => $budget->{'budget_code'},
- budget_name => $budget->{'budget_name'},
- budget_amount => $num->format_price( $budget->{'budget_amount'} ),
- );
- # END $OP eq DELETE_CONFIRM
+ $template->param(
+ budget_id => $budget->{'budget_id'},
+ budget_code => $budget->{'budget_code'},
+ budget_name => $budget->{'budget_name'},
+ budget_amount => $num->format_price( $budget->{'budget_amount'} ),
+ );
+ # END $OP eq DELETE_CONFIRM
# called by delete_confirm, used to effectively confirm deletion of data in DB
-} else {
+} else{
if ( $op eq 'delete_confirmed' ) {
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 );
+ }elsif( $op eq 'add_validate' ) {
+ 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 );
- $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'},
- );
- my $moo = GetBudgetHierarchy($budget_period_id, $template->{param_map}->{'USER_INFO'}[0]->{'branchcode'}, $show_mine?$borrower_id:'');
+ $template->param(
+ budget_id => $budget_id,
+ %$period,
+ );
+
+ my $moo = GetBudgetHierarchy($$period{budget_period_id}, C4::Context->userenv->{branchcode}, $show_mine?$borrower_id:'');
my @budgets = @$moo; #FIXME
my $toggle = 0;
my @loop;
my $period_total = 0;
- my ( $period_alloc_total, $base_alloc_total, $sub_alloc_total, $base_spent_total, $base_remaining_total );
+ my ( $period_alloc_total, $base_spent_total );
+
+ #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) {
+ #Level and sublevels total spent
+ $budget->{'total_levels_spent'} = GetChildBudgetsSpent($budget->{"budget_id"});
# 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'} ) {
+ unless ( $template->{VARS}->{'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 ) {
## TOTALS
# adds to total - only if budget is a 'top-level' budget
$period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
- $base_alloc_total += $budget->{'budget_amount'};
- $sub_alloc_total += $budget->{'budget_amount_sublevel'};
$base_spent_total += $budget->{'budget_spent'};
- $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'};
- $base_remaining_total += $budget->{'budget_remaining'};
+ $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'total_levels_spent'};
# if amount == 0 dont display...
delete $budget->{'budget_unalloc_sublevel'} if $budget->{'budget_unalloc_sublevel'} == 0 ;
- delete $budget->{'budget_amount_sublevel'} if $budget->{'budget_amount_sublevel'} == 0 ;
$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'};
-
- my $borrower = &GetMember( $budget->{budget_owner_id} );
- $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
- $budget->{budget_borrowernumber} = $borrower->{'borrowernumber'};
+ for (grep {/total_levels_spent|budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
+ $budget->{$_} = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
+ }
+
+ # Value of budget_spent equals 0 instead of undefined value
+ $budget->{"budget_spent"} = $num->format_price(0) unless defined($budget->{"budget_spent"});
+
+ my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
+ $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
+ $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
+
+ #Make a list of parents of the bugdet
+ my @budget_hierarchy;
+ push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
+ my $parent_id = $budget->{"budget_parent_id"};
+ while ($parent_id) {
+ my $parent = GetBudget($parent_id);
+ push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
+ $parent_id = $parent->{"budget_parent_id"};
+ }
+ push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
+ @budget_hierarchy = reverse(@budget_hierarchy);
push( @loop, { %{$budget},
- toggle => $toggle++%2,
branchname => $branches->{ $budget->{branchcode} }->{branchname},
- startdate => format_date($budget->{startdate}),
- enddate => format_date($budget->{enddate}),
+ budget_hierarchy => \@budget_hierarchy,
}
);
}
+ my $budget_period_total;
+ if ( $period->{budget_period_total} ) {
+ $budget_period_total =
+ $num->format_price( $period->{budget_period_total} );
+ }
$template->param(
else => 1,
budget => \@loop,
- budget_period_total => $num->format_price($budget_period_total),
+ budget_period_total => $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),
base_spent_total => $num->format_price($base_spent_total),
- 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,
- # pagination_bar => pagination_bar(
- # $script_name, getnbpages(scalar @results, $pagesize),
- # $page, 'page' ),
);
} #---- END $OP eq DEFAULT