Bug 17580: Add the Koha::Patron->get_overdues method

This method will be used by several patches later.

Test plan:
  prove t/db_dependent/Koha/Patrons.t
should return green

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-11-08 12:16:49 +00:00 committed by Kyle M Hall
parent 9d0c436cb4
commit 46432511a6
2 changed files with 91 additions and 0 deletions

View file

@ -496,6 +496,29 @@ sub add_enrolment_fee_if_needed {
return $enrolment_fee || 0;
}
=head3 get_overdues
my $overdue_items = $patron->get_overdues
Return the overdued items
=cut
sub get_overdues {
my ($self) = @_;
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
my $issues = Koha::Issues->search(
{
'me.borrowernumber' => $self->borrowernumber,
'me.date_due' => { '<' => $dtf->format_datetime(dt_from_string) },
},
{
prefetch => { item => { biblio => 'biblioitems' } },
}
);
return $issues;
}
=head3 type
=cut

View file

@ -21,7 +21,10 @@ use Modern::Perl;
use Test::More tests => 14;
use Test::Warn;
use DateTime;
use C4::Biblio;
use C4::Circulation;
use C4::Members;
use Koha::Holds;
@ -403,6 +406,71 @@ subtest 'add_enrolment_fee_if_needed' => sub {
$patron->delete;
};
subtest 'get_overdues' => sub {
plan tests => 4;
my $library = $builder->build( { source => 'Branch' } );
my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
my $item_1 = $builder->build(
{
source => 'Item',
value => {
homebranch => $library->{branchcode},
holdingbranch => $library->{branchcode},
biblionumber => $biblionumber_1
}
}
);
my $item_2 = $builder->build(
{
source => 'Item',
value => {
homebranch => $library->{branchcode},
holdingbranch => $library->{branchcode},
biblionumber => $biblionumber_1
}
}
);
my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
my $item_3 = $builder->build(
{
source => 'Item',
value => {
homebranch => $library->{branchcode},
holdingbranch => $library->{branchcode},
biblionumber => $biblionumber_2
}
}
);
my $patron = $builder->build(
{
source => 'Borrower',
value => { branchcode => $library->{branchcode} }
}
);
# Not sure how this is useful, but AddIssue pass this variable to different other subroutines
$patron = GetMember( borrowernumber => $patron->{borrowernumber} );
my $module = new Test::MockModule('C4::Context');
$module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
AddIssue( $patron, $item_3->{barcode} );
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
my $overdues = $patron->get_overdues;
is( $overdues->count, 2, 'Patron should have 2 overdues');
is( ref($overdues), 'Koha::Issues', 'Koha::Patron->get_overdues should return Koha::Issues' );
is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
# Clean stuffs
Koha::Issues->search( { borrowernumber => $patron->borrowernumber } )->delete;
$patron->delete;
};
$retrieved_patron_1->delete;
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );