1 package C4::Circulation::Circ2;
3 #package to deal with Returns
4 #written 3/11/99 by olwen@katipo.co.nz
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
30 #use C4::InterfaceCDK;
31 #use C4::Circulation::Main;
32 #use C4::Circulation::Renewals;
39 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41 # set the version for version checking
45 @EXPORT = qw(&getbranches &getprinters &getpatroninformation ¤tissues &getissues &getiteminformation &findborrower &issuebook &returnbook &find_reserves &transferbook &decode
47 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
49 # your exported package globals go here,
50 # as well as any optionally exported functions
52 @EXPORT_OK = qw($Var1 %Hashit);
55 # non-exported package globals go here
56 #use vars qw(@more $stuff);
58 # initalize package globals, first exported ones
63 # then the others (which are still accessible as $Some::Module::stuff)
67 # all file-scoped lexicals must be created before
68 # the functions below that use them.
70 # file-private lexicals go here
74 # here's a file-private function as a closure,
75 # callable as &$priv_func; it cannot be prototyped.
80 # make all your functions, whether exported or not;
84 # returns a reference to a hash of references to branches...
86 my $dbh = C4::Context->dbh;
87 my $sth=$dbh->prepare("select * from branches");
89 while (my $branch=$sth->fetchrow_hashref) {
90 my $tmp = $branch->{'branchcode'}; my $brc = $dbh->quote($tmp);
91 my $query = "select categorycode from branchrelations where branchcode = $brc";
92 my $nsth = $dbh->prepare($query);
94 while (my ($cat) = $nsth->fetchrow_array) {
98 $branches{$branch->{'branchcode'}}=$branch;
107 my $dbh = C4::Context->dbh;
108 my $sth=$dbh->prepare("select * from printers");
110 while (my $printer=$sth->fetchrow_hashref) {
111 $printers{$printer->{'printqueue'}}=$printer;
118 sub getpatroninformation {
120 my ($env, $borrowernumber,$cardnumber) = @_;
121 my $dbh = C4::Context->dbh;
124 if ($borrowernumber) {
125 $query = "select * from borrowers where borrowernumber=$borrowernumber";
126 } elsif ($cardnumber) {
127 $query = "select * from borrowers where cardnumber=$cardnumber";
129 $env->{'apierror'} = "invalid borrower information passed to getpatroninformation subroutine";
132 $env->{'mess'} = $query;
133 $sth = $dbh->prepare($query);
135 my $borrower = $sth->fetchrow_hashref;
136 my $flags = patronflags($env, $borrower, $dbh);
138 $borrower->{'flags'}=$flags;
139 return($borrower, $flags);
144 my $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
145 my @s = map { index($seq,$_); } split(//,$encoded);
160 my $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
161 $r .=chr(($n >> 16) ^ 67) .
162 chr(($n >> 8 & 255) ^ 67) .
163 chr(($n & 255) ^ 67);
166 $r = substr($r,0,length($r)-$l);
173 sub getiteminformation {
174 # returns a hash of item information given either the itemnumber or the barcode
175 my ($env, $itemnumber, $barcode) = @_;
176 my $dbh = C4::Context->dbh;
179 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.itemnumber=$itemnumber and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
181 my $q_barcode=$dbh->quote($barcode);
182 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.barcode=$q_barcode and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
184 $env->{'apierror'}="getiteminformation() subroutine must be called with either an itemnumber or a barcode";
189 my $iteminformation=$sth->fetchrow_hashref;
191 if ($iteminformation) {
192 $sth=$dbh->prepare("select date_due from issues where itemnumber=$iteminformation->{'itemnumber'} and isnull(returndate)");
194 my ($date_due) = $sth->fetchrow;
195 $iteminformation->{'date_due'}=$date_due;
197 #$iteminformation->{'dewey'}=~s/0*$//;
198 ($iteminformation->{'dewey'} == 0) && ($iteminformation->{'dewey'}='');
199 $sth=$dbh->prepare("select * from itemtypes where itemtype='$iteminformation->{'itemtype'}'");
201 my $itemtype=$sth->fetchrow_hashref;
202 $iteminformation->{'loanlength'}=$itemtype->{'loanlength'};
203 $iteminformation->{'notforloan'}=$itemtype->{'notforloan'};
206 return($iteminformation);
210 # returns an array of borrower hash references, given a cardnumber or a partial
212 my ($env, $key) = @_;
213 my $dbh = C4::Context->dbh;
215 my $q_key=$dbh->quote($key);
216 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
219 my ($borrower)=$sth->fetchrow_hashref;
220 push (@borrowers, $borrower);
222 $q_key=$dbh->quote("$key%");
224 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
226 while (my $borrower = $sth->fetchrow_hashref) {
227 push (@borrowers, $borrower);
236 # transfer book code....
237 my ($tbr, $barcode, $ignoreRs) = @_;
241 my $branches = getbranches();
242 my $iteminformation = getiteminformation(\%env, 0, $barcode);
244 if (not $iteminformation) {
245 $messages->{'BadBarcode'} = $barcode;
248 # get branches of book...
249 my $hbr = $iteminformation->{'homebranch'};
250 my $fbr = $iteminformation->{'holdingbranch'};
252 if ($branches->{$hbr}->{'PE'}) {
253 $messages->{'IsPermanent'} = $hbr;
255 # cant transfer book if is already there....
257 $messages->{'DestinationEqualsHolding'} = 1;
260 # check if it is still issued to someone, return it...
261 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
262 if ($currentborrower) {
263 returnbook($barcode, $fbr);
264 $messages->{'WasReturned'} = $currentborrower;
267 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
268 if ($resfound and not $ignoreRs) {
269 $resrec->{'ResFound'} = $resfound;
270 $messages->{'ResFound'} = $resrec;
273 #actually do the transfer....
275 dotransfer($iteminformation->{'itemnumber'}, $fbr, $tbr);
276 $messages->{'WasTransfered'} = 1;
278 return ($dotransfer, $messages, $iteminformation);
282 my ($itm, $fbr, $tbr) = @_;
283 my $dbh = C4::Context->dbh;
284 $itm = $dbh->quote($itm);
285 $fbr = $dbh->quote($fbr);
286 $tbr = $dbh->quote($tbr);
287 #new entry in branchtransfers....
289 INSERT INTO branchtransfers
290 (itemnumber, frombranch, datearrived, tobranch)
291 VALUES ($itm, $fbr, now(), $tbr)
294 #update holdingbranch in items .....
297 SET datelastseen = now(),
299 WHERE items.itemnumber = $itm
306 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
307 my $dbh = C4::Context->dbh;
308 my $iteminformation = getiteminformation($env, 0, $barcode);
310 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
313 if ($patroninformation->{'gonenoaddress'}) {
314 $rejected="Patron is gone, with no known address.";
317 if ($patroninformation->{'lost'}) {
318 $rejected="Patron's card has been reported lost.";
321 if ($patroninformation->{'debarred'}) {
322 $rejected="Patron is Debarred";
325 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
326 if ($amount > 5 && $patroninformation->{'categorycode'} ne 'L' &&
327 $patroninformation->{'categorycode'} ne 'W' &&
328 $patroninformation->{'categorycode'} ne 'I' &&
329 $patroninformation->{'categorycode'} ne 'B' &&
330 $patroninformation->{'categorycode'} ne 'P') {
331 $rejected = sprintf "Patron owes \$%.02f.", $amount;
334 unless ($iteminformation) {
335 $rejected = "$barcode is not a valid barcode.";
338 if ($iteminformation->{'notforloan'} == 1) {
339 $rejected="Reference item: not for loan.";
342 if ($iteminformation->{'wthdrawn'} == 1) {
343 $rejected="Item withdrawn.";
346 if ($iteminformation->{'restricted'} == 1) {
347 $rejected="Restricted item.";
350 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
351 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
352 # Already issued to current borrower
353 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
354 if ($renewstatus == 0) {
355 $rejected="No more renewals allowed for this item.";
358 if ($responses->{4} eq '') {
360 $question = "Book is issued to this borrower.\nRenew?";
361 $defaultanswer = 'Y';
363 } elsif ($responses->{4} eq 'Y') {
364 my $charge = calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
366 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
367 $iteminformation->{'charge'} = $charge;
369 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
370 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
377 } elsif ($currentborrower ne '') {
378 my ($currborrower, $cbflags) = getpatroninformation($env,$currentborrower,0);
379 if ($responses->{1} eq '') {
381 $question = "Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
384 } elsif ($responses->{1} eq 'Y') {
385 returnbook($iteminformation->{'barcode'}, $env->{'branch'});
392 my ($restype, $res) = CheckReserves($iteminformation->{'itemnumber'});
394 my $resbor = $res->{'borrowernumber'};
395 if ($resbor eq $patroninformation->{'borrowernumber'}) {
397 } elsif ($restype eq "Waiting") {
398 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
399 my $branches = getbranches();
400 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
401 if ($responses->{2} eq '') {
403 $question="<font color=red>Waiting</font> for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) at $branchname \nAllow issue?";
406 } elsif ($responses->{2} eq 'N') {
410 if ($responses->{3} eq '') {
412 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
415 } elsif ($responses->{3} eq 'Y') {
416 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
419 } elsif ($restype eq "Reserved") {
420 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
421 my $branches = getbranches();
422 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
423 if ($responses->{5} eq '') {
425 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $res->{'reservedate'} \nAllow issue?";
428 } elsif ($responses->{5} eq 'N') {
429 if ($responses->{6} eq '') {
431 $question="Set reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) to waiting and transfer to $branchname?";
433 } elsif ($responses->{6} eq 'Y') {
434 my $tobrcd = ReserveWaiting($res->{'itemnumber'}, $res->{'borrowernumber'});
435 transferbook($tobrcd, $barcode, 1);
436 $message = "Item should now be waiting at $branchname";
441 if ($responses->{7} eq '') {
443 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
446 } elsif ($responses->{7} eq 'Y') {
447 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
454 unless (($question) || ($rejected) || ($noissue)) {
456 if ($iteminformation->{'loanlength'}) {
457 $loanlength=$iteminformation->{'loanlength'};
460 my $datedue=time+($loanlength)*86400;
461 my @datearr = localtime($datedue);
462 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
463 if ($env->{'datedue'}) {
464 $dateduef=$env->{'datedue'};
466 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
467 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
470 $iteminformation->{'issues'}++;
471 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'},datelastseen=now() where itemnumber=$iteminformation->{'itemnumber'}");
474 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
476 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
477 $iteminformation->{'charge'}=$charge;
479 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
481 if ($iteminformation->{'charge'}) {
482 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
484 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
490 my ($barcode, $branch) = @_;
494 # get information on item
495 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
496 if (not $iteminformation) {
497 $messages->{'BadBarcode'} = $barcode;
501 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
502 if ((not $currentborrower) && $doreturn) {
503 $messages->{'NotIssued'} = $barcode;
506 # check if the book is in a permanent collection....
507 my $hbr = $iteminformation->{'homebranch'};
508 my $branches = getbranches();
509 if ($branches->{$hbr}->{'PE'}) {
510 $messages->{'IsPermanent'} = $hbr;
512 # check that the book has been cancelled
513 if ($iteminformation->{'wthdrawn'}) {
514 $messages->{'wthdrawn'} = 1;
517 # update issues, thereby returning book (should push this out into another subroutine
518 my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
519 if ($doreturn) { # FIXME - perl -wc complains about this line.
520 doreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
521 $messages->{'WasReturned'};
523 ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
525 my ($transfered, $mess, $item) = transferbook($branch, $barcode, 1);
526 if ($transfered) { # FIXME - perl -wc complains about this line.
527 $messages->{'WasTransfered'};
529 # fix up the accounts.....
530 if ($iteminformation->{'itemlost'}) { # FIXME - perl -wc complains about this line.
531 updateitemlost($iteminformation->{'itemnumber'});
532 fixaccountforlostandreturned($iteminformation, $borrower);
533 $messages->{'WasLost'};
535 # fix up the overdues in accounts...
536 fixoverduesonreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
538 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
540 $resrec->{'ResFound'} = $resfound;
541 $messages->{'ResFound'} = $resrec;
544 UpdateStats(\%env, $branch ,'return','0','',$iteminformation->{'itemnumber'});
545 return ($doreturn, $messages, $iteminformation, $borrower);
550 my ($brn, $itm) = @_;
551 my $dbh = C4::Context->dbh;
552 $brn = $dbh->quote($brn);
553 $itm = $dbh->quote($itm);
554 my $query = "update issues set returndate = now() where (borrowernumber = $brn)
555 and (itemnumber = $itm) and (returndate is null)";
556 my $sth = $dbh->prepare($query);
559 $query="update items set datelastseen=now() where itemnumber=$itm";
560 $sth=$dbh->prepare($query);
568 my $dbh = C4::Context->dbh;
573 WHERE itemnumber = $itemno
577 sub fixaccountforlostandreturned {
578 my ($iteminfo, $borrower) = @_;
580 my $dbh = C4::Context->dbh;
581 my $itm = $dbh->quote($iteminfo->{'itemnumber'});
582 # check for charge made for lost book
583 my $query = "select * from accountlines where (itemnumber = $itm)
584 and (accounttype='L' or accounttype='Rep') order by date desc";
585 my $sth = $dbh->prepare($query);
587 if (my $data = $sth->fetchrow_hashref) {
588 # writeoff this amount
590 my $amount = $data->{'amount'};
591 my $acctno = $data->{'accountno'};
593 if ($data->{'amountoutstanding'} == $amount) {
594 $offset = $data->{'amount'};
597 $offset = $amount - $data->{'amountoutstanding'};
598 $amountleft = $data->{'amountoutstanding'} - $amount;
600 my $uquery = "update accountlines set accounttype = 'LR',amountoutstanding='0'
601 where (borrowernumber = '$data->{'borrowernumber'}')
602 and (itemnumber = $itm) and (accountno = '$acctno') ";
603 my $usth = $dbh->prepare($uquery);
606 #check if any credit is left if so writeoff other accounts
607 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
608 if ($amountleft < 0){
611 if ($amountleft > 0){
612 my $query = "select * from accountlines where (borrowernumber = '$data->{'borrowernumber'}')
613 and (amountoutstanding >0) order by date";
614 my $msth = $dbh->prepare($query);
616 # offset transactions
619 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
620 if ($accdata->{'amountoutstanding'} < $amountleft) {
622 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
624 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
627 my $thisacct = $accdata->{'accountno'};
628 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
629 where (borrowernumber = '$data->{'borrowernumber'}')
630 and (accountno='$thisacct')";
631 my $usth = $dbh->prepare($updquery);
634 $updquery = "insert into accountoffsets
635 (borrowernumber, accountno, offsetaccount, offsetamount)
637 ('$data->{'borrowernumber'}','$accdata->{'accountno'}','$nextaccntno','$newamtos')";
638 $usth = $dbh->prepare($updquery);
644 if ($amountleft > 0){
647 my $desc="Book Returned ".$iteminfo->{'barcode'};
648 $uquery = "insert into accountlines
649 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
650 values ('$data->{'borrowernumber'}','$nextaccntno',now(),0-$amount,'$desc',
652 $usth = $dbh->prepare($uquery);
655 $uquery = "insert into accountoffsets
656 (borrowernumber, accountno, offsetaccount, offsetamount)
657 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
658 $usth = $dbh->prepare($uquery);
661 $uquery = "update items set paidfor='' where itemnumber=$itm";
662 $usth = $dbh->prepare($uquery);
670 sub fixoverduesonreturn {
671 my ($brn, $itm) = @_;
672 my $dbh = C4::Context->dbh;
673 $itm = $dbh->quote($itm);
674 $brn = $dbh->quote($brn);
675 # check for overdue fine
676 my $query = "select * from accountlines where (borrowernumber=$brn)
677 and (itemnumber = $itm) and (accounttype='FU' or accounttype='O')";
678 my $sth = $dbh->prepare($query);
680 # alter fine to show that the book has been returned
681 if (my $data = $sth->fetchrow_hashref) {
682 my $query = "update accountlines set accounttype='F' where (borrowernumber = $brn)
683 and (itemnumber = $itm) and (acccountno='$data->{'accountno'}')";
684 my $usth=$dbh->prepare($query);
693 # Original subroutine for Circ2.pm
695 my ($env, $patroninformation, $dbh) = @_;
696 my $amount = checkaccount($env, $patroninformation->{'borrowernumber'}, $dbh);
699 $flaginfo{'message'}= sprintf "Patron owes \$%.02f", $amount;
701 $flaginfo{'noissues'} = 1;
703 $flags{'CHARGES'} = \%flaginfo;
704 } elsif ($amount < 0){
706 $amount = $amount*-1;
707 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", $amount;
708 $flags{'CHARGES'} = \%flaginfo;
710 if ($patroninformation->{'gonenoaddress'} == 1) {
712 $flaginfo{'message'} = 'Borrower has no valid address.';
713 $flaginfo{'noissues'} = 1;
714 $flags{'GNA'} = \%flaginfo;
716 if ($patroninformation->{'lost'} == 1) {
718 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
719 $flaginfo{'noissues'} = 1;
720 $flags{'LOST'} = \%flaginfo;
722 if ($patroninformation->{'debarred'} == 1) {
724 $flaginfo{'message'} = 'Borrower is Debarred.';
725 $flaginfo{'noissues'} = 1;
726 $flags{'DBARRED'} = \%flaginfo;
728 if ($patroninformation->{'borrowernotes'}) {
730 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
731 $flags{'NOTES'} = \%flaginfo;
733 my ($odues, $itemsoverdue)
734 = checkoverdues($env, $patroninformation->{'borrowernumber'}, $dbh);
737 $flaginfo{'message'} = "Yes";
738 $flaginfo{'itemlist'} = $itemsoverdue;
739 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
740 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
742 $flags{'ODUES'} = \%flaginfo;
744 my ($nowaiting, $itemswaiting)
745 = CheckWaiting($patroninformation->{'borrowernumber'});
746 if ($nowaiting > 0) {
748 $flaginfo{'message'} = "Reserved items available";
749 $flaginfo{'itemlist'} = $itemswaiting;
750 $flags{'WAITING'} = \%flaginfo;
757 # From Main.pm, modified to return a list of overdueitems, in addition to a count
758 #checks whether a borrower has overdue items
759 my ($env, $bornum, $dbh)=@_;
760 my @datearr = localtime;
761 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
764 my $query = "SELECT * FROM issues,biblio,biblioitems,items
765 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
766 AND items.biblionumber = biblio.biblionumber
767 AND issues.itemnumber = items.itemnumber
768 AND issues.borrowernumber = $bornum
769 AND issues.returndate is NULL
770 AND issues.date_due < '$today'";
771 my $sth = $dbh->prepare($query);
773 while (my $data = $sth->fetchrow_hashref) {
774 push (@overdueitems, $data);
778 return ($count, \@overdueitems);
781 sub currentborrower {
782 # Original subroutine for Circ2.pm
783 my ($itemnumber) = @_;
784 my $dbh = C4::Context->dbh;
785 my $q_itemnumber = $dbh->quote($itemnumber);
786 my $sth=$dbh->prepare("select borrowers.borrowernumber from
787 issues,borrowers where issues.itemnumber=$q_itemnumber and
788 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
791 my ($borrower) = $sth->fetchrow;
796 # Stolen from Main.pm
797 # Check for reserves for biblio
798 my ($env,$dbh,$itemnum)=@_;
800 my $query = "select * from reserves,items
801 where (items.itemnumber = '$itemnum')
802 and (reserves.cancellationdate is NULL)
803 and (items.biblionumber = reserves.biblionumber)
804 and ((reserves.found = 'W')
805 or (reserves.found is null))
807 my $sth = $dbh->prepare($query);
810 my $data=$sth->fetchrow_hashref;
811 while ($data && $resbor eq '') {
813 my $const = $data->{'constrainttype'};
815 $resbor = $data->{'borrowernumber'};
818 my $cquery = "select * from reserveconstraints,items
819 where (borrowernumber='$data->{'borrowernumber'}')
820 and reservedate='$data->{'reservedate'}'
821 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
822 and (items.itemnumber=$itemnum and
823 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
824 my $csth = $dbh->prepare($cquery);
826 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
828 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
830 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
834 $data=$sth->fetchrow_hashref;
837 return ($resbor,$resrec);
841 # New subroutine for Circ2.pm
842 my ($env, $borrower) = @_;
843 my $dbh = C4::Context->dbh;
846 my $borrowernumber = $borrower->{'borrowernumber'};
848 if ($env->{'todaysissues'}) {
849 my @datearr = localtime(time());
850 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
851 $crit=" and issues.timestamp like '$today%' ";
853 if ($env->{'nottodaysissues'}) {
854 my @datearr = localtime(time());
855 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
856 $crit=" and !(issues.timestamp like '$today%') ";
858 my $select="select * from issues,items,biblioitems,biblio where
859 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
860 items.biblionumber=biblio.biblionumber and
861 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
862 $crit order by issues.date_due";
864 my $sth=$dbh->prepare($select);
866 while (my $data = $sth->fetchrow_hashref) {
867 $data->{'dewey'}=~s/0*$//;
868 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
869 my @datearr = localtime(time());
870 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
871 +1)).sprintf ("%0.2d", $datearr[3]);
872 my $datedue=$data->{'date_due'};
874 if ($datedue < $todaysdate) {
875 $data->{'overdue'}=1;
877 my $itemnumber=$data->{'itemnumber'};
878 $currentissues{$counter}=$data;
882 return(\%currentissues);
886 # New subroutine for Circ2.pm
888 my $dbh = C4::Context->dbh;
889 my $borrowernumber = $borrower->{'borrowernumber'};
890 my $brn =$dbh->quote($borrowernumber);
892 my $select = "select issues.timestamp, issues.date_due, items.biblionumber,
893 items.barcode, biblio.title, biblio.author, biblioitems.dewey,
895 from issues,items,biblioitems,biblio
896 where issues.borrowernumber = $brn
897 and issues.itemnumber = items.itemnumber
898 and items.biblionumber = biblio.biblionumber
899 and items.biblioitemnumber = biblioitems.biblioitemnumber
900 and issues.returndate is null
901 order by issues.date_due";
903 my $sth=$dbh->prepare($select);
906 while (my $data = $sth->fetchrow_hashref) {
907 $data->{'dewey'} =~ s/0*$//;
908 ($data->{'dewey'} == 0) && ($data->{'dewey'} = '');
909 my @datearr = localtime(time());
910 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
911 my $datedue = $data->{'date_due'};
913 if ($datedue < $todaysdate) {
914 $data->{'overdue'} = 1;
916 $currentissues{$counter} = $data;
920 return(\%currentissues);
925 # check for reserves waiting
926 my ($env,$dbh,$bornum)=@_;
928 my $query = "select * from reserves
929 where (borrowernumber = '$bornum')
930 and (reserves.found='W') and cancellationdate is NULL";
931 my $sth = $dbh->prepare($query);
934 if (my $data=$sth->fetchrow_hashref) {
935 $itemswaiting[$cnt] =$data;
939 return ($cnt,\@itemswaiting);
942 # FIXME - This is nearly-identical to &C4::Accounts::checkaccount
944 # Stolen from Accounts.pm
945 #take borrower number
946 #check accounts and list amounts owing
947 my ($env,$bornumber,$dbh,$date)=@_;
948 my $select="Select sum(amountoutstanding) from accountlines where
949 borrowernumber=$bornumber and amountoutstanding<>0";
951 $select.=" and date < '$date'";
954 my $sth=$dbh->prepare($select);
957 while (my $data=$sth->fetchrow_hashref){
958 $total=$total+$data->{'sum(amountoutstanding)'};
961 # output(1,2,"borrower owes $total");
963 # # output(1,2,"borrower owes $total");
965 # reconcileaccount($env,$dbh,$bornumber,$total);
973 # Stolen from Renewals.pm
974 # check renewal status
975 my ($env,$dbh,$bornum,$itemno)=@_;
978 my $q1 = "select * from issues
979 where (borrowernumber = '$bornum')
980 and (itemnumber = '$itemno')
981 and returndate is null";
982 my $sth1 = $dbh->prepare($q1);
984 if (my $data1 = $sth1->fetchrow_hashref) {
985 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
986 where (items.itemnumber = '$itemno')
987 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
988 and (biblioitems.itemtype = itemtypes.itemtype)";
989 my $sth2 = $dbh->prepare($q2);
991 if (my $data2=$sth2->fetchrow_hashref) {
992 $renews = $data2->{'renewalsallowed'};
994 if ($renews > $data1->{'renewals'}) {
1004 # Stolen from Renewals.pm
1005 # mark book as renewed
1006 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
1007 $datedue=$env->{'datedue'};
1008 if ($datedue eq "" ) {
1010 my $query= "Select * from biblioitems,items,itemtypes
1011 where (items.itemnumber = '$itemno')
1012 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1013 and (biblioitems.itemtype = itemtypes.itemtype)";
1014 my $sth=$dbh->prepare($query);
1016 if (my $data=$sth->fetchrow_hashref) {
1017 $loanlength = $data->{'loanlength'}
1021 my $datedu = time + ($loanlength * 86400);
1022 my @datearr = localtime($datedu);
1023 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
1025 my @date = split("-",$datedue);
1026 my $odatedue = ($date[2]+0)."-".($date[1]+0)."-".$date[0];
1027 my $issquery = "select * from issues where borrowernumber='$bornum' and
1028 itemnumber='$itemno' and returndate is null";
1029 my $sth=$dbh->prepare($issquery);
1031 my $issuedata=$sth->fetchrow_hashref;
1033 my $renews = $issuedata->{'renewals'} +1;
1034 my $updquery = "update issues
1035 set date_due = '$datedue', renewals = '$renews'
1036 where borrowernumber='$bornum' and
1037 itemnumber='$itemno' and returndate is null";
1038 $sth=$dbh->prepare($updquery);
1045 # FIXME - This is almost, but not quite, identical to
1046 # &C4::Circulation::Issues::calc_charges and
1047 # &C4::Circulation::Renewals2::calc_charges.
1048 # Pick one and stick with it.
1050 # Stolen from Issues.pm
1051 # calculate charges due
1052 my ($env, $dbh, $itemno, $bornum)=@_;
1057 # open (FILE,">>/tmp/charges");
1059 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
1060 where (items.itemnumber ='$itemno')
1061 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1062 and (biblioitems.itemtype = itemtypes.itemtype)";
1063 my $sth1= $dbh->prepare($q1);
1064 # print FILE "$q1\n";
1066 if (my $data1=$sth1->fetchrow_hashref) {
1067 $item_type = $data1->{'itemtype'};
1068 $charge = $data1->{'rentalcharge'};
1069 # print FILE "charge is $charge\n";
1070 my $q2 = "select rentaldiscount from borrowers,categoryitem
1071 where (borrowers.borrowernumber = '$bornum')
1072 and (borrowers.categorycode = categoryitem.categorycode)
1073 and (categoryitem.itemtype = '$item_type')";
1074 my $sth2=$dbh->prepare($q2);
1077 if (my $data2=$sth2->fetchrow_hashref) {
1078 my $discount = $data2->{'rentaldiscount'};
1079 # print FILE "discount is $discount";
1080 if ($discount eq 'NULL') {
1083 $charge = ($charge *(100 - $discount)) / 100;
1093 #Stolen from Issues.pm
1094 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
1095 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
1096 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
1097 my $sth = $dbh->prepare($query);
1104 # Stolen from Accounts.pm
1105 my ($env,$bornumber,$dbh)=@_;
1106 my $nextaccntno = 1;
1107 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
1108 my $sth = $dbh->prepare($query);
1110 if (my $accdata=$sth->fetchrow_hashref){
1111 $nextaccntno = $accdata->{'accountno'} + 1;
1114 return($nextaccntno);
1118 # Stolen from Returns.pm
1121 my $dbh = C4::Context->dbh;
1122 my ($itemdata) = getiteminformation(\%env, $itemno,0);
1123 my $bibno = $dbh->quote($itemdata->{'biblionumber'});
1124 my $bibitm = $dbh->quote($itemdata->{'biblioitemnumber'});
1125 my $query = "select * from reserves where ((found = 'W') or (found is null))
1126 and biblionumber = $bibno and cancellationdate is NULL
1127 order by priority, reservedate ";
1128 my $sth = $dbh->prepare($query);
1134 while (($resrec = $sth->fetchrow_hashref) && (not $resfound)) {
1136 my $brn = $dbh->quote($resrec->{'borrowernumber'});
1137 my $rdate = $dbh->quote($resrec->{'reservedate'});
1138 my $bibno = $dbh->quote($resrec->{'biblionumber'});
1139 if ($resrec->{'found'} eq "W") {
1140 if ($resrec->{'itemnumber'} eq $itemno) {
1144 if ($resrec->{'constrainttype'} eq "a") {
1147 my $conquery = "select * from reserveconstraints where borrowernumber = $brn
1148 and reservedate = $rdate and biblionumber = $bibno and biblioitemnumber = $bibitm";
1149 my $consth = $dbh->prepare($conquery);
1151 if (my $conrec = $consth->fetchrow_hashref) {
1152 if ($resrec->{'constrainttype'} eq "o") {
1160 my $updquery = "update reserves set found = 'W', itemnumber = '$itemno'
1161 where borrowernumber = $brn and reservedate = $rdate and biblionumber = $bibno";
1162 my $updsth = $dbh->prepare($updquery);
1168 return ($resfound,$lastrec);
1171 END { } # module clean-up code here (global destructor)