Bug 22521: (QA follow-up) Update number of tests in Circulation.t
[koha-equinox.git] / t / db_dependent / Circulation.t
index cea92ab..29d13cc 100755 (executable)
 use Modern::Perl;
 use utf8;
 
-use Test::More tests => 126;
+use Test::More tests => 130;
 use Test::MockModule;
 
 use Data::Dumper;
 use DateTime;
+use Time::Fake;
 use POSIX qw( floor );
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -105,6 +106,8 @@ my $borrower = {
     branchcode => $library2->{branchcode}
 };
 
+t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
+
 # No userenv, PickupLibrary
 t::lib::Mocks::mock_preference('IndependentBranches', '0');
 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
@@ -489,9 +492,13 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
         action => "ISSUE"
     );
     my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
-    AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
+    my $dt = dt_from_string();
+    Time::Fake->offset( $dt->epoch );
+    my $datedue1 = AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
     my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
+    isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
+    Time::Fake->reset;
 
     t::lib::Mocks::mock_preference('RenewalLog', 1);
     $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
@@ -502,8 +509,8 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
 
     my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
     is( $fines->count, 2 );
-    is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
-    is( $fines->next->accounttype, 'F', 'Fine on renewed item is closed out properly' );
+    isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
+    isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
     $fines->delete();
 
 
@@ -684,11 +691,12 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
         $account->add_debit(
             {
                 amount      => $fines_amount,
-                type        => 'fine',
+                interface   => 'test',
+                type        => 'overdue',
                 item_id     => $item_to_auto_renew->{itemnumber},
                 description => "Some fines"
             }
-        )->accounttype->('F')->store;
+        )->status('RETURNED')->store;
         ( $renewokay, $error ) =
           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
@@ -697,11 +705,12 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
         $account->add_debit(
             {
                 amount      => $fines_amount,
-                type        => 'fine',
+                interface   => 'test',
+                type        => 'overdue',
                 item_id     => $item_to_auto_renew->{itemnumber},
                 description => "Some fines"
             }
-        )->accounttype->('F')->store;
+        )->status('RETURNED')->store;
         ( $renewokay, $error ) =
           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
@@ -710,11 +719,12 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
         $account->add_debit(
             {
                 amount      => $fines_amount,
-                type        => 'fine',
+                interface   => 'test',
+                type        => 'overdue',
                 item_id     => $item_to_auto_renew->{itemnumber},
                 description => "Some fines"
             }
-        )->accounttype->('F')->store;
+        )->status('RETURNED')->store;
         ( $renewokay, $error ) =
           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
@@ -848,14 +858,14 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     );
 
     my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
-    is( $line->accounttype, 'FU', 'Account line type is FU' );
-    is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' );
+    is( $line->accounttype, 'OVERDUE', 'Account line type is OVERDUE' );
+    is( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
     is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
     is( $line->amount, '15.000000', 'Account line amount is 15.00' );
     is( $line->issue_id, $issue->id, 'Account line issue id matches' );
 
     my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
-    is( $offset->type, 'Fine', 'Account offset type is Fine' );
+    is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
     is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
 
     t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
@@ -864,7 +874,8 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     LostItem( $item_1->itemnumber, 'test', 1 );
 
     $line = Koha::Account::Lines->find($line->id);
-    is( $line->accounttype, 'F', 'Account type correctly changed from FU to F' );
+    is( $line->accounttype, 'OVERDUE', 'Account type remains as OVERDUE' );
+    isnt( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
 
     my $item = Koha::Items->find($item_1->itemnumber);
     ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
@@ -916,11 +927,21 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
 
+    my $manager = $builder->build_object({ class => "Koha::Patrons" });
+    t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
     $checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
     LostItem( $item_3->itemnumber, 'test', 0 );
     my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
     is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
+    is(
+        $accountline->description,
+        sprintf( "%s %s %s",
+            $item_3->biblio->title  || '',
+            $item_3->barcode        || '',
+            $item_3->itemcallnumber || '' ),
+        "Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
+    );
   }
 
 {
@@ -1900,6 +1921,64 @@ subtest 'AddReturn + suspension_chargeperiod' => sub {
     );
 };
 
+subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
+    plan tests => 2;
+
+    my $library = $builder->build( { source => 'Branch' } );
+    my $patron1 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value  => {
+                branchcode => $library->{branchcode},
+                firstname => "Happy",
+                surname => "Gilmore",
+            }
+        }
+    );
+    my $patron2 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value  => {
+                branchcode => $library->{branchcode},
+                firstname => "Billy",
+                surname => "Madison",
+            }
+        }
+    );
+
+    C4::Context->_new_userenv('xxx');
+    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Random Library', '', '', '');
+
+    my $biblioitem = $builder->build( { source => 'Biblioitem' } );
+    my $biblionumber = $biblioitem->{biblionumber};
+    my $item = $builder->build(
+        {   source => 'Item',
+            value  => {
+                homebranch    => $library->{branchcode},
+                holdingbranch => $library->{branchcode},
+                notforloan    => 0,
+                itemlost      => 0,
+                withdrawn     => 0,
+                biblionumber  => $biblionumber,
+            }
+        }
+    );
+
+    my ( $error, $question, $alerts );
+    my $issue = AddIssue( $patron1->unblessed, $item->{barcode} );
+
+    t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
+    ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
+    is( $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER question flag should be set if AutoReturnCheckedOutItems is disabled and item is checked out to another' );
+
+    t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 1);
+    ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
+    is( $alerts->{RETURNED_FROM_ANOTHER}->{patron}->borrowernumber, $patron1->borrowernumber, 'RETURNED_FROM_ANOTHER alert flag should be set if AutoReturnCheckedOutItems is enabled and item is checked out to another' );
+
+    t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
+};
+
+
 subtest 'AddReturn | is_overdue' => sub {
     plan tests => 5;
 
@@ -1971,7 +2050,7 @@ subtest 'AddReturn | is_overdue' => sub {
     # specify dropbox date 5 days later => overdue, or... not
     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
     AddReturn( $item->{barcode}, $library->{branchcode}, $five_days_ago );
-    is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue in dropbox mode' ); # FIXME? This is weird, the FU fine is created ( _CalculateAndUpdateFine > C4::Overdues::UpdateFine ) then remove later (in _FixOverduesOnReturn). Looks like it is a feature
+    is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue in dropbox mode' ); # FIXME? This is weird, the OVERDUE fine is created ( _CalculateAndUpdateFine > C4::Overdues::UpdateFine ) then remove later (in _FixOverduesOnReturn). Looks like it is a feature
     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
 };
 
@@ -2004,6 +2083,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         plan tests => 10;
 
         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+        my $manager = $builder->build_object({ class => "Koha::Patrons" });
+        t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
 
         my $item = $builder->build_sample_item(
             {
@@ -2043,7 +2124,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         # Write off the debt
         my $credit = $account->add_credit(
             {   amount => $account->balance,
-                type   => 'writeoff'
+                type   => 'writeoff',
+                interface => 'test',
             }
         );
         $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
@@ -2103,7 +2185,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         # Write off the debt
         my $credit = $account->add_credit(
             {   amount => $account->balance,
-                type   => 'payment'
+                type   => 'payment',
+                interface => 'test',
             }
         );
         $credit->apply( { debits => $debts, offset_type => 'Payment' } );
@@ -2223,7 +2306,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         my $payment_amount = 27;
         my $payment        = $account->add_credit(
             {   amount => $payment_amount,
-                type   => 'payment'
+                type   => 'payment',
+                interface => 'test',
             }
         );
 
@@ -2233,7 +2317,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         my $write_off_amount = 25;
         my $write_off        = $account->add_credit(
             {   amount => $write_off_amount,
-                type   => 'writeoff'
+                type   => 'writeoff',
+                interface => 'test',
             }
         );
         $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
@@ -2324,7 +2409,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
         my $payment_amount = 27;
         my $payment        = $account->add_credit(
             {   amount => $payment_amount,
-                type   => 'payment'
+                type   => 'payment',
+                interface => 'test',
             }
         );
         $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' });
@@ -2334,9 +2420,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
             'Payment applied'
         );
 
-        # TODO use add_debit when time comes
         my $manual_debit_amount = 80;
-        C4::Accounts::manualinvoice( $patron->id, undef, undef, 'FU', $manual_debit_amount );
+        $account->add_debit( { amount => $manual_debit_amount, type => 'overdue', interface =>'test' } );
 
         is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
 
@@ -2347,7 +2432,7 @@ subtest '_FixAccountForLostAndReturned' => sub {
 
         is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
 
-        my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'FU' })->next;
+        my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next;
         is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
     };
 };
@@ -2374,11 +2459,12 @@ subtest '_FixOverduesOnReturn' => sub {
     my $accountline = Koha::Account::Line->new(
         {
             borrowernumber => $patron->{borrowernumber},
-            accounttype    => 'FU',
+            accounttype    => 'OVERDUE',
+            status         => 'UNRETURNED',
             itemnumber     => $item->itemnumber,
             amount => 99.00,
             amountoutstanding => 99.00,
-            lastincrement => 9.00,
+            interface => 'test',
         }
     )->store();
 
@@ -2387,13 +2473,14 @@ subtest '_FixOverduesOnReturn' => sub {
     $accountline->_result()->discard_changes();
 
     is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
-    is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
+    is( $accountline->status, 'RETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status RETURNED )');
 
 
     ## Run again, with exemptfine enabled
     $accountline->set(
         {
-            accounttype    => 'FU',
+            accounttype    => 'OVERDUE',
+            status         => 'UNRETURNED',
             amountoutstanding => 99.00,
         }
     )->store();
@@ -2404,7 +2491,7 @@ subtest '_FixOverduesOnReturn' => sub {
     my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
 
     is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
-    is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
+    is( $accountline->status, 'FORGIVEN', 'Open fine ( account type OVERDUE ) has been set to fine forgiven ( status FORGIVEN )');
     is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
     is( $offset->amount, '-99.000000', "Amount of offset is correct" );
 };