Bug 15128 - (QA Followup) Fix use of 'my' variable causing loss of data
[koha.git] / acqui / supplier.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2008-2009 BibLibre SARL
5 # Copyright 2010 PTFS Europe Ltd
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 =head1 NAME
23
24 supplier.pl
25
26 =head1 DESCRIPTION
27
28 this script shows the details for a bookseller given on input arg.
29 It allows to edit & save information about this bookseller.
30
31 =head1 CGI PARAMETERS
32
33 =over 4
34
35 =item booksellerid
36
37 To know the bookseller this script has to display details.
38
39 =back
40
41 =cut
42
43 use strict;
44 use warnings;
45 use C4::Auth;
46 use C4::Contract;
47 use C4::Biblio;
48 use C4::Output;
49 use CGI qw ( -utf8 );
50
51 use C4::Bookseller qw( DelBookseller );
52 use C4::Bookseller::Contact;
53 use C4::Budgets;
54
55 use Koha::Acquisition::Bookseller;
56 use Koha::Acquisition::Currencies;
57
58 my $query    = CGI->new;
59 my $op = $query->param('op') || 'display';
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {   template_name   => 'acqui/supplier.tt',
62         query           => $query,
63         type            => 'intranet',
64         authnotrequired => 0,
65         flagsrequired   => { acquisition => '*' },
66         debug           => 1,
67     }
68 );
69 my $booksellerid       = $query->param('booksellerid');
70 my $supplier = {};
71 if ($booksellerid) {
72     $supplier = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
73     foreach ( keys %{$supplier} ) {
74         $template->{'VARS'}->{$_} = $supplier->{$_};
75     }
76     $template->{'VARS'}->{'booksellerid'} = $booksellerid;
77 }
78 $template->{'VARS'}->{'contacts'} = C4::Bookseller::Contact->new() unless $template->{'VARS'}->{'contacts'};
79
80 if ( $op eq 'display' ) {
81     my $contracts = GetContracts( { booksellerid => $booksellerid } );
82
83     $template->param(
84         active        => $supplier->{'active'},
85         gstrate       => $supplier->{'gstrate'} + 0.0,
86         invoiceprice  => $supplier->{'invoiceprice'},
87         listprice     => $supplier->{'listprice'},
88         basketcount   => $supplier->{'basketcount'},
89         subscriptioncount   => $supplier->{'subscriptioncount'},
90         contracts     => $contracts,
91     );
92 } elsif ( $op eq 'delete' ) {
93     # no further message needed for the user
94     # the DELETE button only appears in the template if basketcount == 0
95     if ( $supplier->{'basketcount'} == 0 ) {
96         DelBookseller($booksellerid);
97     }
98     print $query->redirect('/cgi-bin/koha/acqui/acqui-home.pl');
99     exit;
100 } else {
101     my @currencies = Koha::Acquisition::Currencies->search;
102
103     # get option values from gist syspref
104     my @gst_values = map {
105         option => $_ + 0.0
106     }, split( '\|', C4::Context->preference("gist") );
107
108     $template->param(
109         # set active ON by default for supplier add (id empty for add)
110         active       => $booksellerid ? $supplier->{'active'} : 1,
111         gstrate       => $supplier->{gstrate} ? $supplier->{'gstrate'}+0.0 : 0,
112         gst_values    => \@gst_values,
113         currencies    => \@currencies,
114         enter         => 1,
115     );
116 }
117
118 output_html_with_http_headers $query, $cookie, $template->output;