Bug 15006 Drop raw connection if login fails
[koha.git] / C4 / Bookseller.pm
blob44b93895fac05163e36adda99b8ac5a03aa4ba8c
1 package C4::Bookseller;
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 PTFS Europe
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
24 use base qw( Exporter );
26 use C4::Bookseller::Contact;
28 our @EXPORT_OK = qw(
29 GetBooksellersWithLateOrders
30 ModBookseller
31 DelBookseller
32 AddBookseller
35 =head1 NAME
37 C4::Bookseller - Koha functions for dealing with booksellers.
39 =head1 SYNOPSIS
41 use C4::Bookseller;
43 =head1 DESCRIPTION
45 The functions in this module deal with booksellers. They allow to
46 add a new bookseller, to modify it or to get some informations around
47 a bookseller.
49 =head1 FUNCTIONS
51 =head2 GetBooksellersWithLateOrders
53 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
55 Searches for suppliers with late orders.
57 =cut
59 sub GetBooksellersWithLateOrders {
60 my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
61 my $dbh = C4::Context->dbh;
63 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
64 # should be tested with other DBMs
66 my $query;
67 my @query_params = ();
68 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
69 $query = "
70 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
71 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
72 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
73 WHERE
74 ( datereceived = ''
75 OR datereceived IS NULL
76 OR aqorders.quantityreceived < aqorders.quantity
78 AND aqorders.rrp <> 0
79 AND aqorders.ecost <> 0
80 AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
81 AND aqbasket.closedate IS NOT NULL
83 if ( defined $delay && $delay >= 0 ) {
84 $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
85 push @query_params, $delay;
86 } elsif ( $delay && $delay < 0 ){
87 warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
88 return;
90 if ( defined $estimateddeliverydatefrom ) {
91 $query .= '
92 AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
93 push @query_params, $estimateddeliverydatefrom;
94 if ( defined $estimateddeliverydateto ) {
95 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
96 push @query_params, $estimateddeliverydateto;
97 } else {
98 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
101 if ( defined $estimateddeliverydateto ) {
102 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
103 push @query_params, $estimateddeliverydateto;
106 my $sth = $dbh->prepare($query);
107 $sth->execute( @query_params );
108 my %supplierlist;
109 while ( my ( $id, $name ) = $sth->fetchrow ) {
110 $supplierlist{$id} = $name;
113 return %supplierlist;
116 #--------------------------------------------------------------------#
118 =head2 AddBookseller
120 $id = &AddBookseller($bookseller);
122 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
123 keys are the fields of the aqbooksellers table in the Koha database.
124 All fields must be present.
126 Returns the ID of the newly-created bookseller.
128 =cut
130 sub AddBookseller {
131 my ($data, $contacts) = @_;
132 my $dbh = C4::Context->dbh;
133 my $query = q|
134 INSERT INTO aqbooksellers
136 name, address1, address2, address3, address4,
137 postal, phone, accountnumber,fax, url,
138 active, listprice, invoiceprice, gstreg,
139 listincgst,invoiceincgst, gstrate, discount, notes,
140 deliverytime
142 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
144 my $sth = $dbh->prepare($query);
145 $sth->execute(
146 $data->{'name'}, $data->{'address1'},
147 $data->{'address2'}, $data->{'address3'},
148 $data->{'address4'}, $data->{'postal'},
149 $data->{'phone'}, $data->{'accountnumber'},
150 $data->{'fax'}, $data->{'url'},
151 $data->{'active'}, $data->{'listprice'},
152 $data->{'invoiceprice'}, $data->{'gstreg'},
153 $data->{'listincgst'}, $data->{'invoiceincgst'},
154 $data->{'gstrate'}, $data->{'discount'},
155 $data->{notes}, $data->{deliverytime},
158 # return the id of this new supplier
159 my $id = $dbh->{'mysql_insertid'};
160 if ($id && $contacts) {
161 foreach my $contact (@$contacts) {
162 $contact = C4::Bookseller::Contact->new( $contact )
163 unless ref $contacts eq 'C4::Bookseller::Contact';
164 $contact->bookseller($id);
165 $contact->save();
168 return $id;
171 #-----------------------------------------------------------------#
173 =head2 ModBookseller
175 ModBookseller($bookseller);
177 Updates the information for a given bookseller. C<$bookseller> is a
178 reference-to-hash whose keys are the fields of the aqbooksellers table
179 in the Koha database. It must contain entries for all of the fields.
180 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
182 The easiest way to get all of the necessary fields is to look up a
183 book seller with C<Koha::Acquisition::Bookseller>, modify what's necessary, then call
184 C<&ModBookseller> with the result.
186 =cut
188 sub ModBookseller {
189 my ($data, $contacts) = @_;
190 my $dbh = C4::Context->dbh;
191 return unless $data->{'id'};
192 my $query = 'UPDATE aqbooksellers
193 SET name=?,address1=?,address2=?,address3=?,address4=?,
194 postal=?,phone=?,accountnumber=?,fax=?,url=?,
195 active=?,listprice=?, invoiceprice=?,
196 gstreg=?,listincgst=?,invoiceincgst=?,
197 discount=?,notes=?,gstrate=?,deliverytime=?
198 WHERE id=?';
199 my $sth = $dbh->prepare($query);
200 my $cnt = $sth->execute(
201 $data->{'name'}, $data->{'address1'},
202 $data->{'address2'}, $data->{'address3'},
203 $data->{'address4'}, $data->{'postal'},
204 $data->{'phone'}, $data->{'accountnumber'},
205 $data->{'fax'}, $data->{'url'},
206 $data->{'active'}, $data->{'listprice'},
207 $data->{'invoiceprice'}, $data->{'gstreg'},
208 $data->{'listincgst'}, $data->{'invoiceincgst'},
209 $data->{'discount'}, $data->{'notes'},
210 $data->{'gstrate'}, $data->{deliverytime},
211 $data->{'id'}
213 $contacts ||= $data->{'contacts'};
214 my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
215 my @contactparams = ($data->{'id'});
216 if ($contacts) {
217 foreach my $contact (@$contacts) {
218 $contact = C4::Bookseller::Contact->new( $contact )
219 unless ref $contacts eq 'C4::Bookseller::Contact';
220 $contact->bookseller($data->{'id'});
221 $contact->save();
222 push @contactparams, $contact->id if $contact->id;
224 if ($#contactparams > 0) {
225 $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
228 $sth = $dbh->prepare($contactquery);
229 $sth->execute(@contactparams);
230 return $cnt;
233 =head2 DelBookseller
235 DelBookseller($booksellerid);
237 delete the supplier record identified by $booksellerid
238 This sub assumes it is called only if the supplier has no order.
240 =cut
242 sub DelBookseller {
243 my $id = shift;
244 my $dbh = C4::Context->dbh;
245 my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
246 return $sth->execute($id);
251 __END__
253 =head1 AUTHOR
255 Koha Development Team <http://koha-community.org/>
257 =cut