Bug 25992: Make SIP2 logger subroutines exportable to prevent crash
[koha.git] / admin / aqbudgets.pl
blob684e0e2159aab4a82fec5ee58eb1cb52c32bf0b8
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
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use List::Util qw/min/;
27 use Koha::Database;
28 use C4::Auth qw/get_user_subpermissions/;
29 use C4::Auth;
30 use C4::Acquisition;
31 use C4::Budgets;
32 use C4::Context;
33 use C4::Output;
34 use C4::Koha;
35 use C4::Debug;
36 use Koha::Acquisition::Currencies;
37 use Koha::Patrons;
39 my $input = new CGI;
40 my $dbh = C4::Context->dbh;
42 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
43 { template_name => "admin/aqbudgets.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { acquisition => 'budget_manage' },
48 debug => 0,
52 my $active_currency = Koha::Acquisition::Currencies->get_active;
53 if ( $active_currency ) {
54 $template->param( symbol => $active_currency->symbol,
55 currency => $active_currency->currency
59 my $op = $input->param('op') || 'list';
61 # see if the user want to see all budgets or only owned ones by default
62 my $show_mine = $input->param('show_mine') // 0;
64 # IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
65 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
66 and $op eq 'add_form')
68 $op = 'list';
71 # get only the columns of aqbudgets in budget_hash
72 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
73 my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => scalar $input->param($_) ) : () } keys( %{$input->Vars()}) } ;
75 my $budget_id = $input->param('budget_id');
76 my $budget_period_id = $input->param('budget_period_id');
77 my $budget_permission = $input->param('budget_permission');
78 my $budget_users_ids = $input->param('budget_users_ids');
79 my $filter_budgetbranch = $input->param('filter_budgetbranch') // '';
80 my $filter_budgetname = $input->param('filter_budgetname');
83 # ' ------- get periods stuff ------------------'
84 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
85 my $period;
86 if ( $budget_period_id ) {
87 $period = GetBudgetPeriod( $budget_period_id );
90 # ------- get periods stuff ------------------
92 $template->param(
93 show_mine => $show_mine,
94 op => $op,
95 selected_branchcode => $filter_budgetbranch,
98 my $budget;
100 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
101 if $budget_period_id;
103 # Used to create form to add or modify a record
104 if ($op eq 'add_form') {
105 #### ------------------- ADD_FORM -------------------------
106 # if no buget_id is passed then its an add
107 # pass the period_id to build the dropbox - because we only want to show budgets from this period
108 my $dropbox_disabled;
109 if (defined $budget_id ) { ### MOD
110 $budget = GetBudget($budget_id);
111 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
112 $template->param(error_not_authorised_to_modify => 1);
113 output_html_with_http_headers $input, $cookie, $template->output;
114 exit;
116 $dropbox_disabled = BudgetHasChildren($budget_id);
117 $budget->{budget_owner} = Koha::Patrons->find( $budget->{budget_owner_id} );
118 } elsif ( $period->{budget_period_locked} ) {
119 output_and_exit( $input, $cookie, $template, 'budget_is_locked' );
122 # build budget hierarchy
123 my %labels;
124 my @values;
125 my $hier = GetBudgetHierarchy($$period{budget_period_id});
126 foreach my $r (@$hier) {
127 $labels{"$r->{budget_id}"} = $r->{budget_code};
128 push @values, $r->{budget_id};
130 push @values, '';
131 # if no buget_id is passed then its an add
132 my $budget_parent;
133 my $budget_parent_id;
134 if ($budget){
135 $budget_parent_id = $budget->{'budget_parent_id'} ;
136 }else{
137 $budget_parent_id = $input->param('budget_parent_id');
139 $budget_parent = GetBudget($budget_parent_id);
141 # populates the planning button
142 $template->param(
143 sort1_auth => $budget->{sort1_authcat},
144 sort2_auth => $budget->{sort2_authcat},
147 if($budget->{'budget_permission'}){
148 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
149 $template->param($budget_permission => 1);
152 if ($budget) {
153 my @budgetusers = GetBudgetUsers($budget->{budget_id});
154 my @budgetusers_loop;
155 foreach my $borrowernumber (@budgetusers) {
156 my $patron = Koha::Patrons->find( $borrowernumber );
157 push @budgetusers_loop, {
158 firstname => $patron->firstname, # FIXME Should pass the patron object
159 surname => $patron->surname,
160 borrowernumber => $borrowernumber
163 $template->param(
164 budget_users => \@budgetusers_loop,
165 budget_users_ids => join ':', @budgetusers
169 # if no buget_id is passed then its an add
170 $template->param(
171 budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
172 budget_parent_id => $budget_parent->{'budget_id'},
173 budget_parent_name => $budget_parent->{'budget_name'},
174 %$period,
175 %$budget,
177 # END $OP eq ADD_FORM
178 #---------------------- DEFAULT DISPLAY BELOW ---------------------
180 # called by default form, used to confirm deletion of data in DB
181 } elsif ($op eq 'delete_confirm') {
183 my $budget = GetBudget($budget_id);
184 $template->param(
185 budget_id => $budget->{'budget_id'},
186 budget_code => $budget->{'budget_code'},
187 budget_name => $budget->{'budget_name'},
188 budget_amount => $budget->{'budget_amount'},
190 # END $OP eq DELETE_CONFIRM
191 # called by delete_confirm, used to effectively confirm deletion of data in DB
192 } elsif ( $op eq 'delete_confirmed' ) {
193 if ( BudgetHasChildren( $budget_id ) ) {
194 # We should never be here, the interface does not provide this action.
195 die("Delete a fund with children is not possible");
197 my $rc = DelBudget($budget_id);
198 $op = 'list';
199 } elsif( $op eq 'add_validate' ) {
200 my @budgetusersid;
201 if (defined $budget_users_ids){
202 @budgetusersid = split(':', $budget_users_ids);
205 my $budget_modified = 0;
206 if (defined $budget_id) {
207 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
208 $staffflags)
210 ModBudget( $budget_hash );
211 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
212 $budget_modified = 1;
214 else {
215 $template->param(error_not_authorised_to_modify => 1);
217 } else {
218 $budget_hash->{budget_id} = AddBudget( $budget_hash );
219 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
220 $budget_modified = 1;
223 my $set_owner_to_children = $input->param('set_owner_to_children');
224 if ( $set_owner_to_children and $budget_modified ) {
225 C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} );
227 $op = 'list';
230 if ( $op eq 'list' ) {
231 $template->param(
232 budget_id => $budget_id,
233 %$period,
236 my @budgets = @{
237 GetBudgetHierarchy( $$period{budget_period_id}, undef, ( $show_mine ? $borrowernumber : 0 ))
240 my $period_total = 0;
241 my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
243 #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 ?
245 my @budgets_to_display;
246 foreach my $budget (@budgets) {
247 # PERMISSIONS
248 next unless CanUserUseBudget($borrowernumber, $budget, $staffflags);
249 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
250 $budget->{'budget_lock'} = 1;
253 # if a budget search doesn't match, next
254 if ($filter_budgetname) {
255 next
256 unless $budget->{budget_code} =~ m/$filter_budgetname/i
257 || $budget->{budget_name} =~ m/$filter_budgetname/i;
259 if ($filter_budgetbranch ) {
260 next unless $budget->{budget_branchcode} eq $filter_budgetbranch;
263 ## TOTALS
264 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
265 $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
266 # adds to total - only if budget is a 'top-level' budget
267 unless ( defined $budget->{budget_parent_id} ) {
268 $period_alloc_total += $budget->{'budget_amount'};
269 $spent_total += $budget->{total_spent};
270 $ordered_total += $budget->{total_ordered};
271 $available_total += $budget->{total_remaining};
274 # if amount == 0 don't display...
275 delete $budget->{'budget_unalloc_sublevel'}
276 if (!defined $budget->{'budget_unalloc_sublevel'}
277 or $budget->{'budget_unalloc_sublevel'} == 0);
279 # Value of budget_spent equals 0 instead of undefined value
280 $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
281 $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
283 #Make a list of parents of the bugdet
284 my @budget_hierarchy;
285 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
286 my $parent_id = $budget->{"budget_parent_id"};
287 while ($parent_id) {
288 my $parent = GetBudget($parent_id);
289 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
290 $parent_id = $parent->{"budget_parent_id"};
292 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
293 @budget_hierarchy = reverse(@budget_hierarchy);
295 $budget->{budget_hierarchy} = \@budget_hierarchy;
297 $budget->{budget_has_children} = BudgetHasChildren( $budget->{budget_id} );
298 push @budgets_to_display, $budget;
301 my $budget_period_total = $period->{budget_period_total};
303 my $periods = GetBudgetPeriods();
305 $template->param(
306 op => 'list',
307 budgets => \@budgets_to_display,
308 periods => $periods,
309 budget_period_total => $budget_period_total,
310 period_alloc_total => $period_alloc_total,
311 spent_total => $spent_total,
312 ordered_total => $ordered_total,
313 available_total => $available_total,
314 filter_budgetname => $filter_budgetname,
317 } #---- END list
319 output_html_with_http_headers $input, $cookie, $template->output;