acqui/addorder.pl - use reliable GetBookSellerFromId instead of relying on name search.
[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 use strict;
21
22 use vars qw($VERSION @ISA @EXPORT);
23
24 BEGIN {
25         # set the version for version checking
26         $VERSION = 3.01;
27         @ISA    = qw(Exporter);
28         @EXPORT = qw(
29                 &GetBookSeller &GetBooksellersWithLateOrders &GetBookSellerFromId
30                 &ModBookseller
31                 &DelBookseller
32                 &AddBookseller
33         );
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 =head2 GetBookSeller
54
55 @results = &GetBookSeller($searchstring);
56
57 Looks up a book seller. C<$searchstring> may be either a book seller
58 ID, or a string to look for in the book seller's name.
59
60 C<@results> is an array of references-to-hash, whose keys are the fields of of the
61 aqbooksellers table in the Koha database.
62
63 =cut
64
65 # FIXME: This function is badly named.  It should be something like 
66 # SearchBookSellersByName.  It is NOT a singular return value.
67
68 sub GetBookSeller($) {
69     my ($searchstring) = @_;
70     my $dbh = C4::Context->dbh;
71     my $query = "SELECT * FROM aqbooksellers WHERE name LIKE ?";
72     my $sth =$dbh->prepare($query);
73     $sth->execute( "$searchstring%" );
74     my @results;
75     # count how many baskets this bookseller has.
76     # if it has none, the bookseller can be deleted
77     my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
78     while ( my $data = $sth->fetchrow_hashref ) {
79         $sth2->execute($data->{id});
80         $data->{basketcount} = $sth2->fetchrow();
81         push( @results, $data );
82     }
83     $sth->finish;
84     return  @results ;
85 }
86
87
88 sub GetBookSellerFromId($) {
89         my ($id) = shift or return undef;
90         my $dbh = C4::Context->dbh();
91         my $query = "SELECT * FROM aqbooksellers WHERE id = ?";
92         my $sth =$dbh->prepare($query);
93         $sth->execute( $id );
94         if (my $data = $sth->fetchrow_hashref()){
95                 my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
96                 $sth2->execute($id);
97                 $data->{basketcount}=$sth2->fetchrow();
98                 return ($data);
99         }
100         return 0;
101 }
102 #-----------------------------------------------------------------#
103
104 =head2 GetBooksellersWithLateOrders
105
106 %results = &GetBooksellersWithLateOrders;
107
108 Searches for suppliers with late orders.
109
110 =cut
111
112 sub GetBooksellersWithLateOrders {
113     my ($delay,$branch) = @_;
114     my $dbh   = C4::Context->dbh;
115
116 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
117 # should be tested with other DBMs
118
119     my $strsth;
120     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
121     if ( $dbdriver eq "mysql" ) {
122         $strsth = "
123             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
124             FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
125             LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
126             WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
127                 AND (datereceived = '' OR datereceived IS NULL))
128         ";
129     }
130     else {
131         $strsth = "
132             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
133             FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
134             LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
135             WHERE (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
136                 AND (datereceived = '' OR datereceived IS NULL))
137         ";
138     }
139
140     my $sth = $dbh->prepare($strsth);
141     $sth->execute;
142     my %supplierlist;
143     while ( my ( $id, $name ) = $sth->fetchrow ) {
144         $supplierlist{$id} = $name;
145     }
146
147     return %supplierlist;
148 }
149
150 #--------------------------------------------------------------------#
151
152 =head2 AddBookseller
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 =cut
163
164 sub AddBookseller {
165     my ($data) = @_;
166     my $dbh = C4::Context->dbh;
167     my $query = "
168         INSERT INTO aqbooksellers
169             (
170                 name,      address1,      address2,   address3,      address4,
171                 postal,    phone,         fax,        url,           contact,
172                 contpos,   contphone,     contfax,    contaltphone,  contemail,
173                 contnotes, active,        listprice,  invoiceprice,  gstreg,
174                 listincgst,invoiceincgst, specialty,  discount,      invoicedisc,
175                 nocalc,    notes
176             )
177         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
178     ";
179     my $sth = $dbh->prepare($query);
180     $sth->execute(
181         $data->{'name'},         $data->{'address1'},
182         $data->{'address2'},     $data->{'address3'},
183         $data->{'address4'},     $data->{'postal'},
184         $data->{'phone'},        $data->{'fax'},
185         $data->{'url'},          $data->{'contact'},
186         $data->{'contpos'},      $data->{'contphone'},
187         $data->{'contfax'},      $data->{'contaltphone'},
188         $data->{'contemail'},    $data->{'contnotes'},
189         $data->{'active'},       $data->{'listprice'},
190         $data->{'invoiceprice'}, $data->{'gstreg'},
191         $data->{'listincgst'},   $data->{'invoiceincgst'},
192         $data->{'specialty'},    $data->{'discount'},
193         $data->{'invoicedisc'},  $data->{'nocalc'},
194         $data->{'notes'}
195     );
196
197     # return the id of this new supplier
198     $query = "
199         SELECT max(id)
200         FROM   aqbooksellers
201     ";
202     $sth = $dbh->prepare($query);
203     $sth->execute;
204     return scalar($sth->fetchrow);
205 }
206
207 #-----------------------------------------------------------------#
208
209 =head2 ModSupplier
210
211 &ModSupplier($bookseller);
212
213 Updates the information for a given bookseller. C<$bookseller> is a
214 reference-to-hash whose keys are the fields of the aqbooksellers table
215 in the Koha database. It must contain entries for all of the fields.
216 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
217
218 The easiest way to get all of the necessary fields is to look up a
219 book seller with C<&booksellers>, modify what's necessary, then call
220 C<&ModSupplier> with the result.
221
222 =cut
223
224 sub ModBookseller {
225     my ($data) = @_;
226     my $dbh    = C4::Context->dbh;
227     my $query = "
228         UPDATE aqbooksellers
229         SET name=?,address1=?,address2=?,address3=?,address4=?,
230             postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
231             contphone=?,contfax=?,contaltphone=?,contemail=?,
232             contnotes=?,active=?,listprice=?, invoiceprice=?,
233             gstreg=?, listincgst=?,invoiceincgst=?,
234             specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=?
235         WHERE id=?
236     ";
237     my $sth    = $dbh->prepare($query);
238     $sth->execute(
239         $data->{'name'},         $data->{'address1'},
240         $data->{'address2'},     $data->{'address3'},
241         $data->{'address4'},     $data->{'postal'},
242         $data->{'phone'},        $data->{'fax'},
243         $data->{'url'},          $data->{'contact'},
244         $data->{'contpos'},      $data->{'contphone'},
245         $data->{'contfax'},      $data->{'contaltphone'},
246         $data->{'contemail'},    $data->{'contnotes'},
247         $data->{'active'},       $data->{'listprice'},
248         $data->{'invoiceprice'}, $data->{'gstreg'},
249         $data->{'listincgst'},   $data->{'invoiceincgst'},
250         $data->{'specialty'},    $data->{'discount'},
251         $data->{'invoicedisc'},  $data->{'nocalc'},
252         $data->{'notes'},        $data->{'id'}
253     );
254     $sth->finish;
255 }
256
257 =head2 DelBookseller
258
259 &DelBookseller($booksellerid);
260
261 delete the supplier identified by $booksellerid
262 This sub can be called only if the supplier has no order.
263
264 =cut
265 sub DelBookseller {
266     my ($id) = @_;
267     my $dbh=C4::Context->dbh;
268     my $sth=$dbh->prepare("DELETE FROM aqbooksellers WHERE id=?");
269     $sth->execute($id);
270 }
271
272 1;
273
274 __END__
275
276 =head1 AUTHOR
277
278 Koha Developement team <info@koha.org>
279
280 =cut