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') // 1;
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')
72 # get only the columns of aqbudgets in budget_hash
73 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
74 my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) ) : () } keys( %{$input->Vars()}) } ;
76 my $budget_id = $input->param('budget_id');
77 my $budget_period_id = $input->param('budget_period_id');
78 my $budget_permission = $input->param('budget_permission');
79 my $budget_users_ids = $input->param('budget_users_ids');
80 my $filter_budgetbranch = $input->param('filter_budgetbranch') // '';
81 my $filter_budgetname = $input->param('filter_budgetname');
84 # ' ------- get periods stuff ------------------'
85 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
87 if ( $budget_period_id ) {
88 $period = GetBudgetPeriod( $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 show_mine => $show_mine,
106 my $branches = GetBranches($show_mine);
108 foreach my $thisbranch (keys %$branches) {
110 value => $thisbranch,
111 branchname => $branches->{$thisbranch}->{'branchname'},
113 $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
114 push @branchloop2, \%row;
117 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
118 if $budget_period_id;
120 # Used to create form to add or modify a record
121 if ($op eq 'add_form') {
122 #### ------------------- ADD_FORM -------------------------
123 # if no buget_id is passed then its an add
124 # pass the period_id to build the dropbox - because we only want to show budgets from this period
125 my $dropbox_disabled;
126 if (defined $budget_id ) { ### MOD
127 $budget = GetBudget($budget_id);
128 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
129 $template->param(error_not_authorised_to_modify => 1);
130 output_html_with_http_headers $input, $cookie, $template->output;
133 $dropbox_disabled = BudgetHasChildren($budget_id);
134 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
135 $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
136 $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{ /amount|encumb|expend/ } keys %$budget;
139 # build budget hierarchy
142 my $hier = GetBudgetHierarchy($$period{budget_period_id});
143 foreach my $r (@$hier) {
144 $r->{budget_code_indent} =~ s/ /\~/g; #
145 $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
146 push @values, $r->{budget_id};
149 # if no buget_id is passed then its an add
151 my $budget_parent_id;
153 $budget_parent_id = $budget->{'budget_parent_id'} ;
155 $budget_parent_id = $input->param('budget_parent_id');
157 $budget_parent = GetBudget($budget_parent_id);
159 # build branches select
160 my $branches = GetBranches;
161 my @branchloop_select;
162 foreach my $thisbranch ( sort keys %$branches ) {
164 value => $thisbranch,
165 branchname => $branches->{$thisbranch}->{'branchname'},
167 $row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
168 push @branchloop_select, \%row;
171 # populates the YUI planning button
172 my $categories = GetAuthorisedValueCategories();
173 my @auth_cats_loop1 = ();
174 foreach my $category (@$categories) {
175 my $entry = { category => $category,
176 selected => $budget->{sort1_authcat} eq $category ?1:0,
178 push @auth_cats_loop1, $entry;
180 my @auth_cats_loop2 = ();
181 foreach my $category (@$categories) {
182 my $entry = { category => $category,
183 selected => $budget->{sort2_authcat} eq $category ?1:0,
185 push @auth_cats_loop2, $entry;
187 $template->param(authorised_value_categories1 => \@auth_cats_loop1);
188 $template->param(authorised_value_categories2 => \@auth_cats_loop2);
190 if($budget->{'budget_permission'}){
191 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
192 $template->param($budget_permission => 1);
196 my @budgetusers = GetBudgetUsers($budget->{budget_id});
197 my @budgetusers_loop;
198 foreach my $borrowernumber (@budgetusers) {
199 my $member = C4::Members::GetMember(
200 borrowernumber => $borrowernumber);
201 push @budgetusers_loop, {
202 firstname => $member->{firstname},
203 surname => $member->{surname},
204 borrowernumber => $borrowernumber
208 budget_users => \@budgetusers_loop,
209 budget_users_ids => join ':', @budgetusers
213 # if no buget_id is passed then its an add
215 budget_parent_id => $budget_parent->{'budget_id'},
216 budget_parent_name => $budget_parent->{'budget_name'},
217 branchloop_select => \@branchloop_select,
221 # END $OP eq ADD_FORM
222 #---------------------- DEFAULT DISPLAY BELOW ---------------------
224 # called by default form, used to confirm deletion of data in DB
225 } elsif ($op eq 'delete_confirm') {
227 my $budget = GetBudget($budget_id);
229 budget_id => $budget->{'budget_id'},
230 budget_code => $budget->{'budget_code'},
231 budget_name => $budget->{'budget_name'},
232 budget_amount => $num->format_price( $budget->{'budget_amount'} ),
234 # END $OP eq DELETE_CONFIRM
235 # called by delete_confirm, used to effectively confirm deletion of data in DB
236 } elsif ( $op eq 'delete_confirmed' ) {
237 my $rc = DelBudget($budget_id);
239 } elsif( $op eq 'add_validate' ) {
241 if (defined $budget_users_ids){
242 @budgetusersid = split(':', $budget_users_ids);
245 if (defined $budget_id) {
246 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
249 ModBudget( $budget_hash );
250 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
253 $template->param(error_not_authorised_to_modify => 1);
256 AddBudget( $budget_hash );
257 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
262 if ( $op eq 'list' ) {
263 my $branches = GetBranches();
265 budget_id => $budget_id,
270 GetBudgetHierarchy($$period{budget_period_id},
271 C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
274 my $period_total = 0;
275 my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
277 #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 ?
279 foreach my $budget (@budgets) {
281 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
282 $budget->{'budget_lock'} = 1;
285 # if a budget search doesnt match, next
286 if ($filter_budgetname) {
288 unless $budget->{budget_code} =~ m/$filter_budgetname/i
289 || $budget->{budget_name} =~ m/$filter_budgetname/i;
291 if ($filter_budgetbranch ) {
292 next unless $budget->{budget_branchcode} eq $filter_budgetbranch;
296 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
297 $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
298 # adds to total - only if budget is a 'top-level' budget
299 if ($budget->{depth} == 0) {
300 $period_alloc_total += $budget->{'budget_amount'};
301 $spent_total += $budget->{total_spent};
302 $ordered_total += $budget->{total_ordered};
303 $available_total += $budget->{total_remaining};
306 # if amount == 0 dont display...
307 delete $budget->{'budget_unalloc_sublevel'}
308 if (!defined $budget->{'budget_unalloc_sublevel'}
309 or $budget->{'budget_unalloc_sublevel'} == 0);
311 for (grep {/total_spent|budget_spent|total_ordered|budget_ordered|budget_amount/} keys %$budget){
312 $budget->{$_} = $num->format_price( $budget->{$_} ) if defined($budget->{$_})
314 for (qw/budget_remaining total_remaining/) {
315 if (defined $budget->{$_}) {
316 $budget->{$_.'_display'} = $num->format_price($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"});
322 $budget->{budget_ordered} = $num->format_price(0) unless defined($budget->{"budget_ordered"});
324 my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
325 $budget->{"budget_owner_name"} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
326 $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
328 #Make a list of parents of the bugdet
329 my @budget_hierarchy;
330 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
331 my $parent_id = $budget->{"budget_parent_id"};
333 my $parent = GetBudget($parent_id);
334 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
335 $parent_id = $parent->{"budget_parent_id"};
337 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
338 @budget_hierarchy = reverse(@budget_hierarchy);
340 $budget->{branchname} = $branches->{ $budget->{branchcode} }->{branchname};
341 $budget->{budget_hierarchy} = \@budget_hierarchy;
344 my $budget_period_total = $period->{budget_period_total};
346 foreach ($budget_period_total, $period_alloc_total, $spent_total, $ordered_total, $available_total) {
347 $_ = $num->format_price($_);
350 my $periods = GetBudgetPeriods();
354 budgets => \@budgets,
356 budget_period_total => $budget_period_total,
357 period_alloc_total => $period_alloc_total,
358 spent_total => $spent_total,
359 ordered_total => $ordered_total,
360 available_total => $available_total,
361 branchloop => \@branchloop2,
366 output_html_with_http_headers $input, $cookie, $template->output;