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 if ($patroninformation->{'debarred'}) {
204 $rejected="Patron is Debarred";
207 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
209 $rejected=sprintf "Patron owes \$%.02f.", $amount;
212 unless ($iteminformation) {
213 $rejected="$barcode is not a valid barcode.";
216 if ($iteminformation->{'notforloan'} == 1) {
217 $rejected="Item not for loan.";
220 if ($iteminformation->{'wthdrawn'} == 1) {
221 $rejected="Item withdrawn.";
224 if ($iteminformation->{'restricted'} == 1) {
225 $rejected="Restricted item.";
228 if ($iteminformation->{'itemtype'} eq 'REF') {
229 $rejected="Reference item: Not for loan.";
232 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
233 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
234 # Already issued to current borrower
235 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
236 if ($renewstatus == 0) {
237 $rejected="No more renewals allowed for this item.";
240 if ($responses->{4} eq '') {
242 $question="Book is issued to this borrower.\nRenew?";
245 } elsif ($responses->{4} eq 'Y') {
246 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
248 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
249 $iteminformation->{'charge'}=$charge;
251 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
252 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
259 } elsif ($currentborrower ne '') {
260 my ($currborrower, $cbflags)=getpatroninformation($env,$currentborrower,0);
261 if ($responses->{1} eq '') {
263 $question="Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
266 } elsif ($responses->{1} eq 'Y') {
267 returnbook($env,$iteminformation->{'barcode'});
274 my ($resbor, $resrec) = checkreserve($env, $dbh, $iteminformation->{'itemnumber'});
276 if ($resbor eq $patroninformation->{'borrowernumber'}) {
277 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
278 my $rsth = $dbh->prepare($rquery);
281 } elsif ($resbor ne "") {
282 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
283 if ($responses->{2} eq '') {
285 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $resrec->{'reservedate'}\nAllow issue?";
288 } elsif ($responses->{2} eq 'N') {
289 #printreserve($env, $resrec, $resborrower, $iteminformation);
293 if ($responses->{3} eq '') {
295 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
298 } elsif ($responses->{3} eq 'Y') {
299 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
300 my $rsth = $dbh->prepare($rquery);
308 unless (($question) || ($rejected) || ($noissue)) {
310 if ($iteminformation->{'loanlength'}) {
311 $loanlength=$iteminformation->{'loanlength'};
314 my $datedue=time+($loanlength)*86400;
315 my @datearr = localtime($datedue);
316 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
317 if ($env->{'datedue'}) {
318 $dateduef=$env->{'datedue'};
320 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
323 $iteminformation->{'issues'}++;
324 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'} where itemnumber=$iteminformation->{'itemnumber'}");
327 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
329 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
330 $iteminformation->{'charge'}=$charge;
332 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
335 if ($iteminformation->{'charge'}) {
336 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
339 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
344 my ($env, $barcode) = @_;
345 my ($messages, $overduecharge);
347 my ($iteminformation) = getiteminformation($env, 0, $barcode);
349 if ($iteminformation) {
350 my $sth=$dbh->prepare("select * from issues where (itemnumber='$iteminformation->{'itemnumber'}') and (returndate is null)");
352 my ($currentborrower) = currentborrower($env, $iteminformation->{'itemnumber'}, $dbh);
353 updatelastseen($env,$dbh,$iteminformation->{'itemnumber'});
354 if ($currentborrower) {
355 ($borrower)=getpatroninformation($env,$currentborrower,0);
356 my @datearr = localtime(time);
357 my $dateret = (1900+$datearr[5])."-".$datearr[4]."-".$datearr[3];
358 my $query = "update issues set returndate = now(), branchcode ='$env->{'branchcode'}' where (borrowernumber = $borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (returndate is null)";
359 my $sth = $dbh->prepare($query);
364 # check for overdue fine
367 $sth=$dbh->prepare("select * from accountlines where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (accounttype='FU' or accounttype='O')");
369 # alter fine to show that the book has been returned
370 if (my $data = $sth->fetchrow_hashref) {
371 my $usth=$dbh->prepare("update accountlines set accounttype='F' where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber=$iteminformation->{'itemnumber'}) and (acccountno='$data->{'accountno'}')");
374 $overduecharge=$data->{'amountoutstanding'};
377 # check for charge made for lost book
378 $sth=$dbh->prepare("select * from accountlines where (borrowernumber=$borrower->{'borrowernumber'}) and (itemnumber = $iteminformation->{'itemnumber'}) and (accounttype='L')");
380 if (my $data = $sth->fetchrow_hashref) {
381 # writeoff this amount
383 my $amount = $data->{'amount'};
384 my $acctno = $data->{'accountno'};
386 if ($data->{'amountoutstanding'} == $amount) {
387 $offset = $data->{'amount'};
390 $offset = $amount - $data->{'amountoutstanding'};
391 $amountleft = $data->{'amountoutstanding'} - $amount;
393 my $uquery = "update accountlines
394 set accounttype = 'LR',amountoutstanding='0'
395 where (borrowernumber = $borrower->{'borrowernumber'})
396 and (itemnumber = $iteminformation->{'itemnumber'})
397 and (accountno = '$acctno') ";
398 my $usth = $dbh->prepare($uquery);
401 my $nextaccntno = C4::Accounts::getnextacctno($env,$borrower->{'borrowernumber'},$dbh);
402 $uquery = "insert into accountlines
403 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
404 values ($borrower->{'borrowernumber'},$nextaccntno,now(),0-$amount,'Book Returned',
406 $usth = $dbh->prepare($uquery);
409 $uquery = "insert into accountoffsets
410 (borrowernumber, accountno, offsetaccount, offsetamount)
411 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
412 $usth = $dbh->prepare($uquery);
418 my ($resfound,$resrec) = find_reserves($env, $dbh, $iteminformation->{'itemnumber'});
419 if ($resfound eq 'y') {
420 my ($borrower) = getpatroninformation($env,$resrec->{'borrowernumber'},0);
421 #printreserve($env,$resrec,$resborrower,$itemrec);
422 my ($branches) = getbranches();
423 my $branchname=$branches->{$resrec->{'branchcode'}}->{'branchname'};
424 push (@$messages, "<b><font color=red>RESERVED</font></b> for collection by $borrower->{'firstname'} $borrower->{'surname'} ($borrower->{'cardnumber'}) at $branchname");
426 UpdateStats($env,$env->{'branchcode'},'return','0','',$iteminformation->{'itemnumber'});
429 return ($iteminformation, $borrower, $messages, $overduecharge);
434 # Original subroutine for Circ2.pm
436 my ($env,$patroninformation,$dbh) = @_;
437 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh);
440 $flaginfo{'message'}=sprintf "Patron owes \$%.02f", $amount;
442 $flaginfo{'noissues'}=1;
444 $flags{'CHARGES'}=\%flaginfo;
446 if ($patroninformation->{'gonenoaddress'} == 1) {
448 $flaginfo{'message'}='Borrower has no valid address.';
449 $flaginfo{'noissues'}=1;
450 $flags{'GNA'}=\%flaginfo;
452 if ($patroninformation->{'lost'} == 1) {
454 $flaginfo{'message'}='Borrower\'s card reported lost.';
455 $flaginfo{'noissues'}=1;
456 $flags{'LOST'}=\%flaginfo;
458 if ($patroninformation->{'debarred'} == 1) {
460 $flaginfo{'message'}='Borrower is Debarred.';
461 $flaginfo{'noissues'}=1;
462 $flags{'DBARRED'}=\%flaginfo;
464 if ($patroninformation->{'borrowernotes'}) {
466 $flaginfo{'message'}="$patroninformation->{'borrowernotes'}";
467 $flags{'NOTES'}=\%flaginfo;
469 my ($odues, $itemsoverdue) = checkoverdues($env,$patroninformation->{'borrowernumber'},$dbh);
472 $flaginfo{'message'}="Yes";
473 $flaginfo{'itemlist'}=$itemsoverdue;
474 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
475 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
477 $flags{'ODUES'}=\%flaginfo;
479 my ($nowaiting,$itemswaiting) = checkwaiting($env,$dbh,$patroninformation->{'borrowernumber'});
482 $flaginfo{'message'}="Reserved items available";
483 $flaginfo{'itemlist'}=$itemswaiting;
484 $flaginfo{'itemfields'}=['barcode', 'title', 'author', 'dewey', 'subclass', 'holdingbranch'];
485 $flags{'WAITING'}=\%flaginfo;
494 # From Main.pm, modified to return a list of overdueitems, in addition to a count
495 #checks whether a borrower has overdue items
496 my ($env,$bornum,$dbh)=@_;
497 my @datearr = localtime;
498 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
501 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'";
502 my $sth=$dbh->prepare($query);
504 while (my $data = $sth->fetchrow_hashref) {
505 push (@overdueitems, $data);
509 return ($count, \@overdueitems);
513 # Stolen from Returns.pm
514 my ($env,$dbh,$itemnumber)= @_;
515 my $br = $env->{'branchcode'};
516 my $query = "update items
517 set datelastseen = now(), holdingbranch = '$br'
518 where (itemnumber = '$itemnumber')";
519 my $sth = $dbh->prepare($query);
524 sub currentborrower {
525 # Original subroutine for Circ2.pm
526 my ($env, $itemnumber, $dbh) = @_;
527 my $q_itemnumber=$dbh->quote($itemnumber);
528 my $sth=$dbh->prepare("select borrowers.borrowernumber from
529 issues,borrowers where issues.itemnumber=$q_itemnumber and
530 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
533 my ($previousborrower)=$sth->fetchrow;
534 return($previousborrower);
538 # Stolen from Main.pm
539 # Check for reserves for biblio
540 my ($env,$dbh,$itemnum)=@_;
542 my $query = "select * from reserves,items
543 where (items.itemnumber = '$itemnum')
544 and (reserves.cancellationdate is NULL)
545 and (items.biblionumber = reserves.biblionumber)
546 and ((reserves.found = 'W')
547 or (reserves.found is null))
549 my $sth = $dbh->prepare($query);
552 my $data=$sth->fetchrow_hashref;
553 while ($data && $resbor eq '') {
555 my $const = $data->{'constrainttype'};
557 $resbor = $data->{'borrowernumber'};
560 my $cquery = "select * from reserveconstraints,items
561 where (borrowernumber='$data->{'borrowernumber'}')
562 and reservedate='$data->{'reservedate'}'
563 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
564 and (items.itemnumber=$itemnum and
565 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
566 my $csth = $dbh->prepare($cquery);
568 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
570 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
572 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
576 $data=$sth->fetchrow_hashref;
579 return ($resbor,$resrec);
583 # New subroutine for Circ2.pm
584 my ($env, $borrower) = @_;
588 my $borrowernumber=$borrower->{'borrowernumber'};
590 if ($env->{'todaysissues'}) {
591 my @datearr = localtime(time());
592 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
593 $crit=" and issues.timestamp like '$today%' ";
595 if ($env->{'nottodaysissues'}) {
596 my @datearr = localtime(time());
597 my $today = (1900+$datearr[5]).sprintf "0%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
598 $crit=" and !(issues.timestamp like '$today%') ";
600 my $select="select * from issues,items,biblioitems,biblio where
601 borrowernumber=$borrowernumber and issues.itemnumber=items.itemnumber and
602 items.biblionumber=biblio.biblionumber and
603 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
604 $crit order by issues.timestamp desc";
606 my $sth=$dbh->prepare($select);
608 while (my $data = $sth->fetchrow_hashref) {
609 $data->{'dewey'}=~s/0*$//;
610 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
611 my $datedue=$data->{'date_due'};
612 my $itemnumber=$data->{'itemnumber'};
613 $currentissues{$counter}=$data;
618 return(\%currentissues);
623 # check for reserves waiting
624 my ($env,$dbh,$bornum)=@_;
626 my $query = "select * from reserves
627 where (borrowernumber = '$bornum')
628 and (reserves.found='W') and cancellationdate is NULL";
629 my $sth = $dbh->prepare($query);
632 if (my $data=$sth->fetchrow_hashref) {
633 @itemswaiting[$cnt] =$data;
637 return ($cnt,\@itemswaiting);
642 # Stolen from Accounts.pm
643 #take borrower number
644 #check accounts and list amounts owing
645 my ($env,$bornumber,$dbh)=@_;
646 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
647 borrowernumber=$bornumber and amountoutstanding<>0");
650 while (my $data=$sth->fetchrow_hashref){
651 $total=$total+$data->{'sum(amountoutstanding)'};
654 # output(1,2,"borrower owes $total");
656 # # output(1,2,"borrower owes $total");
658 # reconcileaccount($env,$dbh,$bornumber,$total);
666 # Stolen from Renewals.pm
667 # check renewal status
668 my ($env,$dbh,$bornum,$itemno)=@_;
671 my $q1 = "select * from issues
672 where (borrowernumber = '$bornum')
673 and (itemnumber = '$itemno')
674 and returndate is null";
675 my $sth1 = $dbh->prepare($q1);
677 if (my $data1 = $sth1->fetchrow_hashref) {
678 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
679 where (items.itemnumber = '$itemno')
680 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
681 and (biblioitems.itemtype = itemtypes.itemtype)";
682 my $sth2 = $dbh->prepare($q2);
684 if (my $data2=$sth2->fetchrow_hashref) {
685 $renews = $data2->{'renewalsallowed'};
687 if ($renews > $data1->{'renewals'}) {
697 # Stolen from Renewals.pm
698 # mark book as renewed
699 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
700 $datedue=$env->{'datedue'};
701 if ($datedue eq "" ) {
703 my $query= "Select * from biblioitems,items,itemtypes
704 where (items.itemnumber = '$itemno')
705 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
706 and (biblioitems.itemtype = itemtypes.itemtype)";
707 my $sth=$dbh->prepare($query);
709 if (my $data=$sth->fetchrow_hashref) {
710 $loanlength = $data->{'loanlength'}
714 my $datedu = time + ($loanlength * 86400);
715 my @datearr = localtime($datedu);
716 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
718 my @date = split("-",$datedue);
719 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
720 my $issquery = "select * from issues where borrowernumber='$bornum' and
721 itemnumber='$itemno' and returndate is null";
722 my $sth=$dbh->prepare($issquery);
724 my $issuedata=$sth->fetchrow_hashref;
726 my $renews = $issuedata->{'renewals'} +1;
727 my $updquery = "update issues
728 set date_due = '$datedue', renewals = '$renews'
729 where borrowernumber='$bornum' and
730 itemnumber='$itemno' and returndate is null";
731 my $sth=$dbh->prepare($updquery);
739 # Stolen from Issues.pm
740 # calculate charges due
741 my ($env, $dbh, $itemno, $bornum)=@_;
744 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
745 my $sth1= $dbh->prepare($q1);
747 if (my $data1=$sth1->fetchrow_hashref) {
748 $item_type = $data1->{'itemtype'};
749 $charge = $data1->{'rentalcharge'};
750 my $q2 = "select rentaldiscount from borrowers,categoryitem
751 where (borrowers.borrowernumber = '$bornum')
752 and (borrowers.categorycode = categoryitem.categorycode)
753 and (categoryitem.itemtype = '$item_type')";
754 my $sth2=$dbh->prepare($q2);
756 if (my $data2=$sth2->fetchrow_hashref) {
757 my $discount = $data2->{'rentaldiscount'};
758 $charge = ($charge *(100 - $discount)) / 100;
767 #Stolen from Issues.pm
768 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
769 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
770 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
771 my $sth = $dbh->prepare($query);
778 # Stolen from Accounts.pm
779 my ($env,$bornumber,$dbh)=@_;
781 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
782 my $sth = $dbh->prepare($query);
784 if (my $accdata=$sth->fetchrow_hashref){
785 $nextaccntno = $accdata->{'accountno'} + 1;
788 return($nextaccntno);
792 # Stolen from Returns.pm
793 my ($env,$dbh,$itemno) = @_;
794 my ($itemdata) = getiteminformation($env,$itemno,0);
795 my $query = "select * from reserves where
796 ((reserves.found = 'W')
797 or (reserves.found is null))
798 and biblionumber = $itemdata->{'biblionumber'} and cancellationdate is NULL
799 order by priority,reservedate ";
800 my $sth = $dbh->prepare($query);
805 while (($resrec=$sth->fetchrow_hashref) && ($resfound eq "n")) {
807 if ($resrec->{'found'} eq "W") {
808 if ($resrec->{'itemnumber'} eq $itemno) {
812 if ($resrec->{'constrainttype'} eq "a") {
815 my $conquery = "select * from reserveconstraints where borrowernumber
816 = $resrec->{'borrowernumber'} and reservedate = '$resrec->{'reservedate'}' and biblionumber = $resrec->{'biblionumber'} and biblioitemnumber = $itemdata->{'biblioitemnumber'}";
817 my $consth = $dbh->prepare($conquery);
819 if (my $conrec=$consth->fetchrow_hashref) {
820 if ($resrec->{'constrainttype'} eq "o") {
824 if ($resrec->{'constrainttype'} eq "e") {
830 if ($resfound eq "y") {
831 my $updquery = "update reserves
832 set found = 'W',itemnumber='$itemno'
833 where borrowernumber = $resrec->{'borrowernumber'}
834 and reservedate = '$resrec->{'reservedate'}'
835 and biblionumber = $resrec->{'biblionumber'}";
836 my $updsth = $dbh->prepare($updquery);
839 my $itbr = $resrec->{'branchcode'};
840 if ($resrec->{'branchcode'} ne $env->{'branchcode'}) {
841 my $updquery = "update items
842 set holdingbranch = 'TR'
843 where itemnumber = $itemno";
844 my $updsth = $dbh->prepare($updquery);
851 return ($resfound,$lastrec);
854 END { } # module clean-up code here (global destructor)