3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 =head1 admin/aqbudgetperiods.pl
23 script to administer the budget periods table
24 This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
27 this script use an $op to know what to do.
28 if $op is empty or none of the above values,
29 - the default screen is build (with all records, or filtered datas).
30 - the user can clic on add, modify or delete record.
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
35 - the user has just send datas, so we create/modify the record
37 - we show the record having primkey=$primkey and ask for deletion validation form
38 if $op=delete_confirmed
39 - we delete the record having primkey=$primkey
41 - displays the duplication of budget period form (allowing specification of dates)
42 if $op=duplicate_budget
43 - we perform the duplication of the budget period specified as budget_period_id
49 use Number
::Format
qw(format_price);
51 use List
::Util qw
/min/;
62 my $dbh = C4
::Context
->dbh;
66 my $searchfield = $input->param('searchfield') // '';
67 my $budget_period_id = $input->param('budget_period_id');
68 my $op = $input->param('op')||"else";
69 #my $sort1_authcat = $input->param('sort1_authcat');
70 #my $sort2_authcat = $input->param('sort2_authcat');
72 # get only the columns of aqbudgetperiods in budget_period_hashref
73 my @columns = Koha
::Database
->new()->schema->source('Aqbudgetperiod')->columns;
74 my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ?
( $_ => $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
75 $budget_period_hashref->{budget_period_startdate
} = dt_from_string
( $input->param('budget_period_startdate') );
76 $budget_period_hashref->{budget_period_enddate
} = dt_from_string
( $input->param('budget_period_enddate') );
78 my $activepagesize = 20;
79 my $inactivepagesize = 20;
80 $searchfield =~ s/\,//g;
82 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user
(
84 template_name
=> "admin/aqbudgetperiods.tt",
88 flagsrequired
=> { acquisition
=> 'period_manage' },
94 my $cur = GetCurrency
();
95 $template->param( symbol
=> $cur->{symbol
},
96 currency
=> $cur->{currency
}
98 my $cur_format = C4
::Context
->preference("CurrencyFormat");
101 if ( $cur_format eq 'US' ) {
102 $num = new Number
::Format
(
103 'int_curr_symbol' => '',
104 'mon_thousands_sep' => ',',
105 'mon_decimal_point' => '.'
107 } elsif ( $cur_format eq 'FR' ) {
108 $num = new Number
::Format
(
109 'decimal_fill' => '2',
110 'decimal_point' => ',',
111 'int_curr_symbol' => '',
112 'mon_thousands_sep' => ' ',
113 'thousands_sep' => ' ',
114 'mon_decimal_point' => ','
119 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
120 if ( $op eq 'add_form' ) {
121 ## add or modify a budget period (preparation)
122 ## get information about the budget period that must be modified
124 if ($budget_period_id) { # MOD
125 my $budgetperiod_hash=GetBudgetPeriod
($budget_period_id);
128 my $editnum = new Number
::Format
(
129 'int_curr_symbol' => '',
130 'thousands_sep' => '',
131 'mon_thousands_sep' => '',
132 'mon_decimal_point' => '.'
135 $$budgetperiod_hash{budget_period_total
}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
142 elsif ( $op eq 'add_validate' ) {
143 ## add or modify a budget period (confirmation)
145 ## update budget period data
146 if ( $budget_period_id ne '' ) {
147 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
148 my $status=ModBudgetPeriod
($budget_period_hashref);
150 else { # ELSE ITS AN ADD
151 my $budget_period_id=AddBudgetPeriod
($budget_period_hashref);
156 #--------------------------------------------------
157 elsif ( $op eq 'delete_confirm' ) {
158 ## delete a budget period (preparation)
159 my $dbh = C4
::Context
->dbh;
160 ## $total = number of records linked to the record that must be deleted
162 my $data = GetBudgetPeriod
( $budget_period_id);
164 $$data{'budget_period_total'}=$num->format_price( $data->{'budget_period_total'});
170 elsif ( $op eq 'delete_confirmed' ) {
171 ## delete the budget period record
173 my $data = GetBudgetPeriod
( $budget_period_id);
174 DelBudgetPeriod
($budget_period_id);
178 # display the form for duplicating
179 elsif ( $op eq 'duplicate_form'){
180 my $budgetperiod = GetBudgetPeriod
($budget_period_id, $input);
182 'duplicate_form' => '1',
183 'budget_period_id' => $budget_period_id,
184 'budgetperiod' => $budgetperiod,
188 # handle the actual duplication
189 elsif ( $op eq 'duplicate_budget' ){
190 die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
192 my $budget_period_startdate = dt_from_string
$input->param('budget_period_startdate');
193 my $budget_period_enddate = dt_from_string
$input->param('budget_period_enddate');
194 my $budget_period_description = $input->param('budget_period_description');
195 my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
196 my $reset_all_budgets = $input->param('reset_all_budgets');
198 my $new_budget_period_id = C4
::Budgets
::CloneBudgetPeriod
(
200 budget_period_id
=> $budget_period_id,
201 budget_period_startdate
=> $budget_period_startdate,
202 budget_period_enddate
=> $budget_period_enddate,
203 budget_period_description
=> $budget_period_description,
204 mark_original_budget_as_inactive
=> $mark_original_budget_as_inactive,
205 reset_all_budgets
=> $reset_all_budgets,
209 # display the list of budgets
213 elsif ( $op eq 'close_form' ) {
215 my $budget_period = GetBudgetPeriod
($budget_period_id);
217 my $active_budget_periods =
218 C4
::Budgets
::GetBudgetPeriods
( { budget_period_active
=> 1 } );
220 # Remove the budget period from the list
221 $active_budget_periods =
222 [ map { ( $_->{budget_period_id
} == $budget_period_id ) ?
() : $_ }
223 @
$active_budget_periods ];
225 my $budgets_to_move = GetBudgetHierarchy
($budget_period_id);
227 # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
229 my $number_of_unreceived_orders = 0;
230 for my $budget (@
$budgets_to_move) {
232 # We want to move funds from this budget
233 my $unreceived_orders = C4
::Acquisition
::SearchOrders
(
235 budget_id
=> $budget->{budget_id
},
239 $budget->{unreceived_orders
} = $unreceived_orders;
240 $number_of_unreceived_orders += scalar(@
$unreceived_orders);
245 budget_period_id
=> $budget_period_id,
246 budget_period_description
=>
247 $budget_period->{budget_period_description
},
248 budget_periods
=> $active_budget_periods,
249 budgets_to_move
=> $budgets_to_move,
250 number_of_unreceived_orders
=> $number_of_unreceived_orders,
254 elsif ( $op eq 'close_confirmed' ) {
255 my $to_budget_period_id = $input->param('to_budget_period_id');
256 my $move_remaining_unspent = $input->param('move_remaining_unspent');
257 my $report = C4
::Budgets
::MoveOrders
(
259 from_budget_period_id
=> $budget_period_id,
260 to_budget_period_id
=> $to_budget_period_id,
261 move_remaining_unspent
=> $move_remaining_unspent,
265 my $from_budget_period = GetBudgetPeriod
($budget_period_id);
266 my $to_budget_period = GetBudgetPeriod
($to_budget_period_id);
269 budget_period_id
=> $from_budget_period->{budget_period_id
},
270 budget_period_description
=> $from_budget_period->{budget_period_description
},
271 from_budget_period
=> $from_budget_period,
272 to_budget_period
=> $to_budget_period,
277 # DEFAULT - DISPLAY AQPERIODS TABLE
278 # -------------------------------------------------------------------
279 # display the list of budget periods
281 my $activepage = $input->param('apage') || 1;
282 my $inactivepage = $input->param('ipage') || 1;
283 # Get active budget periods
284 my $results = GetBudgetPeriods
(
285 { budget_period_active
=> 1 },
286 { -asc
=> 'budget_period_description' },
288 my $first = ( $activepage - 1 ) * $activepagesize;
289 my $last = min
( $first + $activepagesize - 1, scalar @
{$results} - 1, );
290 my @period_active_loop;
292 foreach my $result ( @
{$results}[ $first .. $last ] ) {
293 my $budgetperiod = $result;
294 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
295 $budgetperiod->{budget_active
} = 1;
296 push( @period_active_loop, $budgetperiod );
298 my $url = "aqbudgetperiods.pl";
299 $url .= "?ipage=$inactivepage" if($inactivepage != 1);
300 my $active_pagination_bar = pagination_bar
($url, getnbpages
( scalar(@
$results), $activepagesize), $activepage, "apage");
302 # Get inactive budget periods
303 $results = GetBudgetPeriods
(
304 { budget_period_active
=> 0 },
305 { -desc
=> 'budget_period_enddate' },
308 $first = ( $inactivepage - 1 ) * $inactivepagesize;
309 $last = min
( $first + $inactivepagesize - 1, scalar @
{$results} - 1, );
310 my @period_inactive_loop;
311 foreach my $result ( @
{$results}[ $first .. $last ] ) {
312 my $budgetperiod = $result;
313 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
314 $budgetperiod->{budget_active
} = 1;
315 push( @period_inactive_loop, $budgetperiod );
317 $url = "aqbudgetperiods.pl?tab=2";
318 $url .= "&apage=$activepage" if($activepage != 1);
319 my $inactive_pagination_bar = pagination_bar
($url, getnbpages
( scalar(@
$results), $inactivepagesize), $inactivepage, "ipage");
321 my $tab = $input->param('tab') ?
$input->param('tab') - 1 : 0;
323 period_active_loop
=> \
@period_active_loop,
324 period_inactive_loop
=> \
@period_inactive_loop,
325 active_pagination_bar
=> $active_pagination_bar,
326 inactive_pagination_bar
=> $inactive_pagination_bar,
330 $template->param($op=>1);
331 output_html_with_http_headers
$input, $cookie, $template->output;