Bug 15721: About page does not display Apache version
[koha.git] / admin / aqplan.pl
blob7374428983504b0e84b3fd293cabf3b7a2b6d3eb
1 #!/usr/bin/perl
3 # Copyright 2008-2009 BibLibre SARL
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #script to administer the aqbudgets0 table
20 #written 20/02/2002 by paul.poulain@free.fr
21 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use CGI qw ( -utf8 );
26 use List::Util qw/min/;
27 use Date::Calc qw/Delta_YMD Easter_Sunday Today Decode_Date_EU/;
28 use Date::Manip qw/ ParseDate UnixDate DateCalc/;
29 use Text::CSV_XS;
31 use C4::Acquisition;
32 use C4::Budgets;
33 use C4::Context;
34 use C4::Output;
35 use C4::Koha;
36 use C4::Auth;
37 use C4::Debug;
39 my $input = new CGI;
40 #### $input
42 my $dbh = C4::Context->dbh;
44 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
45 { template_name => "admin/aqplan.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { acquisition => 'planning_manage' },
50 debug => 0,
54 my $budget_period_id = $input->param('budget_period_id');
55 # ' ------- get periods stuff ------------------'
56 # IF PERIOD_ID IS DEFINED, GET THE PERIOD - ELSE GET THE ACTIVE PERIOD BY DEFAULT
57 my $period = GetBudgetPeriod($budget_period_id);
58 my $count = GetPeriodsCount();
59 my $cur = GetCurrency;
60 $template->param( symbol => $cur->{symbol},
61 currency => $cur->{currency}
63 $template->param( period_button_only => 1 ) if $count == 0;
67 # authcats_loop populates the YUI planning button
68 my $auth_cats_loop = GetBudgetAuthCats($budget_period_id);
69 my $budget_period_id = $period->{'budget_period_id'};
70 my $budget_period_startdate = $period->{'budget_period_startdate'};
71 my $budget_period_enddate = $period->{'budget_period_enddate'};
72 my $budget_period_locked = $period->{'budget_period_locked'};
73 my $budget_period_description = $period->{'budget_period_description'};
76 $template->param(
77 budget_period_id => $budget_period_id,
78 budget_period_locked => $budget_period_locked,
79 budget_period_description => $budget_period_description,
80 auth_cats_loop => $auth_cats_loop,
83 # ------- get periods stuff ------------------
85 my $borrower_id = $template->{VARS}->{'USER_INFO'}->{'borrowernumber'};
86 my $borrower_branchcode = $template->{VARS}->{'USER_INFO'}->{'branchcode'};
88 my $authcat = $input->param('authcat');
89 my $show_active = $input->param('show_active');
90 my $show_actual = $input->param('show_actual');
91 my $show_percent = $input->param('show_percent');
92 my $output = $input->param("output");
93 my $basename = $input->param("basename");
94 my $del = $input->param("sep");
96 my $show_mine = $input->param('show_mine') ;
98 my @hide_cols = $input->param('hide_cols');
100 if ( $budget_period_locked == 1 && not defined $show_actual ) {
101 $show_actual = 1;
104 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
106 my $budget_id = $input->param('budget_id');
107 my $op = $input->param("op");
109 my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{VARS}->{'USER_INFO'}->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}->{'borrowernumber'}:'' );
111 # build categories list
112 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
113 $sth->execute;
115 # the list
116 my @category_list;
118 # a hash, to check that some hardcoded categories exist.
119 my %categories;
120 while ( my ($category) = $sth->fetchrow_array ) {
121 push( @category_list, $category );
122 $categories{$category} = 1;
125 # push koha system categories
126 push( @category_list, 'MONTHS' );
127 push( @category_list, 'ITEMTYPES' );
128 push( @category_list, 'BRANCHES' );
129 push( @category_list, $$_{'authcat'} ) foreach @$auth_cats_loop;
131 #reorder the list
132 @category_list = sort { $a cmp $b } @category_list;
134 $template->param( authcat_dropbox => {
135 values => \@category_list,
136 default => $authcat,
139 my @budgets = @$budgets_ref;
140 my @authvals;
141 my %labels;
143 my @names = $input->param();
144 # ------------------------------------------------------------
145 if ( $op eq 'save' ) {
146 #get budgets
147 my ( @buds, @auth_values );
148 foreach my $n (@names) {
149 next if $n =~ m/^[^0-9]/;
150 my @moo = split( ',', $n );
151 push @buds, $moo[0];
152 push @auth_values, $moo[1];
155 #uniq buds and auth
156 my %seen;
157 @buds = grep { !$seen{$_}++ } @buds;
158 @auth_values = grep { !$seen{$_}++ } @auth_values;
159 my @budget_lines;
161 foreach my $budget (@buds) {
162 my %budget_line;
163 my @cells_line;
164 my %cell_hash;
166 foreach my $authvalue (@auth_values) {
167 # get actual stats
168 my $cell_name = "$budget,$authvalue";
169 my $estimated_amount = $input->param("$cell_name");
170 my %cell_hash = (
171 estimated_amount => $estimated_amount,
172 authvalue => $authvalue,
173 authcat => $authcat,
174 budget_id => $budget,
175 budget_period_id => $budget_period_id,
177 push( @cells_line, \%cell_hash );
180 %budget_line = (
181 lines => \@cells_line,
183 push( @budget_lines, \%budget_line );
185 my $plan = \@budget_lines;
186 ModBudgetPlan( $plan, $budget_period_id, $authcat );
188 HideCols($authcat, @hide_cols);
192 # ------------------------------------------------------------
193 if ( $authcat =~ m/^Asort/ ) {
194 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
195 my $sth = $dbh->prepare($query);
196 $sth->execute($authcat );
197 if ( $sth->rows > 0 ) {
198 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
199 my $results = $sth->fetchrow_hashref;
200 push @authvals, $results->{authorised_value};
201 $labels{ $results->{authorised_value} } = $results->{lib};
204 $sth->finish;
205 @authvals = sort { $a <=> $b } @authvals;
207 elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
209 # build months
210 my @start_date = UnixDate( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
211 my @end_date = UnixDate( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
213 my ( $Dy, $Dm, $Dd ) = Delta_YMD( @start_date, @end_date );
215 #calc number of months between
216 my $months = ( $Dy * 12 ) + $Dm;
217 my $start_month = @start_date[1];
218 my $end_month = ( $Dy * 12 ) + $Dm;
220 for my $mth ( 0 ... $months ) {
221 $mth = DateCalc( $budget_period_startdate, "+ $mth months" );
222 $mth = UnixDate( $mth, "%Y-%m" );
223 push( @authvals, $mth );
225 foreach my $vv (@authvals) {
226 $labels{$vv} = $vv;
230 elsif ( $authcat eq 'ITEMTYPES' ) {
231 my $query = qq| SELECT itemtype, description FROM itemtypes |;
232 my $sth = $dbh->prepare($query);
233 $sth->execute( );
235 if ( $sth->rows > 0 ) {
236 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
237 my $results = $sth->fetchrow_hashref;
238 push @authvals, $results->{itemtype};
239 $labels{ $results->{itemtype} } = $results->{description};
242 $sth->finish;
244 } elsif ( $authcat eq 'BRANCHES' ) {
246 my $query = qq| SELECT branchcode, branchname FROM branches |;
247 my $sth = $dbh->prepare($query);
248 $sth->execute();
250 if ( $sth->rows > 0 ) {
251 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
252 my $results = $sth->fetchrow_hashref;
253 push @authvals, $results->{branchcode};
254 $labels{ $results->{branchcode} } = $results->{branchname};
257 $sth->finish;
258 } elsif ($authcat) {
259 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
260 my $sth = $dbh->prepare($query);
261 $sth->execute($authcat);
262 if ( $sth->rows > 0 ) {
263 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
264 my $results = $sth->fetchrow_hashref;
265 push @authvals, $results->{authorised_value};
266 $labels{ $results->{authorised_value} } = $results->{lib};
269 $sth->finish;
272 my @authvals_row;
273 my $i=1;
274 foreach my $val (@authvals) {
275 my %auth_hash;
276 $auth_hash{val} = $labels{$val};
277 $auth_hash{code} = $val;
278 $auth_hash{colnum} = $i++;
280 # display lookup
281 $auth_hash{display} = GetCols( $authcat, $auth_hash{code});
283 push( @authvals_row, \%auth_hash );
286 #get budgets
287 my ( @buds, @auth_values );
288 foreach my $n (@names) {
289 next if $n =~ m/^[^0-9]/;
290 $n =~ m/(\d*),(.*)/;
291 push @buds, $1;
292 push @auth_values, $2;
296 # ------------------------------------------------------------
297 # DEFAULT DISPLAY BEGINS
299 my $CGIextChoice = ( 'CSV' ); # FIXME translation
300 my $CGIsepChoice = ( C4::Context->preference("delimiter") );
302 my ( @budget_lines, %cell_hash );
305 foreach my $budget (@budgets) {
306 my $budget_lock;
308 unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
309 $budget_lock = 1
312 # check budget permission
313 if ( $period->{budget_period_locked} == 1 ) {
314 $budget_lock = 1;
315 } elsif ( $budget->{budget_permission} == 1 ) {
316 $budget_lock = 1 if $borrower_id != $budget->{'budget_owner_id'};
317 } elsif ( $budget->{budget_permission} == 2 ) {
318 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode};
321 # allow hard-coded itemtype and branch planning
322 unless ( $authcat eq 'ITEMTYPES'
323 or $authcat eq 'BRANCHES'
324 or $authcat eq 'MONTHS' ) {
326 # but skip budgets that don't match the current auth-category
327 next if ( $budget->{'sort1_authcat'} ne $authcat
328 && $budget->{'sort2_authcat'} ne $authcat );
331 my %budget_line; # each row of the table
332 my @cells_line;
333 my $actual_spent;
334 my $estimated_spent;
336 my $i = 0;
337 foreach my $authvalue (@authvals) {
339 # get actual stats
340 my %cell = (
341 budget_id => $budget->{'budget_id'},
342 budget_period_id => $budget->{'budget_period_id'},
343 cell_name => $budget->{'budget_id'} . ',' . $authvalue,
344 authvalue => $authvalue,
345 authcat => $authcat,
346 cell_authvalue => $authvalue,
347 budget_lock => $budget_lock,
350 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell( \%cell, $period, $budget );
351 $cell{actual_amount} = sprintf( "%.2f", $actual );
352 $cell{estimated_amount} = sprintf( "%.2f", $estimated );
353 $cell{display} = $authvals_row[$i]{display};
354 $cell{colnum} = $i;
356 $actual_spent += $cell{actual_amount};
357 $estimated_spent += $cell{estimated_amount};
360 push( @cells_line, \%cell );
361 $i++;
364 my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
365 my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
367 %budget_line = (
368 lines => \@cells_line,
369 budget_name => $budget->{budget_name},
370 budget_amount => $budget->{budget_amount},
371 budget_alloc => $budget->{budget_alloc},
372 budget_act_remain => sprintf( "%.2f", $budget_act_remain ),
373 budget_est_remain => sprintf( "%.2f", $budget_est_remain ),
374 budget_id => $budget->{budget_id},
375 budget_lock => $budget_lock,
380 $budget_line{est_negative} = '1' if $budget_est_remain < 0;
381 $budget_line{est_positive} = '1' if $budget_est_remain > 0;
382 $budget_line{act_negative} = '1' if $budget_act_remain < 0;
383 $budget_line{act_positive} = '1' if $budget_act_remain > 0;
385 # skip if active set , and spent == 0
386 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
388 push( @budget_lines, \%budget_line );
391 if ( $output eq "file" ) {
392 _print_to_csv(\@authvals_row, \@budget_lines);
393 exit(1);
396 my $branchloop = C4::Branch::GetBranchesLoop();
397 $template->param(
398 authvals_row => \@authvals_row,
399 budget_lines => \@budget_lines,
400 budget_period_description => $period->{'budget_period_description'},
401 budget_period_locked => $period->{'budget_period_locked'},
402 budget_period_id => $budget_period_id,
403 authcat => $authcat,
404 show_active => $show_active,
405 show_actual => $show_actual,
406 show_percent => $show_percent,
407 show_mine => $show_mine,
408 CGIextChoice => $CGIextChoice,
409 CGIsepChoice => $CGIsepChoice,
411 authvals => \@authvals_row,
412 hide_cols_loop => \@hide_cols,
413 branchloop => $branchloop,
416 output_html_with_http_headers $input, $cookie, $template->output;
418 sub _print_to_csv {
419 my ( $header, $results ) = @_;
421 binmode STDOUT, ':encoding(UTF-8)';
423 my $csv = Text::CSV_XS->new(
424 { sep_char => $del,
425 always_quote => 'TRUE',
428 print $input->header(
429 -type => 'application/vnd.sun.xml.calc',
430 -encoding => 'utf-8',
431 -attachment => "$basename.csv",
432 -name => "$basename.csv"
434 my @col = ( 'Budget name', 'Budget total' );
435 foreach my $row (@$header) {
436 push @col, $row->{'val'};
438 push @col, 'Budget remaining';
440 $csv->combine(@col);
441 my $str = $csv->string;
442 print "$str\n";
444 foreach my $row (@$results) {
445 my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
446 my $l = $row->{'lines'};
447 foreach my $line (@$l) {
448 push @col, $line->{'estimated_amount'};
450 push @col, $row->{'budget_est_remain'};
451 $csv->combine(@col);
452 my $str = $csv->string;
453 print "$str\n";