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