Bug 15548 [QA Followup] - Catch a couple new Koha::Borrower uses
[koha.git] / admin / currency.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
5 # Copyright Koha Development Team
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27
28 use Koha::Acquisition::Currency;
29 use Koha::Acquisition::Currencies;
30
31 my $input         = CGI->new;
32 my $searchfield   = $input->param('searchfield') || $input->param('description') || q{};
33 my $currency_code = $input->param('currency_code');
34 my $op            = $input->param('op') || 'list';
35 my @messages;
36
37 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {   template_name   => 'admin/currency.tt',
39         query           => $input,
40         type            => 'intranet',
41         authnotrequired => 0,
42         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
43     }
44 );
45
46 if ( $op eq 'add_form' ) {
47     my $currency;
48     if ($currency_code) {
49         $currency = Koha::Acquisition::Currencies->find($currency_code);
50     }
51
52     $template->param( currency => $currency, );
53 } elsif ( $op eq 'add_validate' ) {
54     my $currency_code = $input->param('currency_code');
55     my $symbol        = $input->param('symbol');
56     my $isocode       = $input->param('isocode');
57     my $rate          = $input->param('rate');
58     my $active        = $input->param('active');
59     my $is_a_modif    = $input->param('is_a_modif');
60
61     if ($is_a_modif) {
62         my $currency = Koha::Acquisition::Currencies->find($currency_code);
63         $currency->symbol($symbol);
64         $currency->isocode($isocode);
65         $currency->rate($rate);
66         $currency->active($active);
67         eval { $currency->store; };
68         if ($@) {
69             push @messages, { type => 'error', code => 'error_on_update' };
70         } else {
71             push @messages, { type => 'message', code => 'success_on_update' };
72         }
73     } else {
74         my $currency = Koha::Acquisition::Currency->new(
75             {   currency => $currency_code,
76                 symbol   => $symbol,
77                 isocode  => $isocode,
78                 rate     => $rate,
79                 active   => $active,
80             }
81         );
82         eval { $currency->store; };
83         if ($@) {
84             push @messages, { type => 'error', code => 'error_on_insert' };
85         } else {
86             push @messages, { type => 'message', code => 'success_on_insert' };
87         }
88     }
89     $searchfield = q||;
90     $op          = 'list';
91 } elsif ( $op eq 'delete_confirm' ) {
92     my $currency = Koha::Acquisition::Currencies->find($currency_code);
93
94     # TODO rewrite the following when Koha::Acquisition::Orders will use Koha::Objects
95     my $schema = Koha::Database->schema;
96     my $nb_of_orders = $schema->resultset('Aqorder')->search( { currency => $currency->currency } )->count;
97     $template->param(
98         currency     => $currency,
99         nb_of_orders => $nb_of_orders,
100     );
101 } elsif ( $op eq 'delete' ) {
102     my $currency = Koha::Acquisition::Currencies->find($currency_code);
103     my $deleted = eval { $currency->delete; };
104
105     if ( $@ or not $deleted ) {
106         push @messages, { type => 'error', code => 'error_on_delete' };
107     } else {
108         push @messages, { type => 'message', code => 'success_on_delete' };
109     }
110     $op = 'list';
111 }
112
113 if ( $op eq 'list' ) {
114     $searchfield =~ s/\,//g;
115     my $currencies = Koha::Acquisition::Currencies->search( { currency => { -like => "$searchfield%" } } );
116
117     my $no_active_currency = not Koha::Acquisition::Currencies->search( { active => 1 } )->count;
118     $template->param(
119         currencies         => $currencies,
120         no_active_currency => $no_active_currency,
121     );
122 }
123
124 $template->param(
125     searchfield => $searchfield,
126     messages    => \@messages,
127     op          => $op,
128 );
129
130 output_html_with_http_headers $input, $cookie, $template->output;