Merge remote branch 'kc/new/enh/bug_1962' into kcmaster
[wip/koha-chris_n.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 supplierid
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 C4::Dates qw/format_date /;
50 use CGI;
51
52 use C4::Bookseller qw( GetBookSellerFromId DelBookseller );
53 use C4::Budgets;
54
55 my $query    = CGI->new;
56 my $id       = $query->param('supplierid');
57 my $supplier = {};
58 if ($id) {
59     $supplier = GetBookSellerFromId($id);
60 }
61 my $op = $query->param('op') || 'display';
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {   template_name   => 'acqui/supplier.tmpl',
64         query           => $query,
65         type            => 'intranet',
66         authnotrequired => 0,
67         flagsrequired   => { acquisition => 'vendors_manage' },
68         debug           => 1,
69     }
70 );
71 my $seller_gstrate = $supplier->{'gstrate'};
72
73 # ensure the scalar isn't flagged as a string
74 $seller_gstrate = ( defined $seller_gstrate ) ? $seller_gstrate + 0 : undef;
75 my $tax_rate = $seller_gstrate // C4::Context->preference('gist') // 0;
76 $tax_rate *= 100;
77 #build array for currencies
78 if ( $op eq 'display' ) {
79
80     my $contracts = GetContract( { booksellerid => $id } );
81
82     for ( @{$contracts} ) {
83         $_->{contractstartdate} = format_date( $_->{contractstartdate} );
84         $_->{contractenddate}   = format_date( $_->{contractenddate} );
85     }
86
87     $template->param(
88         id            => $id,
89         name          => $supplier->{'name'},
90         postal        => $supplier->{'postal'},
91         address1      => $supplier->{'address1'},
92         address2      => $supplier->{'address2'},
93         address3      => $supplier->{'address3'},
94         address4      => $supplier->{'address4'},
95         phone         => $supplier->{'phone'},
96         fax           => $supplier->{'fax'},
97         url           => $supplier->{'url'},
98         contact       => $supplier->{'contact'},
99         contpos       => $supplier->{'contpos'},
100         contphone     => $supplier->{'contphone'},
101         contaltphone  => $supplier->{'contaltphone'},
102         contfax       => $supplier->{'contfax'},
103         contemail     => $supplier->{'contemail'},
104         contnotes     => $supplier->{'contnotes'},
105         notes         => $supplier->{'notes'},
106         active        => $supplier->{'active'},
107         gstreg        => $supplier->{'gstreg'},
108         listincgst    => $supplier->{'listincgst'},
109         invoiceincgst => $supplier->{'invoiceincgst'},
110         discount      => $supplier->{'discount'},
111         invoiceprice  => $supplier->{'invoiceprice'},
112         listprice     => $supplier->{'listprice'},
113         GST           => $tax_rate,
114         default_tax   => defined($seller_gstrate),
115         basketcount   => $supplier->{'basketcount'},
116         contracts     => $contracts,
117     );
118 } elsif ( $op eq 'delete' ) {
119     DelBookseller($id);
120     print $query->redirect('/cgi-bin/koha/acqui/acqui-home.pl');
121     exit;
122 } else {
123     my @currencies = GetCurrencies();
124     my $loop_currency;
125     for (@currencies) {
126         push @{$loop_currency},
127           { currency     => $_->{currency},
128             listprice    => ( $_->{currency} eq $supplier->{listprice} ),
129             invoiceprice => ( $_->{currency} eq $supplier->{invoiceprice} ),
130           };
131     }
132
133     my $default_gst_rate = (C4::Context->preference('gist') * 100) || '0.0';
134
135     my $gstrate = defined $supplier->{gstrate} ? $supplier->{gstrate} * 100 : '';
136     $template->param(
137         id           => $id,
138         name         => $supplier->{'name'},
139         postal       => $supplier->{'postal'},
140         address1     => $supplier->{'address1'},
141         address2     => $supplier->{'address2'},
142         address3     => $supplier->{'address3'},
143         address4     => $supplier->{'address4'},
144         phone        => $supplier->{'phone'},
145         fax          => $supplier->{'fax'},
146         url          => $supplier->{'url'},
147         contact      => $supplier->{'contact'},
148         contpos      => $supplier->{'contpos'},
149         contphone    => $supplier->{'contphone'},
150         contaltphone => $supplier->{'contaltphone'},
151         contfax      => $supplier->{'contfax'},
152         contemail    => $supplier->{'contemail'},
153         contnotes    => $supplier->{'contnotes'},
154         notes        => $supplier->{'notes'},
155         # set active ON by default for supplier add (id empty for add)
156         active       => $id ? $supplier->{'active'} : 1,
157         gstreg        => $supplier->{'gstreg'},
158         listincgst    => $supplier->{'listincgst'},
159         invoiceincgst => $supplier->{'invoiceincgst'},
160         gstrate       => $gstrate,
161         discount      => $supplier->{'discount'},
162         loop_currency => $loop_currency,
163         GST           => $tax_rate,
164         enter         => 1,
165         default_gst_rate => $default_gst_rate,
166     );
167 }
168
169 output_html_with_http_headers $query, $cookie, $template->output;