Bug 18382: (QA follow-up) Don't set suspend_until if dt if undefined
[koha_ffzg] / Koha / Hold.pm
index d708054..e76b9fb 100644 (file)
@@ -1,6 +1,7 @@
 package Koha::Hold;
 
 # Copyright ByWater Solutions 2014
+# Copyright 2017 Koha Development team
 #
 # This file is part of Koha.
 #
@@ -30,6 +31,8 @@ use Koha::Patrons;
 use Koha::Biblios;
 use Koha::Items;
 use Koha::Libraries;
+use Koha::Old::Holds;
+use Koha::Calendar;
 
 use base qw(Koha::Object);
 
@@ -43,6 +46,34 @@ Koha::Hold - Koha Hold object class
 
 =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 );
@@ -60,8 +91,7 @@ sub suspend_hold {
     }
 
     $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) )
@@ -274,6 +304,7 @@ Returns the related Koha::Patron object for this Hold
 
 =cut
 
+# FIXME Should be renamed with ->patron
 sub borrower {
     my ($self) = @_;
 
@@ -294,6 +325,58 @@ sub is_suspended {
     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
@@ -302,10 +385,12 @@ sub _type {
     return 'Reserve';
 }
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Kyle M Hall <kyle@bywatersolutions.com>
 
+Jonathan Druart <jonathan.druart@bugs.koha-community.org>
+
 =cut
 
 1;