Bug 11593 - subtag_registry.sql files contain the same information for each language
[koha.git] / admin / aqplan.pl
blob53fa41559451511c1c6264ec0db2932de65df63c
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 under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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 C4::Dates qw/format_date format_date_in_iso/;
30 use Number::Format qw(format_price);
31 use Text::CSV_XS;
33 use C4::Acquisition;
34 use C4::Budgets;
35 use C4::Context;
36 use C4::Output;
37 use C4::Koha;
38 use C4::Auth;
39 use C4::Input;
40 use C4::Debug;
42 my $input = new CGI;
43 #### $input
45 my $dbh = C4::Context->dbh;
47 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
48 { template_name => "admin/aqplan.tt",
49 query => $input,
50 type => "intranet",
51 authnotrequired => 0,
52 flagsrequired => { acquisition => 'planning_manage' },
53 debug => 0,
57 my $budget_period_id = $input->param('budget_period_id');
58 # ' ------- get periods stuff ------------------'
59 # IF PERIOD_ID IS DEFINED, GET THE PERIOD - ELSE GET THE ACTIVE PERIOD BY DEFAULT
60 my $period = GetBudgetPeriod($budget_period_id);
61 my $count = GetPeriodsCount();
62 my $cur = GetCurrency;
63 $template->param( symbol => $cur->{symbol},
64 currency => $cur->{currency}
66 $template->param( period_button_only => 1 ) if $count == 0;
70 # authcats_loop populates the YUI planning button
71 my $auth_cats_loop = GetBudgetAuthCats($budget_period_id);
72 my $budget_period_id = $period->{'budget_period_id'};
73 my $budget_period_startdate = $period->{'budget_period_startdate'};
74 my $budget_period_enddate = $period->{'budget_period_enddate'};
75 my $budget_period_locked = $period->{'budget_period_locked'};
76 my $budget_period_description = $period->{'budget_period_description'};
79 $template->param(
80 budget_period_id => $budget_period_id,
81 budget_period_locked => $budget_period_locked,
82 budget_period_description => $budget_period_description,
83 auth_cats_loop => $auth_cats_loop,
86 # ------- get periods stuff ------------------
88 my $borrower_id = $template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'};
89 my $borrower_branchcode = $template->{VARS}->{'USER_INFO'}[0]->{'branchcode'};
91 my $periods;
92 my $authcat = $input->param('authcat');
93 my $show_active = $input->param('show_active');
94 my $show_actual = $input->param('show_actual');
95 my $show_percent = $input->param('show_percent');
96 my $output = $input->param("output");
97 my $basename = $input->param("basename");
98 my $del = $input->param("sep");
100 my $show_mine = $input->param('show_mine') ;
102 my @hide_cols = $input->param('hide_cols');
104 if ( $budget_period_locked == 1 && not defined $show_actual ) {
105 $show_actual = 1;
108 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
110 my $budget_id = $input->param('budget_id');
111 my $op = $input->param("op");
113 my $budget_branchcode;
115 my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{VARS}->{'USER_INFO'}[0]->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}[0]->{'borrowernumber'}:'' );
117 # build categories list
118 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
119 $sth->execute;
121 # the list
122 my @category_list;
124 # a hash, to check that some hardcoded categories exist.
125 my %categories;
126 while ( my ($category) = $sth->fetchrow_array ) {
127 push( @category_list, $category );
128 $categories{$category} = 1;
131 # push koha system categories
132 push( @category_list, 'MONTHS' );
133 push( @category_list, 'ITEMTYPES' );
134 push( @category_list, 'BRANCHES' );
135 push( @category_list, $$_{'authcat'} ) foreach @$auth_cats_loop;
137 #reorder the list
138 @category_list = sort { $a cmp $b } @category_list;
140 $template->param( authcat_dropbox => {
141 values => \@category_list,
142 default => $authcat,
145 my @budgets = @$budgets_ref;
146 my $CGISort;
147 my @authvals;
148 my %labels;
150 my @names = $input->param();
151 # ------------------------------------------------------------
152 if ( $op eq 'save' ) {
153 #get budgets
154 my ( @buds, @auth_values );
155 foreach my $n (@names) {
156 next if $n =~ m/^[^0-9]/;
157 my @moo = split( ',', $n );
158 push @buds, $moo[0];
159 push @auth_values, $moo[1];
162 #uniq buds and auth
163 my %seen;
164 @buds = grep { !$seen{$_}++ } @buds;
165 @auth_values = grep { !$seen{$_}++ } @auth_values;
166 my @budget_ids;
167 my @budget_lines;
169 foreach my $budget (@buds) {
170 my %budget_line;
171 my @cells_line;
172 my %cell_hash;
174 foreach my $authvalue (@auth_values) {
175 # get actual stats
176 my $cell_name = "$budget,$authvalue";
177 my $estimated_amount = $input->param("$cell_name");
178 my %cell_hash = (
179 estimated_amount => $estimated_amount,
180 authvalue => $authvalue,
181 authcat => $authcat,
182 budget_id => $budget,
183 budget_period_id => $budget_period_id,
185 push( @cells_line, \%cell_hash );
188 %budget_line = (
189 lines => \@cells_line,
191 push( @budget_lines, \%budget_line );
193 my $plan = \@budget_lines;
194 ModBudgetPlan( $plan, $budget_period_id, $authcat );
196 HideCols($authcat, @hide_cols);
200 # ------------------------------------------------------------
201 if ( $authcat =~ m/^Asort/ ) {
202 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
203 my $sth = $dbh->prepare($query);
204 $sth->execute($authcat );
205 if ( $sth->rows > 0 ) {
206 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
207 my $results = $sth->fetchrow_hashref;
208 push @authvals, $results->{authorised_value};
209 $labels{ $results->{authorised_value} } = $results->{lib};
212 $sth->finish;
213 @authvals = sort { $a <=> $b } @authvals;
215 elsif ( $authcat eq 'MONTHS' ) {
217 # build months
218 my @start_date = UnixDate( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
219 my @end_date = UnixDate( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
221 my ( $Dy, $Dm, $Dd ) = Delta_YMD( @start_date, @end_date );
223 #calc number of months between
224 my $months = ( $Dy * 12 ) + $Dm;
225 my $start_month = @start_date[1];
226 my $end_month = ( $Dy * 12 ) + $Dm;
228 for my $mth ( 0 ... $months ) {
229 $mth = DateCalc( $budget_period_startdate, "+ $mth months" );
230 $mth = UnixDate( $mth, "%Y-%m" );
231 push( @authvals, $mth );
233 foreach my $vv (@authvals) {
234 $labels{$vv} = $vv;
238 elsif ( $authcat eq 'ITEMTYPES' ) {
239 my $query = qq| SELECT itemtype, description FROM itemtypes |;
240 my $sth = $dbh->prepare($query);
241 $sth->execute( );
243 if ( $sth->rows > 0 ) {
244 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
245 my $results = $sth->fetchrow_hashref;
246 push @authvals, $results->{itemtype};
247 $labels{ $results->{itemtype} } = $results->{description};
250 $sth->finish;
252 } elsif ( $authcat eq 'BRANCHES' ) {
254 my $query = qq| SELECT branchcode, branchname FROM branches |;
255 my $sth = $dbh->prepare($query);
256 $sth->execute();
258 if ( $sth->rows > 0 ) {
259 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
260 my $results = $sth->fetchrow_hashref;
261 push @authvals, $results->{branchcode};
262 $labels{ $results->{branchcode} } = $results->{branchname};
265 $sth->finish;
266 } elsif ($authcat) {
267 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
268 my $sth = $dbh->prepare($query);
269 $sth->execute($authcat);
270 if ( $sth->rows > 0 ) {
271 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
272 my $results = $sth->fetchrow_hashref;
273 push @authvals, $results->{authorised_value};
274 $labels{ $results->{authorised_value} } = $results->{lib};
277 $sth->finish;
280 my @authvals_row;
281 my $i=1;
282 foreach my $val (@authvals) {
283 my %auth_hash;
284 $auth_hash{val} = $labels{$val};
285 $auth_hash{code} = $val;
286 $auth_hash{colnum} = $i++;
288 # display lookup
289 $auth_hash{display} = GetCols( $authcat, $auth_hash{code});
291 push( @authvals_row, \%auth_hash );
294 #get budgets
295 my ( @buds, @auth_values );
296 foreach my $n (@names) {
297 next if $n =~ m/^[^0-9]/;
298 $n =~ m/(\d*),(.*)/;
299 push @buds, $1;
300 push @auth_values, $2;
304 # ------------------------------------------------------------
305 # DEFAULT DISPLAY BEGINS
307 my $CGIextChoice = ( 'CSV' ); # FIXME translation
308 my $CGIsepChoice = ( C4::Context->preference("delimiter") );
310 my ( @budget_lines, %cell_hash );
313 foreach my $budget (@budgets) {
314 my $budget_lock;
316 unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
317 $budget_lock = 1
320 # check budget permission
321 if ( $period->{budget_period_locked} == 1 ) {
322 $budget_lock = 1;
323 } elsif ( $budget->{budget_permission} == 1 ) {
324 $budget_lock = 1 if $borrower_id != $budget->{'budget_owner_id'};
325 } elsif ( $budget->{budget_permission} == 2 ) {
326 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode};
329 # allow hard-coded itemtype and branch planning
330 unless ( $authcat eq 'ITEMTYPES'
331 or $authcat eq 'BRANCHES'
332 or $authcat eq 'MONTHS' ) {
334 # but skip budgets that dont match the current auth-category
335 next if ( $budget->{'sort1_authcat'} ne $authcat
336 && $budget->{'sort2_authcat'} ne $authcat );
339 my %budget_line; # each row of the table
340 my @cells_line;
341 my $actual_spent;
342 my $estimated_spent;
344 my $i = 0;
345 foreach my $authvalue (@authvals) {
347 # get actual stats
348 my %cell = (
349 budget_id => $budget->{'budget_id'},
350 budget_period_id => $budget->{'budget_period_id'},
351 cell_name => $budget->{'budget_id'} . ',' . $authvalue,
352 authvalue => $authvalue,
353 authcat => $authcat,
354 cell_authvalue => $authvalue,
355 budget_lock => $budget_lock,
358 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell( \%cell, $period, $budget );
359 $cell{actual_amount} = sprintf( "%.2f", $actual );
360 $cell{estimated_amount} = sprintf( "%.2f", $estimated );
361 $cell{display} = $authvals_row[$i]{display};
362 $cell{colnum} = $i;
364 $actual_spent += $cell{actual_amount};
365 $estimated_spent += $cell{estimated_amount};
368 push( @cells_line, \%cell );
369 $i++;
372 my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
373 my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
375 %budget_line = (
376 lines => \@cells_line,
377 budget_name_indent => $budget->{budget_name_indent},
378 budget_amount => $budget->{budget_amount},
379 budget_alloc => $budget->{budget_alloc},
380 budget_act_remain => sprintf( "%.2f", $budget_act_remain ),
381 budget_est_remain => sprintf( "%.2f", $budget_est_remain ),
382 budget_id => $budget->{budget_id},
383 budget_lock => $budget_lock,
388 $budget_line{est_negative} = '1' if $budget_est_remain < 0;
389 $budget_line{est_positive} = '1' if $budget_est_remain > 0;
390 $budget_line{act_negative} = '1' if $budget_act_remain < 0;
391 $budget_line{act_positive} = '1' if $budget_act_remain > 0;
393 # skip if active set , and spent == 0
394 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
396 push( @budget_lines, \%budget_line );
399 if ( $output eq "file" ) {
400 _print_to_csv(\@authvals_row, \@budget_lines);
401 exit(1);
404 $template->param(
405 authvals_row => \@authvals_row,
406 budget_lines => \@budget_lines,
407 budget_period_description => $period->{'budget_period_description'},
408 budget_period_locked => $period->{'budget_period_locked'},
409 budget_period_id => $budget_period_id,
410 authcat => $authcat,
411 show_active => $show_active,
412 show_actual => $show_actual,
413 show_percent => $show_percent,
414 show_mine => $show_mine,
415 CGIextChoice => $CGIextChoice,
416 CGIsepChoice => $CGIsepChoice,
418 authvals => \@authvals_row,
419 hide_cols_loop => \@hide_cols
422 output_html_with_http_headers $input, $cookie, $template->output;
424 sub _print_to_csv {
425 my ( $header, $results ) = @_;
427 binmode STDOUT, ':encoding(UTF-8)';
429 my $csv = Text::CSV_XS->new(
430 { sep_char => $del,
431 always_quote => 'TRUE',
434 print $input->header(
435 -type => 'application/vnd.sun.xml.calc',
436 -encoding => 'utf-8',
437 -attachment => "$basename.csv",
438 -name => "$basename.csv"
440 my @col = ( 'Budget name', 'Budget total' );
441 foreach my $row (@$header) {
442 push @col, $row->{'val'};
444 push @col, 'Budget remaining';
446 $csv->combine(@col);
447 my $str = $csv->string;
448 print "$str\n";
450 foreach my $row (@$results) {
451 $row->{'budget_name_indent'} =~ s/&nbsp;/ /g;
452 my @col = ( $row->{'budget_name_indent'}, $row->{'budget_amount'} );
453 my $l = $row->{'lines'};
454 foreach my $line (@$l) {
455 push @col, $line->{'estimated_amount'};
457 push @col, $row->{'budget_est_remain'};
458 $csv->combine(@col);
459 my $str = $csv->string;
460 print "$str\n";