1 package C4::Circulation::Circ2;
3 #package to deal with Returns
4 #written 3/11/99 by olwen@katipo.co.nz
11 #use C4::InterfaceCDK;
12 #use C4::Circulation::Main;
14 #use C4::Circulation::Renewals;
20 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
22 # set the version for version checking
26 @EXPORT = qw(&getbranches &getprinters &getpatroninformation ¤tissues &getiteminformation &findborrower &issuebook &returnbook);
27 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
29 # your exported package globals go here,
30 # as well as any optionally exported functions
32 @EXPORT_OK = qw($Var1 %Hashit);
35 # non-exported package globals go here
36 #use vars qw(@more $stuff);
38 # initalize package globals, first exported ones
43 # then the others (which are still accessible as $Some::Module::stuff)
47 # all file-scoped lexicals must be created before
48 # the functions below that use them.
50 # file-private lexicals go here
54 # here's a file-private function as a closure,
55 # callable as &$priv_func; it cannot be prototyped.
60 # make all your functions, whether exported or not;
67 my $sth=$dbh->prepare("select * from branches");
69 while (my $branch=$sth->fetchrow_hashref) {
70 # (next) if ($branch->{'branchcode'} eq 'TR');
71 $branches{$branch->{'branchcode'}}=$branch;
82 my $sth=$dbh->prepare("select * from printers");
84 while (my $printer=$sth->fetchrow_hashref) {
85 $printers{$printer->{'printqueue'}}=$printer;
93 sub getpatroninformation {
95 my ($env, $borrowernumber,$cardnumber) = @_;
98 open O, ">>/root/tkcirc.out";
99 print O "Looking up patron $borrowernumber / $cardnumber\n";
100 if ($borrowernumber) {
101 $sth=$dbh->prepare("select * from borrowers where borrowernumber=$borrowernumber");
102 } elsif ($cardnumber) {
103 $sth=$dbh->prepare("select * from borrowers where cardnumber=$cardnumber");
105 # error condition. This subroutine must be called with either a
106 # borrowernumber or a card number.
107 $env->{'apierror'}="invalid borrower information passed to getpatroninformation subroutine";
111 my $borrower=$sth->fetchrow_hashref;
112 my $flags=patronflags($env, $borrower, $dbh);
115 print O "$borrower->{'surname'} <---\n";
117 $borrower->{'flags'}=$flags;
118 return($borrower, $flags);
125 sub getiteminformation {
126 # returns a hash of item information given either the itemnumber or the barcode
127 my ($env, $itemnumber, $barcode) = @_;
131 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.itemnumber=$itemnumber and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
133 my $q_barcode=$dbh->quote($barcode);
134 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.barcode=$q_barcode and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
136 $env->{'apierror'}="getiteminformation() subroutine must be called with either an itemnumber or a barcode";
141 my $iteminformation=$sth->fetchrow_hashref;
143 if ($iteminformation) {
144 $sth=$dbh->prepare("select date_due from issues where itemnumber=$iteminformation->{'itemnumber'} and isnull(returndate)");
146 my ($date_due) = $sth->fetchrow;
147 $iteminformation->{'date_due'}=$date_due;
149 #$iteminformation->{'dewey'}=~s/0*$//;
150 ($iteminformation->{'dewey'} == 0) && ($iteminformation->{'dewey'}='');
151 $sth=$dbh->prepare("select * from itemtypes where itemtype='$iteminformation->{'itemtype'}'");
153 my $itemtype=$sth->fetchrow_hashref;
154 $iteminformation->{'loanlength'}=$itemtype->{'loanlength'};
158 return($iteminformation);
162 # returns an array of borrower hash references, given a cardnumber or a partial
164 my ($env, $key) = @_;
167 my $q_key=$dbh->quote($key);
168 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
171 my ($borrower)=$sth->fetchrow_hashref;
172 push (@borrowers, $borrower);
174 $q_key=$dbh->quote("$key%");
176 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
178 while (my $borrower = $sth->fetchrow_hashref) {
179 push (@borrowers, $borrower);
189 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
191 my $iteminformation=getiteminformation($env, 0, $barcode);
193 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
195 if ($patroninformation->{'gonenoaddress'}) {
196 $rejected="Patron is gone, with no known address.";
199 if ($patroninformation->{'lost'}) {
200 $rejected="Patron's card has been reported lost.";
203 if ($patroninformation->{'debarred'}) {
204 $rejected="Patron is Debarred";
207 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
208 if ($amount>5 && $patroninformation->{'categorycode'} ne 'L' &&
209 $patroninformation->{'categorycode'} ne 'W' &&
210 $patroninformation->{'categorycode'} ne 'I'
211 && $patroninformation->{'categorycode'} ne 'B' &&
212 $patroninformation->{'categorycode'} ne 'P') {
213 $rejected=sprintf "Patron owes \$%.02f.", $amount;
216 unless ($iteminformation) {
217 $rejected="$barcode is not a valid barcode.";
220 if ($iteminformation->{'notforloan'} == 1) {
221 $rejected="Item not for loan.";
224 if ($iteminformation->{'wthdrawn'} == 1) {
225 $rejected="Item withdrawn.";
228 if ($iteminformation->{'restricted'} == 1) {
229 $rejected="Restricted item.";
232 if ($iteminformation->{'itemtype'} eq 'REF') {
233 $rejected="Reference item: Not for loan.";
236 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
237 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
238 # Already issued to current borrower
239 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
240 if ($renewstatus == 0) {
241 $rejected="No more renewals allowed for this item.";
244 if ($responses->{4} eq '') {
246 $question="Book is issued to this borrower.\nRenew?";
249 } elsif ($responses->{4} eq 'Y') {
250 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
252 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
253 $iteminformation->{'charge'}=$charge;
255 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
256 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
263 } elsif ($currentborrower ne '') {
264 my ($currborrower, $cbflags)=getpatroninformation($env,$currentborrower,0);
265 if ($responses->{1} eq '') {
267 $question="Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
270 } elsif ($responses->{1} eq 'Y') {
271 returnbook($env,$iteminformation->{'barcode'});
278 my ($resbor, $resrec) = checkreserve($env, $dbh, $iteminformation->{'itemnumber'});
280 if ($resbor eq $patroninformation->{'borrowernumber'}) {
281 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
282 my $rsth = $dbh->prepare($rquery);
285 } elsif ($resbor ne "") {
286 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
287 if ($responses->{2} eq '') {
289 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $resrec->{'reservedate'}\nAllow issue?";
292 } elsif ($responses->{2} eq 'N') {
293 #printreserve($env, $resrec, $resborrower, $iteminformation);
297 if ($responses->{3} eq '') {
299 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
302 } elsif ($responses->{3} eq 'Y') {
303 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
304 my $rsth = $dbh->prepare($rquery);
312 unless (($question) || ($rejected) || ($noissue)) {
314 if ($iteminformation->{'loanlength'}) {
315 $loanlength=$iteminformation->{'loanlength'};
318 my $datedue=time+($loanlength)*86400;
319 my @datearr = localtime($datedue);
320 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
321 if ($env->{'datedue'}) {
322 $dateduef=$env->{'datedue'};
324 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
325 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
328 $iteminformation->{'issues'}++;
329 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'} where itemnumber=$iteminformation->{'itemnumber'}");
332 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
334 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
335 $iteminformation->{'charge'}=$charge;
337 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
340 if ($iteminformation->{'charge'}) {
341 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
344 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
348 my ($dbh,$itemno)=@_;
349 my $query="update items set itemlost=0 where itemnumber=$itemno";
350 my $sth=$dbh->prepare($query);
356 my ($env, $barcode) = @_;
357 my ($messages, $overduecharge);
359 my ($iteminformation) = getiteminformation($env, 0, $barcode);
361 if ($iteminformation) {
362 my $sth=$dbh->prepare("select * from issues where (itemnumber='$iteminformation->{'itemnumber'}') and (returndate is null)");
364 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
365 updatelastseen($env,$dbh,$iteminformation->{'itemnumber'});
366 updateitemlost($dbh,$iteminformation->{'itemnumber'});
367 if ($currentborrower) {
368 ($borrower)=getpatroninformation($env,$currentborrower,0);
369 my @datearr = localtime(time);
370 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
371 my $query = "update issues set returndate = now(), branchcode ='$env->{'branchcode'}' where (borrowernumber = $borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (returndate is null)";
372 my $sth = $dbh->prepare($query);
377 # check for overdue fine
380 $sth=$dbh->prepare("select * from accountlines where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (accounttype='FU' or accounttype='O')");
382 # alter fine to show that the book has been returned
383 if (my $data = $sth->fetchrow_hashref) {
384 my $usth=$dbh->prepare("update accountlines set accounttype='F' where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber=$iteminformation->{'itemnumber'}) and (acccountno='$data->{'accountno'}')");
387 $overduecharge=$data->{'amountoutstanding'};
391 if ($iteminformation->{'itemlost'} eq '1'){
392 # check for charge made for lost book
393 my $query="select * from accountlines where (itemnumber =
394 $iteminformation->{'itemnumber'}) and (accounttype='L' or accounttype='Rep')
397 $sth=$dbh->prepare($query);
399 if (my $data = $sth->fetchrow_hashref) {
400 # writeoff this amount
402 my $amount = $data->{'amount'};
403 my $acctno = $data->{'accountno'};
406 if ($data->{'amountoutstanding'} == $amount) {
407 $offset = $data->{'amount'};
410 $offset = $amount - $data->{'amountoutstanding'};
411 $amountleft = $data->{'amountoutstanding'} - $amount;
413 my $uquery = "update accountlines
414 set accounttype = 'LR',amountoutstanding='0'
415 where (borrowernumber = $data->{'borrowernumber'})
416 and (itemnumber = $iteminformation->{'itemnumber'})
417 and (accountno = '$acctno') ";
419 my $usth = $dbh->prepare($uquery);
422 #check if any credit is left if so writeoff other accounts]
423 my $nextaccntno = getnextacctno($env,$data->{'borrowernumber'},$dbh);
424 if ($amountleft < 0){
427 if ($amountleft > 0){
429 my $query = "select * from accountlines
430 where (borrowernumber = '$data->{'borrowernumber'}') and (amountoutstanding >0)
432 my $sth = $dbh->prepare($query);
434 # offset transactions
437 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
438 if ($accdata->{'amountoutstanding'} < $amountleft) {
440 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
442 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
445 my $thisacct = $accdata->{accountno};
446 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
447 where (borrowernumber = '$data->{'borrowernumber'}') and (accountno='$thisacct')";
448 my $usth = $dbh->prepare($updquery);
451 $updquery = "insert into accountoffsets
452 (borrowernumber, accountno, offsetaccount, offsetamount)
454 ($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos)";
455 my $usth = $dbh->prepare($updquery);
460 if ($amountleft > 0){
464 my $desc="Book Returned ".$iteminformation->{'barcode'};
465 $uquery = "insert into accountlines
466 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
467 values ($data->{'borrowernumber'},$nextaccntno,now(),0-$amount,'$desc',
469 $usth = $dbh->prepare($uquery);
473 $uquery = "insert into accountoffsets
474 (borrowernumber, accountno, offsetaccount, offsetamount)
475 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
476 $usth = $dbh->prepare($uquery);
479 $uquery="update items set itemnotes='' where itemnumber=$iteminformation->{'itemnumber'}";
480 $usth = $dbh->prepare($uquery);
486 my ($resfound,$resrec) = find_reserves($env, $dbh, $iteminformation->{'itemnumber'});
487 if ($resfound eq 'y') {
488 my ($borrower) = getpatroninformation($env,$resrec->{'borrowernumber'},0);
489 #printreserve($env,$resrec,$resborrower,$itemrec);
490 my ($branches) = getbranches();
491 my $branchname=$branches->{$resrec->{'branchcode'}}->{'branchname'};
492 push (@$messages, "<b><font color=red>RESERVED</font></b> for collection by $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'}) at $branchname");
494 UpdateStats($env,$env->{'branchcode'},'return','0','',$iteminformation->{'itemnumber'});
497 return ($iteminformation, $borrower, $messages, $overduecharge);
502 # Original subroutine for Circ2.pm
504 my ($env,$patroninformation,$dbh) = @_;
505 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
508 $flaginfo{'message'}=sprintf "Patron owes \$%.02f", $amount;
510 $flaginfo{'noissues'}=1;
512 $flags{'CHARGES'}=\%flaginfo;
513 } elsif ($amount < 0){
516 $flaginfo{'message'}=sprintf "Patron has credit of \$%.02f", $amount;
517 $flags{'CHARGES'}=\%flaginfo;
519 if ($patroninformation->{'gonenoaddress'} == 1) {
521 $flaginfo{'message'}='Borrower has no valid address.';
522 $flaginfo{'noissues'}=1;
523 $flags{'GNA'}=\%flaginfo;
525 if ($patroninformation->{'lost'} == 1) {
527 $flaginfo{'message'}='Borrower\'s card reported lost.';
528 $flaginfo{'noissues'}=1;
529 $flags{'LOST'}=\%flaginfo;
531 if ($patroninformation->{'debarred'} == 1) {
533 $flaginfo{'message'}='Borrower is Debarred.';
534 $flaginfo{'noissues'}=1;
535 $flags{'DBARRED'}=\%flaginfo;
537 if ($patroninformation->{'borrowernotes'}) {
539 $flaginfo{'message'}="$patroninformation->{'borrowernotes'}";
540 $flags{'NOTES'}=\%flaginfo;
542 my ($odues, $itemsoverdue) = checkoverdues($env,$patroninformation->{'borrowernumber'},$dbh);
545 $flaginfo{'message'}="Yes";
546 $flaginfo{'itemlist'}=$itemsoverdue;
547 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
548 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
550 $flags{'ODUES'}=\%flaginfo;
552 my ($nowaiting,$itemswaiting) = checkwaiting($env,$dbh,$patroninformation->{'borrowernumber'});
555 $flaginfo{'message'}="Reserved items available";
556 $flaginfo{'itemlist'}=$itemswaiting;
557 $flaginfo{'itemfields'}=['barcode', 'title', 'author', 'dewey', 'subclass', 'holdingbranch'];
558 $flags{'WAITING'}=\%flaginfo;
567 # From Main.pm, modified to return a list of overdueitems, in addition to a count
568 #checks whether a borrower has overdue items
569 my ($env,$bornum,$dbh)=@_;
570 my @datearr = localtime;
571 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
574 my $query = "Select * from issues,biblio,biblioitems,items where items.biblioitemnumber=biblioitems.biblioitemnumber and items.biblionumber=biblio.biblionumber and issues.itemnumber=items.itemnumber and borrowernumber=$bornum and returndate is NULL and date_due < '$today'";
575 my $sth=$dbh->prepare($query);
577 while (my $data = $sth->fetchrow_hashref) {
578 push (@overdueitems, $data);
582 return ($count, \@overdueitems);
586 # Stolen from Returns.pm
587 my ($env,$dbh,$itemnumber)= @_;
588 my $br = $env->{'branchcode'};
589 my $query = "update items
590 set datelastseen = now(), holdingbranch = '$br'
591 where (itemnumber = '$itemnumber')";
592 my $sth = $dbh->prepare($query);
597 sub currentborrower {
598 # Original subroutine for Circ2.pm
599 my ($env, $itemnumber, $dbh) = @_;
600 my $q_itemnumber=$dbh->quote($itemnumber);
601 my $sth=$dbh->prepare("select borrowers.borrowernumber from
602 issues,borrowers where issues.itemnumber=$q_itemnumber and
603 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
606 my ($previousborrower)=$sth->fetchrow;
607 return($previousborrower);
611 # Stolen from Main.pm
612 # Check for reserves for biblio
613 my ($env,$dbh,$itemnum)=@_;
615 my $query = "select * from reserves,items
616 where (items.itemnumber = '$itemnum')
617 and (reserves.cancellationdate is NULL)
618 and (items.biblionumber = reserves.biblionumber)
619 and ((reserves.found = 'W')
620 or (reserves.found is null))
622 my $sth = $dbh->prepare($query);
625 my $data=$sth->fetchrow_hashref;
626 while ($data && $resbor eq '') {
628 my $const = $data->{'constrainttype'};
630 $resbor = $data->{'borrowernumber'};
633 my $cquery = "select * from reserveconstraints,items
634 where (borrowernumber='$data->{'borrowernumber'}')
635 and reservedate='$data->{'reservedate'}'
636 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
637 and (items.itemnumber=$itemnum and
638 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
639 my $csth = $dbh->prepare($cquery);
641 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
643 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
645 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
649 $data=$sth->fetchrow_hashref;
652 return ($resbor,$resrec);
656 # New subroutine for Circ2.pm
657 my ($env, $borrower) = @_;
661 my $borrowernumber=$borrower->{'borrowernumber'};
663 if ($env->{'todaysissues'}) {
664 my @datearr = localtime(time());
665 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
666 $crit=" and issues.timestamp like '$today%' ";
668 if ($env->{'nottodaysissues'}) {
669 my @datearr = localtime(time());
670 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
671 $crit=" and !(issues.timestamp like '$today%') ";
673 my $select="select * from issues,items,biblioitems,biblio where
674 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
675 items.biblionumber=biblio.biblionumber and
676 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
677 $crit order by issues.timestamp desc";
679 my $sth=$dbh->prepare($select);
681 while (my $data = $sth->fetchrow_hashref) {
682 $data->{'dewey'}=~s/0*$//;
683 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
684 my @datearr = localtime(time());
685 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
686 +1)).sprintf ("%0.2d", $datearr[3]);
687 my $datedue=$data->{'date_due'};
689 if ($datedue < $todaysdate) {
690 $data->{'overdue'}=1;
692 my $itemnumber=$data->{'itemnumber'};
693 $currentissues{$counter}=$data;
698 return(\%currentissues);
703 # check for reserves waiting
704 my ($env,$dbh,$bornum)=@_;
706 my $query = "select * from reserves
707 where (borrowernumber = '$bornum')
708 and (reserves.found='W') and cancellationdate is NULL";
709 my $sth = $dbh->prepare($query);
712 if (my $data=$sth->fetchrow_hashref) {
713 @itemswaiting[$cnt] =$data;
717 return ($cnt,\@itemswaiting);
722 # Stolen from Accounts.pm
723 #take borrower number
724 #check accounts and list amounts owing
725 my ($env,$bornumber,$dbh,$date)=@_;
726 my $select="Select sum(amountoutstanding) from accountlines where
727 borrowernumber=$bornumber and amountoutstanding<>0";
729 $select.=" and date < '$date'";
732 my $sth=$dbh->prepare($select);
735 while (my $data=$sth->fetchrow_hashref){
736 $total=$total+$data->{'sum(amountoutstanding)'};
739 # output(1,2,"borrower owes $total");
741 # # output(1,2,"borrower owes $total");
743 # reconcileaccount($env,$dbh,$bornumber,$total);
751 # Stolen from Renewals.pm
752 # check renewal status
753 my ($env,$dbh,$bornum,$itemno)=@_;
756 my $q1 = "select * from issues
757 where (borrowernumber = '$bornum')
758 and (itemnumber = '$itemno')
759 and returndate is null";
760 my $sth1 = $dbh->prepare($q1);
762 if (my $data1 = $sth1->fetchrow_hashref) {
763 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
764 where (items.itemnumber = '$itemno')
765 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
766 and (biblioitems.itemtype = itemtypes.itemtype)";
767 my $sth2 = $dbh->prepare($q2);
769 if (my $data2=$sth2->fetchrow_hashref) {
770 $renews = $data2->{'renewalsallowed'};
772 if ($renews > $data1->{'renewals'}) {
782 # Stolen from Renewals.pm
783 # mark book as renewed
784 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
785 $datedue=$env->{'datedue'};
786 if ($datedue eq "" ) {
788 my $query= "Select * from biblioitems,items,itemtypes
789 where (items.itemnumber = '$itemno')
790 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
791 and (biblioitems.itemtype = itemtypes.itemtype)";
792 my $sth=$dbh->prepare($query);
794 if (my $data=$sth->fetchrow_hashref) {
795 $loanlength = $data->{'loanlength'}
799 my $datedu = time + ($loanlength * 86400);
800 my @datearr = localtime($datedu);
801 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
803 my @date = split("-",$datedue);
804 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
805 my $issquery = "select * from issues where borrowernumber='$bornum' and
806 itemnumber='$itemno' and returndate is null";
807 my $sth=$dbh->prepare($issquery);
809 my $issuedata=$sth->fetchrow_hashref;
811 my $renews = $issuedata->{'renewals'} +1;
812 my $updquery = "update issues
813 set date_due = '$datedue', renewals = '$renews'
814 where borrowernumber='$bornum' and
815 itemnumber='$itemno' and returndate is null";
816 my $sth=$dbh->prepare($updquery);
824 # Stolen from Issues.pm
825 # calculate charges due
826 my ($env, $dbh, $itemno, $bornum)=@_;
829 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
830 my $sth1= $dbh->prepare($q1);
832 if (my $data1=$sth1->fetchrow_hashref) {
833 $item_type = $data1->{'itemtype'};
834 $charge = $data1->{'rentalcharge'};
835 my $q2 = "select rentaldiscount from borrowers,categoryitem
836 where (borrowers.borrowernumber = '$bornum')
837 and (borrowers.categorycode = categoryitem.categorycode)
838 and (categoryitem.itemtype = '$item_type')";
839 my $sth2=$dbh->prepare($q2);
841 if (my $data2=$sth2->fetchrow_hashref) {
842 my $discount = $data2->{'rentaldiscount'};
843 $charge = ($charge *(100 - $discount)) / 100;
852 #Stolen from Issues.pm
853 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
854 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
855 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
856 my $sth = $dbh->prepare($query);
863 # Stolen from Accounts.pm
864 my ($env,$bornumber,$dbh)=@_;
866 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
867 my $sth = $dbh->prepare($query);
869 if (my $accdata=$sth->fetchrow_hashref){
870 $nextaccntno = $accdata->{'accountno'} + 1;
873 return($nextaccntno);
877 # Stolen from Returns.pm
878 my ($env,$dbh,$itemno) = @_;
879 my ($itemdata) = getiteminformation($env,$itemno,0);
880 my $query = "select * from reserves where
881 ((reserves.found = 'W')
882 or (reserves.found is null))
883 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
884 order by priority,reservedate ";
885 my $sth = $dbh->prepare($query);
890 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
892 if ($resrec->{'found'} eq "W") {
893 if ($resrec->{'itemnumber'} eq $itemno) {
897 if ($resrec->{'constrainttype'} eq "a") {
900 my $conquery = "select * from reserveconstraints where borrowernumber
901 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
902 my $consth = $dbh->prepare($conquery);
904 if (my $conrec=$consth->fetchrow_hashref) {
905 if ($resrec->{'constrainttype'} eq "o") {
909 if ($resrec->{'constrainttype'} eq "e") {
915 if ($resfound eq "y") {
916 my $updquery = "update reserves
917 set found = 'W',itemnumber='$itemno'
918 where borrowernumber = $resrec->{'borrowernumber'}
919 and reservedate = '$resrec->{'reservedate'}'
920 and biblionumber = $resrec->{'biblionumber'}";
921 my $updsth = $dbh->prepare($updquery);
924 my $itbr = $resrec->{'branchcode'};
925 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
926 my $updquery = "update items
927 set holdingbranch = 'TR'
928 where itemnumber = $itemno";
929 my $updsth = $dbh->prepare($updquery);
936 return ($resfound,$lastrec);
939 END { } # module clean-up code here (global destructor)