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);
46 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
50 $VERSION = 3.08.01.002; # for version checking
53 # FIXME subs that should probably be elsewhere
60 # subs to deal with issuing a book
71 &GetBranchBorrowerCircRule
75 &AnonymiseIssueHistory
78 # subs to deal with returns
84 # subs to deal with transfers
91 &IsBranchTransferAllowed
92 &CreateBranchTransferLimit
93 &DeleteBranchTransferLimits
97 # subs to deal with offline circulation
102 &DeleteOfflineOperation
103 &ProcessOfflineOperation
109 C4::Circulation - Koha circulation module
117 The functions in this module deal with circulation, issues, and
118 returns, as well as general information about the library.
119 Also deals with stocktaking.
125 $str = &barcodedecode($barcode, [$filter]);
127 Generic filter function for barcode string.
128 Called on every circ if the System Pref itemBarcodeInputFilter is set.
129 Will do some manipulation of the barcode for systems that deliver a barcode
130 to circulation.pl that differs from the barcode stored for the item.
131 For proper functioning of this filter, calling the function on the
132 correct barcode string (items.barcode) should return an unaltered barcode.
134 The optional $filter argument is to allow for testing or explicit
135 behavior that ignores the System Pref. Valid values are the same as the
140 # FIXME -- the &decode fcn below should be wrapped into this one.
141 # FIXME -- these plugins should be moved out of Circulation.pm
144 my ($barcode, $filter) = @_;
145 my $branch = C4::Branch::mybranch();
146 $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter;
147 $filter or return $barcode; # ensure filter is defined, else return untouched barcode
148 if ($filter eq 'whitespace') {
150 } elsif ($filter eq 'cuecat') {
152 my @fields = split( /\./, $barcode );
153 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
154 ($#results == 2) and return $results[2];
155 } elsif ($filter eq 'T-prefix') {
156 if ($barcode =~ /^[Tt](\d)/) {
157 (defined($1) and $1 eq '0') and return $barcode;
158 $barcode = substr($barcode, 2) + 0; # FIXME: probably should be substr($barcode, 1)
160 return sprintf("T%07d", $barcode);
161 # FIXME: $barcode could be "T1", causing warning: substr outside of string
162 # Why drop the nonzero digit after the T?
163 # Why pass non-digits (or empty string) to "T%07d"?
164 } elsif ($filter eq 'libsuite8') {
165 unless($barcode =~ m/^($branch)-/i){ #if barcode starts with branch code its in Koha style. Skip it.
166 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
167 $barcode =~ s/^[0]*(\d+)$/$branch-b-$1/i;
169 $barcode =~ s/^(\D+)[0]*(\d+)$/$branch-$1-$2/i;
173 return $barcode; # return barcode, modified or not
178 $str = &decode($chunk);
180 Decodes a segment of a string emitted by a CueCat barcode scanner and
183 FIXME: Should be replaced with Barcode::Cuecat from CPAN
184 or Javascript based decoding on the client side.
191 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
192 my @s = map { index( $seq, $_ ); } split( //, $encoded );
193 my $l = ( $#s + 1 ) % 4;
196 # warn "Error: Cuecat decode parsing failed!";
204 my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
206 chr( ( $n >> 16 ) ^ 67 )
207 .chr( ( $n >> 8 & 255 ) ^ 67 )
208 .chr( ( $n & 255 ) ^ 67 );
211 $r = substr( $r, 0, length($r) - $l );
217 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch,
218 $barcode, $ignore_reserves);
220 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
222 C<$newbranch> is the code for the branch to which the item should be transferred.
224 C<$barcode> is the barcode of the item to be transferred.
226 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
227 Otherwise, if an item is reserved, the transfer fails.
229 Returns three values:
235 is true if the transfer was successful.
239 is a reference-to-hash which may have any of the following keys:
245 There is no item in the catalog with the given barcode. The value is C<$barcode>.
249 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.
251 =item C<DestinationEqualsHolding>
253 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.
257 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.
261 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>.
263 =item C<WasTransferred>
265 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
274 my ( $tbr, $barcode, $ignoreRs ) = @_;
277 my $branches = GetBranches();
278 my $itemnumber = GetItemnumberFromBarcode( $barcode );
279 my $issue = GetItemIssue($itemnumber);
280 my $biblio = GetBiblioFromItemNumber($itemnumber);
283 if ( not $itemnumber ) {
284 $messages->{'BadBarcode'} = $barcode;
288 # get branches of book...
289 my $hbr = $biblio->{'homebranch'};
290 my $fbr = $biblio->{'holdingbranch'};
292 # if using Branch Transfer Limits
293 if ( C4::Context->preference("UseBranchTransferLimits") == 1 ) {
294 if ( C4::Context->preference("item-level_itypes") && C4::Context->preference("BranchTransferLimitsType") eq 'itemtype' ) {
295 if ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{'itype'} ) ) {
296 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{'itype'};
299 } elsif ( ! IsBranchTransferAllowed( $tbr, $fbr, $biblio->{ C4::Context->preference("BranchTransferLimitsType") } ) ) {
300 $messages->{'NotAllowed'} = $tbr . "::" . $biblio->{ C4::Context->preference("BranchTransferLimitsType") };
306 if ( $hbr && $branches->{$hbr}->{'PE'} ) {
307 $messages->{'IsPermanent'} = $hbr;
311 # can't transfer book if is already there....
312 if ( $fbr eq $tbr ) {
313 $messages->{'DestinationEqualsHolding'} = 1;
317 # check if it is still issued to someone, return it...
318 if ($issue->{borrowernumber}) {
319 AddReturn( $barcode, $fbr );
320 $messages->{'WasReturned'} = $issue->{borrowernumber};
324 # That'll save a database query.
325 my ( $resfound, $resrec, undef ) =
326 CheckReserves( $itemnumber );
327 if ( $resfound and not $ignoreRs ) {
328 $resrec->{'ResFound'} = $resfound;
330 # $messages->{'ResFound'} = $resrec;
334 #actually do the transfer....
336 ModItemTransfer( $itemnumber, $fbr, $tbr );
338 # don't need to update MARC anymore, we do it in batch now
339 $messages->{'WasTransfered'} = 1;
342 ModDateLastSeen( $itemnumber );
343 return ( $dotransfer, $messages, $biblio );
348 my $borrower = shift;
349 my $biblionumber = shift;
351 my $cat_borrower = $borrower->{'categorycode'};
352 my $dbh = C4::Context->dbh;
354 # Get which branchcode we need
355 $branch = _GetCircControlBranch($item,$borrower);
356 my $type = (C4::Context->preference('item-level_itypes'))
357 ? $item->{'itype'} # item-level
358 : $item->{'itemtype'}; # biblio-level
360 # given branch, patron category, and item type, determine
361 # applicable issuing rule
362 my $issuing_rule = GetIssuingRule($cat_borrower, $type, $branch);
364 # if a rule is found and has a loan limit set, count
365 # how many loans the patron already has that meet that
367 if (defined($issuing_rule) and defined($issuing_rule->{'maxissueqty'})) {
369 my $count_query = "SELECT COUNT(*) FROM issues
370 JOIN items USING (itemnumber) ";
372 my $rule_itemtype = $issuing_rule->{itemtype};
373 if ($rule_itemtype eq "*") {
374 # matching rule has the default item type, so count only
375 # those existing loans that don't fall under a more
377 if (C4::Context->preference('item-level_itypes')) {
378 $count_query .= " WHERE items.itype NOT IN (
379 SELECT itemtype FROM issuingrules
381 AND (categorycode = ? OR categorycode = ?)
385 $count_query .= " JOIN biblioitems USING (biblionumber)
386 WHERE biblioitems.itemtype NOT IN (
387 SELECT itemtype FROM issuingrules
389 AND (categorycode = ? OR categorycode = ?)
393 push @bind_params, $issuing_rule->{branchcode};
394 push @bind_params, $issuing_rule->{categorycode};
395 push @bind_params, $cat_borrower;
397 # rule has specific item type, so count loans of that
399 if (C4::Context->preference('item-level_itypes')) {
400 $count_query .= " WHERE items.itype = ? ";
402 $count_query .= " JOIN biblioitems USING (biblionumber)
403 WHERE biblioitems.itemtype= ? ";
405 push @bind_params, $type;
408 $count_query .= " AND borrowernumber = ? ";
409 push @bind_params, $borrower->{'borrowernumber'};
410 my $rule_branch = $issuing_rule->{branchcode};
411 if ($rule_branch ne "*") {
412 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
413 $count_query .= " AND issues.branchcode = ? ";
414 push @bind_params, $branch;
415 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
416 ; # if branch is the patron's home branch, then count all loans by patron
418 $count_query .= " AND items.homebranch = ? ";
419 push @bind_params, $branch;
423 my $count_sth = $dbh->prepare($count_query);
424 $count_sth->execute(@bind_params);
425 my ($current_loan_count) = $count_sth->fetchrow_array;
427 my $max_loans_allowed = $issuing_rule->{'maxissueqty'};
428 if ($current_loan_count >= $max_loans_allowed) {
429 return ($current_loan_count, $max_loans_allowed);
433 # Now count total loans against the limit for the branch
434 my $branch_borrower_circ_rule = GetBranchBorrowerCircRule($branch, $cat_borrower);
435 if (defined($branch_borrower_circ_rule->{maxissueqty})) {
436 my @bind_params = ();
437 my $branch_count_query = "SELECT COUNT(*) FROM issues
438 JOIN items USING (itemnumber)
439 WHERE borrowernumber = ? ";
440 push @bind_params, $borrower->{borrowernumber};
442 if (C4::Context->preference('CircControl') eq 'PickupLibrary') {
443 $branch_count_query .= " AND issues.branchcode = ? ";
444 push @bind_params, $branch;
445 } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
446 ; # if branch is the patron's home branch, then count all loans by patron
448 $branch_count_query .= " AND items.homebranch = ? ";
449 push @bind_params, $branch;
451 my $branch_count_sth = $dbh->prepare($branch_count_query);
452 $branch_count_sth->execute(@bind_params);
453 my ($current_loan_count) = $branch_count_sth->fetchrow_array;
455 my $max_loans_allowed = $branch_borrower_circ_rule->{maxissueqty};
456 if ($current_loan_count >= $max_loans_allowed) {
457 return ($current_loan_count, $max_loans_allowed);
461 # OK, the patron can issue !!!
467 @issues = &itemissues($biblioitemnumber, $biblio);
469 Looks up information about who has borrowed the bookZ<>(s) with the
470 given biblioitemnumber.
472 C<$biblio> is ignored.
474 C<&itemissues> returns an array of references-to-hash. The keys
475 include the fields from the C<items> table in the Koha database.
476 Additional keys include:
482 If the item is currently on loan, this gives the due date.
484 If the item is not on loan, then this is either "Available" or
485 "Cancelled", if the item has been withdrawn.
489 If the item is currently on loan, this gives the card number of the
490 patron who currently has the item.
492 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
494 These give the timestamp for the last three times the item was
497 =item C<card0>, C<card1>, C<card2>
499 The card number of the last three patrons who borrowed this item.
501 =item C<borrower0>, C<borrower1>, C<borrower2>
503 The borrower number of the last three patrons who borrowed this item.
511 my ( $bibitem, $biblio ) = @_;
512 my $dbh = C4::Context->dbh;
514 $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
519 $sth->execute($bibitem) || die $sth->errstr;
521 while ( my $data = $sth->fetchrow_hashref ) {
523 # Find out who currently has this item.
524 # FIXME - Wouldn't it be better to do this as a left join of
525 # some sort? Currently, this code assumes that if
526 # fetchrow_hashref() fails, then the book is on the shelf.
527 # fetchrow_hashref() can fail for any number of reasons (e.g.,
528 # database server crash), not just because no items match the
530 my $sth2 = $dbh->prepare(
531 "SELECT * FROM issues
532 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
537 $sth2->execute( $data->{'itemnumber'} );
538 if ( my $data2 = $sth2->fetchrow_hashref ) {
539 $data->{'date_due'} = $data2->{'date_due'};
540 $data->{'card'} = $data2->{'cardnumber'};
541 $data->{'borrower'} = $data2->{'borrowernumber'};
544 $data->{'date_due'} = ($data->{'wthdrawn'} eq '1') ? 'Cancelled' : 'Available';
548 # Find the last 3 people who borrowed this item.
549 $sth2 = $dbh->prepare(
550 "SELECT * FROM old_issues
551 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
553 ORDER BY returndate DESC,timestamp DESC"
556 $sth2->execute( $data->{'itemnumber'} );
557 for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
558 { # FIXME : error if there is less than 3 pple borrowing this item
559 if ( my $data2 = $sth2->fetchrow_hashref ) {
560 $data->{"timestamp$i2"} = $data2->{'timestamp'};
561 $data->{"card$i2"} = $data2->{'cardnumber'};
562 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
566 $results[$i] = $data;
573 =head2 CanBookBeIssued
575 ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $borrower,
576 $barcode, $duedatespec, $inprocess, $ignore_reserves );
578 Check if a book can be issued.
580 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
584 =item C<$borrower> hash with borrower informations (from GetMember or GetMemberDetails)
586 =item C<$barcode> is the bar code of the book being issued.
588 =item C<$duedatespec> is a C4::Dates object.
590 =item C<$inprocess> boolean switch
591 =item C<$ignore_reserves> boolean switch
599 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
600 Possible values are :
606 sticky due date is invalid
610 borrower gone with no address
614 borrower declared it's card lost
620 =head3 UNKNOWN_BARCODE
634 item is restricted (set by ??)
636 C<$needsconfirmation> a reference to a hash. It contains reasons why the loan
637 could be prevented, but ones that can be overriden by the operator.
639 Possible values are :
647 renewing, not issuing
649 =head3 ISSUED_TO_ANOTHER
651 issued to someone else.
655 reserved for someone else.
659 sticky due date is invalid or due date in the past
663 if the borrower borrows to much things
667 sub CanBookBeIssued {
668 my ( $borrower, $barcode, $duedate, $inprocess, $ignore_reserves ) = @_;
669 my %needsconfirmation; # filled with problems that needs confirmations
670 my %issuingimpossible; # filled with problems that causes the issue to be IMPOSSIBLE
671 my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
672 my $issue = GetItemIssue($item->{itemnumber});
673 my $biblioitem = GetBiblioItemData($item->{biblioitemnumber});
674 $item->{'itemtype'}=$item->{'itype'};
675 my $dbh = C4::Context->dbh;
677 # MANDATORY CHECKS - unless item exists, nothing else matters
678 unless ( $item->{barcode} ) {
679 $issuingimpossible{UNKNOWN_BARCODE} = 1;
681 return ( \%issuingimpossible, \%needsconfirmation ) if %issuingimpossible;
684 # DUE DATE is OK ? -- should already have checked.
686 if ($duedate && ref $duedate ne 'DateTime') {
687 $duedate = dt_from_string($duedate);
689 my $now = DateTime->now( time_zone => C4::Context->tz() );
690 unless ( $duedate ) {
691 my $issuedate = $now->clone();
693 my $branch = _GetCircControlBranch($item,$borrower);
694 my $itype = ( C4::Context->preference('item-level_itypes') ) ? $item->{'itype'} : $biblioitem->{'itemtype'};
695 $duedate = CalcDateDue( $issuedate, $itype, $branch, $borrower );
697 # Offline circ calls AddIssue directly, doesn't run through here
698 # So issuingimpossible should be ok.
701 my $today = $now->clone();
702 $today->truncate( to => 'minute');
703 if (DateTime->compare($duedate,$today) == -1 ) { # duedate cannot be before now
704 $needsconfirmation{INVALID_DATE} = output_pref($duedate);
707 $issuingimpossible{INVALID_DATE} = output_pref($duedate);
713 if ( $borrower->{'category_type'} eq 'X' && ( $item->{barcode} )) {
714 # stats only borrower -- add entry to statistics table, and return issuingimpossible{STATS} = 1 .
715 &UpdateStats(C4::Context->userenv->{'branch'},'localuse','','',$item->{'itemnumber'},$item->{'itemtype'},$borrower->{'borrowernumber'});
716 ModDateLastSeen( $item->{'itemnumber'} );
717 return( { STATS => 1 }, {});
719 if ( $borrower->{flags}->{GNA} ) {
720 $issuingimpossible{GNA} = 1;
722 if ( $borrower->{flags}->{'LOST'} ) {
723 $issuingimpossible{CARD_LOST} = 1;
725 if ( $borrower->{flags}->{'DBARRED'} ) {
726 $issuingimpossible{DEBARRED} = 1;
728 if ( !defined $borrower->{dateexpiry} || $borrower->{'dateexpiry'} eq '0000-00-00') {
729 $issuingimpossible{EXPIRED} = 1;
731 my ($y, $m, $d) = split /-/,$borrower->{'dateexpiry'};
732 if ($y && $m && $d) { # are we really writing oinvalid dates to borrs
733 my $expiry_dt = DateTime->new(
737 time_zone => C4::Context->tz,
739 $expiry_dt->truncate( to => 'day');
740 my $today = $now->clone()->truncate(to => 'day');
741 if (DateTime->compare($today, $expiry_dt) == 1) {
742 $issuingimpossible{EXPIRED} = 1;
745 carp("Invalid expity date in borr");
746 $issuingimpossible{EXPIRED} = 1;
755 C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->ymd() );
756 my $amountlimit = C4::Context->preference("noissuescharge");
757 my $allowfineoverride = C4::Context->preference("AllowFineOverride");
758 my $allfinesneedoverride = C4::Context->preference("AllFinesNeedOverride");
759 if ( C4::Context->preference("IssuingInProcess") ) {
760 if ( $amount > $amountlimit && !$inprocess && !$allowfineoverride) {
761 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
762 } elsif ( $amount > $amountlimit && !$inprocess && $allowfineoverride) {
763 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
764 } elsif ( $allfinesneedoverride && $amount > 0 && $amount <= $amountlimit && !$inprocess ) {
765 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
769 if ( $amount > $amountlimit && $allowfineoverride ) {
770 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
771 } elsif ( $amount > $amountlimit && !$allowfineoverride) {
772 $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
773 } elsif ( $amount > 0 && $allfinesneedoverride ) {
774 $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
778 my ($blocktype, $count) = C4::Members::IsMemberBlocked($borrower->{'borrowernumber'});
779 if ($blocktype == -1) {
780 ## patron has outstanding overdue loans
781 if ( C4::Context->preference("OverduesBlockCirc") eq 'block'){
782 $issuingimpossible{USERBLOCKEDOVERDUE} = $count;
784 elsif ( C4::Context->preference("OverduesBlockCirc") eq 'confirmation'){
785 $needsconfirmation{USERBLOCKEDOVERDUE} = $count;
787 } elsif($blocktype == 1) {
788 # patron has accrued fine days
789 $issuingimpossible{USERBLOCKEDREMAINING} = $count;
793 # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
795 my ($current_loan_count, $max_loans_allowed) = TooMany( $borrower, $item->{biblionumber}, $item );
796 # if TooMany max_loans_allowed returns 0 the user doesn't have permission to check out this book
797 if (defined $max_loans_allowed && $max_loans_allowed == 0) {
798 $needsconfirmation{PATRON_CANT} = 1;
800 if($max_loans_allowed){
801 $needsconfirmation{TOO_MANY} = 1;
802 $needsconfirmation{current_loan_count} = $current_loan_count;
803 $needsconfirmation{max_loans_allowed} = $max_loans_allowed;
810 if ( $item->{'notforloan'}
811 && $item->{'notforloan'} > 0 )
813 if(!C4::Context->preference("AllowNotForLoanOverride")){
814 $issuingimpossible{NOT_FOR_LOAN} = 1;
816 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
819 elsif ( !$item->{'notforloan'} ){
820 # we have to check itemtypes.notforloan also
821 if (C4::Context->preference('item-level_itypes')){
822 # this should probably be a subroutine
823 my $sth = $dbh->prepare("SELECT notforloan FROM itemtypes WHERE itemtype = ?");
824 $sth->execute($item->{'itemtype'});
825 my $notforloan=$sth->fetchrow_hashref();
827 if ($notforloan->{'notforloan'}) {
828 if (!C4::Context->preference("AllowNotForLoanOverride")) {
829 $issuingimpossible{NOT_FOR_LOAN} = 1;
831 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
835 elsif ($biblioitem->{'notforloan'} == 1){
836 if (!C4::Context->preference("AllowNotForLoanOverride")) {
837 $issuingimpossible{NOT_FOR_LOAN} = 1;
839 $needsconfirmation{NOT_FOR_LOAN_FORCING} = 1;
843 if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} > 0 )
845 $issuingimpossible{WTHDRAWN} = 1;
847 if ( $item->{'restricted'}
848 && $item->{'restricted'} == 1 )
850 $issuingimpossible{RESTRICTED} = 1;
852 if ( $item->{'itemlost'} ) {
853 $needsconfirmation{ITEM_LOST} = GetAuthorisedValueByCode( 'LOST', $item->{'itemlost'} );
855 if ( C4::Context->preference("IndependantBranches") ) {
856 my $userenv = C4::Context->userenv;
857 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
858 $issuingimpossible{ITEMNOTSAMEBRANCH} = 1
859 if ( $item->{C4::Context->preference("HomeOrHoldingBranch")} ne $userenv->{branch} );
860 $needsconfirmation{BORRNOTSAMEBRANCH} = GetBranchName( $borrower->{'branchcode'} )
861 if ( $borrower->{'branchcode'} ne $userenv->{branch} );
866 # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
868 if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
871 # Already issued to current borrower. Ask whether the loan should
873 my ($CanBookBeRenewed,$renewerror) = CanBookBeRenewed(
874 $borrower->{'borrowernumber'},
875 $item->{'itemnumber'}
877 if ( $CanBookBeRenewed == 0 ) { # no more renewals allowed
878 $issuingimpossible{NO_MORE_RENEWALS} = 1;
881 $needsconfirmation{RENEW_ISSUE} = 1;
884 elsif ($issue->{borrowernumber}) {
886 # issued to someone else
887 my $currborinfo = C4::Members::GetMember( borrowernumber => $issue->{borrowernumber} );
889 # warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
890 $needsconfirmation{ISSUED_TO_ANOTHER} = 1;
891 $needsconfirmation{issued_firstname} = $currborinfo->{'firstname'};
892 $needsconfirmation{issued_surname} = $currborinfo->{'surname'};
893 $needsconfirmation{issued_cardnumber} = $currborinfo->{'cardnumber'};
894 $needsconfirmation{issued_borrowernumber} = $currborinfo->{'borrowernumber'};
897 unless ( $ignore_reserves ) {
898 # See if the item is on reserve.
899 my ( $restype, $res ) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
901 my $resbor = $res->{'borrowernumber'};
902 if ( $resbor ne $borrower->{'borrowernumber'} ) {
903 my ( $resborrower ) = C4::Members::GetMember( borrowernumber => $resbor );
904 my $branchname = GetBranchName( $res->{'branchcode'} );
905 if ( $restype eq "Waiting" )
907 # The item is on reserve and waiting, but has been
908 # reserved by some other patron.
909 $needsconfirmation{RESERVE_WAITING} = 1;
910 $needsconfirmation{'resfirstname'} = $resborrower->{'firstname'};
911 $needsconfirmation{'ressurname'} = $resborrower->{'surname'};
912 $needsconfirmation{'rescardnumber'} = $resborrower->{'cardnumber'};
913 $needsconfirmation{'resborrowernumber'} = $resborrower->{'borrowernumber'};
914 $needsconfirmation{'resbranchname'} = $branchname;
915 $needsconfirmation{'reswaitingdate'} = format_date($res->{'waitingdate'});
917 elsif ( $restype eq "Reserved" ) {
918 # The item is on reserve for someone else.
919 $needsconfirmation{RESERVED} = 1;
920 $needsconfirmation{'resfirstname'} = $resborrower->{'firstname'};
921 $needsconfirmation{'ressurname'} = $resborrower->{'surname'};
922 $needsconfirmation{'rescardnumber'} = $resborrower->{'cardnumber'};
923 $needsconfirmation{'resborrowernumber'} = $resborrower->{'borrowernumber'};
924 $needsconfirmation{'resbranchname'} = $branchname;
925 $needsconfirmation{'resreservedate'} = format_date($res->{'reservedate'});
930 return ( \%issuingimpossible, \%needsconfirmation );
935 &AddIssue($borrower, $barcode, [$datedue], [$cancelreserve], [$issuedate])
937 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
941 =item C<$borrower> is a hash with borrower informations (from GetMember or GetMemberDetails).
943 =item C<$barcode> is the barcode of the item being issued.
945 =item C<$datedue> is a C4::Dates object for the max date of return, i.e. the date due (optional).
948 =item C<$cancelreserve> is 1 to override and cancel any pending reserves for the item (optional).
950 =item C<$issuedate> is the date to issue the item in iso (YYYY-MM-DD) format (optional).
951 Defaults to today. Unlike C<$datedue>, NOT a C4::Dates object, unfortunately.
953 AddIssue does the following things :
955 - step 01: check that there is a borrowernumber & a barcode provided
956 - check for RENEWAL (book issued & being issued to the same patron)
957 - renewal YES = Calculate Charge & renew
959 * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
961 - fill reserve if reserve to this patron
962 - cancel reserve or not, otherwise
963 * TRANSFERT PENDING ?
964 - complete the transfert
972 my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
973 my $dbh = C4::Context->dbh;
974 my $barcodecheck=CheckValidBarcode($barcode);
975 if ($datedue && ref $datedue ne 'DateTime') {
976 $datedue = dt_from_string($datedue);
978 # $issuedate defaults to today.
979 if ( ! defined $issuedate ) {
980 $issuedate = DateTime->now(time_zone => C4::Context->tz());
983 if ( ref $issuedate ne 'DateTime') {
984 $issuedate = dt_from_string($issuedate);
988 if ($borrower and $barcode and $barcodecheck ne '0'){#??? wtf
989 # find which item we issue
990 my $item = GetItem('', $barcode) or return undef; # if we don't get an Item, abort.
991 my $branch = _GetCircControlBranch($item,$borrower);
993 # get actual issuing if there is one
994 my $actualissue = GetItemIssue( $item->{itemnumber});
996 # get biblioinformation for this item
997 my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
1000 # check if we just renew the issue.
1002 if ($actualissue->{borrowernumber} eq $borrower->{'borrowernumber'}) {
1003 $datedue = AddRenewal(
1004 $borrower->{'borrowernumber'},
1005 $item->{'itemnumber'},
1008 $issuedate, # here interpreted as the renewal date
1012 # it's NOT a renewal
1013 if ( $actualissue->{borrowernumber}) {
1014 # This book is currently on loan, but not to the person
1015 # who wants to borrow it now. mark it returned before issuing to the new borrower
1018 C4::Context->userenv->{'branch'}
1022 MoveReserve( $item->{'itemnumber'}, $borrower->{'borrowernumber'}, $cancelreserve );
1023 # Starting process for transfer job (checking transfert and validate it if we have one)
1024 my ($datesent) = GetTransfers($item->{'itemnumber'});
1026 # updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for visibility of this case (maybe for stats ....)
1029 "UPDATE branchtransfers
1030 SET datearrived = now(),
1032 comments = 'Forced branchtransfer'
1033 WHERE itemnumber= ? AND datearrived IS NULL"
1035 $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
1038 # Record in the database the fact that the book was issued.
1042 (borrowernumber, itemnumber,issuedate, date_due, branchcode)
1046 my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'};
1047 $datedue = CalcDateDue( $issuedate, $itype, $branch, $borrower );
1050 $datedue->truncate( to => 'minute');
1052 $borrower->{'borrowernumber'}, # borrowernumber
1053 $item->{'itemnumber'}, # itemnumber
1054 $issuedate->strftime('%Y-%m-%d %H:%M:00'), # issuedate
1055 $datedue->strftime('%Y-%m-%d %H:%M:00'), # date_due
1056 C4::Context->userenv->{'branch'} # branchcode
1058 if ( C4::Context->preference('ReturnToShelvingCart') ) { ## ReturnToShelvingCart is on, anything issued should be taken off the cart.
1059 CartToShelf( $item->{'itemnumber'} );
1061 $item->{'issues'}++;
1063 ## If item was lost, it has now been found, reverse any list item charges if neccessary.
1064 if ( $item->{'itemlost'} ) {
1065 _FixAccountForLostAndReturned( $item->{'itemnumber'}, undef, $item->{'barcode'} );
1068 ModItem({ issues => $item->{'issues'},
1069 holdingbranch => C4::Context->userenv->{'branch'},
1071 datelastborrowed => DateTime->now(time_zone => C4::Context->tz())->ymd(),
1072 onloan => $datedue->ymd(),
1073 }, $item->{'biblionumber'}, $item->{'itemnumber'});
1074 ModDateLastSeen( $item->{'itemnumber'} );
1076 # If it costs to borrow this book, charge it to the patron's account.
1077 my ( $charge, $itemtype ) = GetIssuingCharges(
1078 $item->{'itemnumber'},
1079 $borrower->{'borrowernumber'}
1081 if ( $charge > 0 ) {
1083 $item->{'itemnumber'},
1084 $borrower->{'borrowernumber'}, $charge
1086 $item->{'charge'} = $charge;
1089 # Record the fact that this book was issued.
1091 C4::Context->userenv->{'branch'},
1093 ($sipmode ? "SIP-$sipmode" : ''), $item->{'itemnumber'},
1094 $item->{'itype'}, $borrower->{'borrowernumber'}
1097 # Send a checkout slip.
1098 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1100 branchcode => $branch,
1101 categorycode => $borrower->{categorycode},
1102 item_type => $item->{itype},
1103 notification => 'CHECKOUT',
1105 if ($circulation_alert->is_enabled_for(\%conditions)) {
1106 SendCirculationAlert({
1109 borrower => $borrower,
1115 logaction("CIRCULATION", "ISSUE", $borrower->{'borrowernumber'}, $biblio->{'biblionumber'})
1116 if C4::Context->preference("IssueLog");
1118 return ($datedue); # not necessarily the same as when it came in!
1121 =head2 GetLoanLength
1123 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1125 Get loan length for an itemtype, a borrower type and a branch
1130 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1131 my $dbh = C4::Context->dbh;
1134 'select issuelength, lengthunit from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null'
1136 # warn "in get loan lenght $borrowertype $itemtype $branchcode ";
1137 # try to find issuelength & return the 1st available.
1138 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1139 $sth->execute( $borrowertype, $itemtype, $branchcode );
1140 my $loanlength = $sth->fetchrow_hashref;
1142 if defined($loanlength) && $loanlength->{issuelength};
1144 $sth->execute( $borrowertype, '*', $branchcode );
1145 $loanlength = $sth->fetchrow_hashref;
1147 if defined($loanlength) && $loanlength->{issuelength};
1149 $sth->execute( '*', $itemtype, $branchcode );
1150 $loanlength = $sth->fetchrow_hashref;
1152 if defined($loanlength) && $loanlength->{issuelength};
1154 $sth->execute( '*', '*', $branchcode );
1155 $loanlength = $sth->fetchrow_hashref;
1157 if defined($loanlength) && $loanlength->{issuelength};
1159 $sth->execute( $borrowertype, $itemtype, '*' );
1160 $loanlength = $sth->fetchrow_hashref;
1162 if defined($loanlength) && $loanlength->{issuelength};
1164 $sth->execute( $borrowertype, '*', '*' );
1165 $loanlength = $sth->fetchrow_hashref;
1167 if defined($loanlength) && $loanlength->{issuelength};
1169 $sth->execute( '*', $itemtype, '*' );
1170 $loanlength = $sth->fetchrow_hashref;
1172 if defined($loanlength) && $loanlength->{issuelength};
1174 $sth->execute( '*', '*', '*' );
1175 $loanlength = $sth->fetchrow_hashref;
1177 if defined($loanlength) && $loanlength->{issuelength};
1179 # if no rule is set => 21 days (hardcoded)
1182 lengthunit => 'days',
1188 =head2 GetHardDueDate
1190 my ($hardduedate,$hardduedatecompare) = &GetHardDueDate($borrowertype,$itemtype,branchcode)
1192 Get the Hard Due Date and it's comparison for an itemtype, a borrower type and a branch
1196 sub GetHardDueDate {
1197 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1199 my $rule = GetIssuingRule( $borrowertype, $itemtype, $branchcode );
1201 if ( defined( $rule ) ) {
1202 if ( $rule->{hardduedate} ) {
1203 return (dt_from_string($rule->{hardduedate}, 'iso'),$rule->{hardduedatecompare});
1205 return (undef, undef);
1210 =head2 GetIssuingRule
1212 my $irule = &GetIssuingRule($borrowertype,$itemtype,branchcode)
1214 FIXME - This is a copy-paste of GetLoanLength
1215 as a stop-gap. Do not wish to change API for GetLoanLength
1216 this close to release, however, Overdues::GetIssuingRules is broken.
1218 Get the issuing rule for an itemtype, a borrower type and a branch
1219 Returns a hashref from the issuingrules table.
1223 sub GetIssuingRule {
1224 my ( $borrowertype, $itemtype, $branchcode ) = @_;
1225 my $dbh = C4::Context->dbh;
1226 my $sth = $dbh->prepare( "select * from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null" );
1229 $sth->execute( $borrowertype, $itemtype, $branchcode );
1230 $irule = $sth->fetchrow_hashref;
1231 return $irule if defined($irule) ;
1233 $sth->execute( $borrowertype, "*", $branchcode );
1234 $irule = $sth->fetchrow_hashref;
1235 return $irule if defined($irule) ;
1237 $sth->execute( "*", $itemtype, $branchcode );
1238 $irule = $sth->fetchrow_hashref;
1239 return $irule if defined($irule) ;
1241 $sth->execute( "*", "*", $branchcode );
1242 $irule = $sth->fetchrow_hashref;
1243 return $irule if defined($irule) ;
1245 $sth->execute( $borrowertype, $itemtype, "*" );
1246 $irule = $sth->fetchrow_hashref;
1247 return $irule if defined($irule) ;
1249 $sth->execute( $borrowertype, "*", "*" );
1250 $irule = $sth->fetchrow_hashref;
1251 return $irule if defined($irule) ;
1253 $sth->execute( "*", $itemtype, "*" );
1254 $irule = $sth->fetchrow_hashref;
1255 return $irule if defined($irule) ;
1257 $sth->execute( "*", "*", "*" );
1258 $irule = $sth->fetchrow_hashref;
1259 return $irule if defined($irule) ;
1261 # if no rule matches,
1265 =head2 GetBranchBorrowerCircRule
1267 my $branch_cat_rule = GetBranchBorrowerCircRule($branchcode, $categorycode);
1269 Retrieves circulation rule attributes that apply to the given
1270 branch and patron category, regardless of item type.
1271 The return value is a hashref containing the following key:
1273 maxissueqty - maximum number of loans that a
1274 patron of the given category can have at the given
1275 branch. If the value is undef, no limit.
1277 This will first check for a specific branch and
1278 category match from branch_borrower_circ_rules.
1280 If no rule is found, it will then check default_branch_circ_rules
1281 (same branch, default category). If no rule is found,
1282 it will then check default_borrower_circ_rules (default
1283 branch, same category), then failing that, default_circ_rules
1284 (default branch, default category).
1286 If no rule has been found in the database, it will default to
1291 C<$branchcode> and C<$categorycode> should contain the
1292 literal branch code and patron category code, respectively - no
1297 sub GetBranchBorrowerCircRule {
1298 my $branchcode = shift;
1299 my $categorycode = shift;
1301 my $branch_cat_query = "SELECT maxissueqty
1302 FROM branch_borrower_circ_rules
1303 WHERE branchcode = ?
1304 AND categorycode = ?";
1305 my $dbh = C4::Context->dbh();
1306 my $sth = $dbh->prepare($branch_cat_query);
1307 $sth->execute($branchcode, $categorycode);
1309 if ($result = $sth->fetchrow_hashref()) {
1313 # try same branch, default borrower category
1314 my $branch_query = "SELECT maxissueqty
1315 FROM default_branch_circ_rules
1316 WHERE branchcode = ?";
1317 $sth = $dbh->prepare($branch_query);
1318 $sth->execute($branchcode);
1319 if ($result = $sth->fetchrow_hashref()) {
1323 # try default branch, same borrower category
1324 my $category_query = "SELECT maxissueqty
1325 FROM default_borrower_circ_rules
1326 WHERE categorycode = ?";
1327 $sth = $dbh->prepare($category_query);
1328 $sth->execute($categorycode);
1329 if ($result = $sth->fetchrow_hashref()) {
1333 # try default branch, default borrower category
1334 my $default_query = "SELECT maxissueqty
1335 FROM default_circ_rules";
1336 $sth = $dbh->prepare($default_query);
1338 if ($result = $sth->fetchrow_hashref()) {
1342 # built-in default circulation rule
1344 maxissueqty => undef,
1348 =head2 GetBranchItemRule
1350 my $branch_item_rule = GetBranchItemRule($branchcode, $itemtype);
1352 Retrieves circulation rule attributes that apply to the given
1353 branch and item type, regardless of patron category.
1355 The return value is a hashref containing the following keys:
1357 holdallowed => Hold policy for this branch and itemtype. Possible values:
1358 0: No holds allowed.
1359 1: Holds allowed only by patrons that have the same homebranch as the item.
1360 2: Holds allowed from any patron.
1362 returnbranch => branch to which to return item. Possible values:
1363 noreturn: do not return, let item remain where checked in (floating collections)
1364 homebranch: return to item's home branch
1366 This searches branchitemrules in the following order:
1368 * Same branchcode and itemtype
1369 * Same branchcode, itemtype '*'
1370 * branchcode '*', same itemtype
1371 * branchcode and itemtype '*'
1373 Neither C<$branchcode> nor C<$itemtype> should be '*'.
1377 sub GetBranchItemRule {
1378 my ( $branchcode, $itemtype ) = @_;
1379 my $dbh = C4::Context->dbh();
1383 ['SELECT holdallowed, returnbranch
1384 FROM branch_item_rules
1385 WHERE branchcode = ?
1386 AND itemtype = ?', $branchcode, $itemtype],
1387 ['SELECT holdallowed, returnbranch
1388 FROM default_branch_circ_rules
1389 WHERE branchcode = ?', $branchcode],
1390 ['SELECT holdallowed, returnbranch
1391 FROM default_branch_item_rules
1392 WHERE itemtype = ?', $itemtype],
1393 ['SELECT holdallowed, returnbranch
1394 FROM default_circ_rules'],
1397 foreach my $attempt (@attempts) {
1398 my ($query, @bind_params) = @{$attempt};
1399 my $search_result = $dbh->selectrow_hashref ( $query , {}, @bind_params );
1401 # Since branch/category and branch/itemtype use the same per-branch
1402 # defaults tables, we have to check that the key we want is set, not
1403 # just that a row was returned
1404 $result->{'holdallowed'} = $search_result->{'holdallowed'} unless ( defined $result->{'holdallowed'} );
1405 $result->{'returnbranch'} = $search_result->{'returnbranch'} unless ( defined $result->{'returnbranch'} );
1408 # built-in default circulation rule
1409 $result->{'holdallowed'} = 2 unless ( defined $result->{'holdallowed'} );
1410 $result->{'returnbranch'} = 'homebranch' unless ( defined $result->{'returnbranch'} );
1417 ($doreturn, $messages, $iteminformation, $borrower) =
1418 &AddReturn($barcode, $branch, $exemptfine, $dropbox);
1424 =item C<$barcode> is the bar code of the book being returned.
1426 =item C<$branch> is the code of the branch where the book is being returned.
1428 =item C<$exemptfine> indicates that overdue charges for the item will be
1431 =item C<$dropbox> indicates that the check-in date is assumed to be
1432 yesterday, or the last non-holiday as defined in C4::Calendar . If
1433 overdue charges are applied and C<$dropbox> is true, the last charge
1434 will be removed. This assumes that the fines accrual script has run
1439 C<&AddReturn> returns a list of four items:
1441 C<$doreturn> is true iff the return succeeded.
1443 C<$messages> is a reference-to-hash giving feedback on the operation.
1444 The keys of the hash are:
1450 No item with this barcode exists. The value is C<$barcode>.
1454 The book is not currently on loan. The value is C<$barcode>.
1456 =item C<IsPermanent>
1458 The book's home branch is a permanent collection. If you have borrowed
1459 this book, you are not allowed to return it. The value is the code for
1460 the book's home branch.
1464 This book has been withdrawn/cancelled. The value should be ignored.
1466 =item C<Wrongbranch>
1468 This book has was returned to the wrong branch. The value is a hashref
1469 so that C<$messages->{Wrongbranch}->{Wrongbranch}> and C<$messages->{Wrongbranch}->{Rightbranch}>
1470 contain the branchcode of the incorrect and correct return library, respectively.
1474 The item was reserved. The value is a reference-to-hash whose keys are
1475 fields from the reserves table of the Koha database, and
1476 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1477 either C<Waiting>, C<Reserved>, or 0.
1481 C<$iteminformation> is a reference-to-hash, giving information about the
1482 returned item from the issues table.
1484 C<$borrower> is a reference-to-hash, giving information about the
1485 patron who last borrowed the book.
1490 my ( $barcode, $branch, $exemptfine, $dropbox ) = @_;
1492 if ($branch and not GetBranchDetail($branch)) {
1493 warn "AddReturn error: branch '$branch' not found. Reverting to " . C4::Context->userenv->{'branch'};
1496 $branch = C4::Context->userenv->{'branch'} unless $branch; # we trust userenv to be a safe fallback/default
1501 my $validTransfert = 0;
1502 my $stat_type = 'return';
1504 # get information on item
1505 my $itemnumber = GetItemnumberFromBarcode( $barcode );
1506 unless ($itemnumber) {
1507 return (0, { BadBarcode => $barcode }); # no barcode means no item or borrower. bail out.
1509 my $issue = GetItemIssue($itemnumber);
1510 # warn Dumper($iteminformation);
1511 if ($issue and $issue->{borrowernumber}) {
1512 $borrower = C4::Members::GetMemberDetails($issue->{borrowernumber})
1513 or die "Data inconsistency: barcode $barcode (itemnumber:$itemnumber) claims to be issued to non-existant borrowernumber '$issue->{borrowernumber}'\n"
1514 . Dumper($issue) . "\n";
1516 $messages->{'NotIssued'} = $barcode;
1517 # even though item is not on loan, it may still be transferred; therefore, get current branch info
1519 # No issue, no borrowernumber. ONLY if $doreturn, *might* you have a $borrower later.
1520 # Record this as a local use, instead of a return, if the RecordLocalUseOnReturn is on
1521 if (C4::Context->preference("RecordLocalUseOnReturn")) {
1522 $messages->{'LocalUse'} = 1;
1523 $stat_type = 'localuse';
1527 my $item = GetItem($itemnumber) or die "GetItem($itemnumber) failed";
1528 # full item data, but no borrowernumber or checkout info (no issue)
1529 # we know GetItem should work because GetItemnumberFromBarcode worked
1530 my $hbr = GetBranchItemRule($item->{'homebranch'}, $item->{'itype'})->{'returnbranch'} || "homebranch";
1531 # get the proper branch to which to return the item
1532 $hbr = $item->{$hbr} || $branch ;
1533 # if $hbr was "noreturn" or any other non-item table value, then it should 'float' (i.e. stay at this branch)
1535 my $borrowernumber = $borrower->{'borrowernumber'} || undef; # we don't know if we had a borrower or not
1537 # check if the book is in a permanent collection....
1538 # FIXME -- This 'PE' attribute is largely undocumented. afaict, there's no user interface that reflects this functionality.
1540 my $branches = GetBranches(); # a potentially expensive call for a non-feature.
1541 $branches->{$hbr}->{PE} and $messages->{'IsPermanent'} = $hbr;
1544 # if indy branches and returning to different branch, refuse the return
1545 if ($hbr ne $branch && C4::Context->preference("IndependantBranches")){
1546 $messages->{'Wrongbranch'} = {
1547 Wrongbranch => $branch,
1548 Rightbranch => $hbr,
1551 # bailing out here - in this case, current desired behavior
1552 # is to act as if no return ever happened at all.
1553 # FIXME - even in an indy branches situation, there should
1554 # still be an option for the library to accept the item
1555 # and transfer it to its owning library.
1556 return ( $doreturn, $messages, $issue, $borrower );
1559 if ( $item->{'wthdrawn'} ) { # book has been cancelled
1560 $messages->{'wthdrawn'} = 1;
1564 # case of a return of document (deal with issues and holdingbranch)
1565 my $today = DateTime->now( time_zone => C4::Context->tz() );
1567 my $datedue = $issue->{date_due};
1568 $borrower or warn "AddReturn without current borrower";
1569 my $circControlBranch;
1571 # define circControlBranch only if dropbox mode is set
1572 # don't allow dropbox mode to create an invalid entry in issues (issuedate > today)
1573 # FIXME: check issuedate > returndate, factoring in holidays
1574 #$circControlBranch = _GetCircControlBranch($item,$borrower) unless ( $item->{'issuedate'} eq C4::Dates->today('iso') );;
1575 $circControlBranch = _GetCircControlBranch($item,$borrower);
1576 $issue->{'overdue'} = DateTime->compare($issue->{'date_due'}, $today ) == -1 ? 1 : 0;
1579 if ($borrowernumber) {
1580 if($issue->{'overdue'}){
1581 my ( $amount, $type, $daycounttotal ) = C4::Overdues::CalcFine( $item, $borrower->{categorycode},$branch, $datedue, $today );
1583 if ( $amount > 0 && ( C4::Context->preference('finesMode') eq 'production' )) {
1584 C4::Overdues::UpdateFine(
1585 $issue->{itemnumber},
1586 $issue->{borrowernumber},
1587 $amount, $type, output_pref($datedue)
1591 MarkIssueReturned($borrowernumber, $item->{'itemnumber'}, $circControlBranch, '', $borrower->{'privacy'});
1592 $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right? This could be the borrower hash.
1595 ModItem({ onloan => undef }, $issue->{'biblionumber'}, $item->{'itemnumber'});
1598 # the holdingbranch is updated if the document is returned to another location.
1599 # this is always done regardless of whether the item was on loan or not
1600 if ($item->{'holdingbranch'} ne $branch) {
1601 UpdateHoldingbranch($branch, $item->{'itemnumber'});
1602 $item->{'holdingbranch'} = $branch; # update item data holdingbranch too
1604 ModDateLastSeen( $item->{'itemnumber'} );
1606 # check if we have a transfer for this document
1607 my ($datesent,$frombranch,$tobranch) = GetTransfers( $item->{'itemnumber'} );
1609 # if we have a transfer to do, we update the line of transfers with the datearrived
1611 if ( $tobranch eq $branch ) {
1612 my $sth = C4::Context->dbh->prepare(
1613 "UPDATE branchtransfers SET datearrived = now() WHERE itemnumber= ? AND datearrived IS NULL"
1615 $sth->execute( $item->{'itemnumber'} );
1616 # if we have a reservation with valid transfer, we can set it's status to 'W'
1617 ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1618 C4::Reserves::ModReserveStatus($item->{'itemnumber'}, 'W');
1620 $messages->{'WrongTransfer'} = $tobranch;
1621 $messages->{'WrongTransferItem'} = $item->{'itemnumber'};
1623 $validTransfert = 1;
1625 ShelfToCart( $item->{'itemnumber'} ) if ( C4::Context->preference("ReturnToShelvingCart") );
1628 # fix up the accounts.....
1629 if ($item->{'itemlost'}) {
1630 _FixAccountForLostAndReturned($item->{'itemnumber'}, $borrowernumber, $barcode); # can tolerate undef $borrowernumber
1631 $messages->{'WasLost'} = 1;
1634 # fix up the overdues in accounts...
1635 if ($borrowernumber) {
1636 my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox);
1637 defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!"; # zero is OK, check defined
1639 if ( $issue->{overdue} && $issue->{date_due} ) {
1642 _debar_user_on_return( $borrower, $item, $issue->{date_due}, $today );
1643 $messages->{Debarred} = $debardate if ($debardate);
1647 # find reserves.....
1648 # if we don't have a reserve with the status W, we launch the Checkreserves routine
1649 my ($resfound, $resrec, undef) = C4::Reserves::CheckReserves( $item->{'itemnumber'} );
1651 $resrec->{'ResFound'} = $resfound;
1652 $messages->{'ResFound'} = $resrec;
1656 # Record the fact that this book was returned.
1658 $branch, $stat_type, '0', '',
1659 $item->{'itemnumber'},
1660 $biblio->{'itemtype'},
1664 # Send a check-in slip. # NOTE: borrower may be undef. probably shouldn't try to send messages then.
1665 my $circulation_alert = 'C4::ItemCirculationAlertPreference';
1667 branchcode => $branch,
1668 categorycode => $borrower->{categorycode},
1669 item_type => $item->{itype},
1670 notification => 'CHECKIN',
1672 if ($doreturn && $circulation_alert->is_enabled_for(\%conditions)) {
1673 SendCirculationAlert({
1676 borrower => $borrower,
1681 logaction("CIRCULATION", "RETURN", $borrowernumber, $item->{'biblionumber'})
1682 if C4::Context->preference("ReturnLog");
1684 # FIXME: make this comment intelligible.
1685 #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1686 #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1688 if (($doreturn or $messages->{'NotIssued'}) and !$resfound and ($branch ne $hbr) and not $messages->{'WrongTransfer'}){
1689 if ( C4::Context->preference("AutomaticItemReturn" ) or
1690 (C4::Context->preference("UseBranchTransferLimits") and
1691 ! IsBranchTransferAllowed($branch, $hbr, $item->{C4::Context->preference("BranchTransferLimitsType")} )
1693 $debug and warn sprintf "about to call ModItemTransfer(%s, %s, %s)", $item->{'itemnumber'},$branch, $hbr;
1694 $debug and warn "item: " . Dumper($item);
1695 ModItemTransfer($item->{'itemnumber'}, $branch, $hbr);
1696 $messages->{'WasTransfered'} = 1;
1698 $messages->{'NeedsTransfer'} = 1; # TODO: instead of 1, specify branchcode that the transfer SHOULD go to, $item->{homebranch}
1701 return ( $doreturn, $messages, $issue, $borrower );
1704 =head2 MarkIssueReturned
1706 MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy);
1708 Unconditionally marks an issue as being returned by
1709 moving the C<issues> row to C<old_issues> and
1710 setting C<returndate> to the current date, or
1711 the last non-holiday date of the branccode specified in
1712 C<dropbox_branch> . Assumes you've already checked that
1713 it's safe to do this, i.e. last non-holiday > issuedate.
1715 if C<$returndate> is specified (in iso format), it is used as the date
1716 of the return. It is ignored when a dropbox_branch is passed in.
1718 C<$privacy> contains the privacy parameter. If the patron has set privacy to 2,
1719 the old_issue is immediately anonymised
1721 Ideally, this function would be internal to C<C4::Circulation>,
1722 not exported, but it is currently needed by one
1723 routine in C<C4::Accounts>.
1727 sub MarkIssueReturned {
1728 my ( $borrowernumber, $itemnumber, $dropbox_branch, $returndate, $privacy ) = @_;
1730 my $dbh = C4::Context->dbh;
1731 my $query = 'UPDATE issues SET returndate=';
1733 if ($dropbox_branch) {
1734 my $calendar = Koha::Calendar->new( branchcode => $dropbox_branch );
1735 my $dropboxdate = $calendar->addDate( DateTime->now( time_zone => C4::Context->tz), -1 );
1737 push @bind, $dropboxdate->strftime('%Y-%m-%d %H:%M');
1738 } elsif ($returndate) {
1740 push @bind, $returndate;
1742 $query .= ' now() ';
1744 $query .= ' WHERE borrowernumber = ? AND itemnumber = ?';
1745 push @bind, $borrowernumber, $itemnumber;
1747 my $sth_upd = $dbh->prepare($query);
1748 $sth_upd->execute(@bind);
1749 my $sth_copy = $dbh->prepare('INSERT INTO old_issues SELECT * FROM issues
1750 WHERE borrowernumber = ?
1751 AND itemnumber = ?');
1752 $sth_copy->execute($borrowernumber, $itemnumber);
1753 # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
1754 if ( $privacy == 2) {
1755 # The default of 0 does not work due to foreign key constraints
1756 # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
1757 my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
1758 my $sth_ano = $dbh->prepare("UPDATE old_issues SET borrowernumber=?
1759 WHERE borrowernumber = ?
1760 AND itemnumber = ?");
1761 $sth_ano->execute($anonymouspatron, $borrowernumber, $itemnumber);
1763 my $sth_del = $dbh->prepare("DELETE FROM issues
1764 WHERE borrowernumber = ?
1765 AND itemnumber = ?");
1766 $sth_del->execute($borrowernumber, $itemnumber);
1769 =head2 _debar_user_on_return
1771 _debar_user_on_return($borrower, $item, $datedue, today);
1773 C<$borrower> borrower hashref
1775 C<$item> item hashref
1777 C<$datedue> date due DateTime object
1779 C<$today> DateTime object representing the return time
1781 Internal function, called only by AddReturn that calculates and updates
1782 the user fine days, and debars him if necessary.
1784 Should only be called for overdue returns
1788 sub _debar_user_on_return {
1789 my ( $borrower, $item, $dt_due, $dt_today ) = @_;
1791 my $branchcode = _GetCircControlBranch( $item, $borrower );
1792 my $calendar = Koha::Calendar->new( branchcode => $branchcode );
1794 # $deltadays is a DateTime::Duration object
1795 my $deltadays = $calendar->days_between( $dt_due, $dt_today );
1797 my $circcontrol = C4::Context->preference('CircControl');
1799 GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
1800 my $finedays = $issuingrule->{finedays};
1801 my $unit = $issuingrule->{lengthunit};
1805 # finedays is in days, so hourly loans must multiply by 24
1806 # thus 1 hour late equals 1 day suspension * finedays rate
1807 $finedays = $finedays * 24 if ( $unit eq 'hours' );
1809 # grace period is measured in the same units as the loan
1811 DateTime::Duration->new( $unit => $issuingrule->{firstremind} );
1812 if ( $deltadays->subtract($grace)->is_positive() ) {
1815 $dt_today->clone()->add_duration( $deltadays * $finedays );
1816 if ( $borrower->{debarred} ) {
1817 my $borrower_debar_dt = dt_from_string( $borrower->{debarred} );
1819 # Update patron only if new date > old
1820 if ( DateTime->compare( $borrower_debar_dt, $new_debar_dt ) !=
1827 C4::Members::DebarMember( $borrower->{borrowernumber},
1828 $new_debar_dt->ymd() );
1829 return $new_debar_dt->ymd();
1835 =head2 _FixOverduesOnReturn
1837 &_FixOverduesOnReturn($brn,$itm, $exemptfine, $dropboxmode);
1839 C<$brn> borrowernumber
1843 C<$exemptfine> BOOL -- remove overdue charge associated with this issue.
1844 C<$dropboxmode> BOOL -- remove lastincrement on overdue charge associated with this issue.
1846 Internal function, called only by AddReturn
1850 sub _FixOverduesOnReturn {
1851 my ($borrowernumber, $item);
1852 unless ($borrowernumber = shift) {
1853 warn "_FixOverduesOnReturn() not supplied valid borrowernumber";
1856 unless ($item = shift) {
1857 warn "_FixOverduesOnReturn() not supplied valid itemnumber";
1860 my ($exemptfine, $dropbox) = @_;
1861 my $dbh = C4::Context->dbh;
1863 # check for overdue fine
1864 my $sth = $dbh->prepare(
1865 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1867 $sth->execute( $borrowernumber, $item );
1869 # alter fine to show that the book has been returned
1870 my $data = $sth->fetchrow_hashref;
1871 return 0 unless $data; # no warning, there's just nothing to fix
1874 my @bind = ($borrowernumber, $item, $data->{'accountno'});
1876 $uquery = "update accountlines set accounttype='FFOR', amountoutstanding=0";
1877 if (C4::Context->preference("FinesLog")) {
1878 &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item");
1880 } elsif ($dropbox && $data->{lastincrement}) {
1881 my $outstanding = $data->{amountoutstanding} - $data->{lastincrement} ;
1882 my $amt = $data->{amount} - $data->{lastincrement} ;
1883 if (C4::Context->preference("FinesLog")) {
1884 &logaction("FINES", 'MODIFY',$borrowernumber,"Dropbox adjustment $amt, item $item");
1886 $uquery = "update accountlines set accounttype='F' ";
1887 if($outstanding >= 0 && $amt >=0) {
1888 $uquery .= ", amount = ? , amountoutstanding=? ";
1889 unshift @bind, ($amt, $outstanding) ;
1892 $uquery = "update accountlines set accounttype='F' ";
1894 $uquery .= " where (borrowernumber = ?) and (itemnumber = ?) and (accountno = ?)";
1895 my $usth = $dbh->prepare($uquery);
1896 return $usth->execute(@bind);
1899 =head2 _FixAccountForLostAndReturned
1901 &_FixAccountForLostAndReturned($itemnumber, [$borrowernumber, $barcode]);
1903 Calculates the charge for a book lost and returned.
1905 Internal function, not exported, called only by AddReturn.
1907 FIXME: This function reflects how inscrutable fines logic is. Fix both.
1908 FIXME: Give a positive return value on success. It might be the $borrowernumber who received credit, or the amount forgiven.
1912 sub _FixAccountForLostAndReturned {
1913 my $itemnumber = shift or return;
1914 my $borrowernumber = @_ ? shift : undef;
1915 my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description
1916 my $dbh = C4::Context->dbh;
1917 # check for charge made for lost book
1918 my $sth = $dbh->prepare("SELECT * FROM accountlines WHERE itemnumber = ? AND accounttype IN ('L', 'Rep', 'W') ORDER BY date DESC, accountno DESC");
1919 $sth->execute($itemnumber);
1920 my $data = $sth->fetchrow_hashref;
1921 $data or return; # bail if there is nothing to do
1922 $data->{accounttype} eq 'W' and return; # Written off
1924 # writeoff this amount
1926 my $amount = $data->{'amount'};
1927 my $acctno = $data->{'accountno'};
1928 my $amountleft; # Starts off undef/zero.
1929 if ($data->{'amountoutstanding'} == $amount) {
1930 $offset = $data->{'amount'};
1931 $amountleft = 0; # Hey, it's zero here, too.
1933 $offset = $amount - $data->{'amountoutstanding'}; # Um, isn't this the same as ZERO? We just tested those two things are ==
1934 $amountleft = $data->{'amountoutstanding'} - $amount; # Um, isn't this the same as ZERO? We just tested those two things are ==
1936 my $usth = $dbh->prepare("UPDATE accountlines SET accounttype = 'LR',amountoutstanding='0'
1937 WHERE (borrowernumber = ?)
1938 AND (itemnumber = ?) AND (accountno = ?) ");
1939 $usth->execute($data->{'borrowernumber'},$itemnumber,$acctno); # We might be adjusting an account for some OTHER borrowernumber now. Not the one we passed in.
1940 #check if any credit is left if so writeoff other accounts
1941 my $nextaccntno = getnextacctno($data->{'borrowernumber'});
1942 $amountleft *= -1 if ($amountleft < 0);
1943 if ($amountleft > 0) {
1944 my $msth = $dbh->prepare("SELECT * FROM accountlines WHERE (borrowernumber = ?)
1945 AND (amountoutstanding >0) ORDER BY date"); # might want to order by amountoustanding ASC (pay smallest first)
1946 $msth->execute($data->{'borrowernumber'});
1947 # offset transactions
1950 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1951 if ($accdata->{'amountoutstanding'} < $amountleft) {
1953 $amountleft -= $accdata->{'amountoutstanding'};
1955 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1958 my $thisacct = $accdata->{'accountno'};
1959 # FIXME: move prepares outside while loop!
1960 my $usth = $dbh->prepare("UPDATE accountlines SET amountoutstanding= ?
1961 WHERE (borrowernumber = ?)
1962 AND (accountno=?)");
1963 $usth->execute($newamtos,$data->{'borrowernumber'},'$thisacct'); # FIXME: '$thisacct' is a string literal!
1964 $usth = $dbh->prepare("INSERT INTO accountoffsets
1965 (borrowernumber, accountno, offsetaccount, offsetamount)
1968 $usth->execute($data->{'borrowernumber'},$accdata->{'accountno'},$nextaccntno,$newamtos);
1970 $msth->finish; # $msth might actually have data left
1972 $amountleft *= -1 if ($amountleft > 0);
1973 my $desc = "Item Returned " . $item_id;
1974 $usth = $dbh->prepare("INSERT INTO accountlines
1975 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1976 VALUES (?,?,now(),?,?,'CR',?)");
1977 $usth->execute($data->{'borrowernumber'},$nextaccntno,0-$amount,$desc,$amountleft);
1978 if ($borrowernumber) {
1979 # FIXME: same as query above. use 1 sth for both
1980 $usth = $dbh->prepare("INSERT INTO accountoffsets
1981 (borrowernumber, accountno, offsetaccount, offsetamount)
1983 $usth->execute($borrowernumber, $data->{'accountno'}, $nextaccntno, $offset);
1985 ModItem({ paidfor => '' }, undef, $itemnumber);
1989 =head2 _GetCircControlBranch
1991 my $circ_control_branch = _GetCircControlBranch($iteminfos, $borrower);
1995 Return the library code to be used to determine which circulation
1996 policy applies to a transaction. Looks up the CircControl and
1997 HomeOrHoldingBranch system preferences.
1999 C<$iteminfos> is a hashref to iteminfo. Only {homebranch or holdingbranch} is used.
2001 C<$borrower> is a hashref to borrower. Only {branchcode} is used.
2005 sub _GetCircControlBranch {
2006 my ($item, $borrower) = @_;
2007 my $circcontrol = C4::Context->preference('CircControl');
2010 if ($circcontrol eq 'PickupLibrary' and (C4::Context->userenv and C4::Context->userenv->{'branch'}) ) {
2011 $branch= C4::Context->userenv->{'branch'};
2012 } elsif ($circcontrol eq 'PatronLibrary') {
2013 $branch=$borrower->{branchcode};
2015 my $branchfield = C4::Context->preference('HomeOrHoldingBranch') || 'homebranch';
2016 $branch = $item->{$branchfield};
2017 # default to item home branch if holdingbranch is used
2018 # and is not defined
2019 if (!defined($branch) && $branchfield eq 'holdingbranch') {
2020 $branch = $item->{homebranch};
2033 $issue = &GetItemIssue($itemnumber);
2035 Returns patron currently having a book, or undef if not checked out.
2037 C<$itemnumber> is the itemnumber.
2039 C<$issue> is a hashref of the row from the issues table.
2044 my ($itemnumber) = @_;
2045 return unless $itemnumber;
2046 my $sth = C4::Context->dbh->prepare(
2049 LEFT JOIN items ON issues.itemnumber=items.itemnumber
2050 WHERE issues.itemnumber=?");
2051 $sth->execute($itemnumber);
2052 my $data = $sth->fetchrow_hashref;
2053 return unless $data;
2054 $data->{issuedate} = dt_from_string($data->{issuedate}, 'sql');
2055 $data->{issuedate}->truncate(to => 'minute');
2056 $data->{date_due} = dt_from_string($data->{date_due}, 'sql');
2057 $data->{date_due}->truncate(to => 'minute');
2058 my $dt = DateTime->now( time_zone => C4::Context->tz)->truncate( to => 'minute');
2059 $data->{'overdue'} = DateTime->compare($data->{'date_due'}, $dt ) == -1 ? 1 : 0;
2065 $issue = GetOpenIssue( $itemnumber );
2067 Returns the row from the issues table if the item is currently issued, undef if the item is not currently issued
2069 C<$itemnumber> is the item's itemnumber
2076 my ( $itemnumber ) = @_;
2078 my $dbh = C4::Context->dbh;
2079 my $sth = $dbh->prepare( "SELECT * FROM issues WHERE itemnumber = ? AND returndate IS NULL" );
2080 $sth->execute( $itemnumber );
2081 my $issue = $sth->fetchrow_hashref();
2085 =head2 GetItemIssues
2087 $issues = &GetItemIssues($itemnumber, $history);
2089 Returns patrons that have issued a book
2091 C<$itemnumber> is the itemnumber
2092 C<$history> is false if you just want the current "issuer" (if any)
2093 and true if you want issues history from old_issues also.
2095 Returns reference to an array of hashes
2100 my ( $itemnumber, $history ) = @_;
2102 my $today = DateTime->now( time_zome => C4::Context->tz); # get today date
2103 $today->truncate( to => 'minute' );
2104 my $sql = "SELECT * FROM issues
2105 JOIN borrowers USING (borrowernumber)
2106 JOIN items USING (itemnumber)
2107 WHERE issues.itemnumber = ? ";
2110 SELECT * FROM old_issues
2111 LEFT JOIN borrowers USING (borrowernumber)
2112 JOIN items USING (itemnumber)
2113 WHERE old_issues.itemnumber = ? ";
2115 $sql .= "ORDER BY date_due DESC";
2116 my $sth = C4::Context->dbh->prepare($sql);
2118 $sth->execute($itemnumber, $itemnumber);
2120 $sth->execute($itemnumber);
2122 my $results = $sth->fetchall_arrayref({});
2123 foreach (@$results) {
2124 my $date_due = dt_from_string($_->{date_due},'sql');
2125 $date_due->truncate( to => 'minute' );
2127 $_->{overdue} = (DateTime->compare($date_due, $today) == -1) ? 1 : 0;
2132 =head2 GetBiblioIssues
2134 $issues = GetBiblioIssues($biblionumber);
2136 this function get all issues from a biblionumber.
2139 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
2140 tables issues and the firstname,surname & cardnumber from borrowers.
2144 sub GetBiblioIssues {
2145 my $biblionumber = shift;
2146 return undef unless $biblionumber;
2147 my $dbh = C4::Context->dbh;
2149 SELECT issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
2151 LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
2152 LEFT JOIN items ON issues.itemnumber = items.itemnumber
2153 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
2154 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
2155 WHERE biblio.biblionumber = ?
2157 SELECT old_issues.*,items.barcode,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
2159 LEFT JOIN borrowers ON borrowers.borrowernumber = old_issues.borrowernumber
2160 LEFT JOIN items ON old_issues.itemnumber = items.itemnumber
2161 LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
2162 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
2163 WHERE biblio.biblionumber = ?
2166 my $sth = $dbh->prepare($query);
2167 $sth->execute($biblionumber, $biblionumber);
2170 while ( my $data = $sth->fetchrow_hashref ) {
2171 push @issues, $data;
2176 =head2 GetUpcomingDueIssues
2178 my $upcoming_dues = GetUpcomingDueIssues( { days_in_advance => 4 } );
2182 sub GetUpcomingDueIssues {
2185 $params->{'days_in_advance'} = 7 unless exists $params->{'days_in_advance'};
2186 my $dbh = C4::Context->dbh;
2188 my $statement = <<END_SQL;
2189 SELECT issues.*, items.itype as itemtype, items.homebranch, TO_DAYS( date_due )-TO_DAYS( NOW() ) as days_until_due, branches.branchemail
2191 LEFT JOIN items USING (itemnumber)
2192 LEFT OUTER JOIN branches USING (branchcode)
2193 WhERE returndate is NULL
2194 AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ?
2197 my @bind_parameters = ( $params->{'days_in_advance'} );
2199 my $sth = $dbh->prepare( $statement );
2200 $sth->execute( @bind_parameters );
2201 my $upcoming_dues = $sth->fetchall_arrayref({});
2204 return $upcoming_dues;
2207 =head2 CanBookBeRenewed
2209 ($ok,$error) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2211 Find out whether a borrowed item may be renewed.
2213 C<$dbh> is a DBI handle to the Koha database.
2215 C<$borrowernumber> is the borrower number of the patron who currently
2216 has the item on loan.
2218 C<$itemnumber> is the number of the item to renew.
2220 C<$override_limit>, if supplied with a true value, causes
2221 the limit on the number of times that the loan can be renewed
2222 (as controlled by the item type) to be ignored.
2224 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
2225 item must currently be on loan to the specified borrower; renewals
2226 must be allowed for the item's type; and the borrower must not have
2227 already renewed the loan. $error will contain the reason the renewal can not proceed
2231 sub CanBookBeRenewed {
2233 # check renewal status
2234 my ( $borrowernumber, $itemnumber, $override_limit ) = @_;
2235 my $dbh = C4::Context->dbh;
2240 # Look in the issues table for this item, lent to this borrower,
2241 # and not yet returned.
2243 # Look in the issues table for this item, lent to this borrower,
2244 # and not yet returned.
2246 'ItemHomeLibrary' => 'items.homebranch',
2247 'PickupLibrary' => 'items.holdingbranch',
2248 'PatronLibrary' => 'borrowers.branchcode'
2250 my $controlbranch = $branch{C4::Context->preference('CircControl')};
2251 my $itype = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype';
2253 my $sthcount = $dbh->prepare("
2255 borrowers.categorycode, biblioitems.itemtype, issues.renewals, renewalsallowed, $controlbranch
2258 LEFT JOIN items USING (itemnumber)
2259 LEFT JOIN borrowers USING (borrowernumber)
2260 LEFT JOIN biblioitems USING (biblioitemnumber)
2263 (issuingrules.categorycode = borrowers.categorycode OR issuingrules.categorycode = '*')
2265 (issuingrules.itemtype = $itype OR issuingrules.itemtype = '*')
2267 (issuingrules.branchcode = $controlbranch OR issuingrules.branchcode = '*')
2273 issuingrules.categorycode desc,
2274 issuingrules.itemtype desc,
2275 issuingrules.branchcode desc
2279 $sthcount->execute( $borrowernumber, $itemnumber );
2280 if ( my $data1 = $sthcount->fetchrow_hashref ) {
2282 if ( ( $data1->{renewalsallowed} && $data1->{renewalsallowed} > $data1->{renewals} ) || $override_limit ) {
2289 my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber);
2296 return ($renewokay,$error);
2301 &AddRenewal($borrowernumber, $itemnumber, $branch, [$datedue], [$lastreneweddate]);
2305 C<$borrowernumber> is the borrower number of the patron who currently
2308 C<$itemnumber> is the number of the item to renew.
2310 C<$branch> is the library where the renewal took place (if any).
2311 The library that controls the circ policies for the renewal is retrieved from the issues record.
2313 C<$datedue> can be a C4::Dates object used to set the due date.
2315 C<$lastreneweddate> is an optional ISO-formatted date used to set issues.lastreneweddate. If
2316 this parameter is not supplied, lastreneweddate is set to the current date.
2318 If C<$datedue> is the empty string, C<&AddRenewal> will calculate the due date automatically
2319 from the book's item type.
2324 my $borrowernumber = shift or return undef;
2325 my $itemnumber = shift or return undef;
2327 my $datedue = shift;
2328 my $lastreneweddate = shift || DateTime->now(time_zone => C4::Context->tz)->ymd();
2329 my $item = GetItem($itemnumber) or return undef;
2330 my $biblio = GetBiblioFromItemNumber($itemnumber) or return undef;
2332 my $dbh = C4::Context->dbh;
2333 # Find the issues record for this book
2335 $dbh->prepare("SELECT * FROM issues
2336 WHERE borrowernumber=?
2339 $sth->execute( $borrowernumber, $itemnumber );
2340 my $issuedata = $sth->fetchrow_hashref;
2342 if(defined $datedue && ref $datedue ne 'DateTime' ) {
2343 carp 'Invalid date passed to AddRenewal.';
2346 # If the due date wasn't specified, calculate it by adding the
2347 # book's loan length to today's date or the current due date
2348 # based on the value of the RenewalPeriodBase syspref.
2351 my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber ) or return undef;
2352 my $itemtype = (C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'};
2354 $datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ?
2355 $issuedata->{date_due} :
2356 DateTime->now( time_zone => C4::Context->tz());
2357 $datedue = CalcDateDue($datedue,$itemtype,$issuedata->{'branchcode'},$borrower);
2360 # Update the issues record to have the new due date, and a new count
2361 # of how many times it has been renewed.
2362 my $renews = $issuedata->{'renewals'} + 1;
2363 $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = ?
2364 WHERE borrowernumber=?
2368 $sth->execute( $datedue->strftime('%Y-%m-%d %H:%M'), $renews, $lastreneweddate, $borrowernumber, $itemnumber );
2371 # Update the renewal count on the item, and tell zebra to reindex
2372 $renews = $biblio->{'renewals'} + 1;
2373 ModItem({ renewals => $renews, onloan => $datedue->strftime('%Y-%m-%d %H:%M')}, $biblio->{'biblionumber'}, $itemnumber);
2375 # Charge a new rental fee, if applicable?
2376 my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
2377 if ( $charge > 0 ) {
2378 my $accountno = getnextacctno( $borrowernumber );
2379 my $item = GetBiblioFromItemNumber($itemnumber);
2381 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
2382 $sth = $dbh->prepare(
2383 "INSERT INTO accountlines
2384 (date, borrowernumber, accountno, amount, manager_id,
2385 description,accounttype, amountoutstanding, itemnumber)
2386 VALUES (now(),?,?,?,?,?,?,?,?)"
2388 $sth->execute( $borrowernumber, $accountno, $charge, $manager_id,
2389 "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
2390 'Rent', $charge, $itemnumber );
2393 UpdateStats( $branch, 'renew', $charge, '', $itemnumber, $item->{itype}, $borrowernumber);
2398 # check renewal status
2399 my ( $bornum, $itemno ) = @_;
2400 my $dbh = C4::Context->dbh;
2402 my $renewsallowed = 0;
2405 my $borrower = C4::Members::GetMember( borrowernumber => $bornum);
2406 my $item = GetItem($itemno);
2408 # Look in the issues table for this item, lent to this borrower,
2409 # and not yet returned.
2411 # FIXME - I think this function could be redone to use only one SQL call.
2412 my $sth = $dbh->prepare(
2413 "select * from issues
2414 where (borrowernumber = ?)
2415 and (itemnumber = ?)"
2417 $sth->execute( $bornum, $itemno );
2418 my $data = $sth->fetchrow_hashref;
2419 $renewcount = $data->{'renewals'} if $data->{'renewals'};
2421 # $item and $borrower should be calculated
2422 my $branchcode = _GetCircControlBranch($item, $borrower);
2424 my $issuingrule = GetIssuingRule($borrower->{categorycode}, $item->{itype}, $branchcode);
2426 $renewsallowed = $issuingrule->{'renewalsallowed'};
2427 $renewsleft = $renewsallowed - $renewcount;
2428 if($renewsleft < 0){ $renewsleft = 0; }
2429 return ( $renewcount, $renewsallowed, $renewsleft );
2432 =head2 GetIssuingCharges
2434 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
2436 Calculate how much it would cost for a given patron to borrow a given
2437 item, including any applicable discounts.
2439 C<$itemnumber> is the item number of item the patron wishes to borrow.
2441 C<$borrowernumber> is the patron's borrower number.
2443 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
2444 and C<$item_type> is the code for the item's item type (e.g., C<VID>
2449 sub GetIssuingCharges {
2451 # calculate charges due
2452 my ( $itemnumber, $borrowernumber ) = @_;
2454 my $dbh = C4::Context->dbh;
2457 # Get the book's item type and rental charge (via its biblioitem).
2458 my $charge_query = 'SELECT itemtypes.itemtype,rentalcharge FROM items
2459 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber';
2460 $charge_query .= (C4::Context->preference('item-level_itypes'))
2461 ? ' LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype'
2462 : ' LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype';
2464 $charge_query .= ' WHERE items.itemnumber =?';
2466 my $sth = $dbh->prepare($charge_query);
2467 $sth->execute($itemnumber);
2468 if ( my $item_data = $sth->fetchrow_hashref ) {
2469 $item_type = $item_data->{itemtype};
2470 $charge = $item_data->{rentalcharge};
2471 my $branch = C4::Branch::mybranch();
2472 my $discount_query = q|SELECT rentaldiscount,
2473 issuingrules.itemtype, issuingrules.branchcode
2475 LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
2476 WHERE borrowers.borrowernumber = ?
2477 AND (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
2478 AND (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')|;
2479 my $discount_sth = $dbh->prepare($discount_query);
2480 $discount_sth->execute( $borrowernumber, $item_type, $branch );
2481 my $discount_rules = $discount_sth->fetchall_arrayref({});
2482 if (@{$discount_rules}) {
2483 # We may have multiple rules so get the most specific
2484 my $discount = _get_discount_from_rule($discount_rules, $branch, $item_type);
2485 $charge = ( $charge * ( 100 - $discount ) ) / 100;
2489 $sth->finish; # we havent _explicitly_ fetched all rows
2490 return ( $charge, $item_type );
2493 # Select most appropriate discount rule from those returned
2494 sub _get_discount_from_rule {
2495 my ($rules_ref, $branch, $itemtype) = @_;
2498 if (@{$rules_ref} == 1) { # only 1 applicable rule use it
2499 $discount = $rules_ref->[0]->{rentaldiscount};
2500 return (defined $discount) ? $discount : 0;
2502 # could have up to 4 does one match $branch and $itemtype
2503 my @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq $itemtype } @{$rules_ref};
2505 $discount = $d[0]->{rentaldiscount};
2506 return (defined $discount) ? $discount : 0;
2508 # do we have item type + all branches
2509 @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq $itemtype } @{$rules_ref};
2511 $discount = $d[0]->{rentaldiscount};
2512 return (defined $discount) ? $discount : 0;
2514 # do we all item types + this branch
2515 @d = grep { $_->{branchcode} eq $branch && $_->{itemtype} eq q{*} } @{$rules_ref};
2517 $discount = $d[0]->{rentaldiscount};
2518 return (defined $discount) ? $discount : 0;
2520 # so all and all (surely we wont get here)
2521 @d = grep { $_->{branchcode} eq q{*} && $_->{itemtype} eq q{*} } @{$rules_ref};
2523 $discount = $d[0]->{rentaldiscount};
2524 return (defined $discount) ? $discount : 0;
2530 =head2 AddIssuingCharge
2532 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
2536 sub AddIssuingCharge {
2537 my ( $itemnumber, $borrowernumber, $charge ) = @_;
2538 my $dbh = C4::Context->dbh;
2539 my $nextaccntno = getnextacctno( $borrowernumber );
2541 $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
2543 INSERT INTO accountlines
2544 (borrowernumber, itemnumber, accountno,
2545 date, amount, description, accounttype,
2546 amountoutstanding, manager_id)
2547 VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?,?)
2549 my $sth = $dbh->prepare($query);
2550 $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge, $manager_id );
2556 GetTransfers($itemnumber);
2561 my ($itemnumber) = @_;
2563 my $dbh = C4::Context->dbh;
2569 FROM branchtransfers
2570 WHERE itemnumber = ?
2571 AND datearrived IS NULL
2573 my $sth = $dbh->prepare($query);
2574 $sth->execute($itemnumber);
2575 my @row = $sth->fetchrow_array();
2580 =head2 GetTransfersFromTo
2582 @results = GetTransfersFromTo($frombranch,$tobranch);
2584 Returns the list of pending transfers between $from and $to branch
2588 sub GetTransfersFromTo {
2589 my ( $frombranch, $tobranch ) = @_;
2590 return unless ( $frombranch && $tobranch );
2591 my $dbh = C4::Context->dbh;
2593 SELECT itemnumber,datesent,frombranch
2594 FROM branchtransfers
2597 AND datearrived IS NULL
2599 my $sth = $dbh->prepare($query);
2600 $sth->execute( $frombranch, $tobranch );
2603 while ( my $data = $sth->fetchrow_hashref ) {
2604 push @gettransfers, $data;
2607 return (@gettransfers);
2610 =head2 DeleteTransfer
2612 &DeleteTransfer($itemnumber);
2616 sub DeleteTransfer {
2617 my ($itemnumber) = @_;
2618 my $dbh = C4::Context->dbh;
2619 my $sth = $dbh->prepare(
2620 "DELETE FROM branchtransfers
2622 AND datearrived IS NULL "
2624 $sth->execute($itemnumber);
2628 =head2 AnonymiseIssueHistory
2630 $rows = AnonymiseIssueHistory($date,$borrowernumber)
2632 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
2633 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
2635 If c<$borrowernumber> is set, it will delete issue history for only that borrower, regardless of their opac privacy
2636 setting (force delete).
2638 return the number of affected rows.
2642 sub AnonymiseIssueHistory {
2644 my $borrowernumber = shift;
2645 my $dbh = C4::Context->dbh;
2648 SET borrowernumber = ?
2649 WHERE returndate < ?
2650 AND borrowernumber IS NOT NULL
2653 # The default of 0 does not work due to foreign key constraints
2654 # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
2655 my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
2656 my @bind_params = ($anonymouspatron, $date);
2657 if (defined $borrowernumber) {
2658 $query .= " AND borrowernumber = ?";
2659 push @bind_params, $borrowernumber;
2661 $query .= " AND (SELECT privacy FROM borrowers WHERE borrowers.borrowernumber=old_issues.borrowernumber) <> 0";
2663 my $sth = $dbh->prepare($query);
2664 $sth->execute(@bind_params);
2665 my $rows_affected = $sth->rows; ### doublecheck row count return function
2666 return $rows_affected;
2669 =head2 SendCirculationAlert
2671 Send out a C<check-in> or C<checkout> alert using the messaging system.
2679 Valid values for this parameter are: C<CHECKIN> and C<CHECKOUT>.
2683 Hashref of information about the item being checked in or out.
2687 Hashref of information about the borrower of the item.
2691 The branchcode from where the checkout or check-in took place.
2697 SendCirculationAlert({
2700 borrower => $borrower,
2706 sub SendCirculationAlert {
2708 my ($type, $item, $borrower, $branch) =
2709 ($opts->{type}, $opts->{item}, $opts->{borrower}, $opts->{branch});
2710 my %message_name = (
2711 CHECKIN => 'Item_Check_in',
2712 CHECKOUT => 'Item_Checkout',
2714 my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences({
2715 borrowernumber => $borrower->{borrowernumber},
2716 message_name => $message_name{$type},
2718 my $issues_table = ( $type eq 'CHECKOUT' ) ? 'issues' : 'old_issues';
2719 my $letter = C4::Letters::GetPreparedLetter (
2720 module => 'circulation',
2721 letter_code => $type,
2722 branchcode => $branch,
2724 $issues_table => $item->{itemnumber},
2725 'items' => $item->{itemnumber},
2726 'biblio' => $item->{biblionumber},
2727 'biblioitems' => $item->{biblionumber},
2728 'borrowers' => $borrower,
2729 'branches' => $branch,
2733 my @transports = @{ $borrower_preferences->{transports} };
2734 # warn "no transports" unless @transports;
2736 # warn "transport: $_";
2737 my $message = C4::Message->find_last_message($borrower, $type, $_);
2739 #warn "create new message";
2740 C4::Message->enqueue($letter, $borrower, $_);
2742 #warn "append to old message";
2743 $message->append($letter);
2751 =head2 updateWrongTransfer
2753 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
2755 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
2759 sub updateWrongTransfer {
2760 my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
2761 my $dbh = C4::Context->dbh;
2762 # first step validate the actual line of transfert .
2765 "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
2767 $sth->execute($FromLibrary,$itemNumber);
2770 # second step create a new line of branchtransfer to the right location .
2771 ModItemTransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
2773 #third step changing holdingbranch of item
2774 UpdateHoldingbranch($FromLibrary,$itemNumber);
2777 =head2 UpdateHoldingbranch
2779 $items = UpdateHoldingbranch($branch,$itmenumber);
2781 Simple methode for updating hodlingbranch in items BDD line
2785 sub UpdateHoldingbranch {
2786 my ( $branch,$itemnumber ) = @_;
2787 ModItem({ holdingbranch => $branch }, undef, $itemnumber);
2792 $newdatedue = CalcDateDue($startdate,$itemtype,$branchcode,$borrower);
2794 this function calculates the due date given the start date and configured circulation rules,
2795 checking against the holidays calendar as per the 'useDaysMode' syspref.
2796 C<$startdate> = C4::Dates object representing start date of loan period (assumed to be today)
2797 C<$itemtype> = itemtype code of item in question
2798 C<$branch> = location whose calendar to use
2799 C<$borrower> = Borrower object
2804 my ( $startdate, $itemtype, $branch, $borrower ) = @_;
2806 # loanlength now a href
2808 GetLoanLength( $borrower->{'categorycode'}, $itemtype, $branch );
2812 # if globalDueDate ON the datedue is set to that date
2813 if (C4::Context->preference('globalDueDate')
2814 && ( C4::Context->preference('globalDueDate') =~
2815 C4::Dates->regexp('syspref') )
2817 $datedue = dt_from_string(
2818 C4::Context->preference('globalDueDate'),
2819 C4::Context->preference('dateformat')
2823 # otherwise, calculate the datedue as normal
2824 if ( C4::Context->preference('useDaysMode') eq 'Days' )
2825 { # ignoring calendar
2827 DateTime->now( time_zone => C4::Context->tz() )
2828 ->truncate( to => 'minute' );
2829 if ( $loanlength->{lengthunit} eq 'hours' ) {
2830 $dt->add( hours => $loanlength->{issuelength} );
2832 $dt->add( days => $loanlength->{issuelength} );
2834 $dt->set_minute(59);
2841 if ($loanlength->{lengthunit} eq 'hours') {
2842 $dur = DateTime::Duration->new( hours => $loanlength->{issuelength});
2845 $dur = DateTime::Duration->new( days => $loanlength->{issuelength});
2847 if (ref $startdate ne 'DateTime' ) {
2848 $startdate = dt_from_string($startdate);
2850 my $calendar = Koha::Calendar->new( branchcode => $branch );
2851 $datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} );
2852 if ($loanlength->{lengthunit} eq 'days') {
2853 $datedue->set_hour(23);
2854 $datedue->set_minute(59);
2859 # if Hard Due Dates are used, retreive them and apply as necessary
2860 my ( $hardduedate, $hardduedatecompare ) =
2861 GetHardDueDate( $borrower->{'categorycode'}, $itemtype, $branch );
2862 if ($hardduedate) { # hardduedates are currently dates
2863 $hardduedate->truncate( to => 'minute' );
2864 $hardduedate->set_hour(23);
2865 $hardduedate->set_minute(59);
2866 my $cmp = DateTime->compare( $hardduedate, $datedue );
2868 # if the calculated due date is after the 'before' Hard Due Date (ceiling), override
2869 # if the calculated date is before the 'after' Hard Due Date (floor), override
2870 # if the hard due date is set to 'exactly', overrride
2871 if ( $hardduedatecompare == 0 || $hardduedatecompare == $cmp ) {
2872 $datedue = $hardduedate->clone;
2875 # in all other cases, keep the date due as it is
2878 # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
2879 if ( C4::Context->preference('ReturnBeforeExpiry') ) {
2880 my $expiry_dt = dt_from_string( $borrower->{dateexpiry}, 'iso' );
2881 $expiry_dt->set( hour => 23, minute => 59);
2882 if ( DateTime->compare( $datedue, $expiry_dt ) == 1 ) {
2883 $datedue = $expiry_dt->clone;
2891 =head2 CheckRepeatableHolidays
2893 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
2895 This function checks if the date due is a repeatable holiday
2897 C<$date_due> = returndate calculate with no day check
2898 C<$itemnumber> = itemnumber
2899 C<$branchcode> = localisation of issue
2903 sub CheckRepeatableHolidays{
2904 my($itemnumber,$week_day,$branchcode)=@_;
2905 my $dbh = C4::Context->dbh;
2906 my $query = qq|SELECT count(*)
2907 FROM repeatable_holidays
2910 my $sth = $dbh->prepare($query);
2911 $sth->execute($branchcode,$week_day);
2912 my $result=$sth->fetchrow;
2918 =head2 CheckSpecialHolidays
2920 $countspecial = CheckSpecialHolidays($years,$month,$day,$itemnumber,$branchcode);
2922 This function check if the date is a special holiday
2924 C<$years> = the years of datedue
2925 C<$month> = the month of datedue
2926 C<$day> = the day of datedue
2927 C<$itemnumber> = itemnumber
2928 C<$branchcode> = localisation of issue
2932 sub CheckSpecialHolidays{
2933 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
2934 my $dbh = C4::Context->dbh;
2935 my $query=qq|SELECT count(*)
2936 FROM `special_holidays`
2942 my $sth = $dbh->prepare($query);
2943 $sth->execute($years,$month,$day,$branchcode);
2944 my $countspecial=$sth->fetchrow ;
2946 return $countspecial;
2949 =head2 CheckRepeatableSpecialHolidays
2951 $countspecial = CheckRepeatableSpecialHolidays($month,$day,$itemnumber,$branchcode);
2953 This function check if the date is a repeatble special holidays
2955 C<$month> = the month of datedue
2956 C<$day> = the day of datedue
2957 C<$itemnumber> = itemnumber
2958 C<$branchcode> = localisation of issue
2962 sub CheckRepeatableSpecialHolidays{
2963 my ($month,$day,$itemnumber,$branchcode) = @_;
2964 my $dbh = C4::Context->dbh;
2965 my $query=qq|SELECT count(*)
2966 FROM `repeatable_holidays`
2971 my $sth = $dbh->prepare($query);
2972 $sth->execute($month,$day,$branchcode);
2973 my $countspecial=$sth->fetchrow ;
2975 return $countspecial;
2980 sub CheckValidBarcode{
2982 my $dbh = C4::Context->dbh;
2983 my $query=qq|SELECT count(*)
2987 my $sth = $dbh->prepare($query);
2988 $sth->execute($barcode);
2989 my $exist=$sth->fetchrow ;
2994 =head2 IsBranchTransferAllowed
2996 $allowed = IsBranchTransferAllowed( $toBranch, $fromBranch, $code );
2998 Code is either an itemtype or collection doe depending on the pref BranchTransferLimitsType
3002 sub IsBranchTransferAllowed {
3003 my ( $toBranch, $fromBranch, $code ) = @_;
3005 if ( $toBranch eq $fromBranch ) { return 1; } ## Short circuit for speed.
3007 my $limitType = C4::Context->preference("BranchTransferLimitsType");
3008 my $dbh = C4::Context->dbh;
3010 my $sth = $dbh->prepare("SELECT * FROM branch_transfer_limits WHERE toBranch = ? AND fromBranch = ? AND $limitType = ?");
3011 $sth->execute( $toBranch, $fromBranch, $code );
3012 my $limit = $sth->fetchrow_hashref();
3014 ## If a row is found, then that combination is not allowed, if no matching row is found, then the combination *is allowed*
3015 if ( $limit->{'limitId'} ) {
3022 =head2 CreateBranchTransferLimit
3024 CreateBranchTransferLimit( $toBranch, $fromBranch, $code );
3026 $code is either itemtype or collection code depending on what the pref BranchTransferLimitsType is set to.
3030 sub CreateBranchTransferLimit {
3031 my ( $toBranch, $fromBranch, $code ) = @_;
3033 my $limitType = C4::Context->preference("BranchTransferLimitsType");
3035 my $dbh = C4::Context->dbh;
3037 my $sth = $dbh->prepare("INSERT INTO branch_transfer_limits ( $limitType, toBranch, fromBranch ) VALUES ( ?, ?, ? )");
3038 $sth->execute( $code, $toBranch, $fromBranch );
3041 =head2 DeleteBranchTransferLimits
3043 DeleteBranchTransferLimits($frombranch);
3045 Deletes all the branch transfer limits for one branch
3049 sub DeleteBranchTransferLimits {
3051 my $dbh = C4::Context->dbh;
3052 my $sth = $dbh->prepare("DELETE FROM branch_transfer_limits WHERE fromBranch = ?");
3053 $sth->execute($branch);
3057 my ( $borrowernumber, $itemnum ) = @_;
3059 MarkIssueReturned( $borrowernumber, $itemnum );
3060 my $borrower = C4::Members::GetMember( 'borrowernumber'=>$borrowernumber );
3061 my $item = C4::Items::GetItem( $itemnum );
3062 my $old_note = ($item->{'paidfor'} && ($item->{'paidfor'} ne q{})) ? $item->{'paidfor'}.' / ' : q{};
3063 my @datearr = localtime(time);
3064 my $date = ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
3065 my $bor = "$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
3066 ModItem({ paidfor => $old_note."Paid for by $bor $date" }, undef, $itemnum);
3071 my ($itemnumber, $mark_returned, $charge_fee) = @_;
3073 my $dbh = C4::Context->dbh();
3074 my $sth=$dbh->prepare("SELECT issues.*,items.*,biblio.title
3076 JOIN items USING (itemnumber)
3077 JOIN biblio USING (biblionumber)
3078 WHERE issues.itemnumber=?");
3079 $sth->execute($itemnumber);
3080 my $issues=$sth->fetchrow_hashref();
3083 # if a borrower lost the item, add a replacement cost to the their record
3084 if ( my $borrowernumber = $issues->{borrowernumber} ){
3085 my $borrower = C4::Members::GetMemberDetails( $borrowernumber );
3087 C4::Accounts::chargelostitem($borrowernumber, $itemnumber, $issues->{'replacementprice'}, "Lost Item $issues->{'title'} $issues->{'barcode'}")
3089 #FIXME : Should probably have a way to distinguish this from an item that really was returned.
3090 #warn " $issues->{'borrowernumber'} / $itemnumber ";
3091 MarkIssueReturned($borrowernumber,$itemnumber,undef,undef,$borrower->{'privacy'}) if $mark_returned;
3095 sub GetOfflineOperations {
3096 my $dbh = C4::Context->dbh;
3097 my $sth = $dbh->prepare("SELECT * FROM pending_offline_operations WHERE branchcode=? ORDER BY timestamp");
3098 $sth->execute(C4::Context->userenv->{'branch'});
3099 my $results = $sth->fetchall_arrayref({});
3104 sub GetOfflineOperation {
3105 my $dbh = C4::Context->dbh;
3106 my $sth = $dbh->prepare("SELECT * FROM pending_offline_operations WHERE operationid=?");
3107 $sth->execute( shift );
3108 my $result = $sth->fetchrow_hashref;
3113 sub AddOfflineOperation {
3114 my $dbh = C4::Context->dbh;
3115 my $sth = $dbh->prepare("INSERT INTO pending_offline_operations (userid, branchcode, timestamp, action, barcode, cardnumber) VALUES(?,?,?,?,?,?)");
3116 $sth->execute( @_ );
3120 sub DeleteOfflineOperation {
3121 my $dbh = C4::Context->dbh;
3122 my $sth = $dbh->prepare("DELETE FROM pending_offline_operations WHERE operationid=?");
3123 $sth->execute( shift );
3127 sub ProcessOfflineOperation {
3128 my $operation = shift;
3131 if ( $operation->{action} eq 'return' ) {
3132 $report = ProcessOfflineReturn( $operation );
3133 } elsif ( $operation->{action} eq 'issue' ) {
3134 $report = ProcessOfflineIssue( $operation );
3137 DeleteOfflineOperation( $operation->{operationid} ) if $operation->{operationid};
3142 sub ProcessOfflineReturn {
3143 my $operation = shift;
3145 my $itemnumber = C4::Items::GetItemnumberFromBarcode( $operation->{barcode} );
3147 if ( $itemnumber ) {
3148 my $issue = GetOpenIssue( $itemnumber );
3151 $issue->{borrowernumber},
3154 $operation->{timestamp},
3157 { renewals => 0, onloan => undef },
3158 $issue->{'biblionumber'},
3163 return "Item not issued.";
3166 return "Item not found.";
3170 sub ProcessOfflineIssue {
3171 my $operation = shift;
3173 my $borrower = C4::Members::GetMemberDetails( undef, $operation->{cardnumber} ); # Get borrower from operation cardnumber
3175 if ( $borrower->{borrowernumber} ) {
3176 my $itemnumber = C4::Items::GetItemnumberFromBarcode( $operation->{barcode} );
3177 unless ($itemnumber) {
3178 return "Barcode not found.";
3180 my $issue = GetOpenIssue( $itemnumber );
3182 if ( $issue and ( $issue->{borrowernumber} ne $borrower->{borrowernumber} ) ) { # Item already issued to another borrower, mark it returned
3184 $issue->{borrowernumber},
3187 $operation->{timestamp},
3192 $operation->{'barcode'},
3195 $operation->{timestamp},
3200 return "Borrower not found.";
3208 TransferSlip($user_branch, $itemnumber, $to_branch)
3210 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
3215 my ($branch, $itemnumber, $to_branch) = @_;
3217 my $item = GetItem( $itemnumber )
3220 my $pulldate = C4::Dates->new();
3222 return C4::Letters::GetPreparedLetter (
3223 module => 'circulation',
3224 letter_code => 'TRANSFERSLIP',
3225 branchcode => $branch,
3227 'branches' => $to_branch,
3228 'biblio' => $item->{biblionumber},
3241 Koha Development Team <http://koha-community.org/>