Bug 17783: Test to confirm correct effective issuing rule selection
[koha.git] / admin / aqbudgets.pl
blob483ff8f12eb97f9203a01ed6bdbe26dbbfcd48dc
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::Members;
33 use C4::Context;
34 use C4::Output;
35 use C4::Koha;
36 use C4::Debug;
37 use Koha::Acquisition::Currencies;
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 $template->param( symbol => $active_currency->symbol,
54 currency => $active_currency->currency
57 my $op = $input->param('op') || 'list';
59 # see if the user want to see all budgets or only owned ones by default
60 my $show_mine = $input->param('show_mine') // 0;
62 # IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
63 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
64 and $op eq 'add_form')
66 $op = 'list';
69 # get only the columns of aqbudgets in budget_hash
70 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
71 my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => scalar $input->param($_) ) : () } keys( %{$input->Vars()}) } ;
73 my $budget_id = $input->param('budget_id');
74 my $budget_period_id = $input->param('budget_period_id');
75 my $budget_permission = $input->param('budget_permission');
76 my $budget_users_ids = $input->param('budget_users_ids');
77 my $filter_budgetbranch = $input->param('filter_budgetbranch') // '';
78 my $filter_budgetname = $input->param('filter_budgetname');
81 # ' ------- get periods stuff ------------------'
82 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
83 my $period;
84 if ( $budget_period_id ) {
85 $period = GetBudgetPeriod( $budget_period_id );
88 # ------- get periods stuff ------------------
90 # USED FOR PERMISSION COMPARISON LATER
91 my $borrower_id = $template->{VARS}->{'USER_INFO'}->{'borrowernumber'};
92 my $user = C4::Members::GetMember( borrowernumber => $borrower_id );
93 my $user_branchcode = $user->{'branchcode'};
95 $template->param(
96 show_mine => $show_mine,
97 op => $op,
98 selected_branchcode => $filter_budgetbranch,
101 my $budget;
103 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
104 if $budget_period_id;
106 # Used to create form to add or modify a record
107 if ($op eq 'add_form') {
108 #### ------------------- ADD_FORM -------------------------
109 # if no buget_id is passed then its an add
110 # pass the period_id to build the dropbox - because we only want to show budgets from this period
111 my $dropbox_disabled;
112 if (defined $budget_id ) { ### MOD
113 $budget = GetBudget($budget_id);
114 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
115 $template->param(error_not_authorised_to_modify => 1);
116 output_html_with_http_headers $input, $cookie, $template->output;
117 exit;
119 $dropbox_disabled = BudgetHasChildren($budget_id);
120 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
121 $budget->{budget_owner_name} = ( $borrower ? $borrower->{'firstname'} . ' ' . $borrower->{'surname'} : '' );
124 # build budget hierarchy
125 my %labels;
126 my @values;
127 my $hier = GetBudgetHierarchy($$period{budget_period_id});
128 foreach my $r (@$hier) {
129 $labels{"$r->{budget_id}"} = $r->{budget_code};
130 push @values, $r->{budget_id};
132 push @values, '';
133 # if no buget_id is passed then its an add
134 my $budget_parent;
135 my $budget_parent_id;
136 if ($budget){
137 $budget_parent_id = $budget->{'budget_parent_id'} ;
138 }else{
139 $budget_parent_id = $input->param('budget_parent_id');
141 $budget_parent = GetBudget($budget_parent_id);
143 # populates the planning button
144 $template->param(
145 sort1_auth => $budget->{sort1_authcat},
146 sort2_auth => $budget->{sort2_authcat},
149 if($budget->{'budget_permission'}){
150 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
151 $template->param($budget_permission => 1);
154 if ($budget) {
155 my @budgetusers = GetBudgetUsers($budget->{budget_id});
156 my @budgetusers_loop;
157 foreach my $borrowernumber (@budgetusers) {
158 my $member = C4::Members::GetMember(
159 borrowernumber => $borrowernumber);
160 push @budgetusers_loop, {
161 firstname => $member->{firstname},
162 surname => $member->{surname},
163 borrowernumber => $borrowernumber
166 $template->param(
167 budget_users => \@budgetusers_loop,
168 budget_users_ids => join ':', @budgetusers
172 # if no buget_id is passed then its an add
173 $template->param(
174 budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
175 budget_parent_id => $budget_parent->{'budget_id'},
176 budget_parent_name => $budget_parent->{'budget_name'},
177 %$period,
178 %$budget,
180 # END $OP eq ADD_FORM
181 #---------------------- DEFAULT DISPLAY BELOW ---------------------
183 # called by default form, used to confirm deletion of data in DB
184 } elsif ($op eq 'delete_confirm') {
186 my $budget = GetBudget($budget_id);
187 $template->param(
188 budget_id => $budget->{'budget_id'},
189 budget_code => $budget->{'budget_code'},
190 budget_name => $budget->{'budget_name'},
191 budget_amount => $budget->{'budget_amount'},
193 # END $OP eq DELETE_CONFIRM
194 # called by delete_confirm, used to effectively confirm deletion of data in DB
195 } elsif ( $op eq 'delete_confirmed' ) {
196 if ( BudgetHasChildren( $budget_id ) ) {
197 # We should never be here, the interface does not provide this action.
198 die("Delete a fund with children is not possible");
200 my $rc = DelBudget($budget_id);
201 $op = 'list';
202 } elsif( $op eq 'add_validate' ) {
203 my @budgetusersid;
204 if (defined $budget_users_ids){
205 @budgetusersid = split(':', $budget_users_ids);
208 my $budget_modified = 0;
209 if (defined $budget_id) {
210 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
211 $staffflags)
213 ModBudget( $budget_hash );
214 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
215 $budget_modified = 1;
217 else {
218 $template->param(error_not_authorised_to_modify => 1);
220 } else {
221 $budget_hash->{budget_id} = AddBudget( $budget_hash );
222 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
223 $budget_modified = 1;
226 my $set_owner_to_children = $input->param('set_owner_to_children');
227 if ( $set_owner_to_children and $budget_modified ) {
228 C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} );
230 $op = 'list';
233 if ( $op eq 'list' ) {
234 $template->param(
235 budget_id => $budget_id,
236 %$period,
239 my @budgets = @{
240 GetBudgetHierarchy($$period{budget_period_id},
241 C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
244 my $period_total = 0;
245 my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
247 #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 ?
249 my @budgets_to_display;
250 foreach my $budget (@budgets) {
251 # PERMISSIONS
252 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
253 $budget->{'budget_lock'} = 1;
256 # if a budget search doesn't match, next
257 if ($filter_budgetname) {
258 next
259 unless $budget->{budget_code} =~ m/$filter_budgetname/i
260 || $budget->{budget_name} =~ m/$filter_budgetname/i;
262 if ($filter_budgetbranch ) {
263 next unless $budget->{budget_branchcode} eq $filter_budgetbranch;
266 ## TOTALS
267 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
268 $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
269 # adds to total - only if budget is a 'top-level' budget
270 unless ( defined $budget->{budget_parent_id} ) {
271 $period_alloc_total += $budget->{'budget_amount'};
272 $spent_total += $budget->{total_spent};
273 $ordered_total += $budget->{total_ordered};
274 $available_total += $budget->{total_remaining};
277 # if amount == 0 don't display...
278 delete $budget->{'budget_unalloc_sublevel'}
279 if (!defined $budget->{'budget_unalloc_sublevel'}
280 or $budget->{'budget_unalloc_sublevel'} == 0);
282 # Value of budget_spent equals 0 instead of undefined value
283 $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
284 $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
286 #Make a list of parents of the bugdet
287 my @budget_hierarchy;
288 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
289 my $parent_id = $budget->{"budget_parent_id"};
290 while ($parent_id) {
291 my $parent = GetBudget($parent_id);
292 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
293 $parent_id = $parent->{"budget_parent_id"};
295 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
296 @budget_hierarchy = reverse(@budget_hierarchy);
298 $budget->{budget_hierarchy} = \@budget_hierarchy;
300 $budget->{budget_has_children} = BudgetHasChildren( $budget->{budget_id} );
301 push @budgets_to_display, $budget;
304 my $budget_period_total = $period->{budget_period_total};
306 my $periods = GetBudgetPeriods();
308 $template->param(
309 op => 'list',
310 budgets => \@budgets_to_display,
311 periods => $periods,
312 budget_period_total => $budget_period_total,
313 period_alloc_total => $period_alloc_total,
314 spent_total => $spent_total,
315 ordered_total => $ordered_total,
316 available_total => $available_total,
317 filter_budgetname => $filter_budgetname,
320 } #---- END list
322 output_html_with_http_headers $input, $cookie, $template->output;