diff --git a/Koha/Old/Checkout.pm b/Koha/Old/Checkout.pm
index 876e0cd18d..d602a63602 100644
--- a/Koha/Old/Checkout.pm
+++ b/Koha/Old/Checkout.pm
@@ -54,6 +54,7 @@ Return the patron for who the checkout has been done
sub patron {
my ( $self ) = @_;
my $patron_rs = $self->_result->borrower;
+ return unless $patron_rs;
return Koha::Patron->_new_from_dbic( $patron_rs );
}
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/issuehistory.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/issuehistory.tt
index 785857a094..4a3d57d1b8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/issuehistory.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/issuehistory.tt
@@ -44,7 +44,7 @@
[% FOREACH checkout IN checkouts %]
[% IF Koha.Preference('intranetreadinghistory') AND CAN_user_circulate_circulate_remaining_permissions %]
- | [% INCLUDE 'patron-title.inc' patron => checkout.patron hide_patron_infos_if_needed=1 %] |
+ [% IF checkout.patron %][%# Not set for deleted patron records %][% INCLUDE 'patron-title.inc' patron => checkout.patron hide_patron_infos_if_needed=1 %][% END %] |
[% END %]
[% IF checkout.item.barcode %] [%# FIXME This test is not mandatory I think %]
diff --git a/t/db_dependent/Koha/Checkouts.t b/t/db_dependent/Koha/Checkouts.t
index c8ec4c52fa..dfb90e42a8 100644
--- a/t/db_dependent/Koha/Checkouts.t
+++ b/t/db_dependent/Koha/Checkouts.t
@@ -21,6 +21,7 @@ use Modern::Perl;
use Test::More tests => 7;
+use C4::Circulation;
use Koha::Checkouts;
use Koha::Database;
use Koha::DateUtils qw( dt_from_string );
@@ -94,10 +95,31 @@ subtest 'item' => sub {
};
subtest 'patron' => sub {
- plan tests => 2;
- my $p = $new_checkout_1->patron;
- is( ref($p), 'Koha::Patron', 'Koha::Checkout->patron should return a Koha::Patron' );
- is( $p->borrowernumber, $patron->{borrowernumber}, 'Koha::Checkout->patron should return the correct patron' );
+ plan tests => 3;
+ my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
+
+ my $item = $builder->build_object( { class=> 'Koha::Items' } );
+ my $checkout = Koha::Checkout->new(
+ { borrowernumber => $patron->borrowernumber,
+ itemnumber => $item->itemnumber,
+ branchcode => $library->{branchcode},
+ }
+ )->store;
+
+ my $p = $checkout->patron;
+ is( ref($p), 'Koha::Patron',
+ 'Koha::Checkout->patron should return a Koha::Patron' );
+ is( $p->borrowernumber, $patron->borrowernumber,
+ 'Koha::Checkout->patron should return the correct patron' );
+
+ # Testing Koha::Old::Checkout->patron now
+ my $issue_id = $checkout->issue_id;
+ C4::Circulation::MarkIssueReturned( $p->borrowernumber, $checkout->itemnumber );
+ $p->delete;
+ my $old_issue = Koha::Old::Checkouts->find($issue_id);
+ is( $old_issue->patron, undef,
+ 'Koha::Checkout->patron should return undef if the patron record has been deleted'
+ );
};
$retrieved_checkout_1->delete;
|