1 package C4::Circulation::Returns; #assumes C4/Circulation/Returns
3 #package to deal with Returns
4 #written 3/11/99 by olwen@katipo.co.nz
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
30 use C4::Circulation::Main;
31 # FIXME - C4::Circulation::Main and C4::Circulation::Returns
32 # use each other, so functions get redefined.
38 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 # set the version for version checking
44 @EXPORT = qw(&returnrecord &calc_odues &Returns);
45 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
47 # your exported package globals go here,
48 # as well as any optionally exported functions
50 @EXPORT_OK = qw($Var1 %Hashit);
53 # non-exported package globals go here
54 use vars qw(@more $stuff);
56 # initalize package globals, first exported ones
61 # then the others (which are still accessible as $Some::Module::stuff)
65 # all file-scoped lexicals must be created before
66 # the functions below that use them.
68 # file-private lexicals go here
72 # here's a file-private function as a closure,
73 # callable as &$priv_func; it cannot be prototyped.
78 # make all your functions, whether exported or not;
82 my $dbh = C4::Context->dbh;
96 # until (($reason eq "Circ") || ($reason eq "Quit")) {
97 until ($reason ne "") {
99 returnwindow($env,"Enter Returns",
100 $item,\@items,$borrower,$amt_owing,$odues,$dbh,$resp); #C4::Circulation
101 #debug_msg($env,"item = $item");
102 #if (($reason ne "Circ") && ($reason ne "Quit")) {
105 ($resp,$bornum,$borrower,$itemno,$itemrec,$amt_owing) =
106 checkissue($env,$dbh,$item);
108 ($issues,$odues,$amt_owing) = borrdata2($env,$bornum);
115 #if ($resp eq "Returned") {
116 if ($itemno ne "" ) {
117 my $item = itemnodata($env,$dbh,$itemno);
118 my $fmtitem = C4::Circulation::Issues::formatitem($env,$item,"",$amt_owing);
119 unshift @items,$fmtitem;
120 if ($items[20] > "") {
124 #} elsif ($resp ne "") {
125 # error_msg($env,"$resp");
127 #if ($resp ne "Returned") {
128 # error_msg($env,"$resp");
139 my ($env,$dbh, $item) = @_;
147 my $query = "select * from items,biblio
148 where barcode = '$item'
149 and (biblio.biblionumber=items.biblionumber)";
150 my $sth=$dbh->prepare($query);
152 if ($itemrec=$sth->fetchrow_hashref) {
154 $itemno = $itemrec->{'itemnumber'};
155 $query = "select * from issues
156 where (itemnumber='$itemrec->{'itemnumber'}')
157 and (returndate is null)";
158 my $sth=$dbh->prepare($query);
160 if (my $issuerec=$sth->fetchrow_hashref) {
162 $query = "select * from borrowers where
163 (borrowernumber = '$issuerec->{'borrowernumber'}')";
164 my $sth= $dbh->prepare($query);
166 $env->{'bornum'}=$issuerec->{'borrowernumber'};
167 $borrower = $sth->fetchrow_hashref;
168 $bornum = $issuerec->{'borrowernumber'};
169 $itemno = $issuerec->{'itemnumber'};
170 $amt_owing = returnrecord($env,$dbh,$bornum,$itemno);
171 $reason = "Returned";
174 updatelastseen($env,$dbh,$itemrec->{'itemnumber'});
175 $reason = "Item not issued";
177 my ($resfound,$resrec) = find_reserves($env,$dbh,$itemrec->{'itemnumber'});
178 if ($resfound eq "y") {
179 my $bquery = "select * from borrowers
180 where borrowernumber = '$resrec->{'borrowernumber'}'";
181 my $btsh = $dbh->prepare($bquery);
183 my $resborrower = $btsh->fetchrow_hashref;
184 #printreserve($env,$resrec,$resborrower,$itemrec);
185 my $mess = "Reserved for collection at branch $resrec->{'branchcode'}";
186 C4::InterfaceCDK::error_msg($env,$mess);
191 $reason = "Item not found";
193 return ($reason,$bornum,$borrower,$itemno,$itemrec,$amt_owing);
198 # mark items as returned
199 my ($env,$dbh,$bornum,$itemno)=@_;
200 #my $amt_owing = calc_odues($env,$dbh,$bornum,$itemno);
201 my @datearr = localtime(time);
202 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
203 my $query = "update issues set returndate = now(), branchcode ='$env->{'branchcode'}' where
204 (borrowernumber = '$bornum') and (itemnumber = '$itemno')
205 and (returndate is null)";
206 my $sth = $dbh->prepare($query);
209 updatelastseen($env,$dbh,$itemno);
210 # check for overdue fine
212 my $query = "select * from accountlines
213 where (borrowernumber = '$bornum')
214 and (itemnumber = '$itemno')
215 and (accounttype = 'FU' or accounttype='O')";
216 my $sth = $dbh->prepare($query);
218 if (my $data = $sth->fetchrow_hashref) {
219 # alter fine to show that the book has been returned.
220 my $uquery = "update accountlines
221 set accounttype = 'F'
222 where (borrowernumber = '$bornum')
223 and (itemnumber = '$itemno')
224 and (accountno = '$data->{'accountno'}') ";
225 my $usth = $dbh->prepare($uquery);
228 $oduecharge = $data->{'amountoutstanding'};
231 # check for charge made for lost book
232 my $query = "select * from accountlines
233 where (borrowernumber = '$bornum')
234 and (itemnumber = '$itemno')
235 and (accounttype = 'L')";
236 my $sth = $dbh->prepare($query);
238 if (my $data = $sth->fetchrow_hashref) {
239 # writeoff this amount
241 my $amount = $data->{'amount'};
242 my $acctno = $data->{'accountno'};
244 if ($data->{'amountoutstanding'} == $amount) {
245 $offset = $data->{'amount'};
248 $offset = $amount - $data->{'amountoutstanding'};
249 $amountleft = $data->{'amountoutstanding'} - $amount;
251 my $uquery = "update accountlines
252 set accounttype = 'LR',amountoutstanding='0'
253 where (borrowernumber = '$bornum')
254 and (itemnumber = '$itemno')
255 and (accountno = '$acctno') ";
256 my $usth = $dbh->prepare($uquery);
259 my $nextaccntno = C4::Accounts::getnextacctno($env,$bornum,$dbh);
260 $uquery = "insert into accountlines
261 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
262 values ($bornum,$nextaccntno,now(),0-$amount,'Book Returned',
264 $usth = $dbh->prepare($uquery);
267 $uquery = "insert into accountoffsets
268 (borrowernumber, accountno, offsetaccount, offsetamount)
269 values ($bornum,$data->{'accountno'},$nextaccntno,$offset)";
270 $usth = $dbh->prepare($uquery);
275 UpdateStats($env,'branch','return','0','',$itemno);
280 # calculate overdue fees
281 my ($env,$dbh,$bornum,$itemno)=@_;
287 my ($env,$dbh,$itemnumber)= @_;
288 my $br = $env->{'branchcode'};
289 my $query = "update items
290 set datelastseen = now(), holdingbranch = '$br'
291 where (itemnumber = '$itemnumber')";
292 my $sth = $dbh->prepare($query);
298 my ($env,$dbh,$itemno) = @_;
299 my $itemdata = itemnodata($env,$dbh,$itemno);
300 my $query = "select * from reserves where found is null
301 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
302 order by priority,reservedate ";
303 my $sth = $dbh->prepare($query);
307 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
308 if ($resrec->{'found'} eq "W") {
309 if ($resrec->{'itemnumber'} eq $itemno) {
312 } elsif ($resrec->{'constrainttype'} eq "a") {
315 my $conquery = "select * from reserveconstraints where borrowernumber
316 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
317 my $consth = $dbh->prepare($conquery);
319 if (my $conrec=$consth->fetchrow_hashref) {
320 if ($resrec->{'constrainttype'} eq "o") {
324 if ($resrec->{'constrainttype'} eq "e") {
330 if ($resfound eq "y") {
331 my $updquery = "update reserves
332 set found = 'W',itemnumber='$itemno'
333 where borrowernumber = $resrec->{'borrowernumber'}
334 and reservedate = '$resrec->{'reservedate'}'
335 and biblionumber = $resrec->{'biblionumber'}";
336 my $updsth = $dbh->prepare($updquery);
339 my $itbr = $resrec->{'branchcode'};
340 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
341 my $updquery = "update items
342 set holdingbranch = 'TR'
343 where itemnumber = $itemno";
344 my $updsth = $dbh->prepare($updquery);
351 return ($resfound,$resrec);
353 END { } # module clean-up code here (global destructor)