Bug 14753 - Use columns configuration for checkins table
[koha.git] / admin / aqbudgetperiods.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
4 #                SAN Ouest Provence
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 =head1 admin/aqbudgetperiods.pl
22
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)
25
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
44
45 =cut
46
47 use Modern::Perl;
48
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;
60
61 my $dbh = C4::Context->dbh;
62
63 my $input       = new CGI;
64
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');
70
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') );
76
77 my $activepagesize = 20;
78 my $inactivepagesize = 20;
79 $searchfield =~ s/\,//g;
80
81 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
82     {
83         template_name   => "admin/aqbudgetperiods.tt",
84         query           => $input,
85         type            => "intranet",
86         authnotrequired => 0,
87         flagsrequired   => { acquisition => 'period_manage' },
88         debug           => 1,
89     }
90 );
91
92
93 # This is used in incbudgets-active-currency.inc
94 my $cur = GetCurrency();
95 $template->param( symbol => $cur->{symbol},
96                   currency => $cur->{currency}
97                );
98
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
103
104     if ($budget_period_id) {    # MOD
105                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
106         # get dropboxes
107
108         $template->param(
109                         %$budgetperiod_hash
110         );
111     } # IF-MOD
112 }
113
114 elsif ( $op eq 'add_validate' ) {
115 ## add or modify a budget period (confirmation)
116
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);
121         } 
122         else {    # ELSE ITS AN ADD
123                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
124         }
125         $op='else';
126 }
127
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);
135
136     $template->param(
137                 %$data
138     );
139 }
140
141 elsif ( $op eq 'delete_confirmed' ) {
142 ## delete the budget period record
143
144     my $data = GetBudgetPeriod( $budget_period_id);
145     DelBudgetPeriod($budget_period_id);
146         $op='else';
147 }
148
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,
156     );
157 }
158
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 '' );
162
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 $amount_change_percentage = $input->param('amount_change_percentage');
167     my $amount_change_round_increment = $input->param('amount_change_round_increment');
168     my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
169     my $reset_all_budgets = $input->param('reset_all_budgets');
170
171     my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
172         {
173             budget_period_id        => $budget_period_id,
174             budget_period_startdate => $budget_period_startdate,
175             budget_period_enddate   => $budget_period_enddate,
176             budget_period_description => $budget_period_description,
177             amount_change_percentage => $amount_change_percentage,
178             amount_change_round_increment => $amount_change_round_increment,
179             mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
180             reset_all_budgets => $reset_all_budgets,
181         }
182     );
183
184     # display the list of budgets
185     $op = 'else';
186 }
187
188 elsif ( $op eq 'close_form' ) {
189
190     my $budget_period = GetBudgetPeriod($budget_period_id);
191
192     my $active_budget_periods =
193       C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
194
195     # Remove the budget period from the list
196     $active_budget_periods =
197       [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
198           @$active_budget_periods ];
199
200     my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
201
202     # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
203
204     my $number_of_unreceived_orders = 0;
205     for my $budget (@$budgets_to_move) {
206
207         # We want to move funds from this budget
208         my $unreceived_orders = C4::Acquisition::SearchOrders(
209             {
210                 budget_id => $budget->{budget_id},
211                 pending   => 1,
212             }
213         );
214         $budget->{unreceived_orders} = $unreceived_orders;
215         $number_of_unreceived_orders += scalar(@$unreceived_orders);
216     }
217
218     $template->param(
219         close_form       => 1,
220         budget_period_id => $budget_period_id,
221         budget_period_description =>
222           $budget_period->{budget_period_description},
223         budget_periods              => $active_budget_periods,
224         budgets_to_move             => $budgets_to_move,
225         number_of_unreceived_orders => $number_of_unreceived_orders,
226     );
227 }
228
229 elsif ( $op eq 'close_confirmed' ) {
230     my $to_budget_period_id    = $input->param('to_budget_period_id');
231     my $move_remaining_unspent = $input->param('move_remaining_unspent');
232     my $report                 = C4::Budgets::MoveOrders(
233         {
234             from_budget_period_id  => $budget_period_id,
235             to_budget_period_id    => $to_budget_period_id,
236             move_remaining_unspent => $move_remaining_unspent,
237         }
238     );
239
240     my $from_budget_period = GetBudgetPeriod($budget_period_id);
241     my $to_budget_period   = GetBudgetPeriod($to_budget_period_id);
242     $template->param(
243         closed           => 1,
244         budget_period_id => $from_budget_period->{budget_period_id},
245         budget_period_description => $from_budget_period->{budget_period_description},
246         from_budget_period => $from_budget_period,
247         to_budget_period   => $to_budget_period,
248         report             => $report,
249     );
250 }
251
252 # DEFAULT - DISPLAY AQPERIODS TABLE
253 # -------------------------------------------------------------------
254 # display the list of budget periods
255
256 my $activepage = $input->param('apage') || 1;
257 my $inactivepage = $input->param('ipage') || 1;
258 # Get active budget periods
259 my $results = GetBudgetPeriods(
260     { budget_period_active => 1 },
261     { -asc => 'budget_period_description' },
262 );
263 my $first = ( $activepage - 1 ) * $activepagesize;
264 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
265 my @period_active_loop;
266
267 foreach my $result ( @{$results}[ $first .. $last ] ) {
268     my $budgetperiod = $result;
269     $budgetperiod->{budget_active} = 1;
270     push( @period_active_loop, $budgetperiod );
271 }
272 my $url = "aqbudgetperiods.pl";
273 $url .=  "?ipage=$inactivepage" if($inactivepage != 1);
274 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
275
276 # Get inactive budget periods
277 $results = GetBudgetPeriods(
278     { budget_period_active => 0 },
279     { -desc => 'budget_period_enddate' },
280 );
281
282 $first = ( $inactivepage - 1 ) * $inactivepagesize;
283 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
284 my @period_inactive_loop;
285 foreach my $result ( @{$results}[ $first .. $last ] ) {
286     my $budgetperiod = $result;
287     $budgetperiod->{budget_active} = 1;
288     push( @period_inactive_loop, $budgetperiod );
289 }
290 $url = "aqbudgetperiods.pl?tab=2";
291 $url .= "&apage=$activepage" if($activepage != 1);
292 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
293
294 my $branchloop = C4::Branch::GetBranchesLoop();
295
296 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
297 $template->param(
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,
302     tab                     => $tab,
303     branchloop              => $branchloop,
304 );
305
306 $template->param($op=>1);
307 output_html_with_http_headers $input, $cookie, $template->output;