Bug 5549 : Let Timestamp do the right thing if passed a DateTime
[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 = shift;
106     my $dbh   = C4::Context->dbh;
107
108     # TODO delay should be verified
109     my $query_string =
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     my $sth = $dbh->prepare($query_string);
117     $sth->execute;
118     my %supplierlist;
119     while ( my ( $id, $name ) = $sth->fetchrow ) {
120         $supplierlist{$id} = $name;
121     }
122
123     return %supplierlist;
124 }
125
126 #--------------------------------------------------------------------#
127
128 =head2 AddBookseller
129
130 $id = &AddBookseller($bookseller);
131
132 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
133 keys are the fields of the aqbooksellers table in the Koha database.
134 All fields must be present.
135
136 Returns the ID of the newly-created bookseller.
137
138 =cut
139
140 sub AddBookseller {
141     my ($data) = @_;
142     my $dbh    = C4::Context->dbh;
143     my $query  = q|
144         INSERT INTO aqbooksellers
145             (
146                 name,      address1,      address2,   address3,      address4,
147                 postal,    phone,         accountnumber,   fax,      url,           
148                 contact,
149                 contpos,   contphone,     contfax,    contaltphone,  contemail,
150                 contnotes, active,        listprice,  invoiceprice,  gstreg,
151                 listincgst,invoiceincgst, gstrate,    discount,
152                 notes
153             )
154         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
155       ;
156     my $sth = $dbh->prepare($query);
157     $sth->execute(
158         $data->{'name'},         $data->{'address1'},
159         $data->{'address2'},     $data->{'address3'},
160         $data->{'address4'},     $data->{'postal'},
161         $data->{'phone'},        $data->{'accountnumber'},
162         $data->{'fax'},
163         $data->{'url'},          $data->{'contact'},
164         $data->{'contpos'},      $data->{'contphone'},
165         $data->{'contfax'},      $data->{'contaltphone'},
166         $data->{'contemail'},    $data->{'contnotes'},
167         $data->{'active'},       $data->{'listprice'},
168         $data->{'invoiceprice'}, $data->{'gstreg'},
169         $data->{'listincgst'},   $data->{'invoiceincgst'},
170         $data->{'gstrate'},
171         $data->{'discount'},     $data->{'notes'}
172     );
173
174     # return the id of this new supplier
175     return $dbh->{'mysql_insertid'};
176 }
177
178 #-----------------------------------------------------------------#
179
180 =head2 ModBookseller
181
182 ModBookseller($bookseller);
183
184 Updates the information for a given bookseller. C<$bookseller> is a
185 reference-to-hash whose keys are the fields of the aqbooksellers table
186 in the Koha database. It must contain entries for all of the fields.
187 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
188
189 The easiest way to get all of the necessary fields is to look up a
190 book seller with C<&GetBookseller>, modify what's necessary, then call
191 C<&ModBookseller> with the result.
192
193 =cut
194
195 sub ModBookseller {
196     my ($data) = @_;
197     my $dbh    = C4::Context->dbh;
198     my $query  = 'UPDATE aqbooksellers
199         SET name=?,address1=?,address2=?,address3=?,address4=?,
200             postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?,
201             contphone=?,contfax=?,contaltphone=?,contemail=?,
202             contnotes=?,active=?,listprice=?, invoiceprice=?,
203             gstreg=?,listincgst=?,invoiceincgst=?,
204             discount=?,notes=?,gstrate=?
205         WHERE id=?';
206     my $sth = $dbh->prepare($query);
207     $sth->execute(
208         $data->{'name'},         $data->{'address1'},
209         $data->{'address2'},     $data->{'address3'},
210         $data->{'address4'},     $data->{'postal'},
211         $data->{'phone'},        $data->{'accountnumber'},
212         $data->{'fax'},
213         $data->{'url'},          $data->{'contact'},
214         $data->{'contpos'},      $data->{'contphone'},
215         $data->{'contfax'},      $data->{'contaltphone'},
216         $data->{'contemail'},    $data->{'contnotes'},
217         $data->{'active'},       $data->{'listprice'},
218         $data->{'invoiceprice'}, $data->{'gstreg'},
219         $data->{'listincgst'},   $data->{'invoiceincgst'},
220         $data->{'discount'},     $data->{'notes'},
221         $data->{'gstrate'},      $data->{'id'}
222     );
223     return;
224 }
225
226 =head2 DelBookseller
227
228 DelBookseller($booksellerid);
229
230 delete the supplier record identified by $booksellerid
231 This sub assumes it is called only if the supplier has no order.
232
233 =cut
234
235 sub DelBookseller {
236     my $id  = shift;
237     my $dbh = C4::Context->dbh;
238     my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
239     $sth->execute($id);
240     return;
241 }
242
243 1;
244
245 __END__
246
247 =head1 AUTHOR
248
249 Koha Development Team <http://koha-community.org/>
250
251 =cut