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 use vars qw($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::Circ2 - 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
74 # subs to deal with issuing a book
85 &AnonymiseIssueHistory
87 # subs to deal with returns
92 # subs to deal with transfers
109 =head3 $str = &decode($chunk);
113 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
120 # FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
122 # 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 ?
126 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
127 my @s = map { index( $seq, $_ ); } split( //, $encoded );
128 my $l = ( $#s + 1 ) % 4;
139 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
141 chr( ( $n >> 16 ) ^ 67 )
142 .chr( ( $n >> 8 & 255 ) ^ 67 )
143 .chr( ( $n & 255 ) ^ 67 );
146 $r = substr( $r, 0, length($r) - $l );
152 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch, $barcode, $ignore_reserves);
154 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
156 C<$newbranch> is the code for the branch to which the item should be transferred.
158 C<$barcode> is the barcode of the item to be transferred.
160 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
161 Otherwise, if an item is reserved, the transfer fails.
163 Returns three values:
167 is true if the transfer was successful.
171 is a reference-to-hash which may have any of the following keys:
177 There is no item in the catalog with the given barcode. The value is C<$barcode>.
181 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.
183 =item C<DestinationEqualsHolding>
185 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.
189 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.
193 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>.
195 =item C<WasTransferred>
197 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
204 my ( $tbr, $barcode, $ignoreRs ) = @_;
208 my $branches = GetBranches();
209 my $itemnumber = GetItemnumberFromBarcode( $barcode );
210 my $issue = GetItemIssue($itemnumber);
211 my $biblio = GetBiblioFromItemNumber($itemnumber);
214 if ( not $itemnumber ) {
215 $messages->{'BadBarcode'} = $barcode;
219 # get branches of book...
220 my $hbr = $biblio->{'homebranch'};
221 my $fbr = $biblio->{'holdingbranch'};
224 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
225 $messages->{'IsPermanent'} = $hbr;
228 # can't transfer book if is already there....
229 if ( $fbr eq $tbr ) {
230 $messages->{'DestinationEqualsHolding'} = 1;
234 # check if it is still issued to someone, return it...
235 if ($issue->{borrowernumber}) {
236 AddReturn( $barcode, $fbr );
237 $messages->{'WasReturned'} = $issue->{borrowernumber};
241 # That'll save a database query.
242 my ( $resfound, $resrec ) =
243 CheckReserves( $itemnumber );
244 if ( $resfound and not $ignoreRs ) {
245 $resrec->{'ResFound'} = $resfound;
247 # $messages->{'ResFound'} = $resrec;
251 #actually do the transfer....
253 dotransfer( $itemnumber, $fbr, $tbr );
255 # don't need to update MARC anymore, we do it in batch now
256 $messages->{'WasTransfered'} = 1;
258 return ( $dotransfer, $messages, $biblio );
262 # FIXME - This is only used in &transferbook. Why bother making it a
265 my ( $itm, $fbr, $tbr ) = @_;
267 my $dbh = C4::Context->dbh;
268 $itm = $dbh->quote($itm);
269 $fbr = $dbh->quote($fbr);
270 $tbr = $dbh->quote($tbr);
272 #new entry in branchtransfers....
274 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
275 VALUES ($itm, $fbr, now(), $tbr)"
278 #update holdingbranch in items .....
280 "UPDATE items set holdingbranch = $tbr WHERE items.itemnumber = $itm");
281 &ModDateLastSeen($itm);
282 &domarctransfer( $dbh, $itm );
286 ##New sub to dotransfer in marc tables as well. Not exported -TG 10/04/2006
288 my ( $dbh, $itemnumber ) = @_;
289 $itemnumber =~ s /\'//g; ##itemnumber seems to come with quotes-TG
292 "select biblionumber,holdingbranch from items where itemnumber=$itemnumber"
295 while ( my ( $biblionumber, $holdingbranch ) = $sth->fetchrow ) {
296 &ModItemInMarconefield( $biblionumber, $itemnumber,
297 'items.holdingbranch', $holdingbranch );
302 =head2 CanBookBeIssued
304 Check if a book can be issued.
306 my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued($env,$borrower,$barcode,$year,$month,$day);
310 =item C<$env> Environment variable. Should be empty usually, but used by other subs. Next code cleaning could drop it.
312 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
314 =item C<$barcode> is the bar code of the book being issued.
316 =item C<$year> C<$month> C<$day> contains the date of the return (in case it's forced by "stickyduedate".
324 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
325 Possible values are :
331 sticky due date is invalid
335 borrower gone with no address
339 borrower declared it's card lost
345 =head3 UNKNOWN_BARCODE
359 item is restricted (set by ??)
361 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
362 Possible values are :
370 renewing, not issuing
372 =head3 ISSUED_TO_ANOTHER
374 issued to someone else.
378 reserved for someone else.
382 sticky due date is invalid
386 if the borrower borrows to much things
390 # check if a book can be issued.
391 # returns an array with errors if any
394 my $borrower = shift;
395 my $biblionumber = shift;
396 my $cat_borrower = $borrower->{'categorycode'};
397 my $branch_borrower = $borrower->{'branchcode'};
398 my $dbh = C4::Context->dbh;
401 $dbh->prepare('select itemtype from biblioitems where biblionumber = ?');
402 $sth->execute($biblionumber);
403 my $type = $sth->fetchrow;
406 'select * from issuingrules where categorycode = ? and itemtype = ? and branchcode = ?'
409 # my $sth2 = $dbh->prepare("select COUNT(*) from issues i, biblioitems s where i.borrowernumber = ? and i.returndate is null and i.itemnumber = s.biblioitemnumber and s.itemtype like ?");
412 "select COUNT(*) from issues i, biblioitems s1, items s2 where i.borrowernumber = ? and i.returndate is null and i.itemnumber = s2.itemnumber and s1.itemtype like ? and s1.biblioitemnumber = s2.biblioitemnumber"
416 'select COUNT(*) from issues where borrowernumber = ? and returndate is null'
420 # check the 3 parameters
421 $sth->execute( $cat_borrower, $type, $branch_borrower );
422 my $result = $sth->fetchrow_hashref;
424 # warn "==>".$result->{maxissueqty};
426 # Currently, using defined($result) ie on an entire hash reports whether memory
427 # for that aggregate has ever been allocated. As $result is used all over the place
428 # it would rarely return as undefined.
429 if ( defined( $result->{maxissueqty} ) ) {
430 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
431 my $alreadyissued = $sth2->fetchrow;
432 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
433 return ( "a $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
441 $sth->execute( $cat_borrower, $type, "" );
442 $result = $sth->fetchrow_hashref;
443 if ( defined( $result->{maxissueqty} ) ) {
444 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
445 my $alreadyissued = $sth2->fetchrow;
446 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
447 return ( "b $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
454 # check for itemtype=*
455 $sth->execute( $cat_borrower, "*", $branch_borrower );
456 $result = $sth->fetchrow_hashref;
457 if ( defined( $result->{maxissueqty} ) ) {
458 $sth3->execute( $borrower->{'borrowernumber'} );
459 my ($alreadyissued) = $sth3->fetchrow;
460 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
462 # warn "HERE : $alreadyissued / ($result->{maxissueqty} for $borrower->{'borrowernumber'}";
463 return ( "c $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
470 # check for borrowertype=*
471 $sth->execute( "*", $type, $branch_borrower );
472 $result = $sth->fetchrow_hashref;
473 if ( defined( $result->{maxissueqty} ) ) {
474 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
475 my $alreadyissued = $sth2->fetchrow;
476 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
477 return ( "d $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
484 $sth->execute( "*", "*", $branch_borrower );
485 $result = $sth->fetchrow_hashref;
486 if ( defined( $result->{maxissueqty} ) ) {
487 $sth3->execute( $borrower->{'borrowernumber'} );
488 my $alreadyissued = $sth3->fetchrow;
489 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
490 return ( "e $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
497 $sth->execute( "*", $type, "" );
498 $result = $sth->fetchrow_hashref;
499 if ( defined( $result->{maxissueqty} ) && $result->{maxissueqty} >= 0 ) {
500 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
501 my $alreadyissued = $sth2->fetchrow;
502 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
503 return ( "f $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
510 $sth->execute( $cat_borrower, "*", "" );
511 $result = $sth->fetchrow_hashref;
512 if ( defined( $result->{maxissueqty} ) ) {
513 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
514 my $alreadyissued = $sth2->fetchrow;
515 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
516 return ( "g $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
523 $sth->execute( "*", "*", "" );
524 $result = $sth->fetchrow_hashref;
525 if ( defined( $result->{maxissueqty} ) ) {
526 $sth3->execute( $borrower->{'borrowernumber'} );
527 my $alreadyissued = $sth3->fetchrow;
528 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
529 return ( "h $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
540 @issues = &itemissues($biblioitemnumber, $biblio);
542 Looks up information about who has borrowed the bookZ<>(s) with the
543 given biblioitemnumber.
545 C<$biblio> is ignored.
547 C<&itemissues> returns an array of references-to-hash. The keys
548 include the fields from the C<items> table in the Koha database.
549 Additional keys include:
555 If the item is currently on loan, this gives the due date.
557 If the item is not on loan, then this is either "Available" or
558 "Cancelled", if the item has been withdrawn.
562 If the item is currently on loan, this gives the card number of the
563 patron who currently has the item.
565 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
567 These give the timestamp for the last three times the item was
570 =item C<card0>, C<card1>, C<card2>
572 The card number of the last three patrons who borrowed this item.
574 =item C<borrower0>, C<borrower1>, C<borrower2>
576 The borrower number of the last three patrons who borrowed this item.
584 my ( $bibitem, $biblio ) = @_;
585 my $dbh = C4::Context->dbh;
587 # FIXME - If this function die()s, the script will abort, and the
588 # user won't get anything; depending on how far the script has
589 # gotten, the user might get a blank page. It would be much better
590 # to at least print an error message. The easiest way to do this
591 # is to set $SIG{__DIE__}.
593 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
598 $sth->execute($bibitem) || die $sth->errstr;
600 while ( my $data = $sth->fetchrow_hashref ) {
602 # Find out who currently has this item.
603 # FIXME - Wouldn't it be better to do this as a left join of
604 # some sort? Currently, this code assumes that if
605 # fetchrow_hashref() fails, then the book is on the shelf.
606 # fetchrow_hashref() can fail for any number of reasons (e.g.,
607 # database server crash), not just because no items match the
609 my $sth2 = $dbh->prepare(
610 "SELECT * FROM issues
611 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
613 AND returndate IS NULL
617 $sth2->execute( $data->{'itemnumber'} );
618 if ( my $data2 = $sth2->fetchrow_hashref ) {
619 $data->{'date_due'} = $data2->{'date_due'};
620 $data->{'card'} = $data2->{'cardnumber'};
621 $data->{'borrower'} = $data2->{'borrowernumber'};
624 if ( $data->{'wthdrawn'} eq '1' ) {
625 $data->{'date_due'} = 'Cancelled';
628 $data->{'date_due'} = 'Available';
634 # Find the last 3 people who borrowed this item.
635 $sth2 = $dbh->prepare(
636 "SELECT * FROM issues, borrowers
637 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
639 AND returndate IS NOT NULL
640 ORDER BY returndate DESC,timestamp DESC"
643 # $sth2 = $dbh->prepare("
646 # LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
647 # WHERE itemnumber = ?
648 # AND returndate is not NULL
649 # ORDER BY returndate DESC,timestamp DESC
652 $sth2->execute( $data->{'itemnumber'} );
653 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
654 { # FIXME : error if there is less than 3 pple borrowing this item
655 if ( my $data2 = $sth2->fetchrow_hashref ) {
656 $data->{"timestamp$i2"} = $data2->{'timestamp'};
657 $data->{"card$i2"} = $data2->{'cardnumber'};
658 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
663 $results[$i] = $data;
671 =head2 CanBookBeIssued
673 $issuingimpossible, $needsconfirmation =
674 CanBookBeIssued( $env, $borrower, $barcode, $year, $month, $day, $inprocess );
676 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
680 sub CanBookBeIssued {
681 my ( $env, $borrower, $barcode, $year, $month, $day, $inprocess ) = @_;
682 my %needsconfirmation; # filled with problems that needs confirmations
683 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
684 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
685 my $issue = GetItemIssue($item->{itemnumber});
686 my $dbh = C4::Context->dbh;
691 my ( $duedate, $invalidduedate ) = fixdate( $year, $month, $day );
692 $issuingimpossible{INVALID_DATE} = 1 if ($invalidduedate);
697 if ( $borrower->{flags}->{GNA} ) {
698 $issuingimpossible{GNA} = 1;
700 if ( $borrower->{flags}->{'LOST'} ) {
701 $issuingimpossible{CARD_LOST} = 1;
703 if ( $borrower->{flags}->{'DBARRED'} ) {
704 $issuingimpossible{DEBARRED} = 1;
706 if ( Date_to_Days(Today) >
707 Date_to_Days( split "-", $borrower->{'dateexpiry'} ) )
711 #if (&Date_Cmp(&ParseDate($borrower->{expiry}),&ParseDate("today"))<0) {
712 $issuingimpossible{EXPIRED} = 1;
721 checkaccount( $borrower->{'borrowernumber'}, $dbh, $duedate );
722 if ( C4::Context->preference("IssuingInProcess") ) {
723 my $amountlimit = C4::Context->preference("noissuescharge");
724 if ( $amount > $amountlimit && !$inprocess ) {
725 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
727 elsif ( $amount <= $amountlimit && !$inprocess ) {
728 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
733 $needsconfirmation{DEBT} = $amount;
738 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
740 my $toomany = TooMany( $borrower, $item->{biblionumber} );
741 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
746 unless ( $item->{barcode} ) {
747 $issuingimpossible{UNKNOWN_BARCODE} = 1;
749 if ( $item->{'notforloan'}
750 && $item->{'notforloan'} > 0 )
752 $issuingimpossible{NOT_FOR_LOAN} = 1;
754 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
756 $issuingimpossible{WTHDRAWN} = 1;
758 if ( $item->{'restricted'}
759 && $item->{'restricted'} == 1 )
761 $issuingimpossible{RESTRICTED} = 1;
763 if ( C4::Context->preference("IndependantBranches") ) {
764 my $userenv = C4::Context->userenv;
765 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
766 $issuingimpossible{NOTSAMEBRANCH} = 1
767 if ( $item->{'holdingbranch'} ne $userenv->{branch} );
772 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
774 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
777 # Already issued to current borrower. Ask whether the loan should
779 my ($CanBookBeRenewed) = CanBookBeRenewed(
780 $borrower->{'borrowernumber'},
781 $item->{'itemnumber'}
783 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
784 $issuingimpossible{NO_MORE_RENEWALS} = 1;
788 # $needsconfirmation{RENEW_ISSUE} = 1;
791 elsif ($issue->{borrowernumber}) {
793 # issued to someone else
794 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
796 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
797 $needsconfirmation{ISSUED_TO_ANOTHER} =
798 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
801 # See if the item is on reserve.
802 my ( $restype, $res ) = CheckReserves( $item->{'itemnumber'} );
804 my $resbor = $res->{'borrowernumber'};
805 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
808 # The item is on reserve and waiting, but has been
809 # reserved by some other patron.
810 my ( $resborrower, $flags ) =
811 GetMemberDetails( $resbor, 0 );
812 my $branches = GetBranches();
814 $branches->{ $res->{'branchcode'} }->{'branchname'};
815 $needsconfirmation{RESERVE_WAITING} =
816 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
818 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
820 elsif ( $restype eq "Reserved" ) {
822 # The item is on reserve for someone else.
823 my ( $resborrower, $flags ) =
824 GetMemberDetails( $resbor, 0 );
825 my $branches = GetBranches();
827 $branches->{ $res->{'branchcode'} }->{'branchname'};
828 $needsconfirmation{RESERVED} =
829 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
832 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" )
834 if ( $borrower->{'categorycode'} eq 'W' ) {
835 my %issuingimpossible;
836 return ( \%issuingimpossible, \%needsconfirmation );
839 return ( \%issuingimpossible, \%needsconfirmation );
843 return ( \%issuingimpossible, \%needsconfirmation );
849 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
851 &AddIssue($env,$borrower,$barcode,$date)
855 =item C<$env> Environment variable. Should be empty usually, but used by other subs. Next code cleaning could drop it.
857 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
859 =item C<$barcode> is the bar code of the book being issued.
861 =item C<$date> contains the max date of return. calculated if empty.
863 AddIssue does the following things :
864 - step 0�: check that there is a borrowernumber & a barcode provided
865 - check for RENEWAL (book issued & being issued to the same patron)
866 - renewal YES = Calculate Charge & renew
868 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
870 - fill reserve if reserve to this patron
871 - cancel reserve or not, otherwise
872 * TRANSFERT PENDING ?
873 - complete the transfert
881 my ( $env, $borrower, $barcode, $date, $cancelreserve ) = @_;
883 my $dbh = C4::Context->dbh;
884 if ($borrower and $barcode){
885 # my ($borrower, $flags) = &GetMemberDetails($borrowernumber, 0);
886 # find which item we issue
887 my $item = GetItem('', $barcode);
889 # get actual issuing if there is one
890 my $actualissue = GetItemIssue( $item->{itemnumber});
892 # get biblioinformation for this item
893 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
896 # check if we just renew the issue.
898 if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
899 # we renew, do we need to add some charge ?
900 my ( $charge, $itemtype ) = GetIssuingCharges(
901 $item->{'itemnumber'},
902 $borrower->{'borrowernumber'}
906 $item->{'itemnumber'},
907 $borrower->{'borrowernumber'}, $charge
909 $item->{'charge'} = $charge;
912 $env, $env->{'branchcode'},
914 '', $item->{'itemnumber'},
915 $biblio->{'itemtype'}, $borrower->{'borrowernumber'}
918 $borrower->{'borrowernumber'},
919 $item->{'itemnumber'}
922 else {# it's NOT a renewal
923 if ( $actualissue->{borrowernumber}) {
924 # This book is currently on loan, but not to the person
925 # who wants to borrow it now. mark it returned before issuing to the new borrower
928 C4::Context->userenv->{'branch'}
932 # See if the item is on reserve.
933 my ( $restype, $res ) =
934 CheckReserves( $item->{'itemnumber'} );
936 my $resbor = $res->{'borrowernumber'};
937 if ( $resbor eq $borrower->{'borrowernumber'} ) {
939 # The item is reserved by the current patron
942 elsif ( $restype eq "Waiting" ) {
945 # The item is on reserve and waiting, but has been
946 # reserved by some other patron.
947 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
948 my $branches = GetBranches();
950 $branches->{ $res->{'branchcode'} }->{'branchname'};
951 if ($cancelreserve) {
952 CancelReserve( 0, $res->{'itemnumber'},
953 $res->{'borrowernumber'} );
957 # set waiting reserve to first in reserve queue as book isn't waiting now
960 $res->{'biblionumber'},
961 $res->{'borrowernumber'},
966 elsif ( $restype eq "Reserved" ) {
969 # The item is reserved by someone else.
970 my ( $resborrower, $flags ) =
971 GetMemberDetails( $resbor, 0 );
972 my $branches = GetBranches();
974 $branches->{ $res->{'branchcode'} }->{'branchname'};
975 if ($cancelreserve) { # cancel reserves on this item
976 CancelReserve( 0, $res->{'itemnumber'},
977 $res->{'borrowernumber'} );
982 # Starting process for transfer job (checking transfert and validate it if we have one)
983 my ($datesent) = GetTransfers($item->{'itemnumber'});
985 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
988 "UPDATE branchtransfers
989 SET datearrived = now(),
991 comments = 'Forced branchtransfert'
992 WHERE itemnumber= ? AND datearrived IS NULL"
994 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
998 # Record in the database the fact that the book was issued.
1002 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
1005 my $loanlength = GetLoanLength(
1006 $borrower->{'categorycode'},
1007 $biblio->{'itemtype'},
1008 $borrower->{'branchcode'}
1010 my $datedue = time + ($loanlength) * 86400;
1011 my @datearr = localtime($datedue);
1013 ( 1900 + $datearr[5] ) . "-"
1014 . ( $datearr[4] + 1 ) . "-"
1020 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
1021 if ( C4::Context->preference('ReturnBeforeExpiry')
1022 && $dateduef gt $borrower->{dateexpiry} )
1024 $dateduef = $borrower->{dateexpiry};
1027 $borrower->{'borrowernumber'},
1028 $item->{'itemnumber'},
1029 strftime( "%Y-%m-%d", localtime ),$dateduef, $env->{'branchcode'}
1032 $item->{'issues'}++;
1035 "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed = now() WHERE itemnumber=?");
1038 C4::Context->userenv->{'branch'},
1039 $item->{'itemnumber'}
1042 &ModDateLastSeen( $item->{'itemnumber'} );
1043 # If it costs to borrow this book, charge it to the patron's account.
1044 my ( $charge, $itemtype ) = GetIssuingCharges(
1045 $item->{'itemnumber'},
1046 $borrower->{'borrowernumber'}
1048 if ( $charge > 0 ) {
1050 $item->{'itemnumber'},
1051 $borrower->{'borrowernumber'}, $charge
1053 $item->{'charge'} = $charge;
1056 # Record the fact that this book was issued.
1058 $env, $env->{'branchcode'},
1060 '', $item->{'itemnumber'},
1061 $item->{'itemtype'}, $borrower->{'borrowernumber'}
1065 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'})
1066 if C4::Context->preference("IssueLog");
1070 =head2 GetLoanLength
1072 Get loan length for an itemtype, a borrower type and a branch
1074 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1079 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1080 my $dbh = C4::Context->dbh;
1083 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=?"
1086 # try to find issuelength & return the 1st available.
1087 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1088 $sth->execute( $borrowertype, $itemtype, $branchcode );
1089 my $loanlength = $sth->fetchrow_hashref;
1090 return $loanlength->{issuelength}
1091 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1093 $sth->execute( $borrowertype, $itemtype, "" );
1094 $loanlength = $sth->fetchrow_hashref;
1095 return $loanlength->{issuelength}
1096 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1098 $sth->execute( $borrowertype, "*", $branchcode );
1099 $loanlength = $sth->fetchrow_hashref;
1100 return $loanlength->{issuelength}
1101 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1103 $sth->execute( "*", $itemtype, $branchcode );
1104 $loanlength = $sth->fetchrow_hashref;
1105 return $loanlength->{issuelength}
1106 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1108 $sth->execute( $borrowertype, "*", "" );
1109 $loanlength = $sth->fetchrow_hashref;
1110 return $loanlength->{issuelength}
1111 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1113 $sth->execute( "*", "*", $branchcode );
1114 $loanlength = $sth->fetchrow_hashref;
1115 return $loanlength->{issuelength}
1116 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1118 $sth->execute( "*", $itemtype, "" );
1119 $loanlength = $sth->fetchrow_hashref;
1120 return $loanlength->{issuelength}
1121 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1123 $sth->execute( "*", "*", "" );
1124 $loanlength = $sth->fetchrow_hashref;
1125 return $loanlength->{issuelength}
1126 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1128 # if no rule is set => 21 days (hardcoded)
1134 ($doreturn, $messages, $iteminformation, $borrower) =
1135 &AddReturn($barcode, $branch);
1139 C<$barcode> is the bar code of the book being returned. C<$branch> is
1140 the code of the branch where the book is being returned.
1142 C<&AddReturn> returns a list of four items:
1144 C<$doreturn> is true iff the return succeeded.
1146 C<$messages> is a reference-to-hash giving the reason for failure:
1152 No item with this barcode exists. The value is C<$barcode>.
1156 The book is not currently on loan. The value is C<$barcode>.
1158 =item C<IsPermanent>
1160 The book's home branch is a permanent collection. If you have borrowed
1161 this book, you are not allowed to return it. The value is the code for
1162 the book's home branch.
1166 This book has been withdrawn/cancelled. The value should be ignored.
1170 The item was reserved. The value is a reference-to-hash whose keys are
1171 fields from the reserves table of the Koha database, and
1172 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1173 either C<Waiting>, C<Reserved>, or 0.
1177 C<$borrower> is a reference-to-hash, giving information about the
1178 patron who last borrowed the book.
1182 # FIXME - This API is bogus. There's no need to return $borrower and
1183 # $iteminformation; the caller can ask about those separately, if it
1184 # cares (it'd be inefficient to make two database calls instead of
1185 # one, but &GetMemberDetails and &getiteminformation can be
1186 # memoized if this is an issue).
1188 # The ($doreturn, $messages) tuple is redundant: if the return
1189 # succeeded, that's all the caller needs to know. So &AddReturn can
1190 # return 1 and 0 on success and failure, and set
1191 # $C4::Circulation::Circ2::errmsg to indicate the error. Or it can
1192 # return undef for success, and an error message on error (though this
1193 # is more C-ish than Perl-ish).
1196 my ( $barcode, $branch ) = @_;
1197 my $dbh = C4::Context->dbh;
1201 my $validTransfert = 0;
1202 my $reserveDone = 0;
1204 # get information on item
1205 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1206 unless ($iteminformation->{'itemnumber'} ) {
1207 $messages->{'BadBarcode'} = $barcode;
1211 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1212 $messages->{'NotIssued'} = $barcode;
1216 # check if the book is in a permanent collection....
1217 my $hbr = $iteminformation->{'homebranch'};
1218 my $branches = GetBranches();
1219 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1220 $messages->{'IsPermanent'} = $hbr;
1223 # check that the book has been cancelled
1224 if ( $iteminformation->{'wthdrawn'} ) {
1225 $messages->{'wthdrawn'} = 1;
1229 # new op dev : if the book returned in an other branch update the holding branch
1231 # update issues, thereby returning book (should push this out into another subroutine
1232 $borrower = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1234 # case of a return of document (deal with issues and holdingbranch)
1239 "update issues set returndate = now() where (borrowernumber = ?) and (itemnumber = ?) and (returndate is null)"
1241 $sth->execute( $borrower->{'borrowernumber'},
1242 $iteminformation->{'itemnumber'} );
1243 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1246 # continue to deal with returns cases, but not only if we have an issue
1248 # the holdingbranch is updated if the document is returned in an other location .
1249 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} )
1251 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1252 # reload iteminformation holdingbranch with the userenv value
1253 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1255 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1256 ($borrower) = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1258 # fix up the accounts.....
1259 if ( $iteminformation->{'itemlost'} ) {
1260 $messages->{'WasLost'} = 1;
1263 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1264 # check if we have a transfer for this document
1265 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1267 # if we have a transfer to do, we update the line of transfers with the datearrived
1269 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1272 "update branchtransfers set datearrived = now() where itemnumber= ? AND datearrived IS NULL"
1274 $sth->execute( $iteminformation->{'itemnumber'} );
1276 # 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'
1277 SetWaitingStatus( $iteminformation->{'itemnumber'} );
1280 $messages->{'WrongTransfer'} = $tobranch;
1281 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1283 $validTransfert = 1;
1286 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1287 # fix up the overdues in accounts...
1288 fixoverduesonreturn( $borrower->{'borrowernumber'},
1289 $iteminformation->{'itemnumber'} );
1291 # find reserves.....
1292 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1293 my ( $resfound, $resrec ) =
1294 CheckReserves( $iteminformation->{'itemnumber'} );
1296 $resrec->{'ResFound'} = $resfound;
1297 $messages->{'ResFound'} = $resrec;
1302 # Record the fact that this book was returned.
1304 $branch, 'return', '0', '',
1305 $iteminformation->{'itemnumber'},
1306 $iteminformation->{'itemtype'},
1307 $borrower->{'borrowernumber'}
1310 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'})
1311 if C4::Context->preference("ReturnLog");
1313 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1314 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1316 if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1317 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1318 dotransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1319 $messages->{'WasTransfered'} = 1;
1320 warn "was transfered";
1324 return ( $doreturn, $messages, $iteminformation, $borrower );
1327 =head2 fixoverdueonreturn
1329 &fixoverdueonreturn($brn,$itm);
1331 C<$brn> borrowernumber
1337 sub fixoverduesonreturn {
1338 my ( $borrowernumber, $item ) = @_;
1339 my $dbh = C4::Context->dbh;
1341 # check for overdue fine
1344 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1346 $sth->execute( $borrowernumber, $item );
1348 # alter fine to show that the book has been returned
1349 if ( my $data = $sth->fetchrow_hashref ) {
1352 "UPDATE accountlines SET accounttype='F' WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accountno = ?)"
1354 $usth->execute( $borrowernumber, $item, $data->{'accountno'} );
1363 $issues = &GetBorrowerIssue($itemnumber);
1365 Returns patrons currently having a book. nothing if item is not issued atm
1367 C<$itemnumber> is the itemnumber
1369 Returns an array of hashes
1373 my ( $itemnumber) = @_;
1374 return unless $itemnumber;
1375 my $dbh = C4::Context->dbh;
1379 my $today = POSIX::strftime("%Y%m%d", localtime);
1381 my $sth = $dbh->prepare(
1382 "SELECT * FROM issues
1383 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1385 issues.itemnumber=? AND returndate IS NULL ");
1386 $sth->execute($itemnumber);
1387 my $data = $sth->fetchrow_hashref;
1388 my $datedue = $data->{'date_due'};
1390 if ( $datedue < $today ) {
1391 $data->{'overdue'} = 1;
1393 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1398 =head2 GetItemIssues
1400 $issues = &GetBorrowerIssues($itemnumber, $history);
1402 Returns patrons that have issued a book
1404 C<$itemnumber> is the itemnumber
1405 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1407 Returns an array of hashes
1411 my ( $itemnumber,$history ) = @_;
1412 my $dbh = C4::Context->dbh;
1416 my $today = POSIX::strftime("%Y%m%d", localtime);
1418 my $sth = $dbh->prepare(
1419 "SELECT * FROM issues
1421 itemnumber=?".($history?"":" AND returndate IS NULL ").
1422 "ORDER BY issues.date_due DESC"
1424 $sth->execute($itemnumber);
1425 while ( my $data = $sth->fetchrow_hashref ) {
1426 my $datedue = $data->{'date_due'};
1428 if ( $datedue < $today ) {
1429 $data->{'overdue'} = 1;
1431 my $itemnumber = $data->{'itemnumber'};
1433 push @GetItemIssues, $data;
1436 return ( \@GetItemIssues );
1439 =head2 GetBorrowerIssues
1441 $issues = &GetBorrowerIssues($borrower);
1443 Returns a list of books currently on loan to a patron.
1445 C<$borrower->{borrowernumber}> is the borrower number of the patron
1446 whose issues we want to list.
1448 C<&GetBorrowerIssues> returns a PHP-style array: C<$issues> is a
1449 reference-to-hash whose keys are integers in the range 1...I<n>, where
1450 I<n> is the number of items on issue (either today or before today).
1451 C<$issues-E<gt>{I<n>}> is a reference-to-hash whose keys are all of
1452 the fields of the biblio, biblioitems, items, and issues fields of the
1453 Koha database for that particular item.
1457 sub GetBorrowerIssues {
1458 my ( $borrower ) = @_;
1459 my $dbh = C4::Context->dbh;
1460 my @GetBorrowerIssues;
1462 my $today = POSIX::strftime("%Y%m%d", localtime);
1464 my $sth = $dbh->prepare(
1465 "SELECT * FROM issues
1466 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1467 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1468 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1470 borrowernumber=? AND returndate IS NULL
1471 ORDER BY issues.date_due"
1473 $sth->execute($borrower->{'borrowernumber'});
1474 while ( my $data = $sth->fetchrow_hashref ) {
1475 my $datedue = $data->{'date_due'};
1477 if ( $datedue < $today ) {
1478 $data->{'overdue'} = 1;
1480 my $itemnumber = $data->{'itemnumber'};
1482 push @GetBorrowerIssues, $data;
1485 return ( \@GetBorrowerIssues );
1488 =head2 GetBiblioIssues
1490 $issues = GetBiblioIssues($biblionumber);
1492 this function get all issues from a biblionumber.
1495 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1496 tables issues and the firstname,surname & cardnumber from borrowers.
1500 sub GetBiblioIssues {
1501 my $biblionumber = shift;
1502 return undef unless $biblionumber;
1503 my $dbh = C4::Context->dbh;
1505 SELECT issues.*,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1507 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1508 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1509 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1510 LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1511 WHERE biblio.biblionumber = ?
1512 ORDER BY issues.timestamp
1514 my $sth = $dbh->prepare($query);
1515 $sth->execute($biblionumber);
1518 while ( my $data = $sth->fetchrow_hashref ) {
1519 push @issues, $data;
1524 =head2 CanBookBeRenewed
1526 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1528 Find out whether a borrowed item may be renewed.
1532 C<$dbh> is a DBI handle to the Koha database.
1534 C<$borrowernumber> is the borrower number of the patron who currently
1535 has the item on loan.
1537 C<$itemnumber> is the number of the item to renew.
1539 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1540 item must currently be on loan to the specified borrower; renewals
1541 must be allowed for the item's type; and the borrower must not have
1542 already renewed the loan.
1546 sub CanBookBeRenewed {
1548 # check renewal status
1549 my ( $borrowernumber, $itemnumber ) = @_;
1550 my $dbh = C4::Context->dbh;
1554 # Look in the issues table for this item, lent to this borrower,
1555 # and not yet returned.
1557 # FIXME - I think this function could be redone to use only one SQL call.
1558 my $sth1 = $dbh->prepare(
1559 "SELECT * FROM issues
1560 WHERE borrowernumber = ?
1562 AND returndate IS NULL"
1564 $sth1->execute( $borrowernumber, $itemnumber );
1565 if ( my $data1 = $sth1->fetchrow_hashref ) {
1567 # Found a matching item
1569 # See if this item may be renewed. This query is convoluted
1570 # because it's a bit messy: given the item number, we need to find
1571 # the biblioitem, which gives us the itemtype, which tells us
1572 # whether it may be renewed.
1573 my $sth2 = $dbh->prepare(
1574 "SELECT renewalsallowed FROM items
1575 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1576 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1577 WHERE items.itemnumber = ?
1580 $sth2->execute($itemnumber);
1581 if ( my $data2 = $sth2->fetchrow_hashref ) {
1582 $renews = $data2->{'renewalsallowed'};
1584 if ( $renews && $renews > $data1->{'renewals'} ) {
1588 my ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1592 ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1599 return ($renewokay);
1604 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1608 C<$env-E<gt>{branchcode}> is the code of the branch where the
1609 renewal is taking place.
1611 C<$env-E<gt>{usercode}> is the value to log in C<statistics.usercode>
1612 in the Koha database.
1614 C<$borrowernumber> is the borrower number of the patron who currently
1617 C<$itemnumber> is the number of the item to renew.
1619 C<$datedue> can be used to set the due date. If C<$datedue> is the
1620 empty string, C<&AddRenewal> will calculate the due date automatically
1621 from the book's item type. If you wish to set the due date manually,
1622 C<$datedue> should be in the form YYYY-MM-DD.
1628 my ( $borrowernumber, $itemnumber, $datedue ) = @_;
1629 my $dbh = C4::Context->dbh;
1631 # If the due date wasn't specified, calculate it by adding the
1632 # book's loan length to today's date.
1633 if ( $datedue eq "" ) {
1635 my $biblio = GetBiblioFromItemNumber($itemnumber);
1636 my $borrower = GetMemberDetails( $borrowernumber, 0 );
1637 my $loanlength = GetLoanLength(
1638 $borrower->{'categorycode'},
1639 $biblio->{'itemtype'},
1640 $borrower->{'branchcode'}
1642 my ( $due_year, $due_month, $due_day ) =
1643 Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1644 $datedue = "$due_year-$due_month-$due_day";
1648 # Find the issues record for this book
1650 $dbh->prepare("SELECT * FROM issues
1651 WHERE borrowernumber=?
1653 AND returndate IS NULL"
1655 $sth->execute( $borrowernumber, $itemnumber );
1656 my $issuedata = $sth->fetchrow_hashref;
1659 # Update the issues record to have the new due date, and a new count
1660 # of how many times it has been renewed.
1661 my $renews = $issuedata->{'renewals'} + 1;
1662 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1663 WHERE borrowernumber=?
1665 AND returndate IS NULL"
1667 $sth->execute( $datedue, $renews, $borrowernumber, $itemnumber );
1671 UpdateStats( C4::Context->userenv->{'branchcode'}, 'renew', '', '', $itemnumber );
1673 # Charge a new rental fee, if applicable?
1674 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1675 if ( $charge > 0 ) {
1676 my $accountno = getnextacctno( $borrowernumber );
1677 my $item = GetBiblioFromItemNumbe(r$itemnumber);
1678 $sth = $dbh->prepare(
1679 "INSERT INTO accountlines
1680 (borrowernumber,accountno,date,amount,
1681 description,accounttype,amountoutstanding,
1683 VALUES (?,?,now(),?,?,?,?,?)"
1685 $sth->execute( $borrowernumber, $accountno, $charge,
1686 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1687 'Rent', $charge, $itemnumber );
1692 =head2 GetIssuingCharges
1694 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1696 Calculate how much it would cost for a given patron to borrow a given
1697 item, including any applicable discounts.
1701 C<$itemnumber> is the item number of item the patron wishes to borrow.
1703 C<$borrowernumber> is the patron's borrower number.
1705 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1706 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1711 sub GetIssuingCharges {
1713 # calculate charges due
1714 my ( $itemnumber, $borrowernumber ) = @_;
1716 my $dbh = C4::Context->dbh;
1719 # Get the book's item type and rental charge (via its biblioitem).
1720 my $sth1 = $dbh->prepare(
1721 "SELECT itemtypes.itemtype,rentalcharge FROM items
1722 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1723 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1724 WHERE items.itemnumber =?
1727 $sth1->execute($itemnumber);
1728 if ( my $data1 = $sth1->fetchrow_hashref ) {
1729 $item_type = $data1->{'itemtype'};
1730 $charge = $data1->{'rentalcharge'};
1731 my $q2 = "SELECT rentaldiscount FROM borrowers
1732 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1733 WHERE borrowers.borrowernumber = ?
1734 AND issuingrules.itemtype = ?";
1735 my $sth2 = $dbh->prepare($q2);
1736 $sth2->execute( $borrowernumber, $item_type );
1737 if ( my $data2 = $sth2->fetchrow_hashref ) {
1738 my $discount = $data2->{'rentaldiscount'};
1739 if ( $discount eq 'NULL' ) {
1742 $charge = ( $charge * ( 100 - $discount ) ) / 100;
1748 return ( $charge, $item_type );
1751 =head2 AddIssuingCharge
1753 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1757 sub AddIssuingCharge {
1758 my ( $itemnumber, $borrowernumber, $charge ) = @_;
1759 my $dbh = C4::Context->dbh;
1760 my $nextaccntno = getnextacctno( $borrowernumber );
1762 INSERT INTO accountlines
1763 (borrowernumber, itemnumber, accountno,
1764 date, amount, description, accounttype,
1766 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1768 my $sth = $dbh->prepare($query);
1769 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1775 GetTransfers($itemnumber);
1780 my ($itemnumber) = @_;
1782 my $dbh = C4::Context->dbh;
1788 FROM branchtransfers
1789 WHERE itemnumber = ?
1790 AND datearrived IS NULL
1792 my $sth = $dbh->prepare($query);
1793 $sth->execute($itemnumber);
1794 my @row = $sth->fetchrow_array();
1800 =head2 GetTransfersFromTo
1802 @results = GetTransfersFromTo($frombranch,$tobranch);
1804 Returns the list of pending transfers between $from and $to branch
1808 sub GetTransfersFromTo {
1809 my ( $frombranch, $tobranch ) = @_;
1810 return unless ( $frombranch && $tobranch );
1811 my $dbh = C4::Context->dbh;
1813 SELECT itemnumber,datesent,frombranch
1814 FROM branchtransfers
1817 AND datearrived IS NULL
1819 my $sth = $dbh->prepare($query);
1820 $sth->execute( $frombranch, $tobranch );
1823 while ( my $data = $sth->fetchrow_hashref ) {
1824 push @gettransfers, $data;
1827 return (@gettransfers);
1830 =head2 DeleteTransfer
1832 &DeleteTransfer($itemnumber);
1836 sub DeleteTransfer {
1837 my ($itemnumber) = @_;
1838 my $dbh = C4::Context->dbh;
1839 my $sth = $dbh->prepare(
1840 "DELETE FROM branchtransfers
1842 AND datearrived IS NULL "
1844 $sth->execute($itemnumber);
1848 =head2 AnonymiseIssueHistory
1850 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1852 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1853 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1855 return the number of affected rows.
1859 sub AnonymiseIssueHistory {
1861 my $borrowernumber = shift;
1862 my $dbh = C4::Context->dbh;
1865 SET borrowernumber = NULL
1866 WHERE returndate < '".$date."'
1867 AND borrowernumber IS NOT NULL
1869 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1870 my $rows_affected = $dbh->do($query);
1871 return $rows_affected;
1874 =head2 updateWrongTransfer
1876 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1878 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
1882 sub updateWrongTransfer {
1883 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1884 my $dbh = C4::Context->dbh;
1885 # first step validate the actual line of transfert .
1888 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1890 $sth->execute($FromLibrary,$itemNumber);
1893 # second step create a new line of branchtransfer to the right location .
1894 dotransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1896 #third step changing holdingbranch of item
1897 UpdateHoldingbranch($FromLibrary,$itemNumber);
1900 =head2 UpdateHoldingbranch
1902 $items = UpdateHoldingbranch($branch,$itmenumber);
1903 Simple methode for updating hodlingbranch in items BDD line
1906 sub UpdateHoldingbranch {
1907 my ( $branch,$itmenumber ) = @_;
1908 my $dbh = C4::Context->dbh;
1909 # first step validate the actual line of transfert .
1912 "update items set holdingbranch = ? where itemnumber= ?"
1914 $sth->execute($branch,$itmenumber);
1926 Koha Developement team <info@koha.org>