bug 4802 add missing order search help file
[koha.git] / C4 / Bookseller.pm
blobbdc43467ab6036ab5c35b4f6860044eb69741844
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
23 use vars qw($VERSION @ISA @EXPORT);
25 BEGIN {
26 # set the version for version checking
27 $VERSION = 3.01;
28 require Exporter;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetBookSeller &GetBooksellersWithLateOrders &GetBookSellerFromId
32 &ModBookseller
33 &DelBookseller
34 &AddBookseller
39 =head1 NAME
41 C4::Bookseller - Koha functions for dealing with booksellers.
43 =head1 SYNOPSIS
45 use C4::Bookseller;
47 =head1 DESCRIPTION
49 The functions in this module deal with booksellers. They allow to
50 add a new bookseller, to modify it or to get some informations around
51 a bookseller.
53 =head1 FUNCTIONS
55 =head2 GetBookSeller
57 @results = &GetBookSeller($searchstring);
59 Looks up a book seller. C<$searchstring> may be either a book seller
60 ID, or a string to look for in the book seller's name.
62 C<@results> is an array of references-to-hash, whose keys are the fields of of the
63 aqbooksellers table in the Koha database.
65 =cut
67 # FIXME: This function is badly named. It should be something like
68 # SearchBookSellersByName. It is NOT a singular return value.
70 sub GetBookSeller($) {
71 my ($searchstring) = @_;
72 my $dbh = C4::Context->dbh;
73 my $query = "SELECT * FROM aqbooksellers WHERE name LIKE ?";
74 my $sth =$dbh->prepare($query);
75 $sth->execute( "%$searchstring%" );
76 my @results;
77 # count how many baskets this bookseller has.
78 # if it has none, the bookseller can be deleted
79 my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
80 while ( my $data = $sth->fetchrow_hashref ) {
81 $sth2->execute($data->{id});
82 $data->{basketcount} = $sth2->fetchrow();
83 push( @results, $data );
85 $sth->finish;
86 return @results ;
90 sub GetBookSellerFromId($) {
91 my $id = shift or return;
92 my $dbh = C4::Context->dbh();
93 my $query = "SELECT * FROM aqbooksellers WHERE id = ?";
94 my $sth =$dbh->prepare($query);
95 $sth->execute( $id );
96 if (my $data = $sth->fetchrow_hashref()){
97 my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
98 $sth2->execute($id);
99 $data->{basketcount}=$sth2->fetchrow();
100 return $data;
102 return;
104 #-----------------------------------------------------------------#
106 =head2 GetBooksellersWithLateOrders
108 %results = &GetBooksellersWithLateOrders;
110 Searches for suppliers with late orders.
112 =cut
114 sub GetBooksellersWithLateOrders {
115 my ($delay,$branch) = @_; # FIXME: Branch argument unused.
116 my $dbh = C4::Context->dbh;
118 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
119 # should be tested with other DBMs
121 my $strsth;
122 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
123 if ( $dbdriver eq "mysql" ) {
124 $strsth = "
125 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
126 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
127 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
128 WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
129 AND (datereceived = '' OR datereceived IS NULL))
132 else {
133 $strsth = "
134 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
135 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
136 LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
137 WHERE (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
138 AND (datereceived = '' OR datereceived IS NULL))
142 my $sth = $dbh->prepare($strsth);
143 $sth->execute;
144 my %supplierlist;
145 while ( my ( $id, $name ) = $sth->fetchrow ) {
146 $supplierlist{$id} = $name;
149 return %supplierlist;
152 #--------------------------------------------------------------------#
154 =head2 AddBookseller
156 $id = &AddBookseller($bookseller);
158 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
159 keys are the fields of the aqbooksellers table in the Koha database.
160 All fields must be present.
162 Returns the ID of the newly-created bookseller.
164 =cut
166 sub AddBookseller {
167 my ($data) = @_;
168 my $dbh = C4::Context->dbh;
169 my $query = "
170 INSERT INTO aqbooksellers
172 name, address1, address2, address3, address4,
173 postal, phone, fax, url, contact,
174 contpos, contphone, contfax, contaltphone, contemail,
175 contnotes, active, listprice, invoiceprice, gstreg,
176 listincgst,invoiceincgst, discount,
177 notes
179 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
181 my $sth = $dbh->prepare($query);
182 $sth->execute(
183 $data->{'name'}, $data->{'address1'},
184 $data->{'address2'}, $data->{'address3'},
185 $data->{'address4'}, $data->{'postal'},
186 $data->{'phone'}, $data->{'fax'},
187 $data->{'url'}, $data->{'contact'},
188 $data->{'contpos'}, $data->{'contphone'},
189 $data->{'contfax'}, $data->{'contaltphone'},
190 $data->{'contemail'}, $data->{'contnotes'},
191 $data->{'active'}, $data->{'listprice'},
192 $data->{'invoiceprice'}, $data->{'gstreg'},
193 $data->{'listincgst'}, $data->{'invoiceincgst'},
194 $data->{'discount'}, $data->{'notes'}
197 # return the id of this new supplier
198 # FIXME: no protection against simultaneous addition: max(id) might be wrong!
199 $query = "
200 SELECT max(id)
201 FROM aqbooksellers
203 $sth = $dbh->prepare($query);
204 $sth->execute;
205 return scalar($sth->fetchrow);
208 #-----------------------------------------------------------------#
210 =head2 ModBookseller
212 &ModBookseller($bookseller);
214 Updates the information for a given bookseller. C<$bookseller> is a
215 reference-to-hash whose keys are the fields of the aqbooksellers table
216 in the Koha database. It must contain entries for all of the fields.
217 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
219 The easiest way to get all of the necessary fields is to look up a
220 book seller with C<&GetBookseller>, modify what's necessary, then call
221 C<&ModBookseller> with the result.
223 =cut
225 sub ModBookseller {
226 my ($data) = @_;
227 my $dbh = C4::Context->dbh;
228 my $query = "
229 UPDATE aqbooksellers
230 SET name=?,address1=?,address2=?,address3=?,address4=?,
231 postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
232 contphone=?,contfax=?,contaltphone=?,contemail=?,
233 contnotes=?,active=?,listprice=?, invoiceprice=?,
234 gstreg=?,listincgst=?,invoiceincgst=?,
235 discount=?, notes=?, gstrate=?
236 WHERE id=?
238 my $sth = $dbh->prepare($query);
239 $sth->execute(
240 $data->{'name'}, $data->{'address1'},
241 $data->{'address2'}, $data->{'address3'},
242 $data->{'address4'}, $data->{'postal'},
243 $data->{'phone'}, $data->{'fax'},
244 $data->{'url'}, $data->{'contact'},
245 $data->{'contpos'}, $data->{'contphone'},
246 $data->{'contfax'}, $data->{'contaltphone'},
247 $data->{'contemail'}, $data->{'contnotes'},
248 $data->{'active'}, $data->{'listprice'},
249 $data->{'invoiceprice'}, $data->{'gstreg'},
250 $data->{'listincgst'}, $data->{'invoiceincgst'},
251 $data->{'discount'},
252 $data->{'notes'}, $data->{'gstrate'},
253 $data->{'id'}
255 $sth->finish;
258 =head2 DelBookseller
260 &DelBookseller($booksellerid);
262 delete the supplier identified by $booksellerid
263 This sub can be called only if the supplier has no order.
265 =cut
266 sub DelBookseller {
267 my ($id) = @_;
268 my $dbh=C4::Context->dbh;
269 my $sth=$dbh->prepare("DELETE FROM aqbooksellers WHERE id=?");
270 $sth->execute($id);
275 __END__
277 =head1 AUTHOR
279 Koha Developement team <info@koha.org>
281 =cut