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