3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
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
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 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)
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.
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
35 - the user has just send datas, so we create/modify the record
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
45 use Number::Format qw(format_price);
47 use List::Util qw/min/;
48 use C4::Dates qw/format_date format_date_in_iso/;
57 my $dbh = C4::Context->dbh;
61 my $searchfield = $input->param('searchfield');
62 my $budget_period_id = $input->param('budget_period_id');
63 my $budget_period_active = $input->param('budget_period_active');
64 my $budget_period_locked = $input->param('budget_period_locked');
65 my $op = $input->param('op');
67 #my $sort1_authcat = $input->param('sort1_authcat');
68 #my $sort2_authcat = $input->param('sort2_authcat');
71 $searchfield =~ s/\,//g;
73 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
74 { template_name => "admin/aqbudgetperiods.tmpl",
78 flagsrequired => { acquisition => 'period_manage' },
83 my $script_name = "/cgi-bin/koha/admin/aqbudgetperiods.pl"; # ???
85 my ( $count, $results ) = GetBudgetPeriods();
87 $template->param( period_button_only => 1 ) if ($count == 0) ;
89 my $cur = GetCurrency();
90 $template->param( cur => $cur->{symbol} );
91 my $cur_format = C4::Context->preference("CurrencyFormat");
94 if ( $cur_format eq 'US' ) {
95 $num = new Number::Format(
96 'int_curr_symbol' => '',
97 'mon_thousands_sep' => ',',
98 'mon_decimal_point' => '.'
100 } elsif ( $cur_format eq 'FR' ) {
101 $num = new Number::Format(
102 'decimal_fill' => '2',
103 'decimal_point' => ',',
104 'int_curr_symbol' => '',
105 'mon_thousands_sep' => ' ',
106 'thousands_sep' => ' ',
107 'mon_decimal_point' => ','
111 if ($op) { $template->param( $op => 1 ); }
112 else { $template->param( 'else' => 1 ); }
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
119 # my ( $default, $sort1_authcat_dropbox, $sort1_default, $sort2_default );
120 # my ( $default, t );
122 if ($budget_period_id) { # MOD
124 my $dbh = C4::Context->dbh;
125 my $sth = $dbh->prepare(qq|
126 SELECT * FROM aqbudgetperiods
127 WHERE budget_period_id=? | );
128 $sth->execute($budget_period_id);
129 $data = $sth->fetchrow_hashref;
134 budget_period_id => $budget_period_id,
135 budget_period_startdate => format_date( $data->{'budget_period_startdate'} ),
136 budget_period_enddate => format_date( $data->{'budget_period_enddate'} ),
137 budget_period_description => $data->{'budget_period_description'},
138 budget_period_total => sprintf ("%.2f", $data->{'budget_period_total'} ),
139 budget_period_active => $data->{'budget_period_active'},
140 budget_period_locked => $data->{'budget_period_locked'},
143 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
146 elsif ( $op eq 'add_validate' ) {
147 ## add or modify a budget period (confirmation)
149 ## update budget period data
150 if ( $budget_period_id ne '' ) {
152 UPDATE aqbudgetperiods
153 SET budget_period_startdate = ?
154 , budget_period_enddate = ?
155 , budget_period_description = ?
156 , budget_period_total = ?
157 , budget_period_locked = ?
158 WHERE budget_period_id = ?
161 my $sth = $dbh->prepare($query);
163 $input->param('budget_period_startdate') ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef,
164 $input->param('budget_period_enddate') ? format_date_in_iso( $input->param('budget_period_enddate') ) : undef,
165 $input->param('budget_period_description') ? $input->param('budget_period_description') : undef,
166 $input->param('budget_period_total') ? $input->param('budget_period_total') : undef,
167 $input->param('budget_period_locked') ? $input->param('budget_period_locked') : undef,
168 $input->param('budget_period_id'),
171 # IF PASSED ACTIVE - THEN SET IT IN DB TOO.
172 set_active($budget_period_id) if ( $budget_period_active == 1 );
174 } else { # ELSE ITS AN ADD
176 INSERT INTO aqbudgetperiods (
178 , budget_period_startdate
179 , budget_period_enddate
180 , budget_period_total
181 , budget_period_description
182 , budget_period_locked )
183 VALUES (?,?,?,?,?,? );
185 my $sth = $dbh->prepare($query);
188 $input->param('budget_period_startdate') ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef,
189 $input->param('budget_period_enddate') ? format_date_in_iso( $input->param('budget_period_enddate') ) : undef,
190 $input->param('budget_period_total') ? $input->param('budget_period_total') : undef,
191 $input->param('budget_period_description') ? $input->param('budget_period_description') : undef,
192 $input->param('budget_period_locked') ? $input->param('budget_period_locked') : undef,
194 $budget_period_id = $dbh->last_insert_id( undef, undef, 'aqbudgetperiods', undef );
195 set_active($budget_period_id) if ( $budget_period_active == 1 );
198 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbudgetperiods.pl\"></html>"; #YUCK
199 # output_html_with_http_headers $input, $cookie, $template->output; # FIXME: THIS WOULD BE NICER THAN THE PREVIOUS PRINT
203 #--------------------------------------------------
204 elsif ( $op eq 'delete_confirm' ) {
205 ## delete a budget period (preparation)
206 my $dbh = C4::Context->dbh;
207 ## $total = number of records linked to the record that must be deleted
209 my $data = GetBudgetPeriod( $budget_period_id);
212 budget_period_id => $budget_period_id,
213 budget_period_startdate => format_date($data->{'budget_period_startdate'}),
214 budget_period_enddate => format_date($data->{'budget_period_enddate'}),
215 budget_period_total => $num->format_price( $data->{'budget_period_total'} )
217 # budget_period_active => $data->{'budget_period_active'},
218 # budget_period_description => $data->{'budget_period_description'},
219 # template => C4::Context->preference('template'), ## ??!?
223 elsif ( $op eq 'delete_confirmed' ) {
224 ## delete the budget period record
226 my $dbh = C4::Context->dbh;
227 my $budget_period_id = uc( $input->param('budget_period_id') );
228 my $sth = $dbh->prepare("DELETE FROM aqbudgetperiods WHERE budget_period_id=?");
229 $sth->execute($budget_period_id);
231 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbudgetperiods.pl\"></html>";
237 # DEFAULT - DISPLAY AQPERIODS TABLE
238 # -------------------------------------------------------------------
239 # display the list of budget periods
240 my ( $count, $results ) = GetBudgetPeriods();
241 my $page = $input->param('page') || 1;
242 my $first = ( $page - 1 ) * $pagesize;
244 # if we are on the last page, the number of the last word to display
245 # must not exceed the length of the results array
246 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
249 foreach my $result ( @{$results}[ $first .. $last ] ) {
250 my $budgetperiod = $result;
251 $budgetperiod->{'budget_period_startdate'} = format_date( $budgetperiod->{'budget_period_startdate'} );
252 $budgetperiod->{'budget_period_enddate'} = format_date( $budgetperiod->{'budget_period_enddate'} );
253 $budgetperiod->{'budget_period_total'} = $num->format_price( $budgetperiod->{'budget_period_total'} );
254 $budgetperiod->{toggle} = ( $toggle++ % 2 eq 0 ? 1 : 0 );
255 $budgetperiod->{budget_active} = 1;
256 push( @period_loop, $budgetperiod );
258 my $budget_period_dropbox = GetBudgetPeriodsDropbox();
261 budget_period_dropbox => $budget_period_dropbox,
262 period_loop => \@period_loop,
263 # pagination_bar => pagination_bar( $script_name,
264 # getnbpages( scalar @{$results},
265 # $pagesize ), $page, 'page' )
269 output_html_with_http_headers $input, $cookie, $template->output;
273 "UPDATE aqbudgetperiods
274 SET budget_period_active = 0 "
277 "UPDATE aqbudgetperiods
278 SET budget_period_active = 1
279 WHERE budget_period_id = $budget_period_id"