1 package C4::Circulation::Circ2;
3 #package to deal with Returns
4 #written 3/11/99 by olwen@katipo.co.nz
12 #use C4::InterfaceCDK;
13 #use C4::Circulation::Main;
15 #use C4::Circulation::Renewals;
22 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24 # set the version for version checking
28 @EXPORT = qw(&getbranches &getprinters &getpatroninformation ¤tissues &getissues &getiteminformation &findborrower &issuebook &returnbook &find_reserves &transferbook &decode);
29 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
31 # your exported package globals go here,
32 # as well as any optionally exported functions
34 @EXPORT_OK = qw($Var1 %Hashit);
37 # non-exported package globals go here
38 #use vars qw(@more $stuff);
40 # initalize package globals, first exported ones
45 # then the others (which are still accessible as $Some::Module::stuff)
49 # all file-scoped lexicals must be created before
50 # the functions below that use them.
52 # file-private lexicals go here
56 # here's a file-private function as a closure,
57 # callable as &$priv_func; it cannot be prototyped.
62 # make all your functions, whether exported or not;
66 # returns a reference to a hash of references to branches...
69 my $sth=$dbh->prepare("select * from branches");
71 while (my $branch=$sth->fetchrow_hashref) {
72 my $tmp = $branch->{'branchcode'}; my $brc = $dbh->quote($tmp);
73 my $query = "select categorycode from branchrelations where branchcode = $brc";
74 my $nsth = $dbh->prepare($query);
76 while (my ($cat) = $nsth->fetchrow_array) {
80 $branches{$branch->{'branchcode'}}=$branch;
91 my $sth=$dbh->prepare("select * from printers");
93 while (my $printer=$sth->fetchrow_hashref) {
94 $printers{$printer->{'printqueue'}}=$printer;
102 sub getpatroninformation {
104 my ($env, $borrowernumber,$cardnumber) = @_;
108 if ($borrowernumber) {
109 $query = "select * from borrowers where borrowernumber=$borrowernumber";
110 } elsif ($cardnumber) {
111 $query = "select * from borrowers where cardnumber=$cardnumber";
113 $env->{'apierror'} = "invalid borrower information passed to getpatroninformation subroutine";
116 $env->{'mess'} = $query;
117 $sth = $dbh->prepare($query);
119 my $borrower = $sth->fetchrow_hashref;
120 my $flags = patronflags($env, $borrower, $dbh);
123 $borrower->{'flags'}=$flags;
124 return($borrower, $flags);
129 my $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
130 my @s = map { index($seq,$_); } split(//,$encoded);
145 my $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
146 $r .=chr(($n >> 16) ^ 67) .
147 chr(($n >> 8 & 255) ^ 67) .
148 chr(($n & 255) ^ 67);
151 $r = substr($r,0,length($r)-$l);
158 sub getiteminformation {
159 # returns a hash of item information given either the itemnumber or the barcode
160 my ($env, $itemnumber, $barcode) = @_;
164 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.itemnumber=$itemnumber and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
166 my $q_barcode=$dbh->quote($barcode);
167 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.barcode=$q_barcode and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
169 $env->{'apierror'}="getiteminformation() subroutine must be called with either an itemnumber or a barcode";
174 my $iteminformation=$sth->fetchrow_hashref;
176 if ($iteminformation) {
177 $sth=$dbh->prepare("select date_due from issues where itemnumber=$iteminformation->{'itemnumber'} and isnull(returndate)");
179 my ($date_due) = $sth->fetchrow;
180 $iteminformation->{'date_due'}=$date_due;
182 #$iteminformation->{'dewey'}=~s/0*$//;
183 ($iteminformation->{'dewey'} == 0) && ($iteminformation->{'dewey'}='');
184 $sth=$dbh->prepare("select * from itemtypes where itemtype='$iteminformation->{'itemtype'}'");
186 my $itemtype=$sth->fetchrow_hashref;
187 $iteminformation->{'loanlength'}=$itemtype->{'loanlength'};
191 return($iteminformation);
195 # returns an array of borrower hash references, given a cardnumber or a partial
197 my ($env, $key) = @_;
200 my $q_key=$dbh->quote($key);
201 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
204 my ($borrower)=$sth->fetchrow_hashref;
205 push (@borrowers, $borrower);
207 $q_key=$dbh->quote("$key%");
209 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
211 while (my $borrower = $sth->fetchrow_hashref) {
212 push (@borrowers, $borrower);
222 # transfer book code....
223 my ($tbr, $barcode, $ignoreRs) = @_;
227 my $branches = getbranches();
228 my $iteminformation = getiteminformation(\%env, 0, $barcode);
230 if (not $iteminformation) {
231 $messages->{'BadBarcode'} = $barcode;
234 # get branches of book...
235 my $hbr = $iteminformation->{'homebranch'};
236 my $fbr = $iteminformation->{'holdingbranch'};
238 if ($branches->{$hbr}->{'PE'}) {
239 $messages->{'IsPermanent'} = $hbr;
241 # cant transfer book if is already there....
243 $messages->{'DestinationEqualsHolding'} = 1;
246 # check if it is still issued to someone, return it...
247 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
248 if ($currentborrower) {
249 returnbook($barcode, $fbr);
250 $messages->{'WasReturned'} = $currentborrower;
253 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
254 if ($resfound and not $ignoreRs) {
255 $resrec->{'ResFound'} = $resfound;
256 $messages->{'ResFound'} = $resrec;
259 #actually do the transfer....
261 dotransfer($iteminformation->{'itemnumber'}, $fbr, $tbr);
262 $messages->{'WasTransfered'} = 1;
264 return ($dotransfer, $messages, $iteminformation);
268 my ($itm, $fbr, $tbr) = @_;
269 my $dbh = &C4Connect;
270 $itm = $dbh->quote($itm);
271 $fbr = $dbh->quote($fbr);
272 $tbr = $dbh->quote($tbr);
273 #new entry in branchtransfers....
274 my $query = "insert into branchtransfers (itemnumber, frombranch, datearrived, tobranch)
275 values($itm, $fbr, now(), $tbr)";
276 my $sth = $dbh->prepare($query);
279 #update holdingbranch in items .....
280 $query = "update items set datelastseen = now(), holdingbranch=$tbr where items.itemnumber=$itm";
281 $sth = $dbh->prepare($query);
290 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
292 my $iteminformation=getiteminformation($env, 0, $barcode);
294 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
296 if ($patroninformation->{'gonenoaddress'}) {
297 $rejected="Patron is gone, with no known address.";
300 if ($patroninformation->{'lost'}) {
301 $rejected="Patron's card has been reported lost.";
304 if ($patroninformation->{'debarred'}) {
305 $rejected="Patron is Debarred";
308 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
309 if ($amount>5 && $patroninformation->{'categorycode'} ne 'L' &&
310 $patroninformation->{'categorycode'} ne 'W' &&
311 $patroninformation->{'categorycode'} ne 'I' &&
312 $patroninformation->{'categorycode'} ne 'B' &&
313 $patroninformation->{'categorycode'} ne 'P') {
314 $rejected=sprintf "Patron owes \$%.02f.", $amount;
317 unless ($iteminformation) {
318 $rejected="$barcode is not a valid barcode.";
321 if ($iteminformation->{'notforloan'} == 1) {
322 $rejected="Item not for loan.";
325 if ($iteminformation->{'wthdrawn'} == 1) {
326 $rejected="Item withdrawn.";
329 if ($iteminformation->{'restricted'} == 1) {
330 $rejected="Restricted item.";
333 if ($iteminformation->{'itemtype'} eq 'REF') {
334 $rejected="Reference item: Not for loan.";
337 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
338 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
339 # Already issued to current borrower
340 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
341 if ($renewstatus == 0) {
342 $rejected="No more renewals allowed for this item.";
345 if ($responses->{4} eq '') {
347 $question="Book is issued to this borrower.\nRenew?";
350 } elsif ($responses->{4} eq 'Y') {
351 my $charge = calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
353 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
354 $iteminformation->{'charge'}=$charge;
356 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
357 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
364 } elsif ($currentborrower ne '') {
365 my ($currborrower, $cbflags) = getpatroninformation($env,$currentborrower,0);
366 if ($responses->{1} eq '') {
368 $question = "Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
371 } elsif ($responses->{1} eq 'Y') {
372 returnbook($iteminformation->{'barcode'}, $env->{'branch'});
379 my ($resbor, $resrec) = checkreserve($env, $dbh, $iteminformation->{'itemnumber'});
381 if ($resbor eq $patroninformation->{'borrowernumber'}) {
382 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
383 my $rsth = $dbh->prepare($rquery);
386 } elsif ($resbor ne "") {
387 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
388 if ($responses->{2} eq '') {
390 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $resrec->{'reservedate'}\nAllow issue?";
393 } elsif ($responses->{2} eq 'N') {
394 #printreserve($env, $resrec, $resborrower, $iteminformation);
398 if ($responses->{3} eq '') {
400 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
403 } elsif ($responses->{3} eq 'Y') {
404 my $rquery = "update reserves set found = 'F' where reservedate = '$resrec->{'reservedate'}' and borrowernumber = '$resrec->{'borrowernumber'}' and biblionumber = '$resrec->{'biblionumber'}'";
405 my $rsth = $dbh->prepare($rquery);
413 unless (($question) || ($rejected) || ($noissue)) {
415 if ($iteminformation->{'loanlength'}) {
416 $loanlength=$iteminformation->{'loanlength'};
419 my $datedue=time+($loanlength)*86400;
420 my @datearr = localtime($datedue);
421 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
422 if ($env->{'datedue'}) {
423 $dateduef=$env->{'datedue'};
425 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
426 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
429 $iteminformation->{'issues'}++;
430 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'} where itemnumber=$iteminformation->{'itemnumber'}");
433 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
435 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
436 $iteminformation->{'charge'}=$charge;
438 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
441 if ($iteminformation->{'charge'}) {
442 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
445 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
451 my ($barcode, $branch) = @_;
455 # get information on item
456 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
457 if (not $iteminformation) {
458 $messages->{'BadBarcode'} = $barcode;
462 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
463 if ((not $currentborrower) && $doreturn) {
464 $messages->{'NotIssued'} = $barcode;
467 # check if the book is in a permanent collection....
468 my $hbr = $iteminformation->{'homebranch'};
469 my $branches = getbranches();
470 if ($branches->{$hbr}->{'PE'}) {
471 $messages->{'IsPermanent'} = $hbr;
473 # update issues, thereby returning book (should push this out into another subroutine
474 my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
476 doreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
477 $messages->{'WasReturned'};
480 my ($transfered, $mess, $item) = transferbook($branch, $barcode);
482 $messages->{'WasTransfered'};
484 # fix up the accounts.....
485 if ($iteminformation->{'itemlost'}) {
486 updateitemlost($iteminformation->{'itemnumber'});
487 fixaccountforlostandreturned($iteminformation, $borrower);
488 $messages->{'WasLost'};
490 # fix up the overdues in accounts...
491 fixoverduesonreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
493 my ($resfound, $resrec) = FindReserves($iteminformation->{'biblionumber'}, 0);
495 $messages->{'ResFound'} = $resrec;
498 UpdateStats(\%env, $branch ,'return','0','',$iteminformation->{'itemnumber'});
499 return ($doreturn, $messages, $iteminformation, $borrower);
504 my ($brn, $itm) = @_;
506 $brn = $dbh->quote($brn);
507 $itm = $dbh->quote($itm);
508 my $query = "update issues set returndate = now() where (borrowernumber = $brn)
509 and (itemnumber = $itm) and (returndate is null)";
510 my $sth = $dbh->prepare($query);
519 my $query="update items set itemlost=0 where itemnumber=$itemno";
520 my $sth=$dbh->prepare($query);
525 sub fixaccountforlostandreturned {
526 my ($iteminfo, $borrower) = @_;
529 my $itm = $dbh->quote($iteminfo->{'itemnumber'});
530 # check for charge made for lost book
531 my $query = "select * from accountlines where (itemnumber = $itm)
532 and (accounttype='L' or accounttype='Rep') order by date desc";
533 my $sth = $dbh->prepare($query);
535 if (my $data = $sth->fetchrow_hashref) {
536 # writeoff this amount
538 my $amount = $data->{'amount'};
539 my $acctno = $data->{'accountno'};
541 if ($data->{'amountoutstanding'} == $amount) {
542 $offset = $data->{'amount'};
545 $offset = $amount - $data->{'amountoutstanding'};
546 $amountleft = $data->{'amountoutstanding'} - $amount;
548 my $uquery = "update accountlines set accounttype = 'LR',amountoutstanding='0'
549 where (borrowernumber = '$data->{'borrowernumber'}')
550 and (itemnumber = $itm) and (accountno = '$acctno') ";
551 my $usth = $dbh->prepare($uquery);
554 #check if any credit is left if so writeoff other accounts
555 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
556 if ($amountleft < 0){
559 if ($amountleft > 0){
560 my $query = "select * from accountlines where (borrowernumber = '$data->{'borrowernumber'}')
561 and (amountoutstanding >0) order by date";
562 my $msth = $dbh->prepare($query);
564 # offset transactions
567 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
568 if ($accdata->{'amountoutstanding'} < $amountleft) {
570 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
572 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
575 my $thisacct = $accdata->{'accountno'};
576 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
577 where (borrowernumber = '$data->{'borrowernumber'}')
578 and (accountno='$thisacct')";
579 my $usth = $dbh->prepare($updquery);
582 $updquery = "insert into accountoffsets
583 (borrowernumber, accountno, offsetaccount, offsetamount)
585 ('$data->{'borrowernumber'}','$accdata->{'accountno'}','$nextaccntno','$newamtos')";
586 my $usth = $dbh->prepare($updquery);
592 if ($amountleft > 0){
595 my $desc="Book Returned ".$iteminfo->{'barcode'};
596 $uquery = "insert into accountlines
597 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
598 values ('$data->{'borrowernumber'}','$nextaccntno',now(),0-$amount,'$desc',
600 $usth = $dbh->prepare($uquery);
603 $uquery = "insert into accountoffsets
604 (borrowernumber, accountno, offsetaccount, offsetamount)
605 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
606 $usth = $dbh->prepare($uquery);
609 $uquery = "update items set paidfor='' where itemnumber=$itm";
610 $usth = $dbh->prepare($uquery);
618 sub fixoverduesonreturn {
619 my ($brn, $itm) = @_;
621 $itm = $dbh->quote($itm);
622 $brn = $dbh->quote($brn);
623 # check for overdue fine
624 my $query = "select * from accountlines where (borrowernumber=$brn)
625 and (itemnumber = $itm) and (accounttype='FU' or accounttype='O')";
626 my $sth = $dbh->prepare($query);
628 # alter fine to show that the book has been returned
629 if (my $data = $sth->fetchrow_hashref) {
630 my $query = "update accountlines set accounttype='F' where (borrowernumber = $brn)
631 and (itemnumber = $itm) and (acccountno='$data->{'accountno'}')";
632 my $usth=$dbh->prepare($query);
641 # Original subroutine for Circ2.pm
643 my ($env, $patroninformation, $dbh) = @_;
644 my $amount = checkaccount($env, $patroninformation->{'borrowernumber'}, $dbh);
647 $flaginfo{'message'}= sprintf "Patron owes \$%.02f", $amount;
649 $flaginfo{'noissues'} = 1;
651 $flags{'CHARGES'} = \%flaginfo;
652 } elsif ($amount < 0){
654 $amount = $amount*-1;
655 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", $amount;
656 $flags{'CHARGES'} = \%flaginfo;
658 if ($patroninformation->{'gonenoaddress'} == 1) {
660 $flaginfo{'message'} = 'Borrower has no valid address.';
661 $flaginfo{'noissues'} = 1;
662 $flags{'GNA'} = \%flaginfo;
664 if ($patroninformation->{'lost'} == 1) {
666 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
667 $flaginfo{'noissues'} = 1;
668 $flags{'LOST'} = \%flaginfo;
670 if ($patroninformation->{'debarred'} == 1) {
672 $flaginfo{'message'} = 'Borrower is Debarred.';
673 $flaginfo{'noissues'} = 1;
674 $flags{'DBARRED'} = \%flaginfo;
676 if ($patroninformation->{'borrowernotes'}) {
678 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
679 $flags{'NOTES'} = \%flaginfo;
681 my ($odues, $itemsoverdue) = checkoverdues($env, $patroninformation->{'borrowernumber'}, $dbh);
684 $flaginfo{'message'} = "Yes";
685 $flaginfo{'itemlist'} = $itemsoverdue;
686 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
687 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
689 $flags{'ODUES'} = \%flaginfo;
691 my ($nowaiting, $itemswaiting) = checkwaiting($env, $dbh, $patroninformation->{'borrowernumber'});
692 if ($nowaiting > 0) {
694 $flaginfo{'message'} = "Reserved items available";
695 $flaginfo{'itemlist'} = $itemswaiting;
696 $flaginfo{'itemfields'} = ['barcode', 'title', 'author', 'dewey', 'subclass', 'holdingbranch'];
697 $flags{'WAITING'} = \%flaginfo;
704 # From Main.pm, modified to return a list of overdueitems, in addition to a count
705 #checks whether a borrower has overdue items
706 my ($env,$bornum,$dbh)=@_;
707 my @datearr = localtime;
708 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
711 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'";
712 my $sth=$dbh->prepare($query);
714 while (my $data = $sth->fetchrow_hashref) {
715 push (@overdueitems, $data);
719 return ($count, \@overdueitems);
722 sub currentborrower {
723 # Original subroutine for Circ2.pm
724 my ($itemnumber) = @_;
725 my $dbh = &C4Connect;
726 my $q_itemnumber = $dbh->quote($itemnumber);
727 my $sth=$dbh->prepare("select borrowers.borrowernumber from
728 issues,borrowers where issues.itemnumber=$q_itemnumber and
729 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
732 my ($borrower) = $sth->fetchrow;
737 # Stolen from Main.pm
738 # Check for reserves for biblio
739 my ($env,$dbh,$itemnum)=@_;
741 my $query = "select * from reserves,items
742 where (items.itemnumber = '$itemnum')
743 and (reserves.cancellationdate is NULL)
744 and (items.biblionumber = reserves.biblionumber)
745 and ((reserves.found = 'W')
746 or (reserves.found is null))
748 my $sth = $dbh->prepare($query);
751 my $data=$sth->fetchrow_hashref;
752 while ($data && $resbor eq '') {
754 my $const = $data->{'constrainttype'};
756 $resbor = $data->{'borrowernumber'};
759 my $cquery = "select * from reserveconstraints,items
760 where (borrowernumber='$data->{'borrowernumber'}')
761 and reservedate='$data->{'reservedate'}'
762 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
763 and (items.itemnumber=$itemnum and
764 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
765 my $csth = $dbh->prepare($cquery);
767 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
769 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
771 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
775 $data=$sth->fetchrow_hashref;
778 return ($resbor,$resrec);
782 # New subroutine for Circ2.pm
783 my ($env, $borrower) = @_;
787 my $borrowernumber = $borrower->{'borrowernumber'};
789 if ($env->{'todaysissues'}) {
790 my @datearr = localtime(time());
791 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
792 $crit=" and issues.timestamp like '$today%' ";
794 if ($env->{'nottodaysissues'}) {
795 my @datearr = localtime(time());
796 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
797 $crit=" and !(issues.timestamp like '$today%') ";
799 my $select="select * from issues,items,biblioitems,biblio where
800 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
801 items.biblionumber=biblio.biblionumber and
802 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
803 $crit order by issues.timestamp desc";
805 my $sth=$dbh->prepare($select);
807 while (my $data = $sth->fetchrow_hashref) {
808 $data->{'dewey'}=~s/0*$//;
809 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
810 my @datearr = localtime(time());
811 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
812 +1)).sprintf ("%0.2d", $datearr[3]);
813 my $datedue=$data->{'date_due'};
815 if ($datedue < $todaysdate) {
816 $data->{'overdue'}=1;
818 my $itemnumber=$data->{'itemnumber'};
819 $currentissues{$counter}=$data;
824 return(\%currentissues);
827 # New subroutine for Circ2.pm
830 my $borrowernumber = $borrower->{'borrowernumber'};
831 my $brn =$dbh->quote($borrowernumber);
833 my $select = "select issues.timestamp, issues.date_due, items.biblionumber,
834 items.barcode, biblio.title, biblio.author, biblioitems.dewey,
836 from issues,items,biblioitems,biblio
837 where issues.borrowernumber = $brn
838 and issues.itemnumber = items.itemnumber
839 and items.biblionumber = biblio.biblionumber
840 and items.biblioitemnumber = biblioitems.biblioitemnumber
841 and issues.returndate is null
842 order by issues.timestamp desc";
844 my $sth=$dbh->prepare($select);
847 while (my $data = $sth->fetchrow_hashref) {
848 $data->{'dewey'} =~ s/0*$//;
849 ($data->{'dewey'} == 0) && ($data->{'dewey'} = '');
850 my @datearr = localtime(time());
851 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
852 my $datedue = $data->{'date_due'};
854 if ($datedue < $todaysdate) {
855 $data->{'overdue'} = 1;
857 $currentissues{$counter} = $data;
862 return(\%currentissues);
867 # check for reserves waiting
868 my ($env,$dbh,$bornum)=@_;
870 my $query = "select * from reserves
871 where (borrowernumber = '$bornum')
872 and (reserves.found='W') and cancellationdate is NULL";
873 my $sth = $dbh->prepare($query);
876 if (my $data=$sth->fetchrow_hashref) {
877 @itemswaiting[$cnt] =$data;
881 return ($cnt,\@itemswaiting);
886 # Stolen from Accounts.pm
887 #take borrower number
888 #check accounts and list amounts owing
889 my ($env,$bornumber,$dbh,$date)=@_;
890 my $select="Select sum(amountoutstanding) from accountlines where
891 borrowernumber=$bornumber and amountoutstanding<>0";
893 $select.=" and date < '$date'";
896 my $sth=$dbh->prepare($select);
899 while (my $data=$sth->fetchrow_hashref){
900 $total=$total+$data->{'sum(amountoutstanding)'};
903 # output(1,2,"borrower owes $total");
905 # # output(1,2,"borrower owes $total");
907 # reconcileaccount($env,$dbh,$bornumber,$total);
915 # Stolen from Renewals.pm
916 # check renewal status
917 my ($env,$dbh,$bornum,$itemno)=@_;
920 my $q1 = "select * from issues
921 where (borrowernumber = '$bornum')
922 and (itemnumber = '$itemno')
923 and returndate is null";
924 my $sth1 = $dbh->prepare($q1);
926 if (my $data1 = $sth1->fetchrow_hashref) {
927 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
928 where (items.itemnumber = '$itemno')
929 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
930 and (biblioitems.itemtype = itemtypes.itemtype)";
931 my $sth2 = $dbh->prepare($q2);
933 if (my $data2=$sth2->fetchrow_hashref) {
934 $renews = $data2->{'renewalsallowed'};
936 if ($renews > $data1->{'renewals'}) {
946 # Stolen from Renewals.pm
947 # mark book as renewed
948 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
949 $datedue=$env->{'datedue'};
950 if ($datedue eq "" ) {
952 my $query= "Select * from biblioitems,items,itemtypes
953 where (items.itemnumber = '$itemno')
954 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
955 and (biblioitems.itemtype = itemtypes.itemtype)";
956 my $sth=$dbh->prepare($query);
958 if (my $data=$sth->fetchrow_hashref) {
959 $loanlength = $data->{'loanlength'}
963 my $datedu = time + ($loanlength * 86400);
964 my @datearr = localtime($datedu);
965 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
967 my @date = split("-",$datedue);
968 my $odatedue = (@date[2]+0)."-".(@date[1]+0)."-".@date[0];
969 my $issquery = "select * from issues where borrowernumber='$bornum' and
970 itemnumber='$itemno' and returndate is null";
971 my $sth=$dbh->prepare($issquery);
973 my $issuedata=$sth->fetchrow_hashref;
975 my $renews = $issuedata->{'renewals'} +1;
976 my $updquery = "update issues
977 set date_due = '$datedue', renewals = '$renews'
978 where borrowernumber='$bornum' and
979 itemnumber='$itemno' and returndate is null";
980 my $sth=$dbh->prepare($updquery);
988 # Stolen from Issues.pm
989 # calculate charges due
990 my ($env, $dbh, $itemno, $bornum)=@_;
993 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes where (items.itemnumber ='$itemno') and (biblioitems.biblioitemnumber = items.biblioitemnumber) and (biblioitems.itemtype = itemtypes.itemtype)";
994 my $sth1= $dbh->prepare($q1);
996 if (my $data1=$sth1->fetchrow_hashref) {
997 $item_type = $data1->{'itemtype'};
998 $charge = $data1->{'rentalcharge'};
999 my $q2 = "select rentaldiscount from borrowers,categoryitem
1000 where (borrowers.borrowernumber = '$bornum')
1001 and (borrowers.categorycode = categoryitem.categorycode)
1002 and (categoryitem.itemtype = '$item_type')";
1003 my $sth2=$dbh->prepare($q2);
1005 if (my $data2=$sth2->fetchrow_hashref) {
1006 my $discount = $data2->{'rentaldiscount'};
1007 $charge = ($charge *(100 - $discount)) / 100;
1016 #Stolen from Issues.pm
1017 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
1018 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
1019 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
1020 my $sth = $dbh->prepare($query);
1027 # Stolen from Accounts.pm
1028 my ($env,$bornumber,$dbh)=@_;
1029 my $nextaccntno = 1;
1030 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
1031 my $sth = $dbh->prepare($query);
1033 if (my $accdata=$sth->fetchrow_hashref){
1034 $nextaccntno = $accdata->{'accountno'} + 1;
1037 return($nextaccntno);
1041 # Stolen from Returns.pm
1045 my ($itemdata) = getiteminformation(\%env, $itemno,0);
1046 my $bibno = $dbh->quote($itemdata->{'biblionumber'});
1047 my $bibitm = $dbh->quote($itemdata->{'biblioitemnumber'});
1048 my $query = "select * from reserves where ((found = 'W') or (found is null))
1049 and biblionumber = $bibno and cancellationdate is NULL
1050 order by priority, reservedate ";
1051 my $sth = $dbh->prepare($query);
1057 while (($resrec = $sth->fetchrow_hashref) && (not $resfound)) {
1059 my $brn = $dbh->quote($resrec->{'borrowernumber'});
1060 my $rdate = $dbh->quote($resrec->{'reservedate'});
1061 my $bibno = $dbh->quote($resrec->{'biblionumber'});
1062 if ($resrec->{'found'} eq "W") {
1063 if ($resrec->{'itemnumber'} eq $itemno) {
1067 if ($resrec->{'constrainttype'} eq "a") {
1070 my $conquery = "select * from reserveconstraints where borrowernumber = $brn
1071 and reservedate = $rdate and biblionumber = $bibno and biblioitemnumber = $bibitm";
1072 my $consth = $dbh->prepare($conquery);
1074 if (my $conrec = $consth->fetchrow_hashref) {
1075 if ($resrec->{'constrainttype'} eq "o") {
1083 my $updquery = "update reserves set found = 'W', itemnumber = '$itemno'
1084 where borrowernumber = $brn and reservedate = $rdate and biblionumber = $bibno";
1085 my $updsth = $dbh->prepare($updquery);
1091 return ($resfound,$lastrec);
1094 END { } # module clean-up code here (global destructor)