Bug 9259: Use is instead of is_deeply
[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::Bookseller;
29 use Koha::Acquisition::Currency;
30 use Koha::Acquisition::Currencies;
31
32 my $input         = CGI->new;
33 my $searchfield   = $input->param('searchfield') || $input->param('description') || q{};
34 my $currency_code = $input->param('currency_code');
35 my $op            = $input->param('op') || 'list';
36 my @messages;
37
38 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {   template_name   => 'admin/currency.tt',
40         query           => $input,
41         type            => 'intranet',
42         authnotrequired => 0,
43         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
44     }
45 );
46
47 if ( $op eq 'add_form' ) {
48     my $currency;
49     if ($currency_code) {
50         $currency = Koha::Acquisition::Currencies->find($currency_code);
51     }
52
53     $template->param( currency => $currency, );
54 } elsif ( $op eq 'add_validate' ) {
55     my $currency_code = $input->param('currency_code');
56     my $symbol        = $input->param('symbol');
57     my $isocode       = $input->param('isocode');
58     my $rate          = $input->param('rate');
59     my $active        = $input->param('active');
60     my $is_a_modif    = $input->param('is_a_modif');
61
62     if ($is_a_modif) {
63         my $currency = Koha::Acquisition::Currencies->find($currency_code);
64         $currency->symbol($symbol);
65         $currency->isocode($isocode);
66         $currency->rate($rate);
67         $currency->active($active);
68         eval { $currency->store; };
69         if ($@) {
70             push @messages, { type => 'error', code => 'error_on_update' };
71         } else {
72             push @messages, { type => 'message', code => 'success_on_update' };
73         }
74     } else {
75         my $currency = Koha::Acquisition::Currency->new(
76             {   currency => $currency_code,
77                 symbol   => $symbol,
78                 isocode  => $isocode,
79                 rate     => $rate,
80                 active   => $active,
81             }
82         );
83         eval { $currency->store; };
84         if ($@) {
85             push @messages, { type => 'error', code => 'error_on_insert' };
86         } else {
87             push @messages, { type => 'message', code => 'success_on_insert' };
88         }
89     }
90     $searchfield = q||;
91     $op          = 'list';
92 } elsif ( $op eq 'delete_confirm' ) {
93     my $currency = Koha::Acquisition::Currencies->find($currency_code);
94
95     # TODO rewrite the following when Koha::Acquisition::Orders will use Koha::Objects
96     my $schema = Koha::Database->schema;
97     my $nb_of_orders = $schema->resultset('Aqorder')->search( { currency => $currency->currency } )->count;
98     my $nb_of_vendors = Koha::Acquisition::Bookseller->search( { -or => { listprice => $currency->currency, invoiceprice => $currency->currency } });
99     $template->param(
100         currency     => $currency,
101         nb_of_orders => $nb_of_orders,
102         nb_of_vendors => $nb_of_vendors,
103     );
104 } elsif ( $op eq 'delete_confirmed' ) {
105     my $currency = Koha::Acquisition::Currencies->find($currency_code);
106     my $deleted = eval { $currency->delete; };
107
108     if ( $@ or not $deleted ) {
109         push @messages, { type => 'error', code => 'error_on_delete' };
110     } else {
111         push @messages, { type => 'message', code => 'success_on_delete' };
112     }
113     $op = 'list';
114 }
115
116 if ( $op eq 'list' ) {
117     $searchfield =~ s/\,//g;
118     my $currencies = Koha::Acquisition::Currencies->search( { currency => { -like => "$searchfield%" } } );
119
120     my $no_active_currency = not Koha::Acquisition::Currencies->search( { active => 1 } )->count;
121     $template->param(
122         currencies         => $currencies,
123         no_active_currency => $no_active_currency,
124     );
125 }
126
127 $template->param(
128     searchfield => $searchfield,
129     messages    => \@messages,
130     op          => $op,
131 );
132
133 output_html_with_http_headers $input, $cookie, $template->output;