From a945beebdefc208d52394b259b187d7459ebe337 Mon Sep 17 00:00:00 2001
From: Sam Lau <samalau@gmail.com>
Date: Wed, 3 Jul 2024 20:09:37 +0000
Subject: [PATCH] Bug 32581: Automatically update patron expiration date when
 changing their category

To test:
1) In Administration-> Patron categories, set the enrollment period for the 'School' category to 12 months.
2) Find a patron that is from another category, edit their info and change their category from patron to school.
3) Note that the expiry date automatically changes to 12 months from today.
4) Save your changes and ensure on the patron's detail page that their expiration date was successfully changed.

Signed-off-by: Roman Dolny <roman.dolny@jezuici.pl>
Signed-off-by: Jan Kissig <jkissig@th-wildau.de>
---
 .../prog/en/modules/members/memberentrygen.tt         |  4 ++--
 koha-tmpl/intranet-tmpl/prog/js/members.js            | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 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 6877ef453f..eb2f49b32b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
@@ -1007,9 +1007,9 @@ legend.collapsed i.fa.fa-caret-down::before {
                                                     [% END %]
                                                     [% FOREACH category IN patron_categories.$category_type %]
                                                         [% IF category.categorycode == patron_category.categorycode %]
-                                                            <option value="[% category.categorycode | html %]" selected="selected" data-pwd-length="[% category.effective_min_password_length | html %]" data-pwd-strong="[% category.effective_require_strong_password | html %]" data-typename="[% category_type | html %]">[% category.description | html %]</option>
+                                                            <option value="[% category.categorycode | html %]" selected="selected" data-pwd-length="[% category.effective_min_password_length | html %]" data-pwd-strong="[% category.effective_require_strong_password | html %]" data-typename="[% category_type | html %]" data-expiry-date="[% category.get_expiry_date | html %]">[% category.description | html %]</option>
                                                         [% ELSE %]
-                                                            <option value="[% category.categorycode | html %]" data-pwd-length="[% category.effective_min_password_length | html %]" data-pwd-strong="[% category.effective_require_strong_password | html %]" data-typename="[% category_type | html %]">[% category.description | html %]</option>
+                                                            <option value="[% category.categorycode | html %]" data-pwd-length="[% category.effective_min_password_length | html %]" data-pwd-strong="[% category.effective_require_strong_password | html %]" data-typename="[% category_type | html %]" data-expiry-date="[% category.get_expiry_date | html %]">[% category.description | html %]</option>
                                                         [% END %]
                                                     [% END %]
                                                         </optgroup>
diff --git a/koha-tmpl/intranet-tmpl/prog/js/members.js b/koha-tmpl/intranet-tmpl/prog/js/members.js
index 2f58cb888c..9afce16e7b 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/members.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/members.js
@@ -81,6 +81,17 @@ function update_category_code(category_code) {
     var min_length = $('select'+category_selector+' option:selected').data('pwdLength');
     var hint_string = __("Minimum password length: %s").format(min_length);
     hint.html(hint_string);
+
+    // Change patron's expiration date
+    $('#categorycode_entry').change(function() {
+        var fp = $("#to").flatpickr();
+        var expiryDate = $('select'+category_selector+' option:selected').data('expiryDate');
+        // Check if expiryDate is available and format it to YYYY-MM-DD
+        if (expiryDate) {
+            var formattedDate = expiryDate.split('T')[0];
+            fp.setDate(formattedDate);
+        }
+    });
 }
 
 function select_user(borrowernumber, borrower, relationship) {
-- 
2.34.1