Fix bug on opac-detail.pl with switch statement
[koha.git] / admin / currency.pl
blob1e3f16082b92d0d7e69f9248794c9c9734193f4e
1 #!/usr/bin/perl
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 # - the default screen is build (with all records, or filtered datas).
11 # - the user can clic on add, modify or delete record.
12 # if $op=add_form
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
15 # if $op=add_validate
16 # - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 # - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 # - we delete the record having primkey=$primkey
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License along
37 # with Koha; if not, write to the Free Software Foundation, Inc.,
38 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
40 use strict;
41 use warnings;
42 use CGI;
43 use C4::Context;
44 use C4::Auth;
45 use C4::Dates qw(format_date);
46 use C4::Output;
47 use C4::Budgets qw/GetCurrency GetCurrencies/;
49 my $input = CGI->new;
50 my $searchfield = $input->param('searchfield') || $input->param('description') || q{};
51 my $offset = $input->param('offset') || 0;
52 my $op = $input->param('op') || q{};
53 my $script_name = '/cgi-bin/koha/admin/currency.pl';
54 my $pagesize = 20;
56 my ($template, $loggedinuser, $cookie) = get_template_and_user({
57 template_name => 'admin/currency.tmpl',
58 query => $input,
59 type => 'intranet',
60 flagsrequired => {parameters => 1},
61 authnotrequired => 0,
62 });
64 $searchfield=~ s/\,//g;
67 $template->param(searchfield => $searchfield,
68 script_name => $script_name);
70 my $dbh = C4::Context->dbh;
72 if ( $op eq 'add_form' ) {
73 add_form($searchfield);
74 } elsif ( $op eq 'save' ) {
75 add_validate();
76 print $input->redirect('/cgi-bin/koha/admin/currency.pl');
77 } elsif ( $op eq 'delete_confirm' ) {
78 delete_confirm($searchfield);
79 } elsif ( $op eq 'delete_confirmed' ) {
80 delete_currency($searchfield);
81 } else {
82 default_path($searchfield);
85 output_html_with_http_headers $input, $cookie, $template->output;
87 sub default_path {
88 my $searchfield = shift;
89 $template->param( else => 1 );
91 my @currencies = GetCurrencies();
92 if ($searchfield) {
93 @currencies = grep { $_->{currency} =~ m/^$searchfield/o } @currencies;
95 my $end_of_page = $offset + $pagesize;
96 if ( $end_of_page > @currencies ) {
97 $end_of_page = @currencies;
98 } else {
99 $template->param(
100 ltcount => 1,
101 nextpage => $end_of_page
104 $end_of_page--;
105 my @display_curr = @currencies[ $offset .. $end_of_page ];
106 for my $c (@display_curr) {
107 $c->{timestamp} = format_date( $c->{timestamp} );
109 my $activecurrency = GetCurrency();
111 $template->param(
112 loop => \@display_curr,
113 activecurrency => defined $activecurrency,
116 if ( $offset > 0 ) {
117 $template->param(
118 offsetgtzero => 1,
119 prevpage => $offset - $pagesize
122 return;
125 sub delete_currency {
126 my $curr = shift;
128 # TODO This should be a method of Currency
129 # also what about any orders using this currency
130 $template->param( delete_confirmed => 1 );
131 $dbh->do( 'delete from currency where currency=?', {}, $curr );
132 return;
135 sub delete_confirm {
136 my $curr = shift;
138 $template->param( delete_confirm => 1 );
139 my $total_row = $dbh->selectrow_hashref(
140 'select count(*) as total from aqbooksellers where currency=?',
141 {}, $curr );
143 my $curr_ref = $dbh->selectrow_hashref(
144 'select currency,rate from currency where currency=?',
145 {}, $curr );
147 if ( $total_row->{total} ) {
148 $template->param( totalgtzero => 1 );
151 $template->param(
152 rate => $curr_ref->{rate},
153 total => $total_row->{total}
156 return;
159 sub add_form {
160 my $curr = shift;
162 $template->param( add_form => 1 );
164 #---- if primkey exists, it's a modify action, so read values to modify...
165 if ($curr) {
166 my $curr_rec =
167 $dbh->selectrow_hashref( 'select * from currency where currency=?',
168 {}, $curr );
169 for ( keys %{$curr_rec} ) {
170 $template->param( $_ => $curr_rec->{$_} );
173 my $date = $template->param('timestamp');
174 if ($date) {
175 $template->param( 'timestamp' => format_date($date) );
178 return;
181 sub add_validate {
182 $template->param( add_validate => 1 );
184 my $rec = {
185 rate => $input->param('rate'),
186 symbol => $input->param('symbol') || q{},
187 active => $input->param('active') || 0,
188 currency => $input->param('currency'),
191 if ( $rec->{active} == 1 ) {
192 $dbh->do('UPDATE currency SET active = 0');
195 my ($row_count) = $dbh->selectrow_array(
196 'select count(*) as count from currency where currency = ?',
197 {}, $input->param('currency') );
198 if ($row_count) {
199 $dbh->do(
200 q|UPDATE currency SET rate = ?, symbol = ?, active = ? WHERE currency = ? |,
202 $rec->{rate},
203 $rec->{symbol},
204 $rec->{active},
205 $rec->{currency}
207 } else {
208 $dbh->do(
209 q|INSERT INTO currency (currency, rate, symbol, active) VALUES (?,?,?,?) |,
211 $rec->{currency},
212 $rec->{rate},
213 $rec->{symbol},
214 $rec->{active}
218 return;