1 package C4::Circulation;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
39 use POSIX qw(strftime);
40 use C4::Branch; # GetBranches
41 use C4::Log; # logaction
45 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
47 # set the version for version checking
52 C4::Circulation - Koha circulation module
60 The functions in this module deal with circulation, issues, and
61 returns, as well as general information about the library.
62 Also deals with stocktaking.
70 # FIXME subs that should probably be elsewhere
76 # subs to deal with issuing a book
88 &AnonymiseIssueHistory
90 # subs to deal with returns
95 # subs to deal with transfers
104 # FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
105 # FIXME From Paul : i don't understand what this sub does & why it has to be called on every circ. Speak of this with chris maybe ?
109 =head3 $str = &decode($chunk);
113 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
120 sub cuecatbarcodedecode {
123 my @fields = split( /\./, $barcode );
124 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
125 if ( $#results == 2 ) {
135 =head3 $str = &decode($chunk);
139 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
149 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
150 my @s = map { index( $seq, $_ ); } split( //, $encoded );
151 my $l = ( $#s + 1 ) % 4;
162 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
164 chr( ( $n >> 16 ) ^ 67 )
165 .chr( ( $n >> 8 & 255 ) ^ 67 )
166 .chr( ( $n & 255 ) ^ 67 );
169 $r = substr( $r, 0, length($r) - $l );
175 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch, $barcode, $ignore_reserves);
177 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
179 C<$newbranch> is the code for the branch to which the item should be transferred.
181 C<$barcode> is the barcode of the item to be transferred.
183 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
184 Otherwise, if an item is reserved, the transfer fails.
186 Returns three values:
190 is true if the transfer was successful.
194 is a reference-to-hash which may have any of the following keys:
200 There is no item in the catalog with the given barcode. The value is C<$barcode>.
204 The item's home branch is permanent. This doesn't prevent the item from being transferred, though. The value is the code of the item's home branch.
206 =item C<DestinationEqualsHolding>
208 The item is already at the branch to which it is being transferred. The transfer is nonetheless considered to have failed. The value should be ignored.
212 The item was on loan, and C<&transferbook> automatically returned it before transferring it. The value is the borrower number of the patron who had the item.
216 The item was reserved. The value is a reference-to-hash whose keys are fields from the reserves table of the Koha database, and C<biblioitemnumber>. It also has the key C<ResFound>, whose value is either C<Waiting> or C<Reserved>.
218 =item C<WasTransferred>
220 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
227 my ( $tbr, $barcode, $ignoreRs ) = @_;
230 my $branches = GetBranches();
231 my $itemnumber = GetItemnumberFromBarcode( $barcode );
232 my $issue = GetItemIssue($itemnumber);
233 my $biblio = GetBiblioFromItemNumber($itemnumber);
236 if ( not $itemnumber ) {
237 $messages->{'BadBarcode'} = $barcode;
241 # get branches of book...
242 my $hbr = $biblio->{'homebranch'};
243 my $fbr = $biblio->{'holdingbranch'};
246 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
247 $messages->{'IsPermanent'} = $hbr;
250 # can't transfer book if is already there....
251 if ( $fbr eq $tbr ) {
252 $messages->{'DestinationEqualsHolding'} = 1;
256 # check if it is still issued to someone, return it...
257 if ($issue->{borrowernumber}) {
258 AddReturn( $barcode, $fbr );
259 $messages->{'WasReturned'} = $issue->{borrowernumber};
263 # That'll save a database query.
264 my ( $resfound, $resrec ) =
265 CheckReserves( $itemnumber );
266 if ( $resfound and not $ignoreRs ) {
267 $resrec->{'ResFound'} = $resfound;
269 # $messages->{'ResFound'} = $resrec;
273 #actually do the transfer....
275 ModItemTransfer( $itemnumber, $fbr, $tbr );
277 # don't need to update MARC anymore, we do it in batch now
278 $messages->{'WasTransfered'} = 1;
279 ModDateLastSeen( $itemnumber );
281 return ( $dotransfer, $messages, $biblio );
284 =head2 CanBookBeIssued
286 Check if a book can be issued.
288 my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued($borrower,$barcode,$year,$month,$day);
292 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
294 =item C<$barcode> is the bar code of the book being issued.
296 =item C<$year> C<$month> C<$day> contains the date of the return (in case it's forced by "stickyduedate".
304 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
305 Possible values are :
311 sticky due date is invalid
315 borrower gone with no address
319 borrower declared it's card lost
325 =head3 UNKNOWN_BARCODE
339 item is restricted (set by ??)
341 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
342 Possible values are :
350 renewing, not issuing
352 =head3 ISSUED_TO_ANOTHER
354 issued to someone else.
358 reserved for someone else.
362 sticky due date is invalid
366 if the borrower borrows to much things
370 # check if a book can be issued.
374 my $borrower = shift;
375 my $biblionumber = shift;
377 my $cat_borrower = $borrower->{'categorycode'};
378 my $dbh = C4::Context->dbh;
380 # Get which branchcode we need
381 if (C4::Context->preference('CircControl') eq 'PickupLibary'){
382 $branch = C4::Context->userenv->{'branchcode'};
384 elsif (C4::Context->preference('CircControl') eq 'PatronLibary'){
385 $branch = $borrower->{'branchcode'};
389 $branch = $item->{'homebranch'};
391 my $type = (C4::Context->preference('item-level_itypes'))
392 ? $item->{'itype'} # item-level
393 : $item->{'itemtype'}; # biblio-level
397 'SELECT * FROM issuingrules
398 WHERE categorycode = ?
403 my $query2 = "SELECT COUNT(*) FROM issues i, biblioitems s1, items s2
404 WHERE i.borrowernumber = ?
405 AND i.returndate IS NULL
406 AND i.itemnumber = s2.itemnumber
407 AND s1.biblioitemnumber = s2.biblioitemnumber";
408 if (C4::Context->preference('item-level_itypes')){
409 $query2.=" AND s2.itype=? ";
411 $query2.=" AND s1.itemtype= ? ";
413 my $sth2= $dbh->prepare($query2);
416 'SELECT COUNT(*) FROM issues
417 WHERE borrowernumber = ?
418 AND returndate IS NULL'
422 # check the 3 parameters (branch / itemtype / category code
423 $sth->execute( $cat_borrower, $type, $branch );
424 my $result = $sth->fetchrow_hashref;
425 # warn "$cat_borrower, $type, $branch = ".Data::Dumper::Dumper($result);
427 if ( $result->{maxissueqty} ne '' ) {
428 # warn "checking on everything set";
429 $sth2->execute( $borrower->{'borrowernumber'}, $type );
430 my $alreadyissued = $sth2->fetchrow;
431 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
432 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch/category/itemtype failed)" );
434 # now checking for total
435 $sth->execute( $cat_borrower, '', $branch );
436 my $result = $sth->fetchrow_hashref;
437 if ( $result->{maxissueqty} ne '*' ) {
438 $sth2->execute( $borrower->{'borrowernumber'}, $type );
439 my $alreadyissued = $sth2->fetchrow;
440 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
441 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch/category/total failed)" );
446 # check the 2 parameters (branch / itemtype / default categorycode
447 $sth->execute( '*', $type, $branch );
448 my $result = $sth->fetchrow_hashref;
449 # warn "*, $type, $branch = ".Data::Dumper::Dumper($result);
451 if ( $result->{maxissueqty} ne '' ) {
452 # warn "checking on 2 parameters (default categorycode)";
453 $sth2->execute( $borrower->{'borrowernumber'}, $type );
454 my $alreadyissued = $sth2->fetchrow;
455 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
456 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch / default category / itemtype failed)" );
458 # now checking for total
459 $sth->execute( '*', '*', $branch );
460 my $result = $sth->fetchrow_hashref;
461 if ( $result->{maxissueqty} ne '' ) {
462 $sth2->execute( $borrower->{'borrowernumber'}, $type );
463 my $alreadyissued = $sth2->fetchrow;
464 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
465 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch / default category / total failed)" );
470 # check the 1 parameters (default branch / itemtype / categorycode
471 $sth->execute( $cat_borrower, $type, '*' );
472 my $result = $sth->fetchrow_hashref;
473 # warn "$cat_borrower, $type, * = ".Data::Dumper::Dumper($result);
475 if ( $result->{maxissueqty} ne '' ) {
476 # warn "checking on 1 parameter (default branch + categorycode)";
477 $sth2->execute( $borrower->{'borrowernumber'}, $type );
478 my $alreadyissued = $sth2->fetchrow;
479 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
480 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch/category/itemtype failed)" );
482 # now checking for total
483 $sth->execute( $cat_borrower, '*', '*' );
484 my $result = $sth->fetchrow_hashref;
485 if ( $result->{maxissueqty} ne '' ) {
486 $sth2->execute( $borrower->{'borrowernumber'}, $type );
487 my $alreadyissued = $sth2->fetchrow;
488 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
489 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / category / total failed)" );
494 # check the 0 parameters (default branch / itemtype / default categorycode
495 $sth->execute( '*', $type, '*' );
496 my $result = $sth->fetchrow_hashref;
497 # warn "*, $type, * = ".Data::Dumper::Dumper($result);
499 if ( $result->{maxissueqty} ne '' ) {
500 # warn "checking on default branch and default categorycode";
501 $sth2->execute( $borrower->{'borrowernumber'}, $type );
502 my $alreadyissued = $sth2->fetchrow;
503 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
504 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / default category / itemtype failed)" );
507 # now checking for total
508 $sth->execute( '*', '*', '*' );
509 my $result = $sth->fetchrow_hashref;
510 if ( $result->{maxissueqty} ne '' ) {
511 warn "checking total";
512 $sth2->execute( $borrower->{'borrowernumber'}, $type );
513 my $alreadyissued = $sth2->fetchrow;
514 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
515 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / default category / total failed)" );
519 # OK, the patron can issue !!!
525 @issues = &itemissues($biblioitemnumber, $biblio);
527 Looks up information about who has borrowed the bookZ<>(s) with the
528 given biblioitemnumber.
530 C<$biblio> is ignored.
532 C<&itemissues> returns an array of references-to-hash. The keys
533 include the fields from the C<items> table in the Koha database.
534 Additional keys include:
540 If the item is currently on loan, this gives the due date.
542 If the item is not on loan, then this is either "Available" or
543 "Cancelled", if the item has been withdrawn.
547 If the item is currently on loan, this gives the card number of the
548 patron who currently has the item.
550 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
552 These give the timestamp for the last three times the item was
555 =item C<card0>, C<card1>, C<card2>
557 The card number of the last three patrons who borrowed this item.
559 =item C<borrower0>, C<borrower1>, C<borrower2>
561 The borrower number of the last three patrons who borrowed this item.
569 my ( $bibitem, $biblio ) = @_;
570 my $dbh = C4::Context->dbh;
572 # FIXME - If this function die()s, the script will abort, and the
573 # user won't get anything; depending on how far the script has
574 # gotten, the user might get a blank page. It would be much better
575 # to at least print an error message. The easiest way to do this
576 # is to set $SIG{__DIE__}.
578 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
583 $sth->execute($bibitem) || die $sth->errstr;
585 while ( my $data = $sth->fetchrow_hashref ) {
587 # Find out who currently has this item.
588 # FIXME - Wouldn't it be better to do this as a left join of
589 # some sort? Currently, this code assumes that if
590 # fetchrow_hashref() fails, then the book is on the shelf.
591 # fetchrow_hashref() can fail for any number of reasons (e.g.,
592 # database server crash), not just because no items match the
594 my $sth2 = $dbh->prepare(
595 "SELECT * FROM issues
596 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
598 AND returndate IS NULL
602 $sth2->execute( $data->{'itemnumber'} );
603 if ( my $data2 = $sth2->fetchrow_hashref ) {
604 $data->{'date_due'} = $data2->{'date_due'};
605 $data->{'card'} = $data2->{'cardnumber'};
606 $data->{'borrower'} = $data2->{'borrowernumber'};
609 if ( $data->{'wthdrawn'} eq '1' ) {
610 $data->{'date_due'} = 'Cancelled';
613 $data->{'date_due'} = 'Available';
619 # Find the last 3 people who borrowed this item.
620 $sth2 = $dbh->prepare(
621 "SELECT * FROM issues
622 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
624 AND returndate IS NOT NULL
625 ORDER BY returndate DESC,timestamp DESC"
628 $sth2->execute( $data->{'itemnumber'} );
629 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
630 { # FIXME : error if there is less than 3 pple borrowing this item
631 if ( my $data2 = $sth2->fetchrow_hashref ) {
632 $data->{"timestamp$i2"} = $data2->{'timestamp'};
633 $data->{"card$i2"} = $data2->{'cardnumber'};
634 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
639 $results[$i] = $data;
647 =head2 CanBookBeIssued
649 $issuingimpossible, $needsconfirmation =
650 CanBookBeIssued( $borrower, $barcode, $duedatespec, $inprocess );
651 C<$duedatespec> is a C4::Dates object.
652 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
656 sub CanBookBeIssued {
657 my ( $borrower, $barcode, $duedate, $inprocess ) = @_;
658 my %needsconfirmation; # filled with problems that needs confirmations
659 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
660 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
661 my $issue = GetItemIssue($item->{itemnumber});
662 my $biblioitem = GetBiblioItemData($item->{biblioitemnumber});
663 $item->{'itemtype'}=$biblioitem->{'itemtype'};
664 my $dbh = C4::Context->dbh;
667 # DUE DATE is OK ? -- should already have checked.
669 #$issuingimpossible{INVALID_DATE} = 1 unless ($duedate);
674 if ( $borrower->{flags}->{GNA} ) {
675 $issuingimpossible{GNA} = 1;
677 if ( $borrower->{flags}->{'LOST'} ) {
678 $issuingimpossible{CARD_LOST} = 1;
680 if ( $borrower->{flags}->{'DBARRED'} ) {
681 $issuingimpossible{DEBARRED} = 1;
683 if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
684 $issuingimpossible{EXPIRED} = 1;
686 my @expirydate= split /-/,$borrower->{'dateexpiry'};
687 if($expirydate[0]==0 || $expirydate[1]==0|| $expirydate[2]==0 ||
688 Date_to_Days(Today) > Date_to_Days( @expirydate )) {
689 $issuingimpossible{EXPIRED} = 1;
698 C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->output('iso') );
699 if ( C4::Context->preference("IssuingInProcess") ) {
700 my $amountlimit = C4::Context->preference("noissuescharge");
701 if ( $amount > $amountlimit && !$inprocess ) {
702 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
704 elsif ( $amount <= $amountlimit && !$inprocess ) {
705 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
710 $needsconfirmation{DEBT} = $amount;
715 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
717 my $toomany = TooMany( $borrower, $item->{biblionumber}, $item );
718 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
723 unless ( $item->{barcode} ) {
724 $issuingimpossible{UNKNOWN_BARCODE} = 1;
726 if ( $item->{'notforloan'}
727 && $item->{'notforloan'} > 0 )
729 $issuingimpossible{NOT_FOR_LOAN} = 1;
731 elsif ( !$item->{'notforloan'} ){
732 # we have to check itemtypes.notforloan also
733 if (C4::Context->preference('item-level_itypes')){
734 # this should probably be a subroutine
735 my $sth = $dbh->prepare("SELECT notforloan FROM itemtypes WHERE itemtype = ?");
736 $sth->execute($item->{'itemtype'});
737 my $notforloan=$sth->fetchrow_hashref();
739 if ($notforloan->{'notforloan'} == 1){
740 $issuingimpossible{NOT_FOR_LOAN} = 1;
743 elsif ($biblioitem->{'notforloan'} == 1){
744 $issuingimpossible{NOT_FOR_LOAN} = 1;
747 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
749 $issuingimpossible{WTHDRAWN} = 1;
751 if ( $item->{'restricted'}
752 && $item->{'restricted'} == 1 )
754 $issuingimpossible{RESTRICTED} = 1;
756 if ( C4::Context->preference("IndependantBranches") ) {
757 my $userenv = C4::Context->userenv;
758 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
759 $issuingimpossible{NOTSAMEBRANCH} = 1
760 if ( $item->{C4::Context->preference("HomeOrHoldingbranch")} ne $userenv->{branch} );
765 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
767 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
770 # Already issued to current borrower. Ask whether the loan should
772 my ($CanBookBeRenewed) = CanBookBeRenewed(
773 $borrower->{'borrowernumber'},
774 $item->{'itemnumber'}
776 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
777 $issuingimpossible{NO_MORE_RENEWALS} = 1;
780 $needsconfirmation{RENEW_ISSUE} = 1;
783 elsif ($issue->{borrowernumber}) {
785 # issued to someone else
786 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
788 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
789 $needsconfirmation{ISSUED_TO_ANOTHER} =
790 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
793 # See if the item is on reserve.
794 my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
796 my $resbor = $res->{'borrowernumber'};
797 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
800 # The item is on reserve and waiting, but has been
801 # reserved by some other patron.
802 my ( $resborrower, $flags ) =
803 GetMemberDetails( $resbor, 0 );
804 my $branches = GetBranches();
806 $branches->{ $res->{'branchcode'} }->{'branchname'};
807 $needsconfirmation{RESERVE_WAITING} =
808 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
810 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
812 elsif ( $restype eq "Reserved" ) {
814 # The item is on reserve for someone else.
815 my ( $resborrower, $flags ) =
816 GetMemberDetails( $resbor, 0 );
817 my $branches = GetBranches();
819 $branches->{ $res->{'branchcode'} }->{'branchname'};
820 $needsconfirmation{RESERVED} =
821 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
824 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" ) {
825 if ( $borrower->{'categorycode'} eq 'W' ) {
826 my %issuingimpossible;
827 return ( \%issuingimpossible, \%needsconfirmation );
829 return ( \%issuingimpossible, \%needsconfirmation );
832 return ( \%issuingimpossible, \%needsconfirmation );
838 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
840 &AddIssue($borrower,$barcode,$date)
844 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
846 =item C<$barcode> is the bar code of the book being issued.
848 =item C<$date> contains the max date of return. calculated if empty.
850 AddIssue does the following things :
851 - step 01: check that there is a borrowernumber & a barcode provided
852 - check for RENEWAL (book issued & being issued to the same patron)
853 - renewal YES = Calculate Charge & renew
855 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
857 - fill reserve if reserve to this patron
858 - cancel reserve or not, otherwise
859 * TRANSFERT PENDING ?
860 - complete the transfert
868 my ( $borrower, $barcode, $date, $cancelreserve ) = @_;
869 my $dbh = C4::Context->dbh;
870 my $barcodecheck=CheckValidBarcode($barcode);
871 if ($borrower and $barcode and $barcodecheck ne '0'){
872 # find which item we issue
873 my $item = GetItem('', $barcode);
877 # Get which branchcode we need
878 if (C4::Context->preference('CircControl') eq 'PickupLibary'){
879 $branch = C4::Context->userenv->{'branchcode'};
881 elsif (C4::Context->preference('CircControl') eq 'PatronLibary'){
882 $branch = $borrower->{'branchcode'};
886 $branch = $item->{'homebranch'};
889 # get actual issuing if there is one
890 my $actualissue = GetItemIssue( $item->{itemnumber});
892 # get biblioinformation for this item
893 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
896 # check if we just renew the issue.
898 if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
900 $borrower->{'borrowernumber'},
901 $item->{'itemnumber'},
909 if ( $actualissue->{borrowernumber}) {
910 # This book is currently on loan, but not to the person
911 # who wants to borrow it now. mark it returned before issuing to the new borrower
914 C4::Context->userenv->{'branch'}
918 # See if the item is on reserve.
919 my ( $restype, $res ) =
920 C4::Reserves::CheckReserves( $item->{'itemnumber'} );
922 my $resbor = $res->{'borrowernumber'};
923 if ( $resbor eq $borrower->{'borrowernumber'} ) {
925 # The item is reserved by the current patron
926 ModReserveFill($res);
928 elsif ( $restype eq "Waiting" ) {
931 # The item is on reserve and waiting, but has been
932 # reserved by some other patron.
933 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
934 my $branches = GetBranches();
936 $branches->{ $res->{'branchcode'} }->{'branchname'};
938 elsif ( $restype eq "Reserved" ) {
941 # The item is reserved by someone else.
942 my ( $resborrower, $flags ) =
943 GetMemberDetails( $resbor, 0 );
944 my $branches = GetBranches();
945 my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
946 if ($cancelreserve) { # cancel reserves on this item
947 CancelReserve( 0, $res->{'itemnumber'},
948 $res->{'borrowernumber'} );
951 if ($cancelreserve) {
952 CancelReserve( $res->{'biblionumber'}, 0,
953 $res->{'borrowernumber'} );
956 # set waiting reserve to first in reserve queue as book isn't waiting now
958 $res->{'biblionumber'},
959 $res->{'borrowernumber'},
965 # Starting process for transfer job (checking transfert and validate it if we have one)
966 my ($datesent) = GetTransfers($item->{'itemnumber'});
968 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
971 "UPDATE branchtransfers
972 SET datearrived = now(),
974 comments = 'Forced branchtransfert'
975 WHERE itemnumber= ? AND datearrived IS NULL"
977 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
981 # Record in the database the fact that the book was issued.
985 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
992 my $itype=(C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ;
993 my $loanlength = GetLoanLength(
994 $borrower->{'categorycode'},
998 $datedue = time + ($loanlength) * 86400;
999 my @datearr = localtime($datedue);
1000 $dateduef = C4::Dates->new( sprintf("%04d-%02d-%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3]), 'iso');
1001 $dateduef=CheckValidDatedue($dateduef,$item->{'itemnumber'},C4::Context->userenv->{'branch'});
1003 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
1004 if ( C4::Context->preference('ReturnBeforeExpiry') && $dateduef->output('iso') gt $borrower->{dateexpiry} ) {
1005 $dateduef = C4::Dates->new($borrower->{dateexpiry},'iso');
1009 $borrower->{'borrowernumber'},
1010 $item->{'itemnumber'},
1011 strftime( "%Y-%m-%d", localtime ),$dateduef->output('iso'), C4::Context->userenv->{'branch'}
1014 $item->{'issues'}++;
1017 "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed = now(), onloan = ? WHERE itemnumber=?");
1020 C4::Context->userenv->{'branch'},
1021 $dateduef->output('iso'),
1022 $item->{'itemnumber'}
1025 &ModDateLastSeen( $item->{'itemnumber'} );
1026 my $record = GetMarcItem( $item->{'biblionumber'}, $item->{'itemnumber'} );
1027 my $frameworkcode = GetFrameworkCode( $item->{'biblionumber'} );
1028 ModItemInMarc( $record, $item->{'biblionumber'}, $item->{'itemnumber'}, $frameworkcode );
1029 # If it costs to borrow this book, charge it to the patron's account.
1030 my ( $charge, $itemtype ) = GetIssuingCharges(
1031 $item->{'itemnumber'},
1032 $borrower->{'borrowernumber'}
1034 if ( $charge > 0 ) {
1036 $item->{'itemnumber'},
1037 $borrower->{'borrowernumber'}, $charge
1039 $item->{'charge'} = $charge;
1042 # Record the fact that this book was issued.
1044 C4::Context->userenv->{'branch'},
1046 '', $item->{'itemnumber'},
1047 $item->{'itemtype'}, $borrower->{'borrowernumber'}
1051 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'})
1052 if C4::Context->preference("IssueLog");
1057 =head2 GetLoanLength
1059 Get loan length for an itemtype, a borrower type and a branch
1061 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1066 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1067 my $dbh = C4::Context->dbh;
1070 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
1072 # warn "in get loan lenght $borrowertype $itemtype $branchcode ";
1073 # try to find issuelength & return the 1st available.
1074 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1075 $sth->execute( $borrowertype, $itemtype, $branchcode );
1076 my $loanlength = $sth->fetchrow_hashref;
1077 return $loanlength->{issuelength}
1078 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1080 $sth->execute( $borrowertype, $itemtype, "*" );
1081 $loanlength = $sth->fetchrow_hashref;
1082 return $loanlength->{issuelength}
1083 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1085 $sth->execute( $borrowertype, "*", $branchcode );
1086 $loanlength = $sth->fetchrow_hashref;
1087 return $loanlength->{issuelength}
1088 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1090 $sth->execute( "*", $itemtype, $branchcode );
1091 $loanlength = $sth->fetchrow_hashref;
1092 return $loanlength->{issuelength}
1093 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1095 $sth->execute( $borrowertype, "*", "*" );
1096 $loanlength = $sth->fetchrow_hashref;
1097 return $loanlength->{issuelength}
1098 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1100 $sth->execute( "*", "*", $branchcode );
1101 $loanlength = $sth->fetchrow_hashref;
1102 return $loanlength->{issuelength}
1103 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1105 $sth->execute( "*", $itemtype, "*" );
1106 $loanlength = $sth->fetchrow_hashref;
1107 return $loanlength->{issuelength}
1108 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1110 $sth->execute( "*", "*", "*" );
1111 $loanlength = $sth->fetchrow_hashref;
1112 return $loanlength->{issuelength}
1113 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1115 # if no rule is set => 21 days (hardcoded)
1121 ($doreturn, $messages, $iteminformation, $borrower) =
1122 &AddReturn($barcode, $branch, $exemptfine);
1126 C<$barcode> is the bar code of the book being returned. C<$branch> is
1127 the code of the branch where the book is being returned. C<$exemptfine>
1128 indicates that overdue charges for the item will not be applied.
1130 C<&AddReturn> returns a list of four items:
1132 C<$doreturn> is true iff the return succeeded.
1134 C<$messages> is a reference-to-hash giving the reason for failure:
1140 No item with this barcode exists. The value is C<$barcode>.
1144 The book is not currently on loan. The value is C<$barcode>.
1146 =item C<IsPermanent>
1148 The book's home branch is a permanent collection. If you have borrowed
1149 this book, you are not allowed to return it. The value is the code for
1150 the book's home branch.
1154 This book has been withdrawn/cancelled. The value should be ignored.
1158 The item was reserved. The value is a reference-to-hash whose keys are
1159 fields from the reserves table of the Koha database, and
1160 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1161 either C<Waiting>, C<Reserved>, or 0.
1165 C<$borrower> is a reference-to-hash, giving information about the
1166 patron who last borrowed the book.
1171 my ( $barcode, $branch, $exemptfine ) = @_;
1172 my $dbh = C4::Context->dbh;
1176 my $validTransfert = 0;
1177 my $reserveDone = 0;
1179 # get information on item
1180 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1181 my $biblio = GetBiblioFromItemNumber($iteminformation->{'itemnumber'});
1182 unless ($iteminformation->{'itemnumber'} ) {
1183 $messages->{'BadBarcode'} = $barcode;
1187 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1188 $messages->{'NotIssued'} = $barcode;
1192 # check if the book is in a permanent collection....
1193 my $hbr = $iteminformation->{'homebranch'};
1194 my $branches = GetBranches();
1195 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1196 $messages->{'IsPermanent'} = $hbr;
1199 # if independent branches are on and returning to different branch, refuse the return
1200 if ($hbr ne C4::Context->userenv->{'branch'} && C4::Context->preference("IndependantBranches")){
1201 $messages->{'Wrongbranch'} = 1;
1205 # check that the book has been cancelled
1206 if ( $iteminformation->{'wthdrawn'} ) {
1207 $messages->{'wthdrawn'} = 1;
1211 # new op dev : if the book returned in an other branch update the holding branch
1213 # update issues, thereby returning book (should push this out into another subroutine
1214 $borrower = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1216 # case of a return of document (deal with issues and holdingbranch)
1221 "UPDATE issues SET returndate = now() WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (returndate IS NULL)"
1223 $sth->execute( $borrower->{'borrowernumber'},
1224 $iteminformation->{'itemnumber'} );
1225 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1228 # continue to deal with returns cases, but not only if we have an issue
1230 # the holdingbranch is updated if the document is returned in an other location .
1231 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} ) {
1232 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1233 # reload iteminformation holdingbranch with the userenv value
1234 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1236 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1237 my $sth = $dbh->prepare("UPDATE items SET onloan = NULL where itemnumber = ?");
1238 $sth->execute($iteminformation->{'itemnumber'});
1240 my $record = GetMarcItem( $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'} );
1241 my $frameworkcode = GetFrameworkCode( $biblio->{'biblionumber'} );
1242 ModItemInMarc( $record, $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'}, $frameworkcode );
1244 if ($iteminformation->{borrowernumber}){
1245 ($borrower) = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1247 # fix up the accounts.....
1248 if ( $iteminformation->{'itemlost'} ) {
1249 $messages->{'WasLost'} = 1;
1252 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1253 # check if we have a transfer for this document
1254 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1256 # if we have a transfer to do, we update the line of transfers with the datearrived
1258 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1261 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1263 $sth->execute( $iteminformation->{'itemnumber'} );
1265 # now we check if there is a reservation with the validate of transfer if we have one, we can set it with the status 'W'
1266 C4::Reserves::ModReserveStatus( $iteminformation->{'itemnumber'},'W' );
1269 $messages->{'WrongTransfer'} = $tobranch;
1270 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1272 $validTransfert = 1;
1275 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1276 # fix up the accounts.....
1277 if ($iteminformation->{'itemlost'}) {
1278 FixAccountForLostAndReturned($iteminformation, $borrower);
1279 $messages->{'WasLost'} = 1;
1281 # fix up the overdues in accounts...
1282 FixOverduesOnReturn( $borrower->{'borrowernumber'},
1283 $iteminformation->{'itemnumber'}, $exemptfine );
1285 # find reserves.....
1286 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1287 my ( $resfound, $resrec ) =
1288 C4::Reserves::CheckReserves( $iteminformation->{'itemnumber'} );
1290 $resrec->{'ResFound'} = $resfound;
1291 $messages->{'ResFound'} = $resrec;
1296 # Record the fact that this book was returned.
1298 $branch, 'return', '0', '',
1299 $iteminformation->{'itemnumber'},
1300 $iteminformation->{'itemtype'},
1301 $borrower->{'borrowernumber'}
1304 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'})
1305 if C4::Context->preference("ReturnLog");
1307 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1308 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1310 if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1311 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1312 ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1313 $messages->{'WasTransfered'} = 1;
1316 $messages->{'NeedsTransfer'} = 1;
1320 return ( $doreturn, $messages, $iteminformation, $borrower );
1323 =head2 FixOverduesOnReturn
1325 &FixOverduesOnReturn($brn,$itm, $exemptfine);
1327 C<$brn> borrowernumber
1331 internal function, called only by AddReturn
1335 sub FixOverduesOnReturn {
1336 my ( $borrowernumber, $item, $exemptfine ) = @_;
1337 my $dbh = C4::Context->dbh;
1339 # check for overdue fine
1342 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1344 $sth->execute( $borrowernumber, $item );
1346 # alter fine to show that the book has been returned
1348 if ($data = $sth->fetchrow_hashref) {
1349 my $uquery =($exemptfine)? "update accountlines set accounttype='FFOR', amountoutstanding=0":"update accountlines set accounttype='F' ";
1350 $uquery .= " where (borrowernumber = ?) and (itemnumber = ?) and (accountno = ?)";
1351 my $usth = $dbh->prepare($uquery);
1352 $usth->execute($borrowernumber,$item ,$data->{'accountno'});
1360 =head2 FixAccountForLostAndReturned
1362 &FixAccountForLostAndReturned($iteminfo,$borrower);
1364 Calculates the charge for a book lost and returned (Not exported & used only once)
1366 C<$iteminfo> is a hashref to iteminfo. Only {itemnumber} is used.
1368 C<$borrower> is a hashref to borrower. Only {borrowernumber is used.
1370 Internal function, called by AddReturn
1374 sub FixAccountForLostAndReturned {
1375 my ($iteminfo, $borrower) = @_;
1377 my $dbh = C4::Context->dbh;
1378 my $itm = $iteminfo->{'itemnumber'};
1379 # check for charge made for lost book
1380 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE (itemnumber = ?) AND (accounttype='L' OR accounttype='Rep') ORDER BY date DESC");
1381 $sth->execute($itm);
1382 if (my $data = $sth->fetchrow_hashref) {
1383 # writeoff this amount
1385 my $amount = $data->{'amount'};
1386 my $acctno = $data->{'accountno'};
1388 if ($data->{'amountoutstanding'} == $amount) {
1389 $offset = $data->{'amount'};
1392 $offset = $amount - $data->{'amountoutstanding'};
1393 $amountleft = $data->{'amountoutstanding'} - $amount;
1395 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
1396 WHERE (borrowernumber = ?)
1397 AND (itemnumber = ?) AND (accountno = ?) ");
1398 $usth->execute($data->{'borrowernumber'},$itm,$acctno);
1400 #check if any credit is left if so writeoff other accounts
1401 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
1402 if ($amountleft < 0){
1405 if ($amountleft > 0){
1406 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
1407 AND (amountoutstanding >0) ORDER BY date");
1408 $msth->execute($data->{'borrowernumber'});
1409 # offset transactions
1412 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1413 if ($accdata->{'amountoutstanding'} < $amountleft) {
1415 $amountleft -= $accdata->{'amountoutstanding'};
1417 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1420 my $thisacct = $accdata->{'accountno'};
1421 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
1422 WHERE (borrowernumber = ?)
1423 AND (accountno=?)");
1424 $usth->execute($newamtos,$data->{'borrowernumber'},'$thisacct');
1426 $usth = $dbh->prepare("INSERT INTO accountoffsets
1427 (borrowernumber, accountno, offsetaccount, offsetamount)
1430 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
1435 if ($amountleft > 0){
1438 my $desc="Item Returned ".$iteminfo->{'barcode'};
1439 $usth = $dbh->prepare("INSERT INTO accountlines
1440 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1441 VALUES (?,?,now(),?,?,'CR',?)");
1442 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
1444 $usth = $dbh->prepare("INSERT INTO accountoffsets
1445 (borrowernumber, accountno, offsetaccount, offsetamount)
1447 $usth->execute($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset);
1449 $usth = $dbh->prepare("UPDATE items SET paidfor='' WHERE itemnumber=?");
1450 $usth->execute($itm);
1459 $issues = &GetItemIssue($itemnumber);
1461 Returns patrons currently having a book. nothing if item is not issued atm
1463 C<$itemnumber> is the itemnumber
1465 Returns an array of hashes
1469 my ( $itemnumber) = @_;
1470 return unless $itemnumber;
1471 my $dbh = C4::Context->dbh;
1475 my $today = POSIX::strftime("%Y%m%d", localtime);
1477 my $sth = $dbh->prepare(
1478 "SELECT * FROM issues
1479 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1481 issues.itemnumber=? AND returndate IS NULL ");
1482 $sth->execute($itemnumber);
1483 my $data = $sth->fetchrow_hashref;
1484 my $datedue = $data->{'date_due'};
1486 if ( $datedue < $today ) {
1487 $data->{'overdue'} = 1;
1489 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1494 =head2 GetItemIssues
1496 $issues = &GetItemIssues($itemnumber, $history);
1498 Returns patrons that have issued a book
1500 C<$itemnumber> is the itemnumber
1501 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1503 Returns an array of hashes
1507 my ( $itemnumber,$history ) = @_;
1508 my $dbh = C4::Context->dbh;
1512 my $today = POSIX::strftime("%Y%m%d", localtime);
1514 my $sth = $dbh->prepare(
1515 "SELECT * FROM issues
1516 LEFT JOIN borrowers ON borrowers.borrowernumber
1517 LEFT JOIN items ON items.itemnumber=issues.itemnumber
1519 issues.itemnumber=?".($history?"":" AND returndate IS NULL ").
1520 "ORDER BY issues.date_due DESC"
1522 $sth->execute($itemnumber);
1523 while ( my $data = $sth->fetchrow_hashref ) {
1524 my $datedue = $data->{'date_due'};
1526 if ( $datedue < $today ) {
1527 $data->{'overdue'} = 1;
1529 my $itemnumber = $data->{'itemnumber'};
1530 push @GetItemIssues, $data;
1533 return ( \@GetItemIssues );
1536 =head2 GetBiblioIssues
1538 $issues = GetBiblioIssues($biblionumber);
1540 this function get all issues from a biblionumber.
1543 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1544 tables issues and the firstname,surname & cardnumber from borrowers.
1548 sub GetBiblioIssues {
1549 my $biblionumber = shift;
1550 return undef unless $biblionumber;
1551 my $dbh = C4::Context->dbh;
1553 SELECT issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1555 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1556 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1557 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1558 LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1559 WHERE biblio.biblionumber = ?
1560 ORDER BY issues.timestamp
1562 my $sth = $dbh->prepare($query);
1563 $sth->execute($biblionumber);
1566 while ( my $data = $sth->fetchrow_hashref ) {
1567 push @issues, $data;
1572 =head2 CanBookBeRenewed
1574 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1576 Find out whether a borrowed item may be renewed.
1578 C<$dbh> is a DBI handle to the Koha database.
1580 C<$borrowernumber> is the borrower number of the patron who currently
1581 has the item on loan.
1583 C<$itemnumber> is the number of the item to renew.
1585 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1586 item must currently be on loan to the specified borrower; renewals
1587 must be allowed for the item's type; and the borrower must not have
1588 already renewed the loan.
1592 sub CanBookBeRenewed {
1594 # check renewal status
1595 my ( $borrowernumber, $itemnumber ) = @_;
1596 my $dbh = C4::Context->dbh;
1600 # Look in the issues table for this item, lent to this borrower,
1601 # and not yet returned.
1603 # FIXME - I think this function could be redone to use only one SQL call.
1604 my $sth1 = $dbh->prepare(
1605 "SELECT * FROM issues
1606 WHERE borrowernumber = ?
1608 AND returndate IS NULL"
1610 $sth1->execute( $borrowernumber, $itemnumber );
1611 if ( my $data1 = $sth1->fetchrow_hashref ) {
1613 # Found a matching item
1615 # See if this item may be renewed. This query is convoluted
1616 # because it's a bit messy: given the item number, we need to find
1617 # the biblioitem, which gives us the itemtype, which tells us
1618 # whether it may be renewed.
1619 my $sth2 = $dbh->prepare(
1620 "SELECT renewalsallowed FROM items
1621 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1622 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1623 WHERE items.itemnumber = ?
1626 $sth2->execute($itemnumber);
1627 if ( my $data2 = $sth2->fetchrow_hashref ) {
1628 $renews = $data2->{'renewalsallowed'};
1630 if ( $renews && $renews >= $data1->{'renewals'} ) {
1634 my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber);
1641 return ($renewokay);
1646 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1650 C<$borrowernumber> is the borrower number of the patron who currently
1653 C<$itemnumber> is the number of the item to renew.
1655 C<$datedue> can be used to set the due date. If C<$datedue> is the
1656 empty string, C<&AddRenewal> will calculate the due date automatically
1657 from the book's item type. If you wish to set the due date manually,
1658 C<$datedue> should be in the form YYYY-MM-DD.
1664 my ( $borrowernumber, $itemnumber, $branch ,$datedue ) = @_;
1665 my $dbh = C4::Context->dbh;
1667 my $biblio = GetBiblioFromItemNumber($itemnumber);
1668 # If the due date wasn't specified, calculate it by adding the
1669 # book's loan length to today's date.
1670 unless ( $datedue ) {
1673 my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 );
1674 my $loanlength = GetLoanLength(
1675 $borrower->{'categorycode'},
1676 (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ,
1677 $borrower->{'branchcode'}
1679 #FIXME -- choose issuer or borrower branch.
1680 #FIXME -- where's the calendar ?
1681 #FIXME -- $debug-ify the (0)
1682 my @darray = Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1683 $datedue = C4::Dates->new( sprintf("%04d-%02d-%02d",@darray[0..2]), 'iso');
1684 (0) and print STDERR "C4::Dates->new->output = " . C4::Dates->new()->output()
1685 . "\ndatedue->output = " . $datedue->output()
1686 . "\n(Y,M,D) = " . join ',', @darray;
1687 $datedue=CheckValidDatedue($datedue,$itemnumber,$branch);
1690 # Find the issues record for this book
1692 $dbh->prepare("SELECT * FROM issues
1693 WHERE borrowernumber=?
1695 AND returndate IS NULL"
1697 $sth->execute( $borrowernumber, $itemnumber );
1698 my $issuedata = $sth->fetchrow_hashref;
1701 # Update the issues record to have the new due date, and a new count
1702 # of how many times it has been renewed.
1703 my $renews = $issuedata->{'renewals'} + 1;
1704 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1705 WHERE borrowernumber=?
1707 AND returndate IS NULL"
1709 $sth->execute( $datedue->output('iso'), $renews, $borrowernumber, $itemnumber );
1712 # Update the renewal count on the item, and tell zebra to reindex
1713 $renews = $biblio->{'renewals'} + 1;
1714 $sth = $dbh->prepare("UPDATE items SET renewals = ? WHERE itemnumber = ?");
1715 $sth->execute($renews,$itemnumber);
1717 my $record = GetMarcItem( $biblio->{'biblionumber'}, $itemnumber );
1718 my $frameworkcode = GetFrameworkCode( $biblio->{'biblionumber'} );
1719 ModItemInMarc( $record, $biblio->{'biblionumber'}, $itemnumber, $frameworkcode );
1721 # Charge a new rental fee, if applicable?
1722 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1723 if ( $charge > 0 ) {
1724 my $accountno = getnextacctno( $borrowernumber );
1725 my $item = GetBiblioFromItemNumber($itemnumber);
1726 $sth = $dbh->prepare(
1727 "INSERT INTO accountlines
1728 (borrowernumber,accountno,date,amount,
1729 description,accounttype,amountoutstanding,
1731 VALUES (?,?,now(),?,?,?,?,?)"
1733 $sth->execute( $borrowernumber, $accountno, $charge,
1734 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1735 'Rent', $charge, $itemnumber );
1739 UpdateStats( $branch, 'renew', $charge, '', $itemnumber );
1743 # check renewal status
1744 my ($bornum,$itemno)=@_;
1745 my $dbh = C4::Context->dbh;
1747 my $renewsallowed = 0;
1749 # Look in the issues table for this item, lent to this borrower,
1750 # and not yet returned.
1752 # FIXME - I think this function could be redone to use only one SQL call.
1753 my $sth = $dbh->prepare("select * from issues
1754 where (borrowernumber = ?)
1755 and (itemnumber = ?)
1756 and returndate is null");
1757 $sth->execute($bornum,$itemno);
1758 my $data = $sth->fetchrow_hashref;
1759 $renewcount = $data->{'renewals'} if $data->{'renewals'};
1760 my $sth2 = $dbh->prepare("select renewalsallowed from items,biblioitems,itemtypes
1761 where (items.itemnumber = ?)
1762 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
1763 and (biblioitems.itemtype = itemtypes.itemtype)");
1764 $sth2->execute($itemno);
1765 my $data2 = $sth2->fetchrow_hashref();
1766 $renewsallowed = $data2->{'renewalsallowed'};
1767 $renewsleft = $renewsallowed - $renewcount;
1768 warn "Renewcount:$renewcount RenewsAll:$renewsallowed RenewLeft:$renewsleft";
1769 return ($renewcount,$renewsallowed,$renewsleft);
1771 =head2 GetIssuingCharges
1773 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1775 Calculate how much it would cost for a given patron to borrow a given
1776 item, including any applicable discounts.
1778 C<$itemnumber> is the item number of item the patron wishes to borrow.
1780 C<$borrowernumber> is the patron's borrower number.
1782 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1783 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1788 sub GetIssuingCharges {
1790 # calculate charges due
1791 my ( $itemnumber, $borrowernumber ) = @_;
1793 my $dbh = C4::Context->dbh;
1796 # Get the book's item type and rental charge (via its biblioitem).
1797 my $qcharge = "SELECT itemtypes.itemtype,rentalcharge FROM items
1798 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
1799 $qcharge .= (C4::Context->preference('item-level_itypes'))
1800 ? " LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
1801 : " LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
1803 $qcharge .= "WHERE items.itemnumber =?";
1805 my $sth1 = $dbh->prepare($qcharge);
1806 $sth1->execute($itemnumber);
1807 if ( my $data1 = $sth1->fetchrow_hashref ) {
1808 $item_type = $data1->{'itemtype'};
1809 $charge = $data1->{'rentalcharge'};
1810 my $q2 = "SELECT rentaldiscount FROM borrowers
1811 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1812 WHERE borrowers.borrowernumber = ?
1813 AND issuingrules.itemtype = ?";
1814 my $sth2 = $dbh->prepare($q2);
1815 $sth2->execute( $borrowernumber, $item_type );
1816 if ( my $data2 = $sth2->fetchrow_hashref ) {
1817 my $discount = $data2->{'rentaldiscount'};
1818 if ( $discount eq 'NULL' ) {
1821 $charge = ( $charge * ( 100 - $discount ) ) / 100;
1827 return ( $charge, $item_type );
1830 =head2 AddIssuingCharge
1832 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1836 sub AddIssuingCharge {
1837 my ( $itemnumber, $borrowernumber, $charge ) = @_;
1838 my $dbh = C4::Context->dbh;
1839 my $nextaccntno = getnextacctno( $borrowernumber );
1841 INSERT INTO accountlines
1842 (borrowernumber, itemnumber, accountno,
1843 date, amount, description, accounttype,
1845 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1847 my $sth = $dbh->prepare($query);
1848 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1854 GetTransfers($itemnumber);
1859 my ($itemnumber) = @_;
1861 my $dbh = C4::Context->dbh;
1867 FROM branchtransfers
1868 WHERE itemnumber = ?
1869 AND datearrived IS NULL
1871 my $sth = $dbh->prepare($query);
1872 $sth->execute($itemnumber);
1873 my @row = $sth->fetchrow_array();
1879 =head2 GetTransfersFromTo
1881 @results = GetTransfersFromTo($frombranch,$tobranch);
1883 Returns the list of pending transfers between $from and $to branch
1887 sub GetTransfersFromTo {
1888 my ( $frombranch, $tobranch ) = @_;
1889 return unless ( $frombranch && $tobranch );
1890 my $dbh = C4::Context->dbh;
1892 SELECT itemnumber,datesent,frombranch
1893 FROM branchtransfers
1896 AND datearrived IS NULL
1898 my $sth = $dbh->prepare($query);
1899 $sth->execute( $frombranch, $tobranch );
1902 while ( my $data = $sth->fetchrow_hashref ) {
1903 push @gettransfers, $data;
1906 return (@gettransfers);
1909 =head2 DeleteTransfer
1911 &DeleteTransfer($itemnumber);
1915 sub DeleteTransfer {
1916 my ($itemnumber) = @_;
1917 my $dbh = C4::Context->dbh;
1918 my $sth = $dbh->prepare(
1919 "DELETE FROM branchtransfers
1921 AND datearrived IS NULL "
1923 $sth->execute($itemnumber);
1927 =head2 AnonymiseIssueHistory
1929 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1931 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1932 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1934 return the number of affected rows.
1938 sub AnonymiseIssueHistory {
1940 my $borrowernumber = shift;
1941 my $dbh = C4::Context->dbh;
1944 SET borrowernumber = NULL
1945 WHERE returndate < '".$date."'
1946 AND borrowernumber IS NOT NULL
1948 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1949 my $rows_affected = $dbh->do($query);
1950 return $rows_affected;
1953 =head2 updateWrongTransfer
1955 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1957 This function validate the line of brachtransfer but with the wrong destination (mistake from a librarian ...), and create a new line in branchtransfer from the actual library to the original library of reservation
1961 sub updateWrongTransfer {
1962 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1963 my $dbh = C4::Context->dbh;
1964 # first step validate the actual line of transfert .
1967 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1969 $sth->execute($FromLibrary,$itemNumber);
1972 # second step create a new line of branchtransfer to the right location .
1973 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1975 #third step changing holdingbranch of item
1976 UpdateHoldingbranch($FromLibrary,$itemNumber);
1979 =head2 UpdateHoldingbranch
1981 $items = UpdateHoldingbranch($branch,$itmenumber);
1982 Simple methode for updating hodlingbranch in items BDD line
1985 sub UpdateHoldingbranch {
1986 my ( $branch,$itmenumber ) = @_;
1987 my $dbh = C4::Context->dbh;
1988 # first step validate the actual line of transfert .
1991 "update items set holdingbranch = ? where itemnumber= ?"
1993 $sth->execute($branch,$itmenumber);
1998 =head2 CheckValidDatedue
2000 $newdatedue = CheckValidDatedue($date_due,$itemnumber,$branchcode);
2001 this function return a new date due after checked if it's a repeatable or special holiday
2002 C<$date_due> = returndate calculate with no day check
2003 C<$itemnumber> = itemnumber
2004 C<$branchcode> = localisation of issue
2006 # Why not create calendar object? -
2007 # TODO add 'duedate' option to useDaysMode .
2008 sub CheckValidDatedue {
2009 my ($date_due,$itemnumber,$branchcode)=@_;
2010 my @datedue=split('-',$date_due->output('iso'));
2011 my $years=$datedue[0];
2012 my $month=$datedue[1];
2013 my $day=$datedue[2];
2014 # die "Item# $itemnumber ($branchcode) due: " . ${date_due}->output() . "\n(Y,M,D) = ($years,$month,$day)":
2016 for (my $i=0;$i<2;$i++){
2017 $dow=Day_of_Week($years,$month,$day);
2018 ($dow=0) if ($dow>6);
2019 my $result=CheckRepeatableHolidays($itemnumber,$dow,$branchcode);
2020 my $countspecial=CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
2021 my $countspecialrepeatable=CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2022 if (($result ne '0') or ($countspecial ne '0') or ($countspecialrepeatable ne '0') ){
2024 (($years,$month,$day) = Add_Delta_Days($years,$month,$day, 1))if ($i ne '1');
2027 my $newdatedue=C4::Dates->new(sprintf("%04d-%02d-%02d",$years,$month,$day),'iso');
2030 =head2 CheckRepeatableHolidays
2032 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
2033 this function check if the date due is a repeatable holiday
2034 C<$date_due> = returndate calculate with no day check
2035 C<$itemnumber> = itemnumber
2036 C<$branchcode> = localisation of issue
2040 sub CheckRepeatableHolidays{
2041 my($itemnumber,$week_day,$branchcode)=@_;
2042 my $dbh = C4::Context->dbh;
2043 my $query = qq|SELECT count(*)
2044 FROM repeatable_holidays
2047 my $sth = $dbh->prepare($query);
2048 $sth->execute($branchcode,$week_day);
2049 my $result=$sth->fetchrow;
2055 =head2 CheckSpecialHolidays
2057 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
2058 this function check if the date is a special holiday
2059 C<$years> = the years of datedue
2060 C<$month> = the month of datedue
2061 C<$day> = the day of datedue
2062 C<$itemnumber> = itemnumber
2063 C<$branchcode> = localisation of issue
2065 sub CheckSpecialHolidays{
2066 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
2067 my $dbh = C4::Context->dbh;
2068 my $query=qq|SELECT count(*)
2069 FROM `special_holidays`
2075 my $sth = $dbh->prepare($query);
2076 $sth->execute($years,$month,$day,$branchcode);
2077 my $countspecial=$sth->fetchrow ;
2079 return $countspecial;
2082 =head2 CheckRepeatableSpecialHolidays
2084 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2085 this function check if the date is a repeatble special holidays
2086 C<$month> = the month of datedue
2087 C<$day> = the day of datedue
2088 C<$itemnumber> = itemnumber
2089 C<$branchcode> = localisation of issue
2091 sub CheckRepeatableSpecialHolidays{
2092 my ($month,$day,$itemnumber,$branchcode) = @_;
2093 my $dbh = C4::Context->dbh;
2094 my $query=qq|SELECT count(*)
2095 FROM `repeatable_holidays`
2100 my $sth = $dbh->prepare($query);
2101 $sth->execute($month,$day,$branchcode);
2102 my $countspecial=$sth->fetchrow ;
2104 return $countspecial;
2109 sub CheckValidBarcode{
2111 my $dbh = C4::Context->dbh;
2112 my $query=qq|SELECT count(*)
2116 my $sth = $dbh->prepare($query);
2117 $sth->execute($barcode);
2118 my $exist=$sth->fetchrow ;
2129 Koha Developement team <info@koha.org>