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 @datearr = localtime(time());
639 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
640 +1)).sprintf ("%0.2d", $datearr[3]);
641 my $datedue=$data->{'date_due'};
643 if ($datedue < $todaysdate) {
644 $data->{'overdue'}=1;
646 my $itemnumber=$data->{'itemnumber'};
647 $currentissues{$counter}=$data;
652 return(\%currentissues);
657 # check for reserves waiting
658 my ($env,$dbh,$bornum)=@_;
660 my $query = "select * from reserves
661 where (borrowernumber = '$bornum')
662 and (reserves.found='W') and cancellationdate is NULL";
663 my $sth = $dbh->prepare($query);
666 if (my $data=$sth->fetchrow_hashref) {
667 @itemswaiting[$cnt] =$data;
671 return ($cnt,\@itemswaiting);
676 # Stolen from Accounts.pm
677 #take borrower number
678 #check accounts and list amounts owing
679 my ($env,$bornumber,$dbh,$date)=@_;
680 my $select="Select sum(amountoutstanding) from accountlines where
681 borrowernumber=$bornumber and amountoutstanding<>0";
683 $select.=" and date < '$date'";
686 my $sth=$dbh->prepare($select);
689 while (my $data=$sth->fetchrow_hashref){
690 $total=$total+$data->{'sum(amountoutstanding)'};
693 # output(1,2,"borrower owes $total");
695 # # output(1,2,"borrower owes $total");
697 # reconcileaccount($env,$dbh,$bornumber,$total);
705 # Stolen from Renewals.pm
706 # check renewal status
707 my ($env,$dbh,$bornum,$itemno)=@_;
710 my $q1 = "select * from issues
711 where (borrowernumber = '$bornum')
712 and (itemnumber = '$itemno')
713 and returndate is null";
714 my $sth1 = $dbh->prepare($q1);
716 if (my $data1 = $sth1->fetchrow_hashref) {
717 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
718 where (items.itemnumber = '$itemno')
719 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
720 and (biblioitems.itemtype = itemtypes.itemtype)";
721 my $sth2 = $dbh->prepare($q2);
723 if (my $data2=$sth2->fetchrow_hashref) {
724 $renews = $data2->{'renewalsallowed'};
726 if ($renews > $data1->{'renewals'}) {
736 # Stolen from Renewals.pm
737 # mark book as renewed
738 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
739 $datedue=$env->{'datedue'};
740 if ($datedue eq "" ) {
742 my $query= "Select * from biblioitems,items,itemtypes
743 where (items.itemnumber = '$itemno')
744 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
745 and (biblioitems.itemtype = itemtypes.itemtype)";
746 my $sth=$dbh->prepare($query);
748 if (my $data=$sth->fetchrow_hashref) {
749 $loanlength = $data->{'loanlength'}
753 my $datedu = time + ($loanlength * 86400);
754 my @datearr = localtime($datedu);
755 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
757 my @date = split("-",$datedue);
758 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
759 my $issquery = "select * from issues where borrowernumber='$bornum' and
760 itemnumber='$itemno' and returndate is null";
761 my $sth=$dbh->prepare($issquery);
763 my $issuedata=$sth->fetchrow_hashref;
765 my $renews = $issuedata->{'renewals'} +1;
766 my $updquery = "update issues
767 set date_due = '$datedue', renewals = '$renews'
768 where borrowernumber='$bornum' and
769 itemnumber='$itemno' and returndate is null";
770 my $sth=$dbh->prepare($updquery);
778 # Stolen from Issues.pm
779 # calculate charges due
780 my ($env, $dbh, $itemno, $bornum)=@_;
783 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
784 my $sth1= $dbh->prepare($q1);
786 if (my $data1=$sth1->fetchrow_hashref) {
787 $item_type = $data1->{'itemtype'};
788 $charge = $data1->{'rentalcharge'};
789 my $q2 = "select rentaldiscount from borrowers,categoryitem
790 where (borrowers.borrowernumber = '$bornum')
791 and (borrowers.categorycode = categoryitem.categorycode)
792 and (categoryitem.itemtype = '$item_type')";
793 my $sth2=$dbh->prepare($q2);
795 if (my $data2=$sth2->fetchrow_hashref) {
796 my $discount = $data2->{'rentaldiscount'};
797 $charge = ($charge *(100 - $discount)) / 100;
806 #Stolen from Issues.pm
807 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
808 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
809 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
810 my $sth = $dbh->prepare($query);
817 # Stolen from Accounts.pm
818 my ($env,$bornumber,$dbh)=@_;
820 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
821 my $sth = $dbh->prepare($query);
823 if (my $accdata=$sth->fetchrow_hashref){
824 $nextaccntno = $accdata->{'accountno'} + 1;
827 return($nextaccntno);
831 # Stolen from Returns.pm
832 my ($env,$dbh,$itemno) = @_;
833 my ($itemdata) = getiteminformation($env,$itemno,0);
834 my $query = "select * from reserves where
835 ((reserves.found = 'W')
836 or (reserves.found is null))
837 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
838 order by priority,reservedate ";
839 my $sth = $dbh->prepare($query);
844 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
846 if ($resrec->{'found'} eq "W") {
847 if ($resrec->{'itemnumber'} eq $itemno) {
851 if ($resrec->{'constrainttype'} eq "a") {
854 my $conquery = "select * from reserveconstraints where borrowernumber
855 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
856 my $consth = $dbh->prepare($conquery);
858 if (my $conrec=$consth->fetchrow_hashref) {
859 if ($resrec->{'constrainttype'} eq "o") {
863 if ($resrec->{'constrainttype'} eq "e") {
869 if ($resfound eq "y") {
870 my $updquery = "update reserves
871 set found = 'W',itemnumber='$itemno'
872 where borrowernumber = $resrec->{'borrowernumber'}
873 and reservedate = '$resrec->{'reservedate'}'
874 and biblionumber = $resrec->{'biblionumber'}";
875 my $updsth = $dbh->prepare($updquery);
878 my $itbr = $resrec->{'branchcode'};
879 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
880 my $updquery = "update items
881 set holdingbranch = 'TR'
882 where itemnumber = $itemno";
883 my $updsth = $dbh->prepare($updquery);
890 return ($resfound,$lastrec);
893 END { } # module clean-up code here (global destructor)