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)
24 #use warnings; FIXME - Bug 2505
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/;
38 use Koha::Acquisition::Currencies;
43 my $dbh = C4::Context->dbh;
45 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
46 { template_name => "admin/aqplan.tt",
50 flagsrequired => { acquisition => 'planning_manage' },
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'};
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 ) {
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%' ");
121 # a hash, to check that some hardcoded categories exist.
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;
135 @category_list = sort { $a cmp $b } @category_list;
137 $template->param( authcat_dropbox => {
138 values => \@category_list,
142 my @budgets = @$budgets_ref;
146 my @names = $input->multi_param();
147 # ------------------------------------------------------------
148 if ( $op eq 'save' ) {
150 my ( @buds, @auth_values );
151 foreach my $n (@names) {
152 next if $n =~ m/^[^0-9]/;
153 my @moo = split( ',', $n );
155 push @auth_values, $moo[1];
160 @buds = grep { !$seen{$_}++ } @buds;
161 @auth_values = grep { !$seen{$_}++ } @auth_values;
164 foreach my $budget (@buds) {
169 foreach my $authvalue (@auth_values) {
171 my $cell_name = "$budget,$authvalue";
172 my $estimated_amount = $input->param("$cell_name");
174 estimated_amount => $estimated_amount,
175 authvalue => $authvalue,
177 budget_id => $budget,
178 budget_period_id => $budget_period_id,
180 push( @cells_line, \%cell_hash );
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};
208 @authvals = sort { $a <=> $b } @authvals;
210 elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
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) {
233 elsif ( $authcat eq 'ITEMTYPES' ) {
234 my $query = qq| SELECT itemtype, description FROM itemtypes |;
235 my $sth = $dbh->prepare($query);
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};
247 } elsif ( $authcat eq 'BRANCHES' ) {
249 my $query = qq| SELECT branchcode, branchname FROM branches |;
250 my $sth = $dbh->prepare($query);
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};
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};
277 foreach my $val (@authvals) {
279 $auth_hash{val} = $labels{$val};
280 $auth_hash{code} = $val;
281 $auth_hash{colnum} = $i++;
284 $auth_hash{display} = GetCols( $authcat, $auth_hash{code});
286 push( @authvals_row, \%auth_hash );
290 my ( @buds, @auth_values );
291 foreach my $n (@names) {
292 next if $n =~ m/^[^0-9]/;
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) {
311 unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
315 # check budget permission
316 if ( $period->{budget_period_locked} == 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
340 foreach my $authvalue (@authvals) {
344 budget_id => $budget->{'budget_id'},
345 budget_period_id => $budget->{'budget_period_id'},
346 cell_name => $budget->{'budget_id'} . ',' . $authvalue,
347 authvalue => $authvalue,
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};
359 $actual_spent += $cell{actual_amount};
360 $estimated_spent += $cell{estimated_amount};
363 push( @cells_line, \%cell );
367 my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
368 my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
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);
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,
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;
420 my ( $header, $results ) = @_;
422 binmode STDOUT, ':encoding(UTF-8)';
424 my $csv = Text::CSV_XS->new(
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';
442 my $str = $csv->string;
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'};
453 my $str = $csv->string;