From baf39c326b0175e082073718d2a33747accd306d Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 11 Dec 2013 11:12:22 -0500
Subject: [PATCH] Bug 6427 [Part 12] - Add and update unit tests

---
 t/db_dependent/Accounts.t                        |  189 +++++++++++++++++++++-
 t/db_dependent/Circulation.t                     |   22 ++-
 t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t |    6 +-
 3 files changed, 207 insertions(+), 10 deletions(-)

diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t
index 1c2dd5a..7957cbe 100644
--- a/t/db_dependent/Accounts.t
+++ b/t/db_dependent/Accounts.t
@@ -1,16 +1,199 @@
 #!/usr/bin/perl
 #
-# This Koha test module is a stub!  
+# This Koha test module is a stub!
 # Add more tests here!!!
 
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 19;
+
+use C4::Context;
 
 BEGIN {
-        use_ok('C4::Accounts');
+    use_ok('Koha::Database');
+    use_ok('Koha::Accounts');
+    use_ok('Koha::Accounts::DebitTypes');
+    use_ok('Koha::Accounts::CreditTypes');
 }
 
+## Intial Setup ##
+my $borrower = Koha::Database->new()->schema->resultset('Borrower')->create(
+    {
+        surname         => 'Test',
+        categorycode    => 'S',
+        branchcode      => 'MPL',
+        account_balance => 0,
+    }
+);
+
+my $biblio =
+  Koha::Database->new()->schema->resultset('Biblio')
+  ->create( { title => "Test Record" } );
+my $biblioitem =
+  Koha::Database->new()->schema->resultset('Biblioitem')
+  ->create( { biblionumber => $biblio->biblionumber() } );
+my $item = Koha::Database->new()->schema->resultset('Item')->create(
+    {
+        biblionumber     => $biblio->biblionumber(),
+        biblioitemnumber => $biblioitem->biblioitemnumber(),
+        replacementprice => 25.00,
+        barcode          => q{TEST_ITEM_BARCODE}
+    }
+);
+
+my $issue = Koha::Database->new()->schema->resultset('Issue')->create(
+    {
+        borrowernumber => $borrower->borrowernumber(),
+        itemnumber     => $item->itemnumber(),
+    }
+);
+## END initial setup
+
+ok( Koha::Accounts::DebitTypes::Fine eq 'FINE', 'Test DebitTypes::Fine' );
+ok( Koha::Accounts::DebitTypes::Lost eq 'LOST', 'Test DebitTypes::Lost' );
+ok(
+    Koha::Accounts::DebitTypes::IsValid('FINE'),
+    'Test DebitTypes::IsValid with valid debit type'
+);
+ok(
+    !Koha::Accounts::DebitTypes::IsValid('Not A Valid Fee Type'),
+    'Test DebitTypes::IsValid with an invalid debit type'
+);
+my $authorised_value =
+  Koha::Database->new()->schema->resultset('AuthorisedValue')->create(
+    {
+        category         => 'MANUAL_INV',
+        authorised_value => 'TEST',
+        lib              => 'Test',
+    }
+  );
+ok( Koha::Accounts::DebitTypes::IsValid('TEST'),
+    'Test DebitTypes::IsValid with valid authorised value debit type' );
+$authorised_value->delete();
+
+my $debit = AddDebit(
+    {
+        borrower   => $borrower,
+        amount     => 5.00,
+        type       => Koha::Accounts::DebitTypes::Fine,
+        branchcode => 'MPL',
+    }
+);
+ok( $debit, "AddDebit returned a valid debit id " . $debit->id() );
+
+ok(
+    $borrower->account_balance() == 5.00,
+    "Borrower's account balance updated correctly"
+);
+
+my $debit2 = AddDebit(
+    {
+        borrower   => $borrower,
+        amount     => 7.00,
+        type       => Koha::Accounts::DebitTypes::Fine,
+        branchcode => 'MPL',
+    }
+);
+
+my $credit = AddCredit(
+    {
+        borrower   => $borrower,
+        type       => Koha::Accounts::CreditTypes::Payment,
+        amount     => 9.00,
+        branchcode => 'MPL',
+    }
+);
+
+RecalculateAccountBalance( { borrower => $borrower } );
+ok(
+    sprintf( "%.2f", $borrower->account_balance() ) eq "3.00",
+    "RecalculateAccountBalance updated balance correctly."
+);
+
+Koha::Database->new()->schema->resultset('AccountCredit')->create(
+    {
+        borrowernumber   => $borrower->borrowernumber(),
+        type             => Koha::Accounts::CreditTypes::Payment,
+        amount_paid      => 3.00,
+        amount_remaining => 3.00,
+    }
+);
+NormalizeBalances( { borrower => $borrower } );
+ok(
+    $borrower->account_balance() == 0.00,
+    "NormalizeBalances updated balance correctly."
+);
+
+# Adding advance credit with no balance due
+$credit = AddCredit(
+    {
+        borrower   => $borrower,
+        type       => Koha::Accounts::CreditTypes::Payment,
+        amount     => 9.00,
+        branchcode => 'MPL',
+    }
+);
+ok(
+    $borrower->account_balance() == -9,
+'Adding a $9 credit for borrower with 0 balance results in a -9 dollar account balance'
+);
+
+my $debit3 = AddDebit(
+    {
+        borrower   => $borrower,
+        amount     => 5.00,
+        type       => Koha::Accounts::DebitTypes::Fine,
+        branchcode => 'MPL',
+    }
+);
+ok(
+    $borrower->account_balance() == -4,
+'Adding a $5 debit when the balance is negative results in the debit being automatically paid, resulting in a balance of -4'
+);
+
+my $debit4 = AddDebit(
+    {
+        borrower   => $borrower,
+        amount     => 6.00,
+        type       => Koha::Accounts::DebitTypes::Fine,
+        branchcode => 'MPL',
+    }
+);
+ok(
+    $borrower->account_balance() == 2,
+'Adding another debit ( 6.00 ) more than the negative account balance results in a partial credit and a balance due of 2.00'
+);
+$credit = AddCredit(
+    {
+        borrower   => $borrower,
+        type       => Koha::Accounts::CreditTypes::WriteOff,
+        amount     => 2.00,
+        branchcode => 'MPL',
+        debit_id   => $debit4->debit_id(),
+    }
+);
+ok( $borrower->account_balance() == 0,
+    'WriteOff of remaining 2.00 balance succeeds' );
+
+my $debit5 = DebitLostItem(
+    {
+        borrower => $borrower,
+        issue    => $issue,
+    }
+);
+ok( $borrower->account_balance() == 25,
+    'DebitLostItem adds debit for replacement price of item' );
 
