1 package C4::Circulation::Renewals;
5 #package to deal with Renewals
6 #written 7/11/99 by olwen@katipo.co.nz
9 # Copyright 2000-2002 Katipo Communications
11 # This file is part of Koha.
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA 02111-1307 USA
32 use C4::Interface::RenewalsCDK;
33 use C4::Circulation::Issues;
34 use C4::Circulation::Main;
35 # FIXME - C4::Circulation::Main and C4::Circulation::Renewals
36 # use each other, so functions get redefined.
40 use vars qw($VERSION @ISA @EXPORT);
42 # set the version for version checking
47 C4::Circulation::Renewals - Old Koha module dealing with renewals
51 use C4::Circulation::Renewals;
55 This module contains a function for checking whether a book may be
65 @EXPORT = qw(&renewstatus &renewbook &bulkrenew);
69 $ok = &renewstatus($env, $dbh, $borrowernumber, $itemnumber);
71 Find out whether a borrowed item may be renewed.
75 C<$dbh> is a DBI handle to the Koha database.
77 C<$borrowernumber> is the borrower number of the patron who currently
80 C<$itemnumber> is the number of the item to renew.
82 C<$renewstatus> returns a true value iff the item may be renewed. The
83 item must currently be on loan to the specified borrower; renewals
84 must be allowed for the item's type; and the borrower must not have
85 already renewed the loan.
89 # FIXME - This is identical to &C4::Circulation::Circ2::renewstatus,
90 # and virtually identical to &C4::Circulation::Renewals2::renewstatus.
91 # Pick one and stick with it.
93 # check renewal status
94 # FIXME - Two people can't borrow the same book at once, so
95 # presumably we can get $bornum from $itemno.
96 my ($env,$dbh,$bornum,$itemno)=@_;
97 my $renews = 1; # FIXME - I think this is the maximum
98 # number of allowed renewals.
99 # FIXME - I think this function could be redone to use only one SQL
102 # Look in the issues table for this item, lent to this borrower,
103 # and not yet returned.
104 my $sth1 = $dbh->prepare("select * from issues
105 where (borrowernumber = '$bornum')
106 and (itemnumber = '$itemno')
107 and returndate is null");
108 $sth1->execute($bornum,$itemno);
109 # Found a matching item
110 if (my $data1 = $sth1->fetchrow_hashref) {
111 # See if this item may be renewed. This query is convoluted
112 # because it's a bit messy: given the item number, we need to find
113 # the biblioitem, which gives us the itemtype, which tells us
114 # whether it may be renewed.
115 my $sth2 = $dbh->prepare("select renewalsallowed from items,biblioitems,itemtypes
116 where (items.itemnumber = ?)
117 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
118 and (biblioitems.itemtype = itemtypes.itemtype)");
119 $sth2->execute($itemno);
120 if (my $data2=$sth2->fetchrow_hashref) {
121 $renews = $data2->{'renewalsallowed'};
123 if ($renews > $data1->{'renewals'}) {
132 # FIXME - A different version of this function appears in
133 # C4::Circulation::Renewals2. Pick one and stick with it.
134 # FIXME - This function doesn't appear to be used. Presumably it's
136 # Otherwise, it needs a POD.
138 # mark book as renewed
139 # FIXME - Get $dbh from C4::Context->dbh, instead of requiring
140 # an additional argument.
141 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
142 if ($datedue eq "" ) {
144 my $sth=$dbh->prepare("Select * from biblioitems,items,itemtypes
145 where (items.itemnumber = ?)
146 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
147 and (biblioitems.itemtype = itemtypes.itemtype)");
148 $sth->execute($itemno);
149 if (my $data=$sth->fetchrow_hashref) {
150 $loanlength = $data->{'loanlength'}
154 my $datedu = time + ($loanlength * 86400);
155 my @datearr = localtime($datedu);
156 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
158 my @date = split("-",$datedue);
159 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
160 my $sth=$dbh->prepare("select * from issues where borrowernumber=? and
161 itemnumber=? and returndate is null");
162 $sth->execute($bornum,$itemno);
163 my $issuedata=$sth->fetchrow_hashref;
165 my $renews = $issuedata->{'renewals'} +1;
166 my $sth=$dbh->prepare("update issues
167 set date_due = ?, renewals = ?
168 where borrowernumber=? and
169 itemnumber=? and returndate is null");
171 $sth->execute($datedue,$renews,$bornum,$itemno);
176 # FIXME - Only used in C4:InterfaceCDK. Presumably this function is
178 # Otherwise, it needs a POD.
180 my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
181 my $sth = $dbh->prepare("select * from issues where borrowernumber = ? and returndate is null order by date_due");
182 $sth->execute($bornum);
189 while (my $issrec = $sth->fetchrow_hashref) {
190 my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
191 my @date = split("-",$issrec->{'date_due'});
192 #my $line = $issrec->{'date_due'}." ";
193 my $line = @date[2]."-".@date[1]."-".@date[0]." ";
194 my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
195 my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
196 $dbh,$issrec->{'itemnumber'});
200 } elsif ($renewstatus == 0) {
202 $rstatuses[$x] = "N";
205 $rstatuses[$x] = "Y";
207 $line .= fmtdec($env,$issrec->{'renewals'},"20")." ";
208 $line .= $itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
210 #debug_msg($env,$line);
211 $issues[$x] = $issrec;
212 $barcodes[$x] = $itemdata->{'barcode'};
214 if ($issrec->{'renewals'} > 0) {
217 $renewdef[$x] = $rdef;
223 my $renews = C4::Interface::RenewalsCDK::renew_window($env,
224 \@items,$borrower,$amount,$odues);
228 my @renew_errors = "";
229 while ($x < $isscnt) {
230 if (@$renews[$x] == 1) {
231 my $issrec = $issues[$x];
232 if ($rstatuses[$x] eq "Y") {
233 renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
234 my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
235 $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
237 C4::Circulation::Issues::createcharge($env,$dbh,
238 $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
240 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
241 } elsif ($rstatuses[$x] eq "N") {
242 C4::InterfaceCDK::info_msg($env,
243 "</S>$barcodes[$x] - can't renew");
245 C4::InterfaceCDK::info_msg($env,
246 "</S>$barcodes[$x] - on reserve");
261 Koha Developement team <info@koha.org>