From dd8ab05c58a4a974922018870a07091ed44cdd5a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 17 Dec 2021 10:29:53 -0300 Subject: [PATCH] Bug 27946: Make Koha::ArticleRequest->request add a fee if required This patch makes the ->request method add a fee for the patron if required. It relies on methods defined in Koha::Patron for the task. The debit line is linked to the AR if applies. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/ArticleRequest.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/ArticleRequest.pm | 25 +++++++++++++++++++- t/db_dependent/Koha/ArticleRequest.t | 34 +++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) 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,15 +48,38 @@ subtest 'request() tests' => sub { my $ar = Koha::ArticleRequest->new( { borrowernumber => $patron->id, - biblionumber => $biblio->id, + biblionumber => $item->biblionumber, + } + ); + + $ar->request()->discard_changes; + + 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; }; -- 2.32.0