Bug 11650: multiplicated authorities after link_bibs_to_authorities.pl
[koha.git] / admin / aqbudgetperiods.pl
blobd5657460e1bcec1cff81b726e67282eb37e6f44b
1 #!/usr/bin/perl
3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
4 # SAN Ouest Provence
6 # This file is part of Koha.
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.
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.
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.
21 =head1 admin/aqbudgetperiods.pl
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)
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
45 =cut
47 use Modern::Perl;
49 use Number::Format qw(format_price);
50 use CGI;
51 use List::Util qw/min/;
52 use Koha::DateUtils;
53 use Koha::Database;
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;
62 my $dbh = C4::Context->dbh;
64 my $input = new CGI;
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');
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) =~ /$_/ ? ( $_ => $input->param($_) ) : () } keys( %{$input->Vars()} ) } ;
75 $budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
76 $budget_period_hashref->{budget_period_enddate} = dt_from_string( $input->param('budget_period_enddate') );
78 my $activepagesize = 20;
79 my $inactivepagesize = 20;
80 $searchfield =~ s/\,//g;
82 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
83 { template_name => "admin/aqbudgetperiods.tmpl",
84 query => $input,
85 type => "intranet",
86 authnotrequired => 0,
87 flagsrequired => { acquisition => 'period_manage' },
88 debug => 1,
93 my $cur = GetCurrency();
94 $template->param( symbol => $cur->{symbol},
95 currency => $cur->{currency}
97 my $cur_format = C4::Context->preference("CurrencyFormat");
98 my $num;
100 if ( $cur_format eq 'US' ) {
101 $num = new Number::Format(
102 'int_curr_symbol' => '',
103 'mon_thousands_sep' => ',',
104 'mon_decimal_point' => '.'
106 } elsif ( $cur_format eq 'FR' ) {
107 $num = new Number::Format(
108 'decimal_fill' => '2',
109 'decimal_point' => ',',
110 'int_curr_symbol' => '',
111 'mon_thousands_sep' => ' ',
112 'thousands_sep' => ' ',
113 'mon_decimal_point' => ','
118 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
119 if ( $op eq 'add_form' ) {
120 ## add or modify a budget period (preparation)
121 ## get information about the budget period that must be modified
123 if ($budget_period_id) { # MOD
124 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
125 # get dropboxes
127 my $editnum = new Number::Format(
128 'int_curr_symbol' => '',
129 'thousands_sep' => '',
130 'mon_thousands_sep' => '',
131 'mon_decimal_point' => '.'
134 $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
135 $template->param(
136 %$budgetperiod_hash
138 } # IF-MOD
141 elsif ( $op eq 'add_validate' ) {
142 ## add or modify a budget period (confirmation)
144 ## update budget period data
145 if ( $budget_period_id ne '' ) {
146 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
147 my $status=ModBudgetPeriod($budget_period_hashref);
149 else { # ELSE ITS AN ADD
150 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
152 $op='else';
155 #--------------------------------------------------
156 elsif ( $op eq 'delete_confirm' ) {
157 ## delete a budget period (preparation)
158 my $dbh = C4::Context->dbh;
159 ## $total = number of records linked to the record that must be deleted
160 my $total = 0;
161 my $data = GetBudgetPeriod( $budget_period_id);
163 $$data{'budget_period_total'}=$num->format_price( $data->{'budget_period_total'});
164 $template->param(
165 %$data
169 elsif ( $op eq 'delete_confirmed' ) {
170 ## delete the budget period record
172 my $data = GetBudgetPeriod( $budget_period_id);
173 DelBudgetPeriod($budget_period_id);
174 $op='else';
177 # display the form for duplicating
178 elsif ( $op eq 'duplicate_form'){
179 $template->param(
180 'duplicate_form' => '1',
181 'budget_period_id' => $budget_period_id,
185 # handle the actual duplication
186 elsif ( $op eq 'duplicate_budget' ){
187 die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
189 my $data = GetBudgetPeriod( $budget_period_id);
190 $data->{'budget_period_startdate'} = $budget_period_hashref->{budget_period_startdate};
191 $data->{'budget_period_enddate'} = $budget_period_hashref->{budget_period_enddate};
192 delete $data->{'budget_period_id'};
193 my $new_budget_period_id = AddBudgetPeriod($data);
195 my $tree = GetBudgetHierarchy( $budget_period_id );
197 # hash mapping old ids to new
198 my %old_new;
199 # hash mapping old parent ids to list of new children ids
200 # only store a child here if the parents old id isnt in the old_new map
201 # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
202 my %parent_children;
204 for my $entry( @$tree ){
205 die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
206 my $orphan = 0; # set to 1 if we need to make an entry in parent_children
207 my $old_id = delete $entry->{'budget_id'};
208 my $parent_id = delete $entry->{'budget_parent_id'};
209 $entry->{'budget_period_id'} = $new_budget_period_id;
211 if( !defined $parent_id ){
212 } elsif( defined $parent_id && $parent_id eq '' ){
213 } elsif( defined $old_new{$parent_id} ){
214 # set parent id now
215 $entry->{'budget_parent_id'} = $old_new{$parent_id};
216 } else {
217 # make an entry in parent_children
218 $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
219 $orphan = 1;
222 # get only the columns of aqbudgets
223 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
224 my $new_entry = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $entry->{$_} ) : () } keys(%$entry) };
225 # write it to db
226 my $new_id = AddBudget($new_entry);
227 $old_new{$old_id} = $new_id;
228 push @{$parent_children{$parent_id}}, $new_id if $orphan;
230 # deal with any children
231 if( defined $parent_children{$old_id} ){
232 # tell my children my new id
233 for my $child ( @{$parent_children{$old_id}} ){
234 ModBudget( { 'budget_id' => $child, 'budget_parent_id' => $new_id } );
236 delete $parent_children{$old_id};
240 # display the list of budgets
241 $op = 'else';
244 # DEFAULT - DISPLAY AQPERIODS TABLE
245 # -------------------------------------------------------------------
246 # display the list of budget periods
248 my $activepage = $input->param('apage') || 1;
249 my $inactivepage = $input->param('ipage') || 1;
250 # Get active budget periods
251 my $results = GetBudgetPeriods(
252 { budget_period_active => 1 },
253 { -asc => 'budget_period_description' },
255 my $first = ( $activepage - 1 ) * $activepagesize;
256 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
257 my @period_active_loop;
259 foreach my $result ( @{$results}[ $first .. $last ] ) {
260 my $budgetperiod = $result;
261 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
262 $budgetperiod->{budget_active} = 1;
263 push( @period_active_loop, $budgetperiod );
265 my $url = "aqbudgetperiods.pl";
266 $url .= "?ipage=$inactivepage" if($inactivepage != 1);
267 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
269 # Get inactive budget periods
270 $results = GetBudgetPeriods(
271 { budget_period_active => 0 },
272 { -desc => 'budget_period_enddate' },
275 $first = ( $inactivepage - 1 ) * $inactivepagesize;
276 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
277 my @period_inactive_loop;
278 foreach my $result ( @{$results}[ $first .. $last ] ) {
279 my $budgetperiod = $result;
280 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
281 $budgetperiod->{budget_active} = 1;
282 push( @period_inactive_loop, $budgetperiod );
284 $url = "aqbudgetperiods.pl?tab=2";
285 $url .= "&apage=$activepage" if($activepage != 1);
286 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
288 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
289 $template->param(
290 period_active_loop => \@period_active_loop,
291 period_inactive_loop => \@period_inactive_loop,
292 active_pagination_bar => $active_pagination_bar,
293 inactive_pagination_bar => $inactive_pagination_bar,
294 tab => $tab,
297 $template->param($op=>1);
298 output_html_with_http_headers $input, $cookie, $template->output;