Acquisition module has been cut into 3 files :
[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 =over 2
54
55 =cut
56
57 #-------------------------------------------------------------------#
58
59 =head3 GetBookSeller
60
61 =over 4
62
63 @results = &GetBookSeller($searchstring);
64
65 Looks up a book seller. C<$searchstring> may be either a book seller
66 ID, or a string to look for in the book seller's name.
67
68 C<@results> is an array of references-to-hash, whose keys are the fields of of the
69 aqbooksellers table in the Koha database.
70
71 =back
72
73 =cut
74
75 sub GetBookSeller {
76     my ($searchstring) = @_;
77     my $dbh = C4::Context->dbh;
78     my $query = "
79         SELECT *
80         FROM   aqbooksellers
81         WHERE  name LIKE ? OR id = ?
82     ";
83     my $sth =$dbh->prepare($query);
84     $sth->execute("$searchstring%", $searchstring );
85     my @results;
86     while ( my $data = $sth->fetchrow_hashref ) {
87         push( @results, $data );
88     }
89     $sth->finish;
90     return  @results ;
91 }
92
93
94 #-----------------------------------------------------------------#
95
96 =head3 GetBooksellersWithLateOrders
97
98 =over 4
99
100 %results = &GetBooksellersWithLateOrders;
101
102 Searches for suppliers with late orders.
103
104 =back
105
106 =cut
107
108 sub GetBooksellersWithLateOrders {
109     my $delay = shift;
110     my $dbh   = C4::Context->dbh;
111
112 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
113 # should be tested with other DBMs
114
115     my $strsth;
116     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
117     if ( $dbdriver eq "mysql" ) {
118         $strsth = "
119             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
120             FROM aqorders, aqbasket
121             LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
122             WHERE aqorders.basketno = aqbasket.basketno
123                 AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
124                 AND (datereceived = '' OR datereceived IS NULL))
125         ";
126     }
127     else {
128         $strsth = "
129             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
130             FROM aqorders, aqbasket
131             LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
132             WHERE aqorders.basketno = aqbasket.basketno
133                 AND (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
134                 AND (datereceived = '' OR datereceived IS NULL))
135         ";
136     }
137
138     my $sth = $dbh->prepare($strsth);
139     $sth->execute;
140     my %supplierlist;
141     while ( my ( $id, $name ) = $sth->fetchrow ) {
142         $supplierlist{$id} = $name;
143     }
144
145     return %supplierlist;
146 }
147
148 #--------------------------------------------------------------------#
149
150 =head3 AddBookseller
151
152 =over 4
153
154 $id = &AddBookseller($bookseller);
155
156 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
157 keys are the fields of the aqbooksellers table in the Koha database.
158 All fields must be present.
159
160 Returns the ID of the newly-created bookseller.
161
162 =back
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, specialty,  discount,      invoicedisc,
177                 nocalc,    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->{'specialty'},    $data->{'discount'},
195         $data->{'invoicedisc'},  $data->{'nocalc'},
196         $data->{'notes'}
197     );
198
199     # return the id of this new supplier
200     my $query = "
201         SELECT max(id)
202         FROM   aqbooksellers
203     ";
204     my $sth = $dbh->prepare($query);
205     $sth->execute;
206     return scalar($sth->fetchrow);
207 }
208
209 #-----------------------------------------------------------------#
210
211 =head3 ModSupplier
212
213 =over 4
214
215 &ModSupplier($bookseller);
216
217 Updates the information for a given bookseller. C<$bookseller> is a
218 reference-to-hash whose keys are the fields of the aqbooksellers table
219 in the Koha database. It must contain entries for all of the fields.
220 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
221
222 The easiest way to get all of the necessary fields is to look up a
223 book seller with C<&booksellers>, modify what's necessary, then call
224 C<&ModSupplier> with the result.
225
226 =back
227
228 =cut
229
230 sub ModBookseller {
231     my ($data) = @_;
232     my $dbh    = C4::Context->dbh;
233     my $query = "
234         UPDATE aqbooksellers
235         SET name=?,address1=?,address2=?,address3=?,address4=?,
236             postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
237             contphone=?,contfax=?,contaltphone=?,contemail=?,
238             contnotes=?,active=?,listprice=?, invoiceprice=?,
239             gstreg=?, listincgst=?,invoiceincgst=?,
240             specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=?
241         WHERE id=?
242     ";
243     my $sth    = $dbh->prepare($query);
244     $sth->execute(
245         $data->{'name'},         $data->{'address1'},
246         $data->{'address2'},     $data->{'address3'},
247         $data->{'address4'},     $data->{'postal'},
248         $data->{'phone'},        $data->{'fax'},
249         $data->{'url'},          $data->{'contact'},
250         $data->{'contpos'},      $data->{'contphone'},
251         $data->{'contfax'},      $data->{'contaltphone'},
252         $data->{'contemail'},    $data->{'contnotes'},
253         $data->{'active'},       $data->{'listprice'},
254         $data->{'invoiceprice'}, $data->{'gstreg'},
255         $data->{'listincgst'},   $data->{'invoiceincgst'},
256         $data->{'specialty'},    $data->{'discount'},
257         $data->{'invoicedisc'},  $data->{'nocalc'},
258         $data->{'notes'},        $data->{'id'}
259     );
260     $sth->finish;
261 }
262
263
264 END { }    # module clean-up code here (global destructor)
265
266 1;
267
268 __END__
269
270 =back
271
272 =head1 AUTHOR
273
274 Koha Developement team <info@koha.org>
275
276 =cut