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
40 use POSIX qw(strftime);
41 use C4::Branch; # GetBranches
42 use C4::Log; # logaction
44 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
46 # set the version for version checking
47 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
51 C4::Circulation - Koha circulation module
59 The functions in this module deal with circulation, issues, and
60 returns, as well as general information about the library.
61 Also deals with stocktaking.
69 # FIXME subs that should probably be elsewhere
75 # 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.
369 # returns an array with errors if any
372 my $borrower = shift;
373 my $biblionumber = shift;
374 my $cat_borrower = $borrower->{'categorycode'};
375 my $branch_borrower = $borrower->{'branchcode'};
376 my $dbh = C4::Context->dbh;
379 $dbh->prepare('SELECT itemtype FROM biblioitems WHERE biblionumber = ?');
380 $sth->execute($biblionumber);
381 my $type = $sth->fetchrow;
384 'SELECT * FROM issuingrules
385 WHERE categorycode = ?
392 "SELECT COUNT(*) FROM issues i, biblioitems s1, items s2
393 WHERE i.borrowernumber = ?
394 AND i.returndate IS NULL
395 AND i.itemnumber = s2.itemnumber
396 AND s1.itemtype LIKE ?
397 AND s1.biblioitemnumber = s2.biblioitemnumber"
401 'SELECT COUNT(*) FROM issues
402 WHERE borrowernumber = ?
403 AND returndate IS NULL'
407 # check the 3 parameters
408 $sth->execute( $cat_borrower, $type, $branch_borrower );
409 my $result = $sth->fetchrow_hashref;
411 # Currently, using defined($result) ie on an entire hash reports whether memory
412 # for that aggregate has ever been allocated. As $result is used all over the place
413 # it would rarely return as undefined.
414 if ( defined( $result->{maxissueqty} ) ) {
415 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
416 my $alreadyissued = $sth2->fetchrow;
417 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
418 return ( "a $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
426 $sth->execute( $cat_borrower, $type, "" );
427 $result = $sth->fetchrow_hashref;
428 if ( defined( $result->{maxissueqty} ) ) {
429 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
430 my $alreadyissued = $sth2->fetchrow;
431 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
432 return ( "b $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
439 # check for itemtype=*
440 # $sth->execute( $cat_borrower, "*", $branch_borrower );
441 # $result = $sth->fetchrow_hashref;
442 # if ( defined( $result->{maxissueqty} ) ) {
443 # $sth3->execute( $borrower->{'borrowernumber'} );
444 # my ($alreadyissued) = $sth3->fetchrow;
445 # if ( $result->{'maxissueqty'} <= $alreadyissued ) {
446 # warn "HERE : $alreadyissued / ($result->{maxissueqty} for $borrower->{'borrowernumber'}";
447 # return ( "c $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
454 # check for borrowertype=*
455 $sth->execute( "*", $type, $branch_borrower );
456 $result = $sth->fetchrow_hashref;
457 if ( defined( $result->{maxissueqty} ) ) {
458 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
459 my $alreadyissued = $sth2->fetchrow;
460 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
461 return ( "d $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
468 $sth->execute( "*", "*", $branch_borrower );
469 $result = $sth->fetchrow_hashref;
470 if ( defined( $result->{maxissueqty} ) ) {
471 $sth3->execute( $borrower->{'borrowernumber'} );
472 my $alreadyissued = $sth3->fetchrow;
473 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
474 return ( "e $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
481 $sth->execute( "*", $type, "" );
482 $result = $sth->fetchrow_hashref;
483 if ( defined( $result->{maxissueqty} ) && $result->{maxissueqty} >= 0 ) {
484 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
485 my $alreadyissued = $sth2->fetchrow;
486 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
487 return ( "f $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
494 # $sth->execute( $cat_borrower, "*", "" );
495 # $result = $sth->fetchrow_hashref;
496 # if ( defined( $result->{maxissueqty} ) ) {
497 # $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
498 # my $alreadyissued = $sth2->fetchrow;
499 # if ( $result->{'maxissueqty'} <= $alreadyissued ) {
500 # return ( "g $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
507 # $sth->execute( "*", "*", "" );
508 # $result = $sth->fetchrow_hashref;
509 # if ( defined( $result->{maxissueqty} ) ) {
510 # $sth3->execute( $borrower->{'borrowernumber'} );
511 # my $alreadyissued = $sth3->fetchrow;
512 # if ( $result->{'maxissueqty'} <= $alreadyissued ) {
513 # return ( "h $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
524 @issues = &itemissues($biblioitemnumber, $biblio);
526 Looks up information about who has borrowed the bookZ<>(s) with the
527 given biblioitemnumber.
529 C<$biblio> is ignored.
531 C<&itemissues> returns an array of references-to-hash. The keys
532 include the fields from the C<items> table in the Koha database.
533 Additional keys include:
539 If the item is currently on loan, this gives the due date.
541 If the item is not on loan, then this is either "Available" or
542 "Cancelled", if the item has been withdrawn.
546 If the item is currently on loan, this gives the card number of the
547 patron who currently has the item.
549 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
551 These give the timestamp for the last three times the item was
554 =item C<card0>, C<card1>, C<card2>
556 The card number of the last three patrons who borrowed this item.
558 =item C<borrower0>, C<borrower1>, C<borrower2>
560 The borrower number of the last three patrons who borrowed this item.
568 my ( $bibitem, $biblio ) = @_;
569 my $dbh = C4::Context->dbh;
571 # FIXME - If this function die()s, the script will abort, and the
572 # user won't get anything; depending on how far the script has
573 # gotten, the user might get a blank page. It would be much better
574 # to at least print an error message. The easiest way to do this
575 # is to set $SIG{__DIE__}.
577 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
582 $sth->execute($bibitem) || die $sth->errstr;
584 while ( my $data = $sth->fetchrow_hashref ) {
586 # Find out who currently has this item.
587 # FIXME - Wouldn't it be better to do this as a left join of
588 # some sort? Currently, this code assumes that if
589 # fetchrow_hashref() fails, then the book is on the shelf.
590 # fetchrow_hashref() can fail for any number of reasons (e.g.,
591 # database server crash), not just because no items match the
593 my $sth2 = $dbh->prepare(
594 "SELECT * FROM issues
595 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
597 AND returndate IS NULL
601 $sth2->execute( $data->{'itemnumber'} );
602 if ( my $data2 = $sth2->fetchrow_hashref ) {
603 $data->{'date_due'} = $data2->{'date_due'};
604 $data->{'card'} = $data2->{'cardnumber'};
605 $data->{'borrower'} = $data2->{'borrowernumber'};
608 if ( $data->{'wthdrawn'} eq '1' ) {
609 $data->{'date_due'} = 'Cancelled';
612 $data->{'date_due'} = 'Available';
618 # Find the last 3 people who borrowed this item.
619 $sth2 = $dbh->prepare(
620 "SELECT * FROM issues
621 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
623 AND returndate IS NOT NULL
624 ORDER BY returndate DESC,timestamp DESC"
627 # $sth2 = $dbh->prepare("
630 # LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
631 # WHERE itemnumber = ?
632 # AND returndate is not NULL
633 # ORDER BY returndate DESC,timestamp DESC
636 $sth2->execute( $data->{'itemnumber'} );
637 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
638 { # FIXME : error if there is less than 3 pple borrowing this item
639 if ( my $data2 = $sth2->fetchrow_hashref ) {
640 $data->{"timestamp$i2"} = $data2->{'timestamp'};
641 $data->{"card$i2"} = $data2->{'cardnumber'};
642 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
647 $results[$i] = $data;
655 =head2 CanBookBeIssued
657 $issuingimpossible, $needsconfirmation =
658 CanBookBeIssued( $borrower, $barcode, $year, $month, $day, $inprocess );
660 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
664 sub CanBookBeIssued {
665 my ( $borrower, $barcode, $year, $month, $day, $inprocess ) = @_;
666 my %needsconfirmation; # filled with problems that needs confirmations
667 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
668 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
669 my $issue = GetItemIssue($item->{itemnumber});
670 my $dbh = C4::Context->dbh;
675 my ( $duedate, $invalidduedate ) = fixdate( $year, $month, $day );
676 $issuingimpossible{INVALID_DATE} = 1 if ($invalidduedate);
681 if ( $borrower->{flags}->{GNA} ) {
682 $issuingimpossible{GNA} = 1;
684 if ( $borrower->{flags}->{'LOST'} ) {
685 $issuingimpossible{CARD_LOST} = 1;
687 if ( $borrower->{flags}->{'DBARRED'} ) {
688 $issuingimpossible{DEBARRED} = 1;
690 if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
691 $issuingimpossible{EXPIRED} = 1;
693 if ( Date_to_Days(Today) >
694 Date_to_Days( split "-", $borrower->{'dateexpiry'} ) )
696 $issuingimpossible{EXPIRED} = 1;
705 GetMemberAccountRecords( $borrower->{'borrowernumber'}, $duedate );
706 if ( C4::Context->preference("IssuingInProcess") ) {
707 my $amountlimit = C4::Context->preference("noissuescharge");
708 if ( $amount > $amountlimit && !$inprocess ) {
709 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
711 elsif ( $amount <= $amountlimit && !$inprocess ) {
712 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
717 $needsconfirmation{DEBT} = $amount;
722 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
724 my $toomany = TooMany( $borrower, $item->{biblionumber} );
725 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
730 unless ( $item->{barcode} ) {
731 $issuingimpossible{UNKNOWN_BARCODE} = 1;
733 if ( $item->{'notforloan'}
734 && $item->{'notforloan'} > 0 )
736 $issuingimpossible{NOT_FOR_LOAN} = 1;
738 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
740 $issuingimpossible{WTHDRAWN} = 1;
742 if ( $item->{'restricted'}
743 && $item->{'restricted'} == 1 )
745 $issuingimpossible{RESTRICTED} = 1;
747 if ( C4::Context->preference("IndependantBranches") ) {
748 my $userenv = C4::Context->userenv;
749 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
750 $issuingimpossible{NOTSAMEBRANCH} = 1
751 if ( $item->{'holdingbranch'} ne $userenv->{branch} );
756 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
758 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
761 # Already issued to current borrower. Ask whether the loan should
763 my ($CanBookBeRenewed) = CanBookBeRenewed(
764 $borrower->{'borrowernumber'},
765 $item->{'itemnumber'}
767 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
768 $issuingimpossible{NO_MORE_RENEWALS} = 1;
772 # $needsconfirmation{RENEW_ISSUE} = 1;
775 elsif ($issue->{borrowernumber}) {
777 # issued to someone else
778 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
780 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
781 $needsconfirmation{ISSUED_TO_ANOTHER} =
782 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
785 # See if the item is on reserve.
786 my ( $restype, $res ) = CheckReserves( $item->{'itemnumber'} );
788 my $resbor = $res->{'borrowernumber'};
789 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
792 # The item is on reserve and waiting, but has been
793 # reserved by some other patron.
794 my ( $resborrower, $flags ) =
795 GetMemberDetails( $resbor, 0 );
796 my $branches = GetBranches();
798 $branches->{ $res->{'branchcode'} }->{'branchname'};
799 $needsconfirmation{RESERVE_WAITING} =
800 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
802 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
804 elsif ( $restype eq "Reserved" ) {
806 # The item is on reserve for someone else.
807 my ( $resborrower, $flags ) =
808 GetMemberDetails( $resbor, 0 );
809 my $branches = GetBranches();
811 $branches->{ $res->{'branchcode'} }->{'branchname'};
812 $needsconfirmation{RESERVED} =
813 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
816 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" )
818 if ( $borrower->{'categorycode'} eq 'W' ) {
819 my %issuingimpossible;
820 return ( \%issuingimpossible, \%needsconfirmation );
823 return ( \%issuingimpossible, \%needsconfirmation );
827 return ( \%issuingimpossible, \%needsconfirmation );
833 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
835 &AddIssue($borrower,$barcode,$date)
839 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
841 =item C<$barcode> is the bar code of the book being issued.
843 =item C<$date> contains the max date of return. calculated if empty.
845 AddIssue does the following things :
846 - step 01: check that there is a borrowernumber & a barcode provided
847 - check for RENEWAL (book issued & being issued to the same patron)
848 - renewal YES = Calculate Charge & renew
850 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
852 - fill reserve if reserve to this patron
853 - cancel reserve or not, otherwise
854 * TRANSFERT PENDING ?
855 - complete the transfert
863 my ( $borrower, $barcode, $date, $cancelreserve ) = @_;
864 my $dbh = C4::Context->dbh;
865 my $barcodecheck=CheckValidBarcode($barcode);
866 if ($borrower and $barcode and $barcodecheck ne '0'){
867 # my ($borrower, $flags) = &GetMemberDetails($borrowernumber, 0);
868 # find which item we issue
869 my $item = GetItem('', $barcode);
871 # get actual issuing if there is one
872 my $actualissue = GetItemIssue( $item->{itemnumber});
874 # get biblioinformation for this item
875 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
878 # check if we just renew the issue.
880 if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
881 # we renew, do we need to add some charge ?
882 my ( $charge, $itemtype ) = GetIssuingCharges(
883 $item->{'itemnumber'},
884 $borrower->{'borrowernumber'}
888 $item->{'itemnumber'},
889 $borrower->{'borrowernumber'}, $charge
891 $item->{'charge'} = $charge;
894 C4::Context->userenv->{'branch'},
896 '', $item->{'itemnumber'},
897 $biblio->{'itemtype'}, $borrower->{'borrowernumber'}
900 $borrower->{'borrowernumber'},
901 $item->{'itemnumber'}
904 else {# it's NOT a renewal
905 if ( $actualissue->{borrowernumber}) {
906 # This book is currently on loan, but not to the person
907 # who wants to borrow it now. mark it returned before issuing to the new borrower
910 C4::Context->userenv->{'branch'}
914 # See if the item is on reserve.
915 my ( $restype, $res ) =
916 CheckReserves( $item->{'itemnumber'} );
918 my $resbor = $res->{'borrowernumber'};
919 if ( $resbor eq $borrower->{'borrowernumber'} ) {
921 # The item is reserved by the current patron
922 ModReserveFill($res);
924 elsif ( $restype eq "Waiting" ) {
927 # The item is on reserve and waiting, but has been
928 # reserved by some other patron.
929 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
930 my $branches = GetBranches();
932 $branches->{ $res->{'branchcode'} }->{'branchname'};
934 elsif ( $restype eq "Reserved" ) {
937 # The item is reserved by someone else.
938 my ( $resborrower, $flags ) =
939 GetMemberDetails( $resbor, 0 );
940 my $branches = GetBranches();
942 $branches->{ $res->{'branchcode'} }->{'branchname'};
943 if ($cancelreserve) { # cancel reserves on this item
944 CancelReserve( 0, $res->{'itemnumber'},
945 $res->{'borrowernumber'} );
948 if ($cancelreserve) {
949 CancelReserve( $res->{'biblionumber'}, 0,
950 $res->{'borrowernumber'} );
953 # set waiting reserve to first in reserve queue as book isn't waiting now
956 $res->{'biblionumber'},
957 $res->{'borrowernumber'},
963 # Starting process for transfer job (checking transfert and validate it if we have one)
964 my ($datesent) = GetTransfers($item->{'itemnumber'});
966 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
969 "UPDATE branchtransfers
970 SET datearrived = now(),
972 comments = 'Forced branchtransfert'
973 WHERE itemnumber= ? AND datearrived IS NULL"
975 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
979 # Record in the database the fact that the book was issued.
983 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
986 my $loanlength = GetLoanLength(
987 $borrower->{'categorycode'},
988 $biblio->{'itemtype'},
989 $borrower->{'branchcode'}
991 my $datedue = time + ($loanlength) * 86400;
992 my @datearr = localtime($datedue);
994 ( 1900 + $datearr[5] ) . "-"
995 . ( $datearr[4] + 1 ) . "-"
1000 $dateduef=CheckValidDatedue($dateduef,$item->{'itemnumber'},C4::Context->userenv->{'branch'});
1001 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
1002 if ( C4::Context->preference('ReturnBeforeExpiry')
1003 && $dateduef gt $borrower->{dateexpiry} )
1005 $dateduef = $borrower->{dateexpiry};
1008 $borrower->{'borrowernumber'},
1009 $item->{'itemnumber'},
1010 strftime( "%Y-%m-%d", localtime ),$dateduef, C4::Context->userenv->{'branch'}
1013 $item->{'issues'}++;
1016 "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed = now() WHERE itemnumber=?");
1019 C4::Context->userenv->{'branch'},
1020 $item->{'itemnumber'}
1023 &ModDateLastSeen( $item->{'itemnumber'} );
1024 # If it costs to borrow this book, charge it to the patron's account.
1025 my ( $charge, $itemtype ) = GetIssuingCharges(
1026 $item->{'itemnumber'},
1027 $borrower->{'borrowernumber'}
1029 if ( $charge > 0 ) {
1031 $item->{'itemnumber'},
1032 $borrower->{'borrowernumber'}, $charge
1034 $item->{'charge'} = $charge;
1037 # Record the fact that this book was issued.
1039 C4::Context->userenv->{'branch'},
1041 '', $item->{'itemnumber'},
1042 $item->{'itemtype'}, $borrower->{'borrowernumber'}
1046 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'})
1047 if C4::Context->preference("IssueLog");
1051 =head2 GetLoanLength
1053 Get loan length for an itemtype, a borrower type and a branch
1055 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1060 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1061 my $dbh = C4::Context->dbh;
1064 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=?"
1067 # try to find issuelength & return the 1st available.
1068 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1069 $sth->execute( $borrowertype, $itemtype, $branchcode );
1070 my $loanlength = $sth->fetchrow_hashref;
1071 return $loanlength->{issuelength}
1072 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1074 $sth->execute( $borrowertype, $itemtype, "" );
1075 $loanlength = $sth->fetchrow_hashref;
1076 return $loanlength->{issuelength}
1077 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1079 $sth->execute( $borrowertype, "*", $branchcode );
1080 $loanlength = $sth->fetchrow_hashref;
1081 return $loanlength->{issuelength}
1082 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1084 $sth->execute( "*", $itemtype, $branchcode );
1085 $loanlength = $sth->fetchrow_hashref;
1086 return $loanlength->{issuelength}
1087 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1089 $sth->execute( $borrowertype, "*", "" );
1090 $loanlength = $sth->fetchrow_hashref;
1091 return $loanlength->{issuelength}
1092 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1094 $sth->execute( "*", "*", $branchcode );
1095 $loanlength = $sth->fetchrow_hashref;
1096 return $loanlength->{issuelength}
1097 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1099 $sth->execute( "*", $itemtype, "" );
1100 $loanlength = $sth->fetchrow_hashref;
1101 return $loanlength->{issuelength}
1102 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1104 $sth->execute( "*", "*", "" );
1105 $loanlength = $sth->fetchrow_hashref;
1106 return $loanlength->{issuelength}
1107 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1109 # if no rule is set => 21 days (hardcoded)
1115 ($doreturn, $messages, $iteminformation, $borrower) =
1116 &AddReturn($barcode, $branch);
1120 C<$barcode> is the bar code of the book being returned. C<$branch> is
1121 the code of the branch where the book is being returned.
1123 C<&AddReturn> returns a list of four items:
1125 C<$doreturn> is true iff the return succeeded.
1127 C<$messages> is a reference-to-hash giving the reason for failure:
1133 No item with this barcode exists. The value is C<$barcode>.
1137 The book is not currently on loan. The value is C<$barcode>.
1139 =item C<IsPermanent>
1141 The book's home branch is a permanent collection. If you have borrowed
1142 this book, you are not allowed to return it. The value is the code for
1143 the book's home branch.
1147 This book has been withdrawn/cancelled. The value should be ignored.
1151 The item was reserved. The value is a reference-to-hash whose keys are
1152 fields from the reserves table of the Koha database, and
1153 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1154 either C<Waiting>, C<Reserved>, or 0.
1158 C<$borrower> is a reference-to-hash, giving information about the
1159 patron who last borrowed the book.
1164 my ( $barcode, $branch ) = @_;
1165 my $dbh = C4::Context->dbh;
1169 my $validTransfert = 0;
1170 my $reserveDone = 0;
1172 # get information on item
1173 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1174 unless ($iteminformation->{'itemnumber'} ) {
1175 $messages->{'BadBarcode'} = $barcode;
1179 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1180 $messages->{'NotIssued'} = $barcode;
1184 # check if the book is in a permanent collection....
1185 my $hbr = $iteminformation->{'homebranch'};
1186 my $branches = GetBranches();
1187 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1188 $messages->{'IsPermanent'} = $hbr;
1191 # check that the book has been cancelled
1192 if ( $iteminformation->{'wthdrawn'} ) {
1193 $messages->{'wthdrawn'} = 1;
1197 # new op dev : if the book returned in an other branch update the holding branch
1199 # update issues, thereby returning book (should push this out into another subroutine
1200 $borrower = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1202 # case of a return of document (deal with issues and holdingbranch)
1207 "UPDATE issues SET returndate = now() WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (returndate IS NULL)"
1209 $sth->execute( $borrower->{'borrowernumber'},
1210 $iteminformation->{'itemnumber'} );
1211 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1214 # continue to deal with returns cases, but not only if we have an issue
1216 # the holdingbranch is updated if the document is returned in an other location .
1217 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} )
1219 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1220 # reload iteminformation holdingbranch with the userenv value
1221 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1223 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1224 ($borrower) = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1226 # fix up the accounts.....
1227 if ( $iteminformation->{'itemlost'} ) {
1228 $messages->{'WasLost'} = 1;
1231 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1232 # check if we have a transfer for this document
1233 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1235 # if we have a transfer to do, we update the line of transfers with the datearrived
1237 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1240 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1242 $sth->execute( $iteminformation->{'itemnumber'} );
1244 # 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'
1245 C4::Reserves::ModReserveStatus( $iteminformation->{'itemnumber'},'W' );
1248 $messages->{'WrongTransfer'} = $tobranch;
1249 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1251 $validTransfert = 1;
1254 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1255 # fix up the accounts.....
1256 if ($iteminformation->{'itemlost'}) {
1257 FixAccountForLostAndReturned($iteminformation, $borrower);
1258 $messages->{'WasLost'} = 1;
1260 # fix up the overdues in accounts...
1261 FixOverduesOnReturn( $borrower->{'borrowernumber'},
1262 $iteminformation->{'itemnumber'} );
1264 # find reserves.....
1265 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1266 my ( $resfound, $resrec ) =
1267 C4::Reserves::CheckReserves( $iteminformation->{'itemnumber'} );
1269 $resrec->{'ResFound'} = $resfound;
1270 $messages->{'ResFound'} = $resrec;
1275 # Record the fact that this book was returned.
1277 $branch, 'return', '0', '',
1278 $iteminformation->{'itemnumber'},
1279 $iteminformation->{'itemtype'},
1280 $borrower->{'borrowernumber'}
1283 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'})
1284 if C4::Context->preference("ReturnLog");
1286 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1287 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1289 if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1290 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1291 ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1292 $messages->{'WasTransfered'} = 1;
1293 warn "was transfered";
1297 return ( $doreturn, $messages, $iteminformation, $borrower );
1300 =head2 FixOverduesOnReturn
1302 &FixOverduesOnReturn($brn,$itm);
1304 C<$brn> borrowernumber
1308 internal function, called only by AddReturn
1312 sub FixOverduesOnReturn {
1313 my ( $borrowernumber, $item ) = @_;
1314 my $dbh = C4::Context->dbh;
1316 # check for overdue fine
1319 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1321 $sth->execute( $borrowernumber, $item );
1323 # alter fine to show that the book has been returned
1324 if ( my $data = $sth->fetchrow_hashref ) {
1327 "UPDATE accountlines SET accounttype='F' WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accountno = ?)"
1329 $usth->execute( $borrowernumber, $item, $data->{'accountno'} );
1336 =head2 FixAccountForLostAndReturned
1338 &FixAccountForLostAndReturned($iteminfo,$borrower);
1340 Calculates the charge for a book lost and returned (Not exported & used only once)
1342 C<$iteminfo> is a hashref to iteminfo. Only {itemnumber} is used.
1344 C<$borrower> is a hashref to borrower. Only {borrowernumber is used.
1346 Internal function, called by AddReturn
1350 sub FixAccountForLostAndReturned {
1351 my ($iteminfo, $borrower) = @_;
1353 my $dbh = C4::Context->dbh;
1354 my $itm = $iteminfo->{'itemnumber'};
1355 # check for charge made for lost book
1356 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE (itemnumber = ?) AND (accounttype='L' OR accounttype='Rep') ORDER BY date DESC");
1357 $sth->execute($itm);
1358 if (my $data = $sth->fetchrow_hashref) {
1359 # writeoff this amount
1361 my $amount = $data->{'amount'};
1362 my $acctno = $data->{'accountno'};
1364 if ($data->{'amountoutstanding'} == $amount) {
1365 $offset = $data->{'amount'};
1368 $offset = $amount - $data->{'amountoutstanding'};
1369 $amountleft = $data->{'amountoutstanding'} - $amount;
1371 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
1372 WHERE (borrowernumber = ?)
1373 AND (itemnumber = ?) AND (accountno = ?) ");
1374 $usth->execute($data->{'borrowernumber'},$itm,$acctno);
1376 #check if any credit is left if so writeoff other accounts
1377 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
1378 if ($amountleft < 0){
1381 if ($amountleft > 0){
1382 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
1383 AND (amountoutstanding >0) ORDER BY date");
1384 $msth->execute($data->{'borrowernumber'});
1385 # offset transactions
1388 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1389 if ($accdata->{'amountoutstanding'} < $amountleft) {
1391 $amountleft -= $accdata->{'amountoutstanding'};
1393 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1396 my $thisacct = $accdata->{'accountno'};
1397 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
1398 WHERE (borrowernumber = ?)
1399 AND (accountno=?)");
1400 $usth->execute($newamtos,$data->{'borrowernumber'},'$thisacct');
1402 $usth = $dbh->prepare("INSERT INTO accountoffsets
1403 (borrowernumber, accountno, offsetaccount, offsetamount)
1406 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
1411 if ($amountleft > 0){
1414 my $desc="Book Returned ".$iteminfo->{'barcode'};
1415 $usth = $dbh->prepare("INSERT INTO accountlines
1416 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1417 VALUES (?,?,now(),?,?,'CR',?)");
1418 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
1420 $usth = $dbh->prepare("INSERT INTO accountoffsets
1421 (borrowernumber, accountno, offsetaccount, offsetamount)
1423 $usth->execute($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset);
1425 $usth = $dbh->prepare("UPDATE items SET paidfor='' WHERE itemnumber=?");
1426 $usth->execute($itm);
1435 $issues = &GetItemIssue($itemnumber);
1437 Returns patrons currently having a book. nothing if item is not issued atm
1439 C<$itemnumber> is the itemnumber
1441 Returns an array of hashes
1445 my ( $itemnumber) = @_;
1446 return unless $itemnumber;
1447 my $dbh = C4::Context->dbh;
1451 my $today = POSIX::strftime("%Y%m%d", localtime);
1453 my $sth = $dbh->prepare(
1454 "SELECT * FROM issues
1455 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1457 issues.itemnumber=? AND returndate IS NULL ");
1458 $sth->execute($itemnumber);
1459 my $data = $sth->fetchrow_hashref;
1460 my $datedue = $data->{'date_due'};
1462 if ( $datedue < $today ) {
1463 $data->{'overdue'} = 1;
1465 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1470 =head2 GetItemIssues
1472 $issues = &GetItemIssues($itemnumber, $history);
1474 Returns patrons that have issued a book
1476 C<$itemnumber> is the itemnumber
1477 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1479 Returns an array of hashes
1483 my ( $itemnumber,$history ) = @_;
1484 my $dbh = C4::Context->dbh;
1488 my $today = POSIX::strftime("%Y%m%d", localtime);
1490 my $sth = $dbh->prepare(
1491 "SELECT * FROM issues
1493 itemnumber=?".($history?"":" AND returndate IS NULL ").
1494 "ORDER BY issues.date_due DESC"
1496 $sth->execute($itemnumber);
1497 while ( my $data = $sth->fetchrow_hashref ) {
1498 my $datedue = $data->{'date_due'};
1500 if ( $datedue < $today ) {
1501 $data->{'overdue'} = 1;
1503 my $itemnumber = $data->{'itemnumber'};
1505 push @GetItemIssues, $data;
1508 return ( \@GetItemIssues );
1511 =head2 GetBiblioIssues
1513 $issues = GetBiblioIssues($biblionumber);
1515 this function get all issues from a biblionumber.
1518 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1519 tables issues and the firstname,surname & cardnumber from borrowers.
1523 sub GetBiblioIssues {
1524 my $biblionumber = shift;
1525 return undef unless $biblionumber;
1526 my $dbh = C4::Context->dbh;
1528 SELECT issues.*,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1530 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1531 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1532 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1533 LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1534 WHERE biblio.biblionumber = ?
1535 ORDER BY issues.timestamp
1537 my $sth = $dbh->prepare($query);
1538 $sth->execute($biblionumber);
1541 while ( my $data = $sth->fetchrow_hashref ) {
1542 push @issues, $data;
1547 =head2 CanBookBeRenewed
1549 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1551 Find out whether a borrowed item may be renewed.
1553 C<$dbh> is a DBI handle to the Koha database.
1555 C<$borrowernumber> is the borrower number of the patron who currently
1556 has the item on loan.
1558 C<$itemnumber> is the number of the item to renew.
1560 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1561 item must currently be on loan to the specified borrower; renewals
1562 must be allowed for the item's type; and the borrower must not have
1563 already renewed the loan.
1567 sub CanBookBeRenewed {
1569 # check renewal status
1570 my ( $borrowernumber, $itemnumber ) = @_;
1571 my $dbh = C4::Context->dbh;
1575 # Look in the issues table for this item, lent to this borrower,
1576 # and not yet returned.
1578 # FIXME - I think this function could be redone to use only one SQL call.
1579 my $sth1 = $dbh->prepare(
1580 "SELECT * FROM issues
1581 WHERE borrowernumber = ?
1583 AND returndate IS NULL"
1585 $sth1->execute( $borrowernumber, $itemnumber );
1586 if ( my $data1 = $sth1->fetchrow_hashref ) {
1588 # Found a matching item
1590 # See if this item may be renewed. This query is convoluted
1591 # because it's a bit messy: given the item number, we need to find
1592 # the biblioitem, which gives us the itemtype, which tells us
1593 # whether it may be renewed.
1594 my $sth2 = $dbh->prepare(
1595 "SELECT renewalsallowed FROM items
1596 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1597 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1598 WHERE items.itemnumber = ?
1601 $sth2->execute($itemnumber);
1602 if ( my $data2 = $sth2->fetchrow_hashref ) {
1603 $renews = $data2->{'renewalsallowed'};
1605 if ( $renews && $renews >= $data1->{'renewals'} ) {
1609 my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber);
1616 return ($renewokay);
1621 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1625 C<$borrowernumber> is the borrower number of the patron who currently
1628 C<$itemnumber> is the number of the item to renew.
1630 C<$datedue> can be used to set the due date. If C<$datedue> is the
1631 empty string, C<&AddRenewal> will calculate the due date automatically
1632 from the book's item type. If you wish to set the due date manually,
1633 C<$datedue> should be in the form YYYY-MM-DD.
1639 my ( $borrowernumber, $itemnumber, $branch ,$datedue ) = @_;
1640 my $dbh = C4::Context->dbh;
1642 # If the due date wasn't specified, calculate it by adding the
1643 # book's loan length to today's date.
1644 if ( $datedue eq "" ) {
1646 my $biblio = GetBiblioFromItemNumber($itemnumber);
1647 my $borrower = GetMemberDetails( $borrowernumber, 0 );
1648 my $loanlength = GetLoanLength(
1649 $borrower->{'categorycode'},
1650 $biblio->{'itemtype'},
1651 $borrower->{'branchcode'}
1653 my ( $due_year, $due_month, $due_day ) =
1654 Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1655 $datedue = "$due_year-$due_month-$due_day";
1656 $datedue=CheckValidDatedue($datedue,$itemnumber,$branch);
1659 # Find the issues record for this book
1661 $dbh->prepare("SELECT * FROM issues
1662 WHERE borrowernumber=?
1664 AND returndate IS NULL"
1666 $sth->execute( $borrowernumber, $itemnumber );
1667 my $issuedata = $sth->fetchrow_hashref;
1670 # Update the issues record to have the new due date, and a new count
1671 # of how many times it has been renewed.
1672 my $renews = $issuedata->{'renewals'} + 1;
1673 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1674 WHERE borrowernumber=?
1676 AND returndate IS NULL"
1678 $sth->execute( $datedue, $renews, $borrowernumber, $itemnumber );
1682 UpdateStats( C4::Context->userenv->{'branchcode'}, 'renew', '', '', $itemnumber );
1684 # Charge a new rental fee, if applicable?
1685 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1686 if ( $charge > 0 ) {
1687 my $accountno = getnextacctno( $borrowernumber );
1688 my $item = GetBiblioFromItemNumber($itemnumber);
1689 $sth = $dbh->prepare(
1690 "INSERT INTO accountlines
1691 (borrowernumber,accountno,date,amount,
1692 description,accounttype,amountoutstanding,
1694 VALUES (?,?,now(),?,?,?,?,?)"
1696 $sth->execute( $borrowernumber, $accountno, $charge,
1697 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1698 'Rent', $charge, $itemnumber );
1703 =head2 GetIssuingCharges
1705 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1707 Calculate how much it would cost for a given patron to borrow a given
1708 item, including any applicable discounts.
1710 C<$itemnumber> is the item number of item the patron wishes to borrow.
1712 C<$borrowernumber> is the patron's borrower number.
1714 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1715 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1720 sub GetIssuingCharges {
1722 # calculate charges due
1723 my ( $itemnumber, $borrowernumber ) = @_;
1725 my $dbh = C4::Context->dbh;
1728 # Get the book's item type and rental charge (via its biblioitem).
1729 my $sth1 = $dbh->prepare(
1730 "SELECT itemtypes.itemtype,rentalcharge FROM items
1731 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1732 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1733 WHERE items.itemnumber =?
1736 $sth1->execute($itemnumber);
1737 if ( my $data1 = $sth1->fetchrow_hashref ) {
1738 $item_type = $data1->{'itemtype'};
1739 $charge = $data1->{'rentalcharge'};
1740 my $q2 = "SELECT rentaldiscount FROM borrowers
1741 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1742 WHERE borrowers.borrowernumber = ?
1743 AND issuingrules.itemtype = ?";
1744 my $sth2 = $dbh->prepare($q2);
1745 $sth2->execute( $borrowernumber, $item_type );
1746 if ( my $data2 = $sth2->fetchrow_hashref ) {
1747 my $discount = $data2->{'rentaldiscount'};
1748 if ( $discount eq 'NULL' ) {
1751 $charge = ( $charge * ( 100 - $discount ) ) / 100;
1757 return ( $charge, $item_type );
1760 =head2 AddIssuingCharge
1762 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1766 sub AddIssuingCharge {
1767 my ( $itemnumber, $borrowernumber, $charge ) = @_;
1768 my $dbh = C4::Context->dbh;
1769 my $nextaccntno = getnextacctno( $borrowernumber );
1771 INSERT INTO accountlines
1772 (borrowernumber, itemnumber, accountno,
1773 date, amount, description, accounttype,
1775 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1777 my $sth = $dbh->prepare($query);
1778 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1784 GetTransfers($itemnumber);
1789 my ($itemnumber) = @_;
1791 my $dbh = C4::Context->dbh;
1797 FROM branchtransfers
1798 WHERE itemnumber = ?
1799 AND datearrived IS NULL
1801 my $sth = $dbh->prepare($query);
1802 $sth->execute($itemnumber);
1803 my @row = $sth->fetchrow_array();
1809 =head2 GetTransfersFromTo
1811 @results = GetTransfersFromTo($frombranch,$tobranch);
1813 Returns the list of pending transfers between $from and $to branch
1817 sub GetTransfersFromTo {
1818 my ( $frombranch, $tobranch ) = @_;
1819 return unless ( $frombranch && $tobranch );
1820 my $dbh = C4::Context->dbh;
1822 SELECT itemnumber,datesent,frombranch
1823 FROM branchtransfers
1826 AND datearrived IS NULL
1828 my $sth = $dbh->prepare($query);
1829 $sth->execute( $frombranch, $tobranch );
1832 while ( my $data = $sth->fetchrow_hashref ) {
1833 push @gettransfers, $data;
1836 return (@gettransfers);
1839 =head2 DeleteTransfer
1841 &DeleteTransfer($itemnumber);
1845 sub DeleteTransfer {
1846 my ($itemnumber) = @_;
1847 my $dbh = C4::Context->dbh;
1848 my $sth = $dbh->prepare(
1849 "DELETE FROM branchtransfers
1851 AND datearrived IS NULL "
1853 $sth->execute($itemnumber);
1857 =head2 AnonymiseIssueHistory
1859 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1861 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1862 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1864 return the number of affected rows.
1868 sub AnonymiseIssueHistory {
1870 my $borrowernumber = shift;
1871 my $dbh = C4::Context->dbh;
1874 SET borrowernumber = NULL
1875 WHERE returndate < '".$date."'
1876 AND borrowernumber IS NOT NULL
1878 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1879 my $rows_affected = $dbh->do($query);
1880 return $rows_affected;
1883 =head2 updateWrongTransfer
1885 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1887 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
1891 sub updateWrongTransfer {
1892 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1893 my $dbh = C4::Context->dbh;
1894 # first step validate the actual line of transfert .
1897 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1899 $sth->execute($FromLibrary,$itemNumber);
1902 # second step create a new line of branchtransfer to the right location .
1903 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1905 #third step changing holdingbranch of item
1906 UpdateHoldingbranch($FromLibrary,$itemNumber);
1909 =head2 UpdateHoldingbranch
1911 $items = UpdateHoldingbranch($branch,$itmenumber);
1912 Simple methode for updating hodlingbranch in items BDD line
1915 sub UpdateHoldingbranch {
1916 my ( $branch,$itmenumber ) = @_;
1917 my $dbh = C4::Context->dbh;
1918 # first step validate the actual line of transfert .
1921 "update items set holdingbranch = ? where itemnumber= ?"
1923 $sth->execute($branch,$itmenumber);
1928 =head2 CheckValidDatedue
1930 $newdatedue = CheckValidDatedue($date_due,$itemnumber,$branchcode);
1931 this function return a new date due after checked if it's a repeatable or special holiday
1932 C<$date_due> = returndate calculate with no day check
1933 C<$itemnumber> = itemnumber
1934 C<$branchcode> = localisation of issue
1936 sub CheckValidDatedue{
1937 my ($date_due,$itemnumber,$branchcode)=@_;
1938 my @datedue=split('-',$date_due);
1939 my $years=$datedue[0];
1940 my $month=$datedue[1];
1941 my $day=$datedue[2];
1943 for (my $i=0;$i<2;$i++){
1944 $dow=Day_of_Week($years,$month,$day);
1945 ($dow=0) if ($dow>6);
1946 my $result=CheckRepeatableHolidays($itemnumber,$dow,$branchcode);
1947 my $countspecial=CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
1948 my $countspecialrepeatable=CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
1949 if (($result ne '0') or ($countspecial ne '0') or ($countspecialrepeatable ne '0') ){
1951 (($years,$month,$day) = Add_Delta_Days($years,$month,$day, 1))if ($i ne '1');
1954 my $newdatedue=$years."-".$month."-".$day;
1957 =head2 CheckRepeatableHolidays
1959 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
1960 this function check if the date due is a repeatable holiday
1961 C<$date_due> = returndate calculate with no day check
1962 C<$itemnumber> = itemnumber
1963 C<$branchcode> = localisation of issue
1967 sub CheckRepeatableHolidays{
1968 my($itemnumber,$week_day,$branchcode)=@_;
1969 my $dbh = C4::Context->dbh;
1970 my $query = qq|SELECT count(*)
1971 FROM repeatable_holidays
1974 my $sth = $dbh->prepare($query);
1975 $sth->execute($branchcode,$week_day);
1976 my $result=$sth->fetchrow;
1982 =head2 CheckSpecialHolidays
1984 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
1985 this function check if the date is a special holiday
1986 C<$years> = the years of datedue
1987 C<$month> = the month of datedue
1988 C<$day> = the day of datedue
1989 C<$itemnumber> = itemnumber
1990 C<$branchcode> = localisation of issue
1992 sub CheckSpecialHolidays{
1993 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
1994 my $dbh = C4::Context->dbh;
1995 my $query=qq|SELECT count(*)
1996 FROM `special_holidays`
2002 my $sth = $dbh->prepare($query);
2003 $sth->execute($years,$month,$day,$branchcode);
2004 my $countspecial=$sth->fetchrow ;
2006 return $countspecial;
2009 =head2 CheckRepeatableSpecialHolidays
2011 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2012 this function check if the date is a repeatble special holidays
2013 C<$month> = the month of datedue
2014 C<$day> = the day of datedue
2015 C<$itemnumber> = itemnumber
2016 C<$branchcode> = localisation of issue
2018 sub CheckRepeatableSpecialHolidays{
2019 my ($month,$day,$itemnumber,$branchcode) = @_;
2020 my $dbh = C4::Context->dbh;
2021 my $query=qq|SELECT count(*)
2022 FROM `repeatable_holidays`
2027 my $sth = $dbh->prepare($query);
2028 $sth->execute($month,$day,$branchcode);
2029 my $countspecial=$sth->fetchrow ;
2031 return $countspecial;
2036 sub CheckValidBarcode{
2038 my $dbh = C4::Context->dbh;
2039 my $query=qq|SELECT count(*)
2043 my $sth = $dbh->prepare($query);
2044 $sth->execute($barcode);
2045 my $exist=$sth->fetchrow ;
2056 Koha Developement team <info@koha.org>