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'};
210 return($iteminformation);
214 # returns an array of borrower hash references, given a cardnumber or a partial
216 my ($env, $key) = @_;
219 my $q_key=$dbh->quote($key);
220 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
223 my ($borrower)=$sth->fetchrow_hashref;
224 push (@borrowers, $borrower);
226 $q_key=$dbh->quote("$key%");
228 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
230 while (my $borrower = $sth->fetchrow_hashref) {
231 push (@borrowers, $borrower);
241 # transfer book code....
242 my ($tbr, $barcode, $ignoreRs) = @_;
246 my $branches = getbranches();
247 my $iteminformation = getiteminformation(\%env, 0, $barcode);
249 if (not $iteminformation) {
250 $messages->{'BadBarcode'} = $barcode;
253 # get branches of book...
254 my $hbr = $iteminformation->{'homebranch'};
255 my $fbr = $iteminformation->{'holdingbranch'};
257 if ($branches->{$hbr}->{'PE'}) {
258 $messages->{'IsPermanent'} = $hbr;
260 # cant transfer book if is already there....
262 $messages->{'DestinationEqualsHolding'} = 1;
265 # check if it is still issued to someone, return it...
266 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
267 if ($currentborrower) {
268 returnbook($barcode, $fbr);
269 $messages->{'WasReturned'} = $currentborrower;
272 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
273 if ($resfound and not $ignoreRs) {
274 $resrec->{'ResFound'} = $resfound;
275 $messages->{'ResFound'} = $resrec;
278 #actually do the transfer....
280 dotransfer($iteminformation->{'itemnumber'}, $fbr, $tbr);
281 $messages->{'WasTransfered'} = 1;
283 return ($dotransfer, $messages, $iteminformation);
287 my ($itm, $fbr, $tbr) = @_;
288 my $dbh = &C4Connect;
289 $itm = $dbh->quote($itm);
290 $fbr = $dbh->quote($fbr);
291 $tbr = $dbh->quote($tbr);
292 #new entry in branchtransfers....
293 my $query = "insert into branchtransfers (itemnumber, frombranch, datearrived, tobranch)
294 values($itm, $fbr, now(), $tbr)";
295 my $sth = $dbh->prepare($query);
298 #update holdingbranch in items .....
299 $query = "update items set datelastseen = now(), holdingbranch=$tbr where items.itemnumber=$itm";
300 $sth = $dbh->prepare($query);
309 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
311 my $iteminformation = getiteminformation($env, 0, $barcode);
313 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
316 if ($patroninformation->{'gonenoaddress'}) {
317 $rejected="Patron is gone, with no known address.";
320 if ($patroninformation->{'lost'}) {
321 $rejected="Patron's card has been reported lost.";
324 if ($patroninformation->{'debarred'}) {
325 $rejected="Patron is Debarred";
328 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
329 if ($amount > 5 && $patroninformation->{'categorycode'} ne 'L' &&
330 $patroninformation->{'categorycode'} ne 'W' &&
331 $patroninformation->{'categorycode'} ne 'I' &&
332 $patroninformation->{'categorycode'} ne 'B' &&
333 $patroninformation->{'categorycode'} ne 'P') {
334 $rejected = sprintf "Patron owes \$%.02f.", $amount;
337 unless ($iteminformation) {
338 $rejected = "$barcode is not a valid barcode.";
341 if ($iteminformation->{'notforloan'} == 1) {
342 $rejected="Item not for loan.";
345 if ($iteminformation->{'wthdrawn'} == 1) {
346 $rejected="Item withdrawn.";
349 if ($iteminformation->{'restricted'} == 1) {
350 $rejected="Restricted item.";
353 if ($iteminformation->{'itemtype'} eq 'REF') {
354 $rejected="Reference item: Not for loan.";
357 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
358 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
359 # Already issued to current borrower
360 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
361 if ($renewstatus == 0) {
362 $rejected="No more renewals allowed for this item.";
365 if ($responses->{4} eq '') {
367 $question = "Book is issued to this borrower.\nRenew?";
368 $defaultanswer = 'Y';
370 } elsif ($responses->{4} eq 'Y') {
371 my $charge = calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
373 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
374 $iteminformation->{'charge'} = $charge;
376 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
377 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
384 } elsif ($currentborrower ne '') {
385 my ($currborrower, $cbflags) = getpatroninformation($env,$currentborrower,0);
386 if ($responses->{1} eq '') {
388 $question = "Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
391 } elsif ($responses->{1} eq 'Y') {
392 returnbook($iteminformation->{'barcode'}, $env->{'branch'});
399 my ($restype, $res) = CheckReserves($iteminformation->{'itemnumber'});
401 my $resbor = $res->{'borrowernumber'};
402 if ($resbor eq $patroninformation->{'borrowernumber'}) {
404 } elsif ($restype eq "Waiting") {
405 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
406 my $branches = getbranches();
407 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
408 if ($responses->{2} eq '') {
410 $question="Waiting for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) at $branchname \nAllow issue?";
413 } elsif ($responses->{2} eq 'N') {
417 if ($responses->{3} eq '') {
419 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
422 } elsif ($responses->{3} eq 'Y') {
423 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
426 } elsif ($restype eq "Reserved") {
427 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
428 my $branches = getbranches();
429 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
430 if ($responses->{5} eq '') {
432 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $res->{'reservedate'} \nAllow issue?";
435 } elsif ($responses->{5} eq 'N') {
436 if ($responses->{6} eq '') {
438 $question="Set reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) to waiting and transfer to $branchname?";
440 } elsif ($responses->{6} eq 'Y') {
441 my $tobrcd = ReserveWaiting($res->{'itemnumber'}, $res->{'borrowernumber'});
442 transferbook($tobrcd, $barcode, 1);
443 $message = "Item should now be waiting at $branchname";
448 if ($responses->{7} eq '') {
450 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
453 } elsif ($responses->{7} eq 'Y') {
454 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
461 unless (($question) || ($rejected) || ($noissue)) {
463 if ($iteminformation->{'loanlength'}) {
464 $loanlength=$iteminformation->{'loanlength'};
467 my $datedue=time+($loanlength)*86400;
468 my @datearr = localtime($datedue);
469 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
470 if ($env->{'datedue'}) {
471 $dateduef=$env->{'datedue'};
473 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
474 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
477 $iteminformation->{'issues'}++;
478 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'},datelastseen=now() where itemnumber=$iteminformation->{'itemnumber'}");
481 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
483 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
484 $iteminformation->{'charge'}=$charge;
486 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
488 if ($iteminformation->{'charge'}) {
489 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
492 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
498 my ($barcode, $branch) = @_;
502 # get information on item
503 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
504 if (not $iteminformation) {
505 $messages->{'BadBarcode'} = $barcode;
509 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
510 if ((not $currentborrower) && $doreturn) {
511 $messages->{'NotIssued'} = $barcode;
514 # check if the book is in a permanent collection....
515 my $hbr = $iteminformation->{'homebranch'};
516 my $branches = getbranches();
517 if ($branches->{$hbr}->{'PE'}) {
518 $messages->{'IsPermanent'} = $hbr;
520 # check that the book has been cancelled
521 if ($iteminformation->{'wthdrawn'}) {
522 $messages->{'wthdrawn'} = 1;
525 # update issues, thereby returning book (should push this out into another subroutine
526 my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
528 doreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
529 $messages->{'WasReturned'};
531 ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
533 my ($transfered, $mess, $item) = transferbook($branch, $barcode);
535 $messages->{'WasTransfered'};
537 # fix up the accounts.....
538 if ($iteminformation->{'itemlost'}) {
539 updateitemlost($iteminformation->{'itemnumber'});
540 fixaccountforlostandreturned($iteminformation, $borrower);
541 $messages->{'WasLost'};
543 # fix up the overdues in accounts...
544 fixoverduesonreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
546 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
548 $resrec->{'ResFound'} = $resfound;
549 $messages->{'ResFound'} = $resrec;
552 UpdateStats(\%env, $branch ,'return','0','',$iteminformation->{'itemnumber'});
553 return ($doreturn, $messages, $iteminformation, $borrower);
558 my ($brn, $itm) = @_;
560 $brn = $dbh->quote($brn);
561 $itm = $dbh->quote($itm);
562 my $query = "update issues set returndate = now() where (borrowernumber = $brn)
563 and (itemnumber = $itm) and (returndate is null)";
564 my $sth = $dbh->prepare($query);
567 $query="update items set datelastseen=now() where itemnumber=$itm";
568 $sth=$dbh->prepare($query);
578 my $query="update items set itemlost=0 where itemnumber=$itemno";
579 my $sth=$dbh->prepare($query);
584 sub fixaccountforlostandreturned {
585 my ($iteminfo, $borrower) = @_;
588 my $itm = $dbh->quote($iteminfo->{'itemnumber'});
589 # check for charge made for lost book
590 my $query = "select * from accountlines where (itemnumber = $itm)
591 and (accounttype='L' or accounttype='Rep') order by date desc";
592 my $sth = $dbh->prepare($query);
594 if (my $data = $sth->fetchrow_hashref) {
595 # writeoff this amount
597 my $amount = $data->{'amount'};
598 my $acctno = $data->{'accountno'};
600 if ($data->{'amountoutstanding'} == $amount) {
601 $offset = $data->{'amount'};
604 $offset = $amount - $data->{'amountoutstanding'};
605 $amountleft = $data->{'amountoutstanding'} - $amount;
607 my $uquery = "update accountlines set accounttype = 'LR',amountoutstanding='0'
608 where (borrowernumber = '$data->{'borrowernumber'}')
609 and (itemnumber = $itm) and (accountno = '$acctno') ";
610 my $usth = $dbh->prepare($uquery);
613 #check if any credit is left if so writeoff other accounts
614 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
615 if ($amountleft < 0){
618 if ($amountleft > 0){
619 my $query = "select * from accountlines where (borrowernumber = '$data->{'borrowernumber'}')
620 and (amountoutstanding >0) order by date";
621 my $msth = $dbh->prepare($query);
623 # offset transactions
626 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
627 if ($accdata->{'amountoutstanding'} < $amountleft) {
629 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
631 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
634 my $thisacct = $accdata->{'accountno'};
635 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
636 where (borrowernumber = '$data->{'borrowernumber'}')
637 and (accountno='$thisacct')";
638 my $usth = $dbh->prepare($updquery);
641 $updquery = "insert into accountoffsets
642 (borrowernumber, accountno, offsetaccount, offsetamount)
644 ('$data->{'borrowernumber'}','$accdata->{'accountno'}','$nextaccntno','$newamtos')";
645 $usth = $dbh->prepare($updquery);
651 if ($amountleft > 0){
654 my $desc="Book Returned ".$iteminfo->{'barcode'};
655 $uquery = "insert into accountlines
656 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
657 values ('$data->{'borrowernumber'}','$nextaccntno',now(),0-$amount,'$desc',
659 $usth = $dbh->prepare($uquery);
662 $uquery = "insert into accountoffsets
663 (borrowernumber, accountno, offsetaccount, offsetamount)
664 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
665 $usth = $dbh->prepare($uquery);
668 $uquery = "update items set paidfor='' where itemnumber=$itm";
669 $usth = $dbh->prepare($uquery);
677 sub fixoverduesonreturn {
678 my ($brn, $itm) = @_;
680 $itm = $dbh->quote($itm);
681 $brn = $dbh->quote($brn);
682 # check for overdue fine
683 my $query = "select * from accountlines where (borrowernumber=$brn)
684 and (itemnumber = $itm) and (accounttype='FU' or accounttype='O')";
685 my $sth = $dbh->prepare($query);
687 # alter fine to show that the book has been returned
688 if (my $data = $sth->fetchrow_hashref) {
689 my $query = "update accountlines set accounttype='F' where (borrowernumber = $brn)
690 and (itemnumber = $itm) and (acccountno='$data->{'accountno'}')";
691 my $usth=$dbh->prepare($query);
700 # Original subroutine for Circ2.pm
702 my ($env, $patroninformation, $dbh) = @_;
703 my $amount = checkaccount($env, $patroninformation->{'borrowernumber'}, $dbh);
706 $flaginfo{'message'}= sprintf "Patron owes \$%.02f", $amount;
708 $flaginfo{'noissues'} = 1;
710 $flags{'CHARGES'} = \%flaginfo;
711 } elsif ($amount < 0){
713 $amount = $amount*-1;
714 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", $amount;
715 $flags{'CHARGES'} = \%flaginfo;
717 if ($patroninformation->{'gonenoaddress'} == 1) {
719 $flaginfo{'message'} = 'Borrower has no valid address.';
720 $flaginfo{'noissues'} = 1;
721 $flags{'GNA'} = \%flaginfo;
723 if ($patroninformation->{'lost'} == 1) {
725 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
726 $flaginfo{'noissues'} = 1;
727 $flags{'LOST'} = \%flaginfo;
729 if ($patroninformation->{'debarred'} == 1) {
731 $flaginfo{'message'} = 'Borrower is Debarred.';
732 $flaginfo{'noissues'} = 1;
733 $flags{'DBARRED'} = \%flaginfo;
735 if ($patroninformation->{'borrowernotes'}) {
737 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
738 $flags{'NOTES'} = \%flaginfo;
740 my ($odues, $itemsoverdue)
741 = checkoverdues($env, $patroninformation->{'borrowernumber'}, $dbh);
744 $flaginfo{'message'} = "Yes";
745 $flaginfo{'itemlist'} = $itemsoverdue;
746 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
747 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
749 $flags{'ODUES'} = \%flaginfo;
751 my ($nowaiting, $itemswaiting)
752 = CheckWaiting($patroninformation->{'borrowernumber'});
753 if ($nowaiting > 0) {
755 $flaginfo{'message'} = "Reserved items available";
756 $flaginfo{'itemlist'} = $itemswaiting;
757 $flags{'WAITING'} = \%flaginfo;
764 # From Main.pm, modified to return a list of overdueitems, in addition to a count
765 #checks whether a borrower has overdue items
766 my ($env, $bornum, $dbh)=@_;
767 my @datearr = localtime;
768 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
771 my $query = "SELECT * FROM issues,biblio,biblioitems,items
772 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
773 AND items.biblionumber = biblio.biblionumber
774 AND issues.itemnumber = items.itemnumber
775 AND issues.borrowernumber = $bornum
776 AND issues.returndate is NULL
777 AND issues.date_due < '$today'";
778 my $sth = $dbh->prepare($query);
780 while (my $data = $sth->fetchrow_hashref) {
781 push (@overdueitems, $data);
785 return ($count, \@overdueitems);
788 sub currentborrower {
789 # Original subroutine for Circ2.pm
790 my ($itemnumber) = @_;
791 my $dbh = &C4Connect;
792 my $q_itemnumber = $dbh->quote($itemnumber);
793 my $sth=$dbh->prepare("select borrowers.borrowernumber from
794 issues,borrowers where issues.itemnumber=$q_itemnumber and
795 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
798 my ($borrower) = $sth->fetchrow;
803 # Stolen from Main.pm
804 # Check for reserves for biblio
805 my ($env,$dbh,$itemnum)=@_;
807 my $query = "select * from reserves,items
808 where (items.itemnumber = '$itemnum')
809 and (reserves.cancellationdate is NULL)
810 and (items.biblionumber = reserves.biblionumber)
811 and ((reserves.found = 'W')
812 or (reserves.found is null))
814 my $sth = $dbh->prepare($query);
817 my $data=$sth->fetchrow_hashref;
818 while ($data && $resbor eq '') {
820 my $const = $data->{'constrainttype'};
822 $resbor = $data->{'borrowernumber'};
825 my $cquery = "select * from reserveconstraints,items
826 where (borrowernumber='$data->{'borrowernumber'}')
827 and reservedate='$data->{'reservedate'}'
828 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
829 and (items.itemnumber=$itemnum and
830 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
831 my $csth = $dbh->prepare($cquery);
833 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
835 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
837 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
841 $data=$sth->fetchrow_hashref;
844 return ($resbor,$resrec);
848 # New subroutine for Circ2.pm
849 my ($env, $borrower) = @_;
853 my $borrowernumber = $borrower->{'borrowernumber'};
855 if ($env->{'todaysissues'}) {
856 my @datearr = localtime(time());
857 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
858 $crit=" and issues.timestamp like '$today%' ";
860 if ($env->{'nottodaysissues'}) {
861 my @datearr = localtime(time());
862 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
863 $crit=" and !(issues.timestamp like '$today%') ";
865 my $select="select * from issues,items,biblioitems,biblio where
866 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
867 items.biblionumber=biblio.biblionumber and
868 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
869 $crit order by issues.timestamp desc";
871 my $sth=$dbh->prepare($select);
873 while (my $data = $sth->fetchrow_hashref) {
874 $data->{'dewey'}=~s/0*$//;
875 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
876 my @datearr = localtime(time());
877 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
878 +1)).sprintf ("%0.2d", $datearr[3]);
879 my $datedue=$data->{'date_due'};
881 if ($datedue < $todaysdate) {
882 $data->{'overdue'}=1;
884 my $itemnumber=$data->{'itemnumber'};
885 $currentissues{$counter}=$data;
890 return(\%currentissues);
893 # New subroutine for Circ2.pm
896 my $borrowernumber = $borrower->{'borrowernumber'};
897 my $brn =$dbh->quote($borrowernumber);
899 my $select = "select issues.timestamp, issues.date_due, items.biblionumber,
900 items.barcode, biblio.title, biblio.author, biblioitems.dewey,
902 from issues,items,biblioitems,biblio
903 where issues.borrowernumber = $brn
904 and issues.itemnumber = items.itemnumber
905 and items.biblionumber = biblio.biblionumber
906 and items.biblioitemnumber = biblioitems.biblioitemnumber
907 and issues.returndate is null
908 order by issues.date_due";
910 my $sth=$dbh->prepare($select);
913 while (my $data = $sth->fetchrow_hashref) {
914 $data->{'dewey'} =~ s/0*$//;
915 ($data->{'dewey'} == 0) && ($data->{'dewey'} = '');
916 my @datearr = localtime(time());
917 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
918 my $datedue = $data->{'date_due'};
920 if ($datedue < $todaysdate) {
921 $data->{'overdue'} = 1;
923 $currentissues{$counter} = $data;
928 return(\%currentissues);
933 # check for reserves waiting
934 my ($env,$dbh,$bornum)=@_;
936 my $query = "select * from reserves
937 where (borrowernumber = '$bornum')
938 and (reserves.found='W') and cancellationdate is NULL";
939 my $sth = $dbh->prepare($query);
942 if (my $data=$sth->fetchrow_hashref) {
943 $itemswaiting[$cnt] =$data;
947 return ($cnt,\@itemswaiting);
952 # Stolen from Accounts.pm
953 #take borrower number
954 #check accounts and list amounts owing
955 my ($env,$bornumber,$dbh,$date)=@_;
956 my $select="Select sum(amountoutstanding) from accountlines where
957 borrowernumber=$bornumber and amountoutstanding<>0";
959 $select.=" and date < '$date'";
962 my $sth=$dbh->prepare($select);
965 while (my $data=$sth->fetchrow_hashref){
966 $total=$total+$data->{'sum(amountoutstanding)'};
969 # output(1,2,"borrower owes $total");
971 # # output(1,2,"borrower owes $total");
973 # reconcileaccount($env,$dbh,$bornumber,$total);
981 # Stolen from Renewals.pm
982 # check renewal status
983 my ($env,$dbh,$bornum,$itemno)=@_;
986 my $q1 = "select * from issues
987 where (borrowernumber = '$bornum')
988 and (itemnumber = '$itemno')
989 and returndate is null";
990 my $sth1 = $dbh->prepare($q1);
992 if (my $data1 = $sth1->fetchrow_hashref) {
993 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
994 where (items.itemnumber = '$itemno')
995 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
996 and (biblioitems.itemtype = itemtypes.itemtype)";
997 my $sth2 = $dbh->prepare($q2);
999 if (my $data2=$sth2->fetchrow_hashref) {
1000 $renews = $data2->{'renewalsallowed'};
1002 if ($renews > $data1->{'renewals'}) {
1012 # Stolen from Renewals.pm
1013 # mark book as renewed
1014 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
1015 $datedue=$env->{'datedue'};
1016 if ($datedue eq "" ) {
1018 my $query= "Select * from biblioitems,items,itemtypes
1019 where (items.itemnumber = '$itemno')
1020 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1021 and (biblioitems.itemtype = itemtypes.itemtype)";
1022 my $sth=$dbh->prepare($query);
1024 if (my $data=$sth->fetchrow_hashref) {
1025 $loanlength = $data->{'loanlength'}
1029 my $datedu = time + ($loanlength * 86400);
1030 my @datearr = localtime($datedu);
1031 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
1033 my @date = split("-",$datedue);
1034 my $odatedue = ($date[2]+0)."-".($date[1]+0)."-".$date[0];
1035 my $issquery = "select * from issues where borrowernumber='$bornum' and
1036 itemnumber='$itemno' and returndate is null";
1037 my $sth=$dbh->prepare($issquery);
1039 my $issuedata=$sth->fetchrow_hashref;
1041 my $renews = $issuedata->{'renewals'} +1;
1042 my $updquery = "update issues
1043 set date_due = '$datedue', renewals = '$renews'
1044 where borrowernumber='$bornum' and
1045 itemnumber='$itemno' and returndate is null";
1046 $sth=$dbh->prepare($updquery);
1054 # Stolen from Issues.pm
1055 # calculate charges due
1056 my ($env, $dbh, $itemno, $bornum)=@_;
1061 # open (FILE,">>/tmp/charges");
1063 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
1064 where (items.itemnumber ='$itemno')
1065 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1066 and (biblioitems.itemtype = itemtypes.itemtype)";
1067 my $sth1= $dbh->prepare($q1);
1068 # print FILE "$q1\n";
1070 if (my $data1=$sth1->fetchrow_hashref) {
1071 $item_type = $data1->{'itemtype'};
1072 $charge = $data1->{'rentalcharge'};
1073 # print FILE "charge is $charge\n";
1074 my $q2 = "select rentaldiscount from borrowers,categoryitem
1075 where (borrowers.borrowernumber = '$bornum')
1076 and (borrowers.categorycode = categoryitem.categorycode)
1077 and (categoryitem.itemtype = '$item_type')";
1078 my $sth2=$dbh->prepare($q2);
1081 if (my $data2=$sth2->fetchrow_hashref) {
1082 my $discount = $data2->{'rentaldiscount'};
1083 # print FILE "discount is $discount";
1084 if ($discount eq 'NULL') {
1087 $charge = ($charge *(100 - $discount)) / 100;
1097 #Stolen from Issues.pm
1098 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
1099 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
1100 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
1101 my $sth = $dbh->prepare($query);
1108 # Stolen from Accounts.pm
1109 my ($env,$bornumber,$dbh)=@_;
1110 my $nextaccntno = 1;
1111 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
1112 my $sth = $dbh->prepare($query);
1114 if (my $accdata=$sth->fetchrow_hashref){
1115 $nextaccntno = $accdata->{'accountno'} + 1;
1118 return($nextaccntno);
1122 # Stolen from Returns.pm
1126 my ($itemdata) = getiteminformation(\%env, $itemno,0);
1127 my $bibno = $dbh->quote($itemdata->{'biblionumber'});
1128 my $bibitm = $dbh->quote($itemdata->{'biblioitemnumber'});
1129 my $query = "select * from reserves where ((found = 'W') or (found is null))
1130 and biblionumber = $bibno and cancellationdate is NULL
1131 order by priority, reservedate ";
1132 my $sth = $dbh->prepare($query);
1138 while (($resrec = $sth->fetchrow_hashref) && (not $resfound)) {
1140 my $brn = $dbh->quote($resrec->{'borrowernumber'});
1141 my $rdate = $dbh->quote($resrec->{'reservedate'});
1142 my $bibno = $dbh->quote($resrec->{'biblionumber'});
1143 if ($resrec->{'found'} eq "W") {
1144 if ($resrec->{'itemnumber'} eq $itemno) {
1148 if ($resrec->{'constrainttype'} eq "a") {
1151 my $conquery = "select * from reserveconstraints where borrowernumber = $brn
1152 and reservedate = $rdate and biblionumber = $bibno and biblioitemnumber = $bibitm";
1153 my $consth = $dbh->prepare($conquery);
1155 if (my $conrec = $consth->fetchrow_hashref) {
1156 if ($resrec->{'constrainttype'} eq "o") {
1164 my $updquery = "update reserves set found = 'W', itemnumber = '$itemno'
1165 where borrowernumber = $brn and reservedate = $rdate and biblionumber = $bibno";
1166 my $updsth = $dbh->prepare($updquery);
1172 return ($resfound,$lastrec);
1175 END { } # module clean-up code here (global destructor)