+my $lost_credit =
+  CreditLostItem( { borrower => $borrower, debit => $debit5 } );
+ok(
+    $borrower->account_balance() == 0,
+    'CreditLostItem adds credit for same about as the debit for the lost tiem'
+);
 
+## Post test cleanup ##
+$issue->delete();
+$item->delete();
+$biblio->delete();
+$borrower->delete();
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 73c0bf3..ede8af0 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -319,8 +319,15 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     C4::Context->set_preference('WhenLostForgiveFine','1');
     C4::Context->set_preference('WhenLostChargeReplacementFee','1');
 
-    C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
-        15.00, q{}, Koha::DateUtils::output_pref($datedue) );
+    C4::Overdues::UpdateFine(
+        {
+            itemnumber     => $itemnumber,
+            borrowernumber => $renewing_borrower->{borrowernumber},
+            amount         => 15.00,
+            due            => Koha::DateUtils::output_pref($datedue),
+            issue_id       => GetItemIssue($itemnumber)->{issue_id}
+        }
+    );
 
     LostItem( $itemnumber, 1 );
 
@@ -336,8 +343,15 @@ C4::Context->dbh->do("DELETE FROM accountlines");
     C4::Context->set_preference('WhenLostForgiveFine','0');
     C4::Context->set_preference('WhenLostChargeReplacementFee','0');
 
-    C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
-        15.00, q{}, Koha::DateUtils::output_pref($datedue) );
+    C4::Overdues::UpdateFine(
+        {
+            itemnumber     => $itemnumber2,
+            borrowernumber => $renewing_borrower->{borrowernumber},
+            amount         => 15.00,
+            due            => Koha::DateUtils::output_pref($datedue),
+            issue_id       => GetItemIssue($itemnumber2)->{issue_id},
+        }
+    );
 
     LostItem( $itemnumber2, 1 );
 
diff --git a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t
index 46d9ec1..b8f0708 100644
--- a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t
+++ b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t
@@ -40,17 +40,17 @@ my %borrower_data = (
 my $borrowernumber = C4::Members::AddMember( %borrower_data );
 $borrower_data{borrowernumber} = $borrowernumber;
 
-my ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
+my ( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
 is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" );
 
 $borrower_data{categorycode} = 'J';
 C4::Members::ModMember( %borrower_data );
-( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
+( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
 is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) );
 
 # Check with calling directly AddEnrolmentFeeIfNeeded
 C4::Members::AddEnrolmentFeeIfNeeded( 'YA', $borrowernumber );
-( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber );
+( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber );
 is( $total, $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA, "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA ) );
 
 $dbh->rollback;
-- 
1.7.2.5