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