removing '-' in isbn to allow amazon content.
[koha.git] / C4 / Bookseller.pm
1 package C4::Bookseller;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20 # $Id$
21
22 use strict;
23
24 use vars qw($VERSION @ISA @EXPORT);
25
26 # set the version for version checking
27 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
28
29 @ISA    = qw(Exporter);
30 @EXPORT = qw(
31     &GetBookSeller &GetBooksellersWithLateOrders
32     &ModBookseller
33     &AddBookseller
34 );
35
36
37 =head1 NAME
38
39 C4::Bookseller - Koha functions for dealing with booksellers.
40
41 =head1 SYNOPSIS
42
43 use C4::Bookseller;
44
45 =head1 DESCRIPTION
46
47 The functions in this module deal with booksellers. They allow to
48 add a new bookseller, to modify it or to get some informations around
49 a bookseller.
50
51 =head1 FUNCTIONS
52
53 =cut
54
55 #-------------------------------------------------------------------#
56
57 =head2 GetBookSeller
58
59 @results = &GetBookSeller($searchstring);
60
61 Looks up a book seller. C<$searchstring> may be either a book seller
62 ID, or a string to look for in the book seller's name.
63
64 C<@results> is an array of references-to-hash, whose keys are the fields of of the
65 aqbooksellers table in the Koha database.
66
67 =cut
68
69 sub GetBookSeller {
70     my ($searchstring) = @_;
71     my $dbh = C4::Context->dbh;
72     my $query = "
73         SELECT *
74         FROM   aqbooksellers
75         WHERE  name LIKE ? OR id = ?
76     ";
77     my $sth =$dbh->prepare($query);
78     $sth->execute("$searchstring%", $searchstring );
79     my @results;
80     while ( my $data = $sth->fetchrow_hashref ) {
81         push( @results, $data );
82     }
83     $sth->finish;
84     return  @results ;
85 }
86
87
88 #-----------------------------------------------------------------#
89
90 =head2 GetBooksellersWithLateOrders
91
92 %results = &GetBooksellersWithLateOrders;
93
94 Searches for suppliers with late orders.
95
96 =cut
97
98 sub GetBooksellersWithLateOrders {
99     my ($delay,$branch) = @_;
100     my $dbh   = C4::Context->dbh;
101
102 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
103 # should be tested with other DBMs
104
105     my $strsth;
106     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
107     if ( $dbdriver eq "mysql" ) {
108         $strsth = "
109             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
110             FROM aqorders, aqbasket
111             LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
112             WHERE aqorders.basketno = aqbasket.basketno
113                 AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
114                 AND (datereceived = '' OR datereceived IS NULL))
115         ";
116     }
117     else {
118         $strsth = "
119             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
120             FROM aqorders, aqbasket
121             LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
122             WHERE aqorders.basketno = aqbasket.basketno
123                 AND (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
124                 AND (datereceived = '' OR datereceived IS NULL))
125         ";
126     }
127
128     my $sth = $dbh->prepare($strsth);
129     $sth->execute;
130     my %supplierlist;
131     while ( my ( $id, $name ) = $sth->fetchrow ) {
132         $supplierlist{$id} = $name;
133     }
134
135     return %supplierlist;
136 }
137
138 #--------------------------------------------------------------------#
139
140 =head2 AddBookseller
141
142 $id = &AddBookseller($bookseller);
143
144 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
145 keys are the fields of the aqbooksellers table in the Koha database.
146 All fields must be present.
147
148 Returns the ID of the newly-created bookseller.
149
150 =cut
151
152 sub AddBookseller {
153     my ($data) = @_;
154     my $dbh = C4::Context->dbh;
155     my $query = "
156         INSERT INTO aqbooksellers
157             (
158                 name,      address1,      address2,   address3,      address4,
159                 postal,    phone,         fax,        url,           contact,
160                 contpos,   contphone,     contfax,    contaltphone,  contemail,
161                 contnotes, active,        listprice,  invoiceprice,  gstreg,
162                 listincgst,invoiceincgst, specialty,  discount,      invoicedisc,
163                 nocalc,    notes
164             )
165         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
166     ";
167     my $sth = $dbh->prepare($query);
168     $sth->execute(
169         $data->{'name'},         $data->{'address1'},
170         $data->{'address2'},     $data->{'address3'},
171         $data->{'address4'},     $data->{'postal'},
172         $data->{'phone'},        $data->{'fax'},
173         $data->{'url'},          $data->{'contact'},
174         $data->{'contpos'},      $data->{'contphone'},
175         $data->{'contfax'},      $data->{'contaltphone'},
176         $data->{'contemail'},    $data->{'contnotes'},
177         $data->{'active'},       $data->{'listprice'},
178         $data->{'invoiceprice'}, $data->{'gstreg'},
179         $data->{'listincgst'},   $data->{'invoiceincgst'},
180         $data->{'specialty'},    $data->{'discount'},
181         $data->{'invoicedisc'},  $data->{'nocalc'},
182         $data->{'notes'}
183     );
184
185     # return the id of this new supplier
186     $query = "
187         SELECT max(id)
188         FROM   aqbooksellers
189     ";
190     $sth = $dbh->prepare($query);
191     $sth->execute;
192     return scalar($sth->fetchrow);
193 }
194
195 #-----------------------------------------------------------------#
196
197 =head2 ModSupplier
198
199 &ModSupplier($bookseller);
200
201 Updates the information for a given bookseller. C<$bookseller> is a
202 reference-to-hash whose keys are the fields of the aqbooksellers table
203 in the Koha database. It must contain entries for all of the fields.
204 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
205
206 The easiest way to get all of the necessary fields is to look up a
207 book seller with C<&booksellers>, modify what's necessary, then call
208 C<&ModSupplier> with the result.
209
210 =cut
211
212 sub ModBookseller {
213     my ($data) = @_;
214     my $dbh    = C4::Context->dbh;
215     my $query = "
216         UPDATE aqbooksellers
217         SET name=?,address1=?,address2=?,address3=?,address4=?,
218             postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
219             contphone=?,contfax=?,contaltphone=?,contemail=?,
220             contnotes=?,active=?,listprice=?, invoiceprice=?,
221             gstreg=?, listincgst=?,invoiceincgst=?,
222             specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=?
223         WHERE id=?
224     ";
225     my $sth    = $dbh->prepare($query);
226     $sth->execute(
227         $data->{'name'},         $data->{'address1'},
228         $data->{'address2'},     $data->{'address3'},
229         $data->{'address4'},     $data->{'postal'},
230         $data->{'phone'},        $data->{'fax'},
231         $data->{'url'},          $data->{'contact'},
232         $data->{'contpos'},      $data->{'contphone'},
233         $data->{'contfax'},      $data->{'contaltphone'},
234         $data->{'contemail'},    $data->{'contnotes'},
235         $data->{'active'},       $data->{'listprice'},
236         $data->{'invoiceprice'}, $data->{'gstreg'},
237         $data->{'listincgst'},   $data->{'invoiceincgst'},
238         $data->{'specialty'},    $data->{'discount'},
239         $data->{'invoicedisc'},  $data->{'nocalc'},
240         $data->{'notes'},        $data->{'id'}
241     );
242     $sth->finish;
243 }
244
245 END { }    # module clean-up code here (global destructor)
246
247 1;
248
249 __END__
250
251 =head1 AUTHOR
252
253 Koha Developement team <info@koha.org>
254
255 =cut