From e4b466b1626dae35a8a18093dae6061844750209 Mon Sep 17 00:00:00 2001
From: Laura Escamilla <laura.escamilla@bywatersolutions.com>
Date: Tue, 27 Feb 2024 20:03:26 +0000
Subject: [PATCH] Bug 27753: Added two new system preferences for automatically
 resolving claims.

Bug 27753: Removed syspref code in order to move it over to Circulation.pl

Bug: 27753 - Updates to AutoClaimResolutionOnCheckin

Bug 27753: Added code for AutoClaimResolutionOnCheckout

Bug 27753: Added CLAIM_RESOLUTION alert

Bug 27753: Made updates to AutoClaimResolved messages
---
 C4/Circulation.pm                             | 44 +++++++++++++------
 circ/circulation.pl                           | 29 ++++++++++++
 circ/returns.pl                               |  3 ++
 .../data/mysql/atomicupdate/bug_27753.pl      | 20 +++++++++
 installer/data/mysql/mandatory/sysprefs.sql   |  2 +
 .../admin/preferences/circulation.pref        | 12 +++++
 .../prog/en/modules/circ/circulation.tt       |  5 ++-
 .../prog/en/modules/circ/returns.tt           |  6 ++-
 8 files changed, 106 insertions(+), 15 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_27753.pl

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 109809d4b9..83d09e9635 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2248,6 +2248,37 @@ sub AddReturn {
         }
     }
 
+if (C4::Context->preference('ClaimReturnedLostValue')) {
+    my $autoClaimReturnCheckin = C4::Context->preference('AutoClaimReturnStatusOnCheckin');
+
+    my $claims = Koha::Checkouts::ReturnClaims->search(
+        {
+            itemnumber => $item->id,
+            resolution => undef,
+        }
+    );
+
+    if ($claims->count) {
+        if ($autoClaimReturnCheckin) {
+            my $claim = $claims->next;
+
+            my $patron_id = $patron->borrowernumber;
+            my $resolution = $autoClaimReturnCheckin;
+
+            $claim->resolve(
+                {
+                    resolution => $resolution,
+                    resolved_by => $patron_id,
+                }
+            );
+
+            $messages->{ClaimAutoResolved} = $claim;
+        } else {
+            $messages->{ReturnClaims} = $claims;
+        }
+    }
+}
+
     # check if the return is allowed at this branch
     my ($returnallowed, $message) = CanBookBeReturned($item->unblessed, $branch);
     unless ($returnallowed){
@@ -2546,19 +2577,6 @@ sub AddReturn {
         }
     }
 
-    if ( C4::Context->preference('ClaimReturnedLostValue') ) {
-        my $claims = Koha::Checkouts::ReturnClaims->search(
-           {
-               itemnumber => $item->id,
-               resolution => undef,
-           }
-        );
-
-        if ( $claims->count ) {
-            $messages->{ReturnClaims} = $claims;
-        }
-    }
-
     # Check for bundle status
     if ( $item->in_bundle ) {
         my $host = $item->bundle_host;
diff --git a/circ/circulation.pl b/circ/circulation.pl
index a6a30ce288..07ec0c192f 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -39,6 +39,7 @@ use C4::Members;
 use C4::Biblio qw( TransformMarcToKoha );
 use C4::Search qw( new_record_from_zebra );
 use C4::Reserves qw( ModReserveAffect );
+use Koha::Checkouts::ReturnClaims;
 use Koha::Holds;
 use C4::Context;
 use CGI::Session;
@@ -468,6 +469,34 @@ if (@$barcodes && $op eq 'cud-checkout') {
         }
     }
 
