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 ) = @_;
207 my $branches = GetBranches();
208 my $itemnumber = GetItemnumberFromBarcode( $barcode );
209 my $issue = GetItemIssue($itemnumber);
210 my $biblio = GetBiblioFromItemNumber($itemnumber);
213 if ( not $itemnumber ) {
214 $messages->{'BadBarcode'} = $barcode;
218 # get branches of book...
219 my $hbr = $biblio->{'homebranch'};
220 my $fbr = $biblio->{'holdingbranch'};
223 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
224 $messages->{'IsPermanent'} = $hbr;
227 # can't transfer book if is already there....
228 if ( $fbr eq $tbr ) {
229 $messages->{'DestinationEqualsHolding'} = 1;
233 # check if it is still issued to someone, return it...
234 if ($issue->{borrowernumber}) {
235 AddReturn( $barcode, $fbr );
236 $messages->{'WasReturned'} = $issue->{borrowernumber};
240 # That'll save a database query.
241 my ( $resfound, $resrec ) =
242 CheckReserves( $itemnumber );
243 if ( $resfound and not $ignoreRs ) {
244 $resrec->{'ResFound'} = $resfound;
246 # $messages->{'ResFound'} = $resrec;
250 #actually do the transfer....
252 dotransfer( $itemnumber, $fbr, $tbr );
254 # don't need to update MARC anymore, we do it in batch now
255 $messages->{'WasTransfered'} = 1;
257 return ( $dotransfer, $messages, $biblio );
261 # FIXME - This is only used in &transferbook. Why bother making it a
264 my ( $itm, $fbr, $tbr ) = @_;
266 my $dbh = C4::Context->dbh;
267 $itm = $dbh->quote($itm);
268 $fbr = $dbh->quote($fbr);
269 $tbr = $dbh->quote($tbr);
271 #new entry in branchtransfers....
273 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
274 VALUES ($itm, $fbr, now(), $tbr)"
277 #update holdingbranch in items .....
279 "UPDATE items set holdingbranch = $tbr WHERE items.itemnumber = $itm");
280 &ModDateLastSeen($itm);
281 &domarctransfer( $dbh, $itm );
285 ##New sub to dotransfer in marc tables as well. Not exported -TG 10/04/2006
287 my ( $dbh, $itemnumber ) = @_;
288 $itemnumber =~ s /\'//g; ##itemnumber seems to come with quotes-TG
291 "select biblionumber,holdingbranch from items where itemnumber=$itemnumber"
294 while ( my ( $biblionumber, $holdingbranch ) = $sth->fetchrow ) {
295 &ModItemInMarconefield( $biblionumber, $itemnumber,
296 'items.holdingbranch', $holdingbranch );
301 =head2 CanBookBeIssued
303 Check if a book can be issued.
305 my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued($borrower,$barcode,$year,$month,$day);
309 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
311 =item C<$barcode> is the bar code of the book being issued.
313 =item C<$year> C<$month> C<$day> contains the date of the return (in case it's forced by "stickyduedate".
321 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
322 Possible values are :
328 sticky due date is invalid
332 borrower gone with no address
336 borrower declared it's card lost
342 =head3 UNKNOWN_BARCODE
356 item is restricted (set by ??)
358 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
359 Possible values are :
367 renewing, not issuing
369 =head3 ISSUED_TO_ANOTHER
371 issued to someone else.
375 reserved for someone else.
379 sticky due date is invalid
383 if the borrower borrows to much things
387 # check if a book can be issued.
388 # returns an array with errors if any
391 my $borrower = shift;
392 my $biblionumber = shift;
393 my $cat_borrower = $borrower->{'categorycode'};
394 my $branch_borrower = $borrower->{'branchcode'};
395 my $dbh = C4::Context->dbh;
398 $dbh->prepare('select itemtype from biblioitems where biblionumber = ?');
399 $sth->execute($biblionumber);
400 my $type = $sth->fetchrow;
403 'select * from issuingrules where categorycode = ? and itemtype = ? and branchcode = ?'
406 # 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 ?");
409 "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"
413 'select COUNT(*) from issues where borrowernumber = ? and returndate is null'
417 # check the 3 parameters
418 $sth->execute( $cat_borrower, $type, $branch_borrower );
419 my $result = $sth->fetchrow_hashref;
421 # warn "==>".$result->{maxissueqty};
423 # Currently, using defined($result) ie on an entire hash reports whether memory
424 # for that aggregate has ever been allocated. As $result is used all over the place
425 # it would rarely return as undefined.
426 if ( defined( $result->{maxissueqty} ) ) {
427 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
428 my $alreadyissued = $sth2->fetchrow;
429 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
430 return ( "a $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
438 $sth->execute( $cat_borrower, $type, "" );
439 $result = $sth->fetchrow_hashref;
440 if ( defined( $result->{maxissueqty} ) ) {
441 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
442 my $alreadyissued = $sth2->fetchrow;
443 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
444 return ( "b $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
451 # check for itemtype=*
452 $sth->execute( $cat_borrower, "*", $branch_borrower );
453 $result = $sth->fetchrow_hashref;
454 if ( defined( $result->{maxissueqty} ) ) {
455 $sth3->execute( $borrower->{'borrowernumber'} );
456 my ($alreadyissued) = $sth3->fetchrow;
457 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
459 # warn "HERE : $alreadyissued / ($result->{maxissueqty} for $borrower->{'borrowernumber'}";
460 return ( "c $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
467 # check for borrowertype=*
468 $sth->execute( "*", $type, $branch_borrower );
469 $result = $sth->fetchrow_hashref;
470 if ( defined( $result->{maxissueqty} ) ) {
471 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
472 my $alreadyissued = $sth2->fetchrow;
473 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
474 return ( "d $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
481 $sth->execute( "*", "*", $branch_borrower );
482 $result = $sth->fetchrow_hashref;
483 if ( defined( $result->{maxissueqty} ) ) {
484 $sth3->execute( $borrower->{'borrowernumber'} );
485 my $alreadyissued = $sth3->fetchrow;
486 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
487 return ( "e $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
494 $sth->execute( "*", $type, "" );
495 $result = $sth->fetchrow_hashref;
496 if ( defined( $result->{maxissueqty} ) && $result->{maxissueqty} >= 0 ) {
497 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
498 my $alreadyissued = $sth2->fetchrow;
499 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
500 return ( "f $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
507 $sth->execute( $cat_borrower, "*", "" );
508 $result = $sth->fetchrow_hashref;
509 if ( defined( $result->{maxissueqty} ) ) {
510 $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
511 my $alreadyissued = $sth2->fetchrow;
512 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
513 return ( "g $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
520 $sth->execute( "*", "*", "" );
521 $result = $sth->fetchrow_hashref;
522 if ( defined( $result->{maxissueqty} ) ) {
523 $sth3->execute( $borrower->{'borrowernumber'} );
524 my $alreadyissued = $sth3->fetchrow;
525 if ( $result->{'maxissueqty'} <= $alreadyissued ) {
526 return ( "h $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
537 @issues = &itemissues($biblioitemnumber, $biblio);
539 Looks up information about who has borrowed the bookZ<>(s) with the
540 given biblioitemnumber.
542 C<$biblio> is ignored.
544 C<&itemissues> returns an array of references-to-hash. The keys
545 include the fields from the C<items> table in the Koha database.
546 Additional keys include:
552 If the item is currently on loan, this gives the due date.
554 If the item is not on loan, then this is either "Available" or
555 "Cancelled", if the item has been withdrawn.
559 If the item is currently on loan, this gives the card number of the
560 patron who currently has the item.
562 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
564 These give the timestamp for the last three times the item was
567 =item C<card0>, C<card1>, C<card2>
569 The card number of the last three patrons who borrowed this item.
571 =item C<borrower0>, C<borrower1>, C<borrower2>
573 The borrower number of the last three patrons who borrowed this item.
581 my ( $bibitem, $biblio ) = @_;
582 my $dbh = C4::Context->dbh;
584 # FIXME - If this function die()s, the script will abort, and the
585 # user won't get anything; depending on how far the script has
586 # gotten, the user might get a blank page. It would be much better
587 # to at least print an error message. The easiest way to do this
588 # is to set $SIG{__DIE__}.
590 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
595 $sth->execute($bibitem) || die $sth->errstr;
597 while ( my $data = $sth->fetchrow_hashref ) {
599 # Find out who currently has this item.
600 # FIXME - Wouldn't it be better to do this as a left join of
601 # some sort? Currently, this code assumes that if
602 # fetchrow_hashref() fails, then the book is on the shelf.
603 # fetchrow_hashref() can fail for any number of reasons (e.g.,
604 # database server crash), not just because no items match the
606 my $sth2 = $dbh->prepare(
607 "SELECT * FROM issues
608 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
610 AND returndate IS NULL
614 $sth2->execute( $data->{'itemnumber'} );
615 if ( my $data2 = $sth2->fetchrow_hashref ) {
616 $data->{'date_due'} = $data2->{'date_due'};
617 $data->{'card'} = $data2->{'cardnumber'};
618 $data->{'borrower'} = $data2->{'borrowernumber'};
621 if ( $data->{'wthdrawn'} eq '1' ) {
622 $data->{'date_due'} = 'Cancelled';
625 $data->{'date_due'} = 'Available';
631 # Find the last 3 people who borrowed this item.
632 $sth2 = $dbh->prepare(
633 "SELECT * FROM issues, borrowers
634 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
636 AND returndate IS NOT NULL
637 ORDER BY returndate DESC,timestamp DESC"
640 # $sth2 = $dbh->prepare("
643 # LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
644 # WHERE itemnumber = ?
645 # AND returndate is not NULL
646 # ORDER BY returndate DESC,timestamp DESC
649 $sth2->execute( $data->{'itemnumber'} );
650 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
651 { # FIXME : error if there is less than 3 pple borrowing this item
652 if ( my $data2 = $sth2->fetchrow_hashref ) {
653 $data->{"timestamp$i2"} = $data2->{'timestamp'};
654 $data->{"card$i2"} = $data2->{'cardnumber'};
655 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
660 $results[$i] = $data;
668 =head2 CanBookBeIssued
670 $issuingimpossible, $needsconfirmation =
671 CanBookBeIssued( $borrower, $barcode, $year, $month, $day, $inprocess );
673 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
677 sub CanBookBeIssued {
678 my ( $borrower, $barcode, $year, $month, $day, $inprocess ) = @_;
679 my %needsconfirmation; # filled with problems that needs confirmations
680 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
681 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
682 my $issue = GetItemIssue($item->{itemnumber});
683 my $dbh = C4::Context->dbh;
688 my ( $duedate, $invalidduedate ) = fixdate( $year, $month, $day );
689 $issuingimpossible{INVALID_DATE} = 1 if ($invalidduedate);
694 if ( $borrower->{flags}->{GNA} ) {
695 $issuingimpossible{GNA} = 1;
697 if ( $borrower->{flags}->{'LOST'} ) {
698 $issuingimpossible{CARD_LOST} = 1;
700 if ( $borrower->{flags}->{'DBARRED'} ) {
701 $issuingimpossible{DEBARRED} = 1;
703 if ( Date_to_Days(Today) >
704 Date_to_Days( split "-", $borrower->{'dateexpiry'} ) )
708 #if (&Date_Cmp(&ParseDate($borrower->{expiry}),&ParseDate("today"))<0) {
709 $issuingimpossible{EXPIRED} = 1;
718 checkaccount( $borrower->{'borrowernumber'}, $dbh, $duedate );
719 if ( C4::Context->preference("IssuingInProcess") ) {
720 my $amountlimit = C4::Context->preference("noissuescharge");
721 if ( $amount > $amountlimit && !$inprocess ) {
722 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
724 elsif ( $amount <= $amountlimit && !$inprocess ) {
725 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
730 $needsconfirmation{DEBT} = $amount;
735 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
737 my $toomany = TooMany( $borrower, $item->{biblionumber} );
738 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
743 unless ( $item->{barcode} ) {
744 $issuingimpossible{UNKNOWN_BARCODE} = 1;
746 if ( $item->{'notforloan'}
747 && $item->{'notforloan'} > 0 )
749 $issuingimpossible{NOT_FOR_LOAN} = 1;
751 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
753 $issuingimpossible{WTHDRAWN} = 1;
755 if ( $item->{'restricted'}
756 && $item->{'restricted'} == 1 )
758 $issuingimpossible{RESTRICTED} = 1;
760 if ( C4::Context->preference("IndependantBranches") ) {
761 my $userenv = C4::Context->userenv;
762 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
763 $issuingimpossible{NOTSAMEBRANCH} = 1
764 if ( $item->{'holdingbranch'} ne $userenv->{branch} );
769 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
771 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
774 # Already issued to current borrower. Ask whether the loan should
776 my ($CanBookBeRenewed) = CanBookBeRenewed(
777 $borrower->{'borrowernumber'},
778 $item->{'itemnumber'}
780 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
781 $issuingimpossible{NO_MORE_RENEWALS} = 1;
785 # $needsconfirmation{RENEW_ISSUE} = 1;
788 elsif ($issue->{borrowernumber}) {
790 # issued to someone else
791 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
793 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
794 $needsconfirmation{ISSUED_TO_ANOTHER} =
795 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
798 # See if the item is on reserve.
799 my ( $restype, $res ) = CheckReserves( $item->{'itemnumber'} );
801 my $resbor = $res->{'borrowernumber'};
802 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
805 # The item is on reserve and waiting, but has been
806 # reserved by some other patron.
807 my ( $resborrower, $flags ) =
808 GetMemberDetails( $resbor, 0 );
809 my $branches = GetBranches();
811 $branches->{ $res->{'branchcode'} }->{'branchname'};
812 $needsconfirmation{RESERVE_WAITING} =
813 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
815 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
817 elsif ( $restype eq "Reserved" ) {
819 # The item is on reserve for someone else.
820 my ( $resborrower, $flags ) =
821 GetMemberDetails( $resbor, 0 );
822 my $branches = GetBranches();
824 $branches->{ $res->{'branchcode'} }->{'branchname'};
825 $needsconfirmation{RESERVED} =
826 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
829 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" )
831 if ( $borrower->{'categorycode'} eq 'W' ) {
832 my %issuingimpossible;
833 return ( \%issuingimpossible, \%needsconfirmation );
836 return ( \%issuingimpossible, \%needsconfirmation );
840 return ( \%issuingimpossible, \%needsconfirmation );
846 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
848 &AddIssue($borrower,$barcode,$date)
852 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
854 =item C<$barcode> is the bar code of the book being issued.
856 =item C<$date> contains the max date of return. calculated if empty.
858 AddIssue does the following things :
859 - step 0�: check that there is a borrowernumber & a barcode provided
860 - check for RENEWAL (book issued & being issued to the same patron)
861 - renewal YES = Calculate Charge & renew
863 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
865 - fill reserve if reserve to this patron
866 - cancel reserve or not, otherwise
867 * TRANSFERT PENDING ?
868 - complete the transfert
876 my ( $borrower, $barcode, $date, $cancelreserve ) = @_;
878 my $dbh = C4::Context->dbh;
879 if ($borrower and $barcode){
880 # my ($borrower, $flags) = &GetMemberDetails($borrowernumber, 0);
881 # find which item we issue
882 my $item = GetItem('', $barcode);
884 # get actual issuing if there is one
885 my $actualissue = GetItemIssue( $item->{itemnumber});
887 # get biblioinformation for this item
888 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
891 # check if we just renew the issue.
893 if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
894 # we renew, do we need to add some charge ?
895 my ( $charge, $itemtype ) = GetIssuingCharges(
896 $item->{'itemnumber'},
897 $borrower->{'borrowernumber'}
901 $item->{'itemnumber'},
902 $borrower->{'borrowernumber'}, $charge
904 $item->{'charge'} = $charge;
907 C4::Context->userenv->{'branch'},
909 '', $item->{'itemnumber'},
910 $biblio->{'itemtype'}, $borrower->{'borrowernumber'}
913 $borrower->{'borrowernumber'},
914 $item->{'itemnumber'}
917 else {# it's NOT a renewal
918 if ( $actualissue->{borrowernumber}) {
919 # This book is currently on loan, but not to the person
920 # who wants to borrow it now. mark it returned before issuing to the new borrower
923 C4::Context->userenv->{'branch'}
927 # See if the item is on reserve.
928 my ( $restype, $res ) =
929 CheckReserves( $item->{'itemnumber'} );
931 my $resbor = $res->{'borrowernumber'};
932 if ( $resbor eq $borrower->{'borrowernumber'} ) {
934 # The item is reserved by the current patron
937 elsif ( $restype eq "Waiting" ) {
940 # The item is on reserve and waiting, but has been
941 # reserved by some other patron.
942 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
943 my $branches = GetBranches();
945 $branches->{ $res->{'branchcode'} }->{'branchname'};
946 if ($cancelreserve) {
947 CancelReserve( 0, $res->{'itemnumber'},
948 $res->{'borrowernumber'} );
952 # set waiting reserve to first in reserve queue as book isn't waiting now
955 $res->{'biblionumber'},
956 $res->{'borrowernumber'},
961 elsif ( $restype eq "Reserved" ) {
964 # The item is reserved by someone else.
965 my ( $resborrower, $flags ) =
966 GetMemberDetails( $resbor, 0 );
967 my $branches = GetBranches();
969 $branches->{ $res->{'branchcode'} }->{'branchname'};
970 if ($cancelreserve) { # cancel reserves on this item
971 CancelReserve( 0, $res->{'itemnumber'},
972 $res->{'borrowernumber'} );
977 # Starting process for transfer job (checking transfert and validate it if we have one)
978 my ($datesent) = GetTransfers($item->{'itemnumber'});
980 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
983 "UPDATE branchtransfers
984 SET datearrived = now(),
986 comments = 'Forced branchtransfert'
987 WHERE itemnumber= ? AND datearrived IS NULL"
989 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
993 # Record in the database the fact that the book was issued.
997 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
1000 my $loanlength = GetLoanLength(
1001 $borrower->{'categorycode'},
1002 $biblio->{'itemtype'},
1003 $borrower->{'branchcode'}
1005 my $datedue = time + ($loanlength) * 86400;
1006 my @datearr = localtime($datedue);
1008 ( 1900 + $datearr[5] ) . "-"
1009 . ( $datearr[4] + 1 ) . "-"
1015 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
1016 if ( C4::Context->preference('ReturnBeforeExpiry')
1017 && $dateduef gt $borrower->{dateexpiry} )
1019 $dateduef = $borrower->{dateexpiry};
1022 $borrower->{'borrowernumber'},
1023 $item->{'itemnumber'},
1024 strftime( "%Y-%m-%d", localtime ),$dateduef, C4::Context->userenv->{'branch'}
1027 $item->{'issues'}++;
1030 "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed = now() WHERE itemnumber=?");
1033 C4::Context->userenv->{'branch'},
1034 $item->{'itemnumber'}
1037 &ModDateLastSeen( $item->{'itemnumber'} );
1038 # If it costs to borrow this book, charge it to the patron's account.
1039 my ( $charge, $itemtype ) = GetIssuingCharges(
1040 $item->{'itemnumber'},
1041 $borrower->{'borrowernumber'}
1043 if ( $charge > 0 ) {
1045 $item->{'itemnumber'},
1046 $borrower->{'borrowernumber'}, $charge
1048 $item->{'charge'} = $charge;
1051 # Record the fact that this book was issued.
1053 C4::Context->userenv->{'branch'},
1055 '', $item->{'itemnumber'},
1056 $item->{'itemtype'}, $borrower->{'borrowernumber'}
1060 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'})
1061 if C4::Context->preference("IssueLog");
1065 =head2 GetLoanLength
1067 Get loan length for an itemtype, a borrower type and a branch
1069 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1074 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1075 my $dbh = C4::Context->dbh;
1078 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=?"
1081 # try to find issuelength & return the 1st available.
1082 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1083 $sth->execute( $borrowertype, $itemtype, $branchcode );
1084 my $loanlength = $sth->fetchrow_hashref;
1085 return $loanlength->{issuelength}
1086 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1088 $sth->execute( $borrowertype, $itemtype, "" );
1089 $loanlength = $sth->fetchrow_hashref;
1090 return $loanlength->{issuelength}
1091 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1093 $sth->execute( $borrowertype, "*", $branchcode );
1094 $loanlength = $sth->fetchrow_hashref;
1095 return $loanlength->{issuelength}
1096 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1098 $sth->execute( "*", $itemtype, $branchcode );
1099 $loanlength = $sth->fetchrow_hashref;
1100 return $loanlength->{issuelength}
1101 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1103 $sth->execute( $borrowertype, "*", "" );
1104 $loanlength = $sth->fetchrow_hashref;
1105 return $loanlength->{issuelength}
1106 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1108 $sth->execute( "*", "*", $branchcode );
1109 $loanlength = $sth->fetchrow_hashref;
1110 return $loanlength->{issuelength}
1111 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1113 $sth->execute( "*", $itemtype, "" );
1114 $loanlength = $sth->fetchrow_hashref;
1115 return $loanlength->{issuelength}
1116 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1118 $sth->execute( "*", "*", "" );
1119 $loanlength = $sth->fetchrow_hashref;
1120 return $loanlength->{issuelength}
1121 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1123 # if no rule is set => 21 days (hardcoded)
1129 ($doreturn, $messages, $iteminformation, $borrower) =
1130 &AddReturn($barcode, $branch);
1134 C<$barcode> is the bar code of the book being returned. C<$branch> is
1135 the code of the branch where the book is being returned.
1137 C<&AddReturn> returns a list of four items:
1139 C<$doreturn> is true iff the return succeeded.
1141 C<$messages> is a reference-to-hash giving the reason for failure:
1147 No item with this barcode exists. The value is C<$barcode>.
1151 The book is not currently on loan. The value is C<$barcode>.
1153 =item C<IsPermanent>
1155 The book's home branch is a permanent collection. If you have borrowed
1156 this book, you are not allowed to return it. The value is the code for
1157 the book's home branch.
1161 This book has been withdrawn/cancelled. The value should be ignored.
1165 The item was reserved. The value is a reference-to-hash whose keys are
1166 fields from the reserves table of the Koha database, and
1167 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1168 either C<Waiting>, C<Reserved>, or 0.
1172 C<$borrower> is a reference-to-hash, giving information about the
1173 patron who last borrowed the book.
1177 # FIXME - This API is bogus. There's no need to return $borrower and
1178 # $iteminformation; the caller can ask about those separately, if it
1179 # cares (it'd be inefficient to make two database calls instead of
1180 # one, but &GetMemberDetails and &getiteminformation can be
1181 # memoized if this is an issue).
1183 # The ($doreturn, $messages) tuple is redundant: if the return
1184 # succeeded, that's all the caller needs to know. So &AddReturn can
1185 # return 1 and 0 on success and failure, and set
1186 # $C4::Circulation::Circ2::errmsg to indicate the error. Or it can
1187 # return undef for success, and an error message on error (though this
1188 # is more C-ish than Perl-ish).
1191 my ( $barcode, $branch ) = @_;
1192 my $dbh = C4::Context->dbh;
1196 my $validTransfert = 0;
1197 my $reserveDone = 0;
1199 # get information on item
1200 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1201 unless ($iteminformation->{'itemnumber'} ) {
1202 $messages->{'BadBarcode'} = $barcode;
1206 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1207 $messages->{'NotIssued'} = $barcode;
1211 # check if the book is in a permanent collection....
1212 my $hbr = $iteminformation->{'homebranch'};
1213 my $branches = GetBranches();
1214 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1215 $messages->{'IsPermanent'} = $hbr;
1218 # check that the book has been cancelled
1219 if ( $iteminformation->{'wthdrawn'} ) {
1220 $messages->{'wthdrawn'} = 1;
1224 # new op dev : if the book returned in an other branch update the holding branch
1226 # update issues, thereby returning book (should push this out into another subroutine
1227 $borrower = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1229 # case of a return of document (deal with issues and holdingbranch)
1234 "update issues set returndate = now() where (borrowernumber = ?) and (itemnumber = ?) and (returndate is null)"
1236 $sth->execute( $borrower->{'borrowernumber'},
1237 $iteminformation->{'itemnumber'} );
1238 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1241 # continue to deal with returns cases, but not only if we have an issue
1243 # the holdingbranch is updated if the document is returned in an other location .
1244 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} )
1246 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1247 # reload iteminformation holdingbranch with the userenv value
1248 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1250 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1251 ($borrower) = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1253 # fix up the accounts.....
1254 if ( $iteminformation->{'itemlost'} ) {
1255 $messages->{'WasLost'} = 1;
1258 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1259 # check if we have a transfer for this document
1260 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1262 # if we have a transfer to do, we update the line of transfers with the datearrived
1264 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1267 "update branchtransfers set datearrived = now() where itemnumber= ? AND datearrived IS NULL"
1269 $sth->execute( $iteminformation->{'itemnumber'} );
1271 # 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'
1272 SetWaitingStatus( $iteminformation->{'itemnumber'} );
1275 $messages->{'WrongTransfer'} = $tobranch;
1276 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1278 $validTransfert = 1;
1281 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1282 # fix up the overdues in accounts...
1283 fixoverduesonreturn( $borrower->{'borrowernumber'},
1284 $iteminformation->{'itemnumber'} );
1286 # find reserves.....
1287 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1288 my ( $resfound, $resrec ) =
1289 CheckReserves( $iteminformation->{'itemnumber'} );
1291 $resrec->{'ResFound'} = $resfound;
1292 $messages->{'ResFound'} = $resrec;
1297 # Record the fact that this book was returned.
1299 $branch, 'return', '0', '',
1300 $iteminformation->{'itemnumber'},
1301 $iteminformation->{'itemtype'},
1302 $borrower->{'borrowernumber'}
1305 &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'})
1306 if C4::Context->preference("ReturnLog");
1308 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1309 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1311 if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1312 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1313 dotransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1314 $messages->{'WasTransfered'} = 1;
1315 warn "was transfered";
1319 return ( $doreturn, $messages, $iteminformation, $borrower );
1322 =head2 fixoverdueonreturn
1324 &fixoverdueonreturn($brn,$itm);
1326 C<$brn> borrowernumber
1332 sub fixoverduesonreturn {
1333 my ( $borrowernumber, $item ) = @_;
1334 my $dbh = C4::Context->dbh;
1336 # check for overdue fine
1339 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1341 $sth->execute( $borrowernumber, $item );
1343 # alter fine to show that the book has been returned
1344 if ( my $data = $sth->fetchrow_hashref ) {
1347 "UPDATE accountlines SET accounttype='F' WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accountno = ?)"
1349 $usth->execute( $borrowernumber, $item, $data->{'accountno'} );
1358 $issues = &GetBorrowerIssue($itemnumber);
1360 Returns patrons currently having a book. nothing if item is not issued atm
1362 C<$itemnumber> is the itemnumber
1364 Returns an array of hashes
1368 my ( $itemnumber) = @_;
1369 return unless $itemnumber;
1370 my $dbh = C4::Context->dbh;
1374 my $today = POSIX::strftime("%Y%m%d", localtime);
1376 my $sth = $dbh->prepare(
1377 "SELECT * FROM issues
1378 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1380 issues.itemnumber=? AND returndate IS NULL ");
1381 $sth->execute($itemnumber);
1382 my $data = $sth->fetchrow_hashref;
1383 my $datedue = $data->{'date_due'};
1385 if ( $datedue < $today ) {
1386 $data->{'overdue'} = 1;
1388 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1393 =head2 GetItemIssues
1395 $issues = &GetBorrowerIssues($itemnumber, $history);
1397 Returns patrons that have issued a book
1399 C<$itemnumber> is the itemnumber
1400 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1402 Returns an array of hashes
1406 my ( $itemnumber,$history ) = @_;
1407 my $dbh = C4::Context->dbh;
1411 my $today = POSIX::strftime("%Y%m%d", localtime);
1413 my $sth = $dbh->prepare(
1414 "SELECT * FROM issues
1416 itemnumber=?".($history?"":" AND returndate IS NULL ").
1417 "ORDER BY issues.date_due DESC"
1419 $sth->execute($itemnumber);
1420 while ( my $data = $sth->fetchrow_hashref ) {
1421 my $datedue = $data->{'date_due'};
1423 if ( $datedue < $today ) {
1424 $data->{'overdue'} = 1;
1426 my $itemnumber = $data->{'itemnumber'};
1428 push @GetItemIssues, $data;
1431 return ( \@GetItemIssues );
1434 =head2 GetBorrowerIssues
1436 $issues = &GetBorrowerIssues($borrower);
1438 Returns a list of books currently on loan to a patron.
1440 C<$borrower->{borrowernumber}> is the borrower number of the patron
1441 whose issues we want to list.
1443 C<&GetBorrowerIssues> returns a PHP-style array: C<$issues> is a
1444 reference-to-hash whose keys are integers in the range 1...I<n>, where
1445 I<n> is the number of items on issue (either today or before today).
1446 C<$issues-E<gt>{I<n>}> is a reference-to-hash whose keys are all of
1447 the fields of the biblio, biblioitems, items, and issues fields of the
1448 Koha database for that particular item.
1452 sub GetBorrowerIssues {
1453 my ( $borrower ) = @_;
1454 my $dbh = C4::Context->dbh;
1455 my @GetBorrowerIssues;
1457 my $today = POSIX::strftime("%Y%m%d", localtime);
1459 my $sth = $dbh->prepare(
1460 "SELECT * FROM issues
1461 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1462 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1463 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1465 borrowernumber=? AND returndate IS NULL
1466 ORDER BY issues.date_due"
1468 $sth->execute($borrower->{'borrowernumber'});
1469 while ( my $data = $sth->fetchrow_hashref ) {
1470 my $datedue = $data->{'date_due'};
1472 if ( $datedue < $today ) {
1473 $data->{'overdue'} = 1;
1475 my $itemnumber = $data->{'itemnumber'};
1477 push @GetBorrowerIssues, $data;
1480 return ( \@GetBorrowerIssues );
1483 =head2 GetBiblioIssues
1485 $issues = GetBiblioIssues($biblionumber);
1487 this function get all issues from a biblionumber.
1490 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1491 tables issues and the firstname,surname & cardnumber from borrowers.
1495 sub GetBiblioIssues {
1496 my $biblionumber = shift;
1497 return undef unless $biblionumber;
1498 my $dbh = C4::Context->dbh;
1500 SELECT issues.*,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1502 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1503 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1504 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1505 LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1506 WHERE biblio.biblionumber = ?
1507 ORDER BY issues.timestamp
1509 my $sth = $dbh->prepare($query);
1510 $sth->execute($biblionumber);
1513 while ( my $data = $sth->fetchrow_hashref ) {
1514 push @issues, $data;
1519 =head2 CanBookBeRenewed
1521 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1523 Find out whether a borrowed item may be renewed.
1525 C<$dbh> is a DBI handle to the Koha database.
1527 C<$borrowernumber> is the borrower number of the patron who currently
1528 has the item on loan.
1530 C<$itemnumber> is the number of the item to renew.
1532 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1533 item must currently be on loan to the specified borrower; renewals
1534 must be allowed for the item's type; and the borrower must not have
1535 already renewed the loan.
1539 sub CanBookBeRenewed {
1541 # check renewal status
1542 my ( $borrowernumber, $itemnumber ) = @_;
1543 my $dbh = C4::Context->dbh;
1547 # Look in the issues table for this item, lent to this borrower,
1548 # and not yet returned.
1550 # FIXME - I think this function could be redone to use only one SQL call.
1551 my $sth1 = $dbh->prepare(
1552 "SELECT * FROM issues
1553 WHERE borrowernumber = ?
1555 AND returndate IS NULL"
1557 $sth1->execute( $borrowernumber, $itemnumber );
1558 if ( my $data1 = $sth1->fetchrow_hashref ) {
1560 # Found a matching item
1562 # See if this item may be renewed. This query is convoluted
1563 # because it's a bit messy: given the item number, we need to find
1564 # the biblioitem, which gives us the itemtype, which tells us
1565 # whether it may be renewed.
1566 my $sth2 = $dbh->prepare(
1567 "SELECT renewalsallowed FROM items
1568 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1569 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1570 WHERE items.itemnumber = ?
1573 $sth2->execute($itemnumber);
1574 if ( my $data2 = $sth2->fetchrow_hashref ) {
1575 $renews = $data2->{'renewalsallowed'};
1577 if ( $renews && $renews > $data1->{'renewals'} ) {
1581 my ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1585 ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1592 return ($renewokay);
1597 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1601 C<$borrowernumber> is the borrower number of the patron who currently
1604 C<$itemnumber> is the number of the item to renew.
1606 C<$datedue> can be used to set the due date. If C<$datedue> is the
1607 empty string, C<&AddRenewal> will calculate the due date automatically
1608 from the book's item type. If you wish to set the due date manually,
1609 C<$datedue> should be in the form YYYY-MM-DD.
1615 my ( $borrowernumber, $itemnumber, $datedue ) = @_;
1616 my $dbh = C4::Context->dbh;
1618 # If the due date wasn't specified, calculate it by adding the
1619 # book's loan length to today's date.
1620 if ( $datedue eq "" ) {
1622 my $biblio = GetBiblioFromItemNumber($itemnumber);
1623 my $borrower = GetMemberDetails( $borrowernumber, 0 );
1624 my $loanlength = GetLoanLength(
1625 $borrower->{'categorycode'},
1626 $biblio->{'itemtype'},
1627 $borrower->{'branchcode'}
1629 my ( $due_year, $due_month, $due_day ) =
1630 Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1631 $datedue = "$due_year-$due_month-$due_day";
1635 # Find the issues record for this book
1637 $dbh->prepare("SELECT * FROM issues
1638 WHERE borrowernumber=?
1640 AND returndate IS NULL"
1642 $sth->execute( $borrowernumber, $itemnumber );
1643 my $issuedata = $sth->fetchrow_hashref;
1646 # Update the issues record to have the new due date, and a new count
1647 # of how many times it has been renewed.
1648 my $renews = $issuedata->{'renewals'} + 1;
1649 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1650 WHERE borrowernumber=?
1652 AND returndate IS NULL"
1654 $sth->execute( $datedue, $renews, $borrowernumber, $itemnumber );
1658 UpdateStats( C4::Context->userenv->{'branchcode'}, 'renew', '', '', $itemnumber );
1660 # Charge a new rental fee, if applicable?
1661 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1662 if ( $charge > 0 ) {
1663 my $accountno = getnextacctno( $borrowernumber );
1664 my $item = GetBiblioFromItemNumbe(r$itemnumber);
1665 $sth = $dbh->prepare(
1666 "INSERT INTO accountlines
1667 (borrowernumber,accountno,date,amount,
1668 description,accounttype,amountoutstanding,
1670 VALUES (?,?,now(),?,?,?,?,?)"
1672 $sth->execute( $borrowernumber, $accountno, $charge,
1673 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1674 'Rent', $charge, $itemnumber );
1679 =head2 GetIssuingCharges
1681 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1683 Calculate how much it would cost for a given patron to borrow a given
1684 item, including any applicable discounts.
1686 C<$itemnumber> is the item number of item the patron wishes to borrow.
1688 C<$borrowernumber> is the patron's borrower number.
1690 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1691 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1696 sub GetIssuingCharges {
1698 # calculate charges due
1699 my ( $itemnumber, $borrowernumber ) = @_;
1701 my $dbh = C4::Context->dbh;
1704 # Get the book's item type and rental charge (via its biblioitem).
1705 my $sth1 = $dbh->prepare(
1706 "SELECT itemtypes.itemtype,rentalcharge FROM items
1707 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1708 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1709 WHERE items.itemnumber =?
1712 $sth1->execute($itemnumber);
1713 if ( my $data1 = $sth1->fetchrow_hashref ) {
1714 $item_type = $data1->{'itemtype'};
1715 $charge = $data1->{'rentalcharge'};
1716 my $q2 = "SELECT rentaldiscount FROM borrowers
1717 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1718 WHERE borrowers.borrowernumber = ?
1719 AND issuingrules.itemtype = ?";
1720 my $sth2 = $dbh->prepare($q2);
1721 $sth2->execute( $borrowernumber, $item_type );
1722 if ( my $data2 = $sth2->fetchrow_hashref ) {
1723 my $discount = $data2->{'rentaldiscount'};
1724 if ( $discount eq 'NULL' ) {
1727 $charge = ( $charge * ( 100 - $discount ) ) / 100;
1733 return ( $charge, $item_type );
1736 =head2 AddIssuingCharge
1738 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1742 sub AddIssuingCharge {
1743 my ( $itemnumber, $borrowernumber, $charge ) = @_;
1744 my $dbh = C4::Context->dbh;
1745 my $nextaccntno = getnextacctno( $borrowernumber );
1747 INSERT INTO accountlines
1748 (borrowernumber, itemnumber, accountno,
1749 date, amount, description, accounttype,
1751 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1753 my $sth = $dbh->prepare($query);
1754 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1760 GetTransfers($itemnumber);
1765 my ($itemnumber) = @_;
1767 my $dbh = C4::Context->dbh;
1773 FROM branchtransfers
1774 WHERE itemnumber = ?
1775 AND datearrived IS NULL
1777 my $sth = $dbh->prepare($query);
1778 $sth->execute($itemnumber);
1779 my @row = $sth->fetchrow_array();
1785 =head2 GetTransfersFromTo
1787 @results = GetTransfersFromTo($frombranch,$tobranch);
1789 Returns the list of pending transfers between $from and $to branch
1793 sub GetTransfersFromTo {
1794 my ( $frombranch, $tobranch ) = @_;
1795 return unless ( $frombranch && $tobranch );
1796 my $dbh = C4::Context->dbh;
1798 SELECT itemnumber,datesent,frombranch
1799 FROM branchtransfers
1802 AND datearrived IS NULL
1804 my $sth = $dbh->prepare($query);
1805 $sth->execute( $frombranch, $tobranch );
1808 while ( my $data = $sth->fetchrow_hashref ) {
1809 push @gettransfers, $data;
1812 return (@gettransfers);
1815 =head2 DeleteTransfer
1817 &DeleteTransfer($itemnumber);
1821 sub DeleteTransfer {
1822 my ($itemnumber) = @_;
1823 my $dbh = C4::Context->dbh;
1824 my $sth = $dbh->prepare(
1825 "DELETE FROM branchtransfers
1827 AND datearrived IS NULL "
1829 $sth->execute($itemnumber);
1833 =head2 AnonymiseIssueHistory
1835 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1837 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1838 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1840 return the number of affected rows.
1844 sub AnonymiseIssueHistory {
1846 my $borrowernumber = shift;
1847 my $dbh = C4::Context->dbh;
1850 SET borrowernumber = NULL
1851 WHERE returndate < '".$date."'
1852 AND borrowernumber IS NOT NULL
1854 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1855 my $rows_affected = $dbh->do($query);
1856 return $rows_affected;
1859 =head2 updateWrongTransfer
1861 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1863 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
1867 sub updateWrongTransfer {
1868 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1869 my $dbh = C4::Context->dbh;
1870 # first step validate the actual line of transfert .
1873 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1875 $sth->execute($FromLibrary,$itemNumber);
1878 # second step create a new line of branchtransfer to the right location .
1879 dotransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1881 #third step changing holdingbranch of item
1882 UpdateHoldingbranch($FromLibrary,$itemNumber);
1885 =head2 UpdateHoldingbranch
1887 $items = UpdateHoldingbranch($branch,$itmenumber);
1888 Simple methode for updating hodlingbranch in items BDD line
1891 sub UpdateHoldingbranch {
1892 my ( $branch,$itmenumber ) = @_;
1893 my $dbh = C4::Context->dbh;
1894 # first step validate the actual line of transfert .
1897 "update items set holdingbranch = ? where itemnumber= ?"
1899 $sth->execute($branch,$itmenumber);
1911 Koha Developement team <info@koha.org>