1 package C4::Circulation;
3 # Copyright 2000-2002 Katipo Communications
4 # copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
32 use C4::Dates qw(format_date);
34 use C4::ItemCirculationAlertPreference;
37 use C4::Branch; # GetBranches
38 use C4::Log; # logaction
39 use C4::Koha qw(GetAuthorisedValueByCode);
40 use C4::Overdues qw(CalcFine UpdateFine);
41 use Algorithm::CheckDigits;
56 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
60 $VERSION = 3.07.00.049; # for version checking
63 # FIXME subs that should probably be elsewhere
70 # subs to deal with issuing a book
81 &GetBranchBorrowerCircRule
85 &AnonymiseIssueHistory
86 &CheckIfIssuedToPatron
89 # subs to deal with returns
95 # subs to deal with transfers
102 &IsBranchTransferAllowed
103 &CreateBranchTransferLimit
104 &DeleteBranchTransferLimits
108 # subs to deal with offline circulation
110 &GetOfflineOperations
113 &DeleteOfflineOperation
114 &ProcessOfflineOperation
120 C4::Circulation - Koha circulation module
128 The functions in this module deal with circulation, issues, and
129 returns, as well as general information about the library.
130 Also deals with stocktaking.
136 $str = &barcodedecode($barcode, [$filter]);
138 Generic filter function for barcode string.
139 Called on every circ if the System Pref itemBarcodeInputFilter is set.
140 Will do some manipulation of the barcode for systems that deliver a barcode
141 to circulation.pl that differs from the barcode stored for the item.
142 For proper functioning of this filter, calling the function on the
143 correct barcode string (items.barcode) should return an unaltered barcode.
145 The optional $filter argument is to allow for testing or explicit
146 behavior that ignores the System Pref. Valid values are the same as the
151 # FIXME -- the &decode fcn below should be wrapped into this one.
152 # FIXME -- these plugins should be moved out of Circulation.pm
155 my ($barcode, $filter) = @_;
156 my $branch = C4::Branch::mybranch();
157 $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter;
158 $filter or return $barcode; # ensure filter is defined, else return untouched barcode
159 if ($filter eq 'whitespace') {
161 } elsif ($filter eq 'cuecat') {
163 my @fields = split( /\./, $barcode );
164 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
165 ($#results == 2) and return $results[2];
166 } elsif ($filter eq 'T-prefix') {
167 if ($barcode =~ /^[Tt](\d)/) {
168 (defined($1) and $1 eq '0') and return $barcode;
169 $barcode = substr($barcode, 2) + 0; # FIXME: probably should be substr($barcode, 1)
171 return sprintf("T%07d", $barcode);
172 # FIXME: $barcode could be "T1", causing warning: substr outside of string
173 # Why drop the nonzero digit after the T?
174 # Why pass non-digits (or empty string) to "T%07d"?
175 } elsif ($filter eq 'libsuite8') {
176 unless($barcode =~ m/^($branch)-/i){ #if barcode starts with branch code its in Koha style. Skip it.
177 if($barcode =~ m/^(\d)/i){ #Some barcodes even start with 0's & numbers and are assumed to have b as the item type in the libsuite8 software
178 $barcode =~ s/^[0]*(\d+)$/$branch-b-$1/i;
180 $barcode =~ s/^(\D+)[0]*(\d+)$/$branch-$1-$2/i;
183 } elsif ($filter eq 'EAN13') {
184 my $ean = CheckDigits('ean');
185 if ( $ean->is_valid($barcode) ) {
186 #$barcode = sprintf('%013d',$barcode); # this doesn't work on 32-bit systems
187 $barcode = '0' x ( 13 - length($barcode) ) . $barcode;
189 warn "# [$barcode] not valid EAN-13/UPC-A\n";
192 return $barcode; # return barcode, modified or not
197 $str = &decode($chunk);
199 Decodes a segment of a string emitted by a CueCat barcode scanner and
202 FIXME: Should be replaced with Barcode::Cuecat from CPAN
203 or Javascript based decoding on the client side.
210 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
211 my @s = map { index( $seq, $_ ); } split( //, $encoded );
212 my $l = ( $#s + 1 ) % 4;
215 # warn "Error: Cuecat decode parsing failed!";
223 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
225 chr( ( $n >> 16 ) ^ 67 )
226 .chr( ( $n >> 8 & 255 ) ^ 67 )
227 .chr( ( $n & 255 ) ^ 67 );
230 $r = substr( $r, 0, length($r) - $l );
236 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch,
237 $barcode, $ignore_reserves);
239 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
241 C<$newbranch> is the code for the branch to which the item should be transferred.
243 C<$barcode> is the barcode of the item to be transferred.
245 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
246 Otherwise, if an item is reserved, the transfer fails.
248 Returns three values:
254 is true if the transfer was successful.
258 is a reference-to-hash which may have any of the following keys:
264 There is no item in the catalog with the given barcode. The value is C<$barcode>.
268 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.
270 =item C<DestinationEqualsHolding>
272 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.
276 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.
280 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>.
282 =item C<WasTransferred>
284 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
293 my ( $tbr, $barcode, $ignoreRs ) = @_;
296 my $branches = GetBranches();
297 my $itemnumber = GetItemnumberFromBarcode( $barcode );
298 my $issue = GetItemIssue($itemnumber);
299 my $biblio = GetBiblioFromItemNumber($itemnumber);
302 if ( not $itemnumber ) {
303 $messages->{'BadBarcode'} = $barcode;
307 # get branches of book...
308 my $hbr = $biblio->{'homebranch'};
309 my $fbr = $biblio->{'holdingbranch'};
311 # if using Branch Transfer Limits
312 if ( C4::Context->preference("UseBranchTransferLimits") == 1 ) {
313 if ( C4::Context->preference("item-level_itypes") && C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ) {
314 if ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itype'} ) ) {
315 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itype'};
318 } elsif ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{ C4::Context->preference("BranchTransferLimitsType") } ) ) {
319 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{ C4::Context->preference("BranchTransferLimitsType") };
325 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
326 $messages->{'IsPermanent'} = $hbr;
330 # can't transfer book if is already there....
331 if ( $fbr eq $tbr ) {
332 $messages->{'DestinationEqualsHolding'} = 1;
336 # check if it is still issued to someone, return it...
337 if ($issue->{borrowernumber}) {
338 AddReturn( $barcode, $fbr );
339 $messages->{'WasReturned'} = $issue->{borrowernumber};
343 # That'll save a database query.
344 my ( $resfound, $resrec, undef ) =
345 CheckReserves( $itemnumber );
346 if ( $resfound and not $ignoreRs ) {
347 $resrec->{'ResFound'} = $resfound;
349 # $messages->{'ResFound'} = $resrec;
353 #actually do the transfer....
355 ModItemTransfer( $itemnumber, $fbr, $tbr );
357 # don't need to update MARC anymore, we do it in batch now
358 $messages->{'WasTransfered'} = 1;
361 ModDateLastSeen( $itemnumber );
362 return ( $dotransfer, $messages, $biblio );
367 my $borrower = shift;
368 my $biblionumber = shift;
370 my $cat_borrower = $borrower->{'categorycode'};
371 my $dbh = C4::Context->dbh;
373 # Get which branchcode we need
374 $branch = _GetCircControlBranch($item,$borrower);
375 my $type = (C4::Context->preference('item-level_itypes'))
376 ? $item->{'itype'} # item-level
377 : $item->{'itemtype'}; # biblio-level
379 # given branch, patron category, and item type, determine
380 # applicable issuing rule
381 my $issuing_rule = GetIssuingRule($cat_borrower, $type, $branch);
383 # if a rule is found and has a loan limit set, count
384 # how many loans the patron already has that meet that
386 if (defined($issuing_rule) and defined($issuing_rule->{'maxissueqty'})) {
388 my $count_query = "SELECT COUNT(*) FROM issues
389 JOIN items USING (itemnumber) ";
391 my $rule_itemtype = $issuing_rule->{itemtype};
392 if ($rule_itemtype eq "*") {
393 # matching rule has the default item type, so count only
394 # those existing loans that don't fall under a more
396 if (C4::Context->preference('item-level_itypes')) {
397 $count_query .= " WHERE items.itype NOT IN (
398 SELECT itemtype FROM issuingrules
400 AND (categorycode = ? OR categorycode = ?)
404 $count_query .= " JOIN biblioitems USING (biblionumber)
405 WHERE biblioitems.itemtype NOT IN (
406 SELECT itemtype FROM issuingrules
408 AND (categorycode = ? OR categorycode = ?)
412 push @bind_params, $issuing_rule->{branchcode};
413 push @bind_params, $issuing_rule->{categorycode};
414 push @bind_params, $cat_borrower;
416 # rule has specific item type, so count loans of that
418 if (C4::Context->preference('item-level_itypes')) {
419 $count_query .= " WHERE items.itype = ? ";
421 $count_query .= " JOIN biblioitems USING (biblionumber)
422 WHERE biblioitems.itemtype= ? ";
424 push @bind_params, $type;
427 $count_query .= " AND borrowernumber = ? ";
428 push @bind_params, $borrower->{'borrowernumber'};
429 my $rule_branch = $issuing_rule->{branchcode};
430 if ($rule_branch ne "*") {
431 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
432 $count_query .= " AND issues.branchcode = ? ";
433 push @bind_params, $branch;
434 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
435 ; # if branch is the patron's home branch, then count all loans by patron
437 $count_query .= " AND items.homebranch = ? ";
438 push @bind_params, $branch;
442 my $count_sth = $dbh->prepare($count_query);
443 $count_sth->execute(@bind_params);
444 my ($current_loan_count) = $count_sth->fetchrow_array;
446 my $max_loans_allowed = $issuing_rule->{'maxissueqty'};
447 if ($current_loan_count >= $max_loans_allowed) {
448 return ($current_loan_count, $max_loans_allowed);
452 # Now count total loans against the limit for the branch
453 my $branch_borrower_circ_rule = GetBranchBorrowerCircRule($branch, $cat_borrower);
454 if (defined($branch_borrower_circ_rule->{maxissueqty})) {
455 my @bind_params = ();
456 my $branch_count_query = "SELECT COUNT(*) FROM issues
457 JOIN items USING (itemnumber)
458 WHERE borrowernumber = ? ";
459 push @bind_params, $borrower->{borrowernumber};
461 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
462 $branch_count_query .= " AND issues.branchcode = ? ";
463 push @bind_params, $branch;
464 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
465 ; # if branch is the patron's home branch, then count all loans by patron
467 $branch_count_query .= " AND items.homebranch = ? ";
468 push @bind_params, $branch;
470 my $branch_count_sth = $dbh->prepare($branch_count_query);
471 $branch_count_sth->execute(@bind_params);
472 my ($current_loan_count) = $branch_count_sth->fetchrow_array;
474 my $max_loans_allowed = $branch_borrower_circ_rule->{maxissueqty};
475 if ($current_loan_count >= $max_loans_allowed) {
476 return ($current_loan_count, $max_loans_allowed);
480 # OK, the patron can issue !!!
486 @issues = &itemissues($biblioitemnumber, $biblio);
488 Looks up information about who has borrowed the bookZ<>(s) with the
489 given biblioitemnumber.
491 C<$biblio> is ignored.
493 C<&itemissues> returns an array of references-to-hash. The keys
494 include the fields from the C<items> table in the Koha database.
495 Additional keys include:
501 If the item is currently on loan, this gives the due date.
503 If the item is not on loan, then this is either "Available" or
504 "Cancelled", if the item has been withdrawn.
508 If the item is currently on loan, this gives the card number of the
509 patron who currently has the item.
511 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
513 These give the timestamp for the last three times the item was
516 =item C<card0>, C<card1>, C<card2>
518 The card number of the last three patrons who borrowed this item.
520 =item C<borrower0>, C<borrower1>, C<borrower2>
522 The borrower number of the last three patrons who borrowed this item.
530 my ( $bibitem, $biblio ) = @_;
531 my $dbh = C4::Context->dbh;
533 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
538 $sth->execute($bibitem) || die $sth->errstr;
540 while ( my $data = $sth->fetchrow_hashref ) {
542 # Find out who currently has this item.
543 # FIXME - Wouldn't it be better to do this as a left join of
544 # some sort? Currently, this code assumes that if
545 # fetchrow_hashref() fails, then the book is on the shelf.
546 # fetchrow_hashref() can fail for any number of reasons (e.g.,
547 # database server crash), not just because no items match the
549 my $sth2 = $dbh->prepare(
550 "SELECT * FROM issues
551 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
556 $sth2->execute( $data->{'itemnumber'} );
557 if ( my $data2 = $sth2->fetchrow_hashref ) {
558 $data->{'date_due'} = $data2->{'date_due'};
559 $data->{'card'} = $data2->{'cardnumber'};
560 $data->{'borrower'} = $data2->{'borrowernumber'};
563 $data->{'date_due'} = ($data->{'wthdrawn'} eq '1') ? 'Cancelled' : 'Available';
567 # Find the last 3 people who borrowed this item.
568 $sth2 = $dbh->prepare(
569 "SELECT * FROM old_issues
570 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
572 ORDER BY returndate DESC,timestamp DESC"
575 $sth2->execute( $data->{'itemnumber'} );
576 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
577 { # FIXME : error if there is less than 3 pple borrowing this item
578 if ( my $data2 = $sth2->fetchrow_hashref ) {
579 $data->{"timestamp$i2"} = $data2->{'timestamp'};
580 $data->{"card$i2"} = $data2->{'cardnumber'};
581 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
585 $results[$i] = $data;
592 =head2 CanBookBeIssued
594 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower,
595 $barcode, $duedatespec, $inprocess, $ignore_reserves );
597 Check if a book can be issued.
599 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
603 =item C<$borrower> hash with borrower informations (from GetMember or GetMemberDetails)
605 =item C<$barcode> is the bar code of the book being issued.
607 =item C<$duedatespec> is a C4::Dates object.
609 =item C<$inprocess> boolean switch
610 =item C<$ignore_reserves> boolean switch
618 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
619 Possible values are :
625 sticky due date is invalid
629 borrower gone with no address
633 borrower declared it's card lost
639 =head3 UNKNOWN_BARCODE
653 item is restricted (set by ??)
655 C<$needsconfirmation> a reference to a hash. It contains reasons why the loan
656 could be prevented, but ones that can be overriden by the operator.
658 Possible values are :
666 renewing, not issuing
668 =head3 ISSUED_TO_ANOTHER
670 issued to someone else.
674 reserved for someone else.
678 sticky due date is invalid or due date in the past
682 if the borrower borrows to much things
686 sub CanBookBeIssued {
687 my ( $borrower, $barcode, $duedate, $inprocess, $ignore_reserves ) = @_;
688 my %needsconfirmation; # filled with problems that needs confirmations
689 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
690 my %alerts; # filled with messages that shouldn't stop issuing, but the librarian should be aware of.
692 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
693 my $issue = GetItemIssue($item->{itemnumber});
694 my $biblioitem = GetBiblioItemData($item->{biblioitemnumber});
695 $item->{'itemtype'}=$item->{'itype'};
696 my $dbh = C4::Context->dbh;
698 # MANDATORY CHECKS - unless item exists, nothing else matters
699 unless ( $item->{barcode} ) {
700 $issuingimpossible{UNKNOWN_BARCODE} = 1;
702 return ( \%issuingimpossible, \%needsconfirmation ) if %issuingimpossible;
705 # DUE DATE is OK ? -- should already have checked.
707 if ($duedate && ref $duedate ne 'DateTime') {
708 $duedate = dt_from_string($duedate);
710 my $now = DateTime->now( time_zone => C4::Context->tz() );
711 unless ( $duedate ) {
712 my $issuedate = $now->clone();
714 my $branch = _GetCircControlBranch($item,$borrower);
715 my $itype = ( C4::Context->preference('item-level_itypes') ) ? $item->{'itype'} : $biblioitem->{'itemtype'};
716 $duedate = CalcDateDue( $issuedate, $itype, $branch, $borrower );
718 # Offline circ calls AddIssue directly, doesn't run through here
719 # So issuingimpossible should be ok.
722 my $today = $now->clone();
723 $today->truncate( to => 'minute');
724 if (DateTime->compare($duedate,$today) == -1 ) { # duedate cannot be before now
725 $needsconfirmation{INVALID_DATE} = output_pref($duedate);
728 $issuingimpossible{INVALID_DATE} = output_pref($duedate);
734 if ( $borrower->{'category_type'} eq 'X' && ( $item->{barcode} )) {
735 # stats only borrower -- add entry to statistics table, and return issuingimpossible{STATS} = 1 .
736 &UpdateStats(C4::Context->userenv->{'branch'},'localuse','','',$item->{'itemnumber'},$item->{'itemtype'},$borrower->{'borrowernumber'}, undef, $item->{'ccode'});
737 ModDateLastSeen( $item->{'itemnumber'} );
738 return( { STATS => 1 }, {});
740 if ( $borrower->{flags}->{GNA} ) {
741 $issuingimpossible{GNA} = 1;
743 if ( $borrower->{flags}->{'LOST'} ) {
744 $issuingimpossible{CARD_LOST} = 1;
746 if ( $borrower->{flags}->{'DBARRED'} ) {
747 $issuingimpossible{DEBARRED} = 1;
749 if ( !defined $borrower->{dateexpiry} || $borrower->{'dateexpiry'} eq '0000-00-00') {
750 $issuingimpossible{EXPIRED} = 1;
752 my ($y, $m, $d) = split /-/,$borrower->{'dateexpiry'};
753 if ($y && $m && $d) { # are we really writing oinvalid dates to borrs
754 my $expiry_dt = DateTime->new(
758 time_zone => C4::Context->tz,
760 $expiry_dt->truncate( to => 'day');
761 my $today = $now->clone()->truncate(to => 'day');
762 if (DateTime->compare($today, $expiry_dt) == 1) {
763 $issuingimpossible{EXPIRED} = 1;
766 carp("Invalid expity date in borr");
767 $issuingimpossible{EXPIRED} = 1;
775 my ($balance, $non_issue_charges, $other_charges) =
776 C4::Members::GetMemberAccountBalance( $borrower->{'borrowernumber'} );
777 my $amountlimit = C4::Context->preference("noissuescharge");
778 my $allowfineoverride = C4::Context->preference("AllowFineOverride");
779 my $allfinesneedoverride = C4::Context->preference("AllFinesNeedOverride");
780 if ( C4::Context->preference("IssuingInProcess") ) {
781 if ( $non_issue_charges > $amountlimit && !$inprocess && !$allowfineoverride) {
782 $issuingimpossible{DEBT} = sprintf( "%.2f", $non_issue_charges );
783 } elsif ( $non_issue_charges > $amountlimit && !$inprocess && $allowfineoverride) {
784 $needsconfirmation{DEBT} = sprintf( "%.2f", $non_issue_charges );
785 } elsif ( $allfinesneedoverride && $non_issue_charges > 0 && $non_issue_charges <= $amountlimit && !$inprocess ) {
786 $needsconfirmation{DEBT} = sprintf( "%.2f", $non_issue_charges );
790 if ( $non_issue_charges > $amountlimit && $allowfineoverride ) {
791 $needsconfirmation{DEBT} = sprintf( "%.2f", $non_issue_charges );
792 } elsif ( $non_issue_charges > $amountlimit && !$allowfineoverride) {
793 $issuingimpossible{DEBT} = sprintf( "%.2f", $non_issue_charges );
794 } elsif ( $non_issue_charges > 0 && $allfinesneedoverride ) {
795 $needsconfirmation{DEBT} = sprintf( "%.2f", $non_issue_charges );
798 if ($balance > 0 && $other_charges > 0) {
799 $alerts{OTHER_CHARGES} = sprintf( "%.2f", $other_charges );
802 my ($blocktype, $count) = C4::Members::IsMemberBlocked($borrower->{'borrowernumber'});
803 if ($blocktype == -1) {
804 ## patron has outstanding overdue loans
805 if ( C4::Context->preference("OverduesBlockCirc") eq 'block'){
806 $issuingimpossible{USERBLOCKEDOVERDUE} = $count;
808 elsif ( C4::Context->preference("OverduesBlockCirc") eq 'confirmation'){
809 $needsconfirmation{USERBLOCKEDOVERDUE} = $count;
811 } elsif($blocktype == 1) {
812 # patron has accrued fine days
813 $issuingimpossible{USERBLOCKEDREMAINING} = $count;
817 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
819 my ($current_loan_count, $max_loans_allowed) = TooMany( $borrower, $item->{biblionumber}, $item );
820 # if TooMany max_loans_allowed returns 0 the user doesn't have permission to check out this book
821 if (defined $max_loans_allowed && $max_loans_allowed == 0) {
822 $needsconfirmation{PATRON_CANT} = 1;
824 if($max_loans_allowed){
825 $needsconfirmation{TOO_MANY} = 1;
826 $needsconfirmation{current_loan_count} = $current_loan_count;
827 $needsconfirmation{max_loans_allowed} = $max_loans_allowed;
834 if ( $item->{'notforloan'} )
836 if(!C4::Context->preference("AllowNotForLoanOverride")){
837 $issuingimpossible{NOT_FOR_LOAN} = 1;
839 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
843 # we have to check itemtypes.notforloan also
844 if (C4::Context->preference('item-level_itypes')){
845 # this should probably be a subroutine
846 my $sth = $dbh->prepare("SELECT notforloan FROM itemtypes WHERE itemtype = ?");
847 $sth->execute($item->{'itemtype'});
848 my $notforloan=$sth->fetchrow_hashref();
850 if ($notforloan->{'notforloan'}) {
851 if (!C4::Context->preference("AllowNotForLoanOverride")) {
852 $issuingimpossible{NOT_FOR_LOAN} = 1;
854 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
858 elsif ($biblioitem->{'notforloan'} == 1){
859 if (!C4::Context->preference("AllowNotForLoanOverride")) {
860 $issuingimpossible{NOT_FOR_LOAN} = 1;
862 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
866 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} > 0 )
868 $issuingimpossible{WTHDRAWN} = 1;
870 if ( $item->{'restricted'}
871 && $item->{'restricted'} == 1 )
873 $issuingimpossible{RESTRICTED} = 1;
875 if ( $item->{'itemlost'} && C4::Context->preference("IssueLostItem") ne 'nothing' ) {
876 my $code = GetAuthorisedValueByCode( 'LOST', $item->{'itemlost'} );
877 $needsconfirmation{ITEM_LOST} = $code if ( C4::Context->preference("IssueLostItem") eq 'confirm' );
878 $alerts{ITEM_LOST} = $code if ( C4::Context->preference("IssueLostItem") eq 'alert' );
880 if ( C4::Context->preference("IndependantBranches") ) {
881 my $userenv = C4::Context->userenv;
882 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
883 $issuingimpossible{ITEMNOTSAMEBRANCH} = 1
884 if ( $item->{C4::Context->preference("HomeOrHoldingBranch")} ne $userenv->{branch} );
885 $needsconfirmation{BORRNOTSAMEBRANCH} = GetBranchName( $borrower->{'branchcode'} )
886 if ( $borrower->{'branchcode'} ne $userenv->{branch} );
891 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
893 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
896 # Already issued to current borrower. Ask whether the loan should
898 my ($CanBookBeRenewed,$renewerror) = CanBookBeRenewed(
899 $borrower->{'borrowernumber'},
900 $item->{'itemnumber'}
902 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
903 $issuingimpossible{NO_MORE_RENEWALS} = 1;
906 $needsconfirmation{RENEW_ISSUE} = 1;
909 elsif ($issue->{borrowernumber}) {
911 # issued to someone else
912 my $currborinfo = C4::Members::GetMember( borrowernumber => $issue->{borrowernumber} );
914 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
915 $needsconfirmation{ISSUED_TO_ANOTHER} = 1;
916 $needsconfirmation{issued_firstname} = $currborinfo->{'firstname'};
917 $needsconfirmation{issued_surname} = $currborinfo->{'surname'};
918 $needsconfirmation{issued_cardnumber} = $currborinfo->{'cardnumber'};
919 $needsconfirmation{issued_borrowernumber} = $currborinfo->{'borrowernumber'};
922 unless ( $ignore_reserves ) {
923 # See if the item is on reserve.
924 my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
926 my $resbor = $res->{'borrowernumber'};
927 if ( $resbor ne $borrower->{'borrowernumber'} ) {
928 my ( $resborrower ) = C4::Members::GetMember( borrowernumber => $resbor );
929 my $branchname = GetBranchName( $res->{'branchcode'} );
930 if ( $restype eq "Waiting" )
932 # The item is on reserve and waiting, but has been
933 # reserved by some other patron.
934 $needsconfirmation{RESERVE_WAITING} = 1;
935 $needsconfirmation{'resfirstname'} = $resborrower->{'firstname'};
936 $needsconfirmation{'ressurname'} = $resborrower->{'surname'};
937 $needsconfirmation{'rescardnumber'} = $resborrower->{'cardnumber'};
938 $needsconfirmation{'resborrowernumber'} = $resborrower->{'borrowernumber'};
939 $needsconfirmation{'resbranchname'} = $branchname;
940 $needsconfirmation{'reswaitingdate'} = format_date($res->{'waitingdate'});
942 elsif ( $restype eq "Reserved" ) {
943 # The item is on reserve for someone else.
944 $needsconfirmation{RESERVED} = 1;
945 $needsconfirmation{'resfirstname'} = $resborrower->{'firstname'};
946 $needsconfirmation{'ressurname'} = $resborrower->{'surname'};
947 $needsconfirmation{'rescardnumber'} = $resborrower->{'cardnumber'};
948 $needsconfirmation{'resborrowernumber'} = $resborrower->{'borrowernumber'};
949 $needsconfirmation{'resbranchname'} = $branchname;
950 $needsconfirmation{'resreservedate'} = format_date($res->{'reservedate'});
956 # CHECK AGE RESTRICTION
959 # get $marker from preferences. Could be something like "FSK|PEGI|Alter|Age:"
960 my $markers = C4::Context->preference('AgeRestrictionMarker' );
961 my $bibvalues = $biblioitem->{'agerestriction'};
962 if (($markers)&&($bibvalues))
964 # Split $bibvalues to something like FSK 16 or PEGI 6
965 my @values = split ' ', $bibvalues;
967 # Search first occurence of one of the markers
968 my @markers = split /\|/, $markers;
971 for my $value (@values) {
973 for my $marker (@markers) {
974 $marker =~ s/^\s+//; #remove leading spaces
975 $marker =~ s/\s+$//; #remove trailing spaces
976 if (uc($marker) eq uc($value)) {
985 # Index points to the next value
986 my $restrictionyear = 0;
987 if (($take <= $#values) && ($take >= 0)){
988 $restrictionyear += $values[$take];
991 if ($restrictionyear > 0) {
992 if ( $borrower->{'dateofbirth'} ) {
993 my @alloweddate = split /-/,$borrower->{'dateofbirth'} ;
994 $alloweddate[0] += $restrictionyear;
995 #Prevent runime eror on leap year (invalid date)
996 if (($alloweddate[1] == 2) && ($alloweddate[2] == 29)) {
997 $alloweddate[2] = 28;
1000 if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) -1 ) {
1001 if (C4::Context->preference('AgeRestrictionOverride' )) {
1002 $needsconfirmation{AGE_RESTRICTION} = "$bibvalues";
1005 $issuingimpossible{AGE_RESTRICTION} = "$bibvalues";
1012 ## check for high holds decreasing loan period
1013 my $decrease_loan = C4::Context->preference('decreaseLoanHighHolds');
1014 if ( $decrease_loan && $decrease_loan == 1 ) {
1015 my ( $reserved, $num, $duration, $returndate ) =
1016 checkHighHolds( $item, $borrower );
1018 if ( $num >= C4::Context->preference('decreaseLoanHighHoldsValue') ) {
1019 $needsconfirmation{HIGHHOLDS} = {
1021 duration => $duration,
1022 returndate => output_pref($returndate),
1027 return ( \%issuingimpossible, \%needsconfirmation, \%alerts );
1030 =head2 CanBookBeReturned
1032 ($returnallowed, $message) = CanBookBeReturned($item, $branch)
1034 Check whether the item can be returned to the provided branch
1038 =item C<$item> is a hash of item information as returned from GetItem
1040 =item C<$branch> is the branchcode where the return is taking place
1048 =item C<$returnallowed> is 0 or 1, corresponding to whether the return is allowed (1) or not (0)
1050 =item C<$message> is the branchcode where the item SHOULD be returned, if the return is not allowed
1056 sub CanBookBeReturned {
1057 my ($item, $branch) = @_;
1058 my $allowreturntobranch = C4::Context->preference("AllowReturnToBranch") || 'anywhere';
1060 # assume return is allowed to start
1064 # identify all cases where return is forbidden
1065 if ($allowreturntobranch eq 'homebranch' && $branch ne $item->{'homebranch'}) {
1067 $message = $item->{'homebranch'};
1068 } elsif ($allowreturntobranch eq 'holdingbranch' && $branch ne $item->{'holdingbranch'}) {
1070 $message = $item->{'holdingbranch'};
1071 } elsif ($allowreturntobranch eq 'homeorholdingbranch' && $branch ne $item->{'homebranch'} && $branch ne $item->{'holdingbranch'}) {
1073 $message = $item->{'homebranch'}; # FIXME: choice of homebranch is arbitrary
1076 return ($allowed, $message);
1079 =head2 CheckHighHolds
1081 used when syspref decreaseLoanHighHolds is active. Returns 1 or 0 to define whether the minimum value held in
1082 decreaseLoanHighHoldsValue is exceeded, the total number of outstanding holds, the number of days the loan
1083 has been decreased to (held in syspref decreaseLoanHighHoldsValue), and the new due date
1087 sub checkHighHolds {
1088 my ( $item, $borrower ) = @_;
1089 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
1090 my $branch = _GetCircControlBranch( $item, $borrower );
1091 my $dbh = C4::Context->dbh;
1092 my $sth = $dbh->prepare(
1093 'select count(borrowernumber) as num_holds from reserves where biblionumber=?'
1095 $sth->execute( $item->{'biblionumber'} );
1096 my ($holds) = $sth->fetchrow_array;
1098 my $issuedate = DateTime->now( time_zone => C4::Context->tz() );
1100 my $calendar = Koha::Calendar->new( branchcode => $branch );
1103 ( C4::Context->preference('item-level_itypes') )
1104 ? $biblio->{'itype'}
1105 : $biblio->{'itemtype'};
1107 C4::Circulation::CalcDateDue( $issuedate, $itype, $branch,
1110 my $reduced_datedue =
1111 $calendar->addDate( $issuedate,
1112 C4::Context->preference('decreaseLoanHighHoldsDuration') );
1114 if ( DateTime->compare( $reduced_datedue, $orig_due ) == -1 ) {
1116 C4::Context->preference('decreaseLoanHighHoldsDuration'),
1120 return ( 0, 0, 0, undef );
1125 &AddIssue($borrower, $barcode, [$datedue], [$cancelreserve], [$issuedate])
1127 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
1131 =item C<$borrower> is a hash with borrower informations (from GetMember or GetMemberDetails).
1133 =item C<$barcode> is the barcode of the item being issued.
1135 =item C<$datedue> is a C4::Dates object for the max date of return, i.e. the date due (optional).
1136 Calculated if empty.
1138 =item C<$cancelreserve> is 1 to override and cancel any pending reserves for the item (optional).
1140 =item C<$issuedate> is the date to issue the item in iso (YYYY-MM-DD) format (optional).
1141 Defaults to today. Unlike C<$datedue>, NOT a C4::Dates object, unfortunately.
1143 AddIssue does the following things :
1145 - step 01: check that there is a borrowernumber & a barcode provided
1146 - check for RENEWAL (book issued & being issued to the same patron)
1147 - renewal YES = Calculate Charge & renew
1149 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
1151 - fill reserve if reserve to this patron
1152 - cancel reserve or not, otherwise
1153 * TRANSFERT PENDING ?
1154 - complete the transfert
1162 my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
1163 my $dbh = C4::Context->dbh;
1164 my $barcodecheck=CheckValidBarcode($barcode);
1165 if ($datedue && ref $datedue ne 'DateTime') {
1166 $datedue = dt_from_string($datedue);
1168 # $issuedate defaults to today.
1169 if ( ! defined $issuedate ) {
1170 $issuedate = DateTime->now(time_zone => C4::Context->tz());
1173 if ( ref $issuedate ne 'DateTime') {
1174 $issuedate = dt_from_string($issuedate);
1178 if ($borrower and $barcode and $barcodecheck ne '0'){#??? wtf
1179 # find which item we issue
1180 my $item = GetItem('', $barcode) or return; # if we don't get an Item, abort.
1181 my $branch = _GetCircControlBranch($item,$borrower);
1183 # get actual issuing if there is one
1184 my $actualissue = GetItemIssue( $item->{itemnumber});
1186 # get biblioinformation for this item
1187 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
1190 # check if we just renew the issue.
1192 if ($actualissue->{borrowernumber} eq $borrower->{'borrowernumber'}) {
1193 $datedue = AddRenewal(
1194 $borrower->{'borrowernumber'},
1195 $item->{'itemnumber'},
1198 $issuedate, # here interpreted as the renewal date
1202 # it's NOT a renewal
1203 if ( $actualissue->{borrowernumber}) {
1204 # This book is currently on loan, but not to the person
1205 # who wants to borrow it now. mark it returned before issuing to the new borrower
1208 C4::Context->userenv->{'branch'}
1212 MoveReserve( $item->{'itemnumber'}, $borrower->{'borrowernumber'}, $cancelreserve );
1213 # Starting process for transfer job (checking transfert and validate it if we have one)
1214 my ($datesent) = GetTransfers($item->{'itemnumber'});
1216 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for visibility of this case (maybe for stats ....)
1219 "UPDATE branchtransfers
1220 SET datearrived = now(),
1222 comments = 'Forced branchtransfer'
1223 WHERE itemnumber= ? AND datearrived IS NULL"
1225 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
1228 # Record in the database the fact that the book was issued.
1232 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
1236 my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'};
1237 $datedue = CalcDateDue( $issuedate, $itype, $branch, $borrower );
1240 $datedue->truncate( to => 'minute');
1242 $borrower->{'borrowernumber'}, # borrowernumber
1243 $item->{'itemnumber'}, # itemnumber
1244 $issuedate->strftime('%Y-%m-%d %H:%M:00'), # issuedate
1245 $datedue->strftime('%Y-%m-%d %H:%M:00'), # date_due
1246 C4::Context->userenv->{'branch'} # branchcode
1248 if ( C4::Context->preference('ReturnToShelvingCart') ) { ## ReturnToShelvingCart is on, anything issued should be taken off the cart.
1249 CartToShelf( $item->{'itemnumber'} );
1251 $item->{'issues'}++;
1252 if ( C4::Context->preference('UpdateTotalIssuesOnCirc') ) {
1253 UpdateTotalIssues($item->{'biblionumber'}, 1);
1256 ## If item was lost, it has now been found, reverse any list item charges if neccessary.
1257 if ( $item->{'itemlost'} ) {
1258 if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
1259 _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'} );
1263 ModItem({ issues => $item->{'issues'},
1264 holdingbranch => C4::Context->userenv->{'branch'},
1266 datelastborrowed => DateTime->now(time_zone => C4::Context->tz())->ymd(),
1267 onloan => $datedue->ymd(),
1268 }, $item->{'biblionumber'}, $item->{'itemnumber'});
1269 ModDateLastSeen( $item->{'itemnumber'} );
1271 # If it costs to borrow this book, charge it to the patron's account.
1272 my ( $charge, $itemtype ) = GetIssuingCharges(
1273 $item->{'itemnumber'},
1274 $borrower->{'borrowernumber'}
1276 if ( $charge > 0 ) {
1278 $item->{'itemnumber'},
1279 $borrower->{'borrowernumber'}, $charge
1281 $item->{'charge'} = $charge;
1284 # Record the fact that this book was issued.
1286 C4::Context->userenv->{'branch'},
1288 ($sipmode ? "SIP-$sipmode" : ''), $item->{'itemnumber'},
1289 $item->{'itype'}, $borrower->{'borrowernumber'}, undef, $item->{'ccode'}
1292 # Send a checkout slip.
1293 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1295 branchcode => $branch,
1296 categorycode => $borrower->{categorycode},
1297 item_type => $item->{itype},
1298 notification => 'CHECKOUT',
1300 if ($circulation_alert->is_enabled_for(\%conditions)) {
1301 SendCirculationAlert({
1304 borrower => $borrower,
1310 logaction("CIRCULATION", "ISSUE", $borrower->{'borrowernumber'}, $biblio->{'biblionumber'})
1311 if C4::Context->preference("IssueLog");
1313 return ($datedue); # not necessarily the same as when it came in!
1316 =head2 GetLoanLength
1318 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1320 Get loan length for an itemtype, a borrower type and a branch
1325 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1326 my $dbh = C4::Context->dbh;
1329 'select issuelength, lengthunit from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null'
1331 # warn "in get loan lenght $borrowertype $itemtype $branchcode ";
1332 # try to find issuelength & return the 1st available.
1333 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1334 $sth->execute( $borrowertype, $itemtype, $branchcode );
1335 my $loanlength = $sth->fetchrow_hashref;
1337 if defined($loanlength) && $loanlength->{issuelength};
1339 $sth->execute( $borrowertype, '*', $branchcode );
1340 $loanlength = $sth->fetchrow_hashref;
1342 if defined($loanlength) && $loanlength->{issuelength};
1344 $sth->execute( '*', $itemtype, $branchcode );
1345 $loanlength = $sth->fetchrow_hashref;
1347 if defined($loanlength) && $loanlength->{issuelength};
1349 $sth->execute( '*', '*', $branchcode );
1350 $loanlength = $sth->fetchrow_hashref;
1352 if defined($loanlength) && $loanlength->{issuelength};
1354 $sth->execute( $borrowertype, $itemtype, '*' );
1355 $loanlength = $sth->fetchrow_hashref;
1357 if defined($loanlength) && $loanlength->{issuelength};
1359 $sth->execute( $borrowertype, '*', '*' );
1360 $loanlength = $sth->fetchrow_hashref;
1362 if defined($loanlength) && $loanlength->{issuelength};
1364 $sth->execute( '*', $itemtype, '*' );
1365 $loanlength = $sth->fetchrow_hashref;
1367 if defined($loanlength) && $loanlength->{issuelength};
1369 $sth->execute( '*', '*', '*' );
1370 $loanlength = $sth->fetchrow_hashref;
1372 if defined($loanlength) && $loanlength->{issuelength};
1374 # if no rule is set => 21 days (hardcoded)
1377 lengthunit => 'days',
1383 =head2 GetHardDueDate
1385 my ($hardduedate,$hardduedatecompare) = &GetHardDueDate($borrowertype,$itemtype,branchcode)
1387 Get the Hard Due Date and it's comparison for an itemtype, a borrower type and a branch
1391 sub GetHardDueDate {
1392 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1394 my $rule = GetIssuingRule( $borrowertype, $itemtype, $branchcode );
1396 if ( defined( $rule ) ) {
1397 if ( $rule->{hardduedate} ) {
1398 return (dt_from_string($rule->{hardduedate}, 'iso'),$rule->{hardduedatecompare});
1400 return (undef, undef);
1405 =head2 GetIssuingRule
1407 my $irule = &GetIssuingRule($borrowertype,$itemtype,branchcode)
1409 FIXME - This is a copy-paste of GetLoanLength
1410 as a stop-gap. Do not wish to change API for GetLoanLength
1411 this close to release, however, Overdues::GetIssuingRules is broken.
1413 Get the issuing rule for an itemtype, a borrower type and a branch
1414 Returns a hashref from the issuingrules table.
1418 sub GetIssuingRule {
1419 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1420 my $dbh = C4::Context->dbh;
1421 my $sth = $dbh->prepare( "select * from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null" );
1424 $sth->execute( $borrowertype, $itemtype, $branchcode );
1425 $irule = $sth->fetchrow_hashref;
1426 return $irule if defined($irule) ;
1428 $sth->execute( $borrowertype, "*", $branchcode );
1429 $irule = $sth->fetchrow_hashref;
1430 return $irule if defined($irule) ;
1432 $sth->execute( "*", $itemtype, $branchcode );
1433 $irule = $sth->fetchrow_hashref;
1434 return $irule if defined($irule) ;
1436 $sth->execute( "*", "*", $branchcode );
1437 $irule = $sth->fetchrow_hashref;
1438 return $irule if defined($irule) ;
1440 $sth->execute( $borrowertype, $itemtype, "*" );
1441 $irule = $sth->fetchrow_hashref;
1442 return $irule if defined($irule) ;
1444 $sth->execute( $borrowertype, "*", "*" );
1445 $irule = $sth->fetchrow_hashref;
1446 return $irule if defined($irule) ;
1448 $sth->execute( "*", $itemtype, "*" );
1449 $irule = $sth->fetchrow_hashref;
1450 return $irule if defined($irule) ;
1452 $sth->execute( "*", "*", "*" );
1453 $irule = $sth->fetchrow_hashref;
1454 return $irule if defined($irule) ;
1456 # if no rule matches,
1460 =head2 GetBranchBorrowerCircRule
1462 my $branch_cat_rule = GetBranchBorrowerCircRule($branchcode, $categorycode);
1464 Retrieves circulation rule attributes that apply to the given
1465 branch and patron category, regardless of item type.
1466 The return value is a hashref containing the following key:
1468 maxissueqty - maximum number of loans that a
1469 patron of the given category can have at the given
1470 branch. If the value is undef, no limit.
1472 This will first check for a specific branch and
1473 category match from branch_borrower_circ_rules.
1475 If no rule is found, it will then check default_branch_circ_rules
1476 (same branch, default category). If no rule is found,
1477 it will then check default_borrower_circ_rules (default
1478 branch, same category), then failing that, default_circ_rules
1479 (default branch, default category).
1481 If no rule has been found in the database, it will default to
1486 C<$branchcode> and C<$categorycode> should contain the
1487 literal branch code and patron category code, respectively - no
1492 sub GetBranchBorrowerCircRule {
1493 my $branchcode = shift;
1494 my $categorycode = shift;
1496 my $branch_cat_query = "SELECT maxissueqty
1497 FROM branch_borrower_circ_rules
1498 WHERE branchcode = ?
1499 AND categorycode = ?";
1500 my $dbh = C4::Context->dbh();
1501 my $sth = $dbh->prepare($branch_cat_query);
1502 $sth->execute($branchcode, $categorycode);
1504 if ($result = $sth->fetchrow_hashref()) {
1508 # try same branch, default borrower category
1509 my $branch_query = "SELECT maxissueqty
1510 FROM default_branch_circ_rules
1511 WHERE branchcode = ?";
1512 $sth = $dbh->prepare($branch_query);
1513 $sth->execute($branchcode);
1514 if ($result = $sth->fetchrow_hashref()) {
1518 # try default branch, same borrower category
1519 my $category_query = "SELECT maxissueqty
1520 FROM default_borrower_circ_rules
1521 WHERE categorycode = ?";
1522 $sth = $dbh->prepare($category_query);
1523 $sth->execute($categorycode);
1524 if ($result = $sth->fetchrow_hashref()) {
1528 # try default branch, default borrower category
1529 my $default_query = "SELECT maxissueqty
1530 FROM default_circ_rules";
1531 $sth = $dbh->prepare($default_query);
1533 if ($result = $sth->fetchrow_hashref()) {
1537 # built-in default circulation rule
1539 maxissueqty => undef,
1543 =head2 GetBranchItemRule
1545 my $branch_item_rule = GetBranchItemRule($branchcode, $itemtype);
1547 Retrieves circulation rule attributes that apply to the given
1548 branch and item type, regardless of patron category.
1550 The return value is a hashref containing the following keys:
1552 holdallowed => Hold policy for this branch and itemtype. Possible values:
1553 0: No holds allowed.
1554 1: Holds allowed only by patrons that have the same homebranch as the item.
1555 2: Holds allowed from any patron.
1557 returnbranch => branch to which to return item. Possible values:
1558 noreturn: do not return, let item remain where checked in (floating collections)
1559 homebranch: return to item's home branch
1561 This searches branchitemrules in the following order:
1563 * Same branchcode and itemtype
1564 * Same branchcode, itemtype '*'
1565 * branchcode '*', same itemtype
1566 * branchcode and itemtype '*'
1568 Neither C<$branchcode> nor C<$itemtype> should be '*'.
1572 sub GetBranchItemRule {
1573 my ( $branchcode, $itemtype ) = @_;
1574 my $dbh = C4::Context->dbh();
1578 ['SELECT holdallowed, returnbranch
1579 FROM branch_item_rules
1580 WHERE branchcode = ?
1581 AND itemtype = ?', $branchcode, $itemtype],
1582 ['SELECT holdallowed, returnbranch
1583 FROM default_branch_circ_rules
1584 WHERE branchcode = ?', $branchcode],
1585 ['SELECT holdallowed, returnbranch
1586 FROM default_branch_item_rules
1587 WHERE itemtype = ?', $itemtype],
1588 ['SELECT holdallowed, returnbranch
1589 FROM default_circ_rules'],
1592 foreach my $attempt (@attempts) {
1593 my ($query, @bind_params) = @{$attempt};
1594 my $search_result = $dbh->selectrow_hashref ( $query , {}, @bind_params )
1597 # Since branch/category and branch/itemtype use the same per-branch
1598 # defaults tables, we have to check that the key we want is set, not
1599 # just that a row was returned
1600 $result->{'holdallowed'} = $search_result->{'holdallowed'} unless ( defined $result->{'holdallowed'} );
1601 $result->{'returnbranch'} = $search_result->{'returnbranch'} unless ( defined $result->{'returnbranch'} );
1604 # built-in default circulation rule
1605 $result->{'holdallowed'} = 2 unless ( defined $result->{'holdallowed'} );
1606 $result->{'returnbranch'} = 'homebranch' unless ( defined $result->{'returnbranch'} );
1613 ($doreturn, $messages, $iteminformation, $borrower) =
1614 &AddReturn($barcode, $branch, $exemptfine, $dropbox);
1620 =item C<$barcode> is the bar code of the book being returned.
1622 =item C<$branch> is the code of the branch where the book is being returned.
1624 =item C<$exemptfine> indicates that overdue charges for the item will be
1627 =item C<$dropbox> indicates that the check-in date is assumed to be
1628 yesterday, or the last non-holiday as defined in C4::Calendar . If
1629 overdue charges are applied and C<$dropbox> is true, the last charge
1630 will be removed. This assumes that the fines accrual script has run
1635 C<&AddReturn> returns a list of four items:
1637 C<$doreturn> is true iff the return succeeded.
1639 C<$messages> is a reference-to-hash giving feedback on the operation.
1640 The keys of the hash are:
1646 No item with this barcode exists. The value is C<$barcode>.
1650 The book is not currently on loan. The value is C<$barcode>.
1652 =item C<IsPermanent>
1654 The book's home branch is a permanent collection. If you have borrowed
1655 this book, you are not allowed to return it. The value is the code for
1656 the book's home branch.
1660 This book has been withdrawn/cancelled. The value should be ignored.
1662 =item C<Wrongbranch>
1664 This book has was returned to the wrong branch. The value is a hashref
1665 so that C<$messages->{Wrongbranch}->{Wrongbranch}> and C<$messages->{Wrongbranch}->{Rightbranch}>
1666 contain the branchcode of the incorrect and correct return library, respectively.
1670 The item was reserved. The value is a reference-to-hash whose keys are
1671 fields from the reserves table of the Koha database, and
1672 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1673 either C<Waiting>, C<Reserved>, or 0.
1677 C<$iteminformation> is a reference-to-hash, giving information about the
1678 returned item from the issues table.
1680 C<$borrower> is a reference-to-hash, giving information about the
1681 patron who last borrowed the book.
1686 my ( $barcode, $branch, $exemptfine, $dropbox ) = @_;
1688 if ($branch and not GetBranchDetail($branch)) {
1689 warn "AddReturn error: branch '$branch' not found. Reverting to " . C4::Context->userenv->{'branch'};
1692 $branch = C4::Context->userenv->{'branch'} unless $branch; # we trust userenv to be a safe fallback/default
1697 my $validTransfert = 0;
1698 my $stat_type = 'return';
1700 # get information on item
1701 my $itemnumber = GetItemnumberFromBarcode( $barcode );
1702 unless ($itemnumber) {
1703 return (0, { BadBarcode => $barcode }); # no barcode means no item or borrower. bail out.
1705 my $issue = GetItemIssue($itemnumber);
1706 # warn Dumper($iteminformation);
1707 if ($issue and $issue->{borrowernumber}) {
1708 $borrower = C4::Members::GetMemberDetails($issue->{borrowernumber})
1709 or die "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be issued to non-existant borrowernumber '$issue->{borrowernumber}'\n"
1710 . Dumper($issue) . "\n";
1712 $messages->{'NotIssued'} = $barcode;
1713 # even though item is not on loan, it may still be transferred; therefore, get current branch info
1715 # No issue, no borrowernumber. ONLY if $doreturn, *might* you have a $borrower later.
1716 # Record this as a local use, instead of a return, if the RecordLocalUseOnReturn is on
1717 if (C4::Context->preference("RecordLocalUseOnReturn")) {
1718 $messages->{'LocalUse'} = 1;
1719 $stat_type = 'localuse';
1723 my $item = GetItem($itemnumber) or die "GetItem($itemnumber) failed";
1724 # full item data, but no borrowernumber or checkout info (no issue)
1725 # we know GetItem should work because GetItemnumberFromBarcode worked
1726 my $hbr = GetBranchItemRule($item->{'homebranch'}, $item->{'itype'})->{'returnbranch'} || "homebranch";
1727 # get the proper branch to which to return the item
1728 $hbr = $item->{$hbr} || $branch ;
1729 # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch)
1731 my $borrowernumber = $borrower->{'borrowernumber'} || undef; # we don't know if we had a borrower or not
1733 # check if the book is in a permanent collection....
1734 # FIXME -- This 'PE' attribute is largely undocumented. afaict, there's no user interface that reflects this functionality.
1736 my $branches = GetBranches(); # a potentially expensive call for a non-feature.
1737 $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1740 # check if the return is allowed at this branch
1741 my ($returnallowed, $message) = CanBookBeReturned($item, $branch);
1742 unless ($returnallowed){
1743 $messages->{'Wrongbranch'} = {
1744 Wrongbranch => $branch,
1745 Rightbranch => $message
1748 return ( $doreturn, $messages, $issue, $borrower );
1751 if ( $item->{'wthdrawn'} ) { # book has been cancelled
1752 $messages->{'wthdrawn'} = 1;
1753 $doreturn = 0 if C4::Context->preference("BlockReturnOfWithdrawnItems");
1756 # case of a return of document (deal with issues and holdingbranch)
1757 my $today = DateTime->now( time_zone => C4::Context->tz() );
1759 my $datedue = $issue->{date_due};
1760 $borrower or warn "AddReturn without current borrower";
1761 my $circControlBranch;
1763 # define circControlBranch only if dropbox mode is set
1764 # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1765 # FIXME: check issuedate > returndate, factoring in holidays
1766 #$circControlBranch = _GetCircControlBranch($item,$borrower) unless ( $item->{'issuedate'} eq C4::Dates->today('iso') );;
1767 $circControlBranch = _GetCircControlBranch($item,$borrower);
1768 $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1771 if ($borrowernumber) {
1772 if($issue->{'overdue'}){
1773 my ( $amount, $type, $unitcounttotal ) = C4::Overdues::CalcFine( $item, $borrower->{categorycode},$branch, $datedue, $today );
1775 if ( $amount > 0 && ( C4::Context->preference('finesMode') eq 'production' )) {
1776 C4::Overdues::UpdateFine(
1777 $issue->{itemnumber},
1778 $issue->{borrowernumber},
1779 $amount, $type, output_pref($datedue)
1783 MarkIssueReturned($borrowernumber, $item->{'itemnumber'}, $circControlBranch, '', $borrower->{'privacy'});
1784 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right? This could be the borrower hash.
1787 ModItem({ onloan => undef }, $issue->{'biblionumber'}, $item->{'itemnumber'});
1790 # the holdingbranch is updated if the document is returned to another location.
1791 # this is always done regardless of whether the item was on loan or not
1792 if ($item->{'holdingbranch'} ne $branch) {
1793 UpdateHoldingbranch($branch, $item->{'itemnumber'});
1794 $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1796 ModDateLastSeen( $item->{'itemnumber'} );
1798 # check if we have a transfer for this document
1799 my ($datesent,$frombranch,$tobranch) = GetTransfers( $item->{'itemnumber'} );
1801 # if we have a transfer to do, we update the line of transfers with the datearrived
1803 if ( $tobranch eq $branch ) {
1804 my $sth = C4::Context->dbh->prepare(
1805 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1807 $sth->execute( $item->{'itemnumber'} );
1808 # if we have a reservation with valid transfer, we can set it's status to 'W'
1809 ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1810 C4::Reserves::ModReserveStatus($item->{'itemnumber'}, 'W');
1812 $messages->{'WrongTransfer'} = $tobranch;
1813 $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1815 $validTransfert = 1;
1817 ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1820 # fix up the accounts.....
1821 if ( $item->{'itemlost'} ) {
1822 $messages->{'WasLost'} = 1;
1824 if ( C4::Context->preference('RefundLostItemFeeOnReturn' ) ) {
1825 _FixAccountForLostAndReturned($item->{'itemnumber'}, $borrowernumber, $barcode); # can tolerate undef $borrowernumber
1826 $messages->{'LostItemFeeRefunded'} = 1;
1830 # fix up the overdues in accounts...
1831 if ($borrowernumber) {
1832 my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1833 defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!"; # zero is OK, check defined
1835 if ( $issue->{overdue} && $issue->{date_due} ) {
1838 _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1839 $messages->{Debarred} = $debardate if ($debardate);
1843 # find reserves.....
1844 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1845 my ($resfound, $resrec);
1846 ($resfound, $resrec, undef) = C4::Reserves::CheckReserves( $item->{'itemnumber'} ) unless ( $item->{'wthdrawn'} );
1848 $resrec->{'ResFound'} = $resfound;
1849 $messages->{'ResFound'} = $resrec;
1853 # Record the fact that this book was returned.
1855 $branch, $stat_type, '0', '',
1856 $item->{'itemnumber'},
1857 $biblio->{'itemtype'},
1858 $borrowernumber, undef, $item->{'ccode'}
1861 # Send a check-in slip. # NOTE: borrower may be undef. probably shouldn't try to send messages then.
1862 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1864 branchcode => $branch,
1865 categorycode => $borrower->{categorycode},
1866 item_type => $item->{itype},
1867 notification => 'CHECKIN',
1869 if ($doreturn && $circulation_alert->is_enabled_for(\%conditions)) {
1870 SendCirculationAlert({
1873 borrower => $borrower,
1878 logaction("CIRCULATION", "RETURN", $borrowernumber, $item->{'biblionumber'})
1879 if C4::Context->preference("ReturnLog");
1881 # FIXME: make this comment intelligible.
1882 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1883 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1885 if (($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $hbr) and not $messages->{'WrongTransfer'}){
1886 if ( C4::Context->preference("AutomaticItemReturn" ) or
1887 (C4::Context->preference("UseBranchTransferLimits") and
1888 ! IsBranchTransferAllowed($branch, $hbr, $item->{C4::Context->preference("BranchTransferLimitsType")} )
1890 $debug and warn sprintf "about to call ModItemTransfer(%s, %s, %s)", $item->{'itemnumber'},$branch, $hbr;
1891 $debug and warn "item: " . Dumper($item);
1892 ModItemTransfer($item->{'itemnumber'}, $branch, $hbr);
1893 $messages->{'WasTransfered'} = 1;
1895 $messages->{'NeedsTransfer'} = 1; # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1898 return ( $doreturn, $messages, $issue, $borrower );
1901 =head2 MarkIssueReturned
1903 MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy);
1905 Unconditionally marks an issue as being returned by
1906 moving the C<issues> row to C<old_issues> and
1907 setting C<returndate> to the current date, or
1908 the last non-holiday date of the branccode specified in
1909 C<dropbox_branch> . Assumes you've already checked that
1910 it's safe to do this, i.e. last non-holiday > issuedate.
1912 if C<$returndate> is specified (in iso format), it is used as the date
1913 of the return. It is ignored when a dropbox_branch is passed in.
1915 C<$privacy> contains the privacy parameter. If the patron has set privacy to 2,
1916 the old_issue is immediately anonymised
1918 Ideally, this function would be internal to C<C4::Circulation>,
1919 not exported, but it is currently needed by one
1920 routine in C<C4::Accounts>.
1924 sub MarkIssueReturned {
1925 my ( $borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy ) = @_;
1927 my $dbh = C4::Context->dbh;
1928 my $query = 'UPDATE issues SET returndate=';
1930 if ($dropbox_branch) {
1931 my $calendar = Koha::Calendar->new( branchcode => $dropbox_branch );
1932 my $dropboxdate = $calendar->addDate( DateTime->now( time_zone => C4::Context->tz), -1 );
1934 push @bind, $dropboxdate->strftime('%Y-%m-%d %H:%M');
1935 } elsif ($returndate) {
1937 push @bind, $returndate;
1939 $query .= ' now() ';
1941 $query .= ' WHERE borrowernumber = ? AND itemnumber = ?';
1942 push @bind, $borrowernumber, $itemnumber;
1944 my $sth_upd = $dbh->prepare($query);
1945 $sth_upd->execute(@bind);
1946 my $sth_copy = $dbh->prepare('INSERT INTO old_issues SELECT * FROM issues
1947 WHERE borrowernumber = ?
1948 AND itemnumber = ?');
1949 $sth_copy->execute($borrowernumber, $itemnumber);
1950 # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
1951 if ( $privacy == 2) {
1952 # The default of 0 does not work due to foreign key constraints
1953 # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
1954 my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
1955 my $sth_ano = $dbh->prepare("UPDATE old_issues SET borrowernumber=?
1956 WHERE borrowernumber = ?
1957 AND itemnumber = ?");
1958 $sth_ano->execute($anonymouspatron, $borrowernumber, $itemnumber);
1960 my $sth_del = $dbh->prepare("DELETE FROM issues
1961 WHERE borrowernumber = ?
1962 AND itemnumber = ?");
1963 $sth_del->execute($borrowernumber, $itemnumber);
1966 =head2 _debar_user_on_return
1968 _debar_user_on_return($borrower, $item, $datedue, today);
1970 C<$borrower> borrower hashref
1972 C<$item> item hashref
1974 C<$datedue> date due DateTime object
1976 C<$today> DateTime object representing the return time
1978 Internal function, called only by AddReturn that calculates and updates
1979 the user fine days, and debars him if necessary.
1981 Should only be called for overdue returns
1985 sub _debar_user_on_return {
1986 my ( $borrower, $item, $dt_due, $dt_today ) = @_;
1988 my $branchcode = _GetCircControlBranch( $item, $borrower );
1989 my $calendar = Koha::Calendar->new( branchcode => $branchcode );
1991 # $deltadays is a DateTime::Duration object
1992 my $deltadays = $calendar->days_between( $dt_due, $dt_today );
1994 my $circcontrol = C4::Context->preference('CircControl');
1996 GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
1997 my $finedays = $issuingrule->{finedays};
1998 my $unit = $issuingrule->{lengthunit};
2002 # finedays is in days, so hourly loans must multiply by 24
2003 # thus 1 hour late equals 1 day suspension * finedays rate
2004 $finedays = $finedays * 24 if ( $unit eq 'hours' );
2006 # grace period is measured in the same units as the loan
2008 DateTime::Duration->new( $unit => $issuingrule->{firstremind} );
2009 if ( $deltadays->subtract($grace)->is_positive() ) {
2012 $dt_today->clone()->add_duration( $deltadays * $finedays );
2013 if ( $borrower->{debarred} ) {
2014 my $borrower_debar_dt = dt_from_string( $borrower->{debarred} );
2016 # Update patron only if new date > old
2017 if ( DateTime->compare( $borrower_debar_dt, $new_debar_dt ) !=
2024 C4::Members::DebarMember( $borrower->{borrowernumber},
2025 $new_debar_dt->ymd() );
2026 return $new_debar_dt->ymd();
2032 =head2 _FixOverduesOnReturn
2034 &_FixOverduesOnReturn($brn,$itm, $exemptfine, $dropboxmode);
2036 C<$brn> borrowernumber
2040 C<$exemptfine> BOOL -- remove overdue charge associated with this issue.
2041 C<$dropboxmode> BOOL -- remove lastincrement on overdue charge associated with this issue.
2043 Internal function, called only by AddReturn
2047 sub _FixOverduesOnReturn {
2048 my ($borrowernumber, $item);
2049 unless ($borrowernumber = shift) {
2050 warn "_FixOverduesOnReturn() not supplied valid borrowernumber";
2053 unless ($item = shift) {
2054 warn "_FixOverduesOnReturn() not supplied valid itemnumber";
2057 my ($exemptfine, $dropbox) = @_;
2058 my $dbh = C4::Context->dbh;
2060 # check for overdue fine
2061 my $sth = $dbh->prepare(
2062 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
2064 $sth->execute( $borrowernumber, $item );
2066 # alter fine to show that the book has been returned
2067 my $data = $sth->fetchrow_hashref;
2068 return 0 unless $data; # no warning, there's just nothing to fix
2071 my @bind = ($data->{'accountlines_id'});
2073 $uquery = "update accountlines set accounttype='FFOR', amountoutstanding=0";
2074 if (C4::Context->preference("FinesLog")) {
2075 &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
2077 } elsif ($dropbox && $data->{lastincrement}) {
2078 my $outstanding = $data->{amountoutstanding} - $data->{lastincrement} ;
2079 my $amt = $data->{amount} - $data->{lastincrement} ;
2080 if (C4::Context->preference("FinesLog")) {
2081 &logaction("FINES", 'MODIFY',$borrowernumber,"Dropbox adjustment $amt, item $item");
2083 $uquery = "update accountlines set accounttype='F' ";
2084 if($outstanding >= 0 && $amt >=0) {
2085 $uquery .= ", amount = ? , amountoutstanding=? ";
2086 unshift @bind, ($amt, $outstanding) ;
2089 $uquery = "update accountlines set accounttype='F' ";
2091 $uquery .= " where (accountlines_id = ?)";
2092 my $usth = $dbh->prepare($uquery);
2093 return $usth->execute(@bind);
2096 =head2 _FixAccountForLostAndReturned
2098 &_FixAccountForLostAndReturned($itemnumber, [$borrowernumber, $barcode]);
2100 Calculates the charge for a book lost and returned.
2102 Internal function, not exported, called only by AddReturn.
2104 FIXME: This function reflects how inscrutable fines logic is. Fix both.
2105 FIXME: Give a positive return value on success. It might be the $borrowernumber who received credit, or the amount forgiven.
2109 sub _FixAccountForLostAndReturned {
2110 my $itemnumber = shift or return;
2111 my $borrowernumber = @_ ? shift : undef;
2112 my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description
2113 my $dbh = C4::Context->dbh;
2114 # check for charge made for lost book
2115 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE itemnumber = ? AND accounttype IN ('L', 'Rep', 'W') ORDER BY date DESC, accountno DESC");
2116 $sth->execute($itemnumber);
2117 my $data = $sth->fetchrow_hashref;
2118 $data or return; # bail if there is nothing to do
2119 $data->{accounttype} eq 'W' and return; # Written off
2121 # writeoff this amount
2123 my $amount = $data->{'amount'};
2124 my $acctno = $data->{'accountno'};
2125 my $amountleft; # Starts off undef/zero.
2126 if ($data->{'amountoutstanding'} == $amount) {
2127 $offset = $data->{'amount'};
2128 $amountleft = 0; # Hey, it's zero here, too.
2130 $offset = $amount - $data->{'amountoutstanding'}; # Um, isn't this the same as ZERO? We just tested those two things are ==
2131 $amountleft = $data->{'amountoutstanding'} - $amount; # Um, isn't this the same as ZERO? We just tested those two things are ==
2133 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
2134 WHERE (accountlines_id = ?)");
2135 $usth->execute($data->{'accountlines_id'}); # We might be adjusting an account for some OTHER borrowernumber now. Not the one we passed in.
2136 #check if any credit is left if so writeoff other accounts
2137 my $nextaccntno = getnextacctno($data->{'borrowernumber'});
2138 $amountleft *= -1 if ($amountleft < 0);
2139 if ($amountleft > 0) {
2140 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
2141 AND (amountoutstanding >0) ORDER BY date"); # might want to order by amountoustanding ASC (pay smallest first)
2142 $msth->execute($data->{'borrowernumber'});
2143 # offset transactions
2146 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
2147 if ($accdata->{'amountoutstanding'} < $amountleft) {
2149 $amountleft -= $accdata->{'amountoutstanding'};
2151 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
2154 my $thisacct = $accdata->{'accountlines_id'};
2155 # FIXME: move prepares outside while loop!
2156 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
2157 WHERE (accountlines_id = ?)");
2158 $usth->execute($newamtos,'$thisacct'); # FIXME: '$thisacct' is a string literal!
2159 $usth = $dbh->prepare("INSERT INTO accountoffsets
2160 (borrowernumber, accountno, offsetaccount, offsetamount)
2163 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
2165 $msth->finish; # $msth might actually have data left
2167 $amountleft *= -1 if ($amountleft > 0);
2168 my $desc = "Item Returned " . $item_id;
2169 $usth = $dbh->prepare("INSERT INTO accountlines
2170 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
2171 VALUES (?,?,now(),?,?,'CR',?)");
2172 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
2173 if ($borrowernumber) {
2174 # FIXME: same as query above. use 1 sth for both
2175 $usth = $dbh->prepare("INSERT INTO accountoffsets
2176 (borrowernumber, accountno, offsetaccount, offsetamount)
2178 $usth->execute($borrowernumber, $data->{'accountno'}, $nextaccntno, $offset);
2180 ModItem({ paidfor => '' }, undef, $itemnumber);
2184 =head2 _GetCircControlBranch
2186 my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
2190 Return the library code to be used to determine which circulation
2191 policy applies to a transaction. Looks up the CircControl and
2192 HomeOrHoldingBranch system preferences.
2194 C<$iteminfos> is a hashref to iteminfo. Only {homebranch or holdingbranch} is used.
2196 C<$borrower> is a hashref to borrower. Only {branchcode} is used.
2200 sub _GetCircControlBranch {
2201 my ($item, $borrower) = @_;
2202 my $circcontrol = C4::Context->preference('CircControl');
2205 if ($circcontrol eq 'PickupLibrary' and (C4::Context->userenv and C4::Context->userenv->{'branch'}) ) {
2206 $branch= C4::Context->userenv->{'branch'};
2207 } elsif ($circcontrol eq 'PatronLibrary') {
2208 $branch=$borrower->{branchcode};
2210 my $branchfield = C4::Context->preference('HomeOrHoldingBranch') || 'homebranch';
2211 $branch = $item->{$branchfield};
2212 # default to item home branch if holdingbranch is used
2213 # and is not defined
2214 if (!defined($branch) && $branchfield eq 'holdingbranch') {
2215 $branch = $item->{homebranch};
2228 $issue = &GetItemIssue($itemnumber);
2230 Returns patron currently having a book, or undef if not checked out.
2232 C<$itemnumber> is the itemnumber.
2234 C<$issue> is a hashref of the row from the issues table.
2239 my ($itemnumber) = @_;
2240 return unless $itemnumber;
2241 my $sth = C4::Context->dbh->prepare(
2244 LEFT JOIN items ON issues.itemnumber=items.itemnumber
2245 WHERE issues.itemnumber=?");
2246 $sth->execute($itemnumber);
2247 my $data = $sth->fetchrow_hashref;
2248 return unless $data;
2249 $data->{issuedate} = dt_from_string($data->{issuedate}, 'sql');
2250 $data->{issuedate}->truncate(to => 'minute');
2251 $data->{date_due} = dt_from_string($data->{date_due}, 'sql');
2252 $data->{date_due}->truncate(to => 'minute');
2253 my $dt = DateTime->now( time_zone => C4::Context->tz)->truncate( to => 'minute');
2254 $data->{'overdue'} = DateTime->compare($data->{'date_due'}, $dt ) == -1 ? 1 : 0;
2260 $issue = GetOpenIssue( $itemnumber );
2262 Returns the row from the issues table if the item is currently issued, undef if the item is not currently issued
2264 C<$itemnumber> is the item's itemnumber
2271 my ( $itemnumber ) = @_;
2273 my $dbh = C4::Context->dbh;
2274 my $sth = $dbh->prepare( "SELECT * FROM issues WHERE itemnumber = ? AND returndate IS NULL" );
2275 $sth->execute( $itemnumber );
2276 my $issue = $sth->fetchrow_hashref();
2280 =head2 GetItemIssues
2282 $issues = &GetItemIssues($itemnumber, $history);
2284 Returns patrons that have issued a book
2286 C<$itemnumber> is the itemnumber
2287 C<$history> is false if you just want the current "issuer" (if any)
2288 and true if you want issues history from old_issues also.
2290 Returns reference to an array of hashes
2295 my ( $itemnumber, $history ) = @_;
2297 my $today = DateTime->now( time_zome => C4::Context->tz); # get today date
2298 $today->truncate( to => 'minute' );
2299 my $sql = "SELECT * FROM issues
2300 JOIN borrowers USING (borrowernumber)
2301 JOIN items USING (itemnumber)
2302 WHERE issues.itemnumber = ? ";
2305 SELECT * FROM old_issues
2306 LEFT JOIN borrowers USING (borrowernumber)
2307 JOIN items USING (itemnumber)
2308 WHERE old_issues.itemnumber = ? ";
2310 $sql .= "ORDER BY date_due DESC";
2311 my $sth = C4::Context->dbh->prepare($sql);
2313 $sth->execute($itemnumber, $itemnumber);
2315 $sth->execute($itemnumber);
2317 my $results = $sth->fetchall_arrayref({});
2318 foreach (@$results) {
2319 my $date_due = dt_from_string($_->{date_due},'sql');
2320 $date_due->truncate( to => 'minute' );
2322 $_->{overdue} = (DateTime->compare($date_due, $today) == -1) ? 1 : 0;
2327 =head2 GetBiblioIssues
2329 $issues = GetBiblioIssues($biblionumber);
2331 this function get all issues from a biblionumber.
2334 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
2335 tables issues and the firstname,surname & cardnumber from borrowers.
2339 sub GetBiblioIssues {
2340 my $biblionumber = shift;
2341 return unless $biblionumber;
2342 my $dbh = C4::Context->dbh;
2344 SELECT issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
2346 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
2347 LEFT JOIN items ON issues.itemnumber = items.itemnumber
2348 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
2349 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
2350 WHERE biblio.biblionumber = ?
2352 SELECT old_issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
2354 LEFT JOIN borrowers ON borrowers.borrowernumber = old_issues.borrowernumber
2355 LEFT JOIN items ON old_issues.itemnumber = items.itemnumber
2356 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
2357 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
2358 WHERE biblio.biblionumber = ?
2361 my $sth = $dbh->prepare($query);
2362 $sth->execute($biblionumber, $biblionumber);
2365 while ( my $data = $sth->fetchrow_hashref ) {
2366 push @issues, $data;
2371 =head2 GetUpcomingDueIssues
2373 my $upcoming_dues = GetUpcomingDueIssues( { days_in_advance => 4 } );
2377 sub GetUpcomingDueIssues {
2380 $params->{'days_in_advance'} = 7 unless exists $params->{'days_in_advance'};
2381 my $dbh = C4::Context->dbh;
2383 my $statement = <<END_SQL;
2384 SELECT issues.*, items.itype as itemtype, items.homebranch, TO_DAYS( date_due )-TO_DAYS( NOW() ) as days_until_due, branches.branchemail
2386 LEFT JOIN items USING (itemnumber)
2387 LEFT OUTER JOIN branches USING (branchcode)
2388 WhERE returndate is NULL
2389 AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ?
2392 my @bind_parameters = ( $params->{'days_in_advance'} );
2394 my $sth = $dbh->prepare( $statement );
2395 $sth->execute( @bind_parameters );
2396 my $upcoming_dues = $sth->fetchall_arrayref({});
2399 return $upcoming_dues;
2402 =head2 CanBookBeRenewed
2404 ($ok,$error) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2406 Find out whether a borrowed item may be renewed.
2408 C<$dbh> is a DBI handle to the Koha database.
2410 C<$borrowernumber> is the borrower number of the patron who currently
2411 has the item on loan.
2413 C<$itemnumber> is the number of the item to renew.
2415 C<$override_limit>, if supplied with a true value, causes
2416 the limit on the number of times that the loan can be renewed
2417 (as controlled by the item type) to be ignored.
2419 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
2420 item must currently be on loan to the specified borrower; renewals
2421 must be allowed for the item's type; and the borrower must not have
2422 already renewed the loan. $error will contain the reason the renewal can not proceed
2426 sub CanBookBeRenewed {
2428 # check renewal status
2429 my ( $borrowernumber, $itemnumber, $override_limit ) = @_;
2430 my $dbh = C4::Context->dbh;
2435 # Look in the issues table for this item, lent to this borrower,
2436 # and not yet returned.
2438 # Look in the issues table for this item, lent to this borrower,
2439 # and not yet returned.
2441 'ItemHomeLibrary' => 'items.homebranch',
2442 'PickupLibrary' => 'items.holdingbranch',
2443 'PatronLibrary' => 'borrowers.branchcode'
2445 my $controlbranch = $branch{C4::Context->preference('CircControl')};
2446 my $itype = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype';
2448 my $sthcount = $dbh->prepare("
2450 borrowers.categorycode, biblioitems.itemtype, issues.renewals, renewalsallowed, $controlbranch
2453 LEFT JOIN items USING (itemnumber)
2454 LEFT JOIN borrowers USING (borrowernumber)
2455 LEFT JOIN biblioitems USING (biblioitemnumber)
2458 (issuingrules.categorycode = borrowers.categorycode OR issuingrules.categorycode = '*')
2460 (issuingrules.itemtype = $itype OR issuingrules.itemtype = '*')
2462 (issuingrules.branchcode = $controlbranch OR issuingrules.branchcode = '*')
2468 issuingrules.categorycode desc,
2469 issuingrules.itemtype desc,
2470 issuingrules.branchcode desc
2474 $sthcount->execute( $borrowernumber, $itemnumber );
2475 if ( my $data1 = $sthcount->fetchrow_hashref ) {
2477 if ( ( $data1->{renewalsallowed} && $data1->{renewalsallowed} > $data1->{renewals} ) || $override_limit ) {
2484 my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber);
2491 return ($renewokay,$error);
2496 &AddRenewal($borrowernumber, $itemnumber, $branch, [$datedue], [$lastreneweddate]);
2500 C<$borrowernumber> is the borrower number of the patron who currently
2503 C<$itemnumber> is the number of the item to renew.
2505 C<$branch> is the library where the renewal took place (if any).
2506 The library that controls the circ policies for the renewal is retrieved from the issues record.
2508 C<$datedue> can be a C4::Dates object used to set the due date.
2510 C<$lastreneweddate> is an optional ISO-formatted date used to set issues.lastreneweddate. If
2511 this parameter is not supplied, lastreneweddate is set to the current date.
2513 If C<$datedue> is the empty string, C<&AddRenewal> will calculate the due date automatically
2514 from the book's item type.
2519 my $borrowernumber = shift or return;
2520 my $itemnumber = shift or return;
2522 my $datedue = shift;
2523 my $lastreneweddate = shift || DateTime->now(time_zone => C4::Context->tz)->ymd();
2524 my $item = GetItem($itemnumber) or return;
2525 my $biblio = GetBiblioFromItemNumber($itemnumber) or return;
2527 my $dbh = C4::Context->dbh;
2528 # Find the issues record for this book
2530 $dbh->prepare("SELECT * FROM issues
2531 WHERE borrowernumber=?
2534 $sth->execute( $borrowernumber, $itemnumber );
2535 my $issuedata = $sth->fetchrow_hashref;
2537 if(defined $datedue && ref $datedue ne 'DateTime' ) {
2538 carp 'Invalid date passed to AddRenewal.';
2541 # If the due date wasn't specified, calculate it by adding the
2542 # book's loan length to today's date or the current due date
2543 # based on the value of the RenewalPeriodBase syspref.
2546 my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber ) or return;
2547 my $itemtype = (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'};
2549 $datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ?
2550 $issuedata->{date_due} :
2551 DateTime->now( time_zone => C4::Context->tz());
2552 $datedue = CalcDateDue($datedue,$itemtype,$issuedata->{'branchcode'},$borrower);
2555 # Update the issues record to have the new due date, and a new count
2556 # of how many times it has been renewed.
2557 my $renews = $issuedata->{'renewals'} + 1;
2558 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = ?
2559 WHERE borrowernumber=?
2563 $sth->execute( $datedue->strftime('%Y-%m-%d %H:%M'), $renews, $lastreneweddate, $borrowernumber, $itemnumber );
2566 # Update the renewal count on the item, and tell zebra to reindex
2567 $renews = $biblio->{'renewals'} + 1;
2568 ModItem({ renewals => $renews, onloan => $datedue->strftime('%Y-%m-%d %H:%M')}, $biblio->{'biblionumber'}, $itemnumber);
2570 # Charge a new rental fee, if applicable?
2571 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
2572 if ( $charge > 0 ) {
2573 my $accountno = getnextacctno( $borrowernumber );
2574 my $item = GetBiblioFromItemNumber($itemnumber);
2576 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
2577 $sth = $dbh->prepare(
2578 "INSERT INTO accountlines
2579 (date, borrowernumber, accountno, amount, manager_id,
2580 description,accounttype, amountoutstanding, itemnumber)
2581 VALUES (now(),?,?,?,?,?,?,?,?)"
2583 $sth->execute( $borrowernumber, $accountno, $charge, $manager_id,
2584 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
2585 'Rent', $charge, $itemnumber );
2588 # Send a renewal slip according to checkout alert preferencei
2589 if ( C4::Context->preference('RenewalSendNotice') eq '1') {
2590 my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 );
2591 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
2593 branchcode => $branch,
2594 categorycode => $borrower->{categorycode},
2595 item_type => $item->{itype},
2596 notification => 'CHECKOUT',
2598 if ($circulation_alert->is_enabled_for(\%conditions)) {
2599 SendCirculationAlert({
2602 borrower => $borrower,
2609 UpdateStats( $branch, 'renew', $charge, '', $itemnumber, $item->{itype}, $borrowernumber, undef, $item->{'ccode'});
2614 # check renewal status
2615 my ( $bornum, $itemno ) = @_;
2616 my $dbh = C4::Context->dbh;
2618 my $renewsallowed = 0;
2621 my $borrower = C4::Members::GetMember( borrowernumber => $bornum);
2622 my $item = GetItem($itemno);
2624 # Look in the issues table for this item, lent to this borrower,
2625 # and not yet returned.
2627 # FIXME - I think this function could be redone to use only one SQL call.
2628 my $sth = $dbh->prepare(
2629 "select * from issues
2630 where (borrowernumber = ?)
2631 and (itemnumber = ?)"
2633 $sth->execute( $bornum, $itemno );
2634 my $data = $sth->fetchrow_hashref;
2635 $renewcount = $data->{'renewals'} if $data->{'renewals'};
2637 # $item and $borrower should be calculated
2638 my $branchcode = _GetCircControlBranch($item, $borrower);
2640 my $issuingrule = GetIssuingRule($borrower->{categorycode}, $item->{itype}, $branchcode);
2642 $renewsallowed = $issuingrule->{'renewalsallowed'};
2643 $renewsleft = $renewsallowed - $renewcount;
2644 if($renewsleft < 0){ $renewsleft = 0; }
2645 return ( $renewcount, $renewsallowed, $renewsleft );
2648 =head2 GetIssuingCharges
2650 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
2652 Calculate how much it would cost for a given patron to borrow a given
2653 item, including any applicable discounts.
2655 C<$itemnumber> is the item number of item the patron wishes to borrow.
2657 C<$borrowernumber> is the patron's borrower number.
2659 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
2660 and C<$item_type> is the code for the item's item type (e.g., C<VID>
2665 sub GetIssuingCharges {
2667 # calculate charges due
2668 my ( $itemnumber, $borrowernumber ) = @_;
2670 my $dbh = C4::Context->dbh;
2673 # Get the book's item type and rental charge (via its biblioitem).
2674 my $charge_query = 'SELECT itemtypes.itemtype,rentalcharge FROM items
2675 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber';
2676 $charge_query .= (C4::Context->preference('item-level_itypes'))
2677 ? ' LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype'
2678 : ' LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype';
2680 $charge_query .= ' WHERE items.itemnumber =?';
2682 my $sth = $dbh->prepare($charge_query);
2683 $sth->execute($itemnumber);
2684 if ( my $item_data = $sth->fetchrow_hashref ) {
2685 $item_type = $item_data->{itemtype};
2686 $charge = $item_data->{rentalcharge};
2687 my $branch = C4::Branch::mybranch();
2688 my $discount_query = q|SELECT rentaldiscount,
2689 issuingrules.itemtype, issuingrules.branchcode
2691 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
2692 WHERE borrowers.borrowernumber = ?
2693 AND (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2694 AND (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')|;
2695 my $discount_sth = $dbh->prepare($discount_query);
2696 $discount_sth->execute( $borrowernumber, $item_type, $branch );
2697 my $discount_rules = $discount_sth->fetchall_arrayref({});
2698 if (@{$discount_rules}) {
2699 # We may have multiple rules so get the most specific
2700 my $discount = _get_discount_from_rule($discount_rules, $branch, $item_type);
2701 $charge = ( $charge * ( 100 - $discount ) ) / 100;
2705 $sth->finish; # we havent _explicitly_ fetched all rows
2706 return ( $charge, $item_type );
2709 # Select most appropriate discount rule from those returned
2710 sub _get_discount_from_rule {
2711 my ($rules_ref, $branch, $itemtype) = @_;
2714 if (@{$rules_ref} == 1) { # only 1 applicable rule use it
2715 $discount = $rules_ref->[0]->{rentaldiscount};
2716 return (defined $discount) ? $discount : 0;
2718 # could have up to 4 does one match $branch and $itemtype
2719 my @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq $itemtype } @{$rules_ref};
2721 $discount = $d[0]->{rentaldiscount};
2722 return (defined $discount) ? $discount : 0;
2724 # do we have item type + all branches
2725 @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq $itemtype } @{$rules_ref};
2727 $discount = $d[0]->{rentaldiscount};
2728 return (defined $discount) ? $discount : 0;
2730 # do we all item types + this branch
2731 @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq q{*} } @{$rules_ref};
2733 $discount = $d[0]->{rentaldiscount};
2734 return (defined $discount) ? $discount : 0;
2736 # so all and all (surely we wont get here)
2737 @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq q{*} } @{$rules_ref};
2739 $discount = $d[0]->{rentaldiscount};
2740 return (defined $discount) ? $discount : 0;
2746 =head2 AddIssuingCharge
2748 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
2752 sub AddIssuingCharge {
2753 my ( $itemnumber, $borrowernumber, $charge ) = @_;
2754 my $dbh = C4::Context->dbh;
2755 my $nextaccntno = getnextacctno( $borrowernumber );
2757 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
2759 INSERT INTO accountlines
2760 (borrowernumber, itemnumber, accountno,
2761 date, amount, description, accounttype,
2762 amountoutstanding, manager_id)
2763 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?,?)
2765 my $sth = $dbh->prepare($query);
2766 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge, $manager_id );
2772 GetTransfers($itemnumber);
2777 my ($itemnumber) = @_;
2779 my $dbh = C4::Context->dbh;
2785 FROM branchtransfers
2786 WHERE itemnumber = ?
2787 AND datearrived IS NULL
2789 my $sth = $dbh->prepare($query);
2790 $sth->execute($itemnumber);
2791 my @row = $sth->fetchrow_array();
2796 =head2 GetTransfersFromTo
2798 @results = GetTransfersFromTo($frombranch,$tobranch);
2800 Returns the list of pending transfers between $from and $to branch
2804 sub GetTransfersFromTo {
2805 my ( $frombranch, $tobranch ) = @_;
2806 return unless ( $frombranch && $tobranch );
2807 my $dbh = C4::Context->dbh;
2809 SELECT itemnumber,datesent,frombranch
2810 FROM branchtransfers
2813 AND datearrived IS NULL
2815 my $sth = $dbh->prepare($query);
2816 $sth->execute( $frombranch, $tobranch );
2819 while ( my $data = $sth->fetchrow_hashref ) {
2820 push @gettransfers, $data;
2823 return (@gettransfers);
2826 =head2 DeleteTransfer
2828 &DeleteTransfer($itemnumber);
2832 sub DeleteTransfer {
2833 my ($itemnumber) = @_;
2834 my $dbh = C4::Context->dbh;
2835 my $sth = $dbh->prepare(
2836 "DELETE FROM branchtransfers
2838 AND datearrived IS NULL "
2840 $sth->execute($itemnumber);
2844 =head2 AnonymiseIssueHistory
2846 $rows = AnonymiseIssueHistory($date,$borrowernumber)
2848 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
2849 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
2851 If c<$borrowernumber> is set, it will delete issue history for only that borrower, regardless of their opac privacy
2852 setting (force delete).
2854 return the number of affected rows.
2858 sub AnonymiseIssueHistory {
2860 my $borrowernumber = shift;
2861 my $dbh = C4::Context->dbh;
2864 SET borrowernumber = ?
2865 WHERE returndate < ?
2866 AND borrowernumber IS NOT NULL
2869 # The default of 0 does not work due to foreign key constraints
2870 # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
2871 my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
2872 my @bind_params = ($anonymouspatron, $date);
2873 if (defined $borrowernumber) {
2874 $query .= " AND borrowernumber = ?";
2875 push @bind_params, $borrowernumber;
2877 $query .= " AND (SELECT privacy FROM borrowers WHERE borrowers.borrowernumber=old_issues.borrowernumber) <> 0";
2879 my $sth = $dbh->prepare($query);
2880 $sth->execute(@bind_params);
2881 my $rows_affected = $sth->rows; ### doublecheck row count return function
2882 return $rows_affected;
2885 =head2 SendCirculationAlert
2887 Send out a C<check-in> or C<checkout> alert using the messaging system.
2895 Valid values for this parameter are: C<CHECKIN> and C<CHECKOUT>.
2899 Hashref of information about the item being checked in or out.
2903 Hashref of information about the borrower of the item.
2907 The branchcode from where the checkout or check-in took place.
2913 SendCirculationAlert({
2916 borrower => $borrower,
2922 sub SendCirculationAlert {
2924 my ($type, $item, $borrower, $branch) =
2925 ($opts->{type}, $opts->{item}, $opts->{borrower}, $opts->{branch});
2926 my %message_name = (
2927 CHECKIN => 'Item_Check_in',
2928 CHECKOUT => 'Item_Checkout',
2929 RENEWAL => 'Item_Checkout',
2931 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences({
2932 borrowernumber => $borrower->{borrowernumber},
2933 message_name => $message_name{$type},
2935 my $issues_table = ( $type eq 'CHECKOUT' || $type eq 'RENEWAL' ) ? 'issues' : 'old_issues';
2936 my $letter = C4::Letters::GetPreparedLetter (
2937 module => 'circulation',
2938 letter_code => $type,
2939 branchcode => $branch,
2941 $issues_table => $item->{itemnumber},
2942 'items' => $item->{itemnumber},
2943 'biblio' => $item->{biblionumber},
2944 'biblioitems' => $item->{biblionumber},
2945 'borrowers' => $borrower,
2946 'branches' => $branch,
2950 my @transports = keys %{ $borrower_preferences->{transports} };
2951 # warn "no transports" unless @transports;
2953 # warn "transport: $_";
2954 my $message = C4::Message->find_last_message($borrower, $type, $_);
2956 #warn "create new message";
2957 C4::Message->enqueue($letter, $borrower, $_);
2959 #warn "append to old message";
2960 $message->append($letter);
2968 =head2 updateWrongTransfer
2970 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
2972 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
2976 sub updateWrongTransfer {
2977 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
2978 my $dbh = C4::Context->dbh;
2979 # first step validate the actual line of transfert .
2982 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
2984 $sth->execute($FromLibrary,$itemNumber);
2987 # second step create a new line of branchtransfer to the right location .
2988 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
2990 #third step changing holdingbranch of item
2991 UpdateHoldingbranch($FromLibrary,$itemNumber);
2994 =head2 UpdateHoldingbranch
2996 $items = UpdateHoldingbranch($branch,$itmenumber);
2998 Simple methode for updating hodlingbranch in items BDD line
3002 sub UpdateHoldingbranch {
3003 my ( $branch,$itemnumber ) = @_;
3004 ModItem({ holdingbranch => $branch }, undef, $itemnumber);
3009 $newdatedue = CalcDateDue($startdate,$itemtype,$branchcode,$borrower);
3011 this function calculates the due date given the start date and configured circulation rules,
3012 checking against the holidays calendar as per the 'useDaysMode' syspref.
3013 C<$startdate> = C4::Dates object representing start date of loan period (assumed to be today)
3014 C<$itemtype> = itemtype code of item in question
3015 C<$branch> = location whose calendar to use
3016 C<$borrower> = Borrower object
3021 my ( $startdate, $itemtype, $branch, $borrower ) = @_;
3023 # loanlength now a href
3025 GetLoanLength( $borrower->{'categorycode'}, $itemtype, $branch );
3029 # if globalDueDate ON the datedue is set to that date
3030 if (C4::Context->preference('globalDueDate')
3031 && ( C4::Context->preference('globalDueDate') =~
3032 C4::Dates->regexp('syspref') )
3034 $datedue = dt_from_string(
3035 C4::Context->preference('globalDueDate'),
3036 C4::Context->preference('dateformat')
3040 # otherwise, calculate the datedue as normal
3041 if ( C4::Context->preference('useDaysMode') eq 'Days' )
3042 { # ignoring calendar
3044 DateTime->now( time_zone => C4::Context->tz() )
3045 ->truncate( to => 'minute' );
3046 if ( $loanlength->{lengthunit} eq 'hours' ) {
3047 $dt->add( hours => $loanlength->{issuelength} );
3049 $dt->add( days => $loanlength->{issuelength} );
3051 $dt->set_minute(59);
3058 if ($loanlength->{lengthunit} eq 'hours') {
3059 $dur = DateTime::Duration->new( hours => $loanlength->{issuelength});
3062 $dur = DateTime::Duration->new( days => $loanlength->{issuelength});
3064 if (ref $startdate ne 'DateTime' ) {
3065 $startdate = dt_from_string($startdate);
3067 my $calendar = Koha::Calendar->new( branchcode => $branch );
3068 $datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} );
3069 if ($loanlength->{lengthunit} eq 'days') {
3070 $datedue->set_hour(23);
3071 $datedue->set_minute(59);
3076 # if Hard Due Dates are used, retreive them and apply as necessary
3077 my ( $hardduedate, $hardduedatecompare ) =
3078 GetHardDueDate( $borrower->{'categorycode'}, $itemtype, $branch );
3079 if ($hardduedate) { # hardduedates are currently dates
3080 $hardduedate->truncate( to => 'minute' );
3081 $hardduedate->set_hour(23);
3082 $hardduedate->set_minute(59);
3083 my $cmp = DateTime->compare( $hardduedate, $datedue );
3085 # if the calculated due date is after the 'before' Hard Due Date (ceiling), override
3086 # if the calculated date is before the 'after' Hard Due Date (floor), override
3087 # if the hard due date is set to 'exactly', overrride
3088 if ( $hardduedatecompare == 0 || $hardduedatecompare == $cmp ) {
3089 $datedue = $hardduedate->clone;
3092 # in all other cases, keep the date due as it is
3095 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
3096 if ( C4::Context->preference('ReturnBeforeExpiry') ) {
3097 my $expiry_dt = dt_from_string( $borrower->{dateexpiry}, 'iso' );
3098 $expiry_dt->set( hour => 23, minute => 59);
3099 if ( DateTime->compare( $datedue, $expiry_dt ) == 1 ) {
3100 $datedue = $expiry_dt->clone;
3108 =head2 CheckRepeatableHolidays
3110 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
3112 This function checks if the date due is a repeatable holiday
3114 C<$date_due> = returndate calculate with no day check
3115 C<$itemnumber> = itemnumber
3116 C<$branchcode> = localisation of issue
3120 sub CheckRepeatableHolidays{
3121 my($itemnumber,$week_day,$branchcode)=@_;
3122 my $dbh = C4::Context->dbh;
3123 my $query = qq|SELECT count(*)
3124 FROM repeatable_holidays
3127 my $sth = $dbh->prepare($query);
3128 $sth->execute($branchcode,$week_day);
3129 my $result=$sth->fetchrow;
3135 =head2 CheckSpecialHolidays
3137 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
3139 This function check if the date is a special holiday
3141 C<$years> = the years of datedue
3142 C<$month> = the month of datedue
3143 C<$day> = the day of datedue
3144 C<$itemnumber> = itemnumber
3145 C<$branchcode> = localisation of issue
3149 sub CheckSpecialHolidays{
3150 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
3151 my $dbh = C4::Context->dbh;
3152 my $query=qq|SELECT count(*)
3153 FROM `special_holidays`
3159 my $sth = $dbh->prepare($query);
3160 $sth->execute($years,$month,$day,$branchcode);
3161 my $countspecial=$sth->fetchrow ;
3163 return $countspecial;
3166 =head2 CheckRepeatableSpecialHolidays
3168 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
3170 This function check if the date is a repeatble special holidays
3172 C<$month> = the month of datedue
3173 C<$day> = the day of datedue
3174 C<$itemnumber> = itemnumber
3175 C<$branchcode> = localisation of issue
3179 sub CheckRepeatableSpecialHolidays{
3180 my ($month,$day,$itemnumber,$branchcode) = @_;
3181 my $dbh = C4::Context->dbh;
3182 my $query=qq|SELECT count(*)
3183 FROM `repeatable_holidays`
3188 my $sth = $dbh->prepare($query);
3189 $sth->execute($month,$day,$branchcode);
3190 my $countspecial=$sth->fetchrow ;
3192 return $countspecial;
3197 sub CheckValidBarcode{
3199 my $dbh = C4::Context->dbh;
3200 my $query=qq|SELECT count(*)
3204 my $sth = $dbh->prepare($query);
3205 $sth->execute($barcode);
3206 my $exist=$sth->fetchrow ;
3211 =head2 IsBranchTransferAllowed
3213 $allowed = IsBranchTransferAllowed( $toBranch, $fromBranch, $code );
3215 Code is either an itemtype or collection doe depending on the pref BranchTransferLimitsType
3219 sub IsBranchTransferAllowed {
3220 my ( $toBranch, $fromBranch, $code ) = @_;
3222 if ( $toBranch eq $fromBranch ) { return 1; } ## Short circuit for speed.
3224 my $limitType = C4::Context->preference("BranchTransferLimitsType");
3225 my $dbh = C4::Context->dbh;
3227 my $sth = $dbh->prepare("SELECT * FROM branch_transfer_limits WHERE toBranch = ? AND fromBranch = ? AND $limitType = ?");
3228 $sth->execute( $toBranch, $fromBranch, $code );
3229 my $limit = $sth->fetchrow_hashref();
3231 ## If a row is found, then that combination is not allowed, if no matching row is found, then the combination *is allowed*
3232 if ( $limit->{'limitId'} ) {
3239 =head2 CreateBranchTransferLimit
3241 CreateBranchTransferLimit( $toBranch, $fromBranch, $code );
3243 $code is either itemtype or collection code depending on what the pref BranchTransferLimitsType is set to.
3247 sub CreateBranchTransferLimit {
3248 my ( $toBranch, $fromBranch, $code ) = @_;
3250 my $limitType = C4::Context->preference("BranchTransferLimitsType");
3252 my $dbh = C4::Context->dbh;
3254 my $sth = $dbh->prepare("INSERT INTO branch_transfer_limits ( $limitType, toBranch, fromBranch ) VALUES ( ?, ?, ? )");
3255 $sth->execute( $code, $toBranch, $fromBranch );
3258 =head2 DeleteBranchTransferLimits
3260 DeleteBranchTransferLimits($frombranch);
3262 Deletes all the branch transfer limits for one branch
3266 sub DeleteBranchTransferLimits {
3268 my $dbh = C4::Context->dbh;
3269 my $sth = $dbh->prepare("DELETE FROM branch_transfer_limits WHERE fromBranch = ?");
3270 $sth->execute($branch);
3274 my ( $borrowernumber, $itemnum ) = @_;
3276 MarkIssueReturned( $borrowernumber, $itemnum );
3277 my $borrower = C4::Members::GetMember( 'borrowernumber'=>$borrowernumber );
3278 my $item = C4::Items::GetItem( $itemnum );
3279 my $old_note = ($item->{'paidfor'} && ($item->{'paidfor'} ne q{})) ? $item->{'paidfor'}.' / ' : q{};
3280 my @datearr = localtime(time);
3281 my $date = ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
3282 my $bor = "$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
3283 ModItem({ paidfor => $old_note."Paid for by $bor $date" }, undef, $itemnum);
3288 my ($itemnumber, $mark_returned, $charge_fee) = @_;
3290 my $dbh = C4::Context->dbh();
3291 my $sth=$dbh->prepare("SELECT issues.*,items.*,biblio.title
3293 JOIN items USING (itemnumber)
3294 JOIN biblio USING (biblionumber)
3295 WHERE issues.itemnumber=?");
3296 $sth->execute($itemnumber);
3297 my $issues=$sth->fetchrow_hashref();
3300 # if a borrower lost the item, add a replacement cost to the their record
3301 if ( my $borrowernumber = $issues->{borrowernumber} ){
3302 my $borrower = C4::Members::GetMemberDetails( $borrowernumber );
3304 C4::Accounts::chargelostitem($borrowernumber, $itemnumber, $issues->{'replacementprice'}, "Lost Item $issues->{'title'} $issues->{'barcode'}")
3306 #FIXME : Should probably have a way to distinguish this from an item that really was returned.
3307 #warn " $issues->{'borrowernumber'} / $itemnumber ";
3308 MarkIssueReturned($borrowernumber,$itemnumber,undef,undef,$borrower->{'privacy'}) if $mark_returned;
3312 sub GetOfflineOperations {
3313 my $dbh = C4::Context->dbh;
3314 my $sth = $dbh->prepare("SELECT * FROM pending_offline_operations WHERE branchcode=? ORDER BY timestamp");
3315 $sth->execute(C4::Context->userenv->{'branch'});
3316 my $results = $sth->fetchall_arrayref({});
3321 sub GetOfflineOperation {
3322 my $dbh = C4::Context->dbh;
3323 my $sth = $dbh->prepare("SELECT * FROM pending_offline_operations WHERE operationid=?");
3324 $sth->execute( shift );
3325 my $result = $sth->fetchrow_hashref;
3330 sub AddOfflineOperation {
3331 my $dbh = C4::Context->dbh;
3332 my $sth = $dbh->prepare("INSERT INTO pending_offline_operations (userid, branchcode, timestamp, action, barcode, cardnumber) VALUES(?,?,?,?,?,?)");
3333 $sth->execute( @_ );
3337 sub DeleteOfflineOperation {
3338 my $dbh = C4::Context->dbh;
3339 my $sth = $dbh->prepare("DELETE FROM pending_offline_operations WHERE operationid=?");
3340 $sth->execute( shift );
3344 sub ProcessOfflineOperation {
3345 my $operation = shift;
3348 if ( $operation->{action} eq 'return' ) {
3349 $report = ProcessOfflineReturn( $operation );
3350 } elsif ( $operation->{action} eq 'issue' ) {
3351 $report = ProcessOfflineIssue( $operation );
3354 DeleteOfflineOperation( $operation->{operationid} ) if $operation->{operationid};
3359 sub ProcessOfflineReturn {
3360 my $operation = shift;
3362 my $itemnumber = C4::Items::GetItemnumberFromBarcode( $operation->{barcode} );
3364 if ( $itemnumber ) {
3365 my $issue = GetOpenIssue( $itemnumber );
3368 $issue->{borrowernumber},
3371 $operation->{timestamp},
3374 { renewals => 0, onloan => undef },
3375 $issue->{'biblionumber'},
3380 return "Item not issued.";
3383 return "Item not found.";
3387 sub ProcessOfflineIssue {
3388 my $operation = shift;
3390 my $borrower = C4::Members::GetMemberDetails( undef, $operation->{cardnumber} ); # Get borrower from operation cardnumber
3392 if ( $borrower->{borrowernumber} ) {
3393 my $itemnumber = C4::Items::GetItemnumberFromBarcode( $operation->{barcode} );
3394 unless ($itemnumber) {
3395 return "Barcode not found.";
3397 my $issue = GetOpenIssue( $itemnumber );
3399 if ( $issue and ( $issue->{borrowernumber} ne $borrower->{borrowernumber} ) ) { # Item already issued to another borrower, mark it returned
3401 $issue->{borrowernumber},
3404 $operation->{timestamp},
3409 $operation->{'barcode'},
3412 $operation->{timestamp},
3417 return "Borrower not found.";
3425 TransferSlip($user_branch, $itemnumber, $to_branch)
3427 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
3432 my ($branch, $itemnumber, $to_branch) = @_;
3434 my $item = GetItem( $itemnumber )
3437 my $pulldate = C4::Dates->new();
3439 return C4::Letters::GetPreparedLetter (
3440 module => 'circulation',
3441 letter_code => 'TRANSFERSLIP',
3442 branchcode => $branch,
3444 'branches' => $to_branch,
3445 'biblio' => $item->{biblionumber},
3451 =head2 CheckIfIssuedToPatron
3453 CheckIfIssuedToPatron($borrowernumber, $biblionumber)
3455 Return 1 if any record item is issued to patron, otherwise return 0
3459 sub CheckIfIssuedToPatron {
3460 my ($borrowernumber, $biblionumber) = @_;
3462 my $items = GetItemsByBiblioitemnumber($biblionumber);
3464 foreach my $item (@{$items}) {
3465 return 1 if ($item->{borrowernumber} && $item->{borrowernumber} eq $borrowernumber);
3478 Koha Development Team <http://koha-community.org/>