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
43 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
45 # set the version for version checking
50 C4::Circulation - Koha circulation module
58 The functions in this module deal with circulation, issues, and
59 returns, as well as general information about the library.
60 Also deals with stocktaking.
68 # FIXME subs that should probably be elsewhere
74 # subs to deal with issuing a book
86 &AnonymiseIssueHistory
88 # subs to deal with returns
93 # subs to deal with transfers
102 # FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
103 # 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 ?
107 =head3 $str = &decode($chunk);
111 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
118 sub cuecatbarcodedecode {
121 my @fields = split( /\./, $barcode );
122 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
123 if ( $#results == 2 ) {
133 =head3 $str = &decode($chunk);
137 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
147 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
148 my @s = map { index( $seq, $_ ); } split( //, $encoded );
149 my $l = ( $#s + 1 ) % 4;
160 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
162 chr( ( $n >> 16 ) ^ 67 )
163 .chr( ( $n >> 8 & 255 ) ^ 67 )
164 .chr( ( $n & 255 ) ^ 67 );
167 $r = substr( $r, 0, length($r) - $l );
173 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch, $barcode, $ignore_reserves);
175 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
177 C<$newbranch> is the code for the branch to which the item should be transferred.
179 C<$barcode> is the barcode of the item to be transferred.
181 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
182 Otherwise, if an item is reserved, the transfer fails.
184 Returns three values:
188 is true if the transfer was successful.
192 is a reference-to-hash which may have any of the following keys:
198 There is no item in the catalog with the given barcode. The value is C<$barcode>.
202 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.
204 =item C<DestinationEqualsHolding>
206 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.
210 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.
214 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>.
216 =item C<WasTransferred>
218 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
225 my ( $tbr, $barcode, $ignoreRs ) = @_;
228 my $branches = GetBranches();
229 my $itemnumber = GetItemnumberFromBarcode( $barcode );
230 my $issue = GetItemIssue($itemnumber);
231 my $biblio = GetBiblioFromItemNumber($itemnumber);
234 if ( not $itemnumber ) {
235 $messages->{'BadBarcode'} = $barcode;
239 # get branches of book...
240 my $hbr = $biblio->{'homebranch'};
241 my $fbr = $biblio->{'holdingbranch'};
244 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
245 $messages->{'IsPermanent'} = $hbr;
248 # can't transfer book if is already there....
249 if ( $fbr eq $tbr ) {
250 $messages->{'DestinationEqualsHolding'} = 1;
254 # check if it is still issued to someone, return it...
255 if ($issue->{borrowernumber}) {
256 AddReturn( $barcode, $fbr );
257 $messages->{'WasReturned'} = $issue->{borrowernumber};
261 # That'll save a database query.
262 my ( $resfound, $resrec ) =
263 CheckReserves( $itemnumber );
264 if ( $resfound and not $ignoreRs ) {
265 $resrec->{'ResFound'} = $resfound;
267 # $messages->{'ResFound'} = $resrec;
271 #actually do the transfer....
273 ModItemTransfer( $itemnumber, $fbr, $tbr );
275 # don't need to update MARC anymore, we do it in batch now
276 $messages->{'WasTransfered'} = 1;
277 ModDateLastSeen( $itemnumber );
279 return ( $dotransfer, $messages, $biblio );
282 =head2 CanBookBeIssued
284 Check if a book can be issued.
286 my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued($borrower,$barcode,$year,$month,$day);
290 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
292 =item C<$barcode> is the bar code of the book being issued.
294 =item C<$year> C<$month> C<$day> contains the date of the return (in case it's forced by "stickyduedate".
302 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
303 Possible values are :
309 sticky due date is invalid
313 borrower gone with no address
317 borrower declared it's card lost
323 =head3 UNKNOWN_BARCODE
337 item is restricted (set by ??)
339 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
340 Possible values are :
348 renewing, not issuing
350 =head3 ISSUED_TO_ANOTHER
352 issued to someone else.
356 reserved for someone else.
360 sticky due date is invalid
364 if the borrower borrows to much things
368 # check if a book can be issued.
372 my $borrower = shift;
373 my $biblionumber = shift;
375 my $cat_borrower = $borrower->{'categorycode'};
376 my $branch_borrower = $borrower->{'branchcode'};
377 my $dbh = C4::Context->dbh;
379 my $branch_issuer = C4::Context->userenv->{'branchcode'};
380 #TODO : specify issuer or borrower for circrule.
381 my $type = (C4::Context->preference('item-level_itypes'))
382 ? $item->{'itype'} # item-level
383 : $item->{'itemtype'}; # biblio-level
387 'SELECT * FROM issuingrules
388 WHERE categorycode = ?
393 my $query2 = "SELECT COUNT(*) FROM issues i, biblioitems s1, items s2
394 WHERE i.borrowernumber = ?
395 AND i.returndate IS NULL
396 AND i.itemnumber = s2.itemnumber
397 AND s1.biblioitemnumber = s2.biblioitemnumber"
398 . (C4::Context->preference('item-level_itypes'))
400 : " AND s1.itemtype= ? ";
401 my $sth2= $dbh->prepare($query2);
404 'SELECT COUNT(*) FROM issues
405 WHERE borrowernumber = ?
406 AND returndate IS NULL'
410 # check the 3 parameters (branch / itemtype / category code
411 $sth->execute( $cat_borrower, $type, $branch_borrower );
412 my $result = $sth->fetchrow_hashref;
413 # warn "$cat_borrower, $type, $branch_borrower = ".Data::Dumper::Dumper($result);
415 if ( $result->{maxissueqty} ne '' ) {
416 # warn "checking on everything set";
417 $sth2->execute( $borrower->{'borrowernumber'}, $type );
418 my $alreadyissued = $sth2->fetchrow;
419 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
420 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch/category/itemtype failed)" );
422 # now checking for total
423 $sth->execute( $cat_borrower, '', $branch_borrower );
424 my $result = $sth->fetchrow_hashref;
425 if ( $result->{maxissueqty} ne '*' ) {
426 $sth2->execute( $borrower->{'borrowernumber'}, $type );
427 my $alreadyissued = $sth2->fetchrow;
428 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
429 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch/category/total failed)" );
434 # check the 2 parameters (branch / itemtype / default categorycode
435 $sth->execute( '*', $type, $branch_borrower );
436 my $result = $sth->fetchrow_hashref;
437 # warn "*, $type, $branch_borrower = ".Data::Dumper::Dumper($result);
439 if ( $result->{maxissueqty} ne '' ) {
440 # warn "checking on 2 parameters (default categorycode)";
441 $sth2->execute( $borrower->{'borrowernumber'}, $type );
442 my $alreadyissued = $sth2->fetchrow;
443 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
444 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch / default category / itemtype failed)" );
446 # now checking for total
447 $sth->execute( '*', '*', $branch_borrower );
448 my $result = $sth->fetchrow_hashref;
449 if ( $result->{maxissueqty} ne '' ) {
450 $sth2->execute( $borrower->{'borrowernumber'}, $type );
451 my $alreadyissued = $sth2->fetchrow;
452 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
453 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on branch / default category / total failed)" );
458 # check the 1 parameters (default branch / itemtype / categorycode
459 $sth->execute( $cat_borrower, $type, '*' );
460 my $result = $sth->fetchrow_hashref;
461 # warn "$cat_borrower, $type, * = ".Data::Dumper::Dumper($result);
463 if ( $result->{maxissueqty} ne '' ) {
464 # warn "checking on 1 parameter (default branch + categorycode)";
465 $sth2->execute( $borrower->{'borrowernumber'}, $type );
466 my $alreadyissued = $sth2->fetchrow;
467 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
468 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch/category/itemtype failed)" );
470 # now checking for total
471 $sth->execute( $cat_borrower, '*', '*' );
472 my $result = $sth->fetchrow_hashref;
473 if ( $result->{maxissueqty} ne '' ) {
474 $sth2->execute( $borrower->{'borrowernumber'}, $type );
475 my $alreadyissued = $sth2->fetchrow;
476 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
477 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / category / total failed)" );
482 # check the 0 parameters (default branch / itemtype / default categorycode
483 $sth->execute( '*', $type, '*' );
484 my $result = $sth->fetchrow_hashref;
485 # warn "*, $type, * = ".Data::Dumper::Dumper($result);
487 if ( $result->{maxissueqty} ne '' ) {
488 # warn "checking on default branch and default categorycode";
489 $sth2->execute( $borrower->{'borrowernumber'}, $type );
490 my $alreadyissued = $sth2->fetchrow;
491 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
492 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / default category / itemtype failed)" );
494 # now checking for total
495 $sth->execute( '*', '*', '*' );
496 my $result = $sth->fetchrow_hashref;
497 if ( $result->{maxissueqty} ne '' ) {
498 $sth2->execute( $borrower->{'borrowernumber'}, $type );
499 my $alreadyissued = $sth2->fetchrow;
500 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
501 return ( "$alreadyissued / ".( $result->{maxissueqty} + 0 )." (rule on default branch / default category / total failed)" );
506 #OK, the patron can issue !!!
512 @issues = &itemissues($biblioitemnumber, $biblio);
514 Looks up information about who has borrowed the bookZ<>(s) with the
515 given biblioitemnumber.
517 C<$biblio> is ignored.
519 C<&itemissues> returns an array of references-to-hash. The keys
520 include the fields from the C<items> table in the Koha database.
521 Additional keys include:
527 If the item is currently on loan, this gives the due date.
529 If the item is not on loan, then this is either "Available" or
530 "Cancelled", if the item has been withdrawn.
534 If the item is currently on loan, this gives the card number of the
535 patron who currently has the item.
537 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
539 These give the timestamp for the last three times the item was
542 =item C<card0>, C<card1>, C<card2>
544 The card number of the last three patrons who borrowed this item.
546 =item C<borrower0>, C<borrower1>, C<borrower2>
548 The borrower number of the last three patrons who borrowed this item.
556 my ( $bibitem, $biblio ) = @_;
557 my $dbh = C4::Context->dbh;
559 # FIXME - If this function die()s, the script will abort, and the
560 # user won't get anything; depending on how far the script has
561 # gotten, the user might get a blank page. It would be much better
562 # to at least print an error message. The easiest way to do this
563 # is to set $SIG{__DIE__}.
565 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
570 $sth->execute($bibitem) || die $sth->errstr;
572 while ( my $data = $sth->fetchrow_hashref ) {
574 # Find out who currently has this item.
575 # FIXME - Wouldn't it be better to do this as a left join of
576 # some sort? Currently, this code assumes that if
577 # fetchrow_hashref() fails, then the book is on the shelf.
578 # fetchrow_hashref() can fail for any number of reasons (e.g.,
579 # database server crash), not just because no items match the
581 my $sth2 = $dbh->prepare(
582 "SELECT * FROM issues
583 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
585 AND returndate IS NULL
589 $sth2->execute( $data->{'itemnumber'} );
590 if ( my $data2 = $sth2->fetchrow_hashref ) {
591 $data->{'date_due'} = $data2->{'date_due'};
592 $data->{'card'} = $data2->{'cardnumber'};
593 $data->{'borrower'} = $data2->{'borrowernumber'};
596 if ( $data->{'wthdrawn'} eq '1' ) {
597 $data->{'date_due'} = 'Cancelled';
600 $data->{'date_due'} = 'Available';
606 # Find the last 3 people who borrowed this item.
607 $sth2 = $dbh->prepare(
608 "SELECT * FROM issues
609 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
611 AND returndate IS NOT NULL
612 ORDER BY returndate DESC,timestamp DESC"
615 $sth2->execute( $data->{'itemnumber'} );
616 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
617 { # FIXME : error if there is less than 3 pple borrowing this item
618 if ( my $data2 = $sth2->fetchrow_hashref ) {
619 $data->{"timestamp$i2"} = $data2->{'timestamp'};
620 $data->{"card$i2"} = $data2->{'cardnumber'};
621 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
626 $results[$i] = $data;
634 =head2 CanBookBeIssued
636 $issuingimpossible, $needsconfirmation =
637 CanBookBeIssued( $borrower, $barcode, $duedatespec, $inprocess );
638 C<$duedatespec> is a C4::Dates object.
639 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
643 sub CanBookBeIssued {
644 my ( $borrower, $barcode, $duedate, $inprocess ) = @_;
645 my %needsconfirmation; # filled with problems that needs confirmations
646 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
647 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
648 my $issue = GetItemIssue($item->{itemnumber});
649 my $dbh = C4::Context->dbh;
652 # DUE DATE is OK ? -- should already have checked.
654 #$issuingimpossible{INVALID_DATE} = 1 unless ($duedate);
659 if ( $borrower->{flags}->{GNA} ) {
660 $issuingimpossible{GNA} = 1;
662 if ( $borrower->{flags}->{'LOST'} ) {
663 $issuingimpossible{CARD_LOST} = 1;
665 if ( $borrower->{flags}->{'DBARRED'} ) {
666 $issuingimpossible{DEBARRED} = 1;
668 if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
669 $issuingimpossible{EXPIRED} = 1;
671 my @expirydate= split /-/,$borrower->{'dateexpiry'};
672 if($expirydate[0]==0 || $expirydate[1]==0|| $expirydate[2]==0 ||
673 Date_to_Days(Today) > Date_to_Days( @expirydate )) {
674 $issuingimpossible{EXPIRED} = 1;
683 C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->output('iso') );
684 if ( C4::Context->preference("IssuingInProcess") ) {
685 my $amountlimit = C4::Context->preference("noissuescharge");
686 if ( $amount > $amountlimit && !$inprocess ) {
687 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
689 elsif ( $amount <= $amountlimit && !$inprocess ) {
690 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
695 $needsconfirmation{DEBT} = $amount;
700 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
703 my $toomany = TooMany( $borrower, $item->{biblionumber}, $item );
704 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
709 unless ( $item->{barcode} ) {
710 $issuingimpossible{UNKNOWN_BARCODE} = 1;
712 if ( $item->{'notforloan'}
713 && $item->{'notforloan'} > 0 )
715 $issuingimpossible{NOT_FOR_LOAN} = 1;
717 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
719 $issuingimpossible{WTHDRAWN} = 1;
721 if ( $item->{'restricted'}
722 && $item->{'restricted'} == 1 )
724 $issuingimpossible{RESTRICTED} = 1;
726 if ( C4::Context->preference("IndependantBranches") ) {
727 my $userenv = C4::Context->userenv;
728 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
729 $issuingimpossible{NOTSAMEBRANCH} = 1
730 if ( $item->{C4::Context->preference("HomeOrHoldingbranch")} ne $userenv->{branch} );
735 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
737 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
740 # Already issued to current borrower. Ask whether the loan should
742 my ($CanBookBeRenewed) = CanBookBeRenewed(
743 $borrower->{'borrowernumber'},
744 $item->{'itemnumber'}
746 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
747 $issuingimpossible{NO_MORE_RENEWALS} = 1;
750 $needsconfirmation{RENEW_ISSUE} = 1;
753 elsif ($issue->{borrowernumber}) {
755 # issued to someone else
756 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
758 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
759 $needsconfirmation{ISSUED_TO_ANOTHER} =
760 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
763 # See if the item is on reserve.
764 my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
766 my $resbor = $res->{'borrowernumber'};
767 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
770 # The item is on reserve and waiting, but has been
771 # reserved by some other patron.
772 my ( $resborrower, $flags ) =
773 GetMemberDetails( $resbor, 0 );
774 my $branches = GetBranches();
776 $branches->{ $res->{'branchcode'} }->{'branchname'};
777 $needsconfirmation{RESERVE_WAITING} =
778 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
780 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
782 elsif ( $restype eq "Reserved" ) {
784 # The item is on reserve for someone else.
785 my ( $resborrower, $flags ) =
786 GetMemberDetails( $resbor, 0 );
787 my $branches = GetBranches();
789 $branches->{ $res->{'branchcode'} }->{'branchname'};
790 $needsconfirmation{RESERVED} =
791 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
794 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" ) {
795 if ( $borrower->{'categorycode'} eq 'W' ) {
796 my %issuingimpossible;
797 return ( \%issuingimpossible, \%needsconfirmation );
799 return ( \%issuingimpossible, \%needsconfirmation );
802 return ( \%issuingimpossible, \%needsconfirmation );
808 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
810 &AddIssue($borrower,$barcode,$date)
814 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
816 =item C<$barcode> is the bar code of the book being issued.
818 =item C<$date> contains the max date of return. calculated if empty.
820 AddIssue does the following things :
821 - step 01: check that there is a borrowernumber & a barcode provided
822 - check for RENEWAL (book issued & being issued to the same patron)
823 - renewal YES = Calculate Charge & renew
825 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
827 - fill reserve if reserve to this patron
828 - cancel reserve or not, otherwise
829 * TRANSFERT PENDING ?
830 - complete the transfert
838 my ( $borrower, $barcode, $date, $cancelreserve ) = @_;
839 my $dbh = C4::Context->dbh;
840 my $barcodecheck=CheckValidBarcode($barcode);
841 if ($borrower and $barcode and $barcodecheck ne '0'){
842 # my ($borrower, $flags) = &GetMemberDetails($borrowernumber, 0);
843 # find which item we issue
844 my $item = GetItem('', $barcode);
847 # get actual issuing if there is one
848 my $actualissue = GetItemIssue( $item->{itemnumber});
850 # get biblioinformation for this item
851 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
854 # check if we just renew the issue.
856 if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
858 $borrower->{'borrowernumber'},
859 $item->{'itemnumber'}
865 if ( $actualissue->{borrowernumber}) {
866 # This book is currently on loan, but not to the person
867 # who wants to borrow it now. mark it returned before issuing to the new borrower
870 C4::Context->userenv->{'branch'}
874 # See if the item is on reserve.
875 my ( $restype, $res ) =
876 C4::Reserves::CheckReserves( $item->{'itemnumber'} );
878 my $resbor = $res->{'borrowernumber'};
879 if ( $resbor eq $borrower->{'borrowernumber'} ) {
881 # The item is reserved by the current patron
882 ModReserveFill($res);
884 elsif ( $restype eq "Waiting" ) {
887 # The item is on reserve and waiting, but has been
888 # reserved by some other patron.
889 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
890 my $branches = GetBranches();
892 $branches->{ $res->{'branchcode'} }->{'branchname'};
894 elsif ( $restype eq "Reserved" ) {
897 # The item is reserved by someone else.
898 my ( $resborrower, $flags ) =
899 GetMemberDetails( $resbor, 0 );
900 my $branches = GetBranches();
902 $branches->{ $res->{'branchcode'} }->{'branchname'};
903 if ($cancelreserve) { # cancel reserves on this item
904 CancelReserve( 0, $res->{'itemnumber'},
905 $res->{'borrowernumber'} );
908 if ($cancelreserve) {
909 CancelReserve( $res->{'biblionumber'}, 0,
910 $res->{'borrowernumber'} );
913 # set waiting reserve to first in reserve queue as book isn't waiting now
916 $res->{'biblionumber'},
917 $res->{'borrowernumber'},
923 # Starting process for transfer job (checking transfert and validate it if we have one)
924 my ($datesent) = GetTransfers($item->{'itemnumber'});
926 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
929 "UPDATE branchtransfers
930 SET datearrived = now(),
932 comments = 'Forced branchtransfert'
933 WHERE itemnumber= ? AND datearrived IS NULL"
935 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
939 # Record in the database the fact that the book was issued.
943 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
950 my $itype=(C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ;
951 my $loanlength = GetLoanLength(
952 $borrower->{'categorycode'},
954 $borrower->{'branchcode'}
956 $datedue = time + ($loanlength) * 86400;
957 my @datearr = localtime($datedue);
958 $dateduef = C4::Dates->new( sprintf("%04d-%02d-%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3]), 'iso');
959 $dateduef=CheckValidDatedue($dateduef,$item->{'itemnumber'},C4::Context->userenv->{'branch'});
961 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
962 if ( C4::Context->preference('ReturnBeforeExpiry') && $dateduef gt $borrower->{dateexpiry} ) {
963 $dateduef = $borrower->{dateexpiry};
967 $borrower->{'borrowernumber'},
968 $item->{'itemnumber'},
969 strftime( "%Y-%m-%d", localtime ),$dateduef->output('iso'), C4::Context->userenv->{'branch'}
975 "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed = now(), onloan = ? WHERE itemnumber=?");
978 C4::Context->userenv->{'branch'},
979 $dateduef->output('iso'),
980 $item->{'itemnumber'}
983 &ModDateLastSeen( $item->{'itemnumber'} );
984 my $record = GetMarcItem( $item->{'biblionumber'}, $item->{'itemnumber'} );
985 my $frameworkcode = GetFrameworkCode( $item->{'biblionumber'} );
986 ModItemInMarc( $record, $item->{'biblionumber'}, $item->{'itemnumber'}, $frameworkcode );
987 # If it costs to borrow this book, charge it to the patron's account.
988 my ( $charge, $itemtype ) = GetIssuingCharges(
989 $item->{'itemnumber'},
990 $borrower->{'borrowernumber'}
994 $item->{'itemnumber'},
995 $borrower->{'borrowernumber'}, $charge
997 $item->{'charge'} = $charge;
1000 # Record the fact that this book was issued.
1002 C4::Context->userenv->{'branch'},
1004 '', $item->{'itemnumber'},
1005 $item->{'itemtype'}, $borrower->{'borrowernumber'}
1009 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'})
1010 if C4::Context->preference("IssueLog");
1015 =head2 GetLoanLength
1017 Get loan length for an itemtype, a borrower type and a branch
1019 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1024 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1025 my $dbh = C4::Context->dbh;
1028 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
1031 # try to find issuelength & return the 1st available.
1032 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1033 $sth->execute( $borrowertype, $itemtype, $branchcode );
1034 my $loanlength = $sth->fetchrow_hashref;
1035 return $loanlength->{issuelength}
1036 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1038 $sth->execute( $borrowertype, $itemtype, "*" );
1039 $loanlength = $sth->fetchrow_hashref;
1040 return $loanlength->{issuelength}
1041 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1043 $sth->execute( $borrowertype, "*", $branchcode );
1044 $loanlength = $sth->fetchrow_hashref;
1045 return $loanlength->{issuelength}
1046 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1048 $sth->execute( "*", $itemtype, $branchcode );
1049 $loanlength = $sth->fetchrow_hashref;
1050 return $loanlength->{issuelength}
1051 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1053 $sth->execute( $borrowertype, "*", "*" );
1054 $loanlength = $sth->fetchrow_hashref;
1055 return $loanlength->{issuelength}
1056 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1058 $sth->execute( "*", "*", $branchcode );
1059 $loanlength = $sth->fetchrow_hashref;
1060 return $loanlength->{issuelength}
1061 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1063 $sth->execute( "*", $itemtype, "*" );
1064 $loanlength = $sth->fetchrow_hashref;
1065 return $loanlength->{issuelength}
1066 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1068 $sth->execute( "*", "*", "*" );
1069 $loanlength = $sth->fetchrow_hashref;
1070 return $loanlength->{issuelength}
1071 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1073 # if no rule is set => 21 days (hardcoded)
1079 ($doreturn, $messages, $iteminformation, $borrower) =
1080 &AddReturn($barcode, $branch);
1084 C<$barcode> is the bar code of the book being returned. C<$branch> is
1085 the code of the branch where the book is being returned.
1087 C<&AddReturn> returns a list of four items:
1089 C<$doreturn> is true iff the return succeeded.
1091 C<$messages> is a reference-to-hash giving the reason for failure:
1097 No item with this barcode exists. The value is C<$barcode>.
1101 The book is not currently on loan. The value is C<$barcode>.
1103 =item C<IsPermanent>
1105 The book's home branch is a permanent collection. If you have borrowed
1106 this book, you are not allowed to return it. The value is the code for
1107 the book's home branch.
1111 This book has been withdrawn/cancelled. The value should be ignored.
1115 The item was reserved. The value is a reference-to-hash whose keys are
1116 fields from the reserves table of the Koha database, and
1117 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1118 either C<Waiting>, C<Reserved>, or 0.
1122 C<$borrower> is a reference-to-hash, giving information about the
1123 patron who last borrowed the book.
1128 my ( $barcode, $branch ) = @_;
1129 my $dbh = C4::Context->dbh;
1133 my $validTransfert = 0;
1134 my $reserveDone = 0;
1136 # get information on item
1137 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1138 unless ($iteminformation->{'itemnumber'} ) {
1139 $messages->{'BadBarcode'} = $barcode;
1143 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1144 $messages->{'NotIssued'} = $barcode;
1148 # check if the book is in a permanent collection....
1149 my $hbr = $iteminformation->{'homebranch'};
1150 my $branches = GetBranches();
1151 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1152 $messages->{'IsPermanent'} = $hbr;
1155 # check that the book has been cancelled
1156 if ( $iteminformation->{'wthdrawn'} ) {
1157 $messages->{'wthdrawn'} = 1;
1161 # new op dev : if the book returned in an other branch update the holding branch
1163 # update issues, thereby returning book (should push this out into another subroutine
1164 $borrower = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1166 # case of a return of document (deal with issues and holdingbranch)
1171 "UPDATE issues SET returndate = now() WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (returndate IS NULL)"
1173 $sth->execute( $borrower->{'borrowernumber'},
1174 $iteminformation->{'itemnumber'} );
1175 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1178 # continue to deal with returns cases, but not only if we have an issue
1180 # the holdingbranch is updated if the document is returned in an other location .
1181 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} ) {
1182 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1183 # reload iteminformation holdingbranch with the userenv value
1184 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1186 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1187 if ($iteminformation->{borrowernumber}){
1188 my $sth = $dbh->prepare("UPDATE items SET onloan = NULL where itemnumber = ?");
1189 $sth->execute($iteminformation->{'itemnumber'});
1191 my $record = GetMarcItem( $iteminformation->{'biblionumber'}, $iteminformation->{'itemnumber'} );
1192 my $frameworkcode = GetFrameworkCode( $iteminformation->{'biblionumber'} );
1193 ModItemInMarc( $record, $iteminformation->{'biblionumber'}, $iteminformation->{'itemnumber'}, $frameworkcode );
1195 ($borrower) = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1197 # fix up the accounts.....
1198 if ( $iteminformation->{'itemlost'} ) {
1199 $messages->{'WasLost'} = 1;
1202 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1203 # check if we have a transfer for this document
1204 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1206 # if we have a transfer to do, we update the line of transfers with the datearrived
1208 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1211 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1213 $sth->execute( $iteminformation->{'itemnumber'} );
1215 # 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'
1216 C4::Reserves::ModReserveStatus( $iteminformation->{'itemnumber'},'W' );
1219 $messages->{'WrongTransfer'} = $tobranch;
1220 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1222 $validTransfert = 1;
1225 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1226 # fix up the accounts.....
1227 if ($iteminformation->{'itemlost'}) {
1228 FixAccountForLostAndReturned($iteminformation, $borrower);
1229 $messages->{'WasLost'} = 1;
1231 # fix up the overdues in accounts...
1232 FixOverduesOnReturn( $borrower->{'borrowernumber'},
1233 $iteminformation->{'itemnumber'} );
1235 # find reserves.....
1236 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1237 my ( $resfound, $resrec ) =
1238 C4::Reserves::CheckReserves( $iteminformation->{'itemnumber'} );
1240 $resrec->{'ResFound'} = $resfound;
1241 $messages->{'ResFound'} = $resrec;
1246 # Record the fact that this book was returned.
1248 $branch, 'return', '0', '',
1249 $iteminformation->{'itemnumber'},
1250 $iteminformation->{'itemtype'},
1251 $borrower->{'borrowernumber'}
1254 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'})
1255 if C4::Context->preference("ReturnLog");
1257 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1258 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1260 if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1261 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1262 ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1263 $messages->{'WasTransfered'} = 1;
1264 warn "was transfered";
1268 return ( $doreturn, $messages, $iteminformation, $borrower );
1271 =head2 FixOverduesOnReturn
1273 &FixOverduesOnReturn($brn,$itm);
1275 C<$brn> borrowernumber
1279 internal function, called only by AddReturn
1283 sub FixOverduesOnReturn {
1284 my ( $borrowernumber, $item ) = @_;
1285 my $dbh = C4::Context->dbh;
1287 # check for overdue fine
1290 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1292 $sth->execute( $borrowernumber, $item );
1294 # alter fine to show that the book has been returned
1295 if ( my $data = $sth->fetchrow_hashref ) {
1298 "UPDATE accountlines SET accounttype='F' WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accountno = ?)"
1300 $usth->execute( $borrowernumber, $item, $data->{'accountno'} );
1307 =head2 FixAccountForLostAndReturned
1309 &FixAccountForLostAndReturned($iteminfo,$borrower);
1311 Calculates the charge for a book lost and returned (Not exported & used only once)
1313 C<$iteminfo> is a hashref to iteminfo. Only {itemnumber} is used.
1315 C<$borrower> is a hashref to borrower. Only {borrowernumber is used.
1317 Internal function, called by AddReturn
1321 sub FixAccountForLostAndReturned {
1322 my ($iteminfo, $borrower) = @_;
1324 my $dbh = C4::Context->dbh;
1325 my $itm = $iteminfo->{'itemnumber'};
1326 # check for charge made for lost book
1327 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE (itemnumber = ?) AND (accounttype='L' OR accounttype='Rep') ORDER BY date DESC");
1328 $sth->execute($itm);
1329 if (my $data = $sth->fetchrow_hashref) {
1330 # writeoff this amount
1332 my $amount = $data->{'amount'};
1333 my $acctno = $data->{'accountno'};
1335 if ($data->{'amountoutstanding'} == $amount) {
1336 $offset = $data->{'amount'};
1339 $offset = $amount - $data->{'amountoutstanding'};
1340 $amountleft = $data->{'amountoutstanding'} - $amount;
1342 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
1343 WHERE (borrowernumber = ?)
1344 AND (itemnumber = ?) AND (accountno = ?) ");
1345 $usth->execute($data->{'borrowernumber'},$itm,$acctno);
1347 #check if any credit is left if so writeoff other accounts
1348 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
1349 if ($amountleft < 0){
1352 if ($amountleft > 0){
1353 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
1354 AND (amountoutstanding >0) ORDER BY date");
1355 $msth->execute($data->{'borrowernumber'});
1356 # offset transactions
1359 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1360 if ($accdata->{'amountoutstanding'} < $amountleft) {
1362 $amountleft -= $accdata->{'amountoutstanding'};
1364 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1367 my $thisacct = $accdata->{'accountno'};
1368 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
1369 WHERE (borrowernumber = ?)
1370 AND (accountno=?)");
1371 $usth->execute($newamtos,$data->{'borrowernumber'},'$thisacct');
1373 $usth = $dbh->prepare("INSERT INTO accountoffsets
1374 (borrowernumber, accountno, offsetaccount, offsetamount)
1377 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
1382 if ($amountleft > 0){
1385 my $desc="Book Returned ".$iteminfo->{'barcode'};
1386 $usth = $dbh->prepare("INSERT INTO accountlines
1387 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1388 VALUES (?,?,now(),?,?,'CR',?)");
1389 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
1391 $usth = $dbh->prepare("INSERT INTO accountoffsets
1392 (borrowernumber, accountno, offsetaccount, offsetamount)
1394 $usth->execute($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset);
1396 $usth = $dbh->prepare("UPDATE items SET paidfor='' WHERE itemnumber=?");
1397 $usth->execute($itm);
1406 $issues = &GetItemIssue($itemnumber);
1408 Returns patrons currently having a book. nothing if item is not issued atm
1410 C<$itemnumber> is the itemnumber
1412 Returns an array of hashes
1416 my ( $itemnumber) = @_;
1417 return unless $itemnumber;
1418 my $dbh = C4::Context->dbh;
1422 my $today = POSIX::strftime("%Y%m%d", localtime);
1424 my $sth = $dbh->prepare(
1425 "SELECT * FROM issues
1426 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1428 issues.itemnumber=? AND returndate IS NULL ");
1429 $sth->execute($itemnumber);
1430 my $data = $sth->fetchrow_hashref;
1431 my $datedue = $data->{'date_due'};
1433 if ( $datedue < $today ) {
1434 $data->{'overdue'} = 1;
1436 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1441 =head2 GetItemIssues
1443 $issues = &GetItemIssues($itemnumber, $history);
1445 Returns patrons that have issued a book
1447 C<$itemnumber> is the itemnumber
1448 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1450 Returns an array of hashes
1454 my ( $itemnumber,$history ) = @_;
1455 my $dbh = C4::Context->dbh;
1459 my $today = POSIX::strftime("%Y%m%d", localtime);
1461 my $sth = $dbh->prepare(
1462 "SELECT * FROM issues
1463 LEFT JOIN borrowers ON borrowers.borrowernumber
1464 LEFT JOIN items ON items.itemnumber=issues.itemnumber
1466 issues.itemnumber=?".($history?"":" AND returndate IS NULL ").
1467 "ORDER BY issues.date_due DESC"
1469 $sth->execute($itemnumber);
1470 while ( my $data = $sth->fetchrow_hashref ) {
1471 my $datedue = $data->{'date_due'};
1473 if ( $datedue < $today ) {
1474 $data->{'overdue'} = 1;
1476 my $itemnumber = $data->{'itemnumber'};
1477 push @GetItemIssues, $data;
1480 return ( \@GetItemIssues );
1483 =head2 GetBiblioIssues
1485 $issues = GetBiblioIssues($biblionumber);
1487 this function get all issues from a biblionumber.
1490 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1491 tables issues and the firstname,surname & cardnumber from borrowers.
1495 sub GetBiblioIssues {
1496 my $biblionumber = shift;
1497 return undef unless $biblionumber;
1498 my $dbh = C4::Context->dbh;
1500 SELECT issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1502 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1503 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1504 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1505 LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1506 WHERE biblio.biblionumber = ?
1507 ORDER BY issues.timestamp
1509 my $sth = $dbh->prepare($query);
1510 $sth->execute($biblionumber);
1513 while ( my $data = $sth->fetchrow_hashref ) {
1514 push @issues, $data;
1519 =head2 CanBookBeRenewed
1521 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1523 Find out whether a borrowed item may be renewed.
1525 C<$dbh> is a DBI handle to the Koha database.
1527 C<$borrowernumber> is the borrower number of the patron who currently
1528 has the item on loan.
1530 C<$itemnumber> is the number of the item to renew.
1532 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1533 item must currently be on loan to the specified borrower; renewals
1534 must be allowed for the item's type; and the borrower must not have
1535 already renewed the loan.
1539 sub CanBookBeRenewed {
1541 # check renewal status
1542 my ( $borrowernumber, $itemnumber ) = @_;
1543 my $dbh = C4::Context->dbh;
1547 # Look in the issues table for this item, lent to this borrower,
1548 # and not yet returned.
1550 # FIXME - I think this function could be redone to use only one SQL call.
1551 my $sth1 = $dbh->prepare(
1552 "SELECT * FROM issues
1553 WHERE borrowernumber = ?
1555 AND returndate IS NULL"
1557 $sth1->execute( $borrowernumber, $itemnumber );
1558 if ( my $data1 = $sth1->fetchrow_hashref ) {
1560 # Found a matching item
1562 # See if this item may be renewed. This query is convoluted
1563 # because it's a bit messy: given the item number, we need to find
1564 # the biblioitem, which gives us the itemtype, which tells us
1565 # whether it may be renewed.
1566 my $sth2 = $dbh->prepare(
1567 "SELECT renewalsallowed FROM items
1568 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1569 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1570 WHERE items.itemnumber = ?
1573 $sth2->execute($itemnumber);
1574 if ( my $data2 = $sth2->fetchrow_hashref ) {
1575 $renews = $data2->{'renewalsallowed'};
1577 if ( $renews && $renews >= $data1->{'renewals'} ) {
1581 my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber);
1588 return ($renewokay);
1593 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1597 C<$borrowernumber> is the borrower number of the patron who currently
1600 C<$itemnumber> is the number of the item to renew.
1602 C<$datedue> can be used to set the due date. If C<$datedue> is the
1603 empty string, C<&AddRenewal> will calculate the due date automatically
1604 from the book's item type. If you wish to set the due date manually,
1605 C<$datedue> should be in the form YYYY-MM-DD.
1611 my ( $borrowernumber, $itemnumber, $branch ,$datedue ) = @_;
1612 my $dbh = C4::Context->dbh;
1614 # If the due date wasn't specified, calculate it by adding the
1615 # book's loan length to today's date.
1616 unless ( $datedue ) {
1618 my $biblio = GetBiblioFromItemNumber($itemnumber);
1619 my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 );
1620 my $loanlength = GetLoanLength(
1621 $borrower->{'categorycode'},
1622 (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ,
1623 $borrower->{'branchcode'}
1625 #FIXME -- choose issuer or borrower branch.
1626 #FIXME -- where's the calendar ?
1627 my ( $due_year, $due_month, $due_day ) =
1628 Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1629 $datedue = C4::Dates->new( "$due_year-$due_month-$due_day",'iso');
1630 $datedue=CheckValidDatedue($datedue,$itemnumber,$branch);
1633 # Find the issues record for this book
1635 $dbh->prepare("SELECT * FROM issues
1636 WHERE borrowernumber=?
1638 AND returndate IS NULL"
1640 $sth->execute( $borrowernumber, $itemnumber );
1641 my $issuedata = $sth->fetchrow_hashref;
1644 # Update the issues record to have the new due date, and a new count
1645 # of how many times it has been renewed.
1646 my $renews = $issuedata->{'renewals'} + 1;
1647 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1648 WHERE borrowernumber=?
1650 AND returndate IS NULL"
1652 $sth->execute( $datedue->output('iso'), $renews, $borrowernumber, $itemnumber );
1657 # Charge a new rental fee, if applicable?
1658 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1659 if ( $charge > 0 ) {
1660 my $accountno = getnextacctno( $borrowernumber );
1661 my $item = GetBiblioFromItemNumber($itemnumber);
1662 $sth = $dbh->prepare(
1663 "INSERT INTO accountlines
1664 (borrowernumber,accountno,date,amount,
1665 description,accounttype,amountoutstanding,
1667 VALUES (?,?,now(),?,?,?,?,?)"
1669 $sth->execute( $borrowernumber, $accountno, $charge,
1670 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1671 'Rent', $charge, $itemnumber );
1674 UpdateStats( $branch, 'renew', $charge, '', $itemnumber );
1678 # check renewal status
1679 my ($bornum,$itemno)=@_;
1680 my $dbh = C4::Context->dbh;
1682 my $renewsallowed = 0;
1684 # Look in the issues table for this item, lent to this borrower,
1685 # and not yet returned.
1687 # FIXME - I think this function could be redone to use only one SQL call.
1688 my $sth = $dbh->prepare("select * from issues
1689 where (borrowernumber = ?)
1690 and (itemnumber = ?)
1691 and returndate is null");
1692 $sth->execute($bornum,$itemno);
1693 my $data = $sth->fetchrow_hashref;
1694 $renewcount = $data->{'renewals'} if $data->{'renewals'};
1695 my $sth2 = $dbh->prepare("select renewalsallowed from items,biblioitems,itemtypes
1696 where (items.itemnumber = ?)
1697 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
1698 and (biblioitems.itemtype = itemtypes.itemtype)");
1699 $sth2->execute($itemno);
1700 my $data2 = $sth2->fetchrow_hashref();
1701 $renewsallowed = $data2->{'renewalsallowed'};
1702 $renewsleft = $renewsallowed - $renewcount;
1703 warn "Renewcount:$renewcount RenewsAll:$renewsallowed RenewLeft:$renewsleft";
1704 return ($renewcount,$renewsallowed,$renewsleft);
1706 =head2 GetIssuingCharges
1708 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1710 Calculate how much it would cost for a given patron to borrow a given
1711 item, including any applicable discounts.
1713 C<$itemnumber> is the item number of item the patron wishes to borrow.
1715 C<$borrowernumber> is the patron's borrower number.
1717 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1718 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1723 sub GetIssuingCharges {
1725 # calculate charges due
1726 my ( $itemnumber, $borrowernumber ) = @_;
1728 my $dbh = C4::Context->dbh;
1731 # Get the book's item type and rental charge (via its biblioitem).
1732 my $qcharge = "SELECT itemtypes.itemtype,rentalcharge FROM items
1733 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
1734 $qcharge .= (C4::Context->preference('item-level_itypes'))
1735 ? " LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
1736 : " LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
1738 $qcharge .= "WHERE items.itemnumber =?";
1740 my $sth1 = $dbh->prepare($qcharge);
1741 $sth1->execute($itemnumber);
1742 if ( my $data1 = $sth1->fetchrow_hashref ) {
1743 $item_type = $data1->{'itemtype'};
1744 $charge = $data1->{'rentalcharge'};
1745 my $q2 = "SELECT rentaldiscount FROM borrowers
1746 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1747 WHERE borrowers.borrowernumber = ?
1748 AND issuingrules.itemtype = ?";
1749 my $sth2 = $dbh->prepare($q2);
1750 $sth2->execute( $borrowernumber, $item_type );
1751 if ( my $data2 = $sth2->fetchrow_hashref ) {
1752 my $discount = $data2->{'rentaldiscount'};
1753 if ( $discount eq 'NULL' ) {
1756 $charge = ( $charge * ( 100 - $discount ) ) / 100;
1762 return ( $charge, $item_type );
1765 =head2 AddIssuingCharge
1767 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1771 sub AddIssuingCharge {
1772 my ( $itemnumber, $borrowernumber, $charge ) = @_;
1773 my $dbh = C4::Context->dbh;
1774 my $nextaccntno = getnextacctno( $borrowernumber );
1776 INSERT INTO accountlines
1777 (borrowernumber, itemnumber, accountno,
1778 date, amount, description, accounttype,
1780 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1782 my $sth = $dbh->prepare($query);
1783 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1789 GetTransfers($itemnumber);
1794 my ($itemnumber) = @_;
1796 my $dbh = C4::Context->dbh;
1802 FROM branchtransfers
1803 WHERE itemnumber = ?
1804 AND datearrived IS NULL
1806 my $sth = $dbh->prepare($query);
1807 $sth->execute($itemnumber);
1808 my @row = $sth->fetchrow_array();
1814 =head2 GetTransfersFromTo
1816 @results = GetTransfersFromTo($frombranch,$tobranch);
1818 Returns the list of pending transfers between $from and $to branch
1822 sub GetTransfersFromTo {
1823 my ( $frombranch, $tobranch ) = @_;
1824 return unless ( $frombranch && $tobranch );
1825 my $dbh = C4::Context->dbh;
1827 SELECT itemnumber,datesent,frombranch
1828 FROM branchtransfers
1831 AND datearrived IS NULL
1833 my $sth = $dbh->prepare($query);
1834 $sth->execute( $frombranch, $tobranch );
1837 while ( my $data = $sth->fetchrow_hashref ) {
1838 push @gettransfers, $data;
1841 return (@gettransfers);
1844 =head2 DeleteTransfer
1846 &DeleteTransfer($itemnumber);
1850 sub DeleteTransfer {
1851 my ($itemnumber) = @_;
1852 my $dbh = C4::Context->dbh;
1853 my $sth = $dbh->prepare(
1854 "DELETE FROM branchtransfers
1856 AND datearrived IS NULL "
1858 $sth->execute($itemnumber);
1862 =head2 AnonymiseIssueHistory
1864 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1866 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1867 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1869 return the number of affected rows.
1873 sub AnonymiseIssueHistory {
1875 my $borrowernumber = shift;
1876 my $dbh = C4::Context->dbh;
1879 SET borrowernumber = NULL
1880 WHERE returndate < '".$date."'
1881 AND borrowernumber IS NOT NULL
1883 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1884 my $rows_affected = $dbh->do($query);
1885 return $rows_affected;
1888 =head2 updateWrongTransfer
1890 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1892 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
1896 sub updateWrongTransfer {
1897 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1898 my $dbh = C4::Context->dbh;
1899 # first step validate the actual line of transfert .
1902 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1904 $sth->execute($FromLibrary,$itemNumber);
1907 # second step create a new line of branchtransfer to the right location .
1908 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1910 #third step changing holdingbranch of item
1911 UpdateHoldingbranch($FromLibrary,$itemNumber);
1914 =head2 UpdateHoldingbranch
1916 $items = UpdateHoldingbranch($branch,$itmenumber);
1917 Simple methode for updating hodlingbranch in items BDD line
1920 sub UpdateHoldingbranch {
1921 my ( $branch,$itmenumber ) = @_;
1922 my $dbh = C4::Context->dbh;
1923 # first step validate the actual line of transfert .
1926 "update items set holdingbranch = ? where itemnumber= ?"
1928 $sth->execute($branch,$itmenumber);
1933 =head2 CheckValidDatedue
1935 $newdatedue = CheckValidDatedue($date_due,$itemnumber,$branchcode);
1936 this function return a new date due after checked if it's a repeatable or special holiday
1937 C<$date_due> = returndate calculate with no day check
1938 C<$itemnumber> = itemnumber
1939 C<$branchcode> = localisation of issue
1941 # Why not create calendar object? -
1942 # TODO add 'duedate' option to useDaysMode .
1943 sub CheckValidDatedue {
1944 my ($date_due,$itemnumber,$branchcode)=@_;
1945 my @datedue=split('-',$date_due->output('iso'));
1946 my $years=$datedue[0];
1947 my $month=$datedue[1];
1948 my $day=$datedue[2];
1950 for (my $i=0;$i<2;$i++){
1951 $dow=Day_of_Week($years,$month,$day);
1952 ($dow=0) if ($dow>6);
1953 my $result=CheckRepeatableHolidays($itemnumber,$dow,$branchcode);
1954 my $countspecial=CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
1955 my $countspecialrepeatable=CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
1956 if (($result ne '0') or ($countspecial ne '0') or ($countspecialrepeatable ne '0') ){
1958 (($years,$month,$day) = Add_Delta_Days($years,$month,$day, 1))if ($i ne '1');
1961 my $newdatedue=C4::Dates->new( $years."-".$month."-".$day,'iso');
1964 =head2 CheckRepeatableHolidays
1966 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
1967 this function check if the date due is a repeatable holiday
1968 C<$date_due> = returndate calculate with no day check
1969 C<$itemnumber> = itemnumber
1970 C<$branchcode> = localisation of issue
1974 sub CheckRepeatableHolidays{
1975 my($itemnumber,$week_day,$branchcode)=@_;
1976 my $dbh = C4::Context->dbh;
1977 my $query = qq|SELECT count(*)
1978 FROM repeatable_holidays
1981 my $sth = $dbh->prepare($query);
1982 $sth->execute($branchcode,$week_day);
1983 my $result=$sth->fetchrow;
1989 =head2 CheckSpecialHolidays
1991 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
1992 this function check if the date is a special holiday
1993 C<$years> = the years of datedue
1994 C<$month> = the month of datedue
1995 C<$day> = the day of datedue
1996 C<$itemnumber> = itemnumber
1997 C<$branchcode> = localisation of issue
1999 sub CheckSpecialHolidays{
2000 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
2001 my $dbh = C4::Context->dbh;
2002 my $query=qq|SELECT count(*)
2003 FROM `special_holidays`
2009 my $sth = $dbh->prepare($query);
2010 $sth->execute($years,$month,$day,$branchcode);
2011 my $countspecial=$sth->fetchrow ;
2013 return $countspecial;
2016 =head2 CheckRepeatableSpecialHolidays
2018 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2019 this function check if the date is a repeatble special holidays
2020 C<$month> = the month of datedue
2021 C<$day> = the day of datedue
2022 C<$itemnumber> = itemnumber
2023 C<$branchcode> = localisation of issue
2025 sub CheckRepeatableSpecialHolidays{
2026 my ($month,$day,$itemnumber,$branchcode) = @_;
2027 my $dbh = C4::Context->dbh;
2028 my $query=qq|SELECT count(*)
2029 FROM `repeatable_holidays`
2034 my $sth = $dbh->prepare($query);
2035 $sth->execute($month,$day,$branchcode);
2036 my $countspecial=$sth->fetchrow ;
2038 return $countspecial;
2043 sub CheckValidBarcode{
2045 my $dbh = C4::Context->dbh;
2046 my $query=qq|SELECT count(*)
2050 my $sth = $dbh->prepare($query);
2051 $sth->execute($barcode);
2052 my $exist=$sth->fetchrow ;
2063 Koha Developement team <info@koha.org>