From 9254c15bd43acf8f35af00a5dcbd624895df29be Mon Sep 17 00:00:00 2001
From: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date: Thu, 17 Oct 2019 16:48:43 +0200
Subject: [PATCH] Bug 25814: SIP: Add a message on successful checkin.

Currently, Koha does not return a message on successful SIP checkin.

This patchs adds the show_checkin_message option to SIPconfig.xml, disabled by
default. When enabled, the following message is displayed on SIP checkin:

"Item checked-in: {homebranch|permanent_location} - {location}"

The UseLocationAsAQInSIP system preference is used to determine whether the
homebranch or the permanent location will be used.

Test plan:

 - Perform a successful checkin using SIP
 - Check that the message is in the checkin response (AF field)
 - prove t/db_dependent/SIP/Transaction.t

Signed-off-by: David Nind <david@davidnind.com>
---
 C4/SIP/ILS.pm                    | 17 +++++++++-
 etc/SIPconfig.xml                |  1 +
 t/db_dependent/SIP/Transaction.t | 56 +++++++++++++++++++++++++++++++-
 3 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/C4/SIP/ILS.pm b/C4/SIP/ILS.pm
index 9bba3c80f5..8c9314e7ec 100644
--- a/C4/SIP/ILS.pm
+++ b/C4/SIP/ILS.pm
@@ -262,6 +262,18 @@ sub checkin {
         delete $item->{borrowernumber};
         delete $item->{due_date};
         $patron->{items} = [ grep { $_ ne $item_id } @{ $patron->{items} } ];
+
+        my $message = '';
+        if ($account->{show_checkin_message}) {
+            my $permanent_location;
+            if (C4::Context->preference("UseLocationAsAQInSIP")) {
+                $permanent_location = $item->{'permanent_location'};
+            } else {
+                $permanent_location = Koha::Libraries->find($item->{permanent_location})->branchname;
+            }
+            $message .= "Item checked-in: " . $permanent_location . " - " . $item->{location} . ".";
+        }
+
         # Check for overdue fines to display
         if ($account->{show_outstanding_amount}) {
             my $kohaitem = Koha::Items->find( { barcode => $item_id } );
@@ -275,10 +287,13 @@ sub checkin {
                     },
                 );
                 if ($charges) {
-                    $circ->screen_msg("You owe " . Koha::Number::Price->new( $charges->total_outstanding )->format({ with_symbol => 1}) . " for this item.");
+                    $message .= "You owe " . Koha::Number::Price->new( $charges->total_outstanding )->format({ with_symbol => 1}) . " for this item.";
                 }
             }
         }
+        if ($message) {
+            $circ->screen_msg($message);
+        }
     } else {
         # Checkin failed: Wrongbranch or withdrawn?
         # Bug 10748 with pref BlockReturnOfLostItems adds another case to come
diff --git a/etc/SIPconfig.xml b/etc/SIPconfig.xml
index 5264c2674c..bda6498ef0 100644
--- a/etc/SIPconfig.xml
+++ b/etc/SIPconfig.xml
@@ -75,6 +75,7 @@
              prevcheckout_block_checkout="0"
              overdues_block_checkout="1"
              show_outstanding_amount="1"
+             show_checkin_message="0"
              format_due_date="0"
              inhouse_item_types=""
              inhouse_patron_categories=""
diff --git a/t/db_dependent/SIP/Transaction.t b/t/db_dependent/SIP/Transaction.t
index 232e6456a5..3c59c63334 100755
--- a/t/db_dependent/SIP/Transaction.t
+++ b/t/db_dependent/SIP/Transaction.t
@@ -334,7 +334,7 @@ subtest "Placing holds via SIP check CanItemBeReserved" => sub {
 };
 
 subtest do_checkin => sub {
-    plan tests => 13;
+    plan tests => 14;
 
     my $mockILS = Test::MockObject->new;
     my $server = { ils => $mockILS };
@@ -440,6 +440,60 @@ subtest do_checkin => sub {
         is( Koha::Checkouts->search({itemnumber => $item->itemnumber})->count, 0, );
     };
 
+    subtest 'Checkin message' => sub {
+        plan tests => 3;
+        my $mockILS = Test::MockObject->new;
+        my $server = { ils => $mockILS };
+        my $library = $builder->build_object({ class => 'Koha::Libraries' });
+        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+        my $homebranch = $builder->build(
+            {
+                source => 'Branch',
+                value  => {
+                    branchcode => 'HMBR',
+                    branchname => 'Homebranch'
+                }
+            }
+        );
+
+        my $institution = {
+            id             => $library->id,
+            implementation => "ILS",
+            policy         => {
+                checkin  => "true",
+                renewal  => "true",
+                checkout => "true",
+                timeout  => 100,
+                retries  => 5,
+            }
+        };
+        my $ils = C4::SIP::ILS->new($institution);
+        my $item = $builder->build_sample_item(
+            {
+                library => $library->branchcode,
+                homebranch => $homebranch->{'branchcode'},
+                location => 'My Location',
+                permanent_location => 'My Permanent Location',
+            }
+        );
+        my $circ = $ils->checkout($patron->cardnumber, $item->barcode, undef, undef, $server->{account});
+        $circ = $ils->checkin( $item->barcode, C4::SIP::Sip::timestamp, undef, $library->branchcode, undef, undef, $server->{account} );
+        is( $circ->{screen_msg}, '', "Checkin message is not displayed when show_checkin_message is disabled" );
+
+        $server->{account}->{show_checkin_message} = 1;
+
+        $circ = $ils->checkout($patron->cardnumber, $item->barcode, undef, undef, $server->{account});
+        $circ = $ils->checkin( $item->barcode, C4::SIP::Sip::timestamp, undef, $library->branchcode, undef, undef, $server->{account} );
+        is( $circ->{screen_msg}, 'Item checked-in: Homebranch - My Location.', "Checkin message when show_checkin_message is enabled and UseLocationAsAQInSIP is disabled" );
+
+        t::lib::Mocks::mock_preference( 'UseLocationAsAQInSIP', '1' );
+
+        $circ = $ils->checkout($patron->cardnumber, $item->barcode, undef, undef, $server->{account});
+        $circ = $ils->checkin( $item->barcode, C4::SIP::Sip::timestamp, undef, $library->branchcode, undef, undef, $server->{account} );
+        is( $circ->{screen_msg}, 'Item checked-in: My Permanent Location - My Location.', "Checkin message when both show_checkin_message and UseLocationAsAQInSIP are enabled" );
+
+    };
+
     subtest 'Checkin with fines' => sub {
         plan tests => 2;
 
-- 
2.30.2