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