Bug 26180: Add descending option to rebuild_elasticsearch.pl
[koha.git] / admin / aqbudgets.pl
blob16f62c5e79c08ef413eabe4e53698d20ad5ba320
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 = CGI->new;
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 flagsrequired => { acquisition => 'budget_manage' },
47 debug => 0,
51 my $active_currency = Koha::Acquisition::Currencies->get_active;
52 if ( $active_currency ) {
53 $template->param( symbol => $active_currency->symbol,
54 currency => $active_currency->currency
58 my $op = $input->param('op') || 'list';
60 # see if the user want to see all budgets or only owned ones by default
61 my $show_mine = $input->param('show_mine') // 0;
63 # IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
64 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
65 and $op eq 'add_form')
67 $op = 'list';
70 # get only the columns of aqbudgets in budget_hash
71 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
72 my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => scalar $input->param($_) ) : () } keys( %{$input->Vars()}) } ;
74 my $budget_id = $input->param('budget_id');
75 my $budget_period_id = $input->param('budget_period_id');
76 my $budget_permission = $input->param('budget_permission');
77 my $budget_users_ids = $input->param('budget_users_ids');
78 my $filter_budgetbranch = $input->param('filter_budgetbranch') // '';
79 my $filter_budgetname = $input->param('filter_budgetname');
82 # ' ------- get periods stuff ------------------'
83 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
84 my $period;
85 if ( $budget_period_id ) {
86 $period = GetBudgetPeriod( $budget_period_id );
89 # ------- get periods stuff ------------------
91 $template->param(
92 show_mine => $show_mine,
93 op => $op,
94 selected_branchcode => $filter_budgetbranch,
97 my $budget;
99 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
100 if $budget_period_id;
102 # Used to create form to add or modify a record
103 if ($op eq 'add_form') {
104 #### ------------------- ADD_FORM -------------------------
105 # if no buget_id is passed then its an add
106 # pass the period_id to build the dropbox - because we only want to show budgets from this period
107 my $dropbox_disabled;
108 if (defined $budget_id ) { ### MOD
109 $budget = GetBudget($budget_id);
110 if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
111 $template->param(error_not_authorised_to_modify => 1);
112 output_html_with_http_headers $input, $cookie, $template->output;
113 exit;
115 $dropbox_disabled = BudgetHasChildren($budget_id);
116 $budget->{budget_owner} = Koha::Patrons->find( $budget->{budget_owner_id} );
117 } elsif ( $period->{budget_period_locked} ) {
118 output_and_exit( $input, $cookie, $template, 'budget_is_locked' );
121 # build budget hierarchy
122 my %labels;
123 my @values;
124 my $hier = GetBudgetHierarchy($$period{budget_period_id});
125 foreach my $r (@$hier) {
126 $labels{"$r->{budget_id}"} = $r->{budget_code};
127 push @values, $r->{budget_id};
129 push @values, '';
130 # if no buget_id is passed then its an add
131 my $budget_parent;
132 my $budget_parent_id;
133 if ($budget){
134 $budget_parent_id = $budget->{'budget_parent_id'} ;
135 }else{
136 $budget_parent_id = $input->param('budget_parent_id');
138 $budget_parent = GetBudget($budget_parent_id);
140 # populates the planning button
141 $template->param(
142 sort1_auth => $budget->{sort1_authcat},
143 sort2_auth => $budget->{sort2_authcat},
146 if($budget->{'budget_permission'}){
147 my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
148 $template->param($budget_permission => 1);
151 if ($budget) {
152 my @budgetusers = GetBudgetUsers($budget->{budget_id});
153 my @budgetusers_loop;
154 foreach my $borrowernumber (@budgetusers) {
155 my $patron = Koha::Patrons->find( $borrowernumber );
156 push @budgetusers_loop, {
157 firstname => $patron->firstname, # FIXME Should pass the patron object
158 surname => $patron->surname,
159 borrowernumber => $borrowernumber
162 $template->param(
163 budget_users => \@budgetusers_loop,
164 budget_users_ids => join ':', @budgetusers
168 # if no buget_id is passed then its an add
169 $template->param(
170 budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
171 budget_parent_id => $budget_parent->{'budget_id'},
172 budget_parent_name => $budget_parent->{'budget_name'},
173 %$period,
174 %$budget,
176 # END $OP eq ADD_FORM
177 #---------------------- DEFAULT DISPLAY BELOW ---------------------
179 # called by default form, used to confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirm') {
182 my $budget = GetBudget($budget_id);
183 $template->param(
184 budget_id => $budget->{'budget_id'},
185 budget_code => $budget->{'budget_code'},
186 budget_name => $budget->{'budget_name'},
187 budget_amount => $budget->{'budget_amount'},
189 # END $OP eq DELETE_CONFIRM
190 # called by delete_confirm, used to effectively confirm deletion of data in DB
191 } elsif ( $op eq 'delete_confirmed' ) {
192 if ( BudgetHasChildren( $budget_id ) ) {
193 # We should never be here, the interface does not provide this action.
194 die("Delete a fund with children is not possible");
196 my $rc = DelBudget($budget_id);
197 $op = 'list';
198 } elsif( $op eq 'add_validate' ) {
199 my @budgetusersid;
200 if (defined $budget_users_ids){
201 @budgetusersid = split(':', $budget_users_ids);
204 my $budget_modified = 0;
205 if (defined $budget_id) {
206 if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
207 $staffflags)
209 ModBudget( $budget_hash );
210 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
211 $budget_modified = 1;
213 else {
214 $template->param(error_not_authorised_to_modify => 1);
216 } else {
217 $budget_hash->{budget_id} = AddBudget( $budget_hash );
218 ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
219 $budget_modified = 1;
222 my $set_owner_to_children = $input->param('set_owner_to_children');
223 if ( $set_owner_to_children and $budget_modified ) {
224 C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} );
226 $op = 'list';
229 if ( $op eq 'list' ) {
230 $template->param(
231 budget_id => $budget_id,
232 %$period,
235 my @budgets = @{
236 GetBudgetHierarchy( $$period{budget_period_id}, undef, ( $show_mine ? $borrowernumber : 0 ))
239 my $period_total = 0;
240 my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
242 #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 ?
244 my @budgets_to_display;
245 foreach my $budget (@budgets) {
246 # PERMISSIONS
247 next unless CanUserUseBudget($borrowernumber, $budget, $staffflags);
248 unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
249 $budget->{'budget_lock'} = 1;
252 # if a budget search doesn't match, next
253 if ($filter_budgetname) {
254 next
255 unless $budget->{budget_code} =~ m/$filter_budgetname/i
256 || $budget->{budget_name} =~ m/$filter_budgetname/i;
258 if ($filter_budgetbranch ) {
259 next unless $budget->{budget_branchcode} eq $filter_budgetbranch;
262 ## TOTALS
263 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
264 $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
265 # adds to total - only if budget is a 'top-level' budget
266 unless ( defined $budget->{budget_parent_id} ) {
267 $period_alloc_total += $budget->{'budget_amount'};
268 $spent_total += $budget->{total_spent};
269 $ordered_total += $budget->{total_ordered};
270 $available_total += $budget->{total_remaining};
273 # if amount == 0 don't display...
274 delete $budget->{'budget_unalloc_sublevel'}
275 if (!defined $budget->{'budget_unalloc_sublevel'}
276 or $budget->{'budget_unalloc_sublevel'} == 0);
278 # Value of budget_spent equals 0 instead of undefined value
279 $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
280 $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
282 #Make a list of parents of the bugdet
283 my @budget_hierarchy;
284 push @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
285 my $parent_id = $budget->{"budget_parent_id"};
286 while ($parent_id) {
287 my $parent = GetBudget($parent_id);
288 push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
289 $parent_id = $parent->{"budget_parent_id"};
291 push @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
292 @budget_hierarchy = reverse(@budget_hierarchy);
294 $budget->{budget_hierarchy} = \@budget_hierarchy;
296 $budget->{budget_has_children} = BudgetHasChildren( $budget->{budget_id} );
297 push @budgets_to_display, $budget;
300 my $budget_period_total = $period->{budget_period_total};
302 my $periods = GetBudgetPeriods();
304 $template->param(
305 op => 'list',
306 budgets => \@budgets_to_display,
307 periods => $periods,
308 budget_period_total => $budget_period_total,
309 period_alloc_total => $period_alloc_total,
310 spent_total => $spent_total,
311 ordered_total => $ordered_total,
312 available_total => $available_total,
313 filter_budgetname => $filter_budgetname,
316 } #---- END list
318 output_html_with_http_headers $input, $cookie, $template->output;