bug 8215: (followup) make sure C4::CourseReserves doesn't export anything
[koha.git] / C4 / Bookseller.pm
1 package C4::Bookseller;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 PTFS Europe
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use base qw( Exporter );
25
26 # set the version for version checking
27 our $VERSION   = 3.07.00.049;
28 our @EXPORT_OK = qw(
29   GetBookSeller GetBooksellersWithLateOrders GetBookSellerFromId
30   ModBookseller
31   DelBookseller
32   AddBookseller
33 );
34
35 =head1 NAME
36
37 C4::Bookseller - Koha functions for dealing with booksellers.
38
39 =head1 SYNOPSIS
40
41 use C4::Bookseller;
42
43 =head1 DESCRIPTION
44
45 The functions in this module deal with booksellers. They allow to
46 add a new bookseller, to modify it or to get some informations around
47 a bookseller.
48
49 =head1 FUNCTIONS
50
51 =head2 GetBookSeller
52
53 @results = GetBookSeller($searchstring);
54
55 Looks up a book seller. C<$searchstring> may be either a book seller
56 ID, or a string to look for in the book seller's name.
57
58 C<@results> is an array of hash_refs whose keys are the fields of of the
59 aqbooksellers table in the Koha database.
60
61 =cut
62
63 sub GetBookSeller {
64     my $searchstring = shift;
65     $searchstring = q{%} . $searchstring . q{%};
66     my $query = "
67         SELECT aqbooksellers.*, count(*) AS basketcount
68         FROM aqbooksellers
69         LEFT JOIN aqbasket ON aqbasket.booksellerid = aqbooksellers.id
70         WHERE name LIKE ? GROUP BY aqbooksellers.id ORDER BY name
71     ";
72
73     my $dbh           = C4::Context->dbh;
74     my $sth           = $dbh->prepare($query);
75     $sth->execute($searchstring);
76     my $resultset_ref = $sth->fetchall_arrayref( {} );
77     return @{$resultset_ref};
78 }
79
80 sub GetBookSellerFromId {
81     my $id = shift or return;
82     my $dbh = C4::Context->dbh;
83     my $vendor =
84       $dbh->selectrow_hashref( 'SELECT * FROM aqbooksellers WHERE id = ?',
85         {}, $id );
86     if ($vendor) {
87         ( $vendor->{basketcount} ) = $dbh->selectrow_array(
88             'SELECT count(*) FROM aqbasket where booksellerid = ?',
89             {}, $id );
90         ( $vendor->{subscriptioncount} ) = $dbh->selectrow_array(
91             'SELECT count(*) FROM subscription WHERE aqbooksellerid = ?',
92             {}, $id );
93     }
94     return $vendor;
95 }
96
97 #-----------------------------------------------------------------#
98
99 =head2 GetBooksellersWithLateOrders
100
101 %results = GetBooksellersWithLateOrders($delay);
102
103 Searches for suppliers with late orders.
104
105 =cut
106
107 sub GetBooksellersWithLateOrders {
108     my ( $delay, $branch, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;    # FIXME: Branch argument unused.
109     my $dbh = C4::Context->dbh;
110
111     # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
112     # should be tested with other DBMs
113
114     my $strsth;
115     my @query_params = ();
116     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
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.booksellerid = aqbooksellers.id
121         WHERE
122             ( datereceived = ''
123             OR datereceived IS NULL
124             OR aqorders.quantityreceived < aqorders.quantity
125             )
126             AND aqorders.rrp <> 0
127             AND aqorders.ecost <> 0
128             AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
129             AND aqbasket.closedate IS NOT NULL
130     ";
131     if ( defined $delay ) {
132         $strsth .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) ";
133         push @query_params, $delay;
134     }
135     if ( defined $estimateddeliverydatefrom ) {
136         $strsth .= '
137             AND aqbooksellers.deliverytime IS NOT NULL
138             AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
139         push @query_params, $estimateddeliverydatefrom;
140     }
141     if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) {
142         $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
143         push @query_params, $estimateddeliverydateto;
144     } elsif ( defined $estimateddeliverydatefrom ) {
145         $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
146     }
147
148     my $sth = $dbh->prepare($strsth);
149     $sth->execute( @query_params );
150     my %supplierlist;
151     while ( my ( $id, $name ) = $sth->fetchrow ) {
152         $supplierlist{$id} = $name;
153     }
154
155     return %supplierlist;
156 }
157
158 #--------------------------------------------------------------------#
159
160 =head2 AddBookseller
161
162 $id = &AddBookseller($bookseller);
163
164 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
165 keys are the fields of the aqbooksellers table in the Koha database.
166 All fields must be present.
167
168 Returns the ID of the newly-created bookseller.
169
170 =cut
171
172 sub AddBookseller {
173     my ($data) = @_;
174     my $dbh    = C4::Context->dbh;
175     my $query  = q|
176         INSERT INTO aqbooksellers
177             (
178                 name,      address1,      address2,   address3,      address4,
179                 postal,    phone,         accountnumber,   fax,      url,           
180                 contact,
181                 contpos,   contphone,     contfax,    contaltphone,  contemail,
182                 contnotes, active,        listprice,  invoiceprice,  gstreg,
183                 listincgst,invoiceincgst, gstrate,    discount,
184                 notes
185             )
186         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
187       ;
188     my $sth = $dbh->prepare($query);
189     $sth->execute(
190         $data->{'name'},         $data->{'address1'},
191         $data->{'address2'},     $data->{'address3'},
192         $data->{'address4'},     $data->{'postal'},
193         $data->{'phone'},        $data->{'accountnumber'},
194         $data->{'fax'},
195         $data->{'url'},          $data->{'contact'},
196         $data->{'contpos'},      $data->{'contphone'},
197         $data->{'contfax'},      $data->{'contaltphone'},
198         $data->{'contemail'},    $data->{'contnotes'},
199         $data->{'active'},       $data->{'listprice'},
200         $data->{'invoiceprice'}, $data->{'gstreg'},
201         $data->{'listincgst'},   $data->{'invoiceincgst'},
202         $data->{'gstrate'},
203         $data->{'discount'},     $data->{'notes'}
204     );
205
206     # return the id of this new supplier
207     return $dbh->{'mysql_insertid'};
208 }
209
210 #-----------------------------------------------------------------#
211
212 =head2 ModBookseller
213
214 ModBookseller($bookseller);
215
216 Updates the information for a given bookseller. C<$bookseller> is a
217 reference-to-hash whose keys are the fields of the aqbooksellers table
218 in the Koha database. It must contain entries for all of the fields.
219 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
220
221 The easiest way to get all of the necessary fields is to look up a
222 book seller with C<&GetBookseller>, modify what's necessary, then call
223 C<&ModBookseller> with the result.
224
225 =cut
226
227 sub ModBookseller {
228     my ($data) = @_;
229     my $dbh    = C4::Context->dbh;
230     my $query  = 'UPDATE aqbooksellers
231         SET name=?,address1=?,address2=?,address3=?,address4=?,
232             postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?,
233             contphone=?,contfax=?,contaltphone=?,contemail=?,
234             contnotes=?,active=?,listprice=?, invoiceprice=?,
235             gstreg=?,listincgst=?,invoiceincgst=?,
236             discount=?,notes=?,gstrate=?,deliverytime=?
237         WHERE id=?';
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->{'accountnumber'},
244         $data->{'fax'},
245         $data->{'url'},          $data->{'contact'},
246         $data->{'contpos'},      $data->{'contphone'},
247         $data->{'contfax'},      $data->{'contaltphone'},
248         $data->{'contemail'},    $data->{'contnotes'},
249         $data->{'active'},       $data->{'listprice'},
250         $data->{'invoiceprice'}, $data->{'gstreg'},
251         $data->{'listincgst'},   $data->{'invoiceincgst'},
252         $data->{'discount'},     $data->{'notes'},
253         $data->{'gstrate'},
254         $data->{deliverytime},
255         $data->{'id'}
256     );
257     return;
258 }
259
260 =head2 DelBookseller
261
262 DelBookseller($booksellerid);
263
264 delete the supplier record identified by $booksellerid
265 This sub assumes it is called only if the supplier has no order.
266
267 =cut
268
269 sub DelBookseller {
270     my $id  = shift;
271     my $dbh = C4::Context->dbh;
272     my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
273     $sth->execute($id);
274     return;
275 }
276
277 1;
278
279 __END__
280
281 =head1 AUTHOR
282
283 Koha Development Team <http://koha-community.org/>
284
285 =cut