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)
25 use List
::Util qw
/min/;
26 use Date
::Calc qw
/Delta_YMD Easter_Sunday Today Decode_Date_EU/;
27 use Date
::Manip qw
/ ParseDate UnixDate DateCalc/;
37 use Koha
::Acquisition
::Currencies
;
42 my $dbh = C4
::Context
->dbh;
44 my ( $template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user
(
45 { template_name
=> "admin/aqplan.tt",
49 flagsrequired
=> { acquisition
=> 'planning_manage' },
54 my $budget_period_id = $input->param('budget_period_id');
55 # ' ------- get periods stuff ------------------'
56 # IF PERIOD_ID IS DEFINED, GET THE PERIOD - ELSE GET THE ACTIVE PERIOD BY DEFAULT
57 my $period = GetBudgetPeriod
($budget_period_id);
58 my $count = GetPeriodsCount
();
59 my $active_currency = Koha
::Acquisition
::Currencies
->get_active;
60 if ( $active_currency ) {
61 $template->param( symbol
=> $active_currency->symbol,
62 currency
=> $active_currency->currency,
65 $template->param( period_button_only
=> 1 ) if $count == 0;
69 # authcats_loop populates the YUI planning button
70 my $auth_cats_loop = GetBudgetAuthCats
($budget_period_id);
71 $budget_period_id = $period->{'budget_period_id'};
72 my $budget_period_startdate = $period->{'budget_period_startdate'};
73 my $budget_period_enddate = $period->{'budget_period_enddate'};
74 my $budget_period_locked = $period->{'budget_period_locked'};
75 my $budget_period_description = $period->{'budget_period_description'};
79 budget_period_id
=> $budget_period_id,
80 budget_period_locked
=> $budget_period_locked,
81 budget_period_description
=> $budget_period_description,
82 auth_cats_loop
=> $auth_cats_loop,
85 # ------- get periods stuff ------------------
88 my $borrower_branchcode = my $branch_code = C4
::Context
->userenv->{'branch'};
90 my $authcat = $input->param('authcat');
91 my $show_active = $input->param('show_active') // 0;
92 my $show_actual = $input->param('show_actual');
93 my $show_percent = $input->param('show_percent');
94 my $output = $input->param("output") // q{};
95 our $basename = $input->param("basename");
96 our $del = $input->param("sep");
98 my $show_mine = $input->param('show_mine') ;
100 my @hide_cols = $input->multi_param('hide_cols');
102 if ( $budget_period_locked == 1 && not defined $show_actual ) {
106 $authcat = 'Asort1' if not defined $authcat; # defaults to Asort if no authcat given
108 my $budget_id = $input->param('budget_id');
109 my $op = $input->param("op") // q{};
111 my $budgets_ref = GetBudgetHierarchy
(
113 $show_mine ?
$borrower_branchcode : '',
114 $show_mine ?
$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, $_ ) 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;
149 my @names = $input->multi_param();
150 # ------------------------------------------------------------
151 if ( $op eq 'save' ) {
153 my ( @buds, @auth_values );
154 foreach my $n (@names) {
155 next if $n =~ m/^[^0-9]/;
156 my @moo = split( ',', $n );
158 push @auth_values, $moo[1];
163 @buds = grep { !$seen{$_}++ } @buds;
164 @auth_values = grep { !$seen{$_}++ } @auth_values;
167 foreach my $budget (@buds) {
172 foreach my $authvalue (@auth_values) {
174 my $cell_name = "$budget,$authvalue";
175 my $estimated_amount = $input->param("$cell_name");
177 estimated_amount
=> $estimated_amount,
178 authvalue
=> $authvalue,
180 budget_id
=> $budget,
181 budget_period_id
=> $budget_period_id,
183 push( @cells_line, \
%cell_hash );
187 lines
=> \
@cells_line,
189 push( @budget_lines, \
%budget_line );
191 my $plan = \
@budget_lines;
192 ModBudgetPlan
( $plan, $budget_period_id, $authcat );
194 HideCols
($authcat, @hide_cols);
198 # ------------------------------------------------------------
199 if ( $authcat =~ m/^Asort/ ) {
200 my $query = qq{ SELECT
* FROM authorised_values WHERE category
=? order by lib
};
201 my $sth = $dbh->prepare($query);
202 $sth->execute($authcat );
203 if ( $sth->rows > 0 ) {
204 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
205 my $results = $sth->fetchrow_hashref;
206 push @authvals, $results->{authorised_value
};
207 $labels{ $results->{authorised_value
} } = $results->{lib
};
211 @authvals = sort { $a <=> $b } @authvals;
213 elsif ( $authcat eq 'MONTHS' && $budget_period_startdate && $budget_period_enddate ) {
216 my @start_date = UnixDate
( $budget_period_startdate, ( '%Y', '%m', '%d' ) );
217 my @end_date = UnixDate
( $budget_period_enddate, ( '%Y', '%m', '%d' ) );
219 my ( $Dy, $Dm, $Dd ) = Delta_YMD
( @start_date, @end_date );
221 #calc number of months between
222 my $months = ( $Dy * 12 ) + $Dm;
223 my $start_month = $start_date[1];
224 my $end_month = ( $Dy * 12 ) + $Dm;
226 for my $mth ( 0 ... $months ) {
227 $mth = DateCalc
( $budget_period_startdate, "+ $mth months" );
228 $mth = UnixDate
( $mth, "%Y-%m" );
229 push( @authvals, $mth );
231 foreach my $vv (@authvals) {
236 elsif ( $authcat eq 'ITEMTYPES' ) {
237 my $query = qq| SELECT itemtype
, description FROM itemtypes
|;
238 my $sth = $dbh->prepare($query);
241 if ( $sth->rows > 0 ) {
242 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
243 my $results = $sth->fetchrow_hashref;
244 push @authvals, $results->{itemtype
};
245 $labels{ $results->{itemtype
} } = $results->{description
};
250 } elsif ( $authcat eq 'BRANCHES' ) {
252 my $query = qq| SELECT branchcode
, branchname FROM branches
|;
253 my $sth = $dbh->prepare($query);
256 if ( $sth->rows > 0 ) {
257 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
258 my $results = $sth->fetchrow_hashref;
259 push @authvals, $results->{branchcode
};
260 $labels{ $results->{branchcode
} } = $results->{branchname
};
265 my $query = qq{ SELECT
* FROM authorised_values WHERE category
=? order by lib
};
266 my $sth = $dbh->prepare($query);
267 $sth->execute($authcat);
268 if ( $sth->rows > 0 ) {
269 for ( my $i = 0 ; $i < $sth->rows ; $i++ ) {
270 my $results = $sth->fetchrow_hashref;
271 push @authvals, $results->{authorised_value
};
272 $labels{ $results->{authorised_value
} } = $results->{lib
};
280 foreach my $val (@authvals) {
282 $auth_hash{val
} = $labels{$val};
283 $auth_hash{code
} = $val;
284 $auth_hash{colnum
} = $i++;
287 $auth_hash{display
} = GetCols
( $authcat, $auth_hash{code
});
289 push( @authvals_row, \
%auth_hash );
293 my ( @buds, @auth_values );
294 foreach my $n (@names) {
295 next if $n =~ m/^[^0-9]/;
298 push @auth_values, $2;
302 # ------------------------------------------------------------
303 # DEFAULT DISPLAY BEGINS
305 my $CGIextChoice = ( 'CSV' ); # FIXME translation
306 my $CGIsepChoice = ( C4
::Context
->preference("delimiter") );
308 my ( @budget_lines, %cell_hash );
311 foreach my $budget (@budgets) {
314 unless (CanUserUseBudget
($borrowernumber, $budget, $staff_flags)) {
318 # check budget permission
319 if ( $period->{budget_period_locked
} == 1 ) {
321 } elsif ( $budget->{budget_permission
} == 1 ) {
322 $budget_lock = 1 if $borrowernumber != $budget->{'budget_owner_id'};
323 } elsif ( $budget->{budget_permission
} == 2 ) {
324 $budget_lock = 1 if $borrower_branchcode ne $budget->{budget_branchcode
};
327 # allow hard-coded itemtype and branch planning
328 unless ( $authcat eq 'ITEMTYPES'
329 or $authcat eq 'BRANCHES'
330 or $authcat eq 'MONTHS' ) {
332 # but skip budgets that don't match the current auth-category
333 next if ( $budget->{'sort1_authcat'} ne $authcat
334 && $budget->{'sort2_authcat'} ne $authcat );
337 my %budget_line; # each row of the table
343 foreach my $authvalue (@authvals) {
347 budget_id
=> $budget->{'budget_id'},
348 budget_period_id
=> $budget->{'budget_period_id'},
349 cell_name
=> $budget->{'budget_id'} . ',' . $authvalue,
350 authvalue
=> $authvalue,
352 cell_authvalue
=> $authvalue,
353 budget_lock
=> $budget_lock,
356 my ( $actual, $estimated, $display ) = GetBudgetsPlanCell
( \
%cell, $period, $budget );
357 $cell{actual_amount
} = sprintf( "%.2f", $actual // 0 );
358 $cell{estimated_amount
} = sprintf( "%.2f", $estimated // 0 );
359 $cell{display
} = $authvals_row[$i]{display
};
362 $actual_spent += $cell{actual_amount
};
363 $estimated_spent += $cell{estimated_amount
};
366 push( @cells_line, \
%cell );
370 my $budget_act_remain = $budget->{budget_amount
} - $actual_spent;
371 my $budget_est_remain = $budget->{budget_amount
} - $estimated_spent;
374 lines
=> \
@cells_line,
375 budget_name
=> $budget->{budget_name
},
376 budget_amount
=> $budget->{budget_amount
},
377 budget_alloc
=> $budget->{budget_alloc
},
378 budget_act_remain
=> sprintf( "%.2f", $budget_act_remain ),
379 budget_est_remain
=> sprintf( "%.2f", $budget_est_remain ),
380 budget_id
=> $budget->{budget_id
},
381 budget_lock
=> $budget_lock,
386 $budget_line{est_negative
} = '1' if $budget_est_remain < 0;
387 $budget_line{est_positive
} = '1' if $budget_est_remain > 0;
388 $budget_line{act_negative
} = '1' if $budget_act_remain < 0;
389 $budget_line{act_positive
} = '1' if $budget_act_remain > 0;
391 # skip if active set , and spent == 0
392 next if ( $show_active == '1' && ( $actual_spent == 0 ) );
394 push( @budget_lines, \
%budget_line );
397 if ( $output eq "file" ) {
398 _print_to_csv
(\
@authvals_row, \
@budget_lines);
403 authvals_row
=> \
@authvals_row,
404 budget_lines
=> \
@budget_lines,
405 budget_period_description
=> $period->{'budget_period_description'},
406 budget_period_locked
=> $period->{'budget_period_locked'},
407 budget_period_id
=> $budget_period_id,
409 show_active
=> $show_active,
410 show_actual
=> $show_actual,
411 show_percent
=> $show_percent,
412 show_mine
=> $show_mine,
413 CGIextChoice
=> $CGIextChoice,
414 CGIsepChoice
=> $CGIsepChoice,
416 authvals
=> \
@authvals_row,
417 hide_cols_loop
=> \
@hide_cols,
420 output_html_with_http_headers
$input, $cookie, $template->output;
423 my ( $header, $results ) = @_;
425 binmode STDOUT
, ':encoding(UTF-8)';
427 my $csv = Text
::CSV_XS
->new(
429 always_quote
=> 'TRUE',
432 print $input->header(
433 -type
=> 'application/vnd.sun.xml.calc',
434 -encoding
=> 'utf-8',
435 -attachment
=> "$basename.csv",
436 -name
=> "$basename.csv"
438 my @col = ( 'Budget name', 'Budget total' );
439 foreach my $row (@
$header) {
440 push @col, $row->{'val'};
442 push @col, 'Budget remaining';
445 my $str = $csv->string;
448 foreach my $row (@
$results) {
449 my @col = ( $row->{'budget_name'}, $row->{'budget_amount'} );
450 my $l = $row->{'lines'};
451 foreach my $line (@
$l) {
452 push @col, $line->{'estimated_amount'};
454 push @col, $row->{'budget_est_remain'};
456 my $str = $csv->string;