Bug 14685: Fix set guarantor for a child or professional accounts
[koha.git] / C4 / Bookseller.pm
blobe37eab77420b13d8a7ad6f7faac34608f7f905d1
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>.
21 use strict;
22 use warnings;
24 use base qw( Exporter );
26 use C4::Bookseller::Contact;
28 # set the version for version checking
29 our $VERSION = 3.07.00.049;
30 our @EXPORT_OK = qw(
31 GetBooksellersWithLateOrders
32 ModBookseller
33 DelBookseller
34 AddBookseller
37 =head1 NAME
39 C4::Bookseller - Koha functions for dealing with booksellers.
41 =head1 SYNOPSIS
43 use C4::Bookseller;
45 =head1 DESCRIPTION
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
49 a bookseller.
51 =head1 FUNCTIONS
53 =head2 GetBooksellersWithLateOrders
55 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
57 Searches for suppliers with late orders.
59 =cut
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
68 my $query;
69 my @query_params = ();
70 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
71 $query = "
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
75 WHERE
76 ( datereceived = ''
77 OR datereceived IS NULL
78 OR aqorders.quantityreceived < aqorders.quantity
80 AND aqorders.rrp <> 0
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';
90 return;
92 if ( defined $estimateddeliverydatefrom ) {
93 $query .= '
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;
99 } else {
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 );
110 my %supplierlist;
111 while ( my ( $id, $name ) = $sth->fetchrow ) {
112 $supplierlist{$id} = $name;
115 return %supplierlist;
118 #--------------------------------------------------------------------#
120 =head2 AddBookseller
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.
130 =cut
132 sub AddBookseller {
133 my ($data, $contacts) = @_;
134 my $dbh = C4::Context->dbh;
135 my $query = q|
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,
142 deliverytime
144 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
146 my $sth = $dbh->prepare($query);
147 $sth->execute(
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);
167 $contact->save();
170 return $id;
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<Koha::Acquisition::Bookseller>, modify what's necessary, then call
186 C<&ModBookseller> with the result.
188 =cut
190 sub ModBookseller {
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=?
200 WHERE id=?';
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},
213 $data->{'id'}
215 $contacts ||= $data->{'contacts'};
216 my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
217 my @contactparams = ($data->{'id'});
218 if ($contacts) {
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'});
223 $contact->save();
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);
232 return $cnt;
235 =head2 DelBookseller
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.
242 =cut
244 sub DelBookseller {
245 my $id = shift;
246 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
248 return $sth->execute($id);
253 __END__
255 =head1 AUTHOR
257 Koha Development Team <http://koha-community.org/>
259 =cut