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