package Koha::Hold;
# Copyright ByWater Solutions 2014
+# Copyright 2017 Koha Development team
#
# This file is part of Koha.
#
use Koha::Biblios;
use Koha::Items;
use Koha::Libraries;
+use Koha::Old::Holds;
+use Koha::Calendar;
use base qw(Koha::Object);
=cut
+=head3 age
+
+returns the number of days since a hold was placed, optionally
+using the calendar
+
+my $age = $hold->age( $use_calendar );
+
+=cut
+
+sub age {
+ my ( $self, $use_calendar ) = @_;
+
+ my $today = dt_from_string;
+ my $age;
+
+ if ( $use_calendar ) {
+ my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
+ $age = $calendar->days_between( dt_from_string( $self->reservedate ), $today );
+ }
+ else {
+ $age = $today->delta_days( dt_from_string( $self->reservedate ) );
+ }
+
+ $age = $age->in_units( 'days' );
+
+ return $age;
+}
+
=head3 suspend_hold
my $hold = $hold->suspend_hold( $suspend_until_dt );
}
$self->suspend(1);
- $self->suspend_until( $dt );
-
+ $self->suspend_until( $dt->datetime ) if ( defined $dt );
$self->store();
logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
return $deleted;
}
-=head3 waiting_expires_on
-
-Returns a DateTime for the date a waiting holds expires on.
-Returns undef if the system peference ReservesMaxPickUpDelay is not set.
-Returns undef if the hold is not waiting ( found = 'W' ).
-
-=cut
-
-sub waiting_expires_on {
- my ($self) = @_;
-
- my $found = $self->found;
- return unless $found && $found eq 'W';
-
- my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
- return unless $ReservesMaxPickUpDelay;
-
- my $dt = dt_from_string( $self->waitingdate() );
-
- $dt->add( days => $ReservesMaxPickUpDelay );
-
- return $dt;
-}
-
=head3 set_waiting
=cut
waitingdate => $today->ymd,
};
- if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
- my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
- my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
- my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
+ my $requested_expiration;
+ if ($self->expirationdate) {
+ $requested_expiration = dt_from_string($self->expirationdate);
+ }
- my $expirationdate = $today->clone;
- $expirationdate->add(days => $max_pickup_delay);
+ my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
+ my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+ my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
- if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
- $expirationdate = $calendar->days_forward( dt_from_string($self->waitingdate), $max_pickup_delay );
- }
+ my $expirationdate = $today->clone;
+ $expirationdate->add(days => $max_pickup_delay);
- $values->{expirationdate} = $expirationdate->ymd;
+ if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
+ $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
}
+ # If patron's requested expiration date is prior to the
+ # calculated one, we keep the patron's one.
+ my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
+ $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
+
$self->set($values)->store();
return $self;
=cut
+# FIXME Should be renamed with ->patron
sub borrower {
my ($self) = @_;
return $self->suspend();
}
+
+=head3 cancel
+
+my $cancel_hold = $hold->cancel();
+
+Cancel a hold:
+- The hold will be moved to the old_reserves table with a priority=0
+- The priority of other holds will be updated
+- The patron will be charge (see ExpireReservesMaxPickUpDelayCharge) if the charge_cancel_fee parameter is set
+- a CANCEL HOLDS log will be done if the pref HoldsLog is on
+
+=cut
+
+sub cancel {
+ my ( $self, $params ) = @_;
+ $self->_result->result_source->schema->txn_do(
+ sub {
+ $self->cancellationdate(dt_from_string);
+ $self->priority(0);
+ $self->_move_to_old;
+ $self->SUPER::delete(); # Do not add a DELETE log
+
+ # now fix the priority on the others....
+ C4::Reserves::_FixPriority({ biblionumber => $self->biblionumber });
+
+ # and, if desired, charge a cancel fee
+ my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
+ if ( $charge && $params->{'charge_cancel_fee'} ) {
+ C4::Accounts::manualinvoice($self->borrowernumber, $self->itemnumber, '', 'HE', $charge);
+ }
+
+ C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
+ if C4::Context->preference('HoldsLog');
+ }
+ );
+ return $self;
+}
+
+=head3 _move_to_old
+
+my $is_moved = $hold->_move_to_old;
+
+Move a hold to the old_reserve table following the same pattern as Koha::Patron->move_to_deleted
+
+=cut
+
+sub _move_to_old {
+ my ($self) = @_;
+ my $hold_infos = $self->unblessed;
+ return Koha::Old::Hold->new( $hold_infos )->store;
+}
+
=head3 type
=cut
return 'Reserve';
}
-=head1 AUTHOR
+=head1 AUTHORS
Kyle M Hall <kyle@bywatersolutions.com>
+Jonathan Druart <jonathan.druart@bugs.koha-community.org>
+
=cut
1;