Bug 18913: Allow symbolic link in /etc/koha/sites
[koha.git] / Koha / Hold.pm
index 871f3e1..949770d 100644 (file)
@@ -1,6 +1,7 @@
 package Koha::Hold;
 
 # Copyright ByWater Solutions 2014
+# Copyright 2017 Koha Development team
 #
 # This file is part of Koha.
 #
@@ -20,14 +21,18 @@ package Koha::Hold;
 use Modern::Perl;
 
 use Carp;
+use Data::Dumper qw(Dumper);
 
 use C4::Context qw(preference);
+use C4::Log;
 
-use Koha::DateUtils qw(dt_from_string);
-use Koha::Borrowers;
+use Koha::DateUtils qw(dt_from_string output_pref);
+use Koha::Patrons;
 use Koha::Biblios;
 use Koha::Items;
 use Koha::Libraries;
+use Koha::Old::Holds;
+use Koha::Calendar;
 
 use base qw(Koha::Object);
 
@@ -41,28 +46,142 @@ Koha::Hold - Koha Hold object class
 
 =cut
 
-=head3 waiting_expires_on
+=head3 age
 
-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' ).
+returns the number of days since a hold was placed, optionally
+using the calendar
+
+my $age = $hold->age( $use_calendar );
 
 =cut
 
-sub waiting_expires_on {
-    my ($self) = @_;
+sub age {
+    my ( $self, $use_calendar ) = @_;
 
-    my $found = $self->found;
-    return unless $found && $found eq 'W';
+    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 );
+
+=cut
+
+sub suspend_hold {
+    my ( $self, $dt ) = @_;
+
+    $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
+
+    if ( $self->is_waiting ) {    # We can't suspend waiting holds
+        carp "Unable to suspend waiting hold!";
+        return $self;
+    }
+
+    $self->suspend(1);
+    $self->suspend_until( $dt );
+
+    $self->store();
+
+    logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
+        if C4::Context->preference('HoldsLog');
+
+    return $self;
+}
+
+=head3 resume
+
+my $hold = $hold->resume();
+
+=cut
+
+sub resume {
+    my ( $self ) = @_;
+
+    $self->suspend(0);
+    $self->suspend_until( undef );
+
+    $self->store();
+
+    logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
+        if C4::Context->preference('HoldsLog');
+
+    return $self;
+}
+
+=head3 delete
+
+$hold->delete();
+
+=cut
+
+sub delete {
+    my ( $self ) = @_;
+
+    my $deleted = $self->SUPER::delete($self);
+
+    logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
+        if C4::Context->preference('HoldsLog');
 
-    my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
-    return unless $ReservesMaxPickUpDelay;
+    return $deleted;
+}
+
+=head3 set_waiting
+
+=cut
+
+sub set_waiting {
+    my ( $self, $transferToDo ) = @_;
+
+    $self->priority(0);
+
+    if ($transferToDo) {
+        $self->found('T')->store();
+        return $self;
+    }
+
+    my $today = dt_from_string();
+    my $values = {
+        found => 'W',
+        waitingdate => $today->ymd,
+    };
+
+    my $requested_expiration;
+    if ($self->expirationdate) {
+        $requested_expiration = dt_from_string($self->expirationdate);
+    }
 
-    my $dt = dt_from_string( $self->waitingdate() );
+    my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
+    my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+    my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
 
-    $dt->add( days => $ReservesMaxPickUpDelay );
+    my $expirationdate = $today->clone;
+    $expirationdate->add(days => $max_pickup_delay);
 
-    return $dt;
+    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;
 }
 
 =head3 is_found
@@ -182,30 +301,97 @@ sub branch {
 
 =head3 borrower
 
-Returns the related Koha::Borrower object for this Hold
+Returns the related Koha::Patron object for this Hold
 
 =cut
 
+# FIXME Should be renamed with ->patron
 sub borrower {
     my ($self) = @_;
 
-    $self->{_borrower} ||= Koha::Borrowers->find( $self->borrowernumber() );
+    $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
 
     return $self->{_borrower};
 }
 
+=head3 is_suspended
+
+my $bool = $hold->is_suspended();
+
+=cut
+
+sub is_suspended {
+    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
 
-sub type {
+sub _type {
     return 'Reserve';
 }
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Kyle M Hall <kyle@bywatersolutions.com>
 
+Jonathan Druart <jonathan.druart@bugs.koha-community.org>
+
 =cut
 
 1;