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
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)
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/;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use Number::Format qw(format_price);
45 my $dbh = C4::Context->dbh;
47 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
48 { template_name => "admin/aqplan.tt",
52 flagsrequired => { acquisition => 'planning_manage' },
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'};
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'};
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 ) {
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%' ");
124 # a hash, to check that some hardcoded categories exist.
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;
138 @category_list = sort { $a cmp $b } @category_list;
140 $template->param( authcat_dropbox => {
141 values => \@category_list,
145 my @budgets = @$budgets_ref;
150 my @names = $input->param();
151 # ------------------------------------------------------------
152 if ( $op eq 'save' ) {
154 my ( @buds, @auth_values );
155 foreach my $n (@names) {
156 next if $n =~ m/^[^0-9]/;
157 my @moo = split( ',', $n );
159 push @auth_values, $moo[1];
164 @buds = grep { !$seen{$_}++ } @buds;
165 @auth_values = grep { !$seen{$_}++ } @auth_values;
169 foreach my $budget (@buds) {
174 foreach my $authvalue (@auth_values) {
176 my $cell_name = "$budget,$authvalue";
177 my $estimated_amount = $input->param("$cell_name");
179 estimated_amount => $estimated_amount,
180 authvalue => $authvalue,
182 budget_id => $budget,
183 budget_period_id => $budget_period_id,
185 push( @cells_line, \%cell_hash );
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};
213 @authvals = sort { $a <=> $b } @authvals;
215 elsif ( $authcat eq '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) {
238 elsif ( $authcat eq 'ITEMTYPES' ) {
239 my $query = qq| SELECT itemtype, description FROM itemtypes |;
240 my $sth = $dbh->prepare($query);
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};
252 } elsif ( $authcat eq 'BRANCHES' ) {
254 my $query = qq| SELECT branchcode, branchname FROM branches |;
255 my $sth = $dbh->prepare($query);
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};
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};
282 foreach my $val (@authvals) {
284 $auth_hash{val} = $labels{$val};
285 $auth_hash{code} = $val;
286 $auth_hash{colnum} = $i++;
289 $auth_hash{display} = GetCols( $authcat, $auth_hash{code});
291 push( @authvals_row, \%auth_hash );
295 my ( @buds, @auth_values );
296 foreach my $n (@names) {
297 next if $n =~ m/^[^0-9]/;
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) {
316 unless (CanUserUseBudget($borrowernumber, $budget, $staff_flags)) {
320 # check budget permission
321 if ( $period->{budget_period_locked} == 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
345 foreach my $authvalue (@authvals) {
349 budget_id => $budget->{'budget_id'},
350 budget_period_id => $budget->{'budget_period_id'},
351 cell_name => $budget->{'budget_id'} . ',' . $authvalue,
352 authvalue => $authvalue,
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};
364 $actual_spent += $cell{actual_amount};
365 $estimated_spent += $cell{estimated_amount};
368 push( @cells_line, \%cell );
372 my $budget_act_remain = $budget->{budget_amount} - $actual_spent;
373 my $budget_est_remain = $budget->{budget_amount} - $estimated_spent;
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);
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,
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;
425 my ( $header, $results ) = @_;
427 binmode STDOUT, ':encoding(UTF-8)';
429 my $csv = Text::CSV_XS->new(
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';
447 my $str = $csv->string;
450 foreach my $row (@$results) {
451 $row->{'budget_name_indent'} =~ s/ / /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'};
459 my $str = $csv->string;