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.
45 use vars qw($VERSION @ISA @EXPORT);
47 # set the version for version checking
51 @EXPORT = qw(&returnrecord &calc_odues &Returns);
53 # FIXME - This is only used in C4::Circmain and C4::Circulation, both
54 # of which appear to be obsolete. Presumably this function is obsolete
56 # Otherwise, it needs a POD.
59 my $dbh = C4::Context->dbh;
73 # until (($reason eq "Circ") || ($reason eq "Quit")) {
74 until ($reason ne "") {
76 returnwindow($env,"Enter Returns",
77 $item,\@items,$borrower,$amt_owing,$odues,$dbh,$resp); #C4::Circulation
78 #debug_msg($env,"item = $item");
79 #if (($reason ne "Circ") && ($reason ne "Quit")) {
82 ($resp,$borrowernumber,$borrower,$itemno,$itemrec,$amt_owing) =
83 checkissue($env,$dbh,$item);
84 if ($borrowernumber ne "") {
85 ($issues,$odues,$amt_owing) = borrdata2($env,$borrowernumber);
92 #if ($resp eq "Returned") {
94 my $item = GetBiblioFromItemNumber($itemno);
95 # FIXME - This relies on C4::Circulation::Main to have a
96 # "use C4::Circulation::Issues;" line, which is bogus.
97 my $fmtitem = C4::Circulation::Issues::formatitem($env,$item,"",$amt_owing);
98 unshift @items,$fmtitem;
99 if ($items[20] > "") {
103 #} elsif ($resp ne "") {
104 # error_msg($env,"$resp");
106 #if ($resp ne "Returned") {
107 # error_msg($env,"$resp");
108 # $borrowernumber = "";
117 # FIXME - Only used in &Returns and in telnet/doreturns.pl, both of
118 # which appear obsolete. Presumably this function is obsolete as well.
119 # Otherwise, it needs a POD.
121 my ($env,$dbh, $item) = @_;
129 my $sth=$dbh->prepare("select * from items,biblio
131 and (biblio.biblionumber=items.biblionumber)");
132 $sth->execute($item);
133 if ($itemrec=$sth->fetchrow_hashref) {
135 $itemno = $itemrec->{'itemnumber'};
136 my $sth=$dbh->prepare("select * from issues
138 and (returndate is null)");
139 $sth->execute($itemrec->{'itemnumber'});
140 if (my $issuerec=$sth->fetchrow_hashref) {
142 my $sth= $dbh->prepare("select * from borrowers where
143 (borrowernumber = ?)");
144 $sth->execute($issuerec->{'borrowernumber'});
145 $env->{'borrowernumber'}=$issuerec->{'borrowernumber'};
146 $borrower = $sth->fetchrow_hashref;
147 $borrowernumber = $issuerec->{'borrowernumber'};
148 $itemno = $issuerec->{'itemnumber'};
149 $amt_owing = returnrecord($env,$dbh,$borrowernumber,$itemno);
150 $reason = "Returned";
153 updatelastseen($env,$dbh,$itemrec->{'itemnumber'});
154 $reason = "Item not issued";
156 my ($resfound,$resrec) = find_reserves($env,$dbh,$itemrec->{'itemnumber'});
157 if ($resfound eq "y") {
158 my $btsh = $dbh->prepare("select * from borrowers
159 where borrowernumber = ?");
160 $btsh->execute($resrec->{'borrowernumber'});
161 my $resborrower = $btsh->fetchrow_hashref;
162 #printreserve($env,$resrec,$resborrower,$itemrec);
163 my $mess = "Reserved for collection at branch $resrec->{'branchcode'}";
164 C4::InterfaceCDK::error_msg($env,$mess);
169 $reason = "Item not found";
171 return ($reason,$borrowernumber,$borrower,$itemno,$itemrec,$amt_owing);
175 # FIXME - Only used in &C4::Circulation::Main::previousissue,
176 # &checkissue, C4/Circulation.pm, and tkperl/tkcirc, all of which
177 # appear to be obsolete. Presumably this function is obsolete as well.
178 # Otherwise, it needs a POD.
180 # mark items as returned
181 my ($env,$dbh,$borrowernumber,$itemno)=@_;
182 #my $amt_owing = calc_odues($env,$dbh,$borrowernumber,$itemno);
183 my @datearr = localtime(time);
184 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
185 my $sth = $dbh->prepare("update issues set returndate = now(), branchcode = ? where
186 (borrowernumber = ?) and (itemnumber = ?)
187 and (returndate is null)");
188 $sth->execute($env->{'branchcode'},$borrowernumber,$itemno);
190 updatelastseen($env,$dbh,$itemno);
191 # check for overdue fine
193 my $sth = $dbh->prepare("select * from accountlines
194 where (borrowernumber = ?)
196 and (accounttype = 'FU' or accounttype='O')");
197 $sth->execute($borrowernumber,$itemno);
198 if (my $data = $sth->fetchrow_hashref) {
199 # alter fine to show that the book has been returned.
200 my $usth = $dbh->prepare("update accountlines
201 set accounttype = 'F'
202 where (borrowernumber = ?)
204 and (accountno = ?) ");
205 $usth->execute($borrowernumber,$itemno,$data->{'accountno'});
207 $oduecharge = $data->{'amountoutstanding'};
210 # check for charge made for lost book
211 my $sth = $dbh->prepare("select * from accountlines
212 where (borrowernumber = ?)
214 and (accounttype = 'L')");
215 $sth->execute($borrowernumber,$itemno);
216 if (my $data = $sth->fetchrow_hashref) {
217 # writeoff this amount
219 my $amount = $data->{'amount'};
220 my $acctno = $data->{'accountno'};
222 if ($data->{'amountoutstanding'} == $amount) {
223 $offset = $data->{'amount'};
226 $offset = $amount - $data->{'amountoutstanding'};
227 $amountleft = $data->{'amountoutstanding'} - $amount;
229 my $usth = $dbh->prepare("update accountlines
230 set accounttype = 'LR',amountoutstanding='0'
231 where (borrowernumber = ?)
233 and (accountno = ?) ");
234 $usth->execute($borrowernumber,$itemno,$acctno);
236 my $nextaccntno = C4::Accounts::getnextacctno($env,$borrowernumber,$dbh);
237 $usth = $dbh->prepare("insert into accountlines
238 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
239 values (?,?,now(),?,'Book Returned','CR',?)");
240 $usth->execute($borrowernumber,$nextaccntno,0-$amount,$amountleft);
242 $uquery = "insert into accountoffsets
243 (borrowernumber, accountno, offsetaccount, offsetamount)
245 $usth = $dbh->prepare("");
246 $usth->execute($borrowernumber,$data->{'accountno'},$nextaccntno,$offset);
250 UpdateStats($env,'branch','return','0','',$itemno);
254 # FIXME - Only used in tkperl/tkcirc. Presumably this function is
256 # Otherwise, it needs a POD.
258 # calculate overdue fees
259 my ($env,$dbh,$borrowernumber,$itemno)=@_;
264 # This function is only used in &checkissue and &returnrecord, both of
265 # which appear to be obsolete. So presumably this function is obsolete
267 # Otherwise, it needs a POD.
269 my ($env,$dbh,$itemnumber)= @_;
270 my $br = $env->{'branchcode'};
271 my $sth = $dbh->prepare("update items
272 set datelastseen = now(), holdingbranch = ?
273 where (itemnumber = ?)");
274 $sth->execute($br,$itemnumber);
280 # FIXME - There's also a &C4::Circulation::Circ2::find_reserves, but
281 # that one looks rather different.
282 # FIXME - This is only used in &checkissue, which appears to be
283 # obsolete. So presumably this function is obsolete too.
285 my ($env,$dbh,$itemno) = @_;
286 warn "!!!!! SHOULD NOT BE HERE : Returns::find_reserves is deprecated !!!";
287 my $itemdata = GetBiblioFromItemNumber($itemno);
288 my $sth = $dbh->prepare("select * from reserves where found is null
289 and biblionumber = ? and cancellationdate is NULL
290 order by priority,reservedate ");
291 $sth->execute($itemdata->{'biblionumber'};
294 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
295 if ($resrec->{'found'} eq "W") {
296 if ($resrec->{'itemnumber'} eq $itemno) {
299 } elsif ($resrec->{'constrainttype'} eq "a") {
302 my $consth = $dbh->prepare("select * from reserveconstraints where borrowernumber = ? and reservedate = ? and biblionumber = ? and biblioitemnumber = ?");
303 $consth->execute($resrec->{'borrowernumber'},$resrec->{'reservedate'},$resrec->{'biblionumber'},$itemdata->{'biblioitemnumber'});
304 if (my $conrec=$consth->fetchrow_hashref) {
305 if ($resrec->{'constrainttype'} eq "o") {
309 if ($resrec->{'constrainttype'} eq "e") {
315 if ($resfound eq "y") {
316 my $updsth = $dbh->prepare("update reserves
317 set found = 'W',itemnumber = ?
318 where borrowernumber = ?
320 and biblionumber = ?");
321 $updsth->execute($itemno,$resrec->{'borrowernumber'},$resrec->{'reservedate'},$resrec->{'biblionumber'});
323 my $itbr = $resrec->{'branchcode'};
324 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
325 my $updsth = $dbh->prepare("update items
326 set holdingbranch = 'TR'
327 where itemnumber = ?");
328 $updsth->execute($itemno);
334 return ($resfound,$resrec);