Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Koha / Account / Line.t
index 4a18526..936fc5f 100755 (executable)
@@ -25,7 +25,7 @@ use Test::MockModule;
 
 use DateTime;
 
-use C4::Circulation qw/AddIssue AddReturn/;
+use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem AddIssue AddReturn );
 use Koha::Account;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
@@ -175,7 +175,7 @@ subtest 'is_credit() and is_debit() tests' => sub {
 
 subtest 'apply() tests' => sub {
 
-    plan tests => 25;
+    plan tests => 31;
 
     $schema->storage->txn_begin;
 
@@ -208,10 +208,9 @@ subtest 'apply() tests' => sub {
     $debit_1->discard_changes;
 
     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
-    my $remaining_credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
-    is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
-    $credit->discard_changes;
-    is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
+    $credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+    is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object');
+    is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' );
 
     # re-read debit info
     $debit_1->discard_changes;
@@ -224,10 +223,8 @@ subtest 'apply() tests' => sub {
     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
 
     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
-    $remaining_credit = $credit->apply( { debits => [ $debits->as_list ] } );
-    is( $remaining_credit, 0, 'No remaining credit left' );
-    $credit->discard_changes;
-    is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
+    $credit = $credit->apply( { debits => [ $debits->as_list ] } );
+    is( $credit->amountoutstanding * 1, 0, 'No remaining credit' );
     $debit_2->discard_changes;
     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
 
@@ -279,12 +276,12 @@ subtest 'apply() tests' => sub {
     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
 
     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
-    $remaining_credit = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
+    $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
 
     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
-    is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
+    is( $credit_2->amountoutstanding * 1, 0, 'No remaining credit' );
 
     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
     my $biblio = $builder->build_sample_biblio();
@@ -327,12 +324,96 @@ subtest 'apply() tests' => sub {
     my $module = Test::MockModule->new('C4::Circulation');
     $module->mock('AddRenewal', sub { $called = 1; });
     $module->mock('CanBookBeRenewed', sub { return 1; });
-    my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
+    my $credit_forgive = $account->add_credit(
+        {
+            amount    => 1,
+            user_id   => $patron->id,
+            interface => 'cli',
+            type      => 'FORGIVEN'
+        }
+    );
     my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
-    $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
+    $credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
+    is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
 
+    $accountline = Koha::Account::Line->new(
+        {
+            issue_id       => $checkout->id,
+            borrowernumber => $patron->id,
+            itemnumber     => $item->id,
+            branchcode     => $library->id,
+            date           => \'NOW()',
+            debit_type_code => 'OVERDUE',
+            status         => 'UNRETURNED',
+            interface      => 'cli',
+            amount => '1',
+            amountoutstanding => '1',
+        }
+    )->store();
+    my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
+    $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
+    $credit_renew = $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
     is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
 
+    my @messages = @{$credit_renew->messages};
+    is( $messages[0]->type, 'info', 'Info message added for renewal' );
+    is( $messages[0]->message, 'renewal', 'Message is "renewal"' );
+    is( $messages[0]->payload->{itemnumber}, $item->id, 'itemnumber found in payload' );
+    is( $messages[0]->payload->{due_date}, 1, 'due_date key in payload' );
+    is( $messages[0]->payload->{success}, 1, "'success' key in payload" );
+
+    t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
+    my $loser  = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $loser_account = $loser->account;
+
+    my $lost_item = $builder->build_sample_item();
+    my $lost_checkout = Koha::Checkout->new(
+        {
+            borrowernumber => $loser->id,
+            itemnumber     => $lost_item->id,
+            date_due       => $five_weeks_ago,
+            branchcode     => $library->id,
+            issuedate      => $seven_weeks_ago
+        }
+    )->store();
+
+    $lost_item->itemlost(1)->store;
+    my $processing_fee = Koha::Account::Line->new(
+        {
+            issue_id       => $lost_checkout->id,
+            borrowernumber => $loser->id,
+            itemnumber     => $lost_item->id,
+            branchcode     => $library->id,
+            date           => \'NOW()',
+            debit_type_code => 'PROCESSING',
+            status         => undef,
+            interface      => 'intranet',
+            amount => '15',
+            amountoutstanding => '15',
+        }
+    )->store();
+    my $lost_fee = Koha::Account::Line->new(
+        {
+            issue_id       => $lost_checkout->id,
+            borrowernumber => $loser->id,
+            itemnumber     => $lost_item->id,
+            branchcode     => $library->id,
+            date           => \'NOW()',
+            debit_type_code => 'LOST',
+            status         => undef,
+            interface      => 'intranet',
+            amount => '12.63',
+            amountoutstanding => '12.63',
+        }
+    )->store();
+    my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
+    my $pay_lines = [ $processing_fee, $lost_fee ];
+    $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
+
+    is( $loser->checkouts->next, undef, "Item has been returned");
+
+
+
     $schema->storage->txn_rollback;
 };
 
@@ -387,7 +468,7 @@ subtest 'Keep account info when related patron, staff, item or cash_register is
 
 subtest 'Renewal related tests' => sub {
 
-    plan tests => 7;
+    plan tests => 8;
 
     $schema->storage->txn_begin;
 
@@ -416,22 +497,25 @@ subtest 'Renewal related tests' => sub {
         interface         => 'commandline',
     })->store;
 
-    is( $line->renewable, 1, "Item is returned as renewable when it meets the conditions" );
+    is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
     $line->amountoutstanding(5);
-    is( $line->renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
+    is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
     $line->amountoutstanding(0);
     $line->debit_type_code("VOID");
-    is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
+    is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
     $line->debit_type_code("OVERDUE");
     $line->status("RETURNED");
-    is( $line->renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
+    is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
 
 
     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
-    is ($line->renew_item, undef, 'Attempt to renew fails when syspref is not set');
+    is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
+    t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
+    is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
+    t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
     is_deeply(
-        $line->renew_item,
+        $line->renew_item({ interface => 'intranet' }),
         {
             itemnumber => $item->itemnumber,
             error      => 'too_many',
@@ -594,12 +678,17 @@ subtest 'checkout() tests' => sub {
         item_id   => $item->itemnumber,
         issue_id  => $checkout->issue_id,
         type      => 'OVERDUE',
+        status    => 'UNRETURNED'
     });
 
     my $line_checkout = $line->checkout;
     is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
     is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
 
+    # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
+    # fine which would subsequently be removed by _FixOverduesOnReturn
+    t::lib::Mocks::mock_preference( 'finesMode', 'off' );
+
     my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
     is( $returned, 1, 'The item should have been returned' );
 
@@ -686,7 +775,7 @@ subtest 'credits() and debits() tests' => sub {
 
 subtest "void() tests" => sub {
 
-    plan tests => 16;
+    plan tests => 23;
 
     $schema->storage->txn_begin;
 
@@ -744,9 +833,35 @@ subtest "void() tests" => sub {
     is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
     is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
 
-    my $ret = $account_payment->void();
+    throws_ok {
+        $line1->void( { interface => 'test' } );
+    }
+    'Koha::Exceptions::Account::IsNotCredit',
+      '->void() can only be used with credits';
+
+    throws_ok {
+        $account_payment->void();
+    }
+    'Koha::Exceptions::MissingParameter',
+      "->void() requires the `interface` parameter is passed";
+
+    throws_ok {
+        $account_payment->void( { interface => 'intranet' } );
+    }
+    'Koha::Exceptions::MissingParameter',
+      "->void() requires the `staff_id` parameter is passed when `interface` equals 'intranet'";
+    throws_ok {
+        $account_payment->void( { interface => 'intranet', staff_id => $borrower->borrowernumber } );
+    }
+    'Koha::Exceptions::MissingParameter',
+      "->void() requires the `branch` parameter is passed when `interface` equals 'intranet'";
+
+    my $void = $account_payment->void({ interface => 'test' });
 
-    is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
+    is( ref($void), 'Koha::Account::Line', 'Void returns the account line' );
+    is( $void->debit_type_code, 'VOID', 'Void returns the VOID account line' );
+    is( $void->manager_id, undef, 'Void proceeds without manager_id OK if interface is not "intranet"' );
+    is( $void->branchcode, undef, 'Void proceeds without branchcode OK if interface is not "intranet"' );
     is( $account->balance(), 30, "Account balance is again 30" );
 
     $account_payment->_result->discard_changes();
@@ -755,19 +870,22 @@ subtest "void() tests" => sub {
 
     is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
     is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
-    is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
+    is( $account_payment->amount+0, -30, 'Voided payment amount is still -30' );
     is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
 
     is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
     is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
 
-    # Accountlines that are not credits should be un-voidable
-    my $line1_pre = $line1->unblessed();
-    $ret = $line1->void();
-    $line1->_result->discard_changes();
-    my $line1_post = $line1->unblessed();
-    is( $ret, undef, 'Attempted void on non-credit returns undef' );
-    is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' );
+    my $credit2 = $account->add_credit( { interface => 'test', amount => 10 } );
+    $void = $credit2->void(
+        {
+            interface => 'intranet',
+            staff_id  => $borrower->borrowernumber,
+            branch    => $branchcode
+        }
+    );
+    is( $void->manager_id, $borrower->borrowernumber, "->void stores the manager_id when it's passed");
+    is( $void->branchcode, $branchcode, "->void stores the branchcode when it's passed");
 
     $schema->storage->txn_rollback;
 };