1 package C4::Circulation::Renewals2;
5 #package to deal with Renewals
6 #written 7/11/99 by olwen@katipo.co.nz
8 #modified by chris@katipo.co.nz
10 #need to update stats with renewals
13 # Copyright 2000-2002 Katipo Communications
15 # This file is part of Koha.
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
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.
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
35 use C4::Circulation::Circ2;
37 use vars qw($VERSION @ISA @EXPORT);
39 # set the version for version checking
44 C4::Circulation::Renewals2 - Koha functions for renewals
48 use C4::Circulation::Renewals2;
52 This module provides a few functions for handling loan renewals.
61 @EXPORT = qw(&renewstatus &renewbook &calc_charges);
65 $ok = &renewstatus($env, $dbh, $borrowernumber, $itemnumber);
67 Find out whether a borrowed item may be renewed.
71 C<$dbh> is a DBI handle to the Koha database.
73 C<$borrowernumber> is the borrower number of the patron who currently
76 C<$itemnumber> is the number of the item to renew.
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.
85 # FIXME - This is virtually identical to
86 # &C4::Circulation::Circ2::renewstatus and
87 # &C4::Circulation::Renewals::renewstatus. Pick one and stick with it.
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;
96 # Look in the issues table for this item, lent to this borrower,
97 # and not yet returned.
99 # FIXME - I think this function could be redone to use only one SQL
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);
107 if (my $data1 = $sth1->fetchrow_hashref) {
108 # Found a matching item
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);
120 if (my $data2=$sth2->fetchrow_hashref) {
121 $renews = $data2->{'renewalsallowed'};
123 if ($renews > $data1->{'renewals'}) {
134 &renewbook($env, $borrowernumber, $itemnumber, $datedue);
138 C<$env-E<gt>{branchcode}> is the code of the branch where the
139 renewal is taking place.
141 C<$env-E<gt>{usercode}> is the value to log in C<statistics.usercode>
142 in the Koha database.
144 C<$borrowernumber> is the borrower number of the patron who currently
147 C<$itemnumber> is the number of the item to renew.
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.
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'.
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;
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
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);
183 if (my $data=$sth->fetchrow_hashref) {
184 $loanlength = $data->{'loanlength'}
187 my $ti = time; # FIXME - Unused
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];
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);
200 my $issuedata=$sth->fetchrow_hashref;
201 # FIXME - Error-checking
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);
217 UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
219 # Charge a new rental fee, if applicable?
220 my ($charge,$type)=calc_charges($env, $itemno, $bornum);
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);
236 ($charge, $item_type) = &calc_charges($env, $itemnumber, $borrowernumber);
238 Calculate how much it would cost for a given patron to borrow a given
239 item, including any applicable discounts.
243 C<$itemnumber> is the item number of item the patron wishes to borrow.
245 C<$borrowernumber> is the patron's borrower number.
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>
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.
258 # calculate charges due
259 my ($env, $itemno, $bornum)=@_;
261 my $dbh = C4::Context->dbh;
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'};
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);
283 if (my$data2=$sth2->fetchrow_hashref) {
284 my $discount = $data2->{'rentaldiscount'};
285 $charge *= (100 - $discount) / 100;
290 # print "item $item_type";
291 return ($charge,$item_type);
301 Koha Developement team <info@koha.org>