From 774536c843a37c34d773df3157a04dc70b84021c Mon Sep 17 00:00:00 2001 From: Martin Renvoize 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 Signed-off-by: Kyle M Hall --- 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 5d263780829..fce56277361 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2640,9 +2640,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 7496f17677b..e375874d342 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 c690d38f405..89a31cb2182 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.43.0