0cf65271109922482b400d27079171b786b224da
[srvgit] / admin / aqbudgets.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4
5 # Copyright 2008-2009 BibLibre SARL
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use List::Util qw/min/;
26
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 Koha::Acquisition::Currencies;
36 use Koha::Patrons;
37
38 my $input = CGI->new;
39 my $dbh     = C4::Context->dbh;
40
41 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
42     {   template_name   => "admin/aqbudgets.tt",
43         query           => $input,
44         type            => "intranet",
45         flagsrequired   => { acquisition => 'budget_manage' },
46     }
47 );
48
49 my $active_currency = Koha::Acquisition::Currencies->get_active;
50 if ( $active_currency ) {
51     $template->param( symbol => $active_currency->symbol,
52                       currency => $active_currency->currency
53                    );
54 }
55
56 my $op = $input->param('op') || 'list';
57
58 # see if the user want to see all budgets or only owned ones by default
59 my $show_mine = $input->param('show_mine') // 0;
60
61 # IF USER DOESN'T HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
62 if (not defined $template->{VARS}->{'CAN_user_acquisition_budget_add_del'}
63     and $op eq 'add_form')
64 {
65     $op = 'list';
66 }
67
68 # get only the columns of aqbudgets in budget_hash
69 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
70 my $budget_hash = { map { join(' ',@columns) =~ /$_/ ? ( $_ => scalar $input->param($_) )  : () } keys( %{$input->Vars()}) } ;
71
72 my $budget_id                 = $input->param('budget_id');
73 my $budget_period_id          = $input->param('budget_period_id');
74 my $budget_permission         = $input->param('budget_permission');
75 my $budget_users_ids          = $input->param('budget_users_ids');
76 my $filter_budgetbranch       = $input->param('filter_budgetbranch') // '';
77 my $filter_budgetname         = $input->param('filter_budgetname');
78
79
80 # ' ------- get periods stuff ------------------'
81 # IF PERIODID IS DEFINED,  GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
82 my $period;
83 if ( $budget_period_id ) {
84     $period = GetBudgetPeriod( $budget_period_id );
85 }
86
87 # ------- get periods stuff ------------------
88
89 $template->param(
90     show_mine   => $show_mine,
91     op  => $op,
92     selected_branchcode => $filter_budgetbranch,
93 );
94
95 my $budget;
96
97 $template->param(auth_cats_loop => GetBudgetAuthCats( $budget_period_id ))
98     if $budget_period_id;
99
100 # Used to create form to add or  modify a record
101 if ($op eq 'add_form') {
102 #### ------------------- ADD_FORM -------------------------
103     # if no buget_id is passed then its an add
104     #  pass the period_id to build the dropbox - because we only want to show  budgets from this period
105     my $dropbox_disabled;
106     if (defined $budget_id ) {    ### MOD
107         $budget = GetBudget($budget_id);
108         if (!CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
109             $template->param(error_not_authorised_to_modify => 1);
110             output_html_with_http_headers $input, $cookie, $template->output;
111             exit;
112         }
113         $dropbox_disabled = BudgetHasChildren($budget_id);
114         $budget->{budget_owner} = Koha::Patrons->find( $budget->{budget_owner_id} );
115     } elsif ( $period->{budget_period_locked} ) {
116         output_and_exit( $input, $cookie, $template, 'budget_is_locked' );
117     }
118
119     # build budget hierarchy
120     my %labels;
121     my @values;
122     my $hier = GetBudgetHierarchy($$period{budget_period_id});
123     foreach my $r (@$hier) {
124         $labels{"$r->{budget_id}"} = $r->{budget_code};
125         push @values, $r->{budget_id};
126     }
127     push @values, '';
128     # if no buget_id is passed then its an add
129     my $budget_parent;
130     my $budget_parent_id;
131     if ($budget){
132         $budget_parent_id = $budget->{'budget_parent_id'} ;
133     }else{
134         $budget_parent_id = $input->param('budget_parent_id');
135     }
136     $budget_parent = GetBudget($budget_parent_id);
137
138     # populates the planning button
139     $template->param(
140         sort1_auth => $budget->{sort1_authcat},
141         sort2_auth => $budget->{sort2_authcat},
142     );
143
144     if($budget->{'budget_permission'}){
145         my $budget_permission = "budget_perm_".$budget->{'budget_permission'};
146         $template->param($budget_permission => 1);
147     }
148
149     if ($budget) {
150         my @budgetusers = GetBudgetUsers($budget->{budget_id});
151         my @budgetusers_loop;
152         foreach my $borrowernumber (@budgetusers) {
153             my $patron = Koha::Patrons->find( $borrowernumber );
154             push @budgetusers_loop, {
155                 firstname => $patron->firstname, # FIXME Should pass the patron object
156                 surname => $patron->surname,
157                 borrowernumber => $borrowernumber
158             };
159         }
160         $template->param(
161             budget_users => \@budgetusers_loop,
162             budget_users_ids => join ':', @budgetusers
163         );
164     }
165
166     # if no buget_id is passed then its an add
167     $template->param(
168         budget_has_children => BudgetHasChildren( $budget->{budget_id} ),
169         budget_parent_id                  => $budget_parent->{'budget_id'},
170         budget_parent_name                => $budget_parent->{'budget_name'},
171                 %$period,
172                 %$budget,
173     );
174                                                     # END $OP eq ADD_FORM
175 #---------------------- DEFAULT DISPLAY BELOW ---------------------
176
177 # called by default form, used to confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirm') {
179
180     my $budget = GetBudget($budget_id);
181     $template->param(
182         budget_id     => $budget->{'budget_id'},
183         budget_code   => $budget->{'budget_code'},
184         budget_name   => $budget->{'budget_name'},
185         budget_amount => $budget->{'budget_amount'},
186     );
187                                                     # END $OP eq DELETE_CONFIRM
188 # called by delete_confirm, used to effectively confirm deletion of data in DB
189 } elsif ( $op eq 'delete_confirmed' ) {
190     if ( BudgetHasChildren( $budget_id ) ) {
191         # We should never be here, the interface does not provide this action.
192         die("Delete a fund with children is not possible");
193     }
194     my $rc = DelBudget($budget_id);
195     $op = 'list';
196 } elsif( $op eq 'add_validate' ) {
197     my @budgetusersid;
198     if (defined $budget_users_ids){
199         @budgetusersid = split(':', $budget_users_ids);
200     }
201
202     my $budget_modified = 0;
203     if (defined $budget_id) {
204         if (CanUserModifyBudget($borrowernumber, $budget_hash->{budget_id},
205             $staffflags)
206         ) {
207             ModBudget( $budget_hash );
208             ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
209             $budget_modified = 1;
210         }
211         else {
212             $template->param(error_not_authorised_to_modify => 1);
213         }
214     } else {
215         $budget_hash->{budget_id} = AddBudget( $budget_hash );
216         ModBudgetUsers($budget_hash->{budget_id}, @budgetusersid);
217         $budget_modified = 1;
218     }
219
220     my $set_owner_to_children = $input->param('set_owner_to_children');
221     if ( $set_owner_to_children and $budget_modified ) {
222         C4::Budgets::SetOwnerToFundHierarchy( $budget_hash->{budget_id}, $budget_hash->{budget_owner_id} );
223     }
224     $op = 'list';
225 }
226
227 if ( $op eq 'list' ) {
228     $template->param(
229         budget_id => $budget_id,
230         %$period,
231     );
232
233     my @budgets = @{
234         GetBudgetHierarchy( $$period{budget_period_id}, undef, ( $show_mine ? $borrowernumber : 0 ))
235     };
236
237     my $period_total = 0;
238     my ($period_alloc_total, $spent_total, $ordered_total, $available_total) = (0,0,0,0);
239
240         #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 ?
241
242     my @budgets_to_display;
243     foreach my $budget (@budgets) {
244         # PERMISSIONS
245         next unless CanUserUseBudget($borrowernumber, $budget, $staffflags);
246         unless(CanUserModifyBudget($borrowernumber, $budget, $staffflags)) {
247             $budget->{'budget_lock'} = 1;
248         }
249
250         # if a budget search doesn't match, next
251         if ($filter_budgetname) {
252             next
253               unless $budget->{budget_code} =~ m/$filter_budgetname/i
254                   || $budget->{budget_name} =~ m/$filter_budgetname/i;
255         }
256         if ($filter_budgetbranch ) {
257             next unless  $budget->{budget_branchcode} eq $filter_budgetbranch;
258         }
259
260 ## TOTALS
261         $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'} - $budget->{budget_ordered};
262         $budget->{'total_remaining'} = $budget->{'budget_amount'} - $budget->{'total_spent'} - $budget->{total_ordered};
263         # adds to total  - only if budget is a 'top-level' budget
264         unless ( defined $budget->{budget_parent_id} ) {
265             $period_alloc_total += $budget->{'budget_amount'};
266             $spent_total += $budget->{total_spent};
267             $ordered_total += $budget->{total_ordered};
268             $available_total += $budget->{total_remaining};
269         }
270
271 # if amount == 0 don't display...
272         delete $budget->{'budget_unalloc_sublevel'}
273             if (!defined $budget->{'budget_unalloc_sublevel'}
274             or $budget->{'budget_unalloc_sublevel'} == 0);
275
276         # Value of budget_spent equals 0 instead of undefined value
277         $budget->{budget_spent} = 0 unless defined($budget->{budget_spent});
278         $budget->{budget_ordered} = 0 unless defined($budget->{budget_ordered});
279
280         #Make a list of parents of the bugdet
281         my @budget_hierarchy;
282         push  @budget_hierarchy, { element_name => $budget->{"budget_name"}, element_id => $budget->{"budget_id"} };
283         my $parent_id = $budget->{"budget_parent_id"};
284         while ($parent_id) {
285             my $parent = GetBudget($parent_id);
286             push @budget_hierarchy, { element_name => $parent->{"budget_name"}, element_id => $parent->{"budget_id"} };
287             $parent_id = $parent->{"budget_parent_id"};
288         }
289         push  @budget_hierarchy, { element_name => $period->{"budget_period_description"} };
290         @budget_hierarchy = reverse(@budget_hierarchy);
291
292         $budget->{budget_hierarchy} = \@budget_hierarchy;
293
294         $budget->{budget_has_children} = BudgetHasChildren( $budget->{budget_id} );
295         push @budgets_to_display, $budget;
296     }
297
298     my $budget_period_total = $period->{budget_period_total};
299
300     my $periods = GetBudgetPeriods();
301
302     $template->param(
303         op                     => 'list',
304         budgets                => \@budgets_to_display,
305         periods                => $periods,
306         budget_period_total    => $budget_period_total,
307         period_alloc_total     => $period_alloc_total,
308         spent_total            => $spent_total,
309         ordered_total          => $ordered_total,
310         available_total        => $available_total,
311         filter_budgetname      => $filter_budgetname,
312     );
313
314 } #---- END list
315
316 output_html_with_http_headers $input, $cookie, $template->output;