From 6e85eb8c31079868b168afacc3f4cda4f54e0306 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 6 Jun 2019 13:25:01 +0100
Subject: [PATCH] Bug 22421: (follow-up) Correct checkout relation

The Koha::Account::Line->checkout relationship accessor needed an update
to respect the new fields and foreign keys introduced here.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 C4/Circulation.pm                  |  5 ++---
 Koha/Account/Line.pm               | 13 ++++++++++---
 t/db_dependent/Koha/Account/Line.t | 29 +++++++++++++++++++----------
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 64d2b5176ab..a8cd9e262ff 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2651,9 +2651,8 @@ sub MarkIssueReturned {
         my $old_checkout = Koha::Old::Checkout->new($issue->unblessed)->store;
 
         # Update accountlines
-        my $accountlines =
-          Koha::Account::Lines->search( { issue_id => $issue->issue_id } );
-        $accountlines->update({ old_issue_id => $issue->issue_id, issue_id => undef });
+        my $accountlines = Koha::Account::Lines->search( { issue_id => $issue->issue_id } );
+        $accountlines->update( { old_issue_id => $issue->issue_id, issue_id => undef } );
 
         # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
         if ( $privacy && $privacy == 2) {
diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm
index 2dc50293750..16752ff0329 100644
--- a/Koha/Account/Line.pm
+++ b/Koha/Account/Line.pm
@@ -91,10 +91,17 @@ Return the checkout linked to this account line if exists
 
 sub checkout {
     my ($self) = @_;
-    return unless $self->issue_id;
 
-    return Koha::Checkouts->find( $self->issue_id )
-      || Koha::Old::Checkouts->find( $self->issue_id );
+    my $result;
+    if ( $self->issue_id ) {
+        my $issue_rs = $self->_result->issue;
+        $result = Koha::Checkout->_new_from_dbic($issue_rs) if $issue_rs;
+    } elsif ( $self->old_issue_id ) {
+        my $old_issue_rs = $self->_result->old_issue;
+        $result = Koha::Old::Checkout->_new_from_dbic($old_issue_rs) if $old_issue_rs;
+    }
+
+    return $result;
 }
 
 =head3 library
diff --git a/t/db_dependent/Koha/Account/Line.t b/t/db_dependent/Koha/Account/Line.t
index 41b76c96627..229bae28d4b 100755
--- a/t/db_dependent/Koha/Account/Line.t
+++ b/t/db_dependent/Koha/Account/Line.t
@@ -706,7 +706,7 @@ subtest 'adjust() tests' => sub {
 };
 
 subtest 'checkout() tests' => sub {
-    plan tests => 6;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
@@ -718,14 +718,17 @@ subtest 'checkout() tests' => sub {
     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
     my $checkout = AddIssue( $patron, $item->barcode );
 
-    my $line = $account->add_debit({
-        amount    => 10,
-        interface => 'commandline',
-        item_id   => $item->itemnumber,
-        issue_id  => $checkout->issue_id,
-        type      => 'OVERDUE',
-        status    => 'UNRETURNED'
-    });
+    my $line = $account->add_debit(
+        {
+            amount       => 10,
+            interface    => 'commandline',
+            item_id      => $item->itemnumber,
+            issue_id     => $checkout->issue_id,
+            old_issue_id => undef,
+            type         => 'OVERDUE',
+            status       => 'UNRETURNED'
+        }
+    );
 
     my $line_checkout = $line->checkout;
     is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
@@ -743,9 +746,15 @@ subtest 'checkout() tests' => sub {
     is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
     is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
 
-    $line->issue_id(undef)->store;
+    $old_line_checkout->delete;
+    $line->discard_changes; #NOTE: discard_changes refreshes the whole resultset, get_from_storage only refreshes the unjoined row
     is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
 
+    $line->old_issue_id(undef)->store;
+    $line->discard_changes; #NOTE: discard_changes refreshes the whole resultset, get_from_storage only refreshes the unjoined row
+    is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
+
+
     $schema->storage->txn_rollback;
 };
 
-- 
2.47.0