Forgot to check for fuzzy-ness.
[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 $sth1 = $dbh->prepare("select * from issues
102     where (borrowernumber = ?)
103     and (itemnumber = ?')
104     and returndate is null");
105   $sth1->execute($bornum,$itemno);
106   if (my $data1 = $sth1->fetchrow_hashref) {
107     # Found a matching item
108
109     # See if this item may be renewed. This query is convoluted
110     # because it's a bit messy: given the item number, we need to find
111     # the biblioitem, which gives us the itemtype, which tells us
112     # whether it may be renewed.
113     my $sth2 = $dbh->prepare("select renewalsallowed from items,biblioitems,itemtypes
114        where (items.itemnumber = ?)
115        and (items.biblioitemnumber = biblioitems.biblioitemnumber)
116        and (biblioitems.itemtype = itemtypes.itemtype)");
117     $sth2->execute($itemno);
118     if (my $data2=$sth2->fetchrow_hashref) {
119       $renews = $data2->{'renewalsallowed'};
120     }
121     if ($renews > $data1->{'renewals'}) {
122       $renewokay = 1;
123     }
124     $sth2->finish;
125   }
126   $sth1->finish;
127   return($renewokay);
128 }
129
130 =item renewbook
131
132   &renewbook($env, $borrowernumber, $itemnumber, $datedue);
133
134 Renews a loan.
135
136 C<$env-E<gt>{branchcode}> is the code of the branch where the
137 renewal is taking place.
138
139 C<$env-E<gt>{usercode}> is the value to log in C<statistics.usercode>
140 in the Koha database.
141
142 C<$borrowernumber> is the borrower number of the patron who currently
143 has the item.
144
145 C<$itemnumber> is the number of the item to renew.
146
147 C<$datedue> can be used to set the due date. If C<$datedue> is the
148 empty string, C<&renewbook> will calculate the due date automatically
149 from the book's item type. If you wish to set the due date manually,
150 C<$datedue> should be in the form YYYY-MM-DD.
151
152 =cut
153 #'
154 # FIXME - A simpler version of this function appears in
155 # C4::Circulation::Renewals. Pick one and stick with it.
156 # There's also a &C4::Circulation::Circ2::renewbook.
157 # I think this function is only used in 'renewscript.pl'.
158 sub renewbook {
159   # mark book as renewed
160   # FIXME - A book can't be on loan to two people at once, so
161   # presumably we can get $bornum from $itemno.
162   my ($env,$bornum,$itemno,$datedue)=@_;
163   my $dbh = C4::Context->dbh;
164
165   # If the due date wasn't specified, calculate it by adding the
166   # book's loan length to today's date.
167   if ($datedue eq "" ) {
168     #debug_msg($env, "getting date");
169     my $loanlength=21;          # Default loan length?
170                                 # FIXME - This is bogus. If there's no
171                                 # loan length defined for some book
172                                 # type or whatever, then that should
173                                 # be an error
174     # Find this item's item type, via its biblioitem.
175     my $sth=$dbh->prepare("Select * from biblioitems,items,itemtypes
176        where (items.itemnumber = ?)
177        and (biblioitems.biblioitemnumber = items.biblioitemnumber)
178        and (biblioitems.itemtype = itemtypes.itemtype)");
179     $sth->execute($itemno);
180     if (my $data=$sth->fetchrow_hashref) {
181       $loanlength = $data->{'loanlength'}
182     }
183     $sth->finish;
184     my $ti = time;              # FIXME - Unused
185     # FIXME - Use
186     #   POSIX::strftime("%Y-%m-%d", localtime(time + ...));
187     my $datedu = time + ($loanlength * 86400);
188     my @datearr = localtime($datedu);
189     $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
190   }
191
192   # Find the issues record for this book
193   my $sth=$dbh->prepare("select * from issues where borrowernumber=? and
194     itemnumber=? and returndate is null");
195   $sth->execute($bornum,$itemno);
196   my $issuedata=$sth->fetchrow_hashref;
197         # FIXME - Error-checking
198   $sth->finish;
199
200   # Update the issues record to have the new due date, and a new count
201   # of how many times it has been renewed.
202   my $renews = $issuedata->{'renewals'} +1;
203   $sth=$dbh->prepare("update issues
204     set date_due = ?, renewals = ?
205     where borrowernumber=? and
206     itemnumber=? and returndate is null");
207   $sth->execute($datedue,$renews,$bornum,$itemno);
208   $sth->finish;
209
210   # Log the renewal
211   UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
212
213   # Charge a new rental fee, if applicable?
214   my ($charge,$type)=calc_charges($env, $itemno, $bornum);
215   if ($charge > 0){
216     my $accountno=getnextacctno($env,$bornum,$dbh);
217     my $item=getiteminformation($env, $itemno);
218     $sth=$dbh->prepare("Insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
219                                                 values (?,?,now(),?,?,?,?,?)");
220     $sth->execute($bornum,$accountno,$charge,"Renewal of Rental Item $item->{'title'} $item->{'barcode'}",'Rent',$charge,$itemno);
221     $sth->finish;
222 #     print $account;
223   }
224
225 #  return();
226 }
227
228 =item calc_charges
229
230   ($charge, $item_type) = &calc_charges($env, $itemnumber, $borrowernumber);
231
232 Calculate how much it would cost for a given patron to borrow a given
233 item, including any applicable discounts.
234
235 C<$env> is ignored.
236
237 C<$itemnumber> is the item number of item the patron wishes to borrow.
238
239 C<$borrowernumber> is the patron's borrower number.
240
241 C<&calc_charges> returns two values: C<$charge> is the rental charge,
242 and C<$item_type> is the code for the item's item type (e.g., C<VID>
243 if it's a video).
244
245 =cut
246 #'
247 # FIXME - This is very similar to
248 # &C4::Circulation::Issues::calc_charges and
249 # &C4::Circulation::Circ2::calc_charges.
250 # Pick one and stick with it.
251 sub calc_charges {
252   # calculate charges due
253   my ($env, $itemno, $bornum)=@_;
254   my $charge=0;
255   my $dbh = C4::Context->dbh;
256   my $item_type;
257
258   # Get the book's item type and rental charge (via its biblioitem).
259   my $sth1= $dbh->prepare("select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
260                                                  where (items.itemnumber =?)
261                                                                 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
262                                                                 and (biblioitems.itemtype = itemtypes.itemtype)");
263   $sth1->execute($itemno);
264   # FIXME - Why not just use fetchrow_array?
265   if (my $data1=$sth1->fetchrow_hashref) {
266     $item_type = $data1->{'itemtype'};
267     $charge = $data1->{'rentalcharge'};
268
269     # Figure out the applicable rental discount
270     my $sth2=$dbh->prepare("select rentaldiscount from
271     borrowers,categoryitem
272     where (borrowers.borrowernumber = ?)
273     and (borrowers.categorycode = categoryitem.categorycode)
274     and (categoryitem.itemtype = ?)");
275     $sth2->execute($bornum,$item_type);
276     if (my$data2=$sth2->fetchrow_hashref) {
277       my $discount = $data2->{'rentaldiscount'};
278       $charge *= (100 - $discount) / 100;
279     }
280     $sth2->finish;
281   }
282   $sth1->finish;
283 #  print "item $item_type";
284   return ($charge,$item_type);
285 }
286
287 1;
288 __END__
289
290 =back
291
292 =head1 AUTHOR
293
294 Koha Developement team <info@koha.org>
295
296 =cut