From 4449cbe2a3cc0cbd9e72795a8e16ba3c13f59236 Mon Sep 17 00:00:00 2001
From: jeremy breuillard <jeremy.breuillard@biblibre.com>
Date: Mon, 9 May 2022 11:42:55 +0200
Subject: [PATCH] Bug 31521: Configure behaviour when checking in a reserved
 item at SCO

This patch adds a system preference (SCOCheckinIfReservedBehaviour)
which allow to control the behaviour of SCO checkin when the returned
item is reserved:
- allow checkin (current behaviour)
- allow checkin and show a message
- forbid checkin and show a message

The message is configurable is another system preference
(SCOCheckinIfReservedMessage)

Test plan:
1. Apply patch, run updatedatabase.pl and restart koha
2. Enable WebBasedSelfCheck and SCOAllowCheckin
3. (Staff) Set SCOCheckinIfReservedBehaviour to "Forbid checkin & show
   message"
4. (Staff) Write some text in SCOCheckinIfReservedMessage
5. (Staff) Checkout an item I to patron A
6. (Staff) Place a hold on item I for patron B
7. (SCO) Log in to SCO with patron A
8. (SCO) Try to return item I
   The configured message should be displayed and item I should still be
   checked out
9. (Staff) Set SCOCheckinIfReservedBehaviour to "Allow checkin & show message"
10. (SCO) Try to return item I
    The configured message should be displayed and item I should have
    been checked in
11. (Staff) Checkout item I to patron A again
12. (Staff) Set SCOCheckinIfReservedBehaviour to "Allow checkin"
13. (SCO) Try to return item I
    The configured message should NOT be displayed and item I should
    have been checked in
---
 .../data/mysql/atomicupdate/bug-31521.pl      | 20 +++++++++++++++++++
 installer/data/mysql/mandatory/sysprefs.sql   |  2 ++
 .../admin/preferences/circulation.pref        | 12 +++++++++++
 .../bootstrap/en/modules/sco/sco-main.tt      | 10 ++++++++++
 opac/sco/sco-main.pl                          |  5 +++++
 5 files changed, 49 insertions(+)
 create mode 100644 installer/data/mysql/atomicupdate/bug-31521.pl

diff --git a/installer/data/mysql/atomicupdate/bug-31521.pl b/installer/data/mysql/atomicupdate/bug-31521.pl
new file mode 100644
index 0000000000..fca18ecbb9
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug-31521.pl
@@ -0,0 +1,20 @@
+use Modern::Perl;
+
+return {
+    bug_number => "31521",
+    description => "Add sysprefs SCOCheckinIfReservedBehaviour and SCOCheckinIfReservedMessage",
+    up => sub {
+        my ($args) = @_;
+        my ($dbh, $out) = @$args{qw(dbh out)};
+
+        $dbh->do(q{
+            INSERT IGNORE INTO systempreferences (`variable`, `value`, `options`, `explanation`, `type`)
+            VALUES ('SCOCheckinIfReservedMessage','', NULL,'Message to display if SCOCheckinIfReservedBehaviour is "Alert" or "Forbid"','Free')
+        });
+
+        $dbh->do(q{
+            INSERT IGNORE INTO systempreferences (`variable`, `value`,`options`,`explanation`, `type`)
+            VALUES ('SCOCheckinIfReservedBehaviour', 'Allow', 'Allow|Alert|Forbid', 'Behaviour of SCO checkin when the returned item is reserved', 'Choice')
+        });
+    },
+};
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index 9a5cb2e0a8..b516b5330f 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -615,6 +615,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('RoutingListNote','To change this note edit <a href=\"/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=RoutingListNote#jumped\">RoutingListNote</a> system preference.','70|10','Define a note to be shown on all routing lists','Textarea'),
 ('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
 ('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'),
+('SCOCheckinIfReservedMessage','', NULL,'Message to display if SCOCheckinIfReservedBehaviour is "Alert" or "Forbid"','Free'),
+('SCOCheckinIfReservedBehaviour', 'Allow', 'Allow|Alert|Forbid', 'Behaviour of SCO checkin when the returned item is reserved', 'Choice'),
 ('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
 ('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
 ('SCOUserJS','',NULL,'Define custom javascript for inclusion in the SCO module','free'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
index f99af038e8..915647aa93 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
@@ -1163,6 +1163,18 @@ Circulation:
               type: textarea
               syntax: css
               class: code
+        -
+            - "Message displayed when there are holds during a self checkin"
+            - pref: SCOCheckinIfReservedMessage
+              type: textarea
+        -
+            - "Behavior for the self checkin when there are holds on the returned item"
+            - pref: SCOCheckinIfReservedBehaviour
+              choices:
+                  Alert: Allow checkin & show message
+                  Allow: Allow checkin
+                  Forbid: Forbid checkin & show message
+            - "</br>The message shown can be configured in system preference SCOCheckinIfReservedMessage"
         -
             - pref: ShowPatronImageInWebBasedSelfCheck
               choices:
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/sco/sco-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/sco/sco-main.tt
index 7feec7e2fc..dc147d98a2 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/sco/sco-main.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/sco/sco-main.tt
@@ -214,11 +214,21 @@
                         <div class="alert alert-info">
                             <p>Item not checked in: please see circulation staff for assistance</p>
                         </div>
+                        [% IF Koha.Preference('SCOCheckinIfReservedBehaviour') == 'Forbid' %]
+                            <div class="alert alert-warning">
+                                [% Koha.Preference('SCOCheckinIfReservedMessage') | html %]
+                            </div>
+                        [% END %]
                     [% ELSIF ( returned == 1 ) %]
                         <span class="sco-alert-success return"></span>
                         <div class="alert alert-info">
                             <p>Item checked in</p>
                         </div>
+                        [% IF Koha.Preference('SCOCheckinIfReservedBehaviour') == 'Alert' %]
+                            <div class="alert alert-warning">
+                                [% Koha.Preference('SCOCheckinIfReservedMessage') | html %]
+                            </div>
+                        [% END %]
                     [% END %]
 
 
diff --git a/opac/sco/sco-main.pl b/opac/sco/sco-main.pl
index ac89db749c..478e7c54cc 100755
--- a/opac/sco/sco-main.pl
+++ b/opac/sco/sco-main.pl
@@ -158,6 +158,11 @@ if ( $patron && $op eq "returnbook" && $allowselfcheckreturns ) {
           unless $patron->checkouts->find( { itemnumber => $item->itemnumber } );
     }
 
+    if ($success && C4::Context->preference('SCOCheckinIfReservedBehaviour') eq 'Forbid') {
+        my $holds_count = Koha::Holds->search({ itemnumber => $item->itemnumber });
+        $success = 0 if $holds_count > 0;
+    }
+
     if ( $success ) {
         ($success) = AddReturn( $barcode, $branch )
     }
-- 
2.30.2