diff --git a/Koha/ArticleRequest.pm b/Koha/ArticleRequest.pm index be4a895e2e..09b19455ae 100644 --- a/Koha/ArticleRequest.pm +++ b/Koha/ArticleRequest.pm @@ -19,7 +19,7 @@ package Koha::ArticleRequest; use Modern::Perl; - +use Koha::Account::Lines; use Koha::Database; use Koha::Patrons; use Koha::Biblios; @@ -57,6 +57,12 @@ sub request { ) unless $self->borrower->can_request_article; $self->status(Koha::ArticleRequest::Status::Requested); + + # 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; @@ -149,6 +155,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 diff --git a/t/db_dependent/Koha/ArticleRequest.t b/t/db_dependent/Koha/ArticleRequest.t index 4339f2a0b2..dfa5ec3f6c 100755 --- a/t/db_dependent/Koha/ArticleRequest.t +++ b/t/db_dependent/Koha/ArticleRequest.t @@ -30,12 +30,17 @@ my $builder = t::lib::TestBuilder->new; subtest 'request() tests' => sub { - plan tests => 3; + plan tests => 11; $schema->storage->txn_begin; + my $amount = 0; + + my $patron_mock = Test::MockModule->new('Koha::Patron'); + $patron_mock->mock( 'article_request_fee', sub { return $amount; } ); + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); - my $biblio = $builder->build_sample_biblio; + my $item = $builder->build_sample_item; my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); @@ -43,7 +48,7 @@ subtest 'request() tests' => sub { my $ar = Koha::ArticleRequest->new( { borrowernumber => $patron->id, - biblionumber => $biblio->id, + biblionumber => $item->biblionumber, } ); @@ -52,6 +57,29 @@ subtest 'request() tests' => sub { is( $ar->status, Koha::ArticleRequest::Status::Requested ); ok( defined $ar->created_on, 'created_on is set' ); + is( $ar->debit_id, undef, 'No fee linked' ); + is( $patron->account->balance, 0, 'No outstanding fees' ); + + # set a fee amount + $amount = 10; + + $ar = Koha::ArticleRequest->new( + { + borrowernumber => $patron->id, + biblionumber => $item->biblionumber, + itemnumber => $item->id, + } + ); + + $ar->request()->discard_changes; + + is( $ar->status, Koha::ArticleRequest::Status::Requested ); + is( $ar->itemnumber, $item->id, 'itemnumber set' ); + ok( defined $ar->created_on, 'created_on is set' ); + + ok( defined $ar->debit_id, 'Fee linked' ); + is( $patron->account->balance, $amount, 'Outstanding fees with the right value' ); + $schema->storage->txn_rollback; };