Merge remote-tracking branch 'origin/new/bug_6634'
[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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 qw/GetContract/;
47 use C4::Biblio;
48 use C4::Output;
49 use CGI;
50
51 use C4::Bookseller qw( GetBookSellerFromId DelBookseller );
52 use C4::Budgets;
53
54 my $query    = CGI->new;
55 my $booksellerid       = $query->param('booksellerid');
56 my $supplier = {};
57 if ($booksellerid) {
58     $supplier = GetBookSellerFromId($booksellerid);
59 }
60 my $op = $query->param('op') || 'display';
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {   template_name   => 'acqui/supplier.tmpl',
63         query           => $query,
64         type            => 'intranet',
65         authnotrequired => 0,
66         flagsrequired   => { acquisition => '*' },
67         debug           => 1,
68     }
69 );
70 my $seller_gstrate = $supplier->{'gstrate'};
71
72 # ensure the scalar isn't flagged as a string
73 $seller_gstrate = ( defined $seller_gstrate ) ? $seller_gstrate + 0 : undef;
74 my $tax_rate = $seller_gstrate // C4::Context->preference('gist') // 0;
75 $tax_rate *= 100;
76 #build array for currencies
77 if ( $op eq 'display' ) {
78
79     my $contracts = GetContract( { booksellerid => $booksellerid } );
80
81     $template->param(
82         booksellerid  => $booksellerid,
83         name          => $supplier->{'name'},
84         postal        => $supplier->{'postal'},
85         address1      => $supplier->{'address1'},
86         address2      => $supplier->{'address2'},
87         address3      => $supplier->{'address3'},
88         address4      => $supplier->{'address4'},
89         phone         => $supplier->{'phone'},
90         accountnumber => $supplier->{'accountnumber'},
91         fax           => $supplier->{'fax'},
92         url           => $supplier->{'url'},
93         contact       => $supplier->{'contact'},
94         contpos       => $supplier->{'contpos'},
95         contphone     => $supplier->{'contphone'},
96         contaltphone  => $supplier->{'contaltphone'},
97         contfax       => $supplier->{'contfax'},
98         contemail     => $supplier->{'contemail'},
99         contnotes     => $supplier->{'contnotes'},
100         notes         => $supplier->{'notes'},
101         active        => $supplier->{'active'},
102         gstreg        => $supplier->{'gstreg'},
103         listincgst    => $supplier->{'listincgst'},
104         invoiceincgst => $supplier->{'invoiceincgst'},
105         discount      => $supplier->{'discount'},
106         deliverytime  => $supplier->{deliverytime},
107         invoiceprice  => $supplier->{'invoiceprice'},
108         listprice     => $supplier->{'listprice'},
109         GST           => $tax_rate,
110         default_tax   => defined($seller_gstrate),
111         basketcount   => $supplier->{'basketcount'},
112         subscriptioncount   => $supplier->{'subscriptioncount'},
113         contracts     => $contracts,
114         dateformat    => C4::Context->preference("dateformat"),
115     );
116 } elsif ( $op eq 'delete' ) {
117     # no further message needed for the user
118     # the DELETE button only appears in the template if basketcount == 0
119     if ( $supplier->{'basketcount'} == 0 ) {
120         DelBookseller($booksellerid);
121     }
122     print $query->redirect('/cgi-bin/koha/acqui/acqui-home.pl');
123     exit;
124 } else {
125     my @currencies = GetCurrencies();
126     my $loop_currency;
127     my $active_currency = GetCurrency();
128     my $active_listprice = $supplier->{'listprice'};
129     my $active_invoiceprice = $supplier->{'invoiceprice'};
130     if (!$supplier->{listprice}) {
131         $active_listprice =  $active_currency->{currency};
132     }
133     if (!$supplier->{invoiceprice}) {
134         $active_invoiceprice =  $active_currency->{currency};
135     }
136     for (@currencies) {
137         push @{$loop_currency},
138             { 
139             currency     => $_->{currency},
140             listprice    => ( $_->{currency} eq $active_listprice ),
141             invoiceprice => ( $_->{currency} eq $active_invoiceprice ),
142             };
143     }
144
145     my $default_gst_rate = (C4::Context->preference('gist') * 100) || '0.0';
146
147     my $gstrate = defined $supplier->{gstrate} ? $supplier->{gstrate} * 100 : '';
148     $template->param(
149         booksellerid => $booksellerid,
150         name         => $supplier->{'name'},
151         postal       => $supplier->{'postal'},
152         address1     => $supplier->{'address1'},
153         address2     => $supplier->{'address2'},
154         address3     => $supplier->{'address3'},
155         address4     => $supplier->{'address4'},
156         phone        => $supplier->{'phone'},
157         accountnumber=> $supplier->{'accountnumber'},
158         fax          => $supplier->{'fax'},
159         url          => $supplier->{'url'},
160         contact      => $supplier->{'contact'},
161         contpos      => $supplier->{'contpos'},
162         contphone    => $supplier->{'contphone'},
163         contaltphone => $supplier->{'contaltphone'},
164         contfax      => $supplier->{'contfax'},
165         contemail    => $supplier->{'contemail'},
166         contnotes    => $supplier->{'contnotes'},
167         notes        => $supplier->{'notes'},
168         # set active ON by default for supplier add (id empty for add)
169         active       => $booksellerid ? $supplier->{'active'} : 1,
170         gstreg        => $supplier->{'gstreg'},
171         listincgst    => $supplier->{'listincgst'},
172         invoiceincgst => $supplier->{'invoiceincgst'},
173         gstrate       => $gstrate,
174         discount      => $supplier->{'discount'},
175         deliverytime  => $supplier->{deliverytime},
176         loop_currency => $loop_currency,
177         GST           => $tax_rate,
178         enter         => 1,
179         default_gst_rate => $default_gst_rate,
180     );
181 }
182
183 output_html_with_http_headers $query, $cookie, $template->output;