Bug 29869: Add Koha::Hold->fill
[koha-ffzg.git] / Koha / Hold.pm
index 9736dc5..4660521 100644 (file)
@@ -179,22 +179,17 @@ sub set_waiting {
     $self->priority(0);
 
     my $today = dt_from_string();
+
     my $values = {
         found => 'W',
-        waitingdate => $today->ymd,
+        ( !$self->waitingdate ? ( waitingdate => $today->ymd ) : () ),
         desk_id => $desk_id,
     };
 
-    my $requested_expiration;
-    if ($self->expirationdate) {
-        $requested_expiration = dt_from_string($self->expirationdate);
-    }
-
     my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
 
-    my $expirationdate = $today->clone;
-    $expirationdate->add(days => $max_pickup_delay);
+    my $new_expiration_date = $today->clone->add(days => $max_pickup_delay);
 
     if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
         my $itemtype = $self->item ? $self->item->effective_itemtype : $self->biblio->itemtype;
@@ -207,13 +202,24 @@ sub set_waiting {
         );
         my $calendar = Koha::Calendar->new( branchcode => $self->branchcode, days_mode => $daysmode );
 
-        $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
+        $new_expiration_date = $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;
+    if ( $self->patron_expiration_date ) {
+        my $requested_expiration = dt_from_string( $self->patron_expiration_date );
+
+        my $cmp =
+          $requested_expiration
+          ? DateTime->compare( $requested_expiration, $new_expiration_date )
+          : 0;
+
+        $new_expiration_date =
+          $cmp == -1 ? $requested_expiration : $new_expiration_date;
+    }
+
+    $values->{expirationdate} = $new_expiration_date->ymd;
 
     $self->set($values)->store();
 
@@ -238,16 +244,16 @@ sub is_pickup_location_valid {
     Koha::Exceptions::MissingParameter->throw('The library_id parameter is mandatory')
         unless $params->{library_id};
 
-    my @pickup_locations;
+    my $pickup_locations;
 
     if ( $self->itemnumber ) { # item-level
-        @pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
+        $pickup_locations = $self->item->pickup_locations({ patron => $self->patron });
     }
     else { # biblio-level
-        @pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
+        $pickup_locations = $self->biblio->pickup_locations({ patron => $self->patron });
     }
 
-    return any { $_->branchcode eq $params->{library_id} } @pickup_locations;
+    return any { $_->branchcode eq $params->{library_id} } $pickup_locations->as_list;
 }
 
 =head3 set_pickup_location
@@ -574,6 +580,57 @@ sub cancel {
     return $self;
 }
 
+=head3 fill
+
+    $hold->fill;
+
+This method marks the hold as filled. It effectively moves it to old_reserves.
+
+=cut
+
+sub fill {
+    my ( $self ) = @_;
+    $self->_result->result_source->schema->txn_do(
+        sub {
+            my $patron = $self->patron;
+
+            $self->set(
+                {
+                    found    => 'F',
+                    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 });
+
+            if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
+                my $fee = $patron->category->reservefee // 0;
+                if ( $fee > 0 ) {
+                    $patron->account->add_debit(
+                        {
+                            amount       => $fee,
+                            description  => $self->biblio->title,
+                            user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                            library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
+                            interface    => C4::Context->interface,
+                            type         => 'RESERVE',
+                            item_id      => $self->itemnumber
+                        }
+                    );
+                }
+            }
+
+            C4::Log::logaction( 'HOLDS', 'FILL', $self->id, $self )
+                if C4::Context->preference('HoldsLog');
+        }
+    );
+    return $self;
+}
+
 =head3 store
 
 Override base store method to set default
@@ -585,10 +642,13 @@ sub store {
     my ($self) = @_;
 
     if ( !$self->in_storage ) {
+        if ( ! $self->expirationdate && $self->patron_expiration_date ) {
+            $self->expirationdate($self->patron_expiration_date);
+        }
+
         if (
             C4::Context->preference('DefaultHoldExpirationdate')
-            and ( not defined $self->expirationdate
-                or $self->expirationdate eq '' )
+                && !$self->expirationdate
           )
         {
             $self->_set_default_expirationdate;
@@ -602,9 +662,7 @@ sub store {
         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 '' )
+                && ! exists $updated_columns{expirationdate}
               )
             {
                 $self->_set_default_expirationdate;
@@ -662,6 +720,7 @@ sub to_api_mapping {
         itemnumber       => 'item_id',
         waitingdate      => 'waiting_date',
         expirationdate   => 'expiration_date',
+        patron_expiration_date => undef,
         lowestPriority   => 'lowest_priority',
         suspend          => 'suspended',
         suspend_until    => 'suspended_until',