DBI fixes re bug 662. Removed worrying suggestion to use dbh->do in comments.
[koha.git] / C4 / Circulation / Renewals2.pm
1 package C4::Circulation::Renewals2;
2
3 # $Id$
4
5 #package to deal with Renewals
6 #written 7/11/99 by olwen@katipo.co.nz
7
8 #modified by chris@katipo.co.nz
9 #18/1/2000
10 #need to update stats with renewals
11
12
13 # Copyright 2000-2002 Katipo Communications
14 #
15 # This file is part of Koha.
16 #
17 # Koha is free software; you can redistribute it and/or modify it under the
18 # terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 2 of the License, or (at your option) any later
20 # version.
21 #
22 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License along with
27 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
28 # Suite 330, Boston, MA  02111-1307 USA
29
30 use strict;
31 require Exporter;
32 use DBI;
33 use C4::Stats;
34 use C4::Accounts2;
35 use C4::Circulation::Circ2;
36
37 use vars qw($VERSION @ISA @EXPORT);
38
39 # set the version for version checking
40 $VERSION = 0.01;
41
42 =head1 NAME
43
44 C4::Circulation::Renewals2 - Koha functions for renewals
45
46 =head1 SYNOPSIS
47
48   use C4::Circulation::Renewals2;
49
50 =head1 DESCRIPTION
51
52 This module provides a few functions for handling loan renewals.
53
54 =head1 FUNCTIONS
55
56 =over 2
57
58 =cut
59
60 @ISA = qw(Exporter);
61 @EXPORT = qw(&renewstatus &renewbook &calc_charges);
62
63 =item renewstatus
64
65   $ok = &renewstatus($env, $dbh, $borrowernumber, $itemnumber);
66
67 Find out whether a borrowed item may be renewed.
68
69 C<$env> is ignored.
70
71 C<$dbh> is a DBI handle to the Koha database.
72
73 C<$borrowernumber> is the borrower number of the patron who currently
74 has the item on loan.
75
76 C<$itemnumber> is the number of the item to renew.
77
78 C<$renewstatus> returns a true value iff the item may be renewed. The
79 item must currently be on loan to the specified borrower; renewals
80 must be allowed for the item's type; and the borrower must not have
81 already renewed the loan.
82
83 =cut
84 #'
85 # FIXME - This is virtually identical to
86 # &C4::Circulation::Circ2::renewstatus and
87 # &C4::Circulation::Renewals::renewstatus. Pick one and stick with it.
88 sub renewstatus {
89   # check renewal status
90   # FIXME - Two people can't borrow the same book at once, so
91   # presumably we can get $bornum from $itemno.
92   my ($env,$bornum,$itemno)=@_;
93   my $dbh = C4::Context->dbh;
94   my $renews = 1;
95   my $renewokay = 0;
96   # Look in the issues table for this item, lent to this borrower,
97   # and not yet returned.
98
99   # FIXME - I think this function could be redone to use only one SQL
100   # call.
101   my $q1 = "select * from issues
102     where (borrowernumber = '$bornum')
103     and (itemnumber = '$itemno')
104     and returndate is null";
105   my $sth1 = $dbh->prepare($q1);
106   $sth1->execute;
107   if (my $data1 = $sth1->fetchrow_hashref) {
108     # Found a matching item
109
110     # See if this item may be renewed. This query is convoluted
111     # because it's a bit messy: given the item number, we need to find
112     # the biblioitem, which gives us the itemtype, which tells us
113     # whether it may be renewed.
114     my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
115        where (items.itemnumber = '$itemno')
116        and (items.biblioitemnumber = biblioitems.biblioitemnumber)
117        and (biblioitems.itemtype = itemtypes.itemtype)";
118     my $sth2 = $dbh->prepare($q2);
119     $sth2->execute;
120     if (my $data2=$sth2->fetchrow_hashref) {
121       $renews = $data2->{'renewalsallowed'};
122     }
123     if ($renews > $data1->{'renewals'}) {
124       $renewokay = 1;
125     }
126     $sth2->finish;
127   }
128   $sth1->finish;
129   return($renewokay);
130 }
131
132 =item renewbook
133
134   &renewbook($env, $borrowernumber, $itemnumber, $datedue);
135
136 Renews a loan.
137
138 C<$env-E<gt>{branchcode}> is the code of the branch where the
139 renewal is taking place.
140
141 C<$env-E<gt>{usercode}> is the value to log in C<statistics.usercode>
142 in the Koha database.
143
144 C<$borrowernumber> is the borrower number of the patron who currently
145 has the item.
146
147 C<$itemnumber> is the number of the item to renew.
148
149 C<$datedue> can be used to set the due date. If C<$datedue> is the
150 empty string, C<&renewbook> will calculate the due date automatically
151 from the book's item type. If you wish to set the due date manually,
152 C<$datedue> should be in the form YYYY-MM-DD.
153
154 =cut
155 #'
156 # FIXME - A simpler version of this function appears in
157 # C4::Circulation::Renewals. Pick one and stick with it.
158 # There's also a &C4::Circulation::Circ2::renewbook.
159 # I think this function is only used in 'renewscript.pl'.
160 sub renewbook {
161   # mark book as renewed
162   # FIXME - A book can't be on loan to two people at once, so
163   # presumably we can get $bornum from $itemno.
164   my ($env,$bornum,$itemno,$datedue)=@_;
165   my $dbh = C4::Context->dbh;
166
167   # If the due date wasn't specified, calculate it by adding the
168   # book's loan length to today's date.
169   if ($datedue eq "" ) {
170     #debug_msg($env, "getting date");
171     my $loanlength=21;          # Default loan length?
172                                 # FIXME - This is bogus. If there's no
173                                 # loan length defined for some book
174                                 # type or whatever, then that should
175                                 # be an error
176     # Find this item's item type, via its biblioitem.
177     my $query= "Select * from biblioitems,items,itemtypes
178        where (items.itemnumber = '$itemno')
179        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
180        and (biblioitems.itemtype = itemtypes.itemtype)";
181     my $sth=$dbh->prepare($query);
182     $sth->execute;
183     if (my $data=$sth->fetchrow_hashref) {
184       $loanlength = $data->{'loanlength'}
185     }
186     $sth->finish;
187     my $ti = time;              # FIXME - Unused
188     # FIXME - Use
189     #   POSIX::strftime("%Y-%m-%d", localtime(time + ...));
190     my $datedu = time + ($loanlength * 86400);
191     my @datearr = localtime($datedu);
192     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
193   }
194
195   # Find the issues record for this book
196   my $issquery = "select * from issues where borrowernumber='$bornum' and
197     itemnumber='$itemno' and returndate is null";
198   my $sth=$dbh->prepare($issquery);
199   $sth->execute;
200   my $issuedata=$sth->fetchrow_hashref;
201         # FIXME - Error-checking
202   $sth->finish;
203
204   # Update the issues record to have the new due date, and a new count
205   # of how many times it has been renewed.
206   my $renews = $issuedata->{'renewals'} +1;
207   my $updquery = "update issues
208     set date_due = '$datedue', renewals = '$renews'
209     where borrowernumber='$bornum' and
210     itemnumber='$itemno' and returndate is null";
211                 # FIXME - Use $dbh->do()
212   $sth=$dbh->prepare($updquery);
213   $sth->execute;
214   $sth->finish;
215
216   # Log the renewal
217   UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
218
219   # Charge a new rental fee, if applicable?
220   my ($charge,$type)=calc_charges($env, $itemno, $bornum);
221   if ($charge > 0){
222     my $accountno=getnextacctno($env,$bornum,$dbh);
223     my $item=getiteminformation($env, $itemno);
224     $sth=$dbh->prepare("Insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
225                                                 values (?,?,now(),?,?,?,?,?)");
226     $sth->execute($bornum,$accountno,$charge,"Renewal of Rental Item $item->{'title'} $item->{'barcode'}",'Rent',$charge,$itemno);
227     $sth->finish;
228 #     print $account;
229   }
230
231 #  return();
232 }
233
234 =item calc_charges
235
236   ($charge, $item_type) = &calc_charges($env, $itemnumber, $borrowernumber);
237
238 Calculate how much it would cost for a given patron to borrow a given
239 item, including any applicable discounts.
240
241 C<$env> is ignored.
242
243 C<$itemnumber> is the item number of item the patron wishes to borrow.
244
245 C<$borrowernumber> is the patron's borrower number.
246
247 C<&calc_charges> returns two values: C<$charge> is the rental charge,
248 and C<$item_type> is the code for the item's item type (e.g., C<VID>
249 if it's a video).
250
251 =cut
252 #'
253 # FIXME - This is very similar to
254 # &C4::Circulation::Issues::calc_charges and
255 # &C4::Circulation::Circ2::calc_charges.
256 # Pick one and stick with it.
257 sub calc_charges {
258   # calculate charges due
259   my ($env, $itemno, $bornum)=@_;
260   my $charge=0;
261   my $dbh = C4::Context->dbh;
262   my $item_type;
263
264   # Get the book's item type and rental charge (via its biblioitem).
265   my $sth1= $dbh->prepare("select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
266                                                  where (items.itemnumber =?)
267                                                                 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
268                                                                 and (biblioitems.itemtype = itemtypes.itemtype)");
269   $sth1->execute($itemno);
270   # FIXME - Why not just use fetchrow_array?
271   if (my $data1=$sth1->fetchrow_hashref) {
272     $item_type = $data1->{'itemtype'};
273     $charge = $data1->{'rentalcharge'};
274
275     # Figure out the applicable rental discount
276     my $q2 = "select rentaldiscount from
277     borrowers,categoryitem
278     where (borrowers.borrowernumber = '$bornum')
279     and (borrowers.categorycode = categoryitem.categorycode)
280     and (categoryitem.itemtype = '$item_type')";
281     my $sth2=$dbh->prepare($q2);
282     $sth2->execute;
283     if (my$data2=$sth2->fetchrow_hashref) {
284       my $discount = $data2->{'rentaldiscount'};
285       $charge *= (100 - $discount) / 100;
286     }
287     $sth2->finish;
288   }
289   $sth1->finish;
290 #  print "item $item_type";
291   return ($charge,$item_type);
292 }
293
294 1;
295 __END__
296
297 =back
298
299 =head1 AUTHOR
300
301 Koha Developement team <info@koha.org>
302
303 =cut