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.
25 use List::Util qw/min/;
26 use Number::Format qw(format_price);
29 use C4::Auth qw/get_user_subpermissions/;
30 use C4::Branch; # GetBranches
31 use C4::Dates qw/format_date format_date_in_iso/;
35 use C4::Members; # calls GetSortDetails()
42 my $dbh = C4::Context->dbh;
44 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
45 { template_name => "admin/aqbudgets.tt",
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') || '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 DOESNT 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')
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) =~ /$_/ ? ( $_ => $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
86 if ( $budget_period_id ) {
87 $period = GetBudgetPeriod( $budget_period_id );
90 # ------- get periods stuff ------------------
92 # USED FOR PERMISSION COMPARISON LATER
93 my $borrower_id = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
94 my $user = GetMemberDetails($borrower_id);
95 my $user_branchcode = $user->{'branchcode'};
98 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( $budget_period_id ))
116 if $budget_period_id;
118 # Used to create form to add or modify a record
119 if ($op eq 'add_form') {
120 #### ------------------- 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 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
127 $template->param(error_not_authorised_to_modify => 1);
128 output_html_with_http_headers $input, $cookie, $template->output;
131 $dropbox_disabled = BudgetHasChildren($budget_id);
132 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
133 $budget->{budget_owner_name} = ( $borrower ? $borrower->{'firstname'} . ' ' . $borrower->{'surname'} : '' );
134 $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{ /amount|encumb|expend/ } keys %$budget;
137 # build budget hierarchy
140 my $hier = GetBudgetHierarchy($$period{budget_period_id});
141 foreach my $r (@$hier) {
142 $r->{budget_code_indent} =~ s/ /\~/g; #
143 $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
144 push @values, $r->{budget_id};
147 # if no buget_id is passed then its an add
149 my $budget_parent_id;
151 $budget_parent_id = $budget->{'budget_parent_id'} ;
153 $budget_parent_id = $input->param('budget_parent_id');
155 $budget_parent = GetBudget($budget_parent_id);
157 # build branches select
158 my $branches = GetBranches;
159 my @branchloop_select;
160 foreach my $thisbranch ( sort keys %$branches ) {
162 value => $thisbranch,
163 branchname => $branches->{$thisbranch}->{'branchname'},
165 $row{selected} = 1 if $budget and $thisbranch eq $budget->{'budget_branchcode'};
166 push @branchloop_select, \%row;
169 # populates the YUI planning button
170 my $categories = GetAuthorisedValueCategories();
171 my @auth_cats_loop1 = ();
172 foreach my $category (@$categories) {
173 my $entry = { category => $category,
174 selected => ( $budget and $budget->{sort1_authcat} eq $category ? 1 : 0 ),
176 push @auth_cats_loop1, $entry;
178 my @auth_cats_loop2 = ();
179 foreach my $category (@$categories) {
180 my $entry = { category => $category,
181 selected => ( $budget and $budget->{sort2_authcat} eq $category ? 1 : 0 ),
183 push @auth_cats_loop2, $entry;
185 $template->param(authorised_value_categories1 => \@auth_cats_loop1);
186 $template->param(authorised_value_categories2 => \@auth_cats_loop2);
188 if($budget->{'budget_permission'}){
189 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
190 $template->param($budget_permission => 1);
194 my @budgetusers = GetBudgetUsers($budget->{budget_id});
195 my @budgetusers_loop;
196 foreach my $borrowernumber (@budgetusers) {
197 my $member = C4::Members::GetMember(
198 borrowernumber => $borrowernumber);
199 push @budgetusers_loop, {
200 firstname => $member->{firstname},
201 surname => $member->{surname},
202 borrowernumber => $borrowernumber
206 budget_users => \@budgetusers_loop,
207 budget_users_ids => join ':', @budgetusers
211 # if no buget_id is passed then its an add
213 budget_parent_id => $budget_parent->{'budget_id'},
214 budget_parent_name => $budget_parent->{'budget_name'},
215 branchloop_select => \@branchloop_select,
219 # END $OP eq ADD_FORM
220 #---------------------- DEFAULT DISPLAY BELOW ---------------------
222 # called by default form, used to confirm deletion of data in DB
223 } elsif ($op eq 'delete_confirm') {
225 my $budget = GetBudget($budget_id);
227 budget_id => $budget->{'budget_id'},
228 budget_code => $budget->{'budget_code'},
229 budget_name => $budget->{'budget_name'},
230 budget_amount => $budget->{'budget_amount'},
232 # END $OP eq DELETE_CONFIRM
233 # called by delete_confirm, used to effectively confirm deletion of data in DB
234 } elsif ( $op eq 'delete_confirmed' ) {
235 my $rc = DelBudget($budget_id);
237 } elsif( $op eq 'add_validate' ) {
239 if (defined $budget_users_ids){
240 @budgetusersid = split(':', $budget_users_ids);
243 if (defined $budget_id) {
244 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
247 ModBudget( $budget_hash );
248 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
251 $template->param(error_not_authorised_to_modify => 1);
254 AddBudget( $budget_hash );
255 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
260 if ( $op eq 'list' ) {
261 my $branches = GetBranches();
263 budget_id => $budget_id,
268 GetBudgetHierarchy($$period{budget_period_id},
269 C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
272 my $period_total = 0;
273 my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
275 #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 ?
277 foreach my $budget (@budgets) {
279 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
280 $budget->{'budget_lock'} = 1;
283 # if a budget search doesnt match, next
284 if ($filter_budgetname) {
286 unless $budget->{budget_code} =~ m/$filter_budgetname/i
287 || $budget->{budget_name} =~ m/$filter_budgetname/i;
289 if ($filter_budgetbranch ) {
290 next unless $budget->{budget_branchcode} eq $filter_budgetbranch;
294 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
295 $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
296 # adds to total - only if budget is a 'top-level' budget
297 if ($budget->{depth} == 0) {
298 $period_alloc_total += $budget->{'budget_amount'};
299 $spent_total += $budget->{total_spent};
300 $ordered_total += $budget->{total_ordered};
301 $available_total += $budget->{total_remaining};
304 # if amount == 0 dont display...
305 delete $budget->{'budget_unalloc_sublevel'}
306 if (!defined $budget->{'budget_unalloc_sublevel'}
307 or $budget->{'budget_unalloc_sublevel'} == 0);
309 # Value of budget_spent equals 0 instead of undefined value
310 $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
311 $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
313 #Make a list of parents of the bugdet
314 my @budget_hierarchy;
315 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
316 my $parent_id = $budget->{"budget_parent_id"};
318 my $parent = GetBudget($parent_id);
319 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
320 $parent_id = $parent->{"budget_parent_id"};
322 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
323 @budget_hierarchy = reverse(@budget_hierarchy);
325 $budget->{budget_hierarchy} = \@budget_hierarchy;
328 my $budget_period_total = $period->{budget_period_total};
330 my $periods = GetBudgetPeriods();
334 budgets => \@budgets,
336 budget_period_total => $budget_period_total,
337 period_alloc_total => $period_alloc_total,
338 spent_total => $spent_total,
339 ordered_total => $ordered_total,
340 available_total => $available_total,
341 branchloop => \@branchloop2,
346 output_html_with_http_headers $input, $cookie, $template->output;