Increment version for 3.22.21
[koha.git] / admin / currency.pl
1 #!/usr/bin/perl
2
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)
6
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
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it
28 # under the terms of the GNU General Public License as published by
29 # the Free Software Foundation; either version 3 of the License, or
30 # (at your option) any later version.
31 #
32 # Koha is distributed in the hope that it will be useful, but
33 # WITHOUT ANY WARRANTY; without even the implied warranty of
34 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 # GNU General Public License for more details.
36 #
37 # You should have received a copy of the GNU General Public License
38 # along with Koha; if not, see <http://www.gnu.org/licenses>.
39
40 use strict;
41 use warnings;
42 use CGI qw ( -utf8 );
43 use C4::Context;
44 use C4::Auth;
45 use C4::Output;
46 use C4::Budgets qw/GetCurrency GetCurrencies/;
47
48 our $input = CGI->new;
49 my $searchfield = $input->param('searchfield') || $input->param('description') || q{};
50 our $offset      = $input->param('offset') || 0;
51 my $op          = $input->param('op')     || q{};
52 my $script_name = '/cgi-bin/koha/admin/currency.pl';
53 our $pagesize = 20;
54
55 our ($template, $loggedinuser, $cookie) = get_template_and_user({
56     template_name => 'admin/currency.tt',
57     query => $input,
58     type => 'intranet',
59     flagsrequired => {parameters => 'parameters_remaining_permissions'},
60     authnotrequired => 0,
61 });
62
63 $searchfield=~ s/\,//g;
64
65
66 $template->param(searchfield => $searchfield,
67         script_name => $script_name);
68
69 our $dbh = C4::Context->dbh;
70
71 if ( $op eq 'add_form' ) {
72     add_form($searchfield);
73 } elsif ( $op eq 'save' ) {
74     add_validate();
75     print $input->redirect('/cgi-bin/koha/admin/currency.pl');
76 } elsif ( $op eq 'delete_confirm' ) {
77     delete_confirm($searchfield);
78 } elsif ( $op eq 'delete_confirmed' ) {
79     delete_currency($searchfield);
80 } else {
81     default_path($searchfield);
82 }
83
84 output_html_with_http_headers $input, $cookie, $template->output;
85
86 sub default_path {
87     my $searchfield = shift;
88     $template->param( else => 1 );
89
90     my @currencies = GetCurrencies();
91     if ($searchfield) {
92         @currencies = grep { $_->{currency} =~ m/^$searchfield/o } @currencies;
93     }
94     my $end_of_page = $offset + $pagesize;
95     if ( $end_of_page > @currencies ) {
96         $end_of_page = @currencies;
97     } else {
98         $template->param(
99             ltcount  => 1,
100             nextpage => $end_of_page
101         );
102     }
103     $end_of_page--;
104     my @display_curr = @currencies[ $offset .. $end_of_page ];
105     my $activecurrency = GetCurrency();
106
107     $template->param(
108         loop           => \@display_curr,
109         activecurrency => defined $activecurrency,
110     );
111
112     if ( $offset > 0 ) {
113         $template->param(
114             offsetgtzero => 1,
115             prevpage     => $offset - $pagesize
116         );
117     }
118     return;
119 }
120
121 sub delete_currency {
122     my $curr = shift;
123
124     # TODO This should be a method of Currency
125     # also what about any orders using this currency
126     $template->param( delete_confirmed => 1 );
127     $dbh->do( 'delete from currency where currency=?', {}, $curr );
128     return;
129 }
130
131 sub delete_confirm {
132     my $curr = shift;
133
134     $template->param( delete_confirm => 1 );
135     my ($nb_of_vendors) = $dbh->selectrow_array(q{
136         select count(*) from aqbooksellers
137         where listprice = ? or invoiceprice = ?
138     }, {}, $curr, $curr);
139     my ($nb_of_orders) = $dbh->selectrow_array(q{
140         select count(*) from aqorders
141         where currency = ?
142     }, {}, $curr);
143
144     my $curr_ref = $dbh->selectrow_hashref(
145         'select currency,rate from currency where currency=?',
146         {}, $curr );
147
148     $template->param(
149         rate  => $curr_ref->{rate},
150         nb_of_orders => $nb_of_orders,
151         nb_of_vendors => $nb_of_vendors,
152     );
153
154     return;
155 }
156
157 sub add_form {
158     my $curr = shift;
159
160     $template->param( add_form => 1 );
161
162     #---- if primkey exists, it's a modify action, so read values to modify...
163     my $date;
164     if ($curr) {
165         my $curr_rec =
166           $dbh->selectrow_hashref( 'select * from currency where currency=?',
167             {}, $curr );
168         for ( keys %{$curr_rec} ) {
169             if($_ eq "timestamp"){ $date = $curr_rec->{$_}; }
170             $template->param( $_ => $curr_rec->{$_} );
171         }
172     }
173
174     return;
175 }
176
177 sub add_validate {
178     $template->param( add_validate => 1 );
179
180     my $rec = {
181         rate     => $input->param('rate'),
182         symbol   => $input->param('symbol') || q{},
183         isocode  => $input->param('isocode') || q{},
184         active   => $input->param('active') || 0,
185         currency => $input->param('currency'),
186     };
187
188     if ( $rec->{active} == 1 ) {
189         $dbh->do('UPDATE currency SET active = 0');
190     }
191
192     my ($row_count) = $dbh->selectrow_array(
193         'select count(*) as count from currency where currency = ?',
194         {}, $input->param('currency') );
195     if ($row_count) {
196         $dbh->do(
197 q|UPDATE currency SET rate = ?, symbol = ?, isocode = ?, active = ? WHERE currency = ? |,
198             {},
199             $rec->{rate},
200             $rec->{symbol},
201             $rec->{isocode},
202             $rec->{active},
203             $rec->{currency}
204         );
205     } else {
206         $dbh->do(
207 q|INSERT INTO currency (currency, rate, symbol, isocode, active) VALUES (?,?,?,?,?) |,
208             {},
209             $rec->{currency},
210             $rec->{rate},
211             $rec->{symbol},
212             $rec->{isocode},
213             $rec->{active}
214         );
215
216     }
217     return;
218 }