Bug 24159: Set days_mode according to circ rules in 3 other places
[srvgit] / Koha / Hold.pm
index 56780f8..700c855 100644 (file)
@@ -5,18 +5,18 @@ package Koha::Hold;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
@@ -34,6 +34,8 @@ use Koha::Libraries;
 use Koha::Old::Holds;
 use Koha::Calendar;
 
+use Koha::Exceptions::Hold;
+
 use base qw(Koha::Object);
 
 =head1 NAME
@@ -83,22 +85,30 @@ my $hold = $hold->suspend_hold( $suspend_until_dt );
 sub suspend_hold {
     my ( $self, $dt ) = @_;
 
-    $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
+    my $date = $dt ? $dt->clone()->truncate( to => 'day' )->datetime : undef;
 
-    if ( $self->is_waiting ) {    # We can't suspend waiting holds
-        carp "Unable to suspend waiting hold!";
-        return $self;
+    if ( $self->is_found ) {    # We can't suspend found holds
+        if ( $self->is_waiting ) {
+            Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' );
+        }
+        elsif ( $self->is_in_transit ) {
+            Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'T' );
+        }
+        else {
+            Koha::Exceptions::Hold::CannotSuspendFound->throw(
+                      'Unhandled data exception on found hold (id='
+                    . $self->id
+                    . ', found='
+                    . $self->found
+                    . ')' );
+        }
     }
 
     $self->suspend(1);
-    if ( defined $dt ){
-        $self->suspend_until( $dt->ymd );
-    } else {
-        $self->suspend_until( $dt );
-    }
+    $self->suspend_until($date);
     $self->store();
 
-    logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
+    logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
         if C4::Context->preference('HoldsLog');
 
     return $self;
@@ -168,12 +178,21 @@ sub set_waiting {
 
     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 $expirationdate = $today->clone;
     $expirationdate->add(days => $max_pickup_delay);
 
     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
+        my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
+        my $useDaysMode_value = Koha::CirculationRules->get_useDaysMode_effective_value(
+            {
+                categorycode => $self->borrower->categorycode,
+                itemtype     => $itemtype,
+                branchcode   => $self->branchcode,
+            }
+        );
+        my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $useDaysMode_value );
+
         $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
     }
 
@@ -227,23 +246,21 @@ sub is_in_transit {
     return $self->found() eq 'T';
 }
 
-=head3 is_cancelable
+=head3 is_cancelable_from_opac
 
 Returns true if hold is a cancelable hold
 
-Holds may be canceled if they not found, or
-are found and waiting. A hold found but in
-transit cannot be canceled.
+Holds may be only canceled if they are not found.
+
+This is used from the OPAC.
 
 =cut
 
-sub is_cancelable {
+sub is_cancelable_from_opac {
     my ($self) = @_;
 
     return 1 unless $self->is_found();
-    return 0 if $self->is_in_transit();
-    return 1 if $self->is_waiting();
-    return 0;
+    return 0; # if ->is_in_transit or if ->is_waiting
 }
 
 =head3 is_at_destination
@@ -346,7 +363,7 @@ sub cancel {
     my ( $self, $params ) = @_;
     $self->_result->result_source->schema->txn_do(
         sub {
-            $self->cancellationdate(dt_from_string);
+            $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) );
             $self->priority(0);
             $self->_move_to_old;
             $self->SUPER::delete(); # Do not add a DELETE log
@@ -357,7 +374,18 @@ sub cancel {
             # 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);
+                my $account =
+                  Koha::Account->new( { patron_id => $self->borrowernumber } );
+                $account->add_debit(
+                    {
+                        amount     => $charge,
+                        user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                        interface  => C4::Context->interface,
+                        library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
+                        type       => 'RESERVE_EXPIRED',
+                        item_id    => $self->itemnumber
+                    }
+                );
             }
 
             C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
@@ -381,7 +409,39 @@ sub _move_to_old {
     return Koha::Old::Hold->new( $hold_infos )->store;
 }
 
-=head3 type
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Hold object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        reserve_id       => 'hold_id',
+        borrowernumber   => 'patron_id',
+        reservedate      => 'hold_date',
+        biblionumber     => 'biblio_id',
+        branchcode       => 'pickup_library_id',
+        notificationdate => undef,
+        reminderdate     => undef,
+        cancellationdate => 'cancellation_date',
+        reservenotes     => 'notes',
+        found            => 'status',
+        itemnumber       => 'item_id',
+        waitingdate      => 'waiting_date',
+        expirationdate   => 'expiration_date',
+        lowestPriority   => 'lowest_priority',
+        suspend          => 'suspended',
+        suspend_until    => 'suspended_until',
+        itemtype         => 'item_type',
+        item_level_hold  => 'item_level',
+    };
+}
+
+=head2 Internal methods
+
+=head3 _type
 
 =cut
 
@@ -392,8 +452,8 @@ sub _type {
 =head1 AUTHORS
 
 Kyle M Hall <kyle@bywatersolutions.com>
-
 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
+Martin Renvoize <martin.renvoize@ptfs-europe.com>
 
 =cut