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 $q1 = "select * from issues
105 where (borrowernumber = '$bornum')
106 and (itemnumber = '$itemno')
107 and returndate is null";
108 my $sth1 = $dbh->prepare($q1);
110 # Found a matching item
111 if (my $data1 = $sth1->fetchrow_hashref) {
112 # See if this item may be renewed. This query is convoluted
113 # because it's a bit messy: given the item number, we need to find
114 # the biblioitem, which gives us the itemtype, which tells us
115 # whether it may be renewed.
116 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
117 where (items.itemnumber = '$itemno')
118 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
119 and (biblioitems.itemtype = itemtypes.itemtype)";
120 my $sth2 = $dbh->prepare($q2);
122 if (my $data2=$sth2->fetchrow_hashref) {
123 $renews = $data2->{'renewalsallowed'};
125 if ($renews > $data1->{'renewals'}) {
134 # FIXME - A different version of this function appears in
135 # C4::Circulation::Renewals2. Pick one and stick with it.
136 # FIXME - This function doesn't appear to be used. Presumably it's
138 # Otherwise, it needs a POD.
140 # mark book as renewed
141 # FIXME - Get $dbh from C4::Context->dbh, instead of requiring
142 # an additional argument.
143 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
144 if ($datedue eq "" ) {
146 my $query= "Select * from biblioitems,items,itemtypes
147 where (items.itemnumber = '$itemno')
148 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
149 and (biblioitems.itemtype = itemtypes.itemtype)";
150 my $sth=$dbh->prepare($query);
152 if (my $data=$sth->fetchrow_hashref) {
153 $loanlength = $data->{'loanlength'}
157 my $datedu = time + ($loanlength * 86400);
158 my @datearr = localtime($datedu);
159 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
161 my @date = split("-",$datedue);
162 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
163 my $issquery = "select * from issues where borrowernumber='$bornum' and
164 itemnumber='$itemno' and returndate is null";
165 my $sth=$dbh->prepare($issquery);
167 my $issuedata=$sth->fetchrow_hashref;
169 my $renews = $issuedata->{'renewals'} +1;
170 my $updquery = "update issues
171 set date_due = '$datedue', renewals = '$renews'
172 where borrowernumber='$bornum' and
173 itemnumber='$itemno' and returndate is null";
174 my $sth=$dbh->prepare($updquery);
181 # FIXME - Only used in C4:InterfaceCDK. Presumably this function is
183 # Otherwise, it needs a POD.
185 my ($env,$dbh,$bornum,$amount,$borrower,$odues) = @_;
186 my $query = "select * from issues
187 where borrowernumber = '$bornum' and returndate is null order by date_due";
188 my $sth = $dbh->prepare($query);
196 while (my $issrec = $sth->fetchrow_hashref) {
197 my $itemdata = C4::Search::itemnodata($env,$dbh,$issrec->{'itemnumber'});
198 my @date = split("-",$issrec->{'date_due'});
199 #my $line = $issrec->{'date_due'}." ";
200 my $line = @date[2]."-".@date[1]."-".@date[0]." ";
201 my $renewstatus = renewstatus($env,$dbh,$bornum,$issrec->{'itemnumber'});
202 my ($resbor,$resrec) = C4::Circulation::Main::checkreserve($env,
203 $dbh,$issrec->{'itemnumber'});
207 } elsif ($renewstatus == 0) {
209 $rstatuses[$x] = "N";
212 $rstatuses[$x] = "Y";
214 $line .= fmtdec($env,$issrec->{'renewals'},"20")." ";
215 $line .= $itemdata->{'barcode'}." ".$itemdata->{'itemtype'}." ".$itemdata->{'title'};
217 #debug_msg($env,$line);
218 $issues[$x] = $issrec;
219 $barcodes[$x] = $itemdata->{'barcode'};
221 if ($issrec->{'renewals'} > 0) {
224 $renewdef[$x] = $rdef;
230 my $renews = C4::Interface::RenewalsCDK::renew_window($env,
231 \@items,$borrower,$amount,$odues);
235 my @renew_errors = "";
236 while ($x < $isscnt) {
237 if (@$renews[$x] == 1) {
238 my $issrec = $issues[$x];
239 if ($rstatuses[$x] eq "Y") {
240 renewbook($env,$dbh,$issrec->{'borrowernumber'},$issrec->{'itemnumber'},"");
241 my $charge = C4::Circulation::Issues::calc_charges($env,$dbh,
242 $issrec->{'itemnumber'},$issrec->{'borrowernumber'});
244 C4::Circulation::Issues::createcharge($env,$dbh,
245 $issrec->{'itemnumber'},$issrec->{'borrowernumber'},$charge);
247 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$issrec->{'itemnumber'});
248 } elsif ($rstatuses[$x] eq "N") {
249 C4::InterfaceCDK::info_msg($env,
250 "</S>$barcodes[$x] - can't renew");
252 C4::InterfaceCDK::info_msg($env,
253 "</S>$barcodes[$x] - on reserve");
268 Koha Developement team <info@koha.org>