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