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;
33 #use C4::Circulation::Renewals;
40 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42 # set the version for version checking
46 @EXPORT = qw(&getbranches &getprinters &getpatroninformation ¤tissues &getissues &getiteminformation &findborrower &issuebook &returnbook &find_reserves &transferbook &decode
48 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
50 # your exported package globals go here,
51 # as well as any optionally exported functions
53 @EXPORT_OK = qw($Var1 %Hashit);
56 # non-exported package globals go here
57 #use vars qw(@more $stuff);
59 # initalize package globals, first exported ones
64 # then the others (which are still accessible as $Some::Module::stuff)
68 # all file-scoped lexicals must be created before
69 # the functions below that use them.
71 # file-private lexicals go here
75 # here's a file-private function as a closure,
76 # callable as &$priv_func; it cannot be prototyped.
81 # make all your functions, whether exported or not;
85 # returns a reference to a hash of references to branches...
88 my $sth=$dbh->prepare("select * from branches");
90 while (my $branch=$sth->fetchrow_hashref) {
91 my $tmp = $branch->{'branchcode'}; my $brc = $dbh->quote($tmp);
92 my $query = "select categorycode from branchrelations where branchcode = $brc";
93 my $nsth = $dbh->prepare($query);
95 while (my ($cat) = $nsth->fetchrow_array) {
99 $branches{$branch->{'branchcode'}}=$branch;
110 my $sth=$dbh->prepare("select * from printers");
112 while (my $printer=$sth->fetchrow_hashref) {
113 $printers{$printer->{'printqueue'}}=$printer;
121 sub getpatroninformation {
123 my ($env, $borrowernumber,$cardnumber) = @_;
127 if ($borrowernumber) {
128 $query = "select * from borrowers where borrowernumber=$borrowernumber";
129 } elsif ($cardnumber) {
130 $query = "select * from borrowers where cardnumber=$cardnumber";
132 $env->{'apierror'} = "invalid borrower information passed to getpatroninformation subroutine";
135 $env->{'mess'} = $query;
136 $sth = $dbh->prepare($query);
138 my $borrower = $sth->fetchrow_hashref;
139 my $flags = patronflags($env, $borrower, $dbh);
142 $borrower->{'flags'}=$flags;
143 return($borrower, $flags);
148 my $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
149 my @s = map { index($seq,$_); } split(//,$encoded);
164 my $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
165 $r .=chr(($n >> 16) ^ 67) .
166 chr(($n >> 8 & 255) ^ 67) .
167 chr(($n & 255) ^ 67);
170 $r = substr($r,0,length($r)-$l);
177 sub getiteminformation {
178 # returns a hash of item information given either the itemnumber or the barcode
179 my ($env, $itemnumber, $barcode) = @_;
183 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.itemnumber=$itemnumber and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
185 my $q_barcode=$dbh->quote($barcode);
186 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.barcode=$q_barcode and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
188 $env->{'apierror'}="getiteminformation() subroutine must be called with either an itemnumber or a barcode";
193 my $iteminformation=$sth->fetchrow_hashref;
195 if ($iteminformation) {
196 $sth=$dbh->prepare("select date_due from issues where itemnumber=$iteminformation->{'itemnumber'} and isnull(returndate)");
198 my ($date_due) = $sth->fetchrow;
199 $iteminformation->{'date_due'}=$date_due;
201 #$iteminformation->{'dewey'}=~s/0*$//;
202 ($iteminformation->{'dewey'} == 0) && ($iteminformation->{'dewey'}='');
203 $sth=$dbh->prepare("select * from itemtypes where itemtype='$iteminformation->{'itemtype'}'");
205 my $itemtype=$sth->fetchrow_hashref;
206 $iteminformation->{'loanlength'}=$itemtype->{'loanlength'};
207 $iteminformation->{'notforloan'}=$itemtype->{'notforloan'};
211 return($iteminformation);
215 # returns an array of borrower hash references, given a cardnumber or a partial
217 my ($env, $key) = @_;
220 my $q_key=$dbh->quote($key);
221 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
224 my ($borrower)=$sth->fetchrow_hashref;
225 push (@borrowers, $borrower);
227 $q_key=$dbh->quote("$key%");
229 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
231 while (my $borrower = $sth->fetchrow_hashref) {
232 push (@borrowers, $borrower);
242 # transfer book code....
243 my ($tbr, $barcode, $ignoreRs) = @_;
247 my $branches = getbranches();
248 my $iteminformation = getiteminformation(\%env, 0, $barcode);
250 if (not $iteminformation) {
251 $messages->{'BadBarcode'} = $barcode;
254 # get branches of book...
255 my $hbr = $iteminformation->{'homebranch'};
256 my $fbr = $iteminformation->{'holdingbranch'};
258 if ($branches->{$hbr}->{'PE'}) {
259 $messages->{'IsPermanent'} = $hbr;
261 # cant transfer book if is already there....
263 $messages->{'DestinationEqualsHolding'} = 1;
266 # check if it is still issued to someone, return it...
267 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
268 if ($currentborrower) {
269 returnbook($barcode, $fbr);
270 $messages->{'WasReturned'} = $currentborrower;
273 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
274 if ($resfound and not $ignoreRs) {
275 $resrec->{'ResFound'} = $resfound;
276 $messages->{'ResFound'} = $resrec;
279 #actually do the transfer....
281 dotransfer($iteminformation->{'itemnumber'}, $fbr, $tbr);
282 $messages->{'WasTransfered'} = 1;
284 return ($dotransfer, $messages, $iteminformation);
288 my ($itm, $fbr, $tbr) = @_;
289 my $dbh = &C4Connect;
290 $itm = $dbh->quote($itm);
291 $fbr = $dbh->quote($fbr);
292 $tbr = $dbh->quote($tbr);
293 #new entry in branchtransfers....
294 my $query = "insert into branchtransfers (itemnumber, frombranch, datearrived, tobranch)
295 values($itm, $fbr, now(), $tbr)";
296 my $sth = $dbh->prepare($query);
299 #update holdingbranch in items .....
300 $query = "update items set datelastseen = now(), holdingbranch=$tbr where items.itemnumber=$itm";
301 $sth = $dbh->prepare($query);
310 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
312 my $iteminformation = getiteminformation($env, 0, $barcode);
314 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
317 if ($patroninformation->{'gonenoaddress'}) {
318 $rejected="Patron is gone, with no known address.";
321 if ($patroninformation->{'lost'}) {
322 $rejected="Patron's card has been reported lost.";
325 if ($patroninformation->{'debarred'}) {
326 $rejected="Patron is Debarred";
329 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
330 if ($amount > 5 && $patroninformation->{'categorycode'} ne 'L' &&
331 $patroninformation->{'categorycode'} ne 'W' &&
332 $patroninformation->{'categorycode'} ne 'I' &&
333 $patroninformation->{'categorycode'} ne 'B' &&
334 $patroninformation->{'categorycode'} ne 'P') {
335 $rejected = sprintf "Patron owes \$%.02f.", $amount;
338 unless ($iteminformation) {
339 $rejected = "$barcode is not a valid barcode.";
342 if ($iteminformation->{'notforloan'} == 1) {
343 $rejected="Reference item: not for loan.";
346 if ($iteminformation->{'wthdrawn'} == 1) {
347 $rejected="Item withdrawn.";
350 if ($iteminformation->{'restricted'} == 1) {
351 $rejected="Restricted item.";
354 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
355 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
356 # Already issued to current borrower
357 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
358 if ($renewstatus == 0) {
359 $rejected="No more renewals allowed for this item.";
362 if ($responses->{4} eq '') {
364 $question = "Book is issued to this borrower.\nRenew?";
365 $defaultanswer = 'Y';
367 } elsif ($responses->{4} eq 'Y') {
368 my $charge = calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
370 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
371 $iteminformation->{'charge'} = $charge;
373 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
374 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
381 } elsif ($currentborrower ne '') {
382 my ($currborrower, $cbflags) = getpatroninformation($env,$currentborrower,0);
383 if ($responses->{1} eq '') {
385 $question = "Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
388 } elsif ($responses->{1} eq 'Y') {
389 returnbook($iteminformation->{'barcode'}, $env->{'branch'});
396 my ($restype, $res) = CheckReserves($iteminformation->{'itemnumber'});
398 my $resbor = $res->{'borrowernumber'};
399 if ($resbor eq $patroninformation->{'borrowernumber'}) {
401 } elsif ($restype eq "Waiting") {
402 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
403 my $branches = getbranches();
404 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
405 if ($responses->{2} eq '') {
407 $question="<font color=red>Waiting</font> for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) at $branchname \nAllow issue?";
410 } elsif ($responses->{2} eq 'N') {
414 if ($responses->{3} eq '') {
416 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
419 } elsif ($responses->{3} eq 'Y') {
420 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
423 } elsif ($restype eq "Reserved") {
424 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
425 my $branches = getbranches();
426 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
427 if ($responses->{5} eq '') {
429 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $res->{'reservedate'} \nAllow issue?";
432 } elsif ($responses->{5} eq 'N') {
433 if ($responses->{6} eq '') {
435 $question="Set reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) to waiting and transfer to $branchname?";
437 } elsif ($responses->{6} eq 'Y') {
438 my $tobrcd = ReserveWaiting($res->{'itemnumber'}, $res->{'borrowernumber'});
439 transferbook($tobrcd, $barcode, 1);
440 $message = "Item should now be waiting at $branchname";
445 if ($responses->{7} eq '') {
447 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
450 } elsif ($responses->{7} eq 'Y') {
451 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
458 unless (($question) || ($rejected) || ($noissue)) {
460 if ($iteminformation->{'loanlength'}) {
461 $loanlength=$iteminformation->{'loanlength'};
464 my $datedue=time+($loanlength)*86400;
465 my @datearr = localtime($datedue);
466 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
467 if ($env->{'datedue'}) {
468 $dateduef=$env->{'datedue'};
470 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
471 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
474 $iteminformation->{'issues'}++;
475 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'},datelastseen=now() where itemnumber=$iteminformation->{'itemnumber'}");
478 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
480 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
481 $iteminformation->{'charge'}=$charge;
483 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
485 if ($iteminformation->{'charge'}) {
486 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
489 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
495 my ($barcode, $branch) = @_;
499 # get information on item
500 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
501 if (not $iteminformation) {
502 $messages->{'BadBarcode'} = $barcode;
506 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
507 if ((not $currentborrower) && $doreturn) {
508 $messages->{'NotIssued'} = $barcode;
511 # check if the book is in a permanent collection....
512 my $hbr = $iteminformation->{'homebranch'};
513 my $branches = getbranches();
514 if ($branches->{$hbr}->{'PE'}) {
515 $messages->{'IsPermanent'} = $hbr;
517 # check that the book has been cancelled
518 if ($iteminformation->{'wthdrawn'}) {
519 $messages->{'wthdrawn'} = 1;
522 # update issues, thereby returning book (should push this out into another subroutine
523 my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
525 doreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
526 $messages->{'WasReturned'};
528 ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
530 my ($transfered, $mess, $item) = transferbook($branch, $barcode);
532 $messages->{'WasTransfered'};
534 # fix up the accounts.....
535 if ($iteminformation->{'itemlost'}) {
536 updateitemlost($iteminformation->{'itemnumber'});
537 fixaccountforlostandreturned($iteminformation, $borrower);
538 $messages->{'WasLost'};
540 # fix up the overdues in accounts...
541 fixoverduesonreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
543 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
545 $resrec->{'ResFound'} = $resfound;
546 $messages->{'ResFound'} = $resrec;
549 UpdateStats(\%env, $branch ,'return','0','',$iteminformation->{'itemnumber'});
550 return ($doreturn, $messages, $iteminformation, $borrower);
555 my ($brn, $itm) = @_;
557 $brn = $dbh->quote($brn);
558 $itm = $dbh->quote($itm);
559 my $query = "update issues set returndate = now() where (borrowernumber = $brn)
560 and (itemnumber = $itm) and (returndate is null)";
561 my $sth = $dbh->prepare($query);
564 $query="update items set datelastseen=now() where itemnumber=$itm";
565 $sth=$dbh->prepare($query);
575 my $query="update items set itemlost=0 where itemnumber=$itemno";
576 my $sth=$dbh->prepare($query);
581 sub fixaccountforlostandreturned {
582 my ($iteminfo, $borrower) = @_;
585 my $itm = $dbh->quote($iteminfo->{'itemnumber'});
586 # check for charge made for lost book
587 my $query = "select * from accountlines where (itemnumber = $itm)
588 and (accounttype='L' or accounttype='Rep') order by date desc";
589 my $sth = $dbh->prepare($query);
591 if (my $data = $sth->fetchrow_hashref) {
592 # writeoff this amount
594 my $amount = $data->{'amount'};
595 my $acctno = $data->{'accountno'};
597 if ($data->{'amountoutstanding'} == $amount) {
598 $offset = $data->{'amount'};
601 $offset = $amount - $data->{'amountoutstanding'};
602 $amountleft = $data->{'amountoutstanding'} - $amount;
604 my $uquery = "update accountlines set accounttype = 'LR',amountoutstanding='0'
605 where (borrowernumber = '$data->{'borrowernumber'}')
606 and (itemnumber = $itm) and (accountno = '$acctno') ";
607 my $usth = $dbh->prepare($uquery);
610 #check if any credit is left if so writeoff other accounts
611 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
612 if ($amountleft < 0){
615 if ($amountleft > 0){
616 my $query = "select * from accountlines where (borrowernumber = '$data->{'borrowernumber'}')
617 and (amountoutstanding >0) order by date";
618 my $msth = $dbh->prepare($query);
620 # offset transactions
623 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
624 if ($accdata->{'amountoutstanding'} < $amountleft) {
626 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
628 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
631 my $thisacct = $accdata->{'accountno'};
632 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
633 where (borrowernumber = '$data->{'borrowernumber'}')
634 and (accountno='$thisacct')";
635 my $usth = $dbh->prepare($updquery);
638 $updquery = "insert into accountoffsets
639 (borrowernumber, accountno, offsetaccount, offsetamount)
641 ('$data->{'borrowernumber'}','$accdata->{'accountno'}','$nextaccntno','$newamtos')";
642 $usth = $dbh->prepare($updquery);
648 if ($amountleft > 0){
651 my $desc="Book Returned ".$iteminfo->{'barcode'};
652 $uquery = "insert into accountlines
653 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
654 values ('$data->{'borrowernumber'}','$nextaccntno',now(),0-$amount,'$desc',
656 $usth = $dbh->prepare($uquery);
659 $uquery = "insert into accountoffsets
660 (borrowernumber, accountno, offsetaccount, offsetamount)
661 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
662 $usth = $dbh->prepare($uquery);
665 $uquery = "update items set paidfor='' where itemnumber=$itm";
666 $usth = $dbh->prepare($uquery);
674 sub fixoverduesonreturn {
675 my ($brn, $itm) = @_;
677 $itm = $dbh->quote($itm);
678 $brn = $dbh->quote($brn);
679 # check for overdue fine
680 my $query = "select * from accountlines where (borrowernumber=$brn)
681 and (itemnumber = $itm) and (accounttype='FU' or accounttype='O')";
682 my $sth = $dbh->prepare($query);
684 # alter fine to show that the book has been returned
685 if (my $data = $sth->fetchrow_hashref) {
686 my $query = "update accountlines set accounttype='F' where (borrowernumber = $brn)
687 and (itemnumber = $itm) and (acccountno='$data->{'accountno'}')";
688 my $usth=$dbh->prepare($query);
697 # Original subroutine for Circ2.pm
699 my ($env, $patroninformation, $dbh) = @_;
700 my $amount = checkaccount($env, $patroninformation->{'borrowernumber'}, $dbh);
703 $flaginfo{'message'}= sprintf "Patron owes \$%.02f", $amount;
705 $flaginfo{'noissues'} = 1;
707 $flags{'CHARGES'} = \%flaginfo;
708 } elsif ($amount < 0){
710 $amount = $amount*-1;
711 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", $amount;
712 $flags{'CHARGES'} = \%flaginfo;
714 if ($patroninformation->{'gonenoaddress'} == 1) {
716 $flaginfo{'message'} = 'Borrower has no valid address.';
717 $flaginfo{'noissues'} = 1;
718 $flags{'GNA'} = \%flaginfo;
720 if ($patroninformation->{'lost'} == 1) {
722 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
723 $flaginfo{'noissues'} = 1;
724 $flags{'LOST'} = \%flaginfo;
726 if ($patroninformation->{'debarred'} == 1) {
728 $flaginfo{'message'} = 'Borrower is Debarred.';
729 $flaginfo{'noissues'} = 1;
730 $flags{'DBARRED'} = \%flaginfo;
732 if ($patroninformation->{'borrowernotes'}) {
734 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
735 $flags{'NOTES'} = \%flaginfo;
737 my ($odues, $itemsoverdue)
738 = checkoverdues($env, $patroninformation->{'borrowernumber'}, $dbh);
741 $flaginfo{'message'} = "Yes";
742 $flaginfo{'itemlist'} = $itemsoverdue;
743 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
744 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
746 $flags{'ODUES'} = \%flaginfo;
748 my ($nowaiting, $itemswaiting)
749 = CheckWaiting($patroninformation->{'borrowernumber'});
750 if ($nowaiting > 0) {
752 $flaginfo{'message'} = "Reserved items available";
753 $flaginfo{'itemlist'} = $itemswaiting;
754 $flags{'WAITING'} = \%flaginfo;
761 # From Main.pm, modified to return a list of overdueitems, in addition to a count
762 #checks whether a borrower has overdue items
763 my ($env, $bornum, $dbh)=@_;
764 my @datearr = localtime;
765 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
768 my $query = "SELECT * FROM issues,biblio,biblioitems,items
769 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
770 AND items.biblionumber = biblio.biblionumber
771 AND issues.itemnumber = items.itemnumber
772 AND issues.borrowernumber = $bornum
773 AND issues.returndate is NULL
774 AND issues.date_due < '$today'";
775 my $sth = $dbh->prepare($query);
777 while (my $data = $sth->fetchrow_hashref) {
778 push (@overdueitems, $data);
782 return ($count, \@overdueitems);
785 sub currentborrower {
786 # Original subroutine for Circ2.pm
787 my ($itemnumber) = @_;
788 my $dbh = &C4Connect;
789 my $q_itemnumber = $dbh->quote($itemnumber);
790 my $sth=$dbh->prepare("select borrowers.borrowernumber from
791 issues,borrowers where issues.itemnumber=$q_itemnumber and
792 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
795 my ($borrower) = $sth->fetchrow;
800 # Stolen from Main.pm
801 # Check for reserves for biblio
802 my ($env,$dbh,$itemnum)=@_;
804 my $query = "select * from reserves,items
805 where (items.itemnumber = '$itemnum')
806 and (reserves.cancellationdate is NULL)
807 and (items.biblionumber = reserves.biblionumber)
808 and ((reserves.found = 'W')
809 or (reserves.found is null))
811 my $sth = $dbh->prepare($query);
814 my $data=$sth->fetchrow_hashref;
815 while ($data && $resbor eq '') {
817 my $const = $data->{'constrainttype'};
819 $resbor = $data->{'borrowernumber'};
822 my $cquery = "select * from reserveconstraints,items
823 where (borrowernumber='$data->{'borrowernumber'}')
824 and reservedate='$data->{'reservedate'}'
825 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
826 and (items.itemnumber=$itemnum and
827 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
828 my $csth = $dbh->prepare($cquery);
830 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
832 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
834 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
838 $data=$sth->fetchrow_hashref;
841 return ($resbor,$resrec);
845 # New subroutine for Circ2.pm
846 my ($env, $borrower) = @_;
850 my $borrowernumber = $borrower->{'borrowernumber'};
852 if ($env->{'todaysissues'}) {
853 my @datearr = localtime(time());
854 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
855 $crit=" and issues.timestamp like '$today%' ";
857 if ($env->{'nottodaysissues'}) {
858 my @datearr = localtime(time());
859 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
860 $crit=" and !(issues.timestamp like '$today%') ";
862 my $select="select * from issues,items,biblioitems,biblio where
863 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
864 items.biblionumber=biblio.biblionumber and
865 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
866 $crit order by issues.timestamp desc";
868 my $sth=$dbh->prepare($select);
870 while (my $data = $sth->fetchrow_hashref) {
871 $data->{'dewey'}=~s/0*$//;
872 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
873 my @datearr = localtime(time());
874 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
875 +1)).sprintf ("%0.2d", $datearr[3]);
876 my $datedue=$data->{'date_due'};
878 if ($datedue < $todaysdate) {
879 $data->{'overdue'}=1;
881 my $itemnumber=$data->{'itemnumber'};
882 $currentissues{$counter}=$data;
887 return(\%currentissues);
890 # New subroutine for Circ2.pm
893 my $borrowernumber = $borrower->{'borrowernumber'};
894 my $brn =$dbh->quote($borrowernumber);
896 my $select = "select issues.timestamp, issues.date_due, items.biblionumber,
897 items.barcode, biblio.title, biblio.author, biblioitems.dewey,
899 from issues,items,biblioitems,biblio
900 where issues.borrowernumber = $brn
901 and issues.itemnumber = items.itemnumber
902 and items.biblionumber = biblio.biblionumber
903 and items.biblioitemnumber = biblioitems.biblioitemnumber
904 and issues.returndate is null
905 order by issues.date_due";
907 my $sth=$dbh->prepare($select);
910 while (my $data = $sth->fetchrow_hashref) {
911 $data->{'dewey'} =~ s/0*$//;
912 ($data->{'dewey'} == 0) && ($data->{'dewey'} = '');
913 my @datearr = localtime(time());
914 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
915 my $datedue = $data->{'date_due'};
917 if ($datedue < $todaysdate) {
918 $data->{'overdue'} = 1;
920 $currentissues{$counter} = $data;
925 return(\%currentissues);
930 # check for reserves waiting
931 my ($env,$dbh,$bornum)=@_;
933 my $query = "select * from reserves
934 where (borrowernumber = '$bornum')
935 and (reserves.found='W') and cancellationdate is NULL";
936 my $sth = $dbh->prepare($query);
939 if (my $data=$sth->fetchrow_hashref) {
940 $itemswaiting[$cnt] =$data;
944 return ($cnt,\@itemswaiting);
949 # Stolen from Accounts.pm
950 #take borrower number
951 #check accounts and list amounts owing
952 my ($env,$bornumber,$dbh,$date)=@_;
953 my $select="Select sum(amountoutstanding) from accountlines where
954 borrowernumber=$bornumber and amountoutstanding<>0";
956 $select.=" and date < '$date'";
959 my $sth=$dbh->prepare($select);
962 while (my $data=$sth->fetchrow_hashref){
963 $total=$total+$data->{'sum(amountoutstanding)'};
966 # output(1,2,"borrower owes $total");
968 # # output(1,2,"borrower owes $total");
970 # reconcileaccount($env,$dbh,$bornumber,$total);
978 # Stolen from Renewals.pm
979 # check renewal status
980 my ($env,$dbh,$bornum,$itemno)=@_;
983 my $q1 = "select * from issues
984 where (borrowernumber = '$bornum')
985 and (itemnumber = '$itemno')
986 and returndate is null";
987 my $sth1 = $dbh->prepare($q1);
989 if (my $data1 = $sth1->fetchrow_hashref) {
990 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
991 where (items.itemnumber = '$itemno')
992 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
993 and (biblioitems.itemtype = itemtypes.itemtype)";
994 my $sth2 = $dbh->prepare($q2);
996 if (my $data2=$sth2->fetchrow_hashref) {
997 $renews = $data2->{'renewalsallowed'};
999 if ($renews > $data1->{'renewals'}) {
1009 # Stolen from Renewals.pm
1010 # mark book as renewed
1011 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
1012 $datedue=$env->{'datedue'};
1013 if ($datedue eq "" ) {
1015 my $query= "Select * from biblioitems,items,itemtypes
1016 where (items.itemnumber = '$itemno')
1017 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1018 and (biblioitems.itemtype = itemtypes.itemtype)";
1019 my $sth=$dbh->prepare($query);
1021 if (my $data=$sth->fetchrow_hashref) {
1022 $loanlength = $data->{'loanlength'}
1026 my $datedu = time + ($loanlength * 86400);
1027 my @datearr = localtime($datedu);
1028 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
1030 my @date = split("-",$datedue);
1031 my $odatedue = ($date[2]+0)."-".($date[1]+0)."-".$date[0];
1032 my $issquery = "select * from issues where borrowernumber='$bornum' and
1033 itemnumber='$itemno' and returndate is null";
1034 my $sth=$dbh->prepare($issquery);
1036 my $issuedata=$sth->fetchrow_hashref;
1038 my $renews = $issuedata->{'renewals'} +1;
1039 my $updquery = "update issues
1040 set date_due = '$datedue', renewals = '$renews'
1041 where borrowernumber='$bornum' and
1042 itemnumber='$itemno' and returndate is null";
1043 $sth=$dbh->prepare($updquery);
1051 # Stolen from Issues.pm
1052 # calculate charges due
1053 my ($env, $dbh, $itemno, $bornum)=@_;
1058 # open (FILE,">>/tmp/charges");
1060 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
1061 where (items.itemnumber ='$itemno')
1062 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1063 and (biblioitems.itemtype = itemtypes.itemtype)";
1064 my $sth1= $dbh->prepare($q1);
1065 # print FILE "$q1\n";
1067 if (my $data1=$sth1->fetchrow_hashref) {
1068 $item_type = $data1->{'itemtype'};
1069 $charge = $data1->{'rentalcharge'};
1070 # print FILE "charge is $charge\n";
1071 my $q2 = "select rentaldiscount from borrowers,categoryitem
1072 where (borrowers.borrowernumber = '$bornum')
1073 and (borrowers.categorycode = categoryitem.categorycode)
1074 and (categoryitem.itemtype = '$item_type')";
1075 my $sth2=$dbh->prepare($q2);
1078 if (my $data2=$sth2->fetchrow_hashref) {
1079 my $discount = $data2->{'rentaldiscount'};
1080 # print FILE "discount is $discount";
1081 if ($discount eq 'NULL') {
1084 $charge = ($charge *(100 - $discount)) / 100;
1094 #Stolen from Issues.pm
1095 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
1096 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
1097 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
1098 my $sth = $dbh->prepare($query);
1105 # Stolen from Accounts.pm
1106 my ($env,$bornumber,$dbh)=@_;
1107 my $nextaccntno = 1;
1108 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
1109 my $sth = $dbh->prepare($query);
1111 if (my $accdata=$sth->fetchrow_hashref){
1112 $nextaccntno = $accdata->{'accountno'} + 1;
1115 return($nextaccntno);
1119 # Stolen from Returns.pm
1123 my ($itemdata) = getiteminformation(\%env, $itemno,0);
1124 my $bibno = $dbh->quote($itemdata->{'biblionumber'});
1125 my $bibitm = $dbh->quote($itemdata->{'biblioitemnumber'});
1126 my $query = "select * from reserves where ((found = 'W') or (found is null))
1127 and biblionumber = $bibno and cancellationdate is NULL
1128 order by priority, reservedate ";
1129 my $sth = $dbh->prepare($query);
1135 while (($resrec = $sth->fetchrow_hashref) && (not $resfound)) {
1137 my $brn = $dbh->quote($resrec->{'borrowernumber'});
1138 my $rdate = $dbh->quote($resrec->{'reservedate'});
1139 my $bibno = $dbh->quote($resrec->{'biblionumber'});
1140 if ($resrec->{'found'} eq "W") {
1141 if ($resrec->{'itemnumber'} eq $itemno) {
1145 if ($resrec->{'constrainttype'} eq "a") {
1148 my $conquery = "select * from reserveconstraints where borrowernumber = $brn
1149 and reservedate = $rdate and biblionumber = $bibno and biblioitemnumber = $bibitm";
1150 my $consth = $dbh->prepare($conquery);
1152 if (my $conrec = $consth->fetchrow_hashref) {
1153 if ($resrec->{'constrainttype'} eq "o") {
1161 my $updquery = "update reserves set found = 'W', itemnumber = '$itemno'
1162 where borrowernumber = $brn and reservedate = $rdate and biblionumber = $bibno";
1163 my $updsth = $dbh->prepare($updquery);
1169 return ($resfound,$lastrec);
1172 END { } # module clean-up code here (global destructor)