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
22 #use warnings; # soon!
33 use C4::ItemCirculationAlertPreference;
43 use POSIX qw(strftime);
44 use C4::Branch; # GetBranches
45 use C4::Log; # logaction
49 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
53 $VERSION = 3.02; # for version checking
56 # FIXME subs that should probably be elsewhere
62 # subs to deal with issuing a book
75 &GetBranchBorrowerCircRule
78 &AnonymiseIssueHistory
81 # subs to deal with returns
87 # subs to deal with transfers
94 &IsBranchTransferAllowed
95 &CreateBranchTransferLimit
96 &DeleteBranchTransferLimits
102 C4::Circulation - Koha circulation module
110 The functions in this module deal with circulation, issues, and
111 returns, as well as general information about the library.
112 Also deals with stocktaking.
118 =head3 $str = &barcodedecode($barcode, [$filter]);
122 =item Generic filter function for barcode string.
123 Called on every circ if the System Pref itemBarcodeInputFilter is set.
124 Will do some manipulation of the barcode for systems that deliver a barcode
125 to circulation.pl that differs from the barcode stored for the item.
126 For proper functioning of this filter, calling the function on the
127 correct barcode string (items.barcode) should return an unaltered barcode.
129 The optional $filter argument is to allow for testing or explicit
130 behavior that ignores the System Pref. Valid values are the same as the
137 # FIXME -- the &decode fcn below should be wrapped into this one.
138 # FIXME -- these plugins should be moved out of Circulation.pm
141 my ($barcode, $filter) = @_;
142 $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter;
143 $filter or return $barcode; # ensure filter is defined, else return untouched barcode
144 if ($filter eq 'whitespace') {
146 } elsif ($filter eq 'cuecat') {
148 my @fields = split( /\./, $barcode );
149 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
150 ($#results == 2) and return $results[2];
151 } elsif ($filter eq 'T-prefix') {
152 if ($barcode =~ /^[Tt](\d)/) {
153 (defined($1) and $1 eq '0') and return $barcode;
154 $barcode = substr($barcode, 2) + 0; # FIXME: probably should be substr($barcode, 1)
156 return sprintf("T%07d", $barcode);
157 # FIXME: $barcode could be "T1", causing warning: substr outside of string
158 # Why drop the nonzero digit after the T?
159 # Why pass non-digits (or empty string) to "T%07d"?
161 return $barcode; # return barcode, modified or not
166 =head3 $str = &decode($chunk);
170 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
173 FIXME: Should be replaced with Barcode::Cuecat from CPAN
174 or Javascript based decoding on the client side.
183 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
184 my @s = map { index( $seq, $_ ); } split( //, $encoded );
185 my $l = ( $#s + 1 ) % 4;
188 # warn "Error: Cuecat decode parsing failed!";
196 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
198 chr( ( $n >> 16 ) ^ 67 )
199 .chr( ( $n >> 8 & 255 ) ^ 67 )
200 .chr( ( $n & 255 ) ^ 67 );
203 $r = substr( $r, 0, length($r) - $l );
209 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch, $barcode, $ignore_reserves);
211 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
213 C<$newbranch> is the code for the branch to which the item should be transferred.
215 C<$barcode> is the barcode of the item to be transferred.
217 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
218 Otherwise, if an item is reserved, the transfer fails.
220 Returns three values:
224 is true if the transfer was successful.
228 is a reference-to-hash which may have any of the following keys:
234 There is no item in the catalog with the given barcode. The value is C<$barcode>.
238 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.
240 =item C<DestinationEqualsHolding>
242 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.
246 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.
250 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>.
252 =item C<WasTransferred>
254 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
261 my ( $tbr, $barcode, $ignoreRs ) = @_;
264 my $branches = GetBranches();
265 my $itemnumber = GetItemnumberFromBarcode( $barcode );
266 my $issue = GetItemIssue($itemnumber);
267 my $biblio = GetBiblioFromItemNumber($itemnumber);
270 if ( not $itemnumber ) {
271 $messages->{'BadBarcode'} = $barcode;
275 # get branches of book...
276 my $hbr = $biblio->{'homebranch'};
277 my $fbr = $biblio->{'holdingbranch'};
279 # if using Branch Transfer Limits
280 if ( C4::Context->preference("UseBranchTransferLimits") == 1 ) {
281 if ( C4::Context->preference("item-level_itypes") ) {
282 if ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itype'} ) ) {
283 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itype'};
286 } elsif ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itemtype'} ) ) {
287 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itemtype'};
293 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
294 $messages->{'IsPermanent'} = $hbr;
298 # can't transfer book if is already there....
299 if ( $fbr eq $tbr ) {
300 $messages->{'DestinationEqualsHolding'} = 1;
304 # check if it is still issued to someone, return it...
305 if ($issue->{borrowernumber}) {
306 AddReturn( $barcode, $fbr );
307 $messages->{'WasReturned'} = $issue->{borrowernumber};
311 # That'll save a database query.
312 my ( $resfound, $resrec ) =
313 CheckReserves( $itemnumber );
314 if ( $resfound and not $ignoreRs ) {
315 $resrec->{'ResFound'} = $resfound;
317 # $messages->{'ResFound'} = $resrec;
321 #actually do the transfer....
323 ModItemTransfer( $itemnumber, $fbr, $tbr );
325 # don't need to update MARC anymore, we do it in batch now
326 $messages->{'WasTransfered'} = 1;
327 ModDateLastSeen( $itemnumber );
329 return ( $dotransfer, $messages, $biblio );
334 my $borrower = shift;
335 my $biblionumber = shift;
337 my $cat_borrower = $borrower->{'categorycode'};
338 my $dbh = C4::Context->dbh;
340 # Get which branchcode we need
341 if (C4::Context->preference('CircControl') eq 'PickupLibrary'){
342 $branch = C4::Context->userenv->{'branch'};
344 elsif (C4::Context->preference('CircControl') eq 'PatronLibrary'){
345 $branch = $borrower->{'branchcode'};
349 $branch = $item->{'homebranch'};
351 my $type = (C4::Context->preference('item-level_itypes'))
352 ? $item->{'itype'} # item-level
353 : $item->{'itemtype'}; # biblio-level
355 # given branch, patron category, and item type, determine
356 # applicable issuing rule
357 my $issuing_rule = GetIssuingRule($cat_borrower, $type, $branch);
359 # if a rule is found and has a loan limit set, count
360 # how many loans the patron already has that meet that
362 if (defined($issuing_rule) and defined($issuing_rule->{'maxissueqty'})) {
364 my $count_query = "SELECT COUNT(*) FROM issues
365 JOIN items USING (itemnumber) ";
367 my $rule_itemtype = $issuing_rule->{itemtype};
368 if ($rule_itemtype eq "*") {
369 # matching rule has the default item type, so count only
370 # those existing loans that don't fall under a more
372 if (C4::Context->preference('item-level_itypes')) {
373 $count_query .= " WHERE items.itype NOT IN (
374 SELECT itemtype FROM issuingrules
376 AND (categorycode = ? OR categorycode = ?)
380 $count_query .= " JOIN biblioitems USING (biblionumber)
381 WHERE biblioitems.itemtype NOT IN (
382 SELECT itemtype FROM issuingrules
384 AND (categorycode = ? OR categorycode = ?)
388 push @bind_params, $issuing_rule->{branchcode};
389 push @bind_params, $issuing_rule->{categorycode};
390 push @bind_params, $cat_borrower;
392 # rule has specific item type, so count loans of that
394 if (C4::Context->preference('item-level_itypes')) {
395 $count_query .= " WHERE items.itype = ? ";
397 $count_query .= " JOIN biblioitems USING (biblionumber)
398 WHERE biblioitems.itemtype= ? ";
400 push @bind_params, $type;
403 $count_query .= " AND borrowernumber = ? ";
404 push @bind_params, $borrower->{'borrowernumber'};
405 my $rule_branch = $issuing_rule->{branchcode};
406 if ($rule_branch ne "*") {
407 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
408 $count_query .= " AND issues.branchcode = ? ";
409 push @bind_params, $branch;
410 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
411 ; # if branch is the patron's home branch, then count all loans by patron
413 $count_query .= " AND items.homebranch = ? ";
414 push @bind_params, $branch;
418 my $count_sth = $dbh->prepare($count_query);
419 $count_sth->execute(@bind_params);
420 my ($current_loan_count) = $count_sth->fetchrow_array;
422 my $max_loans_allowed = $issuing_rule->{'maxissueqty'};
423 if ($current_loan_count >= $max_loans_allowed) {
424 return "$current_loan_count / $max_loans_allowed";
428 # Now count total loans against the limit for the branch
429 my $branch_borrower_circ_rule = GetBranchBorrowerCircRule($branch, $cat_borrower);
430 if (defined($branch_borrower_circ_rule->{maxissueqty})) {
431 my @bind_params = ();
432 my $branch_count_query = "SELECT COUNT(*) FROM issues
433 JOIN items USING (itemnumber)
434 WHERE borrowernumber = ? ";
435 push @bind_params, $borrower->{borrowernumber};
437 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
438 $branch_count_query .= " AND issues.branchcode = ? ";
439 push @bind_params, $branch;
440 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
441 ; # if branch is the patron's home branch, then count all loans by patron
443 $branch_count_query .= " AND items.homebranch = ? ";
444 push @bind_params, $branch;
446 my $branch_count_sth = $dbh->prepare($branch_count_query);
447 $branch_count_sth->execute(@bind_params);
448 my ($current_loan_count) = $branch_count_sth->fetchrow_array;
450 my $max_loans_allowed = $branch_borrower_circ_rule->{maxissueqty};
451 if ($current_loan_count >= $max_loans_allowed) {
452 return "$current_loan_count / $max_loans_allowed";
456 # OK, the patron can issue !!!
462 @issues = &itemissues($biblioitemnumber, $biblio);
464 Looks up information about who has borrowed the bookZ<>(s) with the
465 given biblioitemnumber.
467 C<$biblio> is ignored.
469 C<&itemissues> returns an array of references-to-hash. The keys
470 include the fields from the C<items> table in the Koha database.
471 Additional keys include:
477 If the item is currently on loan, this gives the due date.
479 If the item is not on loan, then this is either "Available" or
480 "Cancelled", if the item has been withdrawn.
484 If the item is currently on loan, this gives the card number of the
485 patron who currently has the item.
487 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
489 These give the timestamp for the last three times the item was
492 =item C<card0>, C<card1>, C<card2>
494 The card number of the last three patrons who borrowed this item.
496 =item C<borrower0>, C<borrower1>, C<borrower2>
498 The borrower number of the last three patrons who borrowed this item.
506 my ( $bibitem, $biblio ) = @_;
507 my $dbh = C4::Context->dbh;
509 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
514 $sth->execute($bibitem) || die $sth->errstr;
516 while ( my $data = $sth->fetchrow_hashref ) {
518 # Find out who currently has this item.
519 # FIXME - Wouldn't it be better to do this as a left join of
520 # some sort? Currently, this code assumes that if
521 # fetchrow_hashref() fails, then the book is on the shelf.
522 # fetchrow_hashref() can fail for any number of reasons (e.g.,
523 # database server crash), not just because no items match the
525 my $sth2 = $dbh->prepare(
526 "SELECT * FROM issues
527 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
532 $sth2->execute( $data->{'itemnumber'} );
533 if ( my $data2 = $sth2->fetchrow_hashref ) {
534 $data->{'date_due'} = $data2->{'date_due'};
535 $data->{'card'} = $data2->{'cardnumber'};
536 $data->{'borrower'} = $data2->{'borrowernumber'};
539 $data->{'date_due'} = ($data->{'wthdrawn'} eq '1') ? 'Cancelled' : 'Available';
544 # Find the last 3 people who borrowed this item.
545 $sth2 = $dbh->prepare(
546 "SELECT * FROM old_issues
547 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
549 ORDER BY returndate DESC,timestamp DESC"
552 $sth2->execute( $data->{'itemnumber'} );
553 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
554 { # FIXME : error if there is less than 3 pple borrowing this item
555 if ( my $data2 = $sth2->fetchrow_hashref ) {
556 $data->{"timestamp$i2"} = $data2->{'timestamp'};
557 $data->{"card$i2"} = $data2->{'cardnumber'};
558 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
563 $results[$i] = $data;
571 =head2 CanBookBeIssued
573 Check if a book can be issued.
575 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower, $barcode, $duedatespec, $inprocess );
577 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
581 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
583 =item C<$barcode> is the bar code of the book being issued.
585 =item C<$duedatespec> is a C4::Dates object.
595 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
596 Possible values are :
602 sticky due date is invalid
606 borrower gone with no address
610 borrower declared it's card lost
616 =head3 UNKNOWN_BARCODE
630 item is restricted (set by ??)
632 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
633 Possible values are :
641 renewing, not issuing
643 =head3 ISSUED_TO_ANOTHER
645 issued to someone else.
649 reserved for someone else.
653 sticky due date is invalid
657 if the borrower borrows to much things
661 sub CanBookBeIssued {
662 my ( $borrower, $barcode, $duedate, $inprocess ) = @_;
663 my %needsconfirmation; # filled with problems that needs confirmations
664 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
665 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
666 my $issue = GetItemIssue($item->{itemnumber});
667 my $biblioitem = GetBiblioItemData($item->{biblioitemnumber});
668 $item->{'itemtype'}=$item->{'itype'};
669 my $dbh = C4::Context->dbh;
672 # DUE DATE is OK ? -- should already have checked.
674 #$issuingimpossible{INVALID_DATE} = 1 unless ($duedate);
679 if ( $borrower->{'category_type'} eq 'X' && ( $item->{barcode} )) {
680 # stats only borrower -- add entry to statistics table, and return issuingimpossible{STATS} = 1 .
681 &UpdateStats(C4::Context->userenv->{'branch'},'localuse','','',$item->{'itemnumber'},$item->{'itemtype'},$borrower->{'borrowernumber'});
682 return( { STATS => 1 }, {});
684 if ( $borrower->{flags}->{GNA} ) {
685 $issuingimpossible{GNA} = 1;
687 if ( $borrower->{flags}->{'LOST'} ) {
688 $issuingimpossible{CARD_LOST} = 1;
690 if ( $borrower->{flags}->{'DBARRED'} ) {
691 $issuingimpossible{DEBARRED} = 1;
693 if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
694 $issuingimpossible{EXPIRED} = 1;
696 my @expirydate= split /-/,$borrower->{'dateexpiry'};
697 if($expirydate[0]==0 || $expirydate[1]==0|| $expirydate[2]==0 ||
698 Date_to_Days(Today) > Date_to_Days( @expirydate )) {
699 $issuingimpossible{EXPIRED} = 1;
708 C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->output('iso') );
709 if ( C4::Context->preference("IssuingInProcess") ) {
710 my $amountlimit = C4::Context->preference("noissuescharge");
711 if ( $amount > $amountlimit && !$inprocess ) {
712 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
714 elsif ( $amount > 0 && $amount <= $amountlimit && !$inprocess ) {
715 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
720 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
725 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
727 my $toomany = TooMany( $borrower, $item->{biblionumber}, $item );
728 $needsconfirmation{TOO_MANY} = $toomany if $toomany;
733 unless ( $item->{barcode} ) {
734 $issuingimpossible{UNKNOWN_BARCODE} = 1;
736 if ( $item->{'notforloan'}
737 && $item->{'notforloan'} > 0 )
739 $issuingimpossible{NOT_FOR_LOAN} = 1;
741 elsif ( !$item->{'notforloan'} ){
742 # we have to check itemtypes.notforloan also
743 if (C4::Context->preference('item-level_itypes')){
744 # this should probably be a subroutine
745 my $sth = $dbh->prepare("SELECT notforloan FROM itemtypes WHERE itemtype = ?");
746 $sth->execute($item->{'itemtype'});
747 my $notforloan=$sth->fetchrow_hashref();
749 if ($notforloan->{'notforloan'} == 1){
750 $issuingimpossible{NOT_FOR_LOAN} = 1;
753 elsif ($biblioitem->{'notforloan'} == 1){
754 $issuingimpossible{NOT_FOR_LOAN} = 1;
757 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
759 $issuingimpossible{WTHDRAWN} = 1;
761 if ( $item->{'restricted'}
762 && $item->{'restricted'} == 1 )
764 $issuingimpossible{RESTRICTED} = 1;
766 if ( C4::Context->preference("IndependantBranches") ) {
767 my $userenv = C4::Context->userenv;
768 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
769 $issuingimpossible{NOTSAMEBRANCH} = 1
770 if ( $item->{C4::Context->preference("HomeOrHoldingBranch")} ne $userenv->{branch} );
775 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
777 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
780 # Already issued to current borrower. Ask whether the loan should
782 my ($CanBookBeRenewed,$renewerror) = CanBookBeRenewed(
783 $borrower->{'borrowernumber'},
784 $item->{'itemnumber'}
786 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
787 $issuingimpossible{NO_MORE_RENEWALS} = 1;
790 $needsconfirmation{RENEW_ISSUE} = 1;
793 elsif ($issue->{borrowernumber}) {
795 # issued to someone else
796 my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
798 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
799 $needsconfirmation{ISSUED_TO_ANOTHER} =
800 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
803 # See if the item is on reserve.
804 my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
806 my $resbor = $res->{'borrowernumber'};
807 my ( $resborrower ) = C4::Members::GetMemberDetails( $resbor, 0 );
808 my $branches = GetBranches();
809 my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
810 if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
812 # The item is on reserve and waiting, but has been
813 # reserved by some other patron.
814 $needsconfirmation{RESERVE_WAITING} =
815 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
817 elsif ( $restype eq "Reserved" ) {
818 # The item is on reserve for someone else.
819 $needsconfirmation{RESERVED} =
820 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
823 if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" ) {
824 if ( $borrower->{'categorycode'} eq 'W' ) {
826 return ( \%emptyhash, \%needsconfirmation );
829 return ( \%issuingimpossible, \%needsconfirmation );
834 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
836 &AddIssue($borrower, $barcode, [$datedue], [$cancelreserve], [$issuedate])
840 =item C<$borrower> is a hash with borrower informations (from GetMemberDetails).
842 =item C<$barcode> is the barcode of the item being issued.
844 =item C<$datedue> is a C4::Dates object for the max date of return, i.e. the date due (optional).
847 =item C<$cancelreserve> is 1 to override and cancel any pending reserves for the item (optional).
849 =item C<$issuedate> is the date to issue the item in iso (YYYY-MM-DD) format (optional).
850 Defaults to today. Unlike C<$datedue>, NOT a C4::Dates object, unfortunately.
852 AddIssue does the following things :
854 - step 01: check that there is a borrowernumber & a barcode provided
855 - check for RENEWAL (book issued & being issued to the same patron)
856 - renewal YES = Calculate Charge & renew
858 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
860 - fill reserve if reserve to this patron
861 - cancel reserve or not, otherwise
862 * TRANSFERT PENDING ?
863 - complete the transfert
871 my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
872 my $dbh = C4::Context->dbh;
873 my $barcodecheck=CheckValidBarcode($barcode);
875 # $issuedate defaults to today.
876 if ( ! defined $issuedate ) {
877 $issuedate = strftime( "%Y-%m-%d", localtime );
878 # TODO: for hourly circ, this will need to be a C4::Dates object
879 # and all calls to AddIssue including issuedate will need to pass a Dates object.
881 if ($borrower and $barcode and $barcodecheck ne '0'){
882 # find which item we issue
883 my $item = GetItem('', $barcode) or return undef; # if we don't get an Item, abort.
884 my $branch = (C4::Context->preference('CircControl') eq 'PickupLibrary') ? C4::Context->userenv->{'branch'} :
885 (C4::Context->preference('CircControl') eq 'PatronLibrary') ? $borrower->{'branchcode'} :
886 $item->{'homebranch'}; # fallback to item's homebranch
888 # get actual issuing if there is one
889 my $actualissue = GetItemIssue( $item->{itemnumber});
891 # get biblioinformation for this item
892 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
895 # check if we just renew the issue.
897 if ($actualissue->{borrowernumber} eq $borrower->{'borrowernumber'}) {
898 $datedue = AddRenewal(
899 $borrower->{'borrowernumber'},
900 $item->{'itemnumber'},
903 $issuedate, # here interpreted as the renewal date
908 if ( $actualissue->{borrowernumber}) {
909 # This book is currently on loan, but not to the person
910 # who wants to borrow it now. mark it returned before issuing to the new borrower
913 C4::Context->userenv->{'branch'}
917 # See if the item is on reserve.
918 my ( $restype, $res ) =
919 C4::Reserves::CheckReserves( $item->{'itemnumber'} );
921 my $resbor = $res->{'borrowernumber'};
922 if ( $resbor eq $borrower->{'borrowernumber'} ) {
923 # The item is reserved by the current patron
924 ModReserveFill($res);
926 elsif ( $restype eq "Waiting" ) {
928 # The item is on reserve and waiting, but has been
929 # reserved by some other patron.
931 elsif ( $restype eq "Reserved" ) {
933 # The item is reserved by someone else.
934 if ($cancelreserve) { # cancel reserves on this item
935 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
938 if ($cancelreserve) {
939 CancelReserve($res->{'biblionumber'}, 0, $res->{'borrowernumber'});
942 # set waiting reserve to first in reserve queue as book isn't waiting now
944 $res->{'biblionumber'},
945 $res->{'borrowernumber'},
951 # Starting process for transfer job (checking transfert and validate it if we have one)
952 my ($datesent) = GetTransfers($item->{'itemnumber'});
954 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for visibility of this case (maybe for stats ....)
957 "UPDATE branchtransfers
958 SET datearrived = now(),
960 comments = 'Forced branchtransfer'
961 WHERE itemnumber= ? AND datearrived IS NULL"
963 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
966 # Record in the database the fact that the book was issued.
970 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
974 my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'};
975 my $loanlength = GetLoanLength( $borrower->{'categorycode'}, $itype, $branch );
976 $datedue = CalcDateDue( C4::Dates->new( $issuedate, 'iso' ), $loanlength, $branch );
978 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
979 if ( C4::Context->preference('ReturnBeforeExpiry') && $datedue->output('iso') gt $borrower->{dateexpiry} ) {
980 $datedue = C4::Dates->new( $borrower->{dateexpiry}, 'iso' );
984 $borrower->{'borrowernumber'}, # borrowernumber
985 $item->{'itemnumber'}, # itemnumber
986 $issuedate, # issuedate
987 $datedue->output('iso'), # date_due
988 C4::Context->userenv->{'branch'} # branchcode
992 ModItem({ issues => $item->{'issues'},
993 holdingbranch => C4::Context->userenv->{'branch'},
995 datelastborrowed => C4::Dates->new()->output('iso'),
996 onloan => $datedue->output('iso'),
997 }, $item->{'biblionumber'}, $item->{'itemnumber'});
998 ModDateLastSeen( $item->{'itemnumber'} );
1000 # If it costs to borrow this book, charge it to the patron's account.
1001 my ( $charge, $itemtype ) = GetIssuingCharges(
1002 $item->{'itemnumber'},
1003 $borrower->{'borrowernumber'}
1005 if ( $charge > 0 ) {
1007 $item->{'itemnumber'},
1008 $borrower->{'borrowernumber'}, $charge
1010 $item->{'charge'} = $charge;
1013 # Record the fact that this book was issued.
1015 C4::Context->userenv->{'branch'},
1017 ($sipmode ? "SIP-$sipmode" : ''), $item->{'itemnumber'},
1018 $item->{'itype'}, $borrower->{'borrowernumber'}
1021 # Send a checkout slip.
1022 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1024 branchcode => $branch,
1025 categorycode => $borrower->{categorycode},
1026 item_type => $item->{itype},
1028 if ($circulation_alert->is_enabled_for(\%conditions)) {
1029 SendCirculationAlert({
1032 borrower => $borrower,
1037 logaction("CIRCULATION", "ISSUE", $borrower->{'borrowernumber'}, $biblio->{'biblionumber'})
1038 if C4::Context->preference("IssueLog");
1040 return ($datedue); # not necessarily the same as when it came in!
1043 =head2 GetLoanLength
1045 Get loan length for an itemtype, a borrower type and a branch
1047 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1052 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1053 my $dbh = C4::Context->dbh;
1056 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
1058 # warn "in get loan lenght $borrowertype $itemtype $branchcode ";
1059 # try to find issuelength & return the 1st available.
1060 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1061 $sth->execute( $borrowertype, $itemtype, $branchcode );
1062 my $loanlength = $sth->fetchrow_hashref;
1063 return $loanlength->{issuelength}
1064 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1066 $sth->execute( $borrowertype, "*", $branchcode );
1067 $loanlength = $sth->fetchrow_hashref;
1068 return $loanlength->{issuelength}
1069 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1071 $sth->execute( "*", $itemtype, $branchcode );
1072 $loanlength = $sth->fetchrow_hashref;
1073 return $loanlength->{issuelength}
1074 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1076 $sth->execute( "*", "*", $branchcode );
1077 $loanlength = $sth->fetchrow_hashref;
1078 return $loanlength->{issuelength}
1079 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1081 $sth->execute( $borrowertype, $itemtype, "*" );
1082 $loanlength = $sth->fetchrow_hashref;
1083 return $loanlength->{issuelength}
1084 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1086 $sth->execute( $borrowertype, "*", "*" );
1087 $loanlength = $sth->fetchrow_hashref;
1088 return $loanlength->{issuelength}
1089 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1091 $sth->execute( "*", $itemtype, "*" );
1092 $loanlength = $sth->fetchrow_hashref;
1093 return $loanlength->{issuelength}
1094 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1096 $sth->execute( "*", "*", "*" );
1097 $loanlength = $sth->fetchrow_hashref;
1098 return $loanlength->{issuelength}
1099 if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1101 # if no rule is set => 21 days (hardcoded)
1105 =head2 GetIssuingRule
1107 FIXME - This is a copy-paste of GetLoanLength
1108 as a stop-gap. Do not wish to change API for GetLoanLength
1109 this close to release, however, Overdues::GetIssuingRules is broken.
1111 Get the issuing rule for an itemtype, a borrower type and a branch
1112 Returns a hashref from the issuingrules table.
1114 my $irule = &GetIssuingRule($borrowertype,$itemtype,branchcode)
1118 sub GetIssuingRule {
1119 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1120 my $dbh = C4::Context->dbh;
1121 my $sth = $dbh->prepare( "select * from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null" );
1124 $sth->execute( $borrowertype, $itemtype, $branchcode );
1125 $irule = $sth->fetchrow_hashref;
1126 return $irule if defined($irule) ;
1128 $sth->execute( $borrowertype, "*", $branchcode );
1129 $irule = $sth->fetchrow_hashref;
1130 return $irule if defined($irule) ;
1132 $sth->execute( "*", $itemtype, $branchcode );
1133 $irule = $sth->fetchrow_hashref;
1134 return $irule if defined($irule) ;
1136 $sth->execute( "*", "*", $branchcode );
1137 $irule = $sth->fetchrow_hashref;
1138 return $irule if defined($irule) ;
1140 $sth->execute( $borrowertype, $itemtype, "*" );
1141 $irule = $sth->fetchrow_hashref;
1142 return $irule if defined($irule) ;
1144 $sth->execute( $borrowertype, "*", "*" );
1145 $irule = $sth->fetchrow_hashref;
1146 return $irule if defined($irule) ;
1148 $sth->execute( "*", $itemtype, "*" );
1149 $irule = $sth->fetchrow_hashref;
1150 return $irule if defined($irule) ;
1152 $sth->execute( "*", "*", "*" );
1153 $irule = $sth->fetchrow_hashref;
1154 return $irule if defined($irule) ;
1156 # if no rule matches,
1160 =head2 GetBranchBorrowerCircRule
1164 my $branch_cat_rule = GetBranchBorrowerCircRule($branchcode, $categorycode);
1168 Retrieves circulation rule attributes that apply to the given
1169 branch and patron category, regardless of item type.
1170 The return value is a hashref containing the following key:
1172 maxissueqty - maximum number of loans that a
1173 patron of the given category can have at the given
1174 branch. If the value is undef, no limit.
1176 This will first check for a specific branch and
1177 category match from branch_borrower_circ_rules.
1179 If no rule is found, it will then check default_branch_circ_rules
1180 (same branch, default category). If no rule is found,
1181 it will then check default_borrower_circ_rules (default
1182 branch, same category), then failing that, default_circ_rules
1183 (default branch, default category).
1185 If no rule has been found in the database, it will default to
1190 C<$branchcode> and C<$categorycode> should contain the
1191 literal branch code and patron category code, respectively - no
1196 sub GetBranchBorrowerCircRule {
1197 my $branchcode = shift;
1198 my $categorycode = shift;
1200 my $branch_cat_query = "SELECT maxissueqty
1201 FROM branch_borrower_circ_rules
1202 WHERE branchcode = ?
1203 AND categorycode = ?";
1204 my $dbh = C4::Context->dbh();
1205 my $sth = $dbh->prepare($branch_cat_query);
1206 $sth->execute($branchcode, $categorycode);
1208 if ($result = $sth->fetchrow_hashref()) {
1212 # try same branch, default borrower category
1213 my $branch_query = "SELECT maxissueqty
1214 FROM default_branch_circ_rules
1215 WHERE branchcode = ?";
1216 $sth = $dbh->prepare($branch_query);
1217 $sth->execute($branchcode);
1218 if ($result = $sth->fetchrow_hashref()) {
1222 # try default branch, same borrower category
1223 my $category_query = "SELECT maxissueqty
1224 FROM default_borrower_circ_rules
1225 WHERE categorycode = ?";
1226 $sth = $dbh->prepare($category_query);
1227 $sth->execute($categorycode);
1228 if ($result = $sth->fetchrow_hashref()) {
1232 # try default branch, default borrower category
1233 my $default_query = "SELECT maxissueqty
1234 FROM default_circ_rules";
1235 $sth = $dbh->prepare($default_query);
1237 if ($result = $sth->fetchrow_hashref()) {
1241 # built-in default circulation rule
1243 maxissueqty => undef,
1247 =head2 GetBranchItemRule
1251 my $branch_item_rule = GetBranchItemRule($branchcode, $itemtype);
1255 Retrieves circulation rule attributes that apply to the given
1256 branch and item type, regardless of patron category.
1258 The return value is a hashref containing the following key:
1260 holdallowed => Hold policy for this branch and itemtype. Possible values:
1261 0: No holds allowed.
1262 1: Holds allowed only by patrons that have the same homebranch as the item.
1263 2: Holds allowed from any patron.
1265 This searches branchitemrules in the following order:
1267 * Same branchcode and itemtype
1268 * Same branchcode, itemtype '*'
1269 * branchcode '*', same itemtype
1270 * branchcode and itemtype '*'
1272 Neither C<$branchcode> nor C<$categorycode> should be '*'.
1276 sub GetBranchItemRule {
1277 my ( $branchcode, $itemtype ) = @_;
1278 my $dbh = C4::Context->dbh();
1282 ['SELECT holdallowed
1283 FROM branch_item_rules
1284 WHERE branchcode = ?
1285 AND itemtype = ?', $branchcode, $itemtype],
1286 ['SELECT holdallowed
1287 FROM default_branch_circ_rules
1288 WHERE branchcode = ?', $branchcode],
1289 ['SELECT holdallowed
1290 FROM default_branch_item_rules
1291 WHERE itemtype = ?', $itemtype],
1292 ['SELECT holdallowed
1293 FROM default_circ_rules'],
1296 foreach my $attempt (@attempts) {
1297 my ($query, @bind_params) = @{$attempt};
1299 # Since branch/category and branch/itemtype use the same per-branch
1300 # defaults tables, we have to check that the key we want is set, not
1301 # just that a row was returned
1302 return $result if ( defined( $result->{'holdallowed'} = $dbh->selectrow_array( $query, {}, @bind_params ) ) );
1305 # built-in default circulation rule
1313 ($doreturn, $messages, $iteminformation, $borrower) =
1314 &AddReturn($barcode, $branch, $exemptfine, $dropbox);
1320 =item C<$barcode> is the bar code of the book being returned.
1322 =item C<$branch> is the code of the branch where the book is being returned.
1324 =item C<$exemptfine> indicates that overdue charges for the item will be
1327 =item C<$dropbox> indicates that the check-in date is assumed to be
1328 yesterday, or the last non-holiday as defined in C4::Calendar . If
1329 overdue charges are applied and C<$dropbox> is true, the last charge
1330 will be removed. This assumes that the fines accrual script has run
1335 C<&AddReturn> returns a list of four items:
1337 C<$doreturn> is true iff the return succeeded.
1339 C<$messages> is a reference-to-hash giving the reason for failure:
1345 No item with this barcode exists. The value is C<$barcode>.
1349 The book is not currently on loan. The value is C<$barcode>.
1351 =item C<IsPermanent>
1353 The book's home branch is a permanent collection. If you have borrowed
1354 this book, you are not allowed to return it. The value is the code for
1355 the book's home branch.
1359 This book has been withdrawn/cancelled. The value should be ignored.
1363 The item was reserved. The value is a reference-to-hash whose keys are
1364 fields from the reserves table of the Koha database, and
1365 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1366 either C<Waiting>, C<Reserved>, or 0.
1370 C<$borrower> is a reference-to-hash, giving information about the
1371 patron who last borrowed the book.
1376 my ( $barcode, $branch, $exemptfine, $dropbox ) = @_;
1377 my $dbh = C4::Context->dbh;
1381 my $validTransfert = 0;
1382 my $reserveDone = 0;
1384 # get information on item
1385 my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1386 my $biblio = GetBiblioItemData($iteminformation->{'biblioitemnumber'});
1387 # use Data::Dumper;warn Data::Dumper::Dumper($iteminformation);
1388 unless ($iteminformation->{'itemnumber'} ) {
1389 $messages->{'BadBarcode'} = $barcode;
1393 if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1394 $messages->{'NotIssued'} = $barcode;
1395 # even though item is not on loan, it may still
1396 # be transferred; therefore, get current branch information
1397 my $curr_iteminfo = GetItem($iteminformation->{'itemnumber'});
1398 $iteminformation->{'homebranch'} = $curr_iteminfo->{'homebranch'};
1399 $iteminformation->{'holdingbranch'} = $curr_iteminfo->{'holdingbranch'};
1403 # check if the book is in a permanent collection....
1404 my $hbr = $iteminformation->{C4::Context->preference("HomeOrHoldingBranch")};
1405 my $branches = GetBranches();
1406 # FIXME -- This 'PE' attribute is largely undocumented. afaict, there's no user interface that reflects this functionality.
1407 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1408 $messages->{'IsPermanent'} = $hbr;
1411 # if independent branches are on and returning to different branch, refuse the return
1412 if ($hbr ne C4::Context->userenv->{'branch'} && C4::Context->preference("IndependantBranches")){
1413 $messages->{'Wrongbranch'} = 1;
1417 # check that the book has been cancelled
1418 if ( $iteminformation->{'wthdrawn'} ) {
1419 $messages->{'wthdrawn'} = 1;
1423 # new op dev : if the book returned in an other branch update the holding branch
1425 # update issues, thereby returning book (should push this out into another subroutine
1426 $borrower = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1428 # case of a return of document (deal with issues and holdingbranch)
1431 my $circControlBranch;
1433 # don't allow dropbox mode to create an invalid entry in issues (issuedate > returndate) FIXME: actually checks eq, not gt
1434 undef($dropbox) if ( $iteminformation->{'issuedate'} eq C4::Dates->today('iso') );
1435 if (C4::Context->preference('CircControl') eq 'ItemHomeBranch' ) {
1436 $circControlBranch = $iteminformation->{homebranch};
1437 } elsif ( C4::Context->preference('CircControl') eq 'PatronLibrary') {
1438 $circControlBranch = $borrower->{branchcode};
1439 } else { # CircControl must be PickupLibrary.
1440 $circControlBranch = $iteminformation->{holdingbranch};
1441 # FIXME - is this right ? are we sure that the holdingbranch is still the pickup branch?
1444 MarkIssueReturned($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'},$circControlBranch);
1445 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
1448 # continue to deal with returns cases, but not only if we have an issue
1450 # the holdingbranch is updated if the document is returned in an other location .
1451 if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} ) {
1452 UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});
1453 # reload iteminformation holdingbranch with the userenv value
1454 $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1456 ModDateLastSeen( $iteminformation->{'itemnumber'} );
1457 ModItem({ onloan => undef }, $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'});
1459 if ($iteminformation->{borrowernumber}){
1460 ($borrower) = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1462 # fix up the accounts.....
1463 if ( $iteminformation->{'itemlost'} ) {
1464 $messages->{'WasLost'} = 1;
1467 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1468 # check if we have a transfer for this document
1469 my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1471 # if we have a transfer to do, we update the line of transfers with the datearrived
1473 if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1476 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1478 $sth->execute( $iteminformation->{'itemnumber'} );
1480 # 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'
1481 C4::Reserves::ModReserveStatus( $iteminformation->{'itemnumber'},'W' );
1484 $messages->{'WrongTransfer'} = $tobranch;
1485 $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1487 $validTransfert = 1;
1490 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1491 # fix up the accounts.....
1492 if ($iteminformation->{'itemlost'}) {
1493 FixAccountForLostAndReturned($iteminformation, $borrower);
1494 $messages->{'WasLost'} = 1;
1496 # fix up the overdues in accounts...
1497 FixOverduesOnReturn( $borrower->{'borrowernumber'},
1498 $iteminformation->{'itemnumber'}, $exemptfine, $dropbox );
1500 # find reserves.....
1501 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1502 my ( $resfound, $resrec ) =
1503 C4::Reserves::CheckReserves( $iteminformation->{'itemnumber'} );
1505 $resrec->{'ResFound'} = $resfound;
1506 $messages->{'ResFound'} = $resrec;
1511 # Record the fact that this book was returned.
1513 $branch, 'return', '0', '',
1514 $iteminformation->{'itemnumber'},
1515 $biblio->{'itemtype'},
1516 $borrower->{'borrowernumber'}
1519 # Send a check-in slip.
1520 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1522 branchcode => $branch,
1523 categorycode => $borrower->{categorycode},
1524 item_type => $iteminformation->{itype},
1526 if ($doreturn && $circulation_alert->is_enabled_for(\%conditions)) {
1527 SendCirculationAlert({
1529 item => $iteminformation,
1530 borrower => $borrower,
1534 logaction("CIRCULATION", "RETURN", $iteminformation->{borrowernumber}, $iteminformation->{'biblionumber'})
1535 if C4::Context->preference("ReturnLog");
1537 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1538 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1540 if ( ( $branch ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1541 if (C4::Context->preference("AutomaticItemReturn") == 1) {
1542 ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1543 $messages->{'WasTransfered'} = 1;
1544 } elsif ( C4::Context->preference("UseBranchTransferLimits") == 1
1545 && ! IsTransferAllowed( $branch, $iteminformation->{'homebranch'}, $iteminformation->{'itemtype'} )
1547 ModItemTransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1548 $messages->{'WasTransfered'} = 1;
1551 $messages->{'NeedsTransfer'} = 1;
1555 return ( $doreturn, $messages, $iteminformation, $borrower );
1558 =head2 MarkIssueReturned
1562 MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch, $returndate);
1566 Unconditionally marks an issue as being returned by
1567 moving the C<issues> row to C<old_issues> and
1568 setting C<returndate> to the current date, or
1569 the last non-holiday date of the branccode specified in
1570 C<dropbox_branch> . Assumes you've already checked that
1571 it's safe to do this, i.e. last non-holiday > issuedate.
1573 if C<$returndate> is specified (in iso format), it is used as the date
1574 of the return. It is ignored when a dropbox_branch is passed in.
1576 Ideally, this function would be internal to C<C4::Circulation>,
1577 not exported, but it is currently needed by one
1578 routine in C<C4::Accounts>.
1582 sub MarkIssueReturned {
1583 my ( $borrowernumber, $itemnumber, $dropbox_branch, $returndate ) = @_;
1584 my $dbh = C4::Context->dbh;
1585 my $query = "UPDATE issues SET returndate=";
1587 if ($dropbox_branch) {
1588 my $calendar = C4::Calendar->new( branchcode => $dropbox_branch );
1589 my $dropboxdate = $calendar->addDate( C4::Dates->new(), -1 );
1591 push @bind, $dropboxdate->output('iso');
1592 } elsif ($returndate) {
1594 push @bind, $returndate;
1596 $query .= " now() ";
1598 $query .= " WHERE borrowernumber = ? AND itemnumber = ?";
1599 push @bind, $borrowernumber, $itemnumber;
1601 my $sth_upd = $dbh->prepare($query);
1602 $sth_upd->execute(@bind);
1603 my $sth_copy = $dbh->prepare("INSERT INTO old_issues SELECT * FROM issues
1604 WHERE borrowernumber = ?
1605 AND itemnumber = ?");
1606 $sth_copy->execute($borrowernumber, $itemnumber);
1607 my $sth_del = $dbh->prepare("DELETE FROM issues
1608 WHERE borrowernumber = ?
1609 AND itemnumber = ?");
1610 $sth_del->execute($borrowernumber, $itemnumber);
1613 =head2 FixOverduesOnReturn
1615 &FixOverduesOnReturn($brn,$itm, $exemptfine, $dropboxmode);
1617 C<$brn> borrowernumber
1621 C<$exemptfine> BOOL -- remove overdue charge associated with this issue.
1622 C<$dropboxmode> BOOL -- remove lastincrement on overdue charge associated with this issue.
1624 internal function, called only by AddReturn
1628 sub FixOverduesOnReturn {
1629 my ( $borrowernumber, $item, $exemptfine, $dropbox ) = @_;
1630 my $dbh = C4::Context->dbh;
1632 # check for overdue fine
1635 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1637 $sth->execute( $borrowernumber, $item );
1639 # alter fine to show that the book has been returned
1641 if ($data = $sth->fetchrow_hashref) {
1643 my @bind = ($borrowernumber,$item ,$data->{'accountno'});
1645 $uquery = "update accountlines set accounttype='FFOR', amountoutstanding=0";
1646 if (C4::Context->preference("FinesLog")) {
1647 &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
1649 } elsif ($dropbox && $data->{lastincrement}) {
1650 my $outstanding = $data->{amountoutstanding} - $data->{lastincrement} ;
1651 my $amt = $data->{amount} - $data->{lastincrement} ;
1652 if (C4::Context->preference("FinesLog")) {
1653 &logaction("FINES", 'MODIFY',$borrowernumber,"Dropbox adjustment $amt, item $item");
1655 $uquery = "update accountlines set accounttype='F' ";
1656 if($outstanding >= 0 && $amt >=0) {
1657 $uquery .= ", amount = ? , amountoutstanding=? ";
1658 unshift @bind, ($amt, $outstanding) ;
1661 $uquery = "update accountlines set accounttype='F' ";
1663 $uquery .= " where (borrowernumber = ?) and (itemnumber = ?) and (accountno = ?)";
1664 my $usth = $dbh->prepare($uquery);
1665 $usth->execute(@bind);
1673 =head2 FixAccountForLostAndReturned
1675 &FixAccountForLostAndReturned($iteminfo,$borrower);
1677 Calculates the charge for a book lost and returned (Not exported & used only once)
1679 C<$iteminfo> is a hashref to iteminfo. Only {itemnumber} is used.
1681 C<$borrower> is a hashref to borrower. Only {borrowernumber is used.
1683 Internal function, called by AddReturn
1687 sub FixAccountForLostAndReturned {
1688 my ($iteminfo, $borrower) = @_;
1689 my $dbh = C4::Context->dbh;
1690 my $itm = $iteminfo->{'itemnumber'};
1691 # check for charge made for lost book
1692 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE (itemnumber = ?) AND (accounttype='L' OR accounttype='Rep') ORDER BY date DESC");
1693 $sth->execute($itm);
1694 if (my $data = $sth->fetchrow_hashref) {
1695 # writeoff this amount
1697 my $amount = $data->{'amount'};
1698 my $acctno = $data->{'accountno'};
1700 if ($data->{'amountoutstanding'} == $amount) {
1701 $offset = $data->{'amount'};
1704 $offset = $amount - $data->{'amountoutstanding'};
1705 $amountleft = $data->{'amountoutstanding'} - $amount;
1707 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
1708 WHERE (borrowernumber = ?)
1709 AND (itemnumber = ?) AND (accountno = ?) ");
1710 $usth->execute($data->{'borrowernumber'},$itm,$acctno);
1712 #check if any credit is left if so writeoff other accounts
1713 my $nextaccntno = getnextacctno($data->{'borrowernumber'});
1714 if ($amountleft < 0){
1717 if ($amountleft > 0){
1718 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
1719 AND (amountoutstanding >0) ORDER BY date");
1720 $msth->execute($data->{'borrowernumber'});
1721 # offset transactions
1724 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1725 if ($accdata->{'amountoutstanding'} < $amountleft) {
1727 $amountleft -= $accdata->{'amountoutstanding'};
1729 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1732 my $thisacct = $accdata->{'accountno'};
1733 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
1734 WHERE (borrowernumber = ?)
1735 AND (accountno=?)");
1736 $usth->execute($newamtos,$data->{'borrowernumber'},'$thisacct');
1738 $usth = $dbh->prepare("INSERT INTO accountoffsets
1739 (borrowernumber, accountno, offsetaccount, offsetamount)
1742 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
1747 if ($amountleft > 0){
1750 my $desc="Item Returned ".$iteminfo->{'barcode'};
1751 $usth = $dbh->prepare("INSERT INTO accountlines
1752 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1753 VALUES (?,?,now(),?,?,'CR',?)");
1754 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
1756 $usth = $dbh->prepare("INSERT INTO accountoffsets
1757 (borrowernumber, accountno, offsetaccount, offsetamount)
1759 $usth->execute($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset);
1761 ModItem({ paidfor => '' }, undef, $itm);
1769 $issues = &GetItemIssue($itemnumber);
1771 Returns patrons currently having a book. nothing if item is not issued atm
1773 C<$itemnumber> is the itemnumber
1775 Returns an array of hashes
1777 FIXME: Though the above says that this function returns nothing if the
1778 item is not issued, this actually returns a hasref that looks like
1789 my ( $itemnumber) = @_;
1790 return unless $itemnumber;
1791 my $dbh = C4::Context->dbh;
1795 my $today = POSIX::strftime("%Y%m%d", localtime);
1797 my $sth = $dbh->prepare(
1798 "SELECT * FROM issues
1799 LEFT JOIN items ON issues.itemnumber=items.itemnumber
1801 issues.itemnumber=?");
1802 $sth->execute($itemnumber);
1803 my $data = $sth->fetchrow_hashref;
1804 my $datedue = $data->{'date_due'};
1806 if ( $datedue < $today ) {
1807 $data->{'overdue'} = 1;
1809 $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1816 $issue = GetOpenIssue( $itemnumber );
1818 Returns the row from the issues table if the item is currently issued, undef if the item is not currently issued
1820 C<$itemnumber> is the item's itemnumber
1827 my ( $itemnumber ) = @_;
1829 my $dbh = C4::Context->dbh;
1830 my $sth = $dbh->prepare( "SELECT * FROM issues WHERE itemnumber = ? AND returndate IS NULL" );
1831 $sth->execute( $itemnumber );
1832 my $issue = $sth->fetchrow_hashref();
1836 =head2 GetItemIssues
1838 $issues = &GetItemIssues($itemnumber, $history);
1840 Returns patrons that have issued a book
1842 C<$itemnumber> is the itemnumber
1843 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1845 Returns an array of hashes
1850 my ( $itemnumber,$history ) = @_;
1851 my $dbh = C4::Context->dbh;
1855 my $today = POSIX::strftime("%Y%m%d", localtime);
1857 my $sql = "SELECT * FROM issues
1858 JOIN borrowers USING (borrowernumber)
1859 JOIN items USING (itemnumber)
1860 WHERE issues.itemnumber = ? ";
1863 SELECT * FROM old_issues
1864 LEFT JOIN borrowers USING (borrowernumber)
1865 JOIN items USING (itemnumber)
1866 WHERE old_issues.itemnumber = ? ";
1868 $sql .= "ORDER BY date_due DESC";
1869 my $sth = $dbh->prepare($sql);
1871 $sth->execute($itemnumber, $itemnumber);
1873 $sth->execute($itemnumber);
1875 while ( my $data = $sth->fetchrow_hashref ) {
1876 my $datedue = $data->{'date_due'};
1878 if ( $datedue < $today ) {
1879 $data->{'overdue'} = 1;
1881 my $itemnumber = $data->{'itemnumber'};
1882 push @GetItemIssues, $data;
1885 return ( \@GetItemIssues );
1888 =head2 GetBiblioIssues
1890 $issues = GetBiblioIssues($biblionumber);
1892 this function get all issues from a biblionumber.
1895 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1896 tables issues and the firstname,surname & cardnumber from borrowers.
1900 sub GetBiblioIssues {
1901 my $biblionumber = shift;
1902 return undef unless $biblionumber;
1903 my $dbh = C4::Context->dbh;
1905 SELECT issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1907 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1908 LEFT JOIN items ON issues.itemnumber = items.itemnumber
1909 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1910 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1911 WHERE biblio.biblionumber = ?
1913 SELECT old_issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1915 LEFT JOIN borrowers ON borrowers.borrowernumber = old_issues.borrowernumber
1916 LEFT JOIN items ON old_issues.itemnumber = items.itemnumber
1917 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1918 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1919 WHERE biblio.biblionumber = ?
1922 my $sth = $dbh->prepare($query);
1923 $sth->execute($biblionumber, $biblionumber);
1926 while ( my $data = $sth->fetchrow_hashref ) {
1927 push @issues, $data;
1932 =head2 GetUpcomingDueIssues
1936 my $upcoming_dues = GetUpcomingDueIssues( { days_in_advance => 4 } );
1942 sub GetUpcomingDueIssues {
1945 $params->{'days_in_advance'} = 7 unless exists $params->{'days_in_advance'};
1946 my $dbh = C4::Context->dbh;
1948 my $statement = <<END_SQL;
1949 SELECT issues.*, items.itype as itemtype, items.homebranch, TO_DAYS( date_due )-TO_DAYS( NOW() ) as days_until_due
1951 LEFT JOIN items USING (itemnumber)
1952 WhERE returndate is NULL
1953 AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ?
1956 my @bind_parameters = ( $params->{'days_in_advance'} );
1958 my $sth = $dbh->prepare( $statement );
1959 $sth->execute( @bind_parameters );
1960 my $upcoming_dues = $sth->fetchall_arrayref({});
1963 return $upcoming_dues;
1966 =head2 CanBookBeRenewed
1968 ($ok,$error) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
1970 Find out whether a borrowed item may be renewed.
1972 C<$dbh> is a DBI handle to the Koha database.
1974 C<$borrowernumber> is the borrower number of the patron who currently
1975 has the item on loan.
1977 C<$itemnumber> is the number of the item to renew.
1979 C<$override_limit>, if supplied with a true value, causes
1980 the limit on the number of times that the loan can be renewed
1981 (as controlled by the item type) to be ignored.
1983 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1984 item must currently be on loan to the specified borrower; renewals
1985 must be allowed for the item's type; and the borrower must not have
1986 already renewed the loan. $error will contain the reason the renewal can not proceed
1990 sub CanBookBeRenewed {
1992 # check renewal status
1993 my ( $borrowernumber, $itemnumber, $override_limit ) = @_;
1994 my $dbh = C4::Context->dbh;
1999 # Look in the issues table for this item, lent to this borrower,
2000 # and not yet returned.
2002 # FIXME - I think this function could be redone to use only one SQL call.
2003 my $sth1 = $dbh->prepare(
2004 "SELECT * FROM issues
2005 WHERE borrowernumber = ?
2008 $sth1->execute( $borrowernumber, $itemnumber );
2009 if ( my $data1 = $sth1->fetchrow_hashref ) {
2011 # Found a matching item
2013 # See if this item may be renewed. This query is convoluted
2014 # because it's a bit messy: given the item number, we need to find
2015 # the biblioitem, which gives us the itemtype, which tells us
2016 # whether it may be renewed.
2017 my $query = "SELECT renewalsallowed FROM items ";
2018 $query .= (C4::Context->preference('item-level_itypes'))
2019 ? "LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
2020 : "LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
2021 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
2022 $query .= "WHERE items.itemnumber = ?";
2023 my $sth2 = $dbh->prepare($query);
2024 $sth2->execute($itemnumber);
2025 if ( my $data2 = $sth2->fetchrow_hashref ) {
2026 $renews = $data2->{'renewalsallowed'};
2028 if ( ( $renews && $renews > $data1->{'renewals'} ) || $override_limit ) {
2035 my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber);
2043 return ($renewokay,$error);
2048 &AddRenewal($borrowernumber, $itemnumber, $branch, [$datedue], [$lastreneweddate]);
2052 C<$borrowernumber> is the borrower number of the patron who currently
2055 C<$itemnumber> is the number of the item to renew.
2057 C<$branch> is the library branch. Defaults to the homebranch of the ITEM.
2059 C<$datedue> can be a C4::Dates object used to set the due date.
2061 C<$lastreneweddate> is an optional ISO-formatted date used to set issues.lastreneweddate. If
2062 this parameter is not supplied, lastreneweddate is set to the current date.
2064 If C<$datedue> is the empty string, C<&AddRenewal> will calculate the due date automatically
2065 from the book's item type.
2070 my $borrowernumber = shift or return undef;
2071 my $itemnumber = shift or return undef;
2072 my $item = GetItem($itemnumber) or return undef;
2073 my $biblio = GetBiblioFromItemNumber($itemnumber) or return undef;
2074 my $branch = (@_) ? shift : $item->{homebranch}; # opac-renew doesn't send branch
2075 my $datedue = shift;
2076 my $lastreneweddate = shift;
2078 # If the due date wasn't specified, calculate it by adding the
2079 # book's loan length to today's date.
2080 unless ($datedue && $datedue->output('iso')) {
2082 my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 ) or return undef;
2083 my $loanlength = GetLoanLength(
2084 $borrower->{'categorycode'},
2085 (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ,
2086 $item->{homebranch} # item's homebranch determines loanlength OR do we want the branch specified by the AddRenewal argument?
2088 #FIXME -- use circControl?
2089 $datedue = CalcDateDue(C4::Dates->new(),$loanlength,$branch); # this branch is the transactional branch.
2090 # The question of whether to use item's homebranch calendar is open.
2093 # $lastreneweddate defaults to today.
2094 unless (defined $lastreneweddate) {
2095 $lastreneweddate = strftime( "%Y-%m-%d", localtime );
2098 my $dbh = C4::Context->dbh;
2099 # Find the issues record for this book
2101 $dbh->prepare("SELECT * FROM issues
2102 WHERE borrowernumber=?
2105 $sth->execute( $borrowernumber, $itemnumber );
2106 my $issuedata = $sth->fetchrow_hashref;
2109 # Update the issues record to have the new due date, and a new count
2110 # of how many times it has been renewed.
2111 my $renews = $issuedata->{'renewals'} + 1;
2112 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = ?
2113 WHERE borrowernumber=?
2116 $sth->execute( $datedue->output('iso'), $renews, $lastreneweddate, $borrowernumber, $itemnumber );
2119 # Update the renewal count on the item, and tell zebra to reindex
2120 $renews = $biblio->{'renewals'} + 1;
2121 ModItem({ renewals => $renews }, $biblio->{'biblionumber'}, $itemnumber);
2123 # Charge a new rental fee, if applicable?
2124 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
2125 if ( $charge > 0 ) {
2126 my $accountno = getnextacctno( $borrowernumber );
2127 my $item = GetBiblioFromItemNumber($itemnumber);
2128 $sth = $dbh->prepare(
2129 "INSERT INTO accountlines
2131 borrowernumber, accountno, amount,
2133 accounttype, amountoutstanding, itemnumber
2135 VALUES (now(),?,?,?,?,?,?,?)"
2137 $sth->execute( $borrowernumber, $accountno, $charge,
2138 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
2139 'Rent', $charge, $itemnumber );
2143 UpdateStats( $branch, 'renew', $charge, '', $itemnumber, $item->{itype}, $borrowernumber);
2148 # check renewal status
2149 my ($bornum,$itemno)=@_;
2150 my $dbh = C4::Context->dbh;
2152 my $renewsallowed = 0;
2154 # Look in the issues table for this item, lent to this borrower,
2155 # and not yet returned.
2157 # FIXME - I think this function could be redone to use only one SQL call.
2158 my $sth = $dbh->prepare("select * from issues
2159 where (borrowernumber = ?)
2160 and (itemnumber = ?)");
2161 $sth->execute($bornum,$itemno);
2162 my $data = $sth->fetchrow_hashref;
2163 $renewcount = $data->{'renewals'} if $data->{'renewals'};
2165 my $query = "SELECT renewalsallowed FROM items ";
2166 $query .= (C4::Context->preference('item-level_itypes'))
2167 ? "LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
2168 : "LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
2169 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
2170 $query .= "WHERE items.itemnumber = ?";
2171 my $sth2 = $dbh->prepare($query);
2172 $sth2->execute($itemno);
2173 my $data2 = $sth2->fetchrow_hashref();
2174 $renewsallowed = $data2->{'renewalsallowed'};
2175 $renewsleft = $renewsallowed - $renewcount;
2176 return ($renewcount,$renewsallowed,$renewsleft);
2179 =head2 GetIssuingCharges
2181 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
2183 Calculate how much it would cost for a given patron to borrow a given
2184 item, including any applicable discounts.
2186 C<$itemnumber> is the item number of item the patron wishes to borrow.
2188 C<$borrowernumber> is the patron's borrower number.
2190 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
2191 and C<$item_type> is the code for the item's item type (e.g., C<VID>
2196 sub GetIssuingCharges {
2198 # calculate charges due
2199 my ( $itemnumber, $borrowernumber ) = @_;
2201 my $dbh = C4::Context->dbh;
2204 # Get the book's item type and rental charge (via its biblioitem).
2205 my $qcharge = "SELECT itemtypes.itemtype,rentalcharge FROM items
2206 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
2207 $qcharge .= (C4::Context->preference('item-level_itypes'))
2208 ? " LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype "
2209 : " LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype ";
2211 $qcharge .= "WHERE items.itemnumber =?";
2213 my $sth1 = $dbh->prepare($qcharge);
2214 $sth1->execute($itemnumber);
2215 if ( my $data1 = $sth1->fetchrow_hashref ) {
2216 $item_type = $data1->{'itemtype'};
2217 $charge = $data1->{'rentalcharge'};
2218 my $q2 = "SELECT rentaldiscount FROM borrowers
2219 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
2220 WHERE borrowers.borrowernumber = ?
2221 AND issuingrules.itemtype = ?";
2222 my $sth2 = $dbh->prepare($q2);
2223 $sth2->execute( $borrowernumber, $item_type );
2224 if ( my $data2 = $sth2->fetchrow_hashref ) {
2225 my $discount = $data2->{'rentaldiscount'};
2226 if ( $discount eq 'NULL' ) {
2229 $charge = ( $charge * ( 100 - $discount ) ) / 100;
2235 return ( $charge, $item_type );
2238 =head2 AddIssuingCharge
2240 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
2244 sub AddIssuingCharge {
2245 my ( $itemnumber, $borrowernumber, $charge ) = @_;
2246 my $dbh = C4::Context->dbh;
2247 my $nextaccntno = getnextacctno( $borrowernumber );
2249 INSERT INTO accountlines
2250 (borrowernumber, itemnumber, accountno,
2251 date, amount, description, accounttype,
2253 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
2255 my $sth = $dbh->prepare($query);
2256 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
2262 GetTransfers($itemnumber);
2267 my ($itemnumber) = @_;
2269 my $dbh = C4::Context->dbh;
2275 FROM branchtransfers
2276 WHERE itemnumber = ?
2277 AND datearrived IS NULL
2279 my $sth = $dbh->prepare($query);
2280 $sth->execute($itemnumber);
2281 my @row = $sth->fetchrow_array();
2286 =head2 GetTransfersFromTo
2288 @results = GetTransfersFromTo($frombranch,$tobranch);
2290 Returns the list of pending transfers between $from and $to branch
2294 sub GetTransfersFromTo {
2295 my ( $frombranch, $tobranch ) = @_;
2296 return unless ( $frombranch && $tobranch );
2297 my $dbh = C4::Context->dbh;
2299 SELECT itemnumber,datesent,frombranch
2300 FROM branchtransfers
2303 AND datearrived IS NULL
2305 my $sth = $dbh->prepare($query);
2306 $sth->execute( $frombranch, $tobranch );
2309 while ( my $data = $sth->fetchrow_hashref ) {
2310 push @gettransfers, $data;
2313 return (@gettransfers);
2316 =head2 DeleteTransfer
2318 &DeleteTransfer($itemnumber);
2322 sub DeleteTransfer {
2323 my ($itemnumber) = @_;
2324 my $dbh = C4::Context->dbh;
2325 my $sth = $dbh->prepare(
2326 "DELETE FROM branchtransfers
2328 AND datearrived IS NULL "
2330 $sth->execute($itemnumber);
2334 =head2 AnonymiseIssueHistory
2336 $rows = AnonymiseIssueHistory($borrowernumber,$date)
2338 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
2339 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
2341 return the number of affected rows.
2345 sub AnonymiseIssueHistory {
2347 my $borrowernumber = shift;
2348 my $dbh = C4::Context->dbh;
2351 SET borrowernumber = NULL
2352 WHERE returndate < '".$date."'
2353 AND borrowernumber IS NOT NULL
2355 $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
2356 my $rows_affected = $dbh->do($query);
2357 return $rows_affected;
2360 =head2 SendCirculationAlert
2362 Send out a C<check-in> or C<checkout> alert using the messaging system.
2368 Valid values for this parameter are: C<check-in> or C<checkout>.
2372 Hashref of information about the item being checked in or out.
2376 Hashref of information about the borrower of the item.
2382 SendCirculationAlert({
2385 borrower => $borrower,
2390 sub SendCirculationAlert {
2392 # TODO - actually send a message ...somehow.
2395 =head2 updateWrongTransfer
2397 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
2399 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
2403 sub updateWrongTransfer {
2404 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
2405 my $dbh = C4::Context->dbh;
2406 # first step validate the actual line of transfert .
2409 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
2411 $sth->execute($FromLibrary,$itemNumber);
2414 # second step create a new line of branchtransfer to the right location .
2415 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
2417 #third step changing holdingbranch of item
2418 UpdateHoldingbranch($FromLibrary,$itemNumber);
2421 =head2 UpdateHoldingbranch
2423 $items = UpdateHoldingbranch($branch,$itmenumber);
2424 Simple methode for updating hodlingbranch in items BDD line
2428 sub UpdateHoldingbranch {
2429 my ( $branch,$itemnumber ) = @_;
2430 ModItem({ holdingbranch => $branch }, undef, $itemnumber);
2435 $newdatedue = CalcDateDue($startdate,$loanlength,$branchcode);
2436 this function calculates the due date given the loan length ,
2437 checking against the holidays calendar as per the 'useDaysMode' syspref.
2438 C<$startdate> = C4::Dates object representing start date of loan period (assumed to be today)
2439 C<$branch> = location whose calendar to use
2440 C<$loanlength> = loan length prior to adjustment
2444 my ($startdate,$loanlength,$branch) = @_;
2445 if(C4::Context->preference('useDaysMode') eq 'Days') { # ignoring calendar
2446 my $datedue = time + ($loanlength) * 86400;
2447 #FIXME - assumes now even though we take a startdate
2448 my @datearr = localtime($datedue);
2449 return C4::Dates->new( sprintf("%04d-%02d-%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3]), 'iso');
2451 my $calendar = C4::Calendar->new( branchcode => $branch );
2452 my $datedue = $calendar->addDate($startdate, $loanlength);
2457 =head2 CheckValidDatedue
2458 This function does not account for holiday exceptions nor does it handle the 'useDaysMode' syspref .
2459 To be replaced by CalcDateDue() once C4::Calendar use is tested.
2461 $newdatedue = CheckValidDatedue($date_due,$itemnumber,$branchcode);
2462 this function validates the loan length against the holidays calendar, and adjusts the due date as per the 'useDaysMode' syspref.
2463 C<$date_due> = returndate calculate with no day check
2464 C<$itemnumber> = itemnumber
2465 C<$branchcode> = location of issue (affected by 'CircControl' syspref)
2466 C<$loanlength> = loan length prior to adjustment
2469 sub CheckValidDatedue {
2470 my ($date_due,$itemnumber,$branchcode)=@_;
2471 my @datedue=split('-',$date_due->output('iso'));
2472 my $years=$datedue[0];
2473 my $month=$datedue[1];
2474 my $day=$datedue[2];
2475 # die "Item# $itemnumber ($branchcode) due: " . ${date_due}->output() . "\n(Y,M,D) = ($years,$month,$day)":
2477 for (my $i=0;$i<2;$i++){
2478 $dow=Day_of_Week($years,$month,$day);
2479 ($dow=0) if ($dow>6);
2480 my $result=CheckRepeatableHolidays($itemnumber,$dow,$branchcode);
2481 my $countspecial=CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
2482 my $countspecialrepeatable=CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2483 if (($result ne '0') or ($countspecial ne '0') or ($countspecialrepeatable ne '0') ){
2485 (($years,$month,$day) = Add_Delta_Days($years,$month,$day, 1))if ($i ne '1');
2488 my $newdatedue=C4::Dates->new(sprintf("%04d-%02d-%02d",$years,$month,$day),'iso');
2493 =head2 CheckRepeatableHolidays
2495 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
2496 this function checks if the date due is a repeatable holiday
2497 C<$date_due> = returndate calculate with no day check
2498 C<$itemnumber> = itemnumber
2499 C<$branchcode> = localisation of issue
2503 sub CheckRepeatableHolidays{
2504 my($itemnumber,$week_day,$branchcode)=@_;
2505 my $dbh = C4::Context->dbh;
2506 my $query = qq|SELECT count(*)
2507 FROM repeatable_holidays
2510 my $sth = $dbh->prepare($query);
2511 $sth->execute($branchcode,$week_day);
2512 my $result=$sth->fetchrow;
2518 =head2 CheckSpecialHolidays
2520 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
2521 this function check if the date is a special holiday
2522 C<$years> = the years of datedue
2523 C<$month> = the month of datedue
2524 C<$day> = the day of datedue
2525 C<$itemnumber> = itemnumber
2526 C<$branchcode> = localisation of issue
2530 sub CheckSpecialHolidays{
2531 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
2532 my $dbh = C4::Context->dbh;
2533 my $query=qq|SELECT count(*)
2534 FROM `special_holidays`
2540 my $sth = $dbh->prepare($query);
2541 $sth->execute($years,$month,$day,$branchcode);
2542 my $countspecial=$sth->fetchrow ;
2544 return $countspecial;
2547 =head2 CheckRepeatableSpecialHolidays
2549 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2550 this function check if the date is a repeatble special holidays
2551 C<$month> = the month of datedue
2552 C<$day> = the day of datedue
2553 C<$itemnumber> = itemnumber
2554 C<$branchcode> = localisation of issue
2558 sub CheckRepeatableSpecialHolidays{
2559 my ($month,$day,$itemnumber,$branchcode) = @_;
2560 my $dbh = C4::Context->dbh;
2561 my $query=qq|SELECT count(*)
2562 FROM `repeatable_holidays`
2567 my $sth = $dbh->prepare($query);
2568 $sth->execute($month,$day,$branchcode);
2569 my $countspecial=$sth->fetchrow ;
2571 return $countspecial;
2576 sub CheckValidBarcode{
2578 my $dbh = C4::Context->dbh;
2579 my $query=qq|SELECT count(*)
2583 my $sth = $dbh->prepare($query);
2584 $sth->execute($barcode);
2585 my $exist=$sth->fetchrow ;
2590 =head2 IsBranchTransferAllowed
2592 $allowed = IsBranchTransferAllowed( $toBranch, $fromBranch, $itemtype );
2596 sub IsBranchTransferAllowed {
2597 my ( $toBranch, $fromBranch, $itemtype ) = @_;
2599 if ( $toBranch eq $fromBranch ) { return 1; } ## Short circuit for speed.
2601 my $dbh = C4::Context->dbh;
2603 my $sth = $dbh->prepare('SELECT * FROM branch_transfer_limits WHERE toBranch = ? AND fromBranch = ? AND itemtype = ?');
2604 $sth->execute( $toBranch, $fromBranch, $itemtype );
2605 my $limit = $sth->fetchrow_hashref();
2607 ## If a row is found, then that combination is not allowed, if no matching row is found, then the combination *is allowed*
2608 if ( $limit->{'limitId'} ) {
2615 =head2 CreateBranchTransferLimit
2617 CreateBranchTransferLimit( $toBranch, $fromBranch, $itemtype );
2621 sub CreateBranchTransferLimit {
2622 my ( $toBranch, $fromBranch, $itemtype ) = @_;
2624 my $dbh = C4::Context->dbh;
2626 my $sth = $dbh->prepare("INSERT INTO branch_transfer_limits ( itemtype, toBranch, fromBranch ) VALUES ( ?, ?, ? )");
2627 $sth->execute( $itemtype, $toBranch, $fromBranch );
2630 =head2 DeleteBranchTransferLimits
2632 DeleteBranchTransferLimits();
2636 sub DeleteBranchTransferLimits {
2637 my $dbh = C4::Context->dbh;
2638 my $sth = $dbh->prepare("TRUNCATE TABLE branch_transfer_limits");
2649 Koha Developement team <info@koha.org>