Bug 8805 - Add a biblio-zebra-indexdefs.xsl for NORMARC
[koha.git] / admin / aqbudgets.pl
blobc5f99e3d719853b3fe8b5bbcba89b3afe075d8e7
1 #!/usr/bin/perl
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
12 # version.
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.
22 use Modern::Perl;
24 use CGI;
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/;
31 use C4::Auth;
32 use C4::Acquisition;
33 use C4::Budgets; #
34 use C4::Members; # calls GetSortDetails()
35 use C4::Context;
36 use C4::Output;
37 use C4::Koha;
38 use C4::Debug;
39 #use POSIX qw(locale_h);
41 my $input = new CGI;
42 my $dbh = C4::Context->dbh;
44 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
45 { template_name => "admin/aqbudgets.tmpl",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { acquisition => 'budget_manage' },
50 debug => 0,
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') // 0; # 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'}
68 and $op eq 'add_form')
70 $op = '';
72 my $num=FormatNumber;
74 my $script_name = "/cgi-bin/koha/admin/aqbudgets.pl";
75 my $budget_hash = $input->Vars;
76 my $budget_id = $$budget_hash{budget_id};
77 my $budget_permission = $input->param('budget_permission');
78 my $filter_budgetbranch = $input->param('filter_budgetbranch') // '';
79 my $filter_budgetname = $input->param('filter_budgetname');
80 #filtering non budget keys
81 delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
83 $template->param(
84 notree => ($filter_budgetbranch or $show_mine)
86 # ' ------- get periods stuff ------------------'
87 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
88 my $period = GetBudgetPeriod($$budget_hash{budget_period_id});
90 $template->param(
91 %$period
93 # ------- get periods stuff ------------------
95 # USED FOR PERMISSION COMPARISON LATER
96 my $borrower_id = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
97 my $user = GetMemberDetails($borrower_id);
98 my $user_branchcode = $user->{'branchcode'};
100 $template->param(
101 action => $script_name,
102 script_name => $script_name,
103 show_mine => $show_mine,
104 $op || else => 1,
108 # retrieve branches
109 my ( $budget, );
111 my $branches = GetBranches($show_mine);
112 my @branchloop2;
113 foreach my $thisbranch (keys %$branches) {
114 my %row = (
115 value => $thisbranch,
116 branchname => $branches->{$thisbranch}->{'branchname'},
118 $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
119 push @branchloop2, \%row;
122 $template->param(auth_cats_loop => GetBudgetAuthCats($$period{budget_period_id}) );
124 # Used to create form to add or modify a record
125 if ($op eq 'add_form') {
126 #### ------------------- ADD_FORM -------------------------
127 # if no buget_id is passed then its an add
128 # pass the period_id to build the dropbox - because we only want to show budgets from this period
129 my $dropbox_disabled;
130 if (defined $budget_id ) { ### MOD
131 $budget = GetBudget($budget_id);
132 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
133 $template->param(error_not_authorised_to_modify => 1);
134 output_html_with_http_headers $input, $cookie, $template->output;
135 exit;
137 $dropbox_disabled = BudgetHasChildren($budget_id);
138 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
139 $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
140 $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget;
143 # build budget hierarchy
144 my %labels;
145 my @values;
146 my $hier = GetBudgetHierarchy($$period{budget_period_id});
147 foreach my $r (@$hier) {
148 $r->{budget_code_indent} =~ s/ /\~/g; #
149 $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
150 push @values, $r->{budget_id};
152 push @values, '';
153 # if no buget_id is passed then its an add
154 my $budget_parent;
155 my $budget_parent_id;
156 if ($budget){
157 $budget_parent_id = $budget->{'budget_parent_id'} ;
158 }else{
159 $budget_parent_id = $input->param('budget_parent_id');
161 $budget_parent = GetBudget($budget_parent_id);
163 # build branches select
164 my $branches = GetBranches;
165 my @branchloop_select;
166 foreach my $thisbranch ( sort keys %$branches ) {
167 my %row = (
168 value => $thisbranch,
169 branchname => $branches->{$thisbranch}->{'branchname'},
171 $row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
172 push @branchloop_select, \%row;
175 # populates the YUI planning button
176 my $categories = GetAuthorisedValueCategories();
177 my @auth_cats_loop1 = ();
178 foreach my $category (@$categories) {
179 my $entry = { category => $category,
180 selected => $budget->{sort1_authcat} eq $category ?1:0,
182 push @auth_cats_loop1, $entry;
184 my @auth_cats_loop2 = ();
185 foreach my $category (@$categories) {
186 my $entry = { category => $category,
187 selected => $budget->{sort2_authcat} eq $category ?1:0,
189 push @auth_cats_loop2, $entry;
191 $template->param(authorised_value_categories1 => \@auth_cats_loop1);
192 $template->param(authorised_value_categories2 => \@auth_cats_loop2);
194 if($budget->{'budget_permission'}){
195 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
196 $template->param($budget_permission => 1);
199 if ($budget) {
200 my @budgetusers = GetBudgetUsers($budget->{budget_id});
201 my @budgetusers_loop;
202 foreach my $borrowernumber (@budgetusers) {
203 my $member = C4::Members::GetMember(
204 borrowernumber => $borrowernumber);
205 push @budgetusers_loop, {
206 firstname => $member->{firstname},
207 surname => $member->{surname},
208 borrowernumber => $borrowernumber
211 $template->param(
212 budget_users => \@budgetusers_loop,
213 budget_users_ids => join ':', @budgetusers
217 # if no buget_id is passed then its an add
218 $template->param(
219 add_validate => 1,
220 dateformat => C4::Dates->new()->visual(),
221 budget_parent_id => $budget_parent->{'budget_id'},
222 budget_parent_name => $budget_parent->{'budget_name'},
223 branchloop_select => \@branchloop_select,
224 %$period,
225 %$budget,
227 # END $OP eq ADD_FORM
228 #---------------------- DEFAULT DISPLAY BELOW ---------------------
230 # called by default form, used to confirm deletion of data in DB
231 } elsif ($op eq 'delete_confirm') {
233 my $budget = GetBudget($budget_id);
234 $template->param(
235 budget_id => $budget->{'budget_id'},
236 budget_code => $budget->{'budget_code'},
237 budget_name => $budget->{'budget_name'},
238 budget_amount => $num->format_price( $budget->{'budget_amount'} ),
240 # END $OP eq DELETE_CONFIRM
241 # called by delete_confirm, used to effectively confirm deletion of data in DB
242 } else{
243 if ( $op eq 'delete_confirmed' ) {
244 my $rc = DelBudget($budget_id);
245 }elsif( $op eq 'add_validate' ) {
246 my @budgetusersid;
247 if (defined $$budget_hash{'budget_users_ids'}){
248 @budgetusersid = split(':', $budget_hash->{'budget_users_ids'});
251 if ( defined $$budget_hash{budget_id} ) {
252 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
253 $staffflags)
255 ModBudget( $budget_hash );
256 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
258 else {
259 $template->param(error_not_authorised_to_modify => 1);
261 } else {
262 AddBudget( $budget_hash );
263 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
266 my $branches = GetBranches();
267 $template->param(
268 budget_id => $budget_id,
269 %$period,
272 my @budgets = @{
273 GetBudgetHierarchy($$period{budget_period_id},
274 C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
277 my $toggle = 0;
278 my @loop;
279 my $period_total = 0;
280 my ( $period_alloc_total, $base_spent_total );
282 #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 ?
284 foreach my $budget (@budgets) {
285 #Level and sublevels total spent
286 $budget->{'total_levels_spent'} = GetChildBudgetsSpent($budget->{"budget_id"});
288 # PERMISSIONS
289 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
290 $budget->{'budget_lock'} = 1;
293 # if a budget search doesnt match, next
294 if ($filter_budgetname) {
295 next
296 unless $budget->{budget_code} =~ m/$filter_budgetname/i
297 || $budget->{budget_name} =~ m/$filter_budgetname/i;
299 if ($filter_budgetbranch ) {
300 next unless $budget->{budget_branchcode} =~ m/$filter_budgetbranch/;
303 ## TOTALS
304 # adds to total - only if budget is a 'top-level' budget
305 $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
306 $base_spent_total += $budget->{'budget_spent'};
307 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'total_levels_spent'};
309 # if amount == 0 dont display...
310 delete $budget->{'budget_unalloc_sublevel'}
311 if (!defined $budget->{'budget_unalloc_sublevel'}
312 or $budget->{'budget_unalloc_sublevel'} == 0);
314 $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
315 $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
316 for (grep {/total_levels_spent|budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
317 $budget->{$_} = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
320 # Value of budget_spent equals 0 instead of undefined value
321 $budget->{"budget_spent"} = $num->format_price(0) unless defined($budget->{"budget_spent"});
323 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
324 $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
325 $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
327 #Make a list of parents of the bugdet
328 my @budget_hierarchy;
329 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
330 my $parent_id = $budget->{"budget_parent_id"};
331 while ($parent_id) {
332 my $parent = GetBudget($parent_id);
333 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
334 $parent_id = $parent->{"budget_parent_id"};
336 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
337 @budget_hierarchy = reverse(@budget_hierarchy);
339 push( @loop, { %{$budget},
340 branchname => $branches->{ $budget->{branchcode} }->{branchname},
341 budget_hierarchy => \@budget_hierarchy,
346 my $budget_period_total;
347 if ( $period->{budget_period_total} ) {
348 $budget_period_total =
349 $num->format_price( $period->{budget_period_total} );
352 if ($period_alloc_total) {
353 $period_alloc_total = $num->format_price($period_alloc_total);
356 if ($base_spent_total) {
357 $base_spent_total = $num->format_price($base_spent_total);
360 $template->param(
361 else => 1,
362 budget => \@loop,
363 budget_period_total => $budget_period_total,
364 period_alloc_total => $period_alloc_total,
365 base_spent_total => $base_spent_total,
366 branchloop => \@branchloop2,
369 } #---- END $OP eq DEFAULT
371 output_html_with_http_headers $input, $cookie, $template->output;