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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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
50 use List::Util qw/min/;
60 use Koha::Acquisition::Currencies;
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) =~ /$_/ ? ( $_ => scalar $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
75 $budget_period_hashref->{budget_period_startdate} = dt_from_string( scalar $input->param('budget_period_startdate') );
76 $budget_period_hashref->{budget_period_enddate} = dt_from_string( scalar $input->param('budget_period_enddate') );
78 $searchfield =~ s/\,//g;
80 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
82 template_name => "admin/aqbudgetperiods.tt",
86 flagsrequired => { acquisition => 'period_manage' },
92 # This is used in incbudgets-active-currency.inc
93 my $active_currency = Koha::Acquisition::Currencies->get_active;
94 $template->param( symbol => $active_currency->symbol,
95 currency => $active_currency->currency
98 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
99 if ( $op eq 'add_form' ) {
100 ## add or modify a budget period (preparation)
101 ## get information about the budget period that must be modified
103 if ($budget_period_id) { # MOD
104 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
113 elsif ( $op eq 'add_validate' ) {
114 ## add or modify a budget period (confirmation)
116 ## update budget period data
117 if ( $budget_period_id ne '' ) {
118 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
119 my $status=ModBudgetPeriod($budget_period_hashref);
121 else { # ELSE ITS AN ADD
122 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
127 #--------------------------------------------------
128 elsif ( $op eq 'delete_confirm' ) {
129 ## delete a budget period (preparation)
130 my $funds = GetBudgets({ budget_period_id => $budget_period_id });
131 my $fund_count = scalar @$funds;
132 if ( $fund_count > 0 ) {
133 $template->param( funds_exist => 1 );
136 #$total = number of records linked to the record that must be deleted
138 my $data = GetBudgetPeriod( $budget_period_id);
144 elsif ( $op eq 'delete_confirmed' ) {
145 ## confirm no funds have been added to budget
146 my $funds = GetBudgets({ budget_period_id => $budget_period_id });
147 my $fund_count = scalar @$funds;
148 if ( $fund_count > 0 ) {
149 $template->param( failed_delete_funds_exist => 1 );
151 ## delete the budget period record
152 my $data = GetBudgetPeriod( $budget_period_id);
153 DelBudgetPeriod($budget_period_id);
158 # display the form for duplicating
159 elsif ( $op eq 'duplicate_form'){
160 my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
162 'duplicate_form' => '1',
163 'budget_period_id' => $budget_period_id,
164 'budgetperiod' => $budgetperiod,
168 # handle the actual duplication
169 elsif ( $op eq 'duplicate_budget' ){
170 die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
172 my $budget_period_startdate = dt_from_string scalar $input->param('budget_period_startdate');
173 my $budget_period_enddate = dt_from_string scalar $input->param('budget_period_enddate');
174 my $budget_period_description = $input->param('budget_period_description');
175 my $amount_change_percentage = $input->param('amount_change_percentage');
176 my $amount_change_round_increment = $input->param('amount_change_round_increment');
177 my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
178 my $reset_all_budgets = $input->param('reset_all_budgets');
180 my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
182 budget_period_id => $budget_period_id,
183 budget_period_startdate => $budget_period_startdate,
184 budget_period_enddate => $budget_period_enddate,
185 budget_period_description => $budget_period_description,
186 amount_change_percentage => $amount_change_percentage,
187 amount_change_round_increment => $amount_change_round_increment,
188 mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
189 reset_all_budgets => $reset_all_budgets,
193 # display the list of budgets
197 elsif ( $op eq 'close_form' ) {
199 my $budget_period = GetBudgetPeriod($budget_period_id);
201 my $active_budget_periods =
202 C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
204 # Remove the budget period from the list
205 $active_budget_periods =
206 [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
207 @$active_budget_periods ];
209 my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
211 my $number_of_unreceived_orders = 0;
212 for my $budget (@$budgets_to_move) {
214 # We want to move funds from this budget
215 my $unreceived_orders = C4::Acquisition::SearchOrders(
217 budget_id => $budget->{budget_id},
221 $budget->{unreceived_orders} = $unreceived_orders;
222 $number_of_unreceived_orders += scalar(@$unreceived_orders);
227 budget_period_id => $budget_period_id,
228 budget_period_description =>
229 $budget_period->{budget_period_description},
230 budget_periods => $active_budget_periods,
231 budgets_to_move => $budgets_to_move,
232 number_of_unreceived_orders => $number_of_unreceived_orders,
236 elsif ( $op eq 'close_confirmed' ) {
237 my $to_budget_period_id = $input->param('to_budget_period_id');
238 my $move_remaining_unspent = $input->param('move_remaining_unspent');
239 my $report = C4::Budgets::MoveOrders(
241 from_budget_period_id => $budget_period_id,
242 to_budget_period_id => $to_budget_period_id,
243 move_remaining_unspent => $move_remaining_unspent,
247 my $from_budget_period = GetBudgetPeriod($budget_period_id);
248 my $to_budget_period = GetBudgetPeriod($to_budget_period_id);
251 budget_period_id => $from_budget_period->{budget_period_id},
252 budget_period_description => $from_budget_period->{budget_period_description},
253 from_budget_period => $from_budget_period,
254 to_budget_period => $to_budget_period,
259 # DEFAULT - DISPLAY AQPERIODS TABLE
260 # -------------------------------------------------------------------
261 # display the list of budget periods
263 my $activepage = $input->param('apage') || 1;
264 my $inactivepage = $input->param('ipage') || 1;
265 # Get active budget periods
266 my $results = GetBudgetPeriods(
267 { budget_period_active => 1 },
268 { -asc => 'budget_period_description' },
271 my @period_active_loop;
273 foreach my $result ( @{$results} ) {
274 my $budgetperiod = $result;
275 $budgetperiod->{budget_active} = 1;
276 my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
277 $budgetperiod->{count} = scalar @$funds;
278 push( @period_active_loop, $budgetperiod );
281 # Get inactive budget periods
282 $results = GetBudgetPeriods(
283 { budget_period_active => 0 },
284 { -desc => 'budget_period_enddate' },
287 my @period_inactive_loop;
288 foreach my $result ( @{$results} ) {
289 my $budgetperiod = $result;
290 $budgetperiod->{budget_active} = 1;
291 my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
292 $budgetperiod->{count} = scalar @$funds;
293 push( @period_inactive_loop, $budgetperiod );
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,
303 $template->param($op=>1);
304 output_html_with_http_headers $input, $cookie, $template->output;