From 0370ef515cf1eb6b1415798dc808ef7bcadb3572 Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Wed, 6 Feb 2019 21:02:24 +0000
Subject: [PATCH] Bug 17712: (follow-up) Make test pass

Test plan:
Run these tests:

t/Koha/Availability.t
t/db_dependent/Koha/Availability/Checks/Biblio.t
t/db_dependent/Koha/Availability/Checks/Biblioitem.t
t/db_dependent/Koha/Availability/Checks/Checkout.t
t/db_dependent/Koha/Availability/Checks/IssuingRule.t
t/db_dependent/Koha/Availability/Checks/Item.t
t/db_dependent/Koha/Availability/Checks/LibraryItemRule.t
t/db_dependent/Koha/Availability/Checks/Patron.t
t/db_dependent/Koha/Biblio/Availability.t
t/db_dependent/Koha/Biblio/Availability/Hold.t
t/db_dependent/Koha/Biblio/Availability/Search.t
t/db_dependent/Koha/Item/Availability.t
t/db_dependent/Koha/Item/Availability/Checkout.t
t/db_dependent/Koha/Item/Availability/Hold/Intranet/HoldPolicyOverride.t
t/db_dependent/Koha/Item/Availability/Hold/Opac/HoldRules.t
t/db_dependent/Koha/Item/Availability/Hold/Opac/ItemStatus.t
t/db_dependent/Koha/Item/Availability/Hold/Opac/LibraryItemRules.t
t/db_dependent/Koha/Item/Availability/Search.t
---
 Koha/Availability/Checks/Item.pm                 |  2 +-
 Koha/Availability/Checks/Patron.pm               | 15 ++++--------
 Koha/Biblio/Availability.pm                      |  8 +++++--
 Koha/Item/Availability.pm                        |  8 +++++--
 t/db_dependent/Koha/Availability/Checks/Patron.t | 30 +++++++++++++-----------
 t/db_dependent/Koha/Item/Availability/Checkout.t |  5 ++--
 6 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/Koha/Availability/Checks/Item.pm b/Koha/Availability/Checks/Item.pm
