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 my $nextaccntno = getnextacctno($env,$data->{'borrowernumber'},$dbh);
423 my $desc="Book Returned ".$iteminformation->{'barcode'};
424 $uquery = "insert into accountlines
425 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
426 values ($data->{'borrowernumber'},$nextaccntno,now(),0-$amount,'$desc',
428 $usth = $dbh->prepare($uquery);
432 $uquery = "insert into accountoffsets
433 (borrowernumber, accountno, offsetaccount, offsetamount)
434 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
435 $usth = $dbh->prepare($uquery);
438 $uquery="update items set itemnotes='' where itemnumber=$iteminformation->{'itemnumber'}";
439 $usth = $dbh->prepare($uquery);
445 my ($resfound,$resrec) = find_reserves($env, $dbh, $iteminformation->{'itemnumber'});
446 if ($resfound eq 'y') {
447 my ($borrower) = getpatroninformation($env,$resrec->{'borrowernumber'},0);
448 #printreserve($env,$resrec,$resborrower,$itemrec);
449 my ($branches) = getbranches();
450 my $branchname=$branches->{$resrec->{'branchcode'}}->{'branchname'};
451 push (@$messages, "<b><font color=red>RESERVED</font></b> for collection by $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'}) at $branchname");
453 UpdateStats($env,$env->{'branchcode'},'return','0','',$iteminformation->{'itemnumber'});
456 return ($iteminformation, $borrower, $messages, $overduecharge);
461 # Original subroutine for Circ2.pm
463 my ($env,$patroninformation,$dbh) = @_;
464 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
467 $flaginfo{'message'}=sprintf "Patron owes \$%.02f", $amount;
469 $flaginfo{'noissues'}=1;
471 $flags{'CHARGES'}=\%flaginfo;
473 if ($patroninformation->{'gonenoaddress'} == 1) {
475 $flaginfo{'message'}='Borrower has no valid address.';
476 $flaginfo{'noissues'}=1;
477 $flags{'GNA'}=\%flaginfo;
479 if ($patroninformation->{'lost'} == 1) {
481 $flaginfo{'message'}='Borrower\'s card reported lost.';
482 $flaginfo{'noissues'}=1;
483 $flags{'LOST'}=\%flaginfo;
485 if ($patroninformation->{'debarred'} == 1) {
487 $flaginfo{'message'}='Borrower is Debarred.';
488 $flaginfo{'noissues'}=1;
489 $flags{'DBARRED'}=\%flaginfo;
491 if ($patroninformation->{'borrowernotes'}) {
493 $flaginfo{'message'}="$patroninformation->{'borrowernotes'}";
494 $flags{'NOTES'}=\%flaginfo;
496 my ($odues, $itemsoverdue) = checkoverdues($env,$patroninformation->{'borrowernumber'},$dbh);
499 $flaginfo{'message'}="Yes";
500 $flaginfo{'itemlist'}=$itemsoverdue;
501 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
502 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
504 $flags{'ODUES'}=\%flaginfo;
506 my ($nowaiting,$itemswaiting) = checkwaiting($env,$dbh,$patroninformation->{'borrowernumber'});
509 $flaginfo{'message'}="Reserved items available";
510 $flaginfo{'itemlist'}=$itemswaiting;
511 $flaginfo{'itemfields'}=['barcode', 'title', 'author', 'dewey', 'subclass', 'holdingbranch'];
512 $flags{'WAITING'}=\%flaginfo;
521 # From Main.pm, modified to return a list of overdueitems, in addition to a count
522 #checks whether a borrower has overdue items
523 my ($env,$bornum,$dbh)=@_;
524 my @datearr = localtime;
525 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
528 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'";
529 my $sth=$dbh->prepare($query);
531 while (my $data = $sth->fetchrow_hashref) {
532 push (@overdueitems, $data);
536 return ($count, \@overdueitems);
540 # Stolen from Returns.pm
541 my ($env,$dbh,$itemnumber)= @_;
542 my $br = $env->{'branchcode'};
543 my $query = "update items
544 set datelastseen = now(), holdingbranch = '$br'
545 where (itemnumber = '$itemnumber')";
546 my $sth = $dbh->prepare($query);
551 sub currentborrower {
552 # Original subroutine for Circ2.pm
553 my ($env, $itemnumber, $dbh) = @_;
554 my $q_itemnumber=$dbh->quote($itemnumber);
555 my $sth=$dbh->prepare("select borrowers.borrowernumber from
556 issues,borrowers where issues.itemnumber=$q_itemnumber and
557 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
560 my ($previousborrower)=$sth->fetchrow;
561 return($previousborrower);
565 # Stolen from Main.pm
566 # Check for reserves for biblio
567 my ($env,$dbh,$itemnum)=@_;
569 my $query = "select * from reserves,items
570 where (items.itemnumber = '$itemnum')
571 and (reserves.cancellationdate is NULL)
572 and (items.biblionumber = reserves.biblionumber)
573 and ((reserves.found = 'W')
574 or (reserves.found is null))
576 my $sth = $dbh->prepare($query);
579 my $data=$sth->fetchrow_hashref;
580 while ($data && $resbor eq '') {
582 my $const = $data->{'constrainttype'};
584 $resbor = $data->{'borrowernumber'};
587 my $cquery = "select * from reserveconstraints,items
588 where (borrowernumber='$data->{'borrowernumber'}')
589 and reservedate='$data->{'reservedate'}'
590 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
591 and (items.itemnumber=$itemnum and
592 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
593 my $csth = $dbh->prepare($cquery);
595 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
597 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
599 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
603 $data=$sth->fetchrow_hashref;
606 return ($resbor,$resrec);
610 # New subroutine for Circ2.pm
611 my ($env, $borrower) = @_;
615 my $borrowernumber=$borrower->{'borrowernumber'};
617 if ($env->{'todaysissues'}) {
618 my @datearr = localtime(time());
619 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
620 $crit=" and issues.timestamp like '$today%' ";
622 if ($env->{'nottodaysissues'}) {
623 my @datearr = localtime(time());
624 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
625 $crit=" and !(issues.timestamp like '$today%') ";
627 my $select="select * from issues,items,biblioitems,biblio where
628 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
629 items.biblionumber=biblio.biblionumber and
630 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
631 $crit order by issues.timestamp desc";
633 my $sth=$dbh->prepare($select);
635 while (my $data = $sth->fetchrow_hashref) {
636 $data->{'dewey'}=~s/0*$//;
637 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
638 my $datedue=$data->{'date_due'};
639 my $itemnumber=$data->{'itemnumber'};
640 $currentissues{$counter}=$data;
645 return(\%currentissues);
650 # check for reserves waiting
651 my ($env,$dbh,$bornum)=@_;
653 my $query = "select * from reserves
654 where (borrowernumber = '$bornum')
655 and (reserves.found='W') and cancellationdate is NULL";
656 my $sth = $dbh->prepare($query);
659 if (my $data=$sth->fetchrow_hashref) {
660 @itemswaiting[$cnt] =$data;
664 return ($cnt,\@itemswaiting);
669 # Stolen from Accounts.pm
670 #take borrower number
671 #check accounts and list amounts owing
672 my ($env,$bornumber,$dbh,$date)=@_;
673 my $select="Select sum(amountoutstanding) from accountlines where
674 borrowernumber=$bornumber and amountoutstanding<>0";
676 $select.=" and date < '$date'";
679 my $sth=$dbh->prepare($select);
682 while (my $data=$sth->fetchrow_hashref){
683 $total=$total+$data->{'sum(amountoutstanding)'};
686 # output(1,2,"borrower owes $total");
688 # # output(1,2,"borrower owes $total");
690 # reconcileaccount($env,$dbh,$bornumber,$total);
698 # Stolen from Renewals.pm
699 # check renewal status
700 my ($env,$dbh,$bornum,$itemno)=@_;
703 my $q1 = "select * from issues
704 where (borrowernumber = '$bornum')
705 and (itemnumber = '$itemno')
706 and returndate is null";
707 my $sth1 = $dbh->prepare($q1);
709 if (my $data1 = $sth1->fetchrow_hashref) {
710 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
711 where (items.itemnumber = '$itemno')
712 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
713 and (biblioitems.itemtype = itemtypes.itemtype)";
714 my $sth2 = $dbh->prepare($q2);
716 if (my $data2=$sth2->fetchrow_hashref) {
717 $renews = $data2->{'renewalsallowed'};
719 if ($renews > $data1->{'renewals'}) {
729 # Stolen from Renewals.pm
730 # mark book as renewed
731 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
732 $datedue=$env->{'datedue'};
733 if ($datedue eq "" ) {
735 my $query= "Select * from biblioitems,items,itemtypes
736 where (items.itemnumber = '$itemno')
737 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
738 and (biblioitems.itemtype = itemtypes.itemtype)";
739 my $sth=$dbh->prepare($query);
741 if (my $data=$sth->fetchrow_hashref) {
742 $loanlength = $data->{'loanlength'}
746 my $datedu = time + ($loanlength * 86400);
747 my @datearr = localtime($datedu);
748 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
750 my @date = split("-",$datedue);
751 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
752 my $issquery = "select * from issues where borrowernumber='$bornum' and
753 itemnumber='$itemno' and returndate is null";
754 my $sth=$dbh->prepare($issquery);
756 my $issuedata=$sth->fetchrow_hashref;
758 my $renews = $issuedata->{'renewals'} +1;
759 my $updquery = "update issues
760 set date_due = '$datedue', renewals = '$renews'
761 where borrowernumber='$bornum' and
762 itemnumber='$itemno' and returndate is null";
763 my $sth=$dbh->prepare($updquery);
771 # Stolen from Issues.pm
772 # calculate charges due
773 my ($env, $dbh, $itemno, $bornum)=@_;
776 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
777 my $sth1= $dbh->prepare($q1);
779 if (my $data1=$sth1->fetchrow_hashref) {
780 $item_type = $data1->{'itemtype'};
781 $charge = $data1->{'rentalcharge'};
782 my $q2 = "select rentaldiscount from borrowers,categoryitem
783 where (borrowers.borrowernumber = '$bornum')
784 and (borrowers.categorycode = categoryitem.categorycode)
785 and (categoryitem.itemtype = '$item_type')";
786 my $sth2=$dbh->prepare($q2);
788 if (my $data2=$sth2->fetchrow_hashref) {
789 my $discount = $data2->{'rentaldiscount'};
790 $charge = ($charge *(100 - $discount)) / 100;
799 #Stolen from Issues.pm
800 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
801 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
802 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
803 my $sth = $dbh->prepare($query);
810 # Stolen from Accounts.pm
811 my ($env,$bornumber,$dbh)=@_;
813 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
814 my $sth = $dbh->prepare($query);
816 if (my $accdata=$sth->fetchrow_hashref){
817 $nextaccntno = $accdata->{'accountno'} + 1;
820 return($nextaccntno);
824 # Stolen from Returns.pm
825 my ($env,$dbh,$itemno) = @_;
826 my ($itemdata) = getiteminformation($env,$itemno,0);
827 my $query = "select * from reserves where
828 ((reserves.found = 'W')
829 or (reserves.found is null))
830 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
831 order by priority,reservedate ";
832 my $sth = $dbh->prepare($query);
837 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
839 if ($resrec->{'found'} eq "W") {
840 if ($resrec->{'itemnumber'} eq $itemno) {
844 if ($resrec->{'constrainttype'} eq "a") {
847 my $conquery = "select * from reserveconstraints where borrowernumber
848 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
849 my $consth = $dbh->prepare($conquery);
851 if (my $conrec=$consth->fetchrow_hashref) {
852 if ($resrec->{'constrainttype'} eq "o") {
856 if ($resrec->{'constrainttype'} eq "e") {
862 if ($resfound eq "y") {
863 my $updquery = "update reserves
864 set found = 'W',itemnumber='$itemno'
865 where borrowernumber = $resrec->{'borrowernumber'}
866 and reservedate = '$resrec->{'reservedate'}'
867 and biblionumber = $resrec->{'biblionumber'}";
868 my $updsth = $dbh->prepare($updquery);
871 my $itbr = $resrec->{'branchcode'};
872 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
873 my $updquery = "update items
874 set holdingbranch = 'TR'
875 where itemnumber = $itemno";
876 my $updsth = $dbh->prepare($updquery);
883 return ($resfound,$lastrec);
886 END { } # module clean-up code here (global destructor)