Bug 14168: (followup) require WWW::YouTube::Download only when syspref enabled
[koha.git] / C4 / Bookseller.pm
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
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.
12 #
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.
17 #
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>.
20
21 use strict;
22 use warnings;
23
24 use base qw( Exporter );
25
26 use C4::Bookseller::Contact;
27
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
35 );
36
37 =head1 NAME
38
39 C4::Bookseller - Koha functions for dealing with booksellers.
40
41 =head1 SYNOPSIS
42
43 use C4::Bookseller;
44
45 =head1 DESCRIPTION
46
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.
50
51 =head1 FUNCTIONS
52
53 =head2 GetBooksellersWithLateOrders
54
55 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
56
57 Searches for suppliers with late orders.
58
59 =cut
60
61 sub GetBooksellersWithLateOrders {
62     my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
63     my $dbh = C4::Context->dbh;
64
65     # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
66     # should be tested with other DBMs
67
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
79             )
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
84     ";
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;
91     }
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)';
101             }
102     }
103     if ( defined $estimateddeliverydateto ) {
104         $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
105         push @query_params, $estimateddeliverydateto;
106     }
107
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;
113     }
114
115     return %supplierlist;
116 }
117
118 #--------------------------------------------------------------------#
119
120 =head2 AddBookseller
121
122 $id = &AddBookseller($bookseller);
123
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.
127
128 Returns the ID of the newly-created bookseller.
129
130 =cut
131
132 sub AddBookseller {
133     my ($data, $contacts) = @_;
134     my $dbh    = C4::Context->dbh;
135     my $query = q|
136         INSERT INTO aqbooksellers
137             (
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
143             )
144         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
145       ;
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},
158     );
159
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();
168         }
169     }
170     return $id;
171 }
172
173 #-----------------------------------------------------------------#
174
175 =head2 ModBookseller
176
177 ModBookseller($bookseller);
178
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}>.
183
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.
187
188 =cut
189
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'}
214     );
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;
225         }
226         if ($#contactparams > 0) {
227             $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
228         }
229     }
230     $sth = $dbh->prepare($contactquery);
231     $sth->execute(@contactparams);
232     return $cnt;
233 }
234
235 =head2 DelBookseller
236
237 DelBookseller($booksellerid);
238
239 delete the supplier record identified by $booksellerid
240 This sub assumes it is called only if the supplier has no order.
241
242 =cut
243
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);
249 }
250
251 1;
252
253 __END__
254
255 =head1 AUTHOR
256
257 Koha Development Team <http://koha-community.org/>
258
259 =cut