Bug 8717 - Wrong subscription notes are showing in catalogue detail view of serials...
[koha.git] / admin / aqbudgetperiods.pl
blob64b333a4850c4bf99417b8c36b0638a770dcc61b
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 ## 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;
63 my $dbh = C4::Context->dbh;
65 my $input = new CGI;
67 my $searchfield = $input->param('searchfield');
68 my $budget_period_id = $input->param('budget_period_id');
69 my $op = $input->param('op')||"else";
71 my $budget_period_hashref= $input->Vars;
72 #my $sort1_authcat = $input->param('sort1_authcat');
73 #my $sort2_authcat = $input->param('sort2_authcat');
75 my $pagesize = 20;
76 $searchfield =~ s/\,//g;
78 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
79 { template_name => "admin/aqbudgetperiods.tmpl",
80 query => $input,
81 type => "intranet",
82 authnotrequired => 0,
83 flagsrequired => { acquisition => 'period_manage' },
84 debug => 1,
89 my $cur = GetCurrency();
90 $template->param( symbol => $cur->{symbol},
91 currency => $cur->{currency}
93 my $cur_format = C4::Context->preference("CurrencyFormat");
94 my $num;
96 if ( $cur_format eq 'US' ) {
97 $num = new Number::Format(
98 'int_curr_symbol' => '',
99 'mon_thousands_sep' => ',',
100 'mon_decimal_point' => '.'
102 } elsif ( $cur_format eq 'FR' ) {
103 $num = new Number::Format(
104 'decimal_fill' => '2',
105 'decimal_point' => ',',
106 'int_curr_symbol' => '',
107 'mon_thousands_sep' => ' ',
108 'thousands_sep' => ' ',
109 'mon_decimal_point' => ','
114 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
115 if ( $op eq 'add_form' ) {
116 ## add or modify a budget period (preparation)
117 ## get information about the budget period that must be modified
120 if ($budget_period_id) { # MOD
121 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
122 # get dropboxes
123 FormatData($budgetperiod_hash);
125 my $editnum = new Number::Format(
126 'int_curr_symbol' => '',
127 'thousands_sep' => '',
128 'mon_thousands_sep' => '',
129 'mon_decimal_point' => '.'
132 $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
133 $template->param(
134 %$budgetperiod_hash
136 } # IF-MOD
137 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
140 elsif ( $op eq 'add_validate' ) {
141 ## add or modify a budget period (confirmation)
143 ## update budget period data
144 if ( $budget_period_id ne '' ) {
145 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
146 my $status=ModBudgetPeriod($budget_period_hashref);
148 else { # ELSE ITS AN ADD
149 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
151 $op='else';
154 #--------------------------------------------------
155 elsif ( $op eq 'delete_confirm' ) {
156 ## delete a budget period (preparation)
157 my $dbh = C4::Context->dbh;
158 ## $total = number of records linked to the record that must be deleted
159 my $total = 0;
160 my $data = GetBudgetPeriod( $budget_period_id);
162 FormatData($data);
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 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
181 'duplicate_form' => '1',
182 'budget_period_id' => $budget_period_id,
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 my $startdate = $input->param('budget_period_startdate');
190 my $enddate = $input->param('budget_period_enddate');
192 my $data = GetBudgetPeriod( $budget_period_id);
194 $data->{'budget_period_startdate'} = $startdate;
195 $data->{'budget_period_enddate'} = $enddate;
196 delete $data->{'budget_period_id'};
197 my $new_budget_period_id = C4::SQLHelper::InsertInTable('aqbudgetperiods', $data);
199 my $tree = GetBudgetHierarchy( $budget_period_id );
201 # hash mapping old ids to new
202 my %old_new;
203 # hash mapping old parent ids to list of new children ids
204 # only store a child here if the parents old id isnt in the old_new map
205 # when the parent is found, this map will be used, and then the entry removed and their id placed in old_new
206 my %parent_children;
208 for my $entry( @$tree ){
209 die "serious errors, parent period $budget_period_id doesnt match child ", $entry->{'budget_period_id'}, "\n" if( $entry->{'budget_period_id'} != $budget_period_id );
210 my $orphan = 0; # set to 1 if we need to make an entry in parent_children
211 my $old_id = delete $entry->{'budget_id'};
212 my $parent_id = delete $entry->{'budget_parent_id'};
213 $entry->{'budget_period_id'} = $new_budget_period_id;
215 if( !defined $parent_id ){
216 } elsif( defined $parent_id && $parent_id eq '' ){
217 } elsif( defined $old_new{$parent_id} ){
218 # set parent id now
219 $entry->{'budget_parent_id'} = $old_new{$parent_id};
220 } else {
221 # make an entry in parent_children
222 $parent_children{$parent_id} = [] unless defined $parent_children{$parent_id};
223 $orphan = 1;
226 # write it to db
227 my $new_id = C4::SQLHelper::InsertInTable('aqbudgets', $entry);
228 $old_new{$old_id} = $new_id;
229 push @{$parent_children{$parent_id}}, $new_id if $orphan;
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 C4::SQLHelper::UpdateInTable('aqcudgets', [ 'budget_id' => $child, 'budget_parent_id' => $new_id ]);
237 delete $parent_children{$old_id};
241 # display the list of budgets
242 $op = 'else';
245 # DEFAULT - DISPLAY AQPERIODS TABLE
246 # -------------------------------------------------------------------
247 # display the list of budget periods
248 my $results = GetBudgetPeriods();
249 $template->param( period_button_only => 1 ) unless (@$results) ;
250 my $page = $input->param('page') || 1;
251 my $first = ( $page - 1 ) * $pagesize;
252 # if we are on the last page, the number of the last word to display
253 # must not exceed the length of the results array
254 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
255 my $toggle = 0;
256 my @period_loop;
257 foreach my $result ( @{$results}[ $first .. $last ] ) {
258 my $budgetperiod = $result;
259 FormatData($budgetperiod);
260 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
261 $budgetperiod->{budget_active} = 1;
262 push( @period_loop, $budgetperiod );
265 $template->param(
266 period_loop => \@period_loop,
267 pagination_bar => pagination_bar("aqbudgetperiods.pl",getnbpages(scalar(@$results),$pagesize),$page),
270 $template->param($op=>1);
271 output_html_with_http_headers $input, $cookie, $template->output;