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