From 077a3d1a93ce75411c995d02e5dfb4eb137fef87 Mon Sep 17 00:00:00 2001
From: Alex Buckley <alexbuckley@catalyst.net.nz>
Date: Mon, 16 Mar 2020 04:10:54 +0000
Subject: [PATCH] Bug 23808: Pre-fill guarantor fields when adding guarantee to
 guarantor

Bug 14570 removed the guarantor pre-fill functionality when selecting
'Add guarantee' to an Adult patron. This is because guarantor
information would now only display if (1) the patron record exists
(which it won't when first adding guarantee to guarantor record) and (2)
if there is already a guarantor added to a guarantee

This patchfix will pre-fill guarantor fields if no relationship (existing
guarantor data exists) and a guarantorid is handed to memberenty.pl in
URL when clicking 'Add guarantee' button on Adults patron record.

Test plan:
1. Add adult patron
2. Select 'Add guarantee'
3. Observe no details of the adult patron
are displaying in the 'Guarantor information' section of the memberentry.pl
page

4. Select 'Search to add', search for your adult patron and choose
'Select' to add them as guarantor

5. Fill out rest of memberentry.pl and 'Save'

6. Observe adult is showing as the guarantor

7. Apply patch

8. Run tests:
sudo koha-shell <instancename>
prove xt
prove t

9. Confirm tests pass

10. Return to your adult patron

11. Select 'Add guarantee'

12. Observe in 'Guarantor information' section of memberentry.pl
pre-filled is the 'patron #' (borrowernumber), surname, firstname

13. Fill out the rest of memberenty.pl and save and confirm your adult
patron is showing as the guarantor

14. Repeat steps 10,11 and 12 and in the 'Guarantor information' select
'Search to add' and add another adult as guarantor

15. Fill out the rest of memberentry.pl and 'Save' and notice with this
patch applied you can still add multiple guarantors successfully

Sponsored-By: South Taranaki Distict Libraries, NZ
---
 .../prog/en/modules/members/memberentrygen.tt      | 50 +++++++++++++++++++
 members/memberentry.pl                             | 57 ++++++++++++++++------
 2 files changed, 93 insertions(+), 14 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
index d4db2e37b3..5bf276015e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
@@ -372,6 +372,55 @@
                                             <legend>Guarantor information</legend>
 
                                             <span id="guarantor_relationships">
+                                            [% IF (!relationships) %]
+                                                <li id="contact-details">
+                                                    <span class="label">Patron #:</span>
+                                                    [% IF guarantorid %]
+                                                          [% IF logged_in_user.can_see_patron_infos( guarantor ) %]
+                                                              <a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% guarantorid | uri %]" target="blank">[% guarantorid | html %]</a>
+                                                          [% ELSE %]
+                                                              [% guarantorid | html %]
+                                                       [% END %]
+                                                       <input type="hidden" class="new_guarantor_id" name="new_guarantor_id" value="[% guarantorid | html %]" />
+                                                    [% END %]
+                                                </li>
+                                                [% UNLESS nocontactname %]
+                                                <li>
+                                                    <label for="contactname">Surname: </label>
+                                                    [% IF ( guarantorid ) %]
+                                                          <span>[% contactname | html %]</span>
+                                                          <input name="contactname" id="contactname" type="hidden" size="20" value="[% contactname | html %]" />
+                                                    [% ELSE %]
+                                                          <input name="contactname" id="contactname" type="text" size="20" value="[% contactname | html %]" />
+                                                    [% END %]
+                                                </li>
+                                                [% END %]
+                                                [% UNLESS nocontactfirstname %]
+                                                <li>
+                                                    <label for="contactfirstname">First name: </label>
+                                                    [% IF ( guarantorid ) %]
+                                                          <span>[% contactfirstname | html %]</span>
+                                                          <input name="contactfirstname" id="contactfirstname" type="hidden" size="20" value="[% contactfirstname | html %]" />
+                                                    [% ELSE %]
+                                                          <input name="contactfirstname" id="contactfirstname" type="text" size="20" value="[% contactfirstname | html %]" />
+                                                    [% END %]
+                                                </li>
+                                               [% END %]
+                                               [% IF ( relshiploop ) %]
+                                               <li>
+                                                    <label for="relationship">Relationship: </label>
+                                                    <select name="relationship" id="relationship" >
+                                                    [% FOREACH relshiploo IN relshiploop %]
+                                                          [% IF ( relshiploo.selected ) %]
+                                                              <option value="[% relshiploo.relationship | html %]" selected="selected" >[% relshiploo.relationship | html %]</option>
+                                                          [% ELSE %]
+                                                              <option value="[% relshiploo.relationship | html %]">[% relshiploo.relationship | html %]</option>
+                                                          [% END %]
+                                                    [% END %]
+                                                    </select>
+                                               </li>
+                                               [% END %]
+                                            [% ELSE %]
                                                 [% FOREACH r IN relationships %]
                                                     <fieldset class="rows">
                                                         <ol>
