Bug 14757 - Allow the use of Template Toolkit syntax for slips and notices
[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.rrp <> 0
79             AND aqorders.ecost <> 0
80             AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
81             AND aqbasket.closedate IS NOT NULL
82     ";
83     if ( defined $delay && $delay >= 0 ) {
84         $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
85         push @query_params, $delay;
86     } elsif ( $delay && $delay < 0 ){
87         warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
88         return;
89     }
90     if ( defined $estimateddeliverydatefrom ) {
91         $query .= '
92             AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
93             push @query_params, $estimateddeliverydatefrom;
94             if ( defined $estimateddeliverydateto ) {
95                 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
96                 push @query_params, $estimateddeliverydateto;
97             } else {
98                     $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
99             }
100     }
101     if ( defined $estimateddeliverydateto ) {
102         $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
103         push @query_params, $estimateddeliverydateto;
104     }
105
106     my $sth = $dbh->prepare($query);
107     $sth->execute( @query_params );
108     my %supplierlist;
109     while ( my ( $id, $name ) = $sth->fetchrow ) {
110         $supplierlist{$id} = $name;
111     }
112
113     return %supplierlist;
114 }
115
116 #--------------------------------------------------------------------#
117
118 =head2 AddBookseller
119
120 $id = &AddBookseller($bookseller);
121
122 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
123 keys are the fields of the aqbooksellers table in the Koha database.
124 All fields must be present.
125
126 Returns the ID of the newly-created bookseller.
127
128 =cut
129
130 sub AddBookseller {
131     my ($data, $contacts) = @_;
132     my $dbh    = C4::Context->dbh;
133     my $query = q|
134         INSERT INTO aqbooksellers
135             (
136                 name,      address1,      address2,     address3, address4,
137                 postal,    phone,         accountnumber,fax,      url,
138                 active,    listprice,     invoiceprice, gstreg,
139                 listincgst,invoiceincgst, gstrate,      discount, notes,
140                 deliverytime
141             )
142         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
143       ;
144     my $sth = $dbh->prepare($query);
145     $sth->execute(
146         $data->{'name'},         $data->{'address1'},
147         $data->{'address2'},     $data->{'address3'},
148         $data->{'address4'},     $data->{'postal'},
149         $data->{'phone'},        $data->{'accountnumber'},
150         $data->{'fax'},          $data->{'url'},
151         $data->{'active'},       $data->{'listprice'},
152         $data->{'invoiceprice'}, $data->{'gstreg'},
153         $data->{'listincgst'},   $data->{'invoiceincgst'},
154         $data->{'gstrate'},      $data->{'discount'},
155         $data->{notes},          $data->{deliverytime},
156     );
157
158     # return the id of this new supplier
159     my $id = $dbh->{'mysql_insertid'};
160     if ($id && $contacts) {
161         foreach my $contact (@$contacts) {
162             $contact = C4::Bookseller::Contact->new( $contact )
163                 unless ref $contacts eq 'C4::Bookseller::Contact';
164             $contact->bookseller($id);
165             $contact->save();
166         }
167     }
168     return $id;
169 }
170
171 #-----------------------------------------------------------------#
172
173 =head2 ModBookseller
174
175 ModBookseller($bookseller);
176
177 Updates the information for a given bookseller. C<$bookseller> is a
178 reference-to-hash whose keys are the fields of the aqbooksellers table
179 in the Koha database. It must contain entries for all of the fields.
180 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
181
182 The easiest way to get all of the necessary fields is to look up a
183 book seller with C<Koha::Acquisition::Bookseller>, modify what's necessary, then call
184 C<&ModBookseller> with the result.
185
186 =cut
187
188 sub ModBookseller {
189     my ($data, $contacts) = @_;
190     my $dbh    = C4::Context->dbh;
191     return unless $data->{'id'};
192     my $query  = 'UPDATE aqbooksellers
193         SET name=?,address1=?,address2=?,address3=?,address4=?,
194             postal=?,phone=?,accountnumber=?,fax=?,url=?,
195             active=?,listprice=?, invoiceprice=?,
196             gstreg=?,listincgst=?,invoiceincgst=?,
197             discount=?,notes=?,gstrate=?,deliverytime=?
198         WHERE id=?';
199     my $sth = $dbh->prepare($query);
200     my $cnt = $sth->execute(
201         $data->{'name'},         $data->{'address1'},
202         $data->{'address2'},     $data->{'address3'},
203         $data->{'address4'},     $data->{'postal'},
204         $data->{'phone'},        $data->{'accountnumber'},
205         $data->{'fax'},          $data->{'url'},
206         $data->{'active'},       $data->{'listprice'},
207         $data->{'invoiceprice'}, $data->{'gstreg'},
208         $data->{'listincgst'},   $data->{'invoiceincgst'},
209         $data->{'discount'},     $data->{'notes'},
210         $data->{'gstrate'},      $data->{deliverytime},
211         $data->{'id'}
212     );
213     $contacts ||= $data->{'contacts'};
214     my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?";
215     my @contactparams = ($data->{'id'});
216     if ($contacts) {
217         foreach my $contact (@$contacts) {
218             $contact = C4::Bookseller::Contact->new( $contact )
219                 unless ref $contacts eq 'C4::Bookseller::Contact';
220             $contact->bookseller($data->{'id'});
221             $contact->save();
222             push @contactparams, $contact->id if $contact->id;
223         }
224         if ($#contactparams > 0) {
225             $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);';
226         }
227     }
228     $sth = $dbh->prepare($contactquery);
229     $sth->execute(@contactparams);
230     return $cnt;
231 }
232
233 =head2 DelBookseller
234
235 DelBookseller($booksellerid);
236
237 delete the supplier record identified by $booksellerid
238 This sub assumes it is called only if the supplier has no order.
239
240 =cut
241
242 sub DelBookseller {
243     my $id  = shift;
244     my $dbh = C4::Context->dbh;
245     my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?');
246     return $sth->execute($id);
247 }
248
249 1;
250
251 __END__
252
253 =head1 AUTHOR
254
255 Koha Development Team <http://koha-community.org/>
256
257 =cut