Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Accounts.t
index 489ae7d..3bcd14b 100755 (executable)
@@ -20,6 +20,7 @@ use Modern::Perl;
 
 use Test::More tests => 27;
 use Test::MockModule;
+use Test::Exception;
 use Test::Warn;
 
 use t::lib::TestBuilder;
@@ -36,7 +37,7 @@ use Koha::DateUtils qw( dt_from_string );
 use C4::Circulation qw( MarkIssueReturned );
 
 BEGIN {
-    use_ok('C4::Accounts');
+    use_ok('C4::Accounts', qw( chargelostitem purge_zero_balance_fees ));
     use_ok('Koha::Object');
     use_ok('Koha::Patron');
     use_ok('Data::Dumper');
@@ -141,9 +142,9 @@ for my $data  (@test_data) {
 
 $dbh->do(q|DELETE FROM accountlines|);
 
-subtest "Koha::Account::pay tests" => sub {
+subtest "Koha::Account::pay - always AutoReconcile + notes tests" => sub {
 
-    plan tests => 14;
+    plan tests => 17;
 
     # Create a borrower
     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
@@ -170,12 +171,10 @@ subtest "Koha::Account::pay tests" => sub {
 
     # There is $100 in the account
     $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    my $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 300, 'The account has 300$ as expected' );
+    my $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 300, 'The account has $300 outstanding debt as expected' );
+    my $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # We make a $20 payment
     my $borrowernumber = $borrower->borrowernumber;
@@ -187,86 +186,58 @@ subtest "Koha::Account::pay tests" => sub {
     is( $accountline->payment_type, "TEST_TYPE", "Payment type passed into pay is set in account line correctly" );
 
     # There is now $280 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 280, 'The account has $280 as expected' );
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 280, 'The account has $280 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    my $note = $sth->fetchrow_array;
-    is($note,'$20.00 payment note', '$20.00 payment note is registered');
+    is($accountline->note,'$20.00 payment note', '$20.00 payment note is registered');
 
-    # We make a -$30 payment (a NEGATIVE payment)
+    # We attempt to make a -$30 payment (a NEGATIVE payment)
     $data = '-30.00';
     $payment_note = '-$30.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
-
-    # There is now $310 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 310, 'The account has $310 as expected' );
-    # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'-$30.00 payment note', '-$30.00 payment note is registered');
+    throws_ok { $account->pay( { amount => $data, note => $payment_note } ) }
+    'Koha::Exceptions::Account::AmountNotPositive',
+      'Croaked on call to pay with negative amount';
 
     #We make a $150 payment ( > 1stLine )
     $data = '150.00';
     $payment_note = '$150.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
+    $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
 
-    # There is now $160 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, 160, 'The account has $160 as expected' );
+    # There is now $130 in the account
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 130, 'The account has $130 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, 0, 'The account has $0 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'$150.00 payment note', '$150.00 payment note is registered');
+    $accountline = Koha::Account::Lines->find( $id );
+    is($accountline->note,'$150.00 payment note', '$150.00 payment note is registered');
 
     #We make a $200 payment ( > amountleft )
     $data = '200.00';
     $payment_note = '$200.00 payment note';
-    $account->pay( { amount => $data, note => $payment_note } );
+    $id = $account->pay( { amount => $data, note => $payment_note } )->{payment_id};
 
-    # There is now -$40 in the account
-    $sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?");
-    $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber);
-    $amountleft = 0;
-    for my $line ( @$amountoutstanding ) {
-        $amountleft += $line;
-    }
-    is($amountleft, -40, 'The account has -$40 as expected, (credit situation)' );
+    # There is now -$70 in the account
+    $outstanding_debt = $account->outstanding_debits->total_outstanding;
+    is($outstanding_debt, 0, 'The account has $0 outstanding debt as expected' );
+    $outstanding_credit = $account->outstanding_credits->total_outstanding;
+    is($outstanding_credit, -70, 'The account has -$70 outstanding credit as expected' );
 
     # Is the payment note well registered
-    $sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1");
-    $sth->execute($borrower->borrowernumber);
-    $note = $sth->fetchrow_array;
-    is($note,'$200.00 payment note', '$200.00 payment note is registered');
+    $accountline = Koha::Account::Lines->find( $id );
+    is($accountline->note,'$200.00 payment note', '$200.00 payment note is registered');
 
     my $line3 = $account->add_debit({ type => 'ACCOUNT', amount => 42, interface => 'commandline' });
-    my $payment_id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
-    my $payment = Koha::Account::Lines->find( $payment_id );
-    is( $payment->amount()+0, -42, "Payment paid the specified fine" );
+    $id = $account->pay( { lines => [$line3], amount => 42 } )->{payment_id};
+    $accountline = Koha::Account::Lines->find( $id );
+    is( $accountline->amount()+0, -42, "Payment paid the specified fine" );
     $line3 = Koha::Account::Lines->find( $line3->id );
     is( $line3->amountoutstanding+0, 0, "Specified fine is paid" );
-    is( $payment->branchcode, undef, 'branchcode passed, then undef' );
+    is( $accountline->branchcode, undef, 'branchcode passed, then undef' );
 };
 
 subtest "Koha::Account::pay particular line tests" => sub {
@@ -352,7 +323,7 @@ subtest "Koha::Account::pay writeoff tests" => sub {
     my $writeoff = Koha::Account::Lines->find( $id );
 
     is( $writeoff->credit_type_code, 'WRITEOFF', 'Type is correct for WRITEOFF' );
-    is( $writeoff->description, 'Writeoff', 'Description is correct' );
+    is( $writeoff->description, '', 'Description is correct' );
     is( $writeoff->amount+0, -42, 'Amount is correct' );
 };
 
@@ -411,7 +382,7 @@ subtest "More Koha::Account::pay tests" => sub {
         is( $stat->type, 'payment', "Correct statistic type" );
         is( $stat->branch, $branch, "Correct branch logged to statistics" );
         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
-        is( $stat->value+0, $amount, "Correct amount logged to statistics" );
+        is( $stat->value+0, -$amount, "Correct amount logged to statistics" );
     }
 };
 
@@ -471,7 +442,7 @@ subtest "Even more Koha::Account::pay tests" => sub {
         is( $stat->type, 'payment', "Correct statistic type" );
         is( $stat->branch, $branch, "Correct branch logged to statistics" );
         is( $stat->borrowernumber, $borrowernumber, "Correct borrowernumber logged to statistics" );
-        is( $stat->value+0, $partialamount, "Correct amount logged to statistics" );
+        is( $stat->value+0, -$partialamount, "Correct amount logged to statistics" );
     }
 };