@@ -417,6 +466,7 @@
                                                         </ol>
                                                     </fieldset>
                                                 [% END # END relationships foreach %]
+                                            [% END %]
                                             </span> <!-- #/guarantor_relationships -->
 
                                             <fieldset class="rows guarantor" id="guarantor_template">
diff --git a/members/memberentry.pl b/members/memberentry.pl
index 902beeb0e8..0a45eb13f6 100755
--- a/members/memberentry.pl
+++ b/members/memberentry.pl
@@ -83,6 +83,7 @@ if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
     $template->param( sms_providers => \@providers );
 }
 
+my $guarantorid    = $input->param('guarantor_id');
 my $actionType     = $input->param('actionType') || '';
 my $modify         = $input->param('modify');
 my $delete         = $input->param('delete');
@@ -98,26 +99,13 @@ $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate');    # FIXME hack to rep
                                      # isn't a duplicate.  Marking FIXME because this
                                      # script needs to be refactored.
 my $nok           = $input->param('nok');
+my $guarantorinfo = $input->param('guarantorinfo');
 my $step          = $input->param('step') || 0;
 my @errors;
 my $borrower_data;
 my $NoUpdateLogin;
 my $userenv = C4::Context->userenv;
 
-## Deal with guarantor stuff
-$template->param( relationships => scalar $patron->guarantor_relationships ) if $patron;
-
-my $guarantor_id = $input->param('guarantor_id');
-my $guarantor = undef;
-$guarantor = Koha::Patrons->find( $guarantor_id ) if $guarantor_id;
-$template->param( guarantor => $guarantor );
-
-my @delete_guarantor = $input->multi_param('delete_guarantor');
-foreach my $id ( @delete_guarantor ) {
-    my $r = Koha::Patron::Relationships->find( $id );
-    $r->delete() if $r;
-}
-
 ## Deal with debarments
 $template->param(
     debarments => scalar GetDebarments( { borrowernumber => $borrowernumber } ) );
@@ -248,6 +236,45 @@ if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' )
     }
 }
 
+
+
+## Deal with guarantor stuff
+$template->param( relationships => scalar $patron->guarantor_relationships ) if $patron;
+my $guarantor_id = $input->param('guarantor_id');
+my $guarantor = undef;
+$guarantor = Koha::Patrons->find( $guarantor_id ) if $guarantor_id;
+$template->param( guarantor => $guarantor );
+
+# Pre-fill guarantor fields if adding a guarantee to guarantor record using 'Add guarantee' button
+# in this case the guarantee patron record does not already exist
+
+if (!$patron) {
+    if ( $guarantorid ) {
+       if (my $guarantor = Koha::Patrons->find( $guarantorid )) {
+           my $guarantordata = $guarantor->unblessed;
+           $categorycode = $guarantordata->{categorycode} eq 'I' ? 'P' : 'C';
+           $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
+           $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
+           $newdata{'contactname'}     = $guarantordata->{'surname'};
+           $newdata{'contacttitle'}    = $guarantordata->{'title'};
+           if ( $op eq 'add' ) {
+             foreach (qw(streetnumber address streettype address2
+               zipcode country city state phone phonepro mobile fax email emailpro branchcode
+               B_streetnumber B_streettype B_address B_address2
+               B_city B_state B_zipcode B_country B_email B_phone)) {
+                    $newdata{$_} = $guarantordata->{$_};
+             }
+           }
+       }
+    }
+}
+
+my @delete_guarantor = $input->multi_param('delete_guarantor');
+foreach my $id ( @delete_guarantor ) {
+    my $r = Koha::Patron::Relationships->find( $id );
+    $r->delete() if $r;
+}
+
 # Test uniqueness of surname, firstname and dateofbirth
 if ( ( $op eq 'insert' ) and !$nodouble ) {
     my $conditions;
@@ -784,11 +811,13 @@ $template->param(
   check_member    => $check_member,#to know if the borrower already exist(=>1) or not (=>0) 
   "op$op"   => 1);
 
+$guarantorid = $borrower_data->{'guarantorid'} || $guarantorid;
 $template->param(
   patron => $patron ? $patron : \%newdata, # Used by address include templates now
   nodouble  => $nodouble,
   borrowernumber  => $borrowernumber, #register number
   relshiploop => \@relshipdata,
+  guarantorid => $guarantorid,
   btitle=> $default_borrowertitle,
   flagloop  => \@flagdata,
   category_type =>$category_type,
-- 
2.11.0