use Koha::CirculationRules;
use Koha::Database;
use Koha::DateUtils qw( dt_from_string output_pref );
-use Koha::Hold;
use Koha::Holds;
use Koha::ItemTypes;
use Koha::Items;
use Koha::Libraries;
-use Koha::Old::Hold;
+use Koha::Old::Holds;
use Koha::Patrons;
use Koha::Plugins;
my $itemtype = $params->{itemtype};
my $non_priority = $params->{non_priority};
- $resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
- or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
-
- $patron_expiration_date = output_pref({ str => $patron_expiration_date, dateonly => 1, dateformat => 'iso' });
+ $resdate ||= dt_from_string;
# if we have an item selectionned, and the pickup branch is the same as the holdingbranch
# of the document, we force the value $priority and $found .
$found = 'W';
}
}
-
- if ( C4::Context->preference('AllowHoldDateInFuture') ) {
-
- # Make room in reserves for this before those of a later reserve date
- $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
+ if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
+ # Make room in reserves for this if passed a priority
+ $priority = _ShiftPriority( $biblionumber, $priority );
}
my $waitingdate;
{
biblio_ids => [ $biblionumber ]
}
- );
+ ) if C4::Context->preference('RealTimeHoldsQueue');
return $reserve_id;
}
return { status =>'alreadypossession' };
}
+ if ( $params->{itemtype} ) {
+
+ # biblio-level, item type-contrained
+ my $patron = Koha::Patrons->find($borrowernumber);
+ my $reservesallowed = Koha::CirculationRules->get_effective_rule(
+ {
+ itemtype => $params->{itemtype},
+ categorycode => $patron->categorycode,
+ branchcode => $pickup_branchcode,
+ rule_name => 'reservesallowed',
+ }
+ )->rule_value;
+
+ $reservesallowed = ( $reservesallowed eq '' ) ? undef : $reservesallowed;
+
+ my $count = $patron->holds->search(
+ {
+ '-or' => [
+ { 'me.itemtype' => $params->{itemtype} },
+ { 'item.itype' => $params->{itemtype} }
+ ]
+ },
+ {
+ join => ['item']
+ }
+ )->count;
+
+ return { status => '' }
+ if defined $reservesallowed and $reservesallowed < $count + 1;
+ }
+
my $items;
#get items linked via host records
my @hostitemnumbers = get_hostitemnumbers_of($biblionumber);
# check if a recall exists on this item from this borrower
return { status => 'recall' }
- if $patron->recalls->filter_by_current->search({ itemnumber => $item->itemnumber })->count;
+ if $patron->recalls->filter_by_current->search({ item_id => $item->itemnumber })->count;
my $controlbranch = C4::Context->preference('ReservesControlBranch');
# If using item-level itypes, fall back to the record
# level itemtype if the hold has no associated item
- $querycount .=
- C4::Context->preference('item-level_itypes')
- ? " AND COALESCE( items.itype, biblioitems.itemtype ) = ?"
- : " AND biblioitems.itemtype = ?"
- if defined $ruleitemtype;
+ if ( defined $ruleitemtype ) {
+ if ( C4::Context->preference('item-level_itypes') ) {
+ $querycount .= q{
+ AND ( COALESCE( items.itype, biblioitems.itemtype ) = ?
+ OR reserves.itemtype = ? )
+ };
+ }
+ else {
+ $querycount .= q{
+ AND ( biblioitems.itemtype = ?
+ OR reserves.itemtype = ? )
+ };
+ }
+ }
my $sthcount = $dbh->prepare($querycount);
if ( defined $ruleitemtype ) {
- $sthcount->execute( $patron->borrowernumber, $reserves_control_branch, $ruleitemtype );
+ $sthcount->execute( $patron->borrowernumber, $reserves_control_branch, $ruleitemtype, $ruleitemtype );
}
else {
$sthcount->execute( $patron->borrowernumber, $reserves_control_branch );
my ( $notissued, $reserved );
( $notissued ) = $dbh->selectrow_array( $issue_qry, undef,
( $biblionumber ) );
- if( $notissued ) {
+ if( $notissued == 0 ) {
+ # all items are issued
( $reserved ) = $dbh->selectrow_array( $holds_qry, undef,
( $biblionumber, $borrowernumber ) );
$fee = 0 if $reserved == 0;
+ } else {
+ $fee = 0;
}
}
return $fee;
if ( defined($hold->found) && $hold->found eq 'W' ) {
$cancel_params->{charge_cancel_fee} = 1;
}
+ $cancel_params->{autofill} = C4::Context->preference('ExpireReservesAutoFill');
$hold->cancel( $cancel_params );
}
}
if C4::Context->preference('HoldsLog');
# The only column that can be updated for a found hold is the expiration date
- $hold->expirationdate(dt_from_string($date))->store();
+ $hold->expirationdate($date)->store();
}
elsif ($rank =~ /^\d+/ and $rank > 0) {
logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, $hold )
if ( defined( $suspend_until ) ) {
if ( $suspend_until ) {
- $suspend_until = eval { dt_from_string( $suspend_until ) };
$hold->suspend_hold( $suspend_until );
} else {
# If the hold is suspended leave the hold suspended, but convert it to an indefinite hold.
=head2 ModReserveAffect
- &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id, $desk_id);
+ &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id, $desk_id, $notify_library);
This function affect an item and a status for a given reserve, either fetched directly
by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber
=cut
sub ModReserveAffect {
- my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id, $desk_id ) = @_;
+ my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id, $desk_id, $notify_library ) = @_;
my $dbh = C4::Context->dbh;
# we want to attach $itemnumber to $borrowernumber, find the biblionumber
$transfer->receive if $transfer;
}
+ _koha_notify_hold_changed( $hold ) if $notify_library;
+
_FixPriority( { biblionumber => $biblionumber } );
my $item = Koha::Items->find($itemnumber);
if ( $item->location && $item->location eq 'CART'
_FixPriority({ reserve_id => $reserve_id, rank => $last_priority });
}
+ Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+ {
+ biblio_ids => [ $hold->biblionumber ]
+ }
+ ) if C4::Context->preference('RealTimeHoldsQueue');
# FIXME Should return the new priority
}
sub ToggleSuspend {
my ( $reserve_id, $suspend_until ) = @_;
- $suspend_until = dt_from_string($suspend_until) if ($suspend_until);
-
my $hold = Koha::Holds->find( $reserve_id );
if ( $hold->is_suspended ) {
my $suspend_until = $params{'suspend_until'} || undef;
my $suspend = defined( $params{'suspend'} ) ? $params{'suspend'} : 1;
- $suspend_until = eval { dt_from_string($suspend_until) }
- if ( defined($suspend_until) );
-
return unless ( $borrowernumber || $biblionumber );
my $params;
sub _koha_notify_reserve {
my $reserve_id = shift;
+
my $hold = Koha::Holds->find($reserve_id);
my $borrowernumber = $hold->borrowernumber;
} );
my $library = Koha::Libraries->find( $hold->branchcode );
- my $admin_email_address = $library->from_email_address;
- $library = $library->unblessed;
+ my $inbound_email_address = $library->inbound_email_address;
my %letter_params = (
module => 'reserves',
branchcode => $hold->branchcode,
lang => $patron->lang,
tables => {
- 'branches' => $library,
+ 'branches' => $library->unblessed,
'borrowers' => $patron->unblessed,
'biblio' => $hold->biblionumber,
'biblioitems' => $hold->biblionumber,
C4::Letters::EnqueueLetter( {
letter => $letter,
borrowernumber => $borrowernumber,
- from_address => $admin_email_address,
+ from_address => $inbound_email_address,
message_transport_type => $mtt,
} );
};
}
-=head2 _ShiftPriorityByDateAndPriority
+=head2 _koha_notify_hold_changed
+
+ _koha_notify_hold_changed( $hold_object );
+
+=cut
+
+sub _koha_notify_hold_changed {
+ my $hold = shift;
+
+ my $patron = $hold->patron;
+ my $library = $hold->branch;
+
+ my $letter = C4::Letters::GetPreparedLetter(
+ module => 'reserves',
+ letter_code => 'HOLD_CHANGED',
+ branchcode => $hold->branchcode,
+ substitute => { today => output_pref( dt_from_string ) },
+ tables => {
+ 'branches' => $library->unblessed,
+ 'borrowers' => $patron->unblessed,
+ 'biblio' => $hold->biblionumber,
+ 'biblioitems' => $hold->biblionumber,
+ 'reserves' => $hold->unblessed,
+ 'items' => $hold->itemnumber,
+ },
+ );
+
+ return unless $letter;
- $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
+ my $email =
+ C4::Context->preference('ExpireReservesAutoFillEmail')
+ || $library->inbound_email_address;
+
+ C4::Letters::EnqueueLetter(
+ {
+ letter => $letter,
+ borrowernumber => $patron->id,
+ message_transport_type => 'email',
+ from_address => $email,
+ to_address => $email,
+ }
+ );
+}
+
+=head2 _ShiftPriority
+
+ $new_priority = _ShiftPriority( $biblionumber, $priority );
This increments the priority of all reserves after the one
with either the lowest date after C<$reservedate>
=cut
-sub _ShiftPriorityByDateAndPriority {
- my ( $biblio, $resdate, $new_priority ) = @_;
+sub _ShiftPriority {
+ my ( $biblio, $new_priority ) = @_;
my $dbh = C4::Context->dbh;
- my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
+ my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND priority > ? ORDER BY priority ASC LIMIT 1";
my $sth = $dbh->prepare( $query );
- $sth->execute( $biblio, $resdate, $new_priority );
+ $sth->execute( $biblio, $new_priority );
my $min_priority = $sth->fetchrow;
# if no such matches are found, $new_priority remains as original value
$new_priority = $min_priority if ( $min_priority );
_FixPriority( { biblionumber => $hold->biblionumber } );
+ Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+ {
+ biblio_ids => [ $hold->biblionumber ]
+ }
+ ) if C4::Context->preference('RealTimeHoldsQueue');
+
+
return $hold;
}
the parameter value.
After calculation of this priority, it is recommended to call
-_ShiftPriorityByDateAndPriority. Note that this is currently done in
+_ShiftPriority. Note that this is currently done in
AddReserves.
=cut