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.tmpl",
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
;
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'};
75 my $budget_period_dropbox = GetBudgetPeriodsDropbox
($budget_period_id );
79 budget_period_id
=> $budget_period_id,
80 budget_period_locked
=> $budget_period_locked,
81 budget_period_description
=> $budget_period_description,
82 budget_period_dropbox
=> $budget_period_dropbox,
83 auth_cats_loop
=> $auth_cats_loop,
86 # ------- get periods stuff ------------------
88 my $borrower_id = $template->{param_map
}->{'USER_INFO'}[0]->{'borrowernumber'};
89 my $borrower_branchcode = $template->{param_map
}->{'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 $mime = $input->param("MIME");
99 my $del = $input->param("sep");
101 my $show_mine = $input->param('show_mine') ;
103 my @hide_cols = $input->param('hide_cols');
105 my $cur_format = C4
::Context
->preference("CurrencyFormat");
108 if ( $cur_format eq 'FR' ) {
109 $num = new Number
::Format
(
110 'decimal_fill' => '2',
111 'decimal_point' => ',',
112 'int_curr_symbol' => '',
113 'mon_thousands_sep' => ' ',
114 'thousands_sep' => ' ',
115 'mon_decimal_point' => ','
117 } else { # US by default..
118 $num = new Number
::Format
(
119 'int_curr_symbol' => '',
120 'mon_thousands_sep' => ',',
121 'mon_decimal_point' => '.'
125 if ( $budget_period_locked == 1 && not defined $show_actual ) {
129 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
131 my $budget_id = $input->param('budget_id');
132 my $op = $input->param("op");
134 my $budget_branchcode;
136 my $budgets_ref = GetBudgetHierarchy
( $budget_period_id, $show_mine?
$template->{param_map
}->{'USER_INFO'}[0]->{'branchcode'}:'', $show_mine?
$template->{param_map
}->{'USER_INFO'}[0]->{'borrowernumber'}:'' );
138 # build categories list
139 my $sth = $dbh->prepare("select distinct category from authorised_values where category like 'A%' ");
145 # a hash, to check that some hardcoded categories exist.
147 while ( my ($category) = $sth->fetchrow_array ) {
148 push( @category_list, $category );
149 $categories{$category} = 1;
152 # push koha system categories
153 push( @category_list, 'MONTHS' );
154 push( @category_list, 'ITEMTYPES' );
155 push( @category_list, 'BRANCHES' );
158 @category_list = sort { $a cmp $b } @category_list;
159 my $tab_list = CGI
::scrolling_list
(
162 -values => \
@category_list,
163 -default => $authcat,
169 $template->param( authcat_dropbox
=> $tab_list );
171 my @budgets = @
$budgets_ref;
177 my @names = $input->param();
179 # ------------------------------------------------------------
180 if ( $op eq 'save' ) {
182 my ( @buds, @auth_values );
183 foreach my $n (@names) {
184 next if $n =~ m/^[^0-9]/;
185 my @moo = split( ',', $n );
187 push @auth_values, $moo[1];
192 @buds = grep { !$seen{$_}++ } @buds;
193 @auth_values = grep { !$seen{$_}++ } @auth_values;
197 foreach my $budget (@buds) {
202 foreach my $authvalue (@auth_values) {
204 my $cell_name = "$budget,$authvalue";
205 my $estimated_amount = $input->param("$cell_name");
207 estimated_amount
=> $estimated_amount,
208 authvalue
=> $authvalue,
210 budget_id
=> $budget,
211 budget_period_id
=> $budget_period_id,
213 push( @cells_line, \
%cell_hash );
217 lines
=> \
@cells_line,
219 push( @budget_lines, \
%budget_line );
221 my $plan = \
@budget_lines;
222 ModBudgetPlan
( $plan, $budget_period_id, $authcat );
224 HideCols
($authcat, @hide_cols);
228 # ------------------------------------------------------------
229 if ( $authcat =~ m/^Asort/ ) {
230 # ----------- copied from C4::Input::buildCGIsort()
231 my $query = qq{ SELECT
* FROM authorised_values WHERE category
=? order by lib
};
232 my $sth = $dbh->prepare($query);
233 $sth->execute($authcat );
234 if ( $sth->rows > 0 ) {
235 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
236 my $results = $sth->fetchrow_hashref;
237 push @authvals, $results->{authorised_value
};
238 $labels{ $results->{authorised_value
} } = $results->{lib
};
242 @authvals = sort { $a <=> $b } @authvals;
244 elsif ( $authcat eq 'MONTHS' ) {
247 my @start_date = UnixDate
( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
248 my @end_date = UnixDate
( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
250 my ( $Dy, $Dm, $Dd ) = Delta_YMD
( @start_date, @end_date );
252 #calc number of months between
253 my $months = ( $Dy * 12 ) + $Dm;
254 my $start_month = @start_date[1];
255 my $end_month = ( $Dy * 12 ) + $Dm;
257 for my $mth ( 0 ... $months ) {
258 $mth = DateCalc
( $budget_period_startdate, "+ $mth months" );
259 $mth = UnixDate
( $mth, "%Y-%m" );
260 push( @authvals, $mth );
262 foreach my $vv (@authvals) {
267 elsif ( $authcat eq 'ITEMTYPES' ) {
268 my $query = qq| SELECT itemtype
, description FROM itemtypes
|;
269 my $sth = $dbh->prepare($query);
272 if ( $sth->rows > 0 ) {
273 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
274 my $results = $sth->fetchrow_hashref;
275 push @authvals, $results->{itemtype
};
276 $labels{ $results->{itemtype
} } = $results->{description
};
281 } elsif ( $authcat eq 'BRANCHES' ) {
283 my $query = qq| SELECT branchcode
, branchname FROM branches
|;
284 my $sth = $dbh->prepare($query);
287 if ( $sth->rows > 0 ) {
288 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
289 my $results = $sth->fetchrow_hashref;
290 push @authvals, $results->{branchcode
};
291 $labels{ $results->{branchcode
} } = $results->{branchname
};
299 foreach my $val (@authvals) {
301 $auth_hash{val
} = $labels{$val};
302 $auth_hash{code
} = $val;
303 $auth_hash{colnum
} = $i++;
306 $auth_hash{display
} = GetCols
( $authcat, $auth_hash{code
});
308 push( @authvals_row, \
%auth_hash );
312 my ( @buds, @auth_values );
313 foreach my $n (@names) {
314 next if $n =~ m/^[^0-9]/;
317 push @auth_values, $2;
321 # ------------------------------------------------------------
322 # DEFAULT DISPLAY BEGINS
324 my @mime = ( C4
::Context
->preference("MIME") );
325 my $CGIextChoice = CGI
::scrolling_list
(
333 my @dels = ( C4
::Context
->preference("delimiter") );
334 my $CGIsepChoice = CGI
::scrolling_list
(
342 my ( @budget_lines, %cell_hash );
345 foreach my $budget (@budgets) {
348 # check budget permission
349 if ( $period->{budget_period_locked
} == 1 ) {
351 } elsif ( $budget->{budget_permission
} == 1 ) {
352 $budget_lock = 1 if $borrower_id != $budget->{'budget_owner_id'};
353 } elsif ( $budget->{budget_permission
} == 2 ) {
354 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode
};
357 # allow hard-coded itemtype and branch planning
358 unless ( $authcat eq 'ITEMTYPES'
359 or $authcat eq 'BRANCHES'
360 or $authcat eq 'MONTHS' ) {
362 # but skip budgets that dont match the current auth-category
363 next if ( $budget->{'sort1_authcat'} ne $authcat
364 && $budget->{'sort2_authcat'} ne $authcat );
367 my %budget_line; # each row of the table
373 foreach my $authvalue (@authvals) {
377 budget_id
=> $budget->{'budget_id'},
378 budget_period_id
=> $budget->{'budget_period_id'},
379 cell_name
=> $budget->{'budget_id'} . ',' . $authvalue,
380 authvalue
=> $authvalue,
382 cell_authvalue
=> $authvalue,
383 budget_lock
=> $budget_lock,
386 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell
( \
%cell, $period, $budget );
387 $cell{actual_amount
} = sprintf( "%.2f", $actual );
388 $cell{estimated_amount
} = sprintf( "%.2f", $estimated );
389 $cell{display
} = $authvals_row[$i]{display
};
392 $actual_spent += $cell{actual_amount
};
393 $estimated_spent += $cell{estimated_amount
};
396 push( @cells_line, \
%cell );
400 my $budget_act_remain = $budget->{budget_amount
} - $actual_spent;
401 my $budget_est_remain = $budget->{budget_amount
} - $estimated_spent;
404 lines
=> \
@cells_line,
405 budget_name_indent
=> $budget->{budget_name_indent
},
406 budget_amount_formatted
=> $num->format_price( $budget->{budget_amount
} ),
407 budget_amount
=> $budget->{budget_amount
},
408 budget_alloc
=> $budget->{budget_alloc
},
409 budget_act_remain
=> sprintf( "%.2f", $budget_act_remain ),
410 budget_est_remain
=> sprintf( "%.2f", $budget_est_remain ),
411 budget_id
=> $budget->{budget_id
},
412 budget_lock
=> $budget_lock,
417 $budget_line{est_negative
} = '1' if $budget_est_remain < 0;
418 $budget_line{est_positive
} = '1' if $budget_est_remain > 0;
419 $budget_line{act_negative
} = '1' if $budget_act_remain < 0;
420 $budget_line{act_positive
} = '1' if $budget_act_remain > 0;
422 # skip if active set , and spent == 0
423 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
425 push( @budget_lines, \
%budget_line );
428 if ( $output eq "file" ) {
429 _print_to_csv
(\
@authvals_row, \
@budget_lines);
434 authvals_row
=> \
@authvals_row,
435 budget_lines
=> \
@budget_lines,
436 budget_period_description
=> $period->{'budget_period_description'},
437 budget_period_locked
=> $period->{'budget_period_locked'},
438 budget_period_id
=> $budget_period_id,
440 show_active
=> $show_active,
441 show_actual
=> $show_actual,
442 show_percent
=> $show_percent,
443 show_mine
=> $show_mine,
444 cur
=> $cur->{symbol
},
445 cur_format
=> $cur_format,
446 CGIextChoice
=> $CGIextChoice,
447 CGIsepChoice
=> $CGIsepChoice,
449 authvals
=> \
@authvals_row,
450 hide_cols_loop
=> \
@hide_cols
453 output_html_with_http_headers
$input, $cookie, $template->output;
456 my ( $header, $results ) = @_;
458 my $csv = Text
::CSV_XS
->new(
460 always_quote
=> 'TRUE',
463 print $input->header(
464 -type
=> 'application/vnd.sun.xml.calc',
465 -encoding
=> 'utf-8',
466 -attachment
=> "$basename.csv",
467 -name
=> "$basename.csv"
469 my @col = ( 'Budget name', 'Budget total' );
470 foreach my $row (@
$header) {
471 push @col, $row->{'val'};
473 push @col, 'Budget remaining';
476 my $str = $csv->string;
479 foreach my $row (@
$results) {
480 my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
481 my $l = $row->{'lines'};
482 foreach my $line (@
$l) {
483 push @col, $line->{'estimated_amount'};
485 push @col, $row->{'budget_est_remain'};
487 my $str = $csv->string;