Bug 12768: (QA follow-up) Don't use manualinvoice for non-manual invoices in chargelo...
[koha.git] / admin / aqplan.pl
blobf96086e013d4b755b61b435faea83ebb02922893
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;
38 use Koha::Acquisition::Currencies;
40 my $input = new CGI;
41 #### $input
43 my $dbh = C4::Context->dbh;
45 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
46 { template_name => "admin/aqplan.tt",
47 query => $input,
48 type => "intranet",
49 authnotrequired => 0,
50 flagsrequired => { acquisition => 'planning_manage' },
51 debug => 0,
55 my $budget_period_id = $input->param('budget_period_id');
56 # ' ------- get periods stuff ------------------'
57 # IF PERIOD_ID IS DEFINED, GET THE PERIOD - ELSE GET THE ACTIVE PERIOD BY DEFAULT
58 my $period = GetBudgetPeriod($budget_period_id);
59 my $count = GetPeriodsCount();
60 my $active_currency = Koha::Acquisition::Currencies->get_active;
61 if ( $active_currency ) {
62 $template->param( symbol => $active_currency->symbol,
63 currency => $active_currency->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'}->{'borrowernumber'};
89 my $borrower_branchcode = $template->{VARS}->{'USER_INFO'}->{'branchcode'};
91 my $authcat = $input->param('authcat');
92 my $show_active = $input->param('show_active');
93 my $show_actual = $input->param('show_actual');
94 my $show_percent = $input->param('show_percent');
95 my $output = $input->param("output");
96 my $basename = $input->param("basename");
97 my $del = $input->param("sep");
99 my $show_mine = $input->param('show_mine') ;
101 my @hide_cols = $input->multi_param('hide_cols');
103 if ( $budget_period_locked == 1 && not defined $show_actual ) {
104 $show_actual = 1;
107 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
109 my $budget_id = $input->param('budget_id');
110 my $op = $input->param("op");
112 my $budgets_ref = GetBudgetHierarchy( $budget_period_id, $show_mine?$template->{VARS}->{'USER_INFO'}->{'branchcode'}:'', $show_mine?$template->{VARS}->{'USER_INFO'}->{'borrowernumber'}:'' );
114 # build categories list
115 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
116 $sth->execute;
118 # the list
119 my @category_list;
121 # a hash, to check that some hardcoded categories exist.
122 my %categories;
123 while ( my ($category) = $sth->fetchrow_array ) {
124 push( @category_list, $category );
125 $categories{$category} = 1;
128 # push koha system categories
129 push( @category_list, 'MONTHS' );
130 push( @category_list, 'ITEMTYPES' );
131 push( @category_list, 'BRANCHES' );
132 push( @category_list, $_ ) foreach @$auth_cats_loop;
134 #reorder the list
135 @category_list = sort { $a cmp $b } @category_list;
137 $template->param( authcat_dropbox => {
138 values => \@category_list,
139 default => $authcat,
142 my @budgets = @$budgets_ref;
143 my @authvals;
144 my %labels;
146 my @names = $input->multi_param();
147 # ------------------------------------------------------------
148 if ( $op eq 'save' ) {
149 #get budgets
150 my ( @buds, @auth_values );
151 foreach my $n (@names) {
152 next if $n =~ m/^[^0-9]/;
153 my @moo = split( ',', $n );
154 push @buds, $moo[0];
155 push @auth_values, $moo[1];
158 #uniq buds and auth
159 my %seen;
160 @buds = grep { !$seen{$_}++ } @buds;
161 @auth_values = grep { !$seen{$_}++ } @auth_values;
162 my @budget_lines;
164 foreach my $budget (@buds) {
165 my %budget_line;
166 my @cells_line;
167 my %cell_hash;
169 foreach my $authvalue (@auth_values) {
170 # get actual stats
171 my $cell_name = "$budget,$authvalue";
172 my $estimated_amount = $input->param("$cell_name");
173 my %cell_hash = (
174 estimated_amount => $estimated_amount,
175 authvalue => $authvalue,
176 authcat => $authcat,
177 budget_id => $budget,
178 budget_period_id => $budget_period_id,
180 push( @cells_line, \%cell_hash );
183 %budget_line = (
184 lines => \@cells_line,
186 push( @budget_lines, \%budget_line );
188 my $plan = \@budget_lines;
189 ModBudgetPlan( $plan, $budget_period_id, $authcat );
191 HideCols($authcat, @hide_cols);
195 # ------------------------------------------------------------
196 if ( $authcat =~ m/^Asort/ ) {
197 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
198 my $sth = $dbh->prepare($query);
199 $sth->execute($authcat );
200 if ( $sth->rows > 0 ) {
201 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
202 my $results = $sth->fetchrow_hashref;
203 push @authvals, $results->{authorised_value};
204 $labels{ $results->{authorised_value} } = $results->{lib};
207 $sth->finish;
208 @authvals = sort { $a <=> $b } @authvals;
210 elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
212 # build months
213 my @start_date = UnixDate( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
214 my @end_date = UnixDate( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
216 my ( $Dy, $Dm, $Dd ) = Delta_YMD( @start_date, @end_date );
218 #calc number of months between
219 my $months = ( $Dy * 12 ) + $Dm;
220 my $start_month = @start_date[1];
221 my $end_month = ( $Dy * 12 ) + $Dm;
223 for my $mth ( 0 ... $months ) {
224 $mth = DateCalc( $budget_period_startdate, "+ $mth months" );
225 $mth = UnixDate( $mth, "%Y-%m" );
226 push( @authvals, $mth );
228 foreach my $vv (@authvals) {
229 $labels{$vv} = $vv;
233 elsif ( $authcat eq 'ITEMTYPES' ) {
234 my $query = qq| SELECT itemtype, description FROM itemtypes |;
235 my $sth = $dbh->prepare($query);
236 $sth->execute( );
238 if ( $sth->rows > 0 ) {
239 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
240 my $results = $sth->fetchrow_hashref;
241 push @authvals, $results->{itemtype};
242 $labels{ $results->{itemtype} } = $results->{description};
245 $sth->finish;
247 } elsif ( $authcat eq 'BRANCHES' ) {
249 my $query = qq| SELECT branchcode, branchname FROM branches |;
250 my $sth = $dbh->prepare($query);
251 $sth->execute();
253 if ( $sth->rows > 0 ) {
254 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
255 my $results = $sth->fetchrow_hashref;
256 push @authvals, $results->{branchcode};
257 $labels{ $results->{branchcode} } = $results->{branchname};
260 $sth->finish;
261 } elsif ($authcat) {
262 my $query = qq{ SELECT * FROM authorised_values WHERE category=? order by lib };
263 my $sth = $dbh->prepare($query);
264 $sth->execute($authcat);
265 if ( $sth->rows > 0 ) {
266 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
267 my $results = $sth->fetchrow_hashref;
268 push @authvals, $results->{authorised_value};
269 $labels{ $results->{authorised_value} } = $results->{lib};
272 $sth->finish;
275 my @authvals_row;
276 my $i=1;
277 foreach my $val (@authvals) {
278 my %auth_hash;
279 $auth_hash{val} = $labels{$val};
280 $auth_hash{code} = $val;
281 $auth_hash{colnum} = $i++;
283 # display lookup
284 $auth_hash{display} = GetCols( $authcat, $auth_hash{code});
286 push( @authvals_row, \%auth_hash );
289 #get budgets
290 my ( @buds, @auth_values );
291 foreach my $n (@names) {
292 next if $n =~ m/^[^0-9]/;
293 $n =~ m/(\d*),(.*)/;
294 push @buds, $1;
295 push @auth_values, $2;
299 # ------------------------------------------------------------
300 # DEFAULT DISPLAY BEGINS
302 my $CGIextChoice = ( 'CSV' ); # FIXME translation
303 my $CGIsepChoice = ( C4::Context->preference("delimiter") );
305 my ( @budget_lines, %cell_hash );
308 foreach my $budget (@budgets) {
309 my $budget_lock;
311 unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
312 $budget_lock = 1
315 # check budget permission
316 if ( $period->{budget_period_locked} == 1 ) {
317 $budget_lock = 1;
318 } elsif ( $budget->{budget_permission} == 1 ) {
319 $budget_lock = 1 if $borrower_id != $budget->{'budget_owner_id'};
320 } elsif ( $budget->{budget_permission} == 2 ) {
321 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode};
324 # allow hard-coded itemtype and branch planning
325 unless ( $authcat eq 'ITEMTYPES'
326 or $authcat eq 'BRANCHES'
327 or $authcat eq 'MONTHS' ) {
329 # but skip budgets that don't match the current auth-category
330 next if ( $budget->{'sort1_authcat'} ne $authcat
331 && $budget->{'sort2_authcat'} ne $authcat );
334 my %budget_line; # each row of the table
335 my @cells_line;
336 my $actual_spent;
337 my $estimated_spent;
339 my $i = 0;
340 foreach my $authvalue (@authvals) {
342 # get actual stats
343 my %cell = (
344 budget_id => $budget->{'budget_id'},
345 budget_period_id => $budget->{'budget_period_id'},
346 cell_name => $budget->{'budget_id'} . ',' . $authvalue,
347 authvalue => $authvalue,
348 authcat => $authcat,
349 cell_authvalue => $authvalue,
350 budget_lock => $budget_lock,
353 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell( \%cell, $period, $budget );
354 $cell{actual_amount} = sprintf( "%.2f", $actual );
355 $cell{estimated_amount} = sprintf( "%.2f", $estimated );
356 $cell{display} = $authvals_row[$i]{display};
357 $cell{colnum} = $i;
359 $actual_spent += $cell{actual_amount};
360 $estimated_spent += $cell{estimated_amount};
363 push( @cells_line, \%cell );
364 $i++;
367 my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
368 my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
370 %budget_line = (
371 lines => \@cells_line,
372 budget_name => $budget->{budget_name},
373 budget_amount => $budget->{budget_amount},
374 budget_alloc => $budget->{budget_alloc},
375 budget_act_remain => sprintf( "%.2f", $budget_act_remain ),
376 budget_est_remain => sprintf( "%.2f", $budget_est_remain ),
377 budget_id => $budget->{budget_id},
378 budget_lock => $budget_lock,
383 $budget_line{est_negative} = '1' if $budget_est_remain < 0;
384 $budget_line{est_positive} = '1' if $budget_est_remain > 0;
385 $budget_line{act_negative} = '1' if $budget_act_remain < 0;
386 $budget_line{act_positive} = '1' if $budget_act_remain > 0;
388 # skip if active set , and spent == 0
389 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
391 push( @budget_lines, \%budget_line );
394 if ( $output eq "file" ) {
395 _print_to_csv(\@authvals_row, \@budget_lines);
396 exit(1);
399 $template->param(
400 authvals_row => \@authvals_row,
401 budget_lines => \@budget_lines,
402 budget_period_description => $period->{'budget_period_description'},
403 budget_period_locked => $period->{'budget_period_locked'},
404 budget_period_id => $budget_period_id,
405 authcat => $authcat,
406 show_active => $show_active,
407 show_actual => $show_actual,
408 show_percent => $show_percent,
409 show_mine => $show_mine,
410 CGIextChoice => $CGIextChoice,
411 CGIsepChoice => $CGIsepChoice,
413 authvals => \@authvals_row,
414 hide_cols_loop => \@hide_cols,
417 output_html_with_http_headers $input, $cookie, $template->output;
419 sub _print_to_csv {
420 my ( $header, $results ) = @_;
422 binmode STDOUT, ':encoding(UTF-8)';
424 my $csv = Text::CSV_XS->new(
425 { sep_char => $del,
426 always_quote => 'TRUE',
429 print $input->header(
430 -type => 'application/vnd.sun.xml.calc',
431 -encoding => 'utf-8',
432 -attachment => "$basename.csv",
433 -name => "$basename.csv"
435 my @col = ( 'Budget name', 'Budget total' );
436 foreach my $row (@$header) {
437 push @col, $row->{'val'};
439 push @col, 'Budget remaining';
441 $csv->combine(@col);
442 my $str = $csv->string;
443 print "$str\n";
445 foreach my $row (@$results) {
446 my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
447 my $l = $row->{'lines'};
448 foreach my $line (@$l) {
449 push @col, $line->{'estimated_amount'};
451 push @col, $row->{'budget_est_remain'};
452 $csv->combine(@col);
453 my $str = $csv->string;
454 print "$str\n";