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) = @_;
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 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
205 $rejected=sprintf "Patron owes \$%.02f.", $amount;
208 unless ($iteminformation) {
209 $rejected="$barcode is not a valid barcode.";
212 if ($iteminformation->{'notforloan'} == 1) {
213 $rejected="Item not for loan.";
216 if ($iteminformation->{'wthdrawn'} == 1) {
217 $rejected="Item withdrawn.";
220 if ($iteminformation->{'restricted'} == 1) {
221 $rejected="Restricted item.";
224 if ($iteminformation->{'itemtype'} eq 'REF') {
225 $rejected="Reference item: Not for loan.";
228 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
229 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
230 # Already issued to current borrower
231 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
232 if ($renewstatus == 0) {
233 $rejected="No more renewals allowed for this item.";
236 if ($responses->{4} eq '') {
238 $question="Book is issued to this borrower.\nRenew?";
241 } elsif ($responses->{4} eq 'Y') {
242 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
244 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
245 $iteminformation->{'charge'}=$charge;
247 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
248 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
255 } elsif ($currentborrower ne '') {
256 my ($currborrower, $cbflags)=getpatroninformation($env,$currentborrower,0);
257 if ($responses->{1} eq '') {
259 $question="Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
262 } elsif ($responses->{1} eq 'Y') {
263 returnbook($env,$iteminformation->{'barcode'});
270 my ($resbor, $resrec) = checkreserve($env, $dbh, $iteminformation->{'itemnumber'});
272 if ($resbor eq $patroninformation->{'borrowernumber'}) {
273 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
274 my $rsth = $dbh->prepare($rquery);
277 } elsif ($resbor ne "") {
278 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
279 if ($responses->{2} eq '') {
281 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $resrec->{'reservedate'}\nAllow issue?";
284 } elsif ($responses->{2} eq 'N') {
285 #printreserve($env, $resrec, $resborrower, $iteminformation);
289 if ($responses->{3} eq '') {
291 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
294 } elsif ($responses->{3} eq 'Y') {
295 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
296 my $rsth = $dbh->prepare($rquery);
304 unless (($question) || ($rejected) || ($noissue)) {
306 if ($iteminformation->{'loanlength'}) {
307 $loanlength=$iteminformation->{'loanlength'};
310 my $datedue=time+($loanlength)*86400;
311 my @datearr = localtime($datedue);
312 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
313 if ($env->{'datedue'}) {
314 $dateduef=$env->{'datedue'};
316 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
319 $iteminformation->{'issues'}++;
320 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'} where itemnumber=$iteminformation->{'itemnumber'}");
323 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
325 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
326 $iteminformation->{'charge'}=$charge;
328 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
331 if ($iteminformation->{'charge'}) {
332 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
335 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
340 my ($env, $barcode) = @_;
341 my ($messages, $overduecharge);
343 my ($iteminformation) = getiteminformation($env, 0, $barcode);
345 if ($iteminformation) {
346 my $sth=$dbh->prepare("select * from issues where (itemnumber='$iteminformation->{'itemnumber'}') and (returndate is null)");
348 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
349 updatelastseen($env,$dbh,$iteminformation->{'itemnumber'});
350 if ($currentborrower) {
351 ($borrower)=getpatroninformation($env,$currentborrower,0);
352 my @datearr = localtime(time);
353 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
354 my $query = "update issues set returndate = now(), branchcode ='$env->{'branchcode'}' where (borrowernumber = $borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (returndate is null)";
355 my $sth = $dbh->prepare($query);
360 # check for overdue fine
363 $sth=$dbh->prepare("select * from accountlines where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (accounttype='FU' or accounttype='O')");
365 # alter fine to show that the book has been returned
366 if (my $data = $sth->fetchrow_hashref) {
367 my $usth=$dbh->prepare("update accountlines set accounttype='F' where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber=$iteminformation->{'itemnumber'}) and (acccountno='$data->{'accountno'}')");
370 $overduecharge=$data->{'amountoutstanding'};
373 # check for charge made for lost book
374 $sth=$dbh->prepare("select * from accountlines where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (accounttype='L')");
376 if (my $data = $sth->fetchrow_hashref) {
377 # writeoff this amount
379 my $amount = $data->{'amount'};
380 my $acctno = $data->{'accountno'};
382 if ($data->{'amountoutstanding'} == $amount) {
383 $offset = $data->{'amount'};
386 $offset = $amount - $data->{'amountoutstanding'};
387 $amountleft = $data->{'amountoutstanding'} - $amount;
389 my $uquery = "update accountlines
390 set accounttype = 'LR',amountoutstanding='0'
391 where (borrowernumber = $borrower->{'borrowernumber'})
392 and (itemnumber = $iteminformation->{'itemnumber'})
393 and (accountno = '$acctno') ";
394 my $usth = $dbh->prepare($uquery);
397 my $nextaccntno = C4::Accounts::getnextacctno($env,$borrower->{'borrowernumber'},$dbh);
398 $uquery = "insert into accountlines
399 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
400 values ($borrower->{'borrowernumber'},$nextaccntno,now(),0-$amount,'Book Returned',
402 $usth = $dbh->prepare($uquery);
405 $uquery = "insert into accountoffsets
406 (borrowernumber, accountno, offsetaccount, offsetamount)
407 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
408 $usth = $dbh->prepare($uquery);
414 my ($resfound,$resrec) = find_reserves($env, $dbh, $iteminformation->{'itemnumber'});
415 if ($resfound eq 'y') {
416 my ($borrower) = getpatroninformation($env,$resrec->{'borrowernumber'},0);
417 #printreserve($env,$resrec,$resborrower,$itemrec);
418 my ($branches) = getbranches();
419 my $branchname=$branches->{$resrec->{'branchcode'}}->{'branchname'};
420 push (@$messages, "Reserved for collection by $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'}) at $branchname");
422 UpdateStats($env,'branch','return','0','',$iteminformation->{'itemnumber'});
425 return ($iteminformation, $borrower, $messages, $overduecharge);
430 # Original subroutine for Circ2.pm
432 my ($env,$patroninformation,$dbh) = @_;
433 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
436 $flaginfo{'message'}=sprintf "Patron owes \$%.02f", $amount;
438 $flaginfo{'noissues'}=1;
440 $flags{'CHARGES'}=\%flaginfo;
442 if ($patroninformation->{'gonenoaddress'} == 1) {
444 $flaginfo{'message'}='Borrower has no valid address.';
445 $flaginfo{'noissues'}=1;
446 $flags{'GNA'}=\%flaginfo;
448 if ($patroninformation->{'lost'} == 1) {
450 $flaginfo{'message'}='Borrower\'s card reported lost.';
451 $flaginfo{'noissues'}=1;
452 $flags{'LOST'}=\%flaginfo;
454 if ($patroninformation->{'borrowernotes'}) {
456 $flaginfo{'message'}="$patroninformation->{'borrowernotes'}";
457 $flags{'NOTES'}=\%flaginfo;
459 my ($odues, $itemsoverdue) = checkoverdues($env,$patroninformation->{'borrowernumber'},$dbh);
462 $flaginfo{'message'}="Patron has overdue items";
463 $flaginfo{'itemlist'}=$itemsoverdue;
464 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
465 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
467 $flags{'ODUES'}=\%flaginfo;
469 my ($nowaiting,$itemswaiting) = checkwaiting($env,$dbh,$patroninformation->{'borrowernumber'});
472 $flaginfo{'message'}="Reserved items available";
473 $flaginfo{'itemlist'}=$itemswaiting;
474 $flaginfo{'itemfields'}=['barcode', 'title', 'author', 'dewey', 'subclass', 'holdingbranch'];
475 $flags{'WAITING'}=\%flaginfo;
484 # From Main.pm, modified to return a list of overdueitems, in addition to a count
485 #checks whether a borrower has overdue items
486 my ($env,$bornum,$dbh)=@_;
487 my @datearr = localtime;
488 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
491 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'";
492 my $sth=$dbh->prepare($query);
494 while (my $data = $sth->fetchrow_hashref) {
495 push (@overdueitems, $data);
499 return ($count, \@overdueitems);
503 # Stolen from Returns.pm
504 my ($env,$dbh,$itemnumber)= @_;
505 my $br = $env->{'branchcode'};
506 my $query = "update items
507 set datelastseen = now(), holdingbranch = '$br'
508 where (itemnumber = '$itemnumber')";
509 my $sth = $dbh->prepare($query);
514 sub currentborrower {
515 # Original subroutine for Circ2.pm
516 my ($env, $itemnumber, $dbh) = @_;
517 my $q_itemnumber=$dbh->quote($itemnumber);
518 my $sth=$dbh->prepare("select borrowers.borrowernumber from
519 issues,borrowers where issues.itemnumber=$q_itemnumber and
520 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
523 my ($previousborrower)=$sth->fetchrow;
524 return($previousborrower);
528 # Stolen from Main.pm
529 # Check for reserves for biblio
530 my ($env,$dbh,$itemnum)=@_;
532 my $query = "select * from reserves,items
533 where (items.itemnumber = '$itemnum')
534 and (reserves.cancellationdate is NULL)
535 and (items.biblionumber = reserves.biblionumber)
536 and ((reserves.found = 'W')
537 or (reserves.found is null))
539 my $sth = $dbh->prepare($query);
542 if (my $data=$sth->fetchrow_hashref) {
544 my $const = $data->{'constrainttype'};
546 $resbor = $data->{'borrowernumber'};
549 my $cquery = "select * from reserveconstraints,items
550 where (borrowernumber='$data->{'borrowernumber'}')
551 and reservedate='$data->{'reservedate'}'
552 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
553 and (items.itemnumber=$itemnum and
554 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
555 my $csth = $dbh->prepare($cquery);
557 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
559 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
561 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
567 return ($resbor,$resrec);
571 # New subroutine for Circ2.pm
572 my ($env, $borrower) = @_;
576 my $borrowernumber=$borrower->{'borrowernumber'};
578 if ($env->{'todaysissues'}) {
579 my @datearr = localtime(time());
580 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
581 $crit=" and issues.timestamp like '$today%' ";
583 if ($env->{'nottodaysissues'}) {
584 my @datearr = localtime(time());
585 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
586 $crit=" and !(issues.timestamp like '$today%') ";
588 my $select="select * from issues,items,biblioitems,biblio where borrowernumber=$borrowernumber and issues.itemnumber=items.itemnumber and items.biblionumber=biblio.biblionumber and items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null $crit order by date_due";
590 my $sth=$dbh->prepare($select);
592 while (my $data = $sth->fetchrow_hashref) {
593 $data->{'dewey'}=~s/0*$//;
594 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
595 my $datedue=$data->{'date_due'};
596 my $itemnumber=$data->{'itemnumber'};
597 $currentissues{$counter}=$data;
602 return(\%currentissues);
607 # check for reserves waiting
608 my ($env,$dbh,$bornum)=@_;
610 my $query = "select * from reserves
611 where (borrowernumber = '$bornum')
612 and (reserves.found='W') and cancellationdate is NULL";
613 my $sth = $dbh->prepare($query);
616 if (my $data=$sth->fetchrow_hashref) {
617 @itemswaiting[$cnt] =$data;
621 return ($cnt,\@itemswaiting);
626 # Stolen from Accounts.pm
627 #take borrower number
628 #check accounts and list amounts owing
629 my ($env,$bornumber,$dbh)=@_;
630 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
631 borrowernumber=$bornumber and amountoutstanding<>0");
634 while (my $data=$sth->fetchrow_hashref){
635 $total=$total+$data->{'sum(amountoutstanding)'};
638 # output(1,2,"borrower owes $total");
640 # # output(1,2,"borrower owes $total");
642 # reconcileaccount($env,$dbh,$bornumber,$total);
650 # Stolen from Renewals.pm
651 # check renewal status
652 my ($env,$dbh,$bornum,$itemno)=@_;
655 my $q1 = "select * from issues
656 where (borrowernumber = '$bornum')
657 and (itemnumber = '$itemno')
658 and returndate is null";
659 my $sth1 = $dbh->prepare($q1);
661 if (my $data1 = $sth1->fetchrow_hashref) {
662 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
663 where (items.itemnumber = '$itemno')
664 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
665 and (biblioitems.itemtype = itemtypes.itemtype)";
666 my $sth2 = $dbh->prepare($q2);
668 if (my $data2=$sth2->fetchrow_hashref) {
669 $renews = $data2->{'renewalsallowed'};
671 if ($renews > $data1->{'renewals'}) {
681 # Stolen from Renewals.pm
682 # mark book as renewed
683 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
684 $datedue=$env->{'datedue'};
685 if ($datedue eq "" ) {
687 my $query= "Select * from biblioitems,items,itemtypes
688 where (items.itemnumber = '$itemno')
689 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
690 and (biblioitems.itemtype = itemtypes.itemtype)";
691 my $sth=$dbh->prepare($query);
693 if (my $data=$sth->fetchrow_hashref) {
694 $loanlength = $data->{'loanlength'}
698 my $datedu = time + ($loanlength * 86400);
699 my @datearr = localtime($datedu);
700 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
702 my @date = split("-",$datedue);
703 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
704 my $issquery = "select * from issues where borrowernumber='$bornum' and
705 itemnumber='$itemno' and returndate is null";
706 my $sth=$dbh->prepare($issquery);
708 my $issuedata=$sth->fetchrow_hashref;
710 my $renews = $issuedata->{'renewals'} +1;
711 my $updquery = "update issues
712 set date_due = '$datedue', renewals = '$renews'
713 where borrowernumber='$bornum' and
714 itemnumber='$itemno' and returndate is null";
715 my $sth=$dbh->prepare($updquery);
723 # Stolen from Issues.pm
724 # calculate charges due
725 my ($env, $dbh, $itemno, $bornum)=@_;
728 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
729 my $sth1= $dbh->prepare($q1);
731 if (my $data1=$sth1->fetchrow_hashref) {
732 $item_type = $data1->{'itemtype'};
733 $charge = $data1->{'rentalcharge'};
734 my $q2 = "select rentaldiscount from borrowers,categoryitem
735 where (borrowers.borrowernumber = '$bornum')
736 and (borrowers.categorycode = categoryitem.categorycode)
737 and (categoryitem.itemtype = '$item_type')";
738 my $sth2=$dbh->prepare($q2);
740 if (my $data2=$sth2->fetchrow_hashref) {
741 my $discount = $data2->{'rentaldiscount'};
742 $charge = ($charge *(100 - $discount)) / 100;
751 #Stolen from Issues.pm
752 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
753 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
754 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
755 my $sth = $dbh->prepare($query);
762 # Stolen from Accounts.pm
763 my ($env,$bornumber,$dbh)=@_;
765 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
766 my $sth = $dbh->prepare($query);
768 if (my $accdata=$sth->fetchrow_hashref){
769 $nextaccntno = $accdata->{'accountno'} + 1;
772 return($nextaccntno);
776 # Stolen from Returns.pm
777 my ($env,$dbh,$itemno) = @_;
778 my ($itemdata) = getiteminformation($env,$itemno,0);
779 my $query = "select * from reserves where found is null
780 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
781 order by priority,reservedate ";
782 my $sth = $dbh->prepare($query);
787 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
789 if ($resrec->{'found'} eq "W") {
790 if ($resrec->{'itemnumber'} eq $itemno) {
793 } elsif ($resrec->{'constrainttype'} eq "a") {
796 my $conquery = "select * from reserveconstraints where borrowernumber
797 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
798 my $consth = $dbh->prepare($conquery);
800 if (my $conrec=$consth->fetchrow_hashref) {
801 if ($resrec->{'constrainttype'} eq "o") {
805 if ($resrec->{'constrainttype'} eq "e") {
811 if ($resfound eq "y") {
812 my $updquery = "update reserves
813 set found = 'W',itemnumber='$itemno'
814 where borrowernumber = $resrec->{'borrowernumber'}
815 and reservedate = '$resrec->{'reservedate'}'
816 and biblionumber = $resrec->{'biblionumber'}";
817 my $updsth = $dbh->prepare($updquery);
820 my $itbr = $resrec->{'branchcode'};
821 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
822 my $updquery = "update items
823 set holdingbranch = 'TR'
824 where itemnumber = $itemno";
825 my $updsth = $dbh->prepare($updquery);
832 return ($resfound,$lastrec);
835 END { } # module clean-up code here (global destructor)