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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use base qw( Exporter );
26 use C4::Bookseller::Contact;
28 # set the version for version checking
29 our $VERSION = 3.07.00.049;
31 GetBooksellersWithLateOrders
39 C4::Bookseller - Koha functions for dealing with booksellers.
47 The functions in this module deal with booksellers. They allow to
48 add a new bookseller, to modify it or to get some informations around
53 =head2 GetBooksellersWithLateOrders
55 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
57 Searches for suppliers with late orders.
61 sub GetBooksellersWithLateOrders {
62 my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
63 my $dbh = C4::Context->dbh;
65 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
66 # should be tested with other DBMs
69 my @query_params = ();
70 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
72 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
73 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
74 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
77 OR datereceived IS NULL
78 OR aqorders.quantityreceived < aqorders.quantity
81 AND aqorders.ecost <> 0
82 AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
83 AND aqbasket.closedate IS NOT NULL
85 if ( defined $delay && $delay >= 0 ) {
86 $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
87 push @query_params, $delay;
88 } elsif ( $delay && $delay < 0 ){
89 warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
92 if ( defined $estimateddeliverydatefrom ) {
94 AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
95 push @query_params, $estimateddeliverydatefrom;
96 if ( defined $estimateddeliverydateto ) {
97 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
98 push @query_params, $estimateddeliverydateto;
100 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
103 if ( defined $estimateddeliverydateto ) {
104 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
105 push @query_params, $estimateddeliverydateto;
108 my $sth = $dbh->prepare($query);
109 $sth->execute( @query_params );
111 while ( my ( $id, $name ) = $sth->fetchrow ) {
112 $supplierlist{$id} = $name;
115 return %supplierlist;
118 #--------------------------------------------------------------------#
122 $id = &AddBookseller($bookseller);
124 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
125 keys are the fields of the aqbooksellers table in the Koha database.
126 All fields must be present.
128 Returns the ID of the newly-created bookseller.
133 my ($data, $contacts) = @_;
134 my $dbh = C4::Context->dbh;
136 INSERT INTO aqbooksellers
138 name, address1, address2, address3, address4,
139 postal, phone, accountnumber,fax, url,
140 active, listprice, invoiceprice, gstreg,
141 listincgst,invoiceincgst, gstrate, discount, notes,
144 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
146 my $sth = $dbh->prepare($query);
148 $data->{'name'}, $data->{'address1'},
149 $data->{'address2'}, $data->{'address3'},
150 $data->{'address4'}, $data->{'postal'},
151 $data->{'phone'}, $data->{'accountnumber'},
152 $data->{'fax'}, $data->{'url'},
153 $data->{'active'}, $data->{'listprice'},
154 $data->{'invoiceprice'}, $data->{'gstreg'},
155 $data->{'listincgst'}, $data->{'invoiceincgst'},
156 $data->{'gstrate'}, $data->{'discount'},
157 $data->{notes}, $data->{deliverytime},
160 # return the id of this new supplier
161 my $id = $dbh->{'mysql_insertid'};
162 if ($id && $contacts) {
163 foreach my $contact (@$contacts) {
164 $contact = C4::Bookseller::Contact->new( $contact )
165 unless ref $contacts eq 'C4::Bookseller::Contact';
166 $contact->bookseller($id);
173 #-----------------------------------------------------------------#
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<Koha::Acquisition::Bookseller>, modify what's necessary, then call
186 C<&ModBookseller> with the result.
191 my ($data, $contacts) = @_;
192 my $dbh = C4::Context->dbh;
193 return unless $data->{'id'};
194 my $query = 'UPDATE aqbooksellers
195 SET name=?,address1=?,address2=?,address3=?,address4=?,
196 postal=?,phone=?,accountnumber=?,fax=?,url=?,
197 active=?,listprice=?, invoiceprice=?,
198 gstreg=?,listincgst=?,invoiceincgst=?,
199 discount=?,notes=?,gstrate=?,deliverytime=?
201 my $sth = $dbh->prepare($query);
202 my $cnt = $sth->execute(
203 $data->{'name'}, $data->{'address1'},
204 $data->{'address2'}, $data->{'address3'},
205 $data->{'address4'}, $data->{'postal'},
206 $data->{'phone'}, $data->{'accountnumber'},
207 $data->{'fax'}, $data->{'url'},
208 $data->{'active'}, $data->{'listprice'},
209 $data->{'invoiceprice'}, $data->{'gstreg'},
210 $data->{'listincgst'}, $data->{'invoiceincgst'},
211 $data->{'discount'}, $data->{'notes'},
212 $data->{'gstrate'}, $data->{deliverytime},
215 $contacts ||= $data->{'contacts'};
216 my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
217 my @contactparams = ($data->{'id'});
219 foreach my $contact (@$contacts) {
220 $contact = C4::Bookseller::Contact->new( $contact )
221 unless ref $contacts eq 'C4::Bookseller::Contact';
222 $contact->bookseller($data->{'id'});
224 push @contactparams, $contact->id if $contact->id;
226 if ($#contactparams > 0) {
227 $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
230 $sth = $dbh->prepare($contactquery);
231 $sth->execute(@contactparams);
237 DelBookseller($booksellerid);
239 delete the supplier record identified by $booksellerid
240 This sub assumes it is called only if the supplier has no order.
246 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
248 return $sth->execute($id);
257 Koha Development Team <http://koha-community.org/>