Bug 33168: Prevent debhelper from renaming history.txt to changelog
[koha-ffzg.git] / Koha / ArticleRequest.pm
index 8e9bd5f..83c88be 100644 (file)
@@ -19,7 +19,7 @@ package Koha::ArticleRequest;
 
 use Modern::Perl;
 
-
+use Koha::Account::Lines;
 use Koha::Database;
 use Koha::Patrons;
 use Koha::Biblios;
@@ -57,7 +57,13 @@ sub request {
     ) unless $self->borrower->can_request_article;
 
     $self->status(Koha::ArticleRequest::Status::Requested);
-    $self->SUPER::store();
+
+    # Handle possible fees
+    my $debit = $self->borrower->add_article_request_fee_if_needed({ item_id => $self->itemnumber });
+    $self->debit_id( $debit->id )
+        if $debit;
+
+    $self->store();
     $self->notify();
     return $self;
 }
@@ -74,7 +80,7 @@ sub set_pending {
     my ($self) = @_;
 
     $self->status(Koha::ArticleRequest::Status::Pending);
-    $self->SUPER::store();
+    $self->store();
     $self->notify();
     return $self;
 }
@@ -122,12 +128,44 @@ Marks the article as cancelled. Send a notification if appropriate.
 =cut
 
 sub cancel {
-    my ( $self, $notes ) = @_;
+    my ( $self, $params ) = @_;
+
+    my $cancellation_reason = $params->{cancellation_reason};
+    my $notes = $params->{notes};
 
     $self->status(Koha::ArticleRequest::Status::Canceled);
+    $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
     $self->notes($notes) if $notes;
     $self->store();
     $self->notify();
+
+    my $debit = $self->debit;
+
+    if ( $debit ) {
+        # fees found, refund
+        my $account = $self->borrower->account;
+
+        my $total_reversible = $debit->debit_offsets->filter_by_reversible->total;
+        if ( $total_reversible ) {
+
+            $account->add_credit(
+                {
+                    amount       => abs $total_reversible,
+                    interface    => C4::Context->interface,
+                    type         => 'REFUND',
+                }
+            );
+        }
+
+        if ( $debit->amountoutstanding ) {
+            $debit->reduce({
+                reduction_type => 'REFUND',
+                amount         => $debit->amountoutstanding,
+                interface      => C4::Context->interface,
+            })->discard_changes;
+        }
+    }
+
     return $self;
 }
 
@@ -145,6 +183,23 @@ sub biblio {
     return $self->{_biblio};
 }
 
+=head3 debit
+
+    my $debit = $article_request->debit;
+
+Returns the related Koha::Account::Line object for this article request
+
+=cut
+
+sub debit {
+    my ($self) = @_;
+
+    my $debit_rs = $self->_result->debit;
+    return unless $debit_rs;
+
+    return Koha::Account::Line->_new_from_dbic( $debit_rs );
+}
+
 =head3 item
 
 Returns the Koha::Item object for this article request
@@ -196,12 +251,12 @@ will have notifications sent.
 
 sub store {
     my ($self) = @_;
-    if ( $self->in_storage ) {
-        return $self->SUPER::store;
-    } else {
+
+    if ( !$self->in_storage ) {
         $self->created_on( dt_from_string() );
-        return $self->request;
     }
+
+    return $self->SUPER::store;
 }
 
 =head2 Internal methods
@@ -219,6 +274,16 @@ sub notify {
     my ($self) = @_;
 
     my $status = $self->status;
+    my $reason = $self->notes;
+    if ( !defined $reason && $self->cancellation_reason ) {
+        my $av = Koha::AuthorisedValues->search(
+            {
+                category            => 'AR_CANCELLATION',
+                authorised_value    => $self->cancellation_reason
+            }
+        )->next;
+        $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
+    }
 
     require C4::Letters;
     if (
@@ -235,6 +300,9 @@ sub notify {
                 items            => $self->itemnumber,
                 branches         => $self->branchcode,
             },
+            substitute => {
+                reason => $reason,
+            },
         )
       )
     {
@@ -244,7 +312,7 @@ sub notify {
                 borrowernumber         => $self->borrowernumber,
                 message_transport_type => 'email',
             }
-        ) or warn "can't enqueue letter ". $letter->{code};
+        ) or warn "can't enqueue letter " . $letter->{code};
     }
 }