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