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 use Koha::Number::Price;
64 my $dbh = C4::Context->dbh;
68 my $searchfield = $input->param('searchfield') // '';
69 my $budget_period_id = $input->param('budget_period_id');
70 my $op = $input->param('op')||"else";
71 #my $sort1_authcat = $input->param('sort1_authcat');
72 #my $sort2_authcat = $input->param('sort2_authcat');
74 # get only the columns of aqbudgetperiods in budget_period_hashref
75 my @columns = Koha::Database->new()->schema->source('Aqbudgetperiod')->columns;
76 my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
77 $budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
78 $budget_period_hashref->{budget_period_enddate} = dt_from_string( $input->param('budget_period_enddate') );
80 my $activepagesize = 20;
81 my $inactivepagesize = 20;
82 $searchfield =~ s/\,//g;
84 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
86 template_name => "admin/aqbudgetperiods.tt",
90 flagsrequired => { acquisition => 'period_manage' },
96 # This is used in incbudgets-active-currency.inc
97 my $cur = GetCurrency();
98 $template->param( symbol => $cur->{symbol},
99 currency => $cur->{currency}
102 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
103 if ( $op eq 'add_form' ) {
104 ## add or modify a budget period (preparation)
105 ## get information about the budget period that must be modified
107 if ($budget_period_id) { # MOD
108 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
111 $budgetperiod_hash->{budget_period_total} =
112 Koha::Number::Price->new( $budgetperiod_hash->{budget_period_total} )
120 elsif ( $op eq 'add_validate' ) {
121 ## add or modify a budget period (confirmation)
123 ## update budget period data
124 if ( $budget_period_id ne '' ) {
125 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
126 my $status=ModBudgetPeriod($budget_period_hashref);
128 else { # ELSE ITS AN ADD
129 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
134 #--------------------------------------------------
135 elsif ( $op eq 'delete_confirm' ) {
136 ## delete a budget period (preparation)
137 my $dbh = C4::Context->dbh;
138 ## $total = number of records linked to the record that must be deleted
140 my $data = GetBudgetPeriod( $budget_period_id);
147 elsif ( $op eq 'delete_confirmed' ) {
148 ## delete the budget period record
150 my $data = GetBudgetPeriod( $budget_period_id);
151 DelBudgetPeriod($budget_period_id);
155 # display the form for duplicating
156 elsif ( $op eq 'duplicate_form'){
157 my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
159 'duplicate_form' => '1',
160 'budget_period_id' => $budget_period_id,
161 'budgetperiod' => $budgetperiod,
165 # handle the actual duplication
166 elsif ( $op eq 'duplicate_budget' ){
167 die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
169 my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate');
170 my $budget_period_enddate = dt_from_string $input->param('budget_period_enddate');
171 my $budget_period_description = $input->param('budget_period_description');
172 my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
173 my $reset_all_budgets = $input->param('reset_all_budgets');
175 my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
177 budget_period_id => $budget_period_id,
178 budget_period_startdate => $budget_period_startdate,
179 budget_period_enddate => $budget_period_enddate,
180 budget_period_description => $budget_period_description,
181 mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
182 reset_all_budgets => $reset_all_budgets,
186 # display the list of budgets
190 elsif ( $op eq 'close_form' ) {
192 my $budget_period = GetBudgetPeriod($budget_period_id);
194 my $active_budget_periods =
195 C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
197 # Remove the budget period from the list
198 $active_budget_periods =
199 [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
200 @$active_budget_periods ];
202 my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
204 # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
206 my $number_of_unreceived_orders = 0;
207 for my $budget (@$budgets_to_move) {
209 # We want to move funds from this budget
210 my $unreceived_orders = C4::Acquisition::SearchOrders(
212 budget_id => $budget->{budget_id},
216 $budget->{unreceived_orders} = $unreceived_orders;
217 $number_of_unreceived_orders += scalar(@$unreceived_orders);
222 budget_period_id => $budget_period_id,
223 budget_period_description =>
224 $budget_period->{budget_period_description},
225 budget_periods => $active_budget_periods,
226 budgets_to_move => $budgets_to_move,
227 number_of_unreceived_orders => $number_of_unreceived_orders,
231 elsif ( $op eq 'close_confirmed' ) {
232 my $to_budget_period_id = $input->param('to_budget_period_id');
233 my $move_remaining_unspent = $input->param('move_remaining_unspent');
234 my $report = C4::Budgets::MoveOrders(
236 from_budget_period_id => $budget_period_id,
237 to_budget_period_id => $to_budget_period_id,
238 move_remaining_unspent => $move_remaining_unspent,
242 my $from_budget_period = GetBudgetPeriod($budget_period_id);
243 my $to_budget_period = GetBudgetPeriod($to_budget_period_id);
246 budget_period_id => $from_budget_period->{budget_period_id},
247 budget_period_description => $from_budget_period->{budget_period_description},
248 from_budget_period => $from_budget_period,
249 to_budget_period => $to_budget_period,
254 # DEFAULT - DISPLAY AQPERIODS TABLE
255 # -------------------------------------------------------------------
256 # display the list of budget periods
258 my $activepage = $input->param('apage') || 1;
259 my $inactivepage = $input->param('ipage') || 1;
260 # Get active budget periods
261 my $results = GetBudgetPeriods(
262 { budget_period_active => 1 },
263 { -asc => 'budget_period_description' },
265 my $first = ( $activepage - 1 ) * $activepagesize;
266 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
267 my @period_active_loop;
269 foreach my $result ( @{$results}[ $first .. $last ] ) {
270 my $budgetperiod = $result;
271 $budgetperiod->{budget_active} = 1;
272 push( @period_active_loop, $budgetperiod );
274 my $url = "aqbudgetperiods.pl";
275 $url .= "?ipage=$inactivepage" if($inactivepage != 1);
276 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
278 # Get inactive budget periods
279 $results = GetBudgetPeriods(
280 { budget_period_active => 0 },
281 { -desc => 'budget_period_enddate' },
284 $first = ( $inactivepage - 1 ) * $inactivepagesize;
285 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
286 my @period_inactive_loop;
287 foreach my $result ( @{$results}[ $first .. $last ] ) {
288 my $budgetperiod = $result;
289 $budgetperiod->{budget_active} = 1;
290 push( @period_inactive_loop, $budgetperiod );
292 $url = "aqbudgetperiods.pl?tab=2";
293 $url .= "&apage=$activepage" if($activepage != 1);
294 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
296 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
298 period_active_loop => \@period_active_loop,
299 period_inactive_loop => \@period_inactive_loop,
300 active_pagination_bar => $active_pagination_bar,
301 inactive_pagination_bar => $inactive_pagination_bar,
305 $template->param($op=>1);
306 output_html_with_http_headers $input, $cookie, $template->output;