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 $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
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'};
121 if ($renews > $data1->{'renewals'}) {
132 &renewbook($env, $borrowernumber, $itemnumber, $datedue);
136 C<$env-E<gt>{branchcode}> is the code of the branch where the
137 renewal is taking place.
139 C<$env-E<gt>{usercode}> is the value to log in C<statistics.usercode>
140 in the Koha database.
142 C<$borrowernumber> is the borrower number of the patron who currently
145 C<$itemnumber> is the number of the item to renew.
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.
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'.
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;
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
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'}
184 my $ti = time; # FIXME - Unused
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];
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
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);
211 UpdateStats($env,$env->{'branchcode'},'renew','','',$itemno);
213 # Charge a new rental fee, if applicable?
214 my ($charge,$type)=calc_charges($env, $itemno, $bornum);
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);
230 ($charge, $item_type) = &calc_charges($env, $itemnumber, $borrowernumber);
232 Calculate how much it would cost for a given patron to borrow a given
233 item, including any applicable discounts.
237 C<$itemnumber> is the item number of item the patron wishes to borrow.
239 C<$borrowernumber> is the patron's borrower number.
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>
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.
252 # calculate charges due
253 my ($env, $itemno, $bornum)=@_;
255 my $dbh = C4::Context->dbh;
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'};
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;
283 # print "item $item_type";
284 return ($charge,$item_type);
294 Koha Developement team <info@koha.org>