Merge remote-tracking branch 'origin/new/bug_5339'
[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 C4::Dates qw/format_date format_date_in_iso/;
54 use C4::Koha;
55 use C4::Context;
56 use C4::Auth;
57 use C4::Output;
58 use C4::Acquisition;
59 use C4::Budgets;
60 use C4::Debug;
61 use C4::SQLHelper;
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
71 my $budget_period_hashref= $input->Vars;
72 #my $sort1_authcat = $input->param('sort1_authcat');
73 #my $sort2_authcat = $input->param('sort2_authcat');
74
75 my $activepagesize = 20;
76 my $inactivepagesize = 20;
77 $searchfield =~ s/\,//g;
78
79 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
80         {   template_name   => "admin/aqbudgetperiods.tmpl",
81                 query           => $input,
82                 type            => "intranet",
83                 authnotrequired => 0,
84                 flagsrequired   => { acquisition => 'period_manage' },
85                 debug           => 1,
86         }
87 );
88
89
90 my $cur = GetCurrency();
91 $template->param( symbol => $cur->{symbol},
92                   currency => $cur->{currency}
93                );
94 my $cur_format = C4::Context->preference("CurrencyFormat");
95 my $num;
96
97 if ( $cur_format eq 'US' ) {
98     $num = new Number::Format(
99         'int_curr_symbol'   => '',
100         'mon_thousands_sep' => ',',
101         'mon_decimal_point' => '.'
102     );
103 } elsif ( $cur_format eq 'FR' ) {
104     $num = new Number::Format(
105         'decimal_fill'      => '2',
106         'decimal_point'     => ',',
107         'int_curr_symbol'   => '',
108         'mon_thousands_sep' => ' ',
109         'thousands_sep'     => ' ',
110         'mon_decimal_point' => ','
111     );
112 }
113
114
115 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
116 if ( $op eq 'add_form' ) {
117     ## add or modify a budget period (preparation)
118     ## get information about the budget period that must be modified
119
120     if ($budget_period_id) {    # MOD
121                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
122         # get dropboxes
123
124         my $editnum = new Number::Format(
125             'int_curr_symbol'   => '',
126             'thousands_sep'     => '',
127             'mon_thousands_sep' => '',
128             'mon_decimal_point' => '.'
129         );
130
131         $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
132         $template->param(
133                         %$budgetperiod_hash
134         );
135     } # IF-MOD
136     $template->param( DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),);
137 }
138
139 elsif ( $op eq 'add_validate' ) {
140 ## add or modify a budget period (confirmation)
141
142         ## update budget period data
143         if ( $budget_period_id ne '' ) {
144                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
145                 my $status=ModBudgetPeriod($budget_period_hashref);
146         } 
147         else {    # ELSE ITS AN ADD
148                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
149         }
150         $op='else';
151 }
152
153 #--------------------------------------------------
154 elsif ( $op eq 'delete_confirm' ) {
155 ## delete a budget period (preparation)
156     my $dbh = C4::Context->dbh;
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
161         $$data{'budget_period_total'}=$num->format_price(  $data->{'budget_period_total'});
162     $template->param(
163                 %$data
164     );
165 }
166
167 elsif ( $op eq 'delete_confirmed' ) {
168 ## delete the budget period record
169
170     my $data = GetBudgetPeriod( $budget_period_id);
171     DelBudgetPeriod($budget_period_id);
172         $op='else';
173 }
174
175 # display the form for duplicating
176 elsif ( $op eq 'duplicate_form'){
177     $template->param(
178         DHTMLcalendar_dateformat        => C4::Dates->DHTMLcalendar(),
179         'duplicate_form' => '1',
180         'budget_period_id' => $budget_period_id,
181     );
182 }
183
184 # handle the actual duplication
185 elsif ( $op eq 'duplicate_budget' ){
186     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
187     my $startdate = $input->param('budget_period_startdate');
188     my $enddate = $input->param('budget_period_enddate');
189
190     my $data = GetBudgetPeriod( $budget_period_id);
191
192     $data->{'budget_period_startdate'} = $startdate;
193     $data->{'budget_period_enddate'} = $enddate;
194     delete $data->{'budget_period_id'};
195     my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data);
196
197     my $tree = GetBudgetHierarchy( $budget_period_id );
198
199     # hash mapping old ids to new
200     my %old_new;
201     # hash mapping old parent ids to list of new children ids
202     # only store a child here if the parents old id isnt in the old_new map
203     # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
204     my %parent_children;
205
206     for my $entry( @$tree ){
207         die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
208         my $orphan = 0; # set to 1 if we need to make an entry in parent_children
209         my $old_id = delete $entry->{'budget_id'};
210         my $parent_id = delete $entry->{'budget_parent_id'};
211         $entry->{'budget_period_id'} = $new_budget_period_id;
212
213         if( !defined $parent_id ){
214         } elsif( defined $parent_id && $parent_id eq '' ){
215         } elsif( defined $old_new{$parent_id} ){
216             # set parent id now
217             $entry->{'budget_parent_id'} = $old_new{$parent_id};
218         } else {
219             # make an entry in parent_children
220             $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
221             $orphan = 1;
222         }
223
224         # write it to db
225         my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry);
226         $old_new{$old_id} = $new_id;
227         push @{$parent_children{$parent_id}}, $new_id if $orphan;
228
229         # deal with any children
230         if( defined $parent_children{$old_id} ){
231             # tell my children my new id
232             for my $child ( @{$parent_children{$old_id}} ){
233                 C4::SQLHelper::UpdateInTable('aqcudgets', [ 'budget_id' => $child, 'budget_parent_id' => $new_id ]);
234             }
235             delete $parent_children{$old_id};
236         }
237     }
238
239     # display the list of budgets
240     $op = 'else';
241 }
242
243 # DEFAULT - DISPLAY AQPERIODS TABLE
244 # -------------------------------------------------------------------
245 # display the list of budget periods
246
247 my $activepage = $input->param('apage') || 1;
248 my $inactivepage = $input->param('ipage') || 1;
249 # Get active budget periods
250 my $results = GetBudgetPeriods(
251     {budget_period_active => 1},
252     [{budget_period_description => 0}]
253 );
254 my $first = ( $activepage - 1 ) * $activepagesize;
255 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
256 my @period_active_loop;
257
258 foreach my $result ( @{$results}[ $first .. $last ] ) {
259     my $budgetperiod = $result;
260     $budgetperiod->{'budget_period_total'}     = $num->format_price( $budgetperiod->{'budget_period_total'} );
261     $budgetperiod->{budget_active} = 1;
262     push( @period_active_loop, $budgetperiod );
263 }
264 my $url = "aqbudgetperiods.pl";
265 $url .=  "?ipage=$inactivepage" if($inactivepage != 1);
266 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
267
268 # Get inactive budget periods
269 $results = GetBudgetPeriods(
270     {budget_period_active => 0},
271     [{budget_period_enddate => 1}]
272 );
273
274 $first = ( $inactivepage - 1 ) * $inactivepagesize;
275 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
276 my @period_inactive_loop;
277 foreach my $result ( @{$results}[ $first .. $last ] ) {
278     my $budgetperiod = $result;
279     $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
280     $budgetperiod->{budget_active} = 1;
281     push( @period_inactive_loop, $budgetperiod );
282 }
283 $url = "aqbudgetperiods.pl?tab=2";
284 $url .= "&apage=$activepage" if($activepage != 1);
285 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
286
287 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
288 $template->param(
289     period_active_loop      => \@period_active_loop,
290     period_inactive_loop    => \@period_inactive_loop,
291     active_pagination_bar   => $active_pagination_bar,
292     inactive_pagination_bar => $inactive_pagination_bar,
293     tab                     => $tab,
294     dateformat              => C4::Context->preference('dateformat'),
295 );
296
297 $template->param($op=>1);
298 output_html_with_http_headers $input, $cookie, $template->output;