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