Bug 28572: Remove C4::Debug
[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 Koha::Acquisition::Currencies;
60
61 my $dbh = C4::Context->dbh;
62
63 my $input       = CGI->new;
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) =~ /$_/ ? ( $_ => scalar $input->param($_) )  : () } keys( %{$input->Vars()} ) } ;
74 $budget_period_hashref->{budget_period_startdate} = dt_from_string( scalar $input->param('budget_period_startdate') );
75 $budget_period_hashref->{budget_period_enddate}   = dt_from_string( scalar $input->param('budget_period_enddate') );
76
77 $searchfield =~ s/\,//g;
78
79 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
80     {
81         template_name   => "admin/aqbudgetperiods.tt",
82         query           => $input,
83         type            => "intranet",
84         flagsrequired   => { acquisition => 'period_manage' },
85     }
86 );
87
88
89 # This is used in incbudgets-active-currency.inc
90 my $active_currency = Koha::Acquisition::Currencies->get_active;
91 if ( $active_currency ) {
92     $template->param( symbol => $active_currency->symbol,
93                       currency => $active_currency->currency
94                    );
95 }
96
97 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
98 if ( $op eq 'add_form' ) {
99     ## add or modify a budget period (preparation)
100     ## get information about the budget period that must be modified
101
102     if ($budget_period_id) {    # MOD
103                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
104         # get dropboxes
105
106         $template->param(
107                         %$budgetperiod_hash
108         );
109     } # IF-MOD
110 }
111
112 elsif ( $op eq 'add_validate' ) {
113 ## add or modify a budget period (confirmation)
114
115     ## update budget period data
116         if ( $budget_period_id ne '' ) {
117                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
118                 my $status=ModBudgetPeriod($budget_period_hashref);
119         } 
120         else {    # ELSE ITS AN ADD
121                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
122         }
123         $op='else';
124 }
125
126 #--------------------------------------------------
127 elsif ( $op eq 'delete_confirm' ) {
128 ## delete a budget period (preparation)
129     my $funds = GetBudgets({ budget_period_id => $budget_period_id });
130     my $fund_count = scalar @$funds;
131     if ( $fund_count > 0 ) {
132         $template->param( funds_exist => 1 );
133     }
134
135     #$total = number of records linked to the record that must be deleted
136     my $total = 0;
137     my $data = GetBudgetPeriod( $budget_period_id);
138     $template->param(
139         %$data
140     );
141 }
142
143 elsif ( $op eq 'delete_confirmed' ) {
144     ## confirm no funds have been added to budget
145     my $funds = GetBudgets({ budget_period_id => $budget_period_id });
146     my $fund_count = scalar @$funds;
147     if ( $fund_count > 0 ) {
148         $template->param( failed_delete_funds_exist => 1 );
149     } else {
150         ## delete the budget period record
151         my $data = GetBudgetPeriod( $budget_period_id);
152         DelBudgetPeriod($budget_period_id);
153     }
154         $op='else';
155 }
156
157 # display the form for duplicating
158 elsif ( $op eq 'duplicate_form'){
159     my $budgetperiod = GetBudgetPeriod($budget_period_id);
160     $template->param(
161         'duplicate_form' => '1',
162         'budget_period_id' => $budget_period_id,
163         'budgetperiod' => $budgetperiod,
164     );
165 }
166
167 # handle the actual duplication
168 elsif ( $op eq 'duplicate_budget' ){
169     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
170
171     my $budget_period_startdate = dt_from_string scalar $input->param('budget_period_startdate');
172     my $budget_period_enddate   = dt_from_string scalar $input->param('budget_period_enddate');
173     my $budget_period_description = $input->param('budget_period_description');
174     my $amount_change_percentage = $input->param('amount_change_percentage');
175     my $amount_change_round_increment = $input->param('amount_change_round_increment');
176     my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
177     my $reset_all_budgets = $input->param('reset_all_budgets');
178
179     my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
180         {
181             budget_period_id        => $budget_period_id,
182             budget_period_startdate => $budget_period_startdate,
183             budget_period_enddate   => $budget_period_enddate,
184             budget_period_description => $budget_period_description,
185             amount_change_percentage => $amount_change_percentage,
186             amount_change_round_increment => $amount_change_round_increment,
187             mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
188             reset_all_budgets => $reset_all_budgets,
189         }
190     );
191
192     # display the list of budgets
193     $op = 'else';
194 }
195
196 elsif ( $op eq 'close_form' ) {
197
198     my $budget_period = GetBudgetPeriod($budget_period_id);
199
200     my $active_budget_periods =
201       C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
202
203     # Remove the budget period from the list
204     $active_budget_periods =
205       [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
206           @$active_budget_periods ];
207
208     my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
209
210     my $number_of_unreceived_orders = 0;
211     for my $budget (@$budgets_to_move) {
212
213         # We want to move funds from this budget
214         my $unreceived_orders = C4::Acquisition::SearchOrders(
215             {
216                 budget_id => $budget->{budget_id},
217                 pending   => 1,
218             }
219         );
220         $budget->{unreceived_orders} = $unreceived_orders;
221         $number_of_unreceived_orders += scalar(@$unreceived_orders);
222     }
223
224     $template->param(
225         close_form       => 1,
226         budget_period_id => $budget_period_id,
227         budget_period_description =>
228           $budget_period->{budget_period_description},
229         budget_periods              => $active_budget_periods,
230         budgets_to_move             => $budgets_to_move,
231         number_of_unreceived_orders => $number_of_unreceived_orders,
232     );
233 }
234
235 elsif ( $op eq 'close_confirmed' ) {
236     my $to_budget_period_id    = $input->param('to_budget_period_id');
237     my $move_remaining_unspent = $input->param('move_remaining_unspent');
238     my $report                 = C4::Budgets::MoveOrders(
239         {
240             from_budget_period_id  => $budget_period_id,
241             to_budget_period_id    => $to_budget_period_id,
242             move_remaining_unspent => $move_remaining_unspent,
243         }
244     );
245
246     my $from_budget_period = GetBudgetPeriod($budget_period_id);
247     my $to_budget_period   = GetBudgetPeriod($to_budget_period_id);
248     $template->param(
249         closed           => 1,
250         budget_period_id => $from_budget_period->{budget_period_id},
251         budget_period_description => $from_budget_period->{budget_period_description},
252         from_budget_period => $from_budget_period,
253         to_budget_period   => $to_budget_period,
254         report             => $report,
255     );
256 }
257
258 # DEFAULT - DISPLAY AQPERIODS TABLE
259 # -------------------------------------------------------------------
260 # display the list of budget periods
261
262 my $activepage = $input->param('apage') || 1;
263 my $inactivepage = $input->param('ipage') || 1;
264 # Get active budget periods
265 my $results = GetBudgetPeriods(
266     { budget_period_active => 1 },
267     { -asc => 'budget_period_description' },
268 );
269
270 my @period_active_loop;
271
272 foreach my $result ( @{$results} ) {
273     my $budgetperiod = $result;
274     $budgetperiod->{budget_active} = 1;
275     my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
276     $budgetperiod->{count} = scalar @$funds;
277     push( @period_active_loop, $budgetperiod );
278 }
279
280 # Get inactive budget periods
281 $results = GetBudgetPeriods(
282     { budget_period_active => 0 },
283     { -desc => 'budget_period_enddate' },
284 );
285
286 my @period_inactive_loop;
287 foreach my $result ( @{$results} ) {
288     my $budgetperiod = $result;
289     $budgetperiod->{budget_active} = 1;
290     my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
291     $budgetperiod->{count} = scalar @$funds;
292     push( @period_inactive_loop, $budgetperiod );
293 }
294
295 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
296 $template->param(
297     period_active_loop      => \@period_active_loop,
298     period_inactive_loop    => \@period_inactive_loop,
299     tab                     => $tab,
300 );
301
302 $template->param($op=>1);
303 output_html_with_http_headers $input, $cookie, $template->output;