3 #script to administer the aqbudget table
5 # Copyright 2008-2009 BibLibre SARL
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 use List::Util qw/min/;
25 use Number::Format qw(format_price);
27 use C4::Auth qw/get_user_subpermissions/;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
33 use C4::Members; # calls GetSortDetails()
38 #use POSIX qw(locale_h);
41 my $dbh = C4::Context->dbh;
43 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
44 { template_name => "admin/aqbudgets.tmpl",
48 flagsrequired => { acquisition => 'budget_manage' },
53 my $op = $input->param('op');
55 # see if the user want to see all budgets or only owned ones
56 my $show_mine = 1; #SHOW BY DEFAULT
57 my $show = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
58 $show_mine = $input->param('show_mine') if $show == 1;
60 # IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
61 if ( not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del'} && $op == 'add_form' ) {
67 my $script_name = "/cgi-bin/koha/admin/aqbudgets.pl";
68 my $budget_hash=$input->Vars;
69 my $budget_id = $$budget_hash{budget_id};
70 my $budget_permission = $input->param('budget_permission');
71 my $budget_period_dropbox = $input->param('budget_period_dropbox');
72 #filtering non budget keys
73 delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
74 my $filter_budgetbranch = $input->param('filter_budgetbranch');
75 my $filter_budgetname = $input->param('filter_budgetname');
77 notree => ($filter_budgetbranch or $show_mine)
79 # ' ------- get periods stuff ------------------'
80 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
81 my $period = GetBudgetPeriod($$budget_hash{budget_period_id});
86 # ------- get periods stuff ------------------
88 # USED FOR PERMISSION COMPARISON LATER
89 my $borrower_id = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
90 my $user = GetMemberDetails($borrower_id);
91 my $user_branchcode = $user->{'branchcode'};
94 action => $script_name,
95 script_name => $script_name,
96 show_mine => $show_mine,
104 my $branches = GetBranches($show_mine);
106 foreach my $thisbranch (keys %$branches) {
108 value => $thisbranch,
109 branchname => $branches->{$thisbranch}->{'branchname'},
111 $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
112 push @branchloop2, \%row;
115 $template->param(auth_cats_loop => GetBudgetAuthCats($$period{budget_period_id}) );
117 # Used to create form to add or modify a record
118 if ($op eq 'add_form') {
119 #### ------------------- ADD_FORM -------------------------
121 # if no buget_id is passed then its an add
122 # pass the period_id to build the dropbox - because we only want to show budgets from this period
123 my $dropbox_disabled;
124 if (defined $budget_id ) { ### MOD
125 $budget = GetBudget($budget_id);
126 $dropbox_disabled = BudgetHasChildren($budget_id);
127 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
128 $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
129 $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget;
132 # build budget hierarchy
135 my $hier = GetBudgetHierarchy($$period{budget_period_id});
136 foreach my $r (@$hier) {
137 $r->{budget_code_indent} =~ s/ /\~/g; #
138 $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
139 push @values, $r->{budget_id};
142 # if no buget_id is passed then its an add
144 my $budget_parent_id;
146 $budget_parent_id = $budget->{'budget_parent_id'} ;
148 $budget_parent_id = $input->param('budget_parent_id');
150 $budget_parent = GetBudget($budget_parent_id);
152 # build branches select
153 my $branches = GetBranches;
154 my @branchloop_select;
155 foreach my $thisbranch ( sort keys %$branches ) {
157 value => $thisbranch,
158 branchname => $branches->{$thisbranch}->{'branchname'},
160 $row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
161 push @branchloop_select, \%row;
164 # populates the YUI planning button
165 my $categories = GetAuthorisedValueCategories();
166 my @auth_cats_loop1 = ();
167 foreach my $category (@$categories) {
168 my $entry = { category => $category,
169 selected => $budget->{sort1_authcat} eq $category ?1:0,
171 push @auth_cats_loop1, $entry;
173 my @auth_cats_loop2 = ();
174 foreach my $category (@$categories) {
175 my $entry = { category => $category,
176 selected => $budget->{sort2_authcat} eq $category ?1:0,
178 push @auth_cats_loop2, $entry;
180 $template->param(authorised_value_categories1 => \@auth_cats_loop1);
181 $template->param(authorised_value_categories2 => \@auth_cats_loop2);
183 my $budget_perm_dropbox =
184 GetBudgetPermDropbox($budget->{'budget_permission'});
186 # if no buget_id is passed then its an add
189 dateformat => C4::Dates->new()->visual(),
190 budget_parent_id => $budget_parent->{'budget_id'},
191 budget_parent_name => $budget_parent->{'budget_name'},
192 budget_perm_dropbox => $budget_perm_dropbox,
193 branchloop_select => \@branchloop_select,
197 # END $OP eq ADD_FORM
198 #---------------------- DEFAULT DISPLAY BELOW ---------------------
200 # called by default form, used to confirm deletion of data in DB
201 } elsif ($op eq 'delete_confirm') {
203 my $budget = GetBudget($budget_id);
205 budget_id => $budget->{'budget_id'},
206 budget_code => $budget->{'budget_code'},
207 budget_name => $budget->{'budget_name'},
208 budget_amount => $num->format_price( $budget->{'budget_amount'} ),
210 # END $OP eq DELETE_CONFIRM
211 # called by delete_confirm, used to effectively confirm deletion of data in DB
213 if ( $op eq 'delete_confirmed' ) {
214 my $rc = DelBudget($budget_id);
215 }elsif( $op eq 'add_validate' ) {
216 if ( defined $$budget_hash{budget_id} ) {
217 ModBudget( $budget_hash );
219 AddBudget( $budget_hash );
222 my $branches = GetBranches();
223 my $budget_period_dropbox = GetBudgetPeriodsDropbox($$period{budget_period_id} );
225 budget_period_dropbox => $budget_period_dropbox,
226 budget_id => $budget_id,
230 my $moo = GetBudgetHierarchy($$period{budget_period_id}, C4::Context->userenv->{branchcode}, $show_mine?$borrower_id:'');
231 my @budgets = @$moo; #FIXME
235 my $period_total = 0;
236 my ( $period_alloc_total, $base_alloc_total, $base_spent_total, $base_remaining_total );
239 $debug && warn Dump(@budgets);
240 #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 ?
242 foreach my $budget (@budgets) {
244 unless($staffflags->{'superlibrarian'} % 2 == 1 ) {
245 #IF NO PERMS, THEN DISABLE EDIT/DELETE
246 unless ( $template->{param_map}->{'CAN_user_acquisition_budget_modify'} ) {
247 $budget->{'budget_lock'} = 1;
249 # check budget permission
250 if ( $$period{budget_period_locked} == 1 ) {
251 $budget->{'budget_lock'} = 1;
253 } elsif ( $budget->{budget_permission} == 1 ) {
255 if ( $borrower_id != $budget->{'budget_owner_id'} ) {
256 $budget->{'budget_lock'} = 1;
258 # check parent perms too
259 my $parents_perm = 0;
260 if ( $budget->{depth} > 0 ) {
261 $parents_perm = CheckBudgetParentPerm( $budget, $borrower_id );
262 delete $budget->{'budget_lock'} if $parents_perm == '1';
264 } elsif ( $budget->{budget_permission} == 2 ) {
266 $budget->{'budget_lock'} = 1 if $user_branchcode ne $budget->{budget_branchcode};
270 # if a budget search doesnt match, next
271 if ($filter_budgetname ) {
272 next unless $budget->{budget_code} =~ m/$filter_budgetname/ ||
273 $budget->{name} =~ m/$filter_budgetname/ ;
275 if ($filter_budgetbranch ) {
276 next unless $budget->{budget_branchcode} =~ m/$filter_budgetbranch/;
280 # adds to total - only if budget is a 'top-level' budget
281 $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
282 $base_alloc_total += $budget->{'budget_amount'};
283 $base_spent_total += $budget->{'budget_spent'};
284 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'};
285 $base_remaining_total += $budget->{'budget_remaining'};
287 # if amount == 0 dont display...
288 delete $budget->{'budget_unalloc_sublevel'} if $budget->{'budget_unalloc_sublevel'} == 0 ;
290 $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
291 $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
292 for (grep {/budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
293 $$budget{$_} = $num->format_price( $$budget{$_} ) if defined($$budget{$_})
296 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
297 $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
298 $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
300 push( @loop, { %{$budget},
301 branchname => $branches->{ $budget->{branchcode} }->{branchname},
306 my $budget_period_total = $num->format_price($$period{budget_period_total}) if $$period{budget_period_total};
310 budget_period_total => $budget_period_total,
311 period_alloc_total => $num->format_price($period_alloc_total),
312 base_alloc_total => $num->format_price($base_alloc_total),
313 base_spent_total => $num->format_price($base_spent_total),
314 base_remaining_total => $num->format_price($base_remaining_total),
315 period_remaining_total => $num->format_price( $period_alloc_total - $base_alloc_total ),
316 branchloop => \@branchloop2,
319 } #---- END $OP eq DEFAULT
321 output_html_with_http_headers $input, $cookie, $template->output;