index a8e8d6e..e9c6b7e 100644
--- a/Koha/Availability/Checks/Item.pm
+++ b/Koha/Availability/Checks/Item.pm
@@ -65,7 +65,7 @@ Returns Koha::Exceptions::Item::CheckedOut if item is checked out.
 sub checked_out {
     my ($self, $issue) = @_;
 
-    $issue ||= C4::Circulation::GetItemIssue($self->item->itemnumber);
+    $issue ||= $self->item->checkout;
     if (ref($issue) eq 'Koha::Checkout') {
         $issue = $issue->unblessed;
         $issue->{date_due} = dt_from_string($issue->{date_due});
diff --git a/Koha/Availability/Checks/Patron.pm b/Koha/Availability/Checks/Patron.pm
index cfa0f7d..f532105 100644
--- a/Koha/Availability/Checks/Patron.pm
+++ b/Koha/Availability/Checks/Patron.pm
@@ -87,9 +87,7 @@ Koha::Exceptions::Patron::Debt additional fields:
 sub debt_checkout {
     my ($self) = @_;
 
-    my ($amount) = C4::Members::GetMemberAccountRecords(
-                        $self->patron->borrowernumber
-    );
+    my $amount = $self->patron->account->balance;
     my $maxoutstanding = C4::Context->preference("noissuescharge");
     if (C4::Context->preference('AllFinesNeedOverride') && $amount > 0) {
         # All fines need override, so return Koha::Exceptions::Patron::Debt.
@@ -124,8 +122,7 @@ sub debt_checkout_guarantees {
     return unless scalar(@guarantees);
     my $guarantees_non_issues_charges;
     foreach my $g (@guarantees) {
-        my ($b, $n, $o) = C4::Members::GetMemberAccountBalance($g->id);
-        $guarantees_non_issues_charges += $n;
+        $guarantees_non_issues_charges += $g->account->non_issues_charges;
     }
     if ($guarantees_non_issues_charges > $max_charges) {
         return Koha::Exceptions::Patron::DebtGuarantees->new(
@@ -150,9 +147,7 @@ Koha::Exceptions::Patron::Debt additional fields:
 sub debt_hold {
     my ($self) = @_;
 
-    my ($amount) = C4::Members::GetMemberAccountRecords(
-                        $self->patron->borrowernumber
-    );
+    my $amount = $self->patron->account->balance;
     my $maxoutstanding = C4::Context->preference("maxoutstanding");
     return $self->_debt($amount, $maxoutstanding);
 }
@@ -171,9 +166,7 @@ Koha::Exceptions::Patron::Debt additional fields:
 sub debt_renew_opac {
     my ($self) = @_;
 
-    my ($amount) = C4::Members::GetMemberAccountRecords(
-                        $self->patron->borrowernumber
-    );
+    my $amount = $self->patron->account->balance;
     my $maxoutstanding = C4::Context->preference("OPACFineNoRenewals");
     return $self->_debt($amount, $maxoutstanding);
 }
diff --git a/Koha/Biblio/Availability.pm b/Koha/Biblio/Availability.pm
index 654457b..866470e 100644
--- a/Koha/Biblio/Availability.pm
+++ b/Koha/Biblio/Availability.pm
@@ -22,9 +22,11 @@ use Scalar::Util qw(looks_like_number);
 
 use base qw(Koha::Availability);
 
+use Koha::Biblios;
 use Koha::Exceptions;
 use Koha::Exceptions::Biblio;
 use Koha::Exceptions::Patron;
+use Koha::Patrons;
 
 =head1 NAME
 
@@ -88,7 +90,8 @@ sub new {
                 parameter => 'biblionumber',
             );
         }
-        $self->biblio(Koha::Biblios->find($params->{'biblionumber'}));
+        my $biblio = Koha::Biblios->find($params->{'biblionumber'});
+        $self->biblio( $biblio );
         unless ($self->biblio) {
             Koha::Exceptions::Biblio::NotFound->throw(
                 error => 'Biblio not found.',
@@ -117,7 +120,8 @@ sub new {
                 parameter => 'borrowernumber',
             );
         }
-        $self->patron(Koha::Patrons->find($params->{'borrowernumber'}));
+        my $patron = Koha::Patrons->find($params->{'borrowernumber'});
+        $self->patron( $patron );
         unless ($self->patron) {
             Koha::Exceptions::Patron::NotFound->throw(
                 error => 'Patron not found.',
diff --git a/Koha/Item/Availability.pm b/Koha/Item/Availability.pm
index c26d79b..71bfc5d 100644
--- a/Koha/Item/Availability.pm
+++ b/Koha/Item/Availability.pm
@@ -25,6 +25,8 @@ use base qw(Koha::Availability);
 use Koha::Exceptions;
 use Koha::Exceptions::Item;
 use Koha::Exceptions::Patron;
+use Koha::Items;
+use Koha::Patrons;
 
 =head1 NAME
 
@@ -82,7 +84,8 @@ sub new {
                 parameter => 'itemnumber',
             );
         }
-        $self->item(Koha::Items->find($params->{'itemnumber'}));
+        my $item = Koha::Items->find($params->{'itemnumber'});
+        $self->item( $item );
         unless ($self->item) {
             Koha::Exceptions::Item::NotFound->throw(
                 error => 'Item not found.',
@@ -111,7 +114,8 @@ sub new {
                 parameter => 'borrowernumber',
             );
         }
-        $self->patron(Koha::Patrons->find($params->{'borrowernumber'}));
+        my $patron = Koha::Patrons->find($params->{'borrowernumber'});
+        $self->patron( $patron );
         unless ($self->patron) {
             Koha::Exceptions::Patron::NotFound->throw(
                 error => 'Patron not found.',
diff --git a/t/db_dependent/Koha/Availability/Checks/Patron.t b/t/db_dependent/Koha/Availability/Checks/Patron.t
index d0b7c16..813f1e8 100644
--- a/t/db_dependent/Koha/Availability/Checks/Patron.t
+++ b/t/db_dependent/Koha/Availability/Checks/Patron.t
@@ -88,7 +88,7 @@ sub t_fines_checkout_less_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9000,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('noissuescharge');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_checkout;
@@ -120,7 +120,7 @@ sub t_fines_checkout_more_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9001,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('noissuescharge');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_checkout;
@@ -146,7 +146,7 @@ sub t_fines_hold_less_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9000,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('maxoutstanding');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_hold;
@@ -167,7 +167,7 @@ sub t_fines_hold_more_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9001,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('maxoutstanding');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_hold;
@@ -193,7 +193,7 @@ sub t_fines_renew_opac_less_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9000,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('OPACFineNoRenewals');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_renew_opac;
@@ -214,7 +214,7 @@ sub t_fines_renew_opac_more_than_max {
         borrowernumber => $patron->borrowernumber,
         amountoutstanding => 9001,
     })->store;
-    my ($outstanding) = C4::Members::GetMemberAccountRecords($patron->borrowernumber);
+    my $outstanding = $patron->account->balance;
     my $maxoutstanding = C4::Context->preference('OPACFineNoRenewals');
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_renew_opac;
@@ -268,14 +268,16 @@ sub t_guarantees_fines_checkout_more_than_max {
     $guarantee1->guarantorid($patron->borrowernumber)->store;
     my $guarantee2 = build_a_test_patron();
     $guarantee2->guarantorid($patron->borrowernumber)->store;
-    my $line1 = Koha::Account::Line->new({
-        borrowernumber => $guarantee1->borrowernumber,
-        amountoutstanding => 3,
-    })->store;
-    my $line2 = Koha::Account::Line->new({
-        borrowernumber => $guarantee2->borrowernumber,
-        amountoutstanding => 3,
-    })->store;
+
+    my $line1 = $guarantee1->account->add_debit({
+        amount => 3,
+        type => 'fine',
+    });
+    my $line2 = $guarantee2->account->add_debit({
+        amount => 3,
+        type => 'fine',
+    });
+
     my $patroncalc = Koha::Availability::Checks::Patron->new($patron);
     my $debt = $patroncalc->debt_checkout_guarantees;
     my $expecting = 'Koha::Exceptions::Patron::DebtGuarantees';
diff --git a/t/db_dependent/Koha/Item/Availability/Checkout.t b/t/db_dependent/Koha/Item/Availability/Checkout.t
index 4f4d37c..f3f4157 100644
--- a/t/db_dependent/Koha/Item/Availability/Checkout.t
+++ b/t/db_dependent/Koha/Item/Availability/Checkout.t
@@ -100,7 +100,7 @@ subtest 'Checkout held item' => sub {
     my $patron = build_a_test_patron();
     my $patron2 = build_a_test_patron();
     my $item = build_a_test_item();
-    my $biblio = C4::Biblio::GetBiblio($item->biblionumber);
+    my $biblio = $item->biblio;
     my $priority= C4::Reserves::CalculatePriority($item->biblionumber);
     my $reserve_id = add_biblio_level_hold($item, $patron2, $item->homebranch);
 
@@ -114,7 +114,8 @@ subtest 'Checkout held item' => sub {
        .' then it is available.');
     is($availability->confirm, 1, 'Then there is one thing to be confirmed.');
     ok($availability->confirmations->{$expected}, $expected);
-    C4::Reserves::CancelReserve({ reserve_id => $reserve_id });
+    my $hold = Koha::Holds->find( $reserve_id );
+    $hold->cancel;
     ok($availability->in_intranet->available, 'Given I have cancelled the hold,'
        .' then the item is still available.');
     ok(!$availability->confirm, 'Then there is no need to make confirmations.');
-- 
2.1.4