Bug 12482 [QA Followup]
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 ## modules
48 use strict;
49 #use warnings; FIXME - Bug 2505
50 use Number::Format qw(format_price);
51 use CGI;
52 use List::Util qw/min/;
53 use Koha::DateUtils;
54 use Koha::Database;
55 use C4::Koha;
56 use C4::Context;
57 use C4::Auth;
58 use C4::Output;
59 use C4::Acquisition;
60 use C4::Budgets;
61 use C4::Debug;
62
63 my $dbh = C4::Context->dbh;
64
65 my $input       = new CGI;
66
67 my $searchfield          = $input->param('searchfield');
68 my $budget_period_id     = $input->param('budget_period_id');
69 my $op                   = $input->param('op')||"else";
70 #my $sort1_authcat = $input->param('sort1_authcat');
71 #my $sort2_authcat = $input->param('sort2_authcat');
72
73 # get only the columns of aqbudgetperiods in budget_period_hashref
74 my @columns = Koha::Database->new()->schema->source('Aqbudgetperiod')->columns;
75 my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) )  : () } keys( %{$input->Vars()} ) } ;
76 $budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
77 $budget_period_hashref->{budget_period_enddate}   = dt_from_string( $input->param('budget_period_enddate') );
78
79 my $activepagesize = 20;
80 my $inactivepagesize = 20;
81 $searchfield =~ s/\,//g;
82
83 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
84         {   template_name   => "admin/aqbudgetperiods.tmpl",
85                 query           => $input,
86                 type            => "intranet",
87                 authnotrequired => 0,
88                 flagsrequired   => { acquisition => 'period_manage' },
89                 debug           => 1,
90         }
91 );
92
93
94 my $cur = GetCurrency();
95 $template->param( symbol => $cur->{symbol},
96                   currency => $cur->{currency}
97                );
98 my $cur_format = C4::Context->preference("CurrencyFormat");
99 my $num;
100
101 if ( $cur_format eq 'US' ) {
102     $num = new Number::Format(
103         'int_curr_symbol'   => '',
104         'mon_thousands_sep' => ',',
105         'mon_decimal_point' => '.'
106     );
107 } elsif ( $cur_format eq 'FR' ) {
108     $num = new Number::Format(
109         'decimal_fill'      => '2',
110         'decimal_point'     => ',',
111         'int_curr_symbol'   => '',
112         'mon_thousands_sep' => ' ',
113         'thousands_sep'     => ' ',
114         'mon_decimal_point' => ','
115     );
116 }
117
118
119 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
120 if ( $op eq 'add_form' ) {
121     ## add or modify a budget period (preparation)
122     ## get information about the budget period that must be modified
123
124     if ($budget_period_id) {    # MOD
125                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
126         # get dropboxes
127
128         my $editnum = new Number::Format(
129             'int_curr_symbol'   => '',
130             'thousands_sep'     => '',
131             'mon_thousands_sep' => '',
132             'mon_decimal_point' => '.'
133         );
134
135         $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
136         $template->param(
137                         %$budgetperiod_hash
138         );
139     } # IF-MOD
140 }
141
142 elsif ( $op eq 'add_validate' ) {
143 ## add or modify a budget period (confirimation)
144
145     ## update budget period data
146         if ( $budget_period_id ne '' ) {
147                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
148                 my $status=ModBudgetPeriod($budget_period_hashref);
149         } 
150         else {    # ELSE ITS AN ADD
151                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
152         }
153         $op='else';
154 }
155
156 #--------------------------------------------------
157 elsif ( $op eq 'delete_confirm' ) {
158 ## delete a budget period (preparation)
159     my $dbh = C4::Context->dbh;
160     ## $total = number of records linked to the record that must be deleted
161     my $total = 0;
162     my $data = GetBudgetPeriod( $budget_period_id);
163
164         $$data{'budget_period_total'}=$num->format_price(  $data->{'budget_period_total'});
165     $template->param(
166                 %$data
167     );
168 }
169
170 elsif ( $op eq 'delete_confirmed' ) {
171 ## delete the budget period record
172
173     my $data = GetBudgetPeriod( $budget_period_id);
174     DelBudgetPeriod($budget_period_id);
175         $op='else';
176 }
177
178 # display the form for duplicating
179 elsif ( $op eq 'duplicate_form'){
180     $template->param(
181         'duplicate_form' => '1',
182         'budget_period_id' => $budget_period_id,
183     );
184 }
185
186 # handle the actual duplication
187 elsif ( $op eq 'duplicate_budget' ){
188     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
189
190     my $data = GetBudgetPeriod( $budget_period_id);
191     $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate};
192     $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate};
193     delete $data->{'budget_period_id'};
194     my $new_budget_period_id = AddBudgetPeriod($data);
195
196     my $tree = GetBudgetHierarchy( $budget_period_id );
197
198     # hash mapping old ids to new
199     my %old_new;
200     # hash mapping old parent ids to list of new children ids
201     # only store a child here if the parents old id isnt in the old_new map
202     # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
203     my %parent_children;
204
205     for my $entry( @$tree ){
206         die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
207         my $orphan = 0; # set to 1 if we need to make an entry in parent_children
208         my $old_id = delete $entry->{'budget_id'};
209         my $parent_id = delete $entry->{'budget_parent_id'};
210         $entry->{'budget_period_id'} = $new_budget_period_id;
211
212         if( !defined $parent_id ){
213         } elsif( defined $parent_id && $parent_id eq '' ){
214         } elsif( defined $old_new{$parent_id} ){
215             # set parent id now
216             $entry->{'budget_parent_id'} = $old_new{$parent_id};
217         } else {
218             # make an entry in parent_children
219             $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
220             $orphan = 1;
221         }
222
223         # get only the columns of aqbudgets
224         my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
225         my $new_entry = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $$entry{$_} )  : () } keys(%$entry) };
226         # write it to db
227         my $new_id = AddBudget($new_entry);
228         $old_new{$old_id} = $new_id;
229         push @{$parent_children{$parent_id}}, $new_id if $orphan;
230
231         # deal with any children
232         if( defined $parent_children{$old_id} ){
233             # tell my children my new id
234             for my $child ( @{$parent_children{$old_id}} ){
235                 ModBudget( { 'budget_id' => $child, 'budget_parent_id' => $new_id } );
236             }
237             delete $parent_children{$old_id};
238         }
239     }
240
241     # display the list of budgets
242     $op = 'else';
243 }
244
245 # DEFAULT - DISPLAY AQPERIODS TABLE
246 # -------------------------------------------------------------------
247 # display the list of budget periods
248
249 my $activepage = $input->param('apage') || 1;
250 my $inactivepage = $input->param('ipage') || 1;
251 # Get active budget periods
252 my $results = GetBudgetPeriods(
253     { budget_period_active => 1 },
254     { -asc => 'budget_period_description' },
255 );
256 my $first = ( $activepage - 1 ) * $activepagesize;
257 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
258 my @period_active_loop;
259
260 foreach my $result ( @{$results}[ $first .. $last ] ) {
261     my $budgetperiod = $result;
262     $budgetperiod->{'budget_period_total'}     = $num->format_price( $budgetperiod->{'budget_period_total'} );
263     $budgetperiod->{budget_active} = 1;
264     push( @period_active_loop, $budgetperiod );
265 }
266 my $url = "aqbudgetperiods.pl";
267 $url .=  "?ipage=$inactivepage" if($inactivepage != 1);
268 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
269
270 # Get inactive budget periods
271 $results = GetBudgetPeriods(
272     { budget_period_active => 0 },
273     { -desc => 'budget_period_enddate' },
274 );
275
276 $first = ( $inactivepage - 1 ) * $inactivepagesize;
277 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
278 my @period_inactive_loop;
279 foreach my $result ( @{$results}[ $first .. $last ] ) {
280     my $budgetperiod = $result;
281     $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
282     $budgetperiod->{budget_active} = 1;
283     push( @period_inactive_loop, $budgetperiod );
284 }
285 $url = "aqbudgetperiods.pl?tab=2";
286 $url .= "&apage=$activepage" if($activepage != 1);
287 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
288
289 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
290 $template->param(
291     period_active_loop      => \@period_active_loop,
292     period_inactive_loop    => \@period_inactive_loop,
293     active_pagination_bar   => $active_pagination_bar,
294     inactive_pagination_bar => $inactive_pagination_bar,
295     tab                     => $tab,
296 );
297
298 $template->param($op=>1);
299 output_html_with_http_headers $input, $cookie, $template->output;