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