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;
29 GetBooksellersWithLateOrders
37 C4::Bookseller - Koha functions for dealing with booksellers.
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
51 =head2 GetBooksellersWithLateOrders
53 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
55 Searches for suppliers with late orders.
59 sub GetBooksellersWithLateOrders {
60 my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
61 my $dbh = C4::Context->dbh;
63 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
64 # should be tested with other DBMs
67 my @query_params = ();
68 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
70 SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
71 FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
72 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
75 OR datereceived IS NULL
76 OR aqorders.quantityreceived < aqorders.quantity
78 AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
79 AND aqbasket.closedate IS NOT NULL
81 if ( defined $delay && $delay >= 0 ) {
82 $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
83 push @query_params, $delay;
84 } elsif ( $delay && $delay < 0 ){
85 warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
88 if ( defined $estimateddeliverydatefrom ) {
90 AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
91 push @query_params, $estimateddeliverydatefrom;
92 if ( defined $estimateddeliverydateto ) {
93 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
94 push @query_params, $estimateddeliverydateto;
96 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
99 if ( defined $estimateddeliverydateto ) {
100 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
101 push @query_params, $estimateddeliverydateto;
104 my $sth = $dbh->prepare($query);
105 $sth->execute( @query_params );
107 while ( my ( $id, $name ) = $sth->fetchrow ) {
108 $supplierlist{$id} = $name;
111 return %supplierlist;
114 #--------------------------------------------------------------------#
118 $id = &AddBookseller($bookseller);
120 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
121 keys are the fields of the aqbooksellers table in the Koha database.
122 All fields must be present.
124 Returns the ID of the newly-created bookseller.
129 my ($data, $contacts) = @_;
130 my $dbh = C4::Context->dbh;
132 INSERT INTO aqbooksellers
134 name, address1, address2, address3, address4,
135 postal, phone, accountnumber,fax, url,
136 active, listprice, invoiceprice, gstreg,
137 listincgst,invoiceincgst, gstrate, discount, notes,
140 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
142 my $sth = $dbh->prepare($query);
144 $data->{'name'}, $data->{'address1'},
145 $data->{'address2'}, $data->{'address3'},
146 $data->{'address4'}, $data->{'postal'},
147 $data->{'phone'}, $data->{'accountnumber'},
148 $data->{'fax'}, $data->{'url'},
149 $data->{'active'}, $data->{'listprice'},
150 $data->{'invoiceprice'}, $data->{'gstreg'},
151 $data->{'listincgst'}, $data->{'invoiceincgst'},
152 $data->{'gstrate'}, $data->{'discount'},
153 $data->{notes}, $data->{deliverytime},
156 # return the id of this new supplier
157 my $id = $dbh->{'mysql_insertid'};
158 if ($id && $contacts) {
159 foreach my $contact (@$contacts) {
160 $contact = C4::Bookseller::Contact->new( $contact )
161 unless ref $contacts eq 'C4::Bookseller::Contact';
162 $contact->bookseller($id);
169 #-----------------------------------------------------------------#
173 ModBookseller($bookseller);
175 Updates the information for a given bookseller. C<$bookseller> is a
176 reference-to-hash whose keys are the fields of the aqbooksellers table
177 in the Koha database. It must contain entries for all of the fields.
178 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
180 The easiest way to get all of the necessary fields is to look up a
181 book seller with C<Koha::Acquisition::Bookseller>, modify what's necessary, then call
182 C<&ModBookseller> with the result.
187 my ($data, $contacts) = @_;
188 my $dbh = C4::Context->dbh;
189 return unless $data->{'id'};
190 my $query = 'UPDATE aqbooksellers
191 SET name=?,address1=?,address2=?,address3=?,address4=?,
192 postal=?,phone=?,accountnumber=?,fax=?,url=?,
193 active=?,listprice=?, invoiceprice=?,
194 gstreg=?,listincgst=?,invoiceincgst=?,
195 discount=?,notes=?,gstrate=?,deliverytime=?
197 my $sth = $dbh->prepare($query);
198 my $cnt = $sth->execute(
199 $data->{'name'}, $data->{'address1'},
200 $data->{'address2'}, $data->{'address3'},
201 $data->{'address4'}, $data->{'postal'},
202 $data->{'phone'}, $data->{'accountnumber'},
203 $data->{'fax'}, $data->{'url'},
204 $data->{'active'}, $data->{'listprice'},
205 $data->{'invoiceprice'}, $data->{'gstreg'},
206 $data->{'listincgst'}, $data->{'invoiceincgst'},
207 $data->{'discount'}, $data->{'notes'},
208 $data->{'gstrate'}, $data->{deliverytime},
211 $contacts ||= $data->{'contacts'};
212 my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
213 my @contactparams = ($data->{'id'});
215 foreach my $contact (@$contacts) {
216 $contact = C4::Bookseller::Contact->new( $contact )
217 unless ref $contacts eq 'C4::Bookseller::Contact';
218 $contact->bookseller($data->{'id'});
220 push @contactparams, $contact->id if $contact->id;
222 if ($#contactparams > 0) {
223 $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
226 $sth = $dbh->prepare($contactquery);
227 $sth->execute(@contactparams);
233 DelBookseller($booksellerid);
235 delete the supplier record identified by $booksellerid
236 This sub assumes it is called only if the supplier has no order.
242 my $dbh = C4::Context->dbh;
243 my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
244 return $sth->execute($id);
253 Koha Development Team <http://koha-community.org/>