Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Koha / Account / Line.t
index 5a6677c..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 => 26;
+    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();
@@ -336,7 +333,7 @@ subtest 'apply() tests' => sub {
         }
     );
     my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
-    $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
+    $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(
@@ -355,9 +352,68 @@ subtest 'apply() tests' => sub {
     )->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->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
+    $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;
 };
 
@@ -719,7 +775,7 @@ subtest 'credits() and debits() tests' => sub {
 
 subtest "void() tests" => sub {
 
-    plan tests => 16;
+    plan tests => 23;
 
     $schema->storage->txn_begin;
 
@@ -777,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({ interface => 'test' });
+    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'";
 
-    is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
+    my $void = $account_payment->void({ interface => 'test' });
+
+    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();
@@ -788,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, -30, 'Voided payment amount is -30' );
+    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({ interface => 'test' });
-    $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;
 };