1 package C4::Circulation::Returns;
5 #package to deal with Returns
6 #written 3/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
26 # FIXME - None of the functions (certainly none of the exported
27 # functions) are used anywhere anymore. Presumably this module is
36 use C4::Circulation::Main;
37 # FIXME - C4::Circulation::Main and C4::Circulation::Returns
38 # use each other, so functions get redefined.
44 use vars qw($VERSION @ISA @EXPORT);
46 # set the version for version checking
50 @EXPORT = qw(&returnrecord &calc_odues &Returns);
52 # FIXME - This is only used in C4::Circmain and C4::Circulation, both
53 # of which appear to be obsolete. Presumably this function is obsolete
55 # Otherwise, it needs a POD.
58 my $dbh = C4::Context->dbh;
72 # until (($reason eq "Circ") || ($reason eq "Quit")) {
73 until ($reason ne "") {
75 returnwindow($env,"Enter Returns",
76 $item,\@items,$borrower,$amt_owing,$odues,$dbh,$resp); #C4::Circulation
77 #debug_msg($env,"item = $item");
78 #if (($reason ne "Circ") && ($reason ne "Quit")) {
81 ($resp,$bornum,$borrower,$itemno,$itemrec,$amt_owing) =
82 checkissue($env,$dbh,$item);
84 ($issues,$odues,$amt_owing) = borrdata2($env,$bornum);
91 #if ($resp eq "Returned") {
93 my $item = itemnodata($env,$dbh,$itemno);
94 # FIXME - This relies on C4::Circulation::Main to have a
95 # "use C4::Circulation::Issues;" line, which is bogus.
96 my $fmtitem = C4::Circulation::Issues::formatitem($env,$item,"",$amt_owing);
97 unshift @items,$fmtitem;
98 if ($items[20] > "") {
102 #} elsif ($resp ne "") {
103 # error_msg($env,"$resp");
105 #if ($resp ne "Returned") {
106 # error_msg($env,"$resp");
116 # FIXME - Only used in &Returns and in telnet/doreturns.pl, both of
117 # which appear obsolete. Presumably this function is obsolete as well.
118 # Otherwise, it needs a POD.
120 my ($env,$dbh, $item) = @_;
128 my $sth=$dbh->prepare("select * from items,biblio
130 and (biblio.biblionumber=items.biblionumber)");
131 $sth->execute($item);
132 if ($itemrec=$sth->fetchrow_hashref) {
134 $itemno = $itemrec->{'itemnumber'};
135 my $sth=$dbh->prepare("select * from issues
137 and (returndate is null)");
138 $sth->execute($itemrec->{'itemnumber'});
139 if (my $issuerec=$sth->fetchrow_hashref) {
141 my $sth= $dbh->prepare("select * from borrowers where
142 (borrowernumber = ?)");
143 $sth->execute($issuerec->{'borrowernumber'});
144 $env->{'bornum'}=$issuerec->{'borrowernumber'};
145 $borrower = $sth->fetchrow_hashref;
146 $bornum = $issuerec->{'borrowernumber'};
147 $itemno = $issuerec->{'itemnumber'};
148 $amt_owing = returnrecord($env,$dbh,$bornum,$itemno);
149 $reason = "Returned";
152 updatelastseen($env,$dbh,$itemrec->{'itemnumber'});
153 $reason = "Item not issued";
155 my ($resfound,$resrec) = find_reserves($env,$dbh,$itemrec->{'itemnumber'});
156 if ($resfound eq "y") {
157 my $btsh = $dbh->prepare("select * from borrowers
158 where borrowernumber = ?");
159 $btsh->execute($resrec->{'borrowernumber'});
160 my $resborrower = $btsh->fetchrow_hashref;
161 #printreserve($env,$resrec,$resborrower,$itemrec);
162 my $mess = "Reserved for collection at branch $resrec->{'branchcode'}";
163 C4::InterfaceCDK::error_msg($env,$mess);
168 $reason = "Item not found";
170 return ($reason,$bornum,$borrower,$itemno,$itemrec,$amt_owing);
174 # FIXME - Only used in &C4::Circulation::Main::previousissue,
175 # &checkissue, C4/Circulation.pm, and tkperl/tkcirc, all of which
176 # appear to be obsolete. Presumably this function is obsolete as well.
177 # Otherwise, it needs a POD.
179 # mark items as returned
180 my ($env,$dbh,$bornum,$itemno)=@_;
181 #my $amt_owing = calc_odues($env,$dbh,$bornum,$itemno);
182 my @datearr = localtime(time);
183 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
184 my $sth = $dbh->prepare("update issues set returndate = now(), branchcode = ? where
185 (borrowernumber = ?) and (itemnumber = ?)
186 and (returndate is null)");
187 $sth->execute($env->{'branchcode'},$bornum,$itemno);
189 updatelastseen($env,$dbh,$itemno);
190 # check for overdue fine
192 my $sth = $dbh->prepare("select * from accountlines
193 where (borrowernumber = ?)
195 and (accounttype = 'FU' or accounttype='O')");
196 $sth->execute($bornum,$itemno);
197 if (my $data = $sth->fetchrow_hashref) {
198 # alter fine to show that the book has been returned.
199 my $usth = $dbh->prepare("update accountlines
200 set accounttype = 'F'
201 where (borrowernumber = ?)
203 and (accountno = ?) ");
204 $usth->execute($bornum,$itemno,$data->{'accountno'});
206 $oduecharge = $data->{'amountoutstanding'};
209 # check for charge made for lost book
210 my $sth = $dbh->prepare("select * from accountlines
211 where (borrowernumber = ?)
213 and (accounttype = 'L')");
214 $sth->execute($bornum,$itemno);
215 if (my $data = $sth->fetchrow_hashref) {
216 # writeoff this amount
218 my $amount = $data->{'amount'};
219 my $acctno = $data->{'accountno'};
221 if ($data->{'amountoutstanding'} == $amount) {
222 $offset = $data->{'amount'};
225 $offset = $amount - $data->{'amountoutstanding'};
226 $amountleft = $data->{'amountoutstanding'} - $amount;
228 my $usth = $dbh->prepare("update accountlines
229 set accounttype = 'LR',amountoutstanding='0'
230 where (borrowernumber = ?)
232 and (accountno = ?) ");
233 $usth->execute($bornum,$itemno,$acctno);
235 my $nextaccntno = C4::Accounts::getnextacctno($env,$bornum,$dbh);
236 $usth = $dbh->prepare("insert into accountlines
237 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
238 values (?,?,now(),?,'Book Returned','CR',?)");
239 $usth->execute($bornum,$nextaccntno,0-$amount,$amountleft);
241 $uquery = "insert into accountoffsets
242 (borrowernumber, accountno, offsetaccount, offsetamount)
244 $usth = $dbh->prepare("");
245 $usth->execute($bornum,$data->{'accountno'},$nextaccntno,$offset);
249 UpdateStats($env,'branch','return','0','',$itemno);
253 # FIXME - Only used in tkperl/tkcirc. Presumably this function is
255 # Otherwise, it needs a POD.
257 # calculate overdue fees
258 my ($env,$dbh,$bornum,$itemno)=@_;
263 # This function is only used in &checkissue and &returnrecord, both of
264 # which appear to be obsolete. So presumably this function is obsolete
266 # Otherwise, it needs a POD.
268 my ($env,$dbh,$itemnumber)= @_;
269 my $br = $env->{'branchcode'};
270 my $sth = $dbh->prepare("update items
271 set datelastseen = now(), holdingbranch = ?
272 where (itemnumber = ?)");
273 $sth->execute($br,$itemnumber);
279 # FIXME - There's also a &C4::Circulation::Circ2::find_reserves, but
280 # that one looks rather different.
281 # FIXME - This is only used in &checkissue, which appears to be
282 # obsolete. So presumably this function is obsolete too.
284 my ($env,$dbh,$itemno) = @_;
285 my $itemdata = itemnodata($env,$dbh,$itemno);
286 my $sth = $dbh->prepare("select * from reserves where found is null
287 and biblionumber = ? and cancellationdate is NULL
288 order by priority,reservedate ");
289 $sth->execute($itemdata->{'biblionumber'};
292 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
293 if ($resrec->{'found'} eq "W") {
294 if ($resrec->{'itemnumber'} eq $itemno) {
297 } elsif ($resrec->{'constrainttype'} eq "a") {
300 my $consth = $dbh->prepare("select * from reserveconstraints where borrowernumber = ? and reservedate = ? and biblionumber = ? and biblioitemnumber = ?");
301 $consth->execute($resrec->{'borrowernumber'},$resrec->{'reservedate'},$resrec->{'biblionumber'},$itemdata->{'biblioitemnumber'});
302 if (my $conrec=$consth->fetchrow_hashref) {
303 if ($resrec->{'constrainttype'} eq "o") {
307 if ($resrec->{'constrainttype'} eq "e") {
313 if ($resfound eq "y") {
314 my $updsth = $dbh->prepare("update reserves
315 set found = 'W',itemnumber = ?
316 where borrowernumber = ?
318 and biblionumber = ?");
319 $updsth->execute($itemno,$resrec->{'borrowernumber'},$resrec->{'reservedate'},$resrec->{'biblionumber'});
321 my $itbr = $resrec->{'branchcode'};
322 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
323 my $updsth = $dbh->prepare("update items
324 set holdingbranch = 'TR'
325 where itemnumber = ?");
326 $updsth->execute($itemno);
332 return ($resfound,$resrec);