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
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
25 use List
::Util qw
/min/;
26 use Number
::Format
qw(format_price);
28 use C4
::Auth qw
/get_user_subpermissions/;
29 use C4
::Branch
; # GetBranches
30 use C4
::Dates qw
/format_date format_date_in_iso/;
34 use C4
::Members
; # calls GetSortDetails()
39 #use POSIX qw(locale_h);
42 my $dbh = C4
::Context
->dbh;
44 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user
(
45 { template_name
=> "admin/aqbudgets.tmpl",
49 flagsrequired
=> { acquisition
=> 'budget_manage' },
54 my $cur = GetCurrency
();
55 $template->param( symbol
=> $cur->{symbol
},
56 currency
=> $cur->{currency
}
59 my $op = $input->param('op');
61 # see if the user want to see all budgets or only owned ones
62 my $show_mine = 1; #SHOW BY DEFAULT
63 my $show = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
64 $show_mine = $input->param('show_mine') if $show == 1;
66 # IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
67 if ( not defined $template->{VARS
}->{'CAN_user_acquisition_budget_add_del'} && $op == 'add_form' ) {
72 my $script_name = "/cgi-bin/koha/admin/aqbudgets.pl";
73 my $budget_hash = $input->Vars;
74 my $budget_id = $$budget_hash{budget_id
};
75 my $budget_permission = $input->param('budget_permission');
76 my $filter_budgetbranch = $input->param('filter_budgetbranch');
77 my $filter_budgetname = $input->param('filter_budgetname');
78 #filtering non budget keys
79 delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
82 notree
=> ($filter_budgetbranch or $show_mine)
84 # ' ------- get periods stuff ------------------'
85 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
86 my $period = GetBudgetPeriod
($$budget_hash{budget_period_id
});
91 # ------- get periods stuff ------------------
93 # USED FOR PERMISSION COMPARISON LATER
94 my $borrower_id = $template->{VARS
}->{'USER_INFO'}[0]->{'borrowernumber'};
95 my $user = GetMemberDetails
($borrower_id);
96 my $user_branchcode = $user->{'branchcode'};
99 action
=> $script_name,
100 script_name
=> $script_name,
101 show_mine
=> $show_mine,
109 my $branches = GetBranches
($show_mine);
111 foreach my $thisbranch (keys %$branches) {
113 value
=> $thisbranch,
114 branchname
=> $branches->{$thisbranch}->{'branchname'},
116 $row{selected
} = 1 if $thisbranch eq $filter_budgetbranch;
117 push @branchloop2, \
%row;
120 $template->param(auth_cats_loop
=> GetBudgetAuthCats
($$period{budget_period_id
}) );
122 # Used to create form to add or modify a record
123 if ($op eq 'add_form') {
124 #### ------------------- ADD_FORM -------------------------
125 # if no buget_id is passed then its an add
126 # pass the period_id to build the dropbox - because we only want to show budgets from this period
127 my $dropbox_disabled;
128 if (defined $budget_id ) { ### MOD
129 $budget = GetBudget
($budget_id);
130 $dropbox_disabled = BudgetHasChildren
($budget_id);
131 my $borrower = &GetMember
( borrowernumber
=>$budget->{budget_owner_id
} );
132 $budget->{budget_owner_name
} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
133 $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget;
136 # build budget hierarchy
139 my $hier = GetBudgetHierarchy
($$period{budget_period_id
});
140 foreach my $r (@
$hier) {
141 $r->{budget_code_indent
} =~ s/ /\~/g; #
142 $labels{"$r->{budget_id}"} = $r->{budget_code_indent
};
143 push @values, $r->{budget_id
};
146 # if no buget_id is passed then its an add
148 my $budget_parent_id;
150 $budget_parent_id = $budget->{'budget_parent_id'} ;
152 $budget_parent_id = $input->param('budget_parent_id');
154 $budget_parent = GetBudget
($budget_parent_id);
156 # build branches select
157 my $branches = GetBranches
;
158 my @branchloop_select;
159 foreach my $thisbranch ( sort keys %$branches ) {
161 value
=> $thisbranch,
162 branchname
=> $branches->{$thisbranch}->{'branchname'},
164 $row{selected
} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
165 push @branchloop_select, \
%row;
168 # populates the YUI planning button
169 my $categories = GetAuthorisedValueCategories
();
170 my @auth_cats_loop1 = ();
171 foreach my $category (@
$categories) {
172 my $entry = { category
=> $category,
173 selected
=> $budget->{sort1_authcat
} eq $category ?
1:0,
175 push @auth_cats_loop1, $entry;
177 my @auth_cats_loop2 = ();
178 foreach my $category (@
$categories) {
179 my $entry = { category
=> $category,
180 selected
=> $budget->{sort2_authcat
} eq $category ?
1:0,
182 push @auth_cats_loop2, $entry;
184 $template->param(authorised_value_categories1
=> \
@auth_cats_loop1);
185 $template->param(authorised_value_categories2
=> \
@auth_cats_loop2);
187 if($budget->{'budget_permission'}){
188 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
189 $template->param($budget_permission => 1);
192 # if no buget_id is passed then its an add
195 dateformat
=> C4
::Dates
->new()->visual(),
196 budget_parent_id
=> $budget_parent->{'budget_id'},
197 budget_parent_name
=> $budget_parent->{'budget_name'},
198 branchloop_select
=> \
@branchloop_select,
202 # END $OP eq ADD_FORM
203 #---------------------- DEFAULT DISPLAY BELOW ---------------------
205 # called by default form, used to confirm deletion of data in DB
206 } elsif ($op eq 'delete_confirm') {
208 my $budget = GetBudget
($budget_id);
210 budget_id
=> $budget->{'budget_id'},
211 budget_code
=> $budget->{'budget_code'},
212 budget_name
=> $budget->{'budget_name'},
213 budget_amount
=> $num->format_price( $budget->{'budget_amount'} ),
215 # END $OP eq DELETE_CONFIRM
216 # called by delete_confirm, used to effectively confirm deletion of data in DB
218 if ( $op eq 'delete_confirmed' ) {
219 my $rc = DelBudget
($budget_id);
220 }elsif( $op eq 'add_validate' ) {
221 if ( defined $$budget_hash{budget_id
} ) {
222 ModBudget
( $budget_hash );
224 AddBudget
( $budget_hash );
227 my $branches = GetBranches
();
229 budget_id
=> $budget_id,
233 my $moo = GetBudgetHierarchy
($$period{budget_period_id
}, C4
::Context
->userenv->{branchcode
}, $show_mine?
$borrower_id:'');
234 my @budgets = @
$moo; #FIXME
238 my $period_total = 0;
239 my ( $period_alloc_total, $base_spent_total );
241 #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 ?
243 foreach my $budget (@budgets) {
244 #Level and sublevels total spent
245 $budget->{'total_levels_spent'} = GetChildBudgetsSpent
($budget->{"budget_id"});
248 unless($staffflags->{'superlibrarian'} % 2 == 1 ) {
249 #IF NO PERMS, THEN DISABLE EDIT/DELETE
250 unless ( $template->{VARS
}->{'CAN_user_acquisition_budget_modify'} ) {
251 $budget->{'budget_lock'} = 1;
253 # check budget permission
254 if ( $$period{budget_period_locked
} == 1 ) {
255 $budget->{'budget_lock'} = 1;
257 } elsif ( $budget->{budget_permission
} == 1 ) {
259 if ( $borrower_id != $budget->{'budget_owner_id'} ) {
260 $budget->{'budget_lock'} = 1;
262 # check parent perms too
263 my $parents_perm = 0;
264 if ( $budget->{depth
} > 0 ) {
265 $parents_perm = CheckBudgetParentPerm
( $budget, $borrower_id );
266 delete $budget->{'budget_lock'} if $parents_perm == '1';
268 } elsif ( $budget->{budget_permission
} == 2 ) {
270 $budget->{'budget_lock'} = 1 if $user_branchcode ne $budget->{budget_branchcode
};
274 # if a budget search doesnt match, next
275 if ($filter_budgetname) {
277 unless $budget->{budget_code
} =~ m/$filter_budgetname/i
278 || $budget->{budget_name
} =~ m/$filter_budgetname/i;
280 if ($filter_budgetbranch ) {
281 next unless $budget->{budget_branchcode
} =~ m/$filter_budgetbranch/;
285 # adds to total - only if budget is a 'top-level' budget
286 $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
287 $base_spent_total += $budget->{'budget_spent'};
288 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'total_levels_spent'};
290 # if amount == 0 dont display...
291 delete $budget->{'budget_unalloc_sublevel'} if $budget->{'budget_unalloc_sublevel'} == 0 ;
293 $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
294 $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
295 for (grep {/total_levels_spent|budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
296 $budget->{$_} = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
299 # Value of budget_spent equals 0 instead of undefined value
300 $budget->{"budget_spent"} = $num->format_price(0) unless defined($budget->{"budget_spent"});
302 my $borrower = &GetMember
( borrowernumber
=>$budget->{budget_owner_id
} );
303 $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
304 $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
306 #Make a list of parents of the bugdet
307 my @budget_hierarchy;
308 push @budget_hierarchy, { element_name
=> $budget->{"budget_name"}, element_id
=> $budget->{"budget_id"} };
309 my $parent_id = $budget->{"budget_parent_id"};
311 my $parent = GetBudget
($parent_id);
312 push @budget_hierarchy, { element_name
=> $parent->{"budget_name"}, element_id
=> $parent->{"budget_id"} };
313 $parent_id = $parent->{"budget_parent_id"};
315 push @budget_hierarchy, { element_name
=> $period->{"budget_period_description"} };
316 @budget_hierarchy = reverse(@budget_hierarchy);
318 push( @loop, { %{$budget},
319 branchname
=> $branches->{ $budget->{branchcode
} }->{branchname
},
320 budget_hierarchy
=> \
@budget_hierarchy,
325 my $budget_period_total;
326 if ( $period->{budget_period_total
} ) {
327 $budget_period_total =
328 $num->format_price( $period->{budget_period_total
} );
331 if ($period_alloc_total) {
332 $period_alloc_total = $num->format_price($period_alloc_total);
335 if ($base_spent_total) {
336 $base_spent_total = $num->format_price($base_spent_total);
342 budget_period_total
=> $budget_period_total,
343 period_alloc_total
=> $period_alloc_total,
344 base_spent_total
=> $base_spent_total,
345 branchloop
=> \
@branchloop2,
348 } #---- END $OP eq DEFAULT
350 output_html_with_http_headers
$input, $cookie, $template->output;