From 42a99a28c17d04fda2ec86be0bbcc85c865c6234 Mon Sep 17 00:00:00 2001
From: Thibaud Guillot <thibaud.guillot@biblibre.com>
Date: Thu, 20 Jul 2023 16:59:20 +0200
Subject: [PATCH] Bug 34326: Add forbidden notforloan status option
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Test plan:

1) Add on your sip account an option called "forbidden_notforloan_status"
   and specify values separated by commas like "1,4,6" related to your
   notforloan statuses.
2) Restart your SIP server after that
3) Launch your SIP command on checkin with some items with these
   statuses. See the normal output
4) Apply this patch
5) Do step 3 again and see the new output with forbidden status and lib
   related to

Signed-off-by: Emmanuel Bétemps <e.betemps@gmail.com>
---
 C4/SIP/ILS.pm                     |  9 +++++++++
 C4/SIP/ILS/Transaction/Checkin.pm | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/C4/SIP/ILS.pm b/C4/SIP/ILS.pm
index 8fdfc47340..9a93093fa1 100644
--- a/C4/SIP/ILS.pm
+++ b/C4/SIP/ILS.pm
@@ -273,6 +273,15 @@ sub checkin {
         return $circ;
     }
 
+   if ( $data->{messages}->{ForbiddenNotForLoanStatus} ) {
+        my $f_status = $data->{messages}->{ForbiddenNotForLoanStatus};
+        $circ->ok(0);
+        $circ->screen_msg(
+            "Return not allowed, notforloan status (" . $f_status . ")" );
+        siplog( "LOG_WARNING", "C4::SIP::ILS::Checkin - forbidden return status" );
+        return $circ;
+    }
+
     if ( !$circ->ok && $circ->alert_type && $circ->alert_type == 98 ) {    # data corruption
         $circ->screen_msg("Checkin failed: data problem");
         siplog( "LOG_WARNING", "Problem with issue_id in issues and old_issues; check the about page" );
diff --git a/C4/SIP/ILS/Transaction/Checkin.pm b/C4/SIP/ILS/Transaction/Checkin.pm
index 1ad0a4b736..05e730e2f2 100644
--- a/C4/SIP/ILS/Transaction/Checkin.pm
+++ b/C4/SIP/ILS/Transaction/Checkin.pm
@@ -55,6 +55,7 @@ sub do_checkin {
     my $cv_triggers_alert   = $account->{cv_triggers_alert};
     my $holds_block_checkin = $account->{holds_block_checkin};
     my $holds_get_captured  = $account->{holds_get_captured} // 1;
+    my @forbidden_notforloan_status = split(",",$account->{forbidden_notforloan_status});
 
     if ( !$branch ) {
         $branch = 'SIP2';
@@ -104,6 +105,22 @@ sub do_checkin {
         $return = 1 unless keys %$messages;
     }
 
+   if (@forbidden_notforloan_status) {
+        my @results = grep { $_ == $item->notforloan } @forbidden_notforloan_status;
+        if (@results) {
+            my $notforloan_desc =
+              Koha::AuthorisedValues->get_description_by_koha_field(
+                {
+                    kohafield        => 'items.notforloan',
+                    authorised_value => $item->notforloan
+                }
+              );
+            my $f_status = $item->notforloan;
+            $f_status .= "-$notforloan_desc->{lib}" if $notforloan_desc->{lib};
+            $messages->{ForbiddenNotForLoanStatus} = $f_status;
+        }
+    }
+
     # biblionumber, biblioitemnumber, itemnumber
     # borrowernumber, reservedate, branchcode
     # cancellationdate, found, reservenotes, priority, timestamp
@@ -140,6 +157,9 @@ sub do_checkin {
         $self->{item}->destination_loc( $item->homebranch );
         $self->alert_type('04');           # send to other branch
     }
+    if ( $messages->{ForbiddenNotForLoanStatus} ) {
+        $self->alert_type('99');
+    }
     if ( $messages->{ResFound} || $checkin_blocked_by_holds ) {
         if ($checkin_blocked_by_holds) {
             $self->alert_type('99');
-- 
2.39.5