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 $template->param( symbol
=> $active_currency->symbol,
62 currency
=> $active_currency->currency,
64 $template->param( period_button_only
=> 1 ) if $count == 0;
68 # authcats_loop populates the YUI planning button
69 my $auth_cats_loop = GetBudgetAuthCats
($budget_period_id);
70 my $budget_period_id = $period->{'budget_period_id'};
71 my $budget_period_startdate = $period->{'budget_period_startdate'};
72 my $budget_period_enddate = $period->{'budget_period_enddate'};
73 my $budget_period_locked = $period->{'budget_period_locked'};
74 my $budget_period_description = $period->{'budget_period_description'};
78 budget_period_id
=> $budget_period_id,
79 budget_period_locked
=> $budget_period_locked,
80 budget_period_description
=> $budget_period_description,
81 auth_cats_loop
=> $auth_cats_loop,
84 # ------- get periods stuff ------------------
86 my $borrower_id = $template->{VARS
}->{'USER_INFO'}->{'borrowernumber'};
87 my $borrower_branchcode = $template->{VARS
}->{'USER_INFO'}->{'branchcode'};
89 my $authcat = $input->param('authcat');
90 my $show_active = $input->param('show_active');
91 my $show_actual = $input->param('show_actual');
92 my $show_percent = $input->param('show_percent');
93 my $output = $input->param("output");
94 my $basename = $input->param("basename");
95 my $del = $input->param("sep");
97 my $show_mine = $input->param('show_mine') ;
99 my @hide_cols = $input->multi_param('hide_cols');
101 if ( $budget_period_locked == 1 && not defined $show_actual ) {
105 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
107 my $budget_id = $input->param('budget_id');
108 my $op = $input->param("op");
110 my $budgets_ref = GetBudgetHierarchy
( $budget_period_id, $show_mine?
$template->{VARS
}->{'USER_INFO'}->{'branchcode'}:'', $show_mine?
$template->{VARS
}->{'USER_INFO'}->{'borrowernumber'}:'' );
112 # build categories list
113 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
119 # a hash, to check that some hardcoded categories exist.
121 while ( my ($category) = $sth->fetchrow_array ) {
122 push( @category_list, $category );
123 $categories{$category} = 1;
126 # push koha system categories
127 push( @category_list, 'MONTHS' );
128 push( @category_list, 'ITEMTYPES' );
129 push( @category_list, 'BRANCHES' );
130 push( @category_list, $_ ) foreach @
$auth_cats_loop;
133 @category_list = sort { $a cmp $b } @category_list;
135 $template->param( authcat_dropbox
=> {
136 values => \
@category_list,
140 my @budgets = @
$budgets_ref;
144 my @names = $input->multi_param();
145 # ------------------------------------------------------------
146 if ( $op eq 'save' ) {
148 my ( @buds, @auth_values );
149 foreach my $n (@names) {
150 next if $n =~ m/^[^0-9]/;
151 my @moo = split( ',', $n );
153 push @auth_values, $moo[1];
158 @buds = grep { !$seen{$_}++ } @buds;
159 @auth_values = grep { !$seen{$_}++ } @auth_values;
162 foreach my $budget (@buds) {
167 foreach my $authvalue (@auth_values) {
169 my $cell_name = "$budget,$authvalue";
170 my $estimated_amount = $input->param("$cell_name");
172 estimated_amount
=> $estimated_amount,
173 authvalue
=> $authvalue,
175 budget_id
=> $budget,
176 budget_period_id
=> $budget_period_id,
178 push( @cells_line, \
%cell_hash );
182 lines
=> \
@cells_line,
184 push( @budget_lines, \
%budget_line );
186 my $plan = \
@budget_lines;
187 ModBudgetPlan
( $plan, $budget_period_id, $authcat );
189 HideCols
($authcat, @hide_cols);
193 # ------------------------------------------------------------
194 if ( $authcat =~ m/^Asort/ ) {
195 my $query = qq{ SELECT
* FROM authorised_values WHERE category
=? order by lib
};
196 my $sth = $dbh->prepare($query);
197 $sth->execute($authcat );
198 if ( $sth->rows > 0 ) {
199 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
200 my $results = $sth->fetchrow_hashref;
201 push @authvals, $results->{authorised_value
};
202 $labels{ $results->{authorised_value
} } = $results->{lib
};
206 @authvals = sort { $a <=> $b } @authvals;
208 elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
211 my @start_date = UnixDate
( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
212 my @end_date = UnixDate
( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
214 my ( $Dy, $Dm, $Dd ) = Delta_YMD
( @start_date, @end_date );
216 #calc number of months between
217 my $months = ( $Dy * 12 ) + $Dm;
218 my $start_month = @start_date[1];
219 my $end_month = ( $Dy * 12 ) + $Dm;
221 for my $mth ( 0 ... $months ) {
222 $mth = DateCalc
( $budget_period_startdate, "+ $mth months" );
223 $mth = UnixDate
( $mth, "%Y-%m" );
224 push( @authvals, $mth );
226 foreach my $vv (@authvals) {
231 elsif ( $authcat eq 'ITEMTYPES' ) {
232 my $query = qq| SELECT itemtype
, description FROM itemtypes
|;
233 my $sth = $dbh->prepare($query);
236 if ( $sth->rows > 0 ) {
237 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
238 my $results = $sth->fetchrow_hashref;
239 push @authvals, $results->{itemtype
};
240 $labels{ $results->{itemtype
} } = $results->{description
};
245 } elsif ( $authcat eq 'BRANCHES' ) {
247 my $query = qq| SELECT branchcode
, branchname FROM branches
|;
248 my $sth = $dbh->prepare($query);
251 if ( $sth->rows > 0 ) {
252 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
253 my $results = $sth->fetchrow_hashref;
254 push @authvals, $results->{branchcode
};
255 $labels{ $results->{branchcode
} } = $results->{branchname
};
260 my $query = qq{ SELECT
* FROM authorised_values WHERE category
=? order by lib
};
261 my $sth = $dbh->prepare($query);
262 $sth->execute($authcat);
263 if ( $sth->rows > 0 ) {
264 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
265 my $results = $sth->fetchrow_hashref;
266 push @authvals, $results->{authorised_value
};
267 $labels{ $results->{authorised_value
} } = $results->{lib
};
275 foreach my $val (@authvals) {
277 $auth_hash{val
} = $labels{$val};
278 $auth_hash{code
} = $val;
279 $auth_hash{colnum
} = $i++;
282 $auth_hash{display
} = GetCols
( $authcat, $auth_hash{code
});
284 push( @authvals_row, \
%auth_hash );
288 my ( @buds, @auth_values );
289 foreach my $n (@names) {
290 next if $n =~ m/^[^0-9]/;
293 push @auth_values, $2;
297 # ------------------------------------------------------------
298 # DEFAULT DISPLAY BEGINS
300 my $CGIextChoice = ( 'CSV' ); # FIXME translation
301 my $CGIsepChoice = ( C4
::Context
->preference("delimiter") );
303 my ( @budget_lines, %cell_hash );
306 foreach my $budget (@budgets) {
309 unless (CanUserUseBudget
($borrowernumber, $budget, $staff_flags)) {
313 # check budget permission
314 if ( $period->{budget_period_locked
} == 1 ) {
316 } elsif ( $budget->{budget_permission
} == 1 ) {
317 $budget_lock = 1 if $borrower_id != $budget->{'budget_owner_id'};
318 } elsif ( $budget->{budget_permission
} == 2 ) {
319 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode
};
322 # allow hard-coded itemtype and branch planning
323 unless ( $authcat eq 'ITEMTYPES'
324 or $authcat eq 'BRANCHES'
325 or $authcat eq 'MONTHS' ) {
327 # but skip budgets that don't match the current auth-category
328 next if ( $budget->{'sort1_authcat'} ne $authcat
329 && $budget->{'sort2_authcat'} ne $authcat );
332 my %budget_line; # each row of the table
338 foreach my $authvalue (@authvals) {
342 budget_id
=> $budget->{'budget_id'},
343 budget_period_id
=> $budget->{'budget_period_id'},
344 cell_name
=> $budget->{'budget_id'} . ',' . $authvalue,
345 authvalue
=> $authvalue,
347 cell_authvalue
=> $authvalue,
348 budget_lock
=> $budget_lock,
351 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell
( \
%cell, $period, $budget );
352 $cell{actual_amount
} = sprintf( "%.2f", $actual );
353 $cell{estimated_amount
} = sprintf( "%.2f", $estimated );
354 $cell{display
} = $authvals_row[$i]{display
};
357 $actual_spent += $cell{actual_amount
};
358 $estimated_spent += $cell{estimated_amount
};
361 push( @cells_line, \
%cell );
365 my $budget_act_remain = $budget->{budget_amount
} - $actual_spent;
366 my $budget_est_remain = $budget->{budget_amount
} - $estimated_spent;
369 lines
=> \
@cells_line,
370 budget_name
=> $budget->{budget_name
},
371 budget_amount
=> $budget->{budget_amount
},
372 budget_alloc
=> $budget->{budget_alloc
},
373 budget_act_remain
=> sprintf( "%.2f", $budget_act_remain ),
374 budget_est_remain
=> sprintf( "%.2f", $budget_est_remain ),
375 budget_id
=> $budget->{budget_id
},
376 budget_lock
=> $budget_lock,
381 $budget_line{est_negative
} = '1' if $budget_est_remain < 0;
382 $budget_line{est_positive
} = '1' if $budget_est_remain > 0;
383 $budget_line{act_negative
} = '1' if $budget_act_remain < 0;
384 $budget_line{act_positive
} = '1' if $budget_act_remain > 0;
386 # skip if active set , and spent == 0
387 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
389 push( @budget_lines, \
%budget_line );
392 if ( $output eq "file" ) {
393 _print_to_csv
(\
@authvals_row, \
@budget_lines);
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,
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,
415 output_html_with_http_headers
$input, $cookie, $template->output;
418 my ( $header, $results ) = @_;
420 binmode STDOUT
, ':encoding(UTF-8)';
422 my $csv = Text
::CSV_XS
->new(
424 always_quote
=> 'TRUE',
427 print $input->header(
428 -type
=> 'application/vnd.sun.xml.calc',
429 -encoding
=> 'utf-8',
430 -attachment
=> "$basename.csv",
431 -name
=> "$basename.csv"
433 my @col = ( 'Budget name', 'Budget total' );
434 foreach my $row (@
$header) {
435 push @col, $row->{'val'};
437 push @col, 'Budget remaining';
440 my $str = $csv->string;
443 foreach my $row (@
$results) {
444 my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
445 my $l = $row->{'lines'};
446 foreach my $line (@
$l) {
447 push @col, $line->{'estimated_amount'};
449 push @col, $row->{'budget_est_remain'};
451 my $str = $csv->string;