Bug 6540 - Add defaultsql command line option
[koha.git] / C4 / Bookseller.pm
blob4ad42cb6f24b05d3a7df29b5b96dee6c00e7b2f7
1 package C4::Bookseller;
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 PTFS Europe
6 # This file is part of Koha.
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.
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.
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.
21 use strict;
22 use warnings;
24 use base qw( Exporter );
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
35 =head1 NAME
37 C4::Bookseller - Koha functions for dealing with booksellers.
39 =head1 SYNOPSIS
41 use C4::Bookseller;
43 =head1 DESCRIPTION
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.
49 =head1 FUNCTIONS
51 =head2 GetBookSeller
53 @results = GetBookSeller($searchstring);
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.
58 C<@results> is an array of hash_refs whose keys are the fields of of the
59 aqbooksellers table in the Koha database.
61 =cut
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';
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};
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 );
88 return $vendor;
91 #-----------------------------------------------------------------#
93 =head2 GetBooksellersWithLateOrders
95 %results = GetBooksellersWithLateOrders($delay);
97 Searches for suppliers with late orders.
99 =cut
101 sub GetBooksellersWithLateOrders {
102 my $delay = shift;
103 my $dbh = C4::Context->dbh;
105 # TODO delay should be verified
106 my $query_string =
107 "SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
108 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
109 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
110 WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
111 AND (datereceived = '' OR datereceived IS NULL))";
113 my $sth = $dbh->prepare($query_string);
114 $sth->execute;
115 my %supplierlist;
116 while ( my ( $id, $name ) = $sth->fetchrow ) {
117 $supplierlist{$id} = $name;
120 return %supplierlist;
123 #--------------------------------------------------------------------#
125 =head2 AddBookseller
127 $id = &AddBookseller($bookseller);
129 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
130 keys are the fields of the aqbooksellers table in the Koha database.
131 All fields must be present.
133 Returns the ID of the newly-created bookseller.
135 =cut
137 sub AddBookseller {
138 my ($data) = @_;
139 my $dbh = C4::Context->dbh;
140 my $query = q|
141 INSERT INTO aqbooksellers
143 name, address1, address2, address3, address4,
144 postal, phone, fax, url, contact,
145 contpos, contphone, contfax, contaltphone, contemail,
146 contnotes, active, listprice, invoiceprice, gstreg,
147 listincgst,invoiceincgst, gstrate, discount,
148 notes
150 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
152 my $sth = $dbh->prepare($query);
153 $sth->execute(
154 $data->{'name'}, $data->{'address1'},
155 $data->{'address2'}, $data->{'address3'},
156 $data->{'address4'}, $data->{'postal'},
157 $data->{'phone'}, $data->{'fax'},
158 $data->{'url'}, $data->{'contact'},
159 $data->{'contpos'}, $data->{'contphone'},
160 $data->{'contfax'}, $data->{'contaltphone'},
161 $data->{'contemail'}, $data->{'contnotes'},
162 $data->{'active'}, $data->{'listprice'},
163 $data->{'invoiceprice'}, $data->{'gstreg'},
164 $data->{'listincgst'}, $data->{'invoiceincgst'},
165 $data->{'gstrate'},
166 $data->{'discount'}, $data->{'notes'}
169 # return the id of this new supplier
170 return $dbh->{'mysql_insertid'};
173 #-----------------------------------------------------------------#
175 =head2 ModBookseller
177 ModBookseller($bookseller);
179 Updates the information for a given bookseller. C<$bookseller> is a
180 reference-to-hash whose keys are the fields of the aqbooksellers table
181 in the Koha database. It must contain entries for all of the fields.
182 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
184 The easiest way to get all of the necessary fields is to look up a
185 book seller with C<&GetBookseller>, modify what's necessary, then call
186 C<&ModBookseller> with the result.
188 =cut
190 sub ModBookseller {
191 my ($data) = @_;
192 my $dbh = C4::Context->dbh;
193 my $query = 'UPDATE aqbooksellers
194 SET name=?,address1=?,address2=?,address3=?,address4=?,
195 postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
196 contphone=?,contfax=?,contaltphone=?,contemail=?,
197 contnotes=?,active=?,listprice=?, invoiceprice=?,
198 gstreg=?,listincgst=?,invoiceincgst=?,
199 discount=?,notes=?,gstrate=?
200 WHERE id=?';
201 my $sth = $dbh->prepare($query);
202 $sth->execute(
203 $data->{'name'}, $data->{'address1'},
204 $data->{'address2'}, $data->{'address3'},
205 $data->{'address4'}, $data->{'postal'},
206 $data->{'phone'}, $data->{'fax'},
207 $data->{'url'}, $data->{'contact'},
208 $data->{'contpos'}, $data->{'contphone'},
209 $data->{'contfax'}, $data->{'contaltphone'},
210 $data->{'contemail'}, $data->{'contnotes'},
211 $data->{'active'}, $data->{'listprice'},
212 $data->{'invoiceprice'}, $data->{'gstreg'},
213 $data->{'listincgst'}, $data->{'invoiceincgst'},
214 $data->{'discount'}, $data->{'notes'},
215 $data->{'gstrate'}, $data->{'id'}
217 return;
220 =head2 DelBookseller
222 DelBookseller($booksellerid);
224 delete the supplier record identified by $booksellerid
225 This sub assumes it is called only if the supplier has no order.
227 =cut
229 sub DelBookseller {
230 my $id = shift;
231 my $dbh = C4::Context->dbh;
232 my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
233 $sth->execute($id);
234 return;
239 __END__
241 =head1 AUTHOR
243 Koha Development Team <http://koha-community.org/>
245 =cut