+    if (C4::Context->preference('ClaimReturnedLostValue')) {
+        my $autoClaimReturnCheckout = C4::Context->preference('AutoClaimReturnStatusOnCheckout');
+
+        my $claims = Koha::Checkouts::ReturnClaims->search(
+            {
+                itemnumber => $item->id,
+            }
+        );
+    
+     if ($claims->count) {
+         if ($autoClaimReturnCheckout) {
+            my $claim = $claims->next;
+
+            my $patron_id = $patron->borrowernumber;
+            my $resolution = $autoClaimReturnCheckout; 
+
+             $claim->resolve(
+                  {
+                     resolution  => $resolution,
+                     resolved_by => $patron_id,
+                  }
+            );
+
+            $template_params->{CLAIM_RESOLUTION} = $claim; 
+        }
+    }
+}
+
     if ($needsconfirmation->{RESERVE_WAITING} or $needsconfirmation->{RESERVED} or $needsconfirmation->{TRANSFERRED} or $needsconfirmation->{PROCESSING}){
         $template->param(
             reserveborrowernumber => $needsconfirmation->{'resborrowernumber'},
diff --git a/circ/returns.pl b/circ/returns.pl
index 5b119e3726..b65ee294fc 100755
--- a/circ/returns.pl
+++ b/circ/returns.pl
@@ -739,6 +739,9 @@ foreach my $code ( keys %$messages ) {
     }
     elsif ( $code eq 'ReturnClaims' ) {
         $template->param( ReturnClaims => $messages->{ReturnClaims} );
+    }
+    elsif ( $code eq 'ClaimAutoResolved' ) {
+        $template->param( ClaimAutoResolved => $messages->{ClaimAutoResolved} );
     } elsif ( $code eq 'RecallFound' ) {
         ;
     } elsif ( $code eq 'RecallNeedsTransfer' ) {
diff --git a/installer/data/mysql/atomicupdate/bug_27753.pl b/installer/data/mysql/atomicupdate/bug_27753.pl
new file mode 100644
index 0000000000..d2698df01d
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_27753.pl
@@ -0,0 +1,20 @@
+use Modern::Perl;
+
+return {
+    bug_number  => "BUG_27753",
+    description => "Automate the resoluton of a claim upon return",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        $dbh->do(q{INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
+                ('AutoClaimReturnStatusOnCheckin','','NULL','When in use this syspref will automatically resolve the claim return and will update the lost authorized value upon check in.','Free')});
+
+say $out "Added new system preference 'AutoClaimReturnStatusOnCheckin'";
+
+        $dbh->do(q{INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
+                ('AutoClaimReturnStatusOnCheckout','','NULL','When in use this syspref will automatically resolve the claim return and will update the lost authorized value upon check out.','Free')});
+
+        say $out "Added new system preference 'AutoClaimReturnStatusOnCheckout'";
+    },
+};
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index 669074aa50..6ac6a44ce1 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -82,6 +82,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('AuthSuccessLog','0',NULL,'If enabled, log successful authentications','YesNo'),
 ('AutoApprovePatronProfileSettings', '0', '', 'Automatically approve patron profile changes from the OPAC.', 'YesNo'),
 ('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
+('AutoClaimReturnStatusOnCheckin','','NULL','When in use this syspref will automatically resolve the claim return and will update the lost authorized value upon check in.','Free'),
+('AutoClaimReturnStatusOnCheckout','','NULL','When in use this syspref will automatically resolve the claim return and will update the lost authorized value upon check out.','Free'),
 ('autoControlNumber','OFF','biblionumber|OFF','Used to autogenerate a Control Number: biblionumber will be as biblionumber, OFF will leave the field as it is;','Choice'),
 ('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
 ('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
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 080e8fa494..99a3ea135e 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
@@ -1334,6 +1334,18 @@ Circulation:
                   charge: charge a lost fee
                   no_charge: don't charge a lost fee
             - .
+        -
+            - Automatically resolve the claim and change the status to the <a href="/cgi-bin/koha/admin/authorised_values.pl?searchfield=RETURN_CLAIM_RESOLUTION">Return claim resolution</a> authorized value
+            - pref: AutoClaimReturnStatusOnCheckin
+              choices: authval
+              source: RETURN_CLAIM_RESOLUTION
+            - upon check in.
+        -
+            - Automatically resolve the claim and change the status to the <a href="/cgi-bin/koha/admin/authorised_values.pl?searchfield=RETURN_CLAIM_RESOLUTION">Return claim resolution</a>      authorized value
+            - pref: AutoClaimReturnStatusOnCheckout
+              choices: authval
+              source: RETURN_CLAIM_RESOLUTION
+            - upon check out.
         -
             - Use the <a href="/cgi-bin/koha/admin/authorised_values.pl?searchfield=LOST">LOST</a> authorized value
             - pref: ClaimReturnedLostValue
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 3eb1a55434..529c2e5a91 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -78,7 +78,10 @@
                             <span class="mats_spec_message">[% ADDITIONAL_MATERIALS | html %]</span>
                         </div>
                     [% END %]
-
+                   
+                    [% IF CLAIM_RESOLUTION %]
+                        <div class="dialog message">The previously claimed returned item has been located, automatically resolving the associated claim.</div>
+                    [% END %]
                     [% IF ( alert.ITEM_LOST ) %]
                         <div class="dialog message">This item has been lost with a status of "[% alert.ITEM_LOST | html %]".</div>
                     [% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
index 6aecdfefbe..a12770898d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
@@ -191,7 +191,11 @@
                             [% END %]
 
                             <!-- Item has return claim(s) -->
-                            [% IF ( ReturnClaims ) %]
+                            [% IF ( ClaimAutoResolved ) %]
+                            <div class="dialog alert return-claim">
+                            <p><strong>The previously claimed returned item has been found, automatically resolving the associated claim.</strong></p>
+                             </div>
+                            [% ELSIF ( ReturnClaims ) %]
                                 <div class="dialog alert return-claim">
                                     <h3>This item has been claimed as returned by:</h3>
                                     <ul>
-- 
2.30.2