Bug 2576: Added OPACFinesTab Syspref
[koha.git] / C4 / Bookseller.pm
blob0244bc349e1bae201b9e46c43a59d87c53de0b9a
1 package C4::Bookseller;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
22 use vars qw($VERSION @ISA @EXPORT);
24 BEGIN {
25 # set the version for version checking
26 $VERSION = 3.01;
27 require Exporter;
28 @ISA = qw(Exporter);
29 @EXPORT = qw(
30 &GetBookSeller &GetBooksellersWithLateOrders &GetBookSellerFromId
31 &ModBookseller
32 &DelBookseller
33 &AddBookseller
38 =head1 NAME
40 C4::Bookseller - Koha functions for dealing with booksellers.
42 =head1 SYNOPSIS
44 use C4::Bookseller;
46 =head1 DESCRIPTION
48 The functions in this module deal with booksellers. They allow to
49 add a new bookseller, to modify it or to get some informations around
50 a bookseller.
52 =head1 FUNCTIONS
54 =head2 GetBookSeller
56 @results = &GetBookSeller($searchstring);
58 Looks up a book seller. C<$searchstring> may be either a book seller
59 ID, or a string to look for in the book seller's name.
61 C<@results> is an array of references-to-hash, whose keys are the fields of of the
62 aqbooksellers table in the Koha database.
64 =cut
66 # FIXME: This function is badly named. It should be something like
67 # SearchBookSellersByName. It is NOT a singular return value.
69 sub GetBookSeller($) {
70 my ($searchstring) = @_;
71 my $dbh = C4::Context->dbh;
72 my $query = "SELECT * FROM aqbooksellers WHERE name LIKE ?";
73 my $sth =$dbh->prepare($query);
74 $sth->execute( "$searchstring%" );
75 my @results;
76 # count how many baskets this bookseller has.
77 # if it has none, the bookseller can be deleted
78 my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
79 while ( my $data = $sth->fetchrow_hashref ) {
80 $sth2->execute($data->{id});
81 $data->{basketcount} = $sth2->fetchrow();
82 push( @results, $data );
84 $sth->finish;
85 return @results ;
89 sub GetBookSellerFromId($) {
90 my ($id) = shift or return undef;
91 my $dbh = C4::Context->dbh();
92 my $query = "SELECT * FROM aqbooksellers WHERE id = ?";
93 my $sth =$dbh->prepare($query);
94 $sth->execute( $id );
95 if (my $data = $sth->fetchrow_hashref()){
96 my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
97 $sth2->execute($id);
98 $data->{basketcount}=$sth2->fetchrow();
99 return ($data);
101 return 0;
103 #-----------------------------------------------------------------#
105 =head2 GetBooksellersWithLateOrders
107 %results = &GetBooksellersWithLateOrders;
109 Searches for suppliers with late orders.
111 =cut
113 sub GetBooksellersWithLateOrders {
114 my ($delay,$branch) = @_; # FIXME: Branch argument unused.
115 my $dbh = C4::Context->dbh;
117 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
118 # should be tested with other DBMs
120 my $strsth;
121 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
122 if ( $dbdriver eq "mysql" ) {
123 $strsth = "
124 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
125 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
126 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
127 WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
128 AND (datereceived = '' OR datereceived IS NULL))
131 else {
132 $strsth = "
133 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
134 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
135 LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
136 WHERE (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
137 AND (datereceived = '' OR datereceived IS NULL))
141 my $sth = $dbh->prepare($strsth);
142 $sth->execute;
143 my %supplierlist;
144 while ( my ( $id, $name ) = $sth->fetchrow ) {
145 $supplierlist{$id} = $name;
148 return %supplierlist;
151 #--------------------------------------------------------------------#
153 =head2 AddBookseller
155 $id = &AddBookseller($bookseller);
157 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
158 keys are the fields of the aqbooksellers table in the Koha database.
159 All fields must be present.
161 Returns the ID of the newly-created bookseller.
163 =cut
165 sub AddBookseller {
166 my ($data) = @_;
167 my $dbh = C4::Context->dbh;
168 my $query = "
169 INSERT INTO aqbooksellers
171 name, address1, address2, address3, address4,
172 postal, phone, fax, url, contact,
173 contpos, contphone, contfax, contaltphone, contemail,
174 contnotes, active, listprice, invoiceprice, gstreg,
175 listincgst,invoiceincgst, specialty, discount, invoicedisc,
176 nocalc, notes
178 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
180 my $sth = $dbh->prepare($query);
181 $sth->execute(
182 $data->{'name'}, $data->{'address1'},
183 $data->{'address2'}, $data->{'address3'},
184 $data->{'address4'}, $data->{'postal'},
185 $data->{'phone'}, $data->{'fax'},
186 $data->{'url'}, $data->{'contact'},
187 $data->{'contpos'}, $data->{'contphone'},
188 $data->{'contfax'}, $data->{'contaltphone'},
189 $data->{'contemail'}, $data->{'contnotes'},
190 $data->{'active'}, $data->{'listprice'},
191 $data->{'invoiceprice'}, $data->{'gstreg'},
192 $data->{'listincgst'}, $data->{'invoiceincgst'},
193 $data->{'specialty'}, $data->{'discount'},
194 $data->{'invoicedisc'}, $data->{'nocalc'},
195 $data->{'notes'}
198 # return the id of this new supplier
199 # FIXME: no protection against simultaneous addition: max(id) might be wrong!
200 $query = "
201 SELECT max(id)
202 FROM aqbooksellers
204 $sth = $dbh->prepare($query);
205 $sth->execute;
206 return scalar($sth->fetchrow);
209 #-----------------------------------------------------------------#
211 =head2 ModBookseller
213 &ModBookseller($bookseller);
215 Updates the information for a given bookseller. C<$bookseller> is a
216 reference-to-hash whose keys are the fields of the aqbooksellers table
217 in the Koha database. It must contain entries for all of the fields.
218 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
220 The easiest way to get all of the necessary fields is to look up a
221 book seller with C<&booksellers>, modify what's necessary, then call
222 C<&ModSupplier> with the result.
224 =cut
226 sub ModBookseller {
227 my ($data) = @_;
228 my $dbh = C4::Context->dbh;
229 my $query = "
230 UPDATE aqbooksellers
231 SET name=?,address1=?,address2=?,address3=?,address4=?,
232 postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
233 contphone=?,contfax=?,contaltphone=?,contemail=?,
234 contnotes=?,active=?,listprice=?, invoiceprice=?,
235 gstreg=?, listincgst=?,invoiceincgst=?,
236 specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=?
237 WHERE id=?
239 my $sth = $dbh->prepare($query);
240 $sth->execute(
241 $data->{'name'}, $data->{'address1'},
242 $data->{'address2'}, $data->{'address3'},
243 $data->{'address4'}, $data->{'postal'},
244 $data->{'phone'}, $data->{'fax'},
245 $data->{'url'}, $data->{'contact'},
246 $data->{'contpos'}, $data->{'contphone'},
247 $data->{'contfax'}, $data->{'contaltphone'},
248 $data->{'contemail'}, $data->{'contnotes'},
249 $data->{'active'}, $data->{'listprice'},
250 $data->{'invoiceprice'}, $data->{'gstreg'},
251 $data->{'listincgst'}, $data->{'invoiceincgst'},
252 $data->{'specialty'}, $data->{'discount'},
253 $data->{'invoicedisc'}, $data->{'nocalc'},
254 $data->{'notes'}, $data->{'id'}
256 $sth->finish;
259 =head2 DelBookseller
261 &DelBookseller($booksellerid);
263 delete the supplier identified by $booksellerid
264 This sub can be called only if the supplier has no order.
266 =cut
267 sub DelBookseller {
268 my ($id) = @_;
269 my $dbh=C4::Context->dbh;
270 my $sth=$dbh->prepare("DELETE FROM aqbooksellers WHERE id=?");
271 $sth->execute($id);
276 __END__
278 =head1 AUTHOR
280 Koha Developement team <info@koha.org>
282 =cut