Bug 12252: Add tests for EmbedItemsInMarcBiblio
[koha.git] / admin / aqbudgetperiods.pl
blobf837b818a1621e288c1626cfd3fcf4b61cd6e510
1 #!/usr/bin/perl
3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
4 # SAN Ouest Provence
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)
26 ALGO :
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.
31 if $op=add_form
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
34 if $op=add_validate
35 - the user has just send datas, so we create/modify the record
36 if $op=delete_confirm
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
40 if $op=duplicate_form
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
45 =cut
47 use Modern::Perl;
49 use CGI qw ( -utf8 );
50 use List::Util qw/min/;
51 use Koha::DateUtils;
52 use Koha::Database;
53 use C4::Koha;
54 use C4::Context;
55 use C4::Auth;
56 use C4::Output;
57 use C4::Acquisition;
58 use C4::Budgets;
59 use C4::Debug;
61 my $dbh = C4::Context->dbh;
63 my $input = new CGI;
65 my $searchfield = $input->param('searchfield') // '';
66 my $budget_period_id = $input->param('budget_period_id');
67 my $op = $input->param('op')||"else";
68 #my $sort1_authcat = $input->param('sort1_authcat');
69 #my $sort2_authcat = $input->param('sort2_authcat');
71 # get only the columns of aqbudgetperiods in budget_period_hashref
72 my @columns = Koha::Database->new()->schema->source('Aqbudgetperiod')->columns;
73 my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
74 $budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
75 $budget_period_hashref->{budget_period_enddate} = dt_from_string( $input->param('budget_period_enddate') );
77 my $activepagesize = 20;
78 my $inactivepagesize = 20;
79 $searchfield =~ s/\,//g;
81 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
83 template_name => "admin/aqbudgetperiods.tt",
84 query => $input,
85 type => "intranet",
86 authnotrequired => 0,
87 flagsrequired => { acquisition => 'period_manage' },
88 debug => 1,
93 # This is used in incbudgets-active-currency.inc
94 my $cur = GetCurrency();
95 $template->param( symbol => $cur->{symbol},
96 currency => $cur->{currency}
99 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
100 if ( $op eq 'add_form' ) {
101 ## add or modify a budget period (preparation)
102 ## get information about the budget period that must be modified
104 if ($budget_period_id) { # MOD
105 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
106 # get dropboxes
108 $template->param(
109 %$budgetperiod_hash
111 } # IF-MOD
114 elsif ( $op eq 'add_validate' ) {
115 ## add or modify a budget period (confirmation)
117 ## update budget period data
118 if ( $budget_period_id ne '' ) {
119 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
120 my $status=ModBudgetPeriod($budget_period_hashref);
122 else { # ELSE ITS AN ADD
123 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
125 $op='else';
128 #--------------------------------------------------
129 elsif ( $op eq 'delete_confirm' ) {
130 ## delete a budget period (preparation)
131 my $dbh = C4::Context->dbh;
132 ## $total = number of records linked to the record that must be deleted
133 my $total = 0;
134 my $data = GetBudgetPeriod( $budget_period_id);
136 $template->param(
137 %$data
141 elsif ( $op eq 'delete_confirmed' ) {
142 ## delete the budget period record
144 my $data = GetBudgetPeriod( $budget_period_id);
145 DelBudgetPeriod($budget_period_id);
146 $op='else';
149 # display the form for duplicating
150 elsif ( $op eq 'duplicate_form'){
151 my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
152 $template->param(
153 'duplicate_form' => '1',
154 'budget_period_id' => $budget_period_id,
155 'budgetperiod' => $budgetperiod,
159 # handle the actual duplication
160 elsif ( $op eq 'duplicate_budget' ){
161 die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
163 my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate');
164 my $budget_period_enddate = dt_from_string $input->param('budget_period_enddate');
165 my $budget_period_description = $input->param('budget_period_description');
166 my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
167 my $reset_all_budgets = $input->param('reset_all_budgets');
169 my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
171 budget_period_id => $budget_period_id,
172 budget_period_startdate => $budget_period_startdate,
173 budget_period_enddate => $budget_period_enddate,
174 budget_period_description => $budget_period_description,
175 mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
176 reset_all_budgets => $reset_all_budgets,
180 # display the list of budgets
181 $op = 'else';
184 elsif ( $op eq 'close_form' ) {
186 my $budget_period = GetBudgetPeriod($budget_period_id);
188 my $active_budget_periods =
189 C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
191 # Remove the budget period from the list
192 $active_budget_periods =
193 [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
194 @$active_budget_periods ];
196 my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
198 # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
200 my $number_of_unreceived_orders = 0;
201 for my $budget (@$budgets_to_move) {
203 # We want to move funds from this budget
204 my $unreceived_orders = C4::Acquisition::SearchOrders(
206 budget_id => $budget->{budget_id},
207 pending => 1,
210 $budget->{unreceived_orders} = $unreceived_orders;
211 $number_of_unreceived_orders += scalar(@$unreceived_orders);
214 $template->param(
215 close_form => 1,
216 budget_period_id => $budget_period_id,
217 budget_period_description =>
218 $budget_period->{budget_period_description},
219 budget_periods => $active_budget_periods,
220 budgets_to_move => $budgets_to_move,
221 number_of_unreceived_orders => $number_of_unreceived_orders,
225 elsif ( $op eq 'close_confirmed' ) {
226 my $to_budget_period_id = $input->param('to_budget_period_id');
227 my $move_remaining_unspent = $input->param('move_remaining_unspent');
228 my $report = C4::Budgets::MoveOrders(
230 from_budget_period_id => $budget_period_id,
231 to_budget_period_id => $to_budget_period_id,
232 move_remaining_unspent => $move_remaining_unspent,
236 my $from_budget_period = GetBudgetPeriod($budget_period_id);
237 my $to_budget_period = GetBudgetPeriod($to_budget_period_id);
238 $template->param(
239 closed => 1,
240 budget_period_id => $from_budget_period->{budget_period_id},
241 budget_period_description => $from_budget_period->{budget_period_description},
242 from_budget_period => $from_budget_period,
243 to_budget_period => $to_budget_period,
244 report => $report,
248 # DEFAULT - DISPLAY AQPERIODS TABLE
249 # -------------------------------------------------------------------
250 # display the list of budget periods
252 my $activepage = $input->param('apage') || 1;
253 my $inactivepage = $input->param('ipage') || 1;
254 # Get active budget periods
255 my $results = GetBudgetPeriods(
256 { budget_period_active => 1 },
257 { -asc => 'budget_period_description' },
259 my $first = ( $activepage - 1 ) * $activepagesize;
260 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
261 my @period_active_loop;
263 foreach my $result ( @{$results}[ $first .. $last ] ) {
264 my $budgetperiod = $result;
265 $budgetperiod->{budget_active} = 1;
266 push( @period_active_loop, $budgetperiod );
268 my $url = "aqbudgetperiods.pl";
269 $url .= "?ipage=$inactivepage" if($inactivepage != 1);
270 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
272 # Get inactive budget periods
273 $results = GetBudgetPeriods(
274 { budget_period_active => 0 },
275 { -desc => 'budget_period_enddate' },
278 $first = ( $inactivepage - 1 ) * $inactivepagesize;
279 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
280 my @period_inactive_loop;
281 foreach my $result ( @{$results}[ $first .. $last ] ) {
282 my $budgetperiod = $result;
283 $budgetperiod->{budget_active} = 1;
284 push( @period_inactive_loop, $budgetperiod );
286 $url = "aqbudgetperiods.pl?tab=2";
287 $url .= "&apage=$activepage" if($activepage != 1);
288 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
290 my $branchloop = C4::Branch::GetBranchesLoop();
292 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
293 $template->param(
294 period_active_loop => \@period_active_loop,
295 period_inactive_loop => \@period_inactive_loop,
296 active_pagination_bar => $active_pagination_bar,
297 inactive_pagination_bar => $inactive_pagination_bar,
298 tab => $tab,
299 branchloop => $branchloop,
302 $template->param($op=>1);
303 output_html_with_http_headers $input, $cookie, $template->output;