From ec6a42eea94836240fa026d9c859334000e2f033 Mon Sep 17 00:00:00 2001
From: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Date: Wed, 8 Jan 2025 16:26:37 +0100
Subject: [PATCH] Bug 38847 - Renewing a child without guarantor whith
 ChildNeedsGuarantor results in an internal server error (edit) o

Test plan:
1 - Create a child, make sure it has no guarantor
2 - Set ChildNeedsGuarantor to Yes
3 - Renew the child (on morember.pl or circulation.pl) -> you will have an internal server error
4 - Apply patch
5 - Go to moremember.pl, try and renew the patron -> you will be
  redirected to moremember with an error message
6 - Go to circulation.pl, try and renew the patron -> you will be
  redirected to circultation.pl with an error message
7 - Create a guarantor for the patron
8 - Repeat 5 & 6 and notice it is now renewed
---
 circ/circulation.pl                           |  1 +
 .../prog/en/modules/circ/circulation.tt       |  4 ++++
 .../prog/en/modules/members/moremember.tt     |  4 ++++
 members/setstatus.pl                          | 24 ++++++++++++++++---
 4 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/circ/circulation.pl b/circ/circulation.pl
index 968470e6..75dc2670 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -785,6 +785,7 @@ $template->param(
     patron_lists_count      => $patron_lists_count,
     override_high_holds     => $override_high_holds,
     nopermission            => scalar $query->param('nopermission'),
+    noguarantor             => scalar $query->param('noguarantor'),
     autoswitched            => $autoswitched,
     logged_in_user          => $logged_in_user,
 );
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 a3c9d73f..a33c4174 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -105,6 +105,10 @@
                         <div class="alert alert-warning">Staff members are not allowed to discharge borrowers, nor borrowers to request a discharge.</div>
                     [% END %]
 
+                    [% IF ( noguarantor ) %]
+                        <div class="alert alert-warning">This patron could not be renewed: they need a guarantor.</div>
+                    [% END %]
+
                     [% IF is_anonymous %]
                         <div class="alert alert-warning">This is the anonymous patron, so circulation is disabled.</div>
                     [% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index a12b9e3a..8fe10d8a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -81,6 +81,10 @@
                                 <h3>Unable to delete patron</h3>
                                 <p>Insufficient privileges.</p>
                             [% END %]
+                            [% IF ( error == 'CANT_RENEW_CHILD_WITHOUT_GUARANTOR' ) %]
+                                <h3>Unable to renew patron</h3>
+                                <p>They must have a guarantor defined</p>
+                            [% END %]
                         </div>
                     [% END %]
 
diff --git a/members/setstatus.pl b/members/setstatus.pl
index 83ef1590..80b97070 100755
--- a/members/setstatus.pl
+++ b/members/setstatus.pl
@@ -24,6 +24,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
+use Try::Tiny;
 
 use CGI qw ( -utf8 );
 use C4::Auth qw( checkauth );
@@ -47,13 +48,24 @@ my $dateexpiry;
 my $logged_in_user = Koha::Patrons->find( { userid =>  $loggedinuserid } );
 my $patron         = Koha::Patrons->find( $borrowernumber );
 
+my $cannot_renew = '0';
+
 # Ideally we should display a warning on the interface if the logged in user is
 # not allowed to modify this patron.
 # But a librarian is not supposed to hack the system
 if ( $logged_in_user->can_see_patron_infos($patron) ) {
     if ( $reregistration eq 'y' ) {
+
         # re-reregistration function to automatic calcul of date expiry
-        $dateexpiry = $patron->renew_account;
+        try {
+            $dateexpiry = $patron->renew_account;
+        } catch {
+            if ( ref($_) eq 'Koha::Exceptions::Patron::Relationship::NoGuarantor' ) {
+                $cannot_renew = '1';
+            } else {
+                $_->rethrow();
+            }
+        }
     } else {
         my $sth = $dbh->prepare("UPDATE borrowers SET debarred = ?, debarredcomment = '' WHERE borrowernumber = ?");
         $sth->execute( $status, $borrowernumber );
@@ -62,13 +74,19 @@ if ( $logged_in_user->can_see_patron_infos($patron) ) {
 }
 
 if($destination eq "circ"){
-    if($dateexpiry){
+    if ($cannot_renew) {
+        print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&noguarantor=1");
+    } elsif ($dateexpiry) {
         print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&was_renewed=1");
     } else {
         print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
     }
 } else {
-    if($dateexpiry){
+    if ($cannot_renew) {
+        print $input->redirect(
+            "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber&error=CANT_RENEW_CHILD_WITHOUT_GUARANTOR"
+        );
+    } elsif ($dateexpiry) {
         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber&was_renewed=1");
     } else {
         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
-- 
2.30.2