Bug 24190: Add acquisition logging
[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 C4::Log qw(logaction);
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         # Grab the previous values so we can log them
118         my $budgetperiod_old=GetBudgetPeriod($budget_period_id);
119         $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
120         my $status=ModBudgetPeriod($budget_period_hashref);
121         # Log the budget modification
122         if (C4::Context->preference("AcqLog")) {
123             my $diff = 0 - ($budgetperiod_old->{budget_period_total} - $budget_period_hashref->{budget_period_total});
124             my $infos =
125                 eval { output_pref({ dt => dt_from_string( $input->param('budget_period_startdate') ), dateformat => 'iso', dateonly => 1 } ); } .
126                 eval { output_pref({ dt => dt_from_string( $input->param('budget_period_enddate') ), dateformat => 'iso', dateonly => 1 } ); } .
127                 sprintf("%010d", $budget_period_hashref->{budget_period_total}) .
128                 eval { output_pref({ dt => dt_from_string( $budgetperiod_old->{budget_period_startdate} ), dateformat => 'iso', dateonly => 1 } ); } .
129                 eval { output_pref({ dt => dt_from_string( $budgetperiod_old->{budget_period_enddate} ), dateformat => 'iso', dateonly => 1 } ); } .
130                 sprintf("%010d", $budgetperiod_old->{budget_period_total}) .
131                 sprintf("%010d", $diff);
132             logaction(
133                 'ACQUISITIONS',
134                 'MODIFY_BUDGET',
135                 $budget_period_id,
136                 $infos
137             );
138         }
139     }
140     else {    # ELSE ITS AN ADD
141         my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
142     }
143     $op='else';
144 }
145
146 #--------------------------------------------------
147 elsif ( $op eq 'delete_confirm' ) {
148 ## delete a budget period (preparation)
149     my $funds = GetBudgets({ budget_period_id => $budget_period_id });
150     my $fund_count = scalar @$funds;
151     if ( $fund_count > 0 ) {
152         $template->param( funds_exist => 1 );
153     }
154
155     #$total = number of records linked to the record that must be deleted
156     my $total = 0;
157     my $data = GetBudgetPeriod( $budget_period_id);
158     $template->param(
159         %$data
160     );
161 }
162
163 elsif ( $op eq 'delete_confirmed' ) {
164     ## confirm no funds have been added to budget
165     my $funds = GetBudgets({ budget_period_id => $budget_period_id });
166     my $fund_count = scalar @$funds;
167     if ( $fund_count > 0 ) {
168         $template->param( failed_delete_funds_exist => 1 );
169     } else {
170         ## delete the budget period record
171         my $data = GetBudgetPeriod( $budget_period_id);
172         DelBudgetPeriod($budget_period_id);
173     }
174         $op='else';
175 }
176
177 # display the form for duplicating
178 elsif ( $op eq 'duplicate_form'){
179     my $budgetperiod = GetBudgetPeriod($budget_period_id);
180     $template->param(
181         'duplicate_form' => '1',
182         'budget_period_id' => $budget_period_id,
183         'budgetperiod' => $budgetperiod,
184     );
185 }
186
187 # handle the actual duplication
188 elsif ( $op eq 'duplicate_budget' ){
189     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
190
191     my $budget_period_startdate = dt_from_string scalar $input->param('budget_period_startdate');
192     my $budget_period_enddate   = dt_from_string scalar $input->param('budget_period_enddate');
193     my $budget_period_description = $input->param('budget_period_description');
194     my $amount_change_percentage = $input->param('amount_change_percentage');
195     my $amount_change_round_increment = $input->param('amount_change_round_increment');
196     my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
197     my $reset_all_budgets = $input->param('reset_all_budgets');
198
199     my $new_budget_period_id = CloneBudgetPeriod(
200         {
201             budget_period_id        => $budget_period_id,
202             budget_period_startdate => $budget_period_startdate,
203             budget_period_enddate   => $budget_period_enddate,
204             budget_period_description => $budget_period_description,
205             amount_change_percentage => $amount_change_percentage,
206             amount_change_round_increment => $amount_change_round_increment,
207             mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
208             reset_all_budgets => $reset_all_budgets,
209         }
210     );
211
212     # display the list of budgets
213     $op = 'else';
214 }
215
216 elsif ( $op eq 'close_form' ) {
217
218     my $budget_period = GetBudgetPeriod($budget_period_id);
219
220     my $active_budget_periods =
221       GetBudgetPeriods( { budget_period_active => 1 } );
222
223     # Remove the budget period from the list
224     $active_budget_periods =
225       [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
226           @$active_budget_periods ];
227
228     my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
229
230     my $number_of_unreceived_orders = 0;
231     for my $budget (@$budgets_to_move) {
232
233         # We want to move funds from this budget
234         my $unreceived_orders = C4::Acquisition::SearchOrders(
235             {
236                 budget_id => $budget->{budget_id},
237                 pending   => 1,
238             }
239         );
240         $budget->{unreceived_orders} = $unreceived_orders;
241         $number_of_unreceived_orders += scalar(@$unreceived_orders);
242     }
243
244     $template->param(
245         close_form       => 1,
246         budget_period_id => $budget_period_id,
247         budget_period_description =>
248           $budget_period->{budget_period_description},
249         budget_periods              => $active_budget_periods,
250         budgets_to_move             => $budgets_to_move,
251         number_of_unreceived_orders => $number_of_unreceived_orders,
252     );
253 }
254
255 elsif ( $op eq 'close_confirmed' ) {
256     my $to_budget_period_id    = $input->param('to_budget_period_id');
257     my $move_remaining_unspent = $input->param('move_remaining_unspent');
258     my $report                 = MoveOrders(
259         {
260             from_budget_period_id  => $budget_period_id,
261             to_budget_period_id    => $to_budget_period_id,
262             move_remaining_unspent => $move_remaining_unspent,
263         }
264     );
265
266     my $from_budget_period = GetBudgetPeriod($budget_period_id);
267     my $to_budget_period   = GetBudgetPeriod($to_budget_period_id);
268     $template->param(
269         closed           => 1,
270         budget_period_id => $from_budget_period->{budget_period_id},
271         budget_period_description => $from_budget_period->{budget_period_description},
272         from_budget_period => $from_budget_period,
273         to_budget_period   => $to_budget_period,
274         report             => $report,
275     );
276 }
277
278 # DEFAULT - DISPLAY AQPERIODS TABLE
279 # -------------------------------------------------------------------
280 # display the list of budget periods
281
282 my $activepage = $input->param('apage') || 1;
283 my $inactivepage = $input->param('ipage') || 1;
284 # Get active budget periods
285 my $results = GetBudgetPeriods(
286     { budget_period_active => 1 },
287     { -asc => 'budget_period_description' },
288 );
289
290 my @period_active_loop;
291
292 foreach my $result ( @{$results} ) {
293     my $budgetperiod = $result;
294     $budgetperiod->{budget_active} = 1;
295     my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
296     $budgetperiod->{count} = scalar @$funds;
297     push( @period_active_loop, $budgetperiod );
298 }
299
300 # Get inactive budget periods
301 $results = GetBudgetPeriods(
302     { budget_period_active => 0 },
303     { -desc => 'budget_period_enddate' },
304 );
305
306 my @period_inactive_loop;
307 foreach my $result ( @{$results} ) {
308     my $budgetperiod = $result;
309     $budgetperiod->{budget_active} = 1;
310     my $funds = GetBudgets({ budget_period_id => $budgetperiod->{budget_period_id} });
311     $budgetperiod->{count} = scalar @$funds;
312     push( @period_inactive_loop, $budgetperiod );
313 }
314
315 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
316 $template->param(
317     period_active_loop      => \@period_active_loop,
318     period_inactive_loop    => \@period_inactive_loop,
319     tab                     => $tab,
320 );
321
322 $template->param($op=>1);
323 output_html_with_http_headers $input, $cookie, $template->output;