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

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Tomás Cohen Arazi 2021-12-17 10:29:53 -03:00 committed by Fridolin Somers
parent d5c8aad712
commit b4fd79e750
2 changed files with 55 additions and 4 deletions

View 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,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

View file

@ -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;
};