use Modern::Perl;
-use Test::More tests => 4;
+use Test::More tests => 5;
use Koha::Account;
use Koha::Account::Lines;
$schema->storage->txn_rollback;
};
+
+subtest 'normalize_balance' => sub {
+
+ plan tests => 3;
+
+ subtest 'more credit than debit' => sub {
+
+ plan tests => 6;
+
+ $schema->storage->txn_begin;
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ my $account = $patron->account;
+
+ # Add Credits
+ $account->add_credit({ amount => 1 });
+ $account->add_credit({ amount => 2 });
+ $account->add_credit({ amount => 3 });
+ $account->add_credit({ amount => 4 });
+ $account->add_credit({ amount => 5 });
+
+ # Add Debits TODO: replace for calls to add_debit when time comes
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+
+ # Paid Off
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+ is( $account->balance(), -5, "Account balance is -5" );
+ is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
+ is( $account->outstanding_credits->total_outstanding, -15, 'Outstanding credits sum -15' );
+
+ $account->normalize_balance();
+
+ is( $account->balance(), -5, "Account balance is -5" );
+ is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
+ is( $account->outstanding_credits->total_outstanding, -5, 'Outstanding credits sum -5' );
+
+ $schema->storage->txn_rollback;
+ };
+
+ subtest 'same debit than credit' => sub {
+
+ plan tests => 6;
+
+ $schema->storage->txn_begin;
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ my $account = $patron->account;
+
+ # Add Credits
+ $account->add_credit({ amount => 1 });
+ $account->add_credit({ amount => 2 });
+ $account->add_credit({ amount => 3 });
+ $account->add_credit({ amount => 4 });
+
+ # Add Debits TODO: replace for calls to add_debit when time comes
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+
+ # Paid Off
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+ is( $account->balance(), 0, "Account balance is 0" );
+ is( $account->outstanding_debits->total_outstanding, 10, 'Outstanding debits sum 10' );
+ is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
+
+ $account->normalize_balance();
+
+ is( $account->balance(), 0, "Account balance is 0" );
+ is( $account->outstanding_debits->total_outstanding, 0, 'No outstanding debits' );
+ is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
+
+ $schema->storage->txn_rollback;
+ };
+
+ subtest 'more debit than credit' => sub {
+
+ plan tests => 6;
+
+ $schema->storage->txn_begin;
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ my $account = $patron->account;
+
+ # Add Credits
+ $account->add_credit({ amount => 1 });
+ $account->add_credit({ amount => 2 });
+ $account->add_credit({ amount => 3 });
+ $account->add_credit({ amount => 4 });
+
+ # Add Debits TODO: replace for calls to add_debit when time comes
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 1 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 2, amountoutstanding => 2 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 3, amountoutstanding => 3 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 4, amountoutstanding => 4 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 5, amountoutstanding => 5 })->store;
+
+ # Paid Off
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+ Koha::Account::Line->new({ borrowernumber => $patron->id, amount => 1, amountoutstanding => 0 })->store;
+
+ is( $account->balance(), 5, "Account balance is 5" );
+ is( $account->outstanding_debits->total_outstanding, 15, 'Outstanding debits sum 15' );
+ is( $account->outstanding_credits->total_outstanding, -10, 'Outstanding credits sum -10' );
+
+ $account->normalize_balance();
+
+ is( $account->balance(), 5, "Account balance is 5" );
+ is( $account->outstanding_debits->total_outstanding, 5, 'Outstanding debits sum 5' );
+ is( $account->outstanding_credits->total_outstanding, 0, 'Outstanding credits sum 0' );
+
+ $schema->storage->txn_rollback;
+ };
+};