File: | C4/Bookseller.pm |
Coverage: | 19.0% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 4 4 4 | 46412 40 170 | use strict; | |||
22 | 4 4 4 | 41 29 205 | use warnings; | |||
23 | ||||||
24 | 4 4 4 | 38 51 4323 | 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 - 61 | =head1 NAME C4::Bookseller - Koha functions for dealing with booksellers. =head1 SYNOPSIS use C4::Bookseller; =head1 DESCRIPTION The functions in this module deal with booksellers. They allow to add a new bookseller, to modify it or to get some informations around a bookseller. =head1 FUNCTIONS =head2 GetBookSeller @results = GetBookSeller($searchstring); Looks up a book seller. C<$searchstring> may be either a book seller ID, or a string to look for in the book seller's name. C<@results> is an array of hash_refs whose keys are the fields of of the aqbooksellers table in the Koha database. =cut | |||||
62 | ||||||
63 | sub GetBookSeller { | |||||
64 | 0 | my $searchstring = shift; | ||||
65 | 0 | $searchstring = q{%} . $searchstring . q{%}; | ||||
66 | 0 | 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 | 0 | my $dbh = C4::Context->dbh; | ||||
71 | 0 | my $sth = $dbh->prepare($query); | ||||
72 | 0 | $sth->execute($searchstring); | ||||
73 | 0 | my $resultset_ref = $sth->fetchall_arrayref( {} ); | ||||
74 | 0 0 | return @{$resultset_ref}; | ||||
75 | } | |||||
76 | ||||||
77 | sub GetBookSellerFromId { | |||||
78 | 0 | my $id = shift or return; | ||||
79 | 0 | my $dbh = C4::Context->dbh; | ||||
80 | 0 | my $vendor = | ||||
81 | $dbh->selectrow_hashref( 'SELECT * FROM aqbooksellers WHERE id = ?', | |||||
82 | {}, $id ); | |||||
83 | 0 | if ($vendor) { | ||||
84 | 0 | ( $vendor->{basketcount} ) = $dbh->selectrow_array( | ||||
85 | 'SELECT count(*) FROM aqbasket where booksellerid = ?', | |||||
86 | {}, $id ); | |||||
87 | } | |||||
88 | 0 | return $vendor; | ||||
89 | } | |||||
90 | ||||||
91 | #-----------------------------------------------------------------# | |||||
92 | ||||||
93 - 99 | =head2 GetBooksellersWithLateOrders %results = GetBooksellersWithLateOrders($delay); Searches for suppliers with late orders. =cut | |||||
100 | ||||||
101 | sub GetBooksellersWithLateOrders { | |||||
102 | 0 | my $delay = shift; | ||||
103 | 0 | my $dbh = C4::Context->dbh; | ||||
104 | ||||||
105 | # TODO delay should be verified | |||||
106 | 0 | 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))"; | |||||
112 | ||||||
113 | 0 | my $sth = $dbh->prepare($query_string); | ||||
114 | 0 | $sth->execute; | ||||
115 | 0 | my %supplierlist; | ||||
116 | 0 | while ( my ( $id, $name ) = $sth->fetchrow ) { | ||||
117 | 0 | $supplierlist{$id} = $name; | ||||
118 | } | |||||
119 | ||||||
120 | 0 | return %supplierlist; | ||||
121 | } | |||||
122 | ||||||
123 | #--------------------------------------------------------------------# | |||||
124 | ||||||
125 - 135 | =head2 AddBookseller $id = &AddBookseller($bookseller); Creates a new bookseller. C<$bookseller> is a reference-to-hash whose keys are the fields of the aqbooksellers table in the Koha database. All fields must be present. Returns the ID of the newly-created bookseller. =cut | |||||
136 | ||||||
137 | sub AddBookseller { | |||||
138 | 0 | my ($data) = @_; | ||||
139 | 0 | my $dbh = C4::Context->dbh; | ||||
140 | 0 | my $query = q| | ||||
141 | INSERT INTO aqbooksellers | |||||
142 | ( | |||||
143 | name, address1, address2, address3, address4, | |||||
144 | postal, phone, accountnumber, fax, url, | |||||
145 | contact, | |||||
146 | contpos, contphone, contfax, contaltphone, contemail, | |||||
147 | contnotes, active, listprice, invoiceprice, gstreg, | |||||
148 | listincgst,invoiceincgst, gstrate, discount, | |||||
149 | notes | |||||
150 | ) | |||||
151 | VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | | |||||
152 | ; | |||||
153 | 0 | my $sth = $dbh->prepare($query); | ||||
154 | 0 | $sth->execute( | ||||
155 | $data->{'name'}, $data->{'address1'}, | |||||
156 | $data->{'address2'}, $data->{'address3'}, | |||||
157 | $data->{'address4'}, $data->{'postal'}, | |||||
158 | $data->{'phone'}, $data->{'accountnumber'}, | |||||
159 | $data->{'fax'}, | |||||
160 | $data->{'url'}, $data->{'contact'}, | |||||
161 | $data->{'contpos'}, $data->{'contphone'}, | |||||
162 | $data->{'contfax'}, $data->{'contaltphone'}, | |||||
163 | $data->{'contemail'}, $data->{'contnotes'}, | |||||
164 | $data->{'active'}, $data->{'listprice'}, | |||||
165 | $data->{'invoiceprice'}, $data->{'gstreg'}, | |||||
166 | $data->{'listincgst'}, $data->{'invoiceincgst'}, | |||||
167 | $data->{'gstrate'}, | |||||
168 | $data->{'discount'}, $data->{'notes'} | |||||
169 | ); | |||||
170 | ||||||
171 | # return the id of this new supplier | |||||
172 | 0 | return $dbh->{'mysql_insertid'}; | ||||
173 | } | |||||
174 | ||||||
175 | #-----------------------------------------------------------------# | |||||
176 | ||||||
177 - 190 | =head2 ModBookseller ModBookseller($bookseller); Updates the information for a given bookseller. C<$bookseller> is a reference-to-hash whose keys are the fields of the aqbooksellers table in the Koha database. It must contain entries for all of the fields. The entry to modify is determined by C<$bookseller-E<gt>{id}>. The easiest way to get all of the necessary fields is to look up a book seller with C<&GetBookseller>, modify what's necessary, then call C<&ModBookseller> with the result. =cut | |||||
191 | ||||||
192 | sub ModBookseller { | |||||
193 | 0 | my ($data) = @_; | ||||
194 | 0 | my $dbh = C4::Context->dbh; | ||||
195 | 0 | my $query = 'UPDATE aqbooksellers | ||||
196 | SET name=?,address1=?,address2=?,address3=?,address4=?, | |||||
197 | postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?, | |||||
198 | contphone=?,contfax=?,contaltphone=?,contemail=?, | |||||
199 | contnotes=?,active=?,listprice=?, invoiceprice=?, | |||||
200 | gstreg=?,listincgst=?,invoiceincgst=?, | |||||
201 | discount=?,notes=?,gstrate=? | |||||
202 | WHERE id=?'; | |||||
203 | 0 | my $sth = $dbh->prepare($query); | ||||
204 | 0 | $sth->execute( | ||||
205 | $data->{'name'}, $data->{'address1'}, | |||||
206 | $data->{'address2'}, $data->{'address3'}, | |||||
207 | $data->{'address4'}, $data->{'postal'}, | |||||
208 | $data->{'phone'}, $data->{'accountnumber'}, | |||||
209 | $data->{'fax'}, | |||||
210 | $data->{'url'}, $data->{'contact'}, | |||||
211 | $data->{'contpos'}, $data->{'contphone'}, | |||||
212 | $data->{'contfax'}, $data->{'contaltphone'}, | |||||
213 | $data->{'contemail'}, $data->{'contnotes'}, | |||||
214 | $data->{'active'}, $data->{'listprice'}, | |||||
215 | $data->{'invoiceprice'}, $data->{'gstreg'}, | |||||
216 | $data->{'listincgst'}, $data->{'invoiceincgst'}, | |||||
217 | $data->{'discount'}, $data->{'notes'}, | |||||
218 | $data->{'gstrate'}, $data->{'id'} | |||||
219 | ); | |||||
220 | 0 | return; | ||||
221 | } | |||||
222 | ||||||
223 - 230 | =head2 DelBookseller DelBookseller($booksellerid); delete the supplier record identified by $booksellerid This sub assumes it is called only if the supplier has no order. =cut | |||||
231 | ||||||
232 | sub DelBookseller { | |||||
233 | 0 | my $id = shift; | ||||
234 | 0 | my $dbh = C4::Context->dbh; | ||||
235 | 0 | my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?'); | ||||
236 | 0 | $sth->execute($id); | ||||
237 | 0 | return; | ||||
238 | } | |||||
239 | ||||||
240 | 1; | |||||
241 |