From 84c907b46e1aa72f66632ae239c11e2412196871 Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Thu, 25 Jan 2024 00:35:19 +0000
Subject: [PATCH] Bug 35840: (QA follow-up) Make sure return and localuse is
 recorded and unit tests

To test:
prove t/db_dependent/Circulation.t
---
 C4/Circulation.pm            |  2 +-
 t/db_dependent/Circulation.t | 23 +++++++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index a8bf926626..5e6199f996 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2473,7 +2473,7 @@ sub AddReturn {
         ccode          => $item->ccode,
         categorycode   => $categorycode,
         interface      => C4::Context->interface,
-    }) unless $localuse;
+    }) unless ( $localuse && $stat_type eq 'localuse' );
 
     # Send a check-in slip. # NOTE: borrower may be undef. Do not try to send messages then.
     if ( $patron ) {
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 5095afde73..76119fd5c0 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -2434,7 +2434,7 @@ subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
 };
 
 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
-    plan tests => 11;
+    plan tests => 14;
 
     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
     my $patron_category_x = $builder->build_object(
@@ -2455,12 +2455,12 @@ subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
             }
         }
     );
+
     my $item_1 = $builder->build_sample_item(
         {
             library => $library->{branchcode},
         }
     );
-
     my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->barcode );
     is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
 
@@ -2474,6 +2474,7 @@ subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
     is( $stat->categorycode,   $patron->categorycode,            'Recorded a categorycode' );
     is( $stat->location,       $item_1->location,                'Recorded a location' );
 
+
     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
     my $patron_2 = $builder->build_object(
         { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
@@ -2486,6 +2487,24 @@ subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
     $item_2->discard_changes;
     ok( !$item_2->onloan, "Checked out item is returned" );
 
+    my $item_3 = $builder->build_sample_item( { library => $library->branchcode } );
+    CanBookBeIssued( $patron, $item_3->barcode );
+    my $stat_2 = Koha::Statistics->search( { itemnumber => $item_3->itemnumber } )->next;
+
+    $item_3->discard_changes;
+
+    is( $stat_2->type, 'localuse', 'Book checked out to statistical patron records a localuse' );
+
+    AddIssue( $patron_2, $item_3->barcode );
+    $item_3->discard_changes;
+
+    CanBookBeIssued( $patron, $item_3->barcode );
+    my $stat_3 = Koha::Statistics->search( { itemnumber => $item_3->itemnumber } )->next;
+
+    my $stat_4 = Koha::Statistics->search( { itemnumber => $item_3->itemnumber } )->last;
+
+    is( $stat_3->type, 'localuse', 'Book checked out to statistical patron records a localuse' );
+    is( $stat_4->type, 'return', 'Book checked out to statistical patron also records a return' );
     # TODO There are other tests to provide here
 };
 
-- 
2.30.2