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