use Modern::Perl;
-use Carp;
-use Data::Dumper qw(Dumper);
+use Data::Dumper qw( Dumper );
+use List::MoreUtils qw( any );
-use C4::Context qw(preference);
-use C4::Letters;
-use C4::Log;
+use C4::Context;
+use C4::Letters qw( GetPreparedLetter EnqueueLetter );
+use C4::Log qw( logaction );
use Koha::AuthorisedValues;
-use Koha::DateUtils qw(dt_from_string output_pref);
+use Koha::DateUtils qw( dt_from_string );
use Koha::Patrons;
use Koha::Biblios;
use Koha::Items;
=head1 API
-=head2 Class Methods
+=head2 Class methods
=cut
elsif ( $self->is_in_transit ) {
Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
}
+ elsif ( $self->is_in_processing ) {
+ Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'P' );
+ }
else {
Koha::Exceptions::Hold::CannotSuspendFound->throw(
'Unhandled data exception on found hold (id='
=cut
sub set_waiting {
- my ( $self ) = @_;
+ my ( $self, $desk_id ) = @_;
$self->priority(0);
my $values = {
found => 'W',
waitingdate => $today->ymd,
+ desk_id => $desk_id,
};
my $requested_expiration;
return $self;
}
+=head3 is_pickup_location_valid
+
+ if ($hold->is_pickup_location_valid({ library_id => $library->id }) ) {
+ ...
+ }
+
+Returns a I<boolean> representing if the passed pickup location is valid for the hold.
+It throws a I<Koha::Exceptions::_MissingParameter> if the library_id parameter is not
+passed.
+
+=cut
+
+sub is_pickup_location_valid {
+ my ( $self, $params ) = @_;
+
+ Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
+ unless $params->{library_id};
+
+ my @pickup_locations;
+
+ if ( $self->itemnumber ) { # item-level
+ @pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
+ }
+ else { # biblio-level
+ @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
+ }
+
+ return any { $_->branchcode eq $params->{library_id} } @pickup_locations;
+}
+
+=head3 set_pickup_location
+
+ $hold->set_pickup_location(
+ {
+ library_id => $library->id,
+ [ force => 0|1 ]
+ }
+ );
+
+Updates the hold pickup location. It throws a I<Koha::Exceptions::Hold::InvalidPickupLocation> if
+the passed pickup location is not valid.
+
+Note: It is up to the caller to verify if I<AllowHoldPolicyOverride> is set when setting the
+B<force> parameter.
+
+=cut
+
+sub set_pickup_location {
+ my ( $self, $params ) = @_;
+
+ Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
+ unless $params->{library_id};
+
+ if (
+ $params->{force}
+ || $self->is_pickup_location_valid(
+ { library_id => $params->{library_id} }
+ )
+ )
+ {
+ # all good, set the new pickup location
+ $self->branchcode( $params->{library_id} )->store;
+ }
+ else {
+ Koha::Exceptions::Hold::InvalidPickupLocation->throw;
+ }
+
+ return $self;
+}
+
+=head3 set_processing
+
+$hold->set_processing;
+
+Mark the hold as in processing.
+
+=cut
+
+sub set_processing {
+ my ( $self ) = @_;
+
+ $self->priority(0);
+ $self->found('P');
+ $self->store();
+
+ return $self;
+}
+
=head3 is_found
-Returns true if hold is a waiting or in transit
+Returns true if hold is waiting, in transit or in processing
=cut
return 0 unless $self->found();
return 1 if $self->found() eq 'W';
return 1 if $self->found() eq 'T';
+ return 1 if $self->found() eq 'P';
}
=head3 is_waiting
return $self->found() eq 'T';
}
+=head3 is_in_processing
+
+Returns true if hold is a in_processing hold
+
+=cut
+
+sub is_in_processing {
+ my ($self) = @_;
+
+ return 0 unless $self->found();
+ return $self->found() eq 'P';
+}
+
=head3 is_cancelable_from_opac
Returns true if hold is a cancelable hold
my ($self) = @_;
return 1 unless $self->is_found();
- return 0; # if ->is_in_transit or if ->is_waiting
+ return 0; # if ->is_in_transit or if ->is_waiting or ->is_in_processing
}
=head3 is_at_destination
return $self->{_biblio};
}
+=head3 patron
+
+Returns the related Koha::Patron object for this hold
+
+=cut
+
+sub patron {
+ my ($self) = @_;
+
+ my $patron_rs = $self->_result->patron;
+ return Koha::Patron->_new_from_dbic($patron_rs);
+}
+
=head3 item
Returns the related Koha::Item object for this Hold
return $self->{_branch};
}
+=head3 desk
+
+Returns the related Koha::Desk object for this Hold
+
+=cut
+
+sub desk {
+ my $self = shift;
+ my $desk_rs = $self->_result->desk;
+ return unless $desk_rs;
+ return Koha::Desk->_new_from_dbic($desk_rs);
+}
+
=head3 borrower
Returns the related Koha::Patron object for this Hold
return $self;
}
+=head3 store
+
+Override base store method to set default
+expirationdate for holds.
+
+=cut
+
+sub store {
+ my ($self) = @_;
+
+ if ( !$self->in_storage ) {
+ if (
+ C4::Context->preference('DefaultHoldExpirationdate')
+ and ( not defined $self->expirationdate
+ or $self->expirationdate eq '' )
+ )
+ {
+ $self->_set_default_expirationdate;
+ }
+ }
+ else {
+
+ my %updated_columns = $self->_result->get_dirty_columns;
+ return $self->SUPER::store unless %updated_columns;
+
+ if ( exists $updated_columns{reservedate} ) {
+ if (
+ C4::Context->preference('DefaultHoldExpirationdate')
+ and ( not exists $updated_columns{expirationdate}
+ or exists $updated_columns{expirationdate}
+ and $updated_columns{expirationdate} eq '' )
+ )
+ {
+ $self->_set_default_expirationdate;
+ }
+ }
+ }
+
+ $self = $self->SUPER::store;
+}
+
+sub _set_default_expirationdate {
+ my $self = shift;
+
+ my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod') || 0;
+ my $timeunit =
+ C4::Context->preference('DefaultHoldExpirationdateUnitOfTime') || 'days';
+
+ $self->expirationdate(
+ dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
+}
+
=head3 _move_to_old
my $is_moved = $hold->_move_to_old;