Revert "Bug 16749: Adjustments for koha-plack"
[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 our @EXPORT_OK = qw(
29   GetBooksellersWithLateOrders
30   ModBookseller
31   DelBookseller
32   AddBookseller
33 );
34
35 =head1 NAME
36
37 C4::Bookseller - Koha functions for dealing with booksellers.
38
39 =head1 SYNOPSIS
40
41 use C4::Bookseller;
42
43 =head1 DESCRIPTION
44
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
47 a bookseller.
48
49 =head1 FUNCTIONS
50
51 =head2 GetBooksellersWithLateOrders
52
53 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
54
55 Searches for suppliers with late orders.
56
57 =cut
58
59 sub GetBooksellersWithLateOrders {
60     my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
61     my $dbh = C4::Context->dbh;
62
63     # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
64     # should be tested with other DBMs
65
66     my $query;
67     my @query_params = ();
68     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
69     $query = "
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
73         WHERE
74             ( datereceived = ''
75             OR datereceived IS NULL
76             OR aqorders.quantityreceived < aqorders.quantity
77             )
78             AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
79             AND aqbasket.closedate IS NOT NULL
80     ";
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';
86         return;
87     }
88     if ( defined $estimateddeliverydatefrom ) {
89         $query .= '
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;
95             } else {
96                     $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
97             }
98     }
99     if ( defined $estimateddeliverydateto ) {
100         $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
101         push @query_params, $estimateddeliverydateto;
102     }
103
104     my $sth = $dbh->prepare($query);
105     $sth->execute( @query_params );
106     my %supplierlist;
107     while ( my ( $id, $name ) = $sth->fetchrow ) {
108         $supplierlist{$id} = $name;
109     }
110
111     return %supplierlist;
112 }
113
114 #--------------------------------------------------------------------#
115
116 =head2 AddBookseller
117
118 $id = &AddBookseller($bookseller);
119
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.
123
124 Returns the ID of the newly-created bookseller.
125
126 =cut
127
128 sub AddBookseller {
129     my ($data, $contacts) = @_;
130     my $dbh    = C4::Context->dbh;
131     my $query = q|
132         INSERT INTO aqbooksellers
133             (
134                 name,      address1,      address2,     address3, address4,
135                 postal,    phone,         accountnumber,fax,      url,
136                 active,    listprice,     invoiceprice, gstreg,
137                 listincgst,invoiceincgst, gstrate,      discount, notes,
138                 deliverytime
139             )
140         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
141       ;
142     my $sth = $dbh->prepare($query);
143     $sth->execute(
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},
154     );
155
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);
163             $contact->save();
164         }
165     }
166     return $id;
167 }
168
169 #-----------------------------------------------------------------#
170
171 =head2 ModBookseller
172
173 ModBookseller($bookseller);
174
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}>.
179
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.
183
184 =cut
185
186 sub ModBookseller {
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=?
196         WHERE id=?';
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},
209         $data->{'id'}
210     );
211     $contacts ||= $data->{'contacts'};
212     my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
213     my @contactparams = ($data->{'id'});
214     if ($contacts) {
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'});
219             $contact->save();
220             push @contactparams, $contact->id if $contact->id;
221         }
222         if ($#contactparams > 0) {
223             $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
224         }
225     }
226     $sth = $dbh->prepare($contactquery);
227     $sth->execute(@contactparams);
228     return $cnt;
229 }
230
231 =head2 DelBookseller
232
233 DelBookseller($booksellerid);
234
235 delete the supplier record identified by $booksellerid
236 This sub assumes it is called only if the supplier has no order.
237
238 =cut
239
240 sub DelBookseller {
241     my $id  = shift;
242     my $dbh = C4::Context->dbh;
243     my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
244     return $sth->execute($id);
245 }
246
247 1;
248
249 __END__
250
251 =head1 AUTHOR
252
253 Koha Development Team <http://koha-community.org/>
254
255 =cut