Merge commit 'kc/master'
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use vars qw($VERSION @ISA @EXPORT);
24
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
35         );
36 }
37
38
39 =head1 NAME
40
41 C4::Bookseller - Koha functions for dealing with booksellers.
42
43 =head1 SYNOPSIS
44
45 use C4::Bookseller;
46
47 =head1 DESCRIPTION
48
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.
52
53 =head1 FUNCTIONS
54
55 =head2 GetBookSeller
56
57 @results = &GetBookSeller($searchstring);
58
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.
61
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.
64
65 =cut
66
67 # FIXME: This function is badly named.  It should be something like 
68 # SearchBookSellersByName.  It is NOT a singular return value.
69
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 );
84     }
85     $sth->finish;
86     return  @results ;
87 }
88
89
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;
101         }
102         return;
103 }
104 #-----------------------------------------------------------------#
105
106 =head2 GetBooksellersWithLateOrders
107
108 %results = &GetBooksellersWithLateOrders;
109
110 Searches for suppliers with late orders.
111
112 =cut
113
114 sub GetBooksellersWithLateOrders {
115     my ($delay,$branch) = @_;   # FIXME: Branch argument unused.
116     my $dbh   = C4::Context->dbh;
117
118 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
119 # should be tested with other DBMs
120
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))
130         ";
131     }
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))
139         ";
140     }
141
142     my $sth = $dbh->prepare($strsth);
143     $sth->execute;
144     my %supplierlist;
145     while ( my ( $id, $name ) = $sth->fetchrow ) {
146         $supplierlist{$id} = $name;
147     }
148
149     return %supplierlist;
150 }
151
152 #--------------------------------------------------------------------#
153
154 =head2 AddBookseller
155
156 $id = &AddBookseller($bookseller);
157
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.
161
162 Returns the ID of the newly-created bookseller.
163
164 =cut
165
166 sub AddBookseller {
167     my ($data) = @_;
168     my $dbh = C4::Context->dbh;
169     my $query = "
170         INSERT INTO aqbooksellers
171             (
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
178             )
179         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
180     ";
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'}
195     );
196
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
202     ";
203     $sth = $dbh->prepare($query);
204     $sth->execute;
205     return scalar($sth->fetchrow);
206 }
207
208 #-----------------------------------------------------------------#
209
210 =head2 ModBookseller
211
212 &ModBookseller($bookseller);
213
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}>.
218
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.
222
223 =cut
224
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=?
237     ";
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'}
254     );
255     $sth->finish;
256 }
257
258 =head2 DelBookseller
259
260 &DelBookseller($booksellerid);
261
262 delete the supplier identified by $booksellerid
263 This sub can be called only if the supplier has no order.
264
265 =cut
266
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);
272 }
273
274 1;
275
276 __END__
277
278 =head1 AUTHOR
279
280 Koha Development Team <info@koha.org>
281
282 =cut