From 7a4f0a01213cf885d46386969a97b684dde21373 Mon Sep 17 00:00:00 2001
From: jeremy breuillard <jeremy.breuillard@biblibre.com>
Date: Tue, 22 Feb 2022 14:03:08 +0100
Subject: [PATCH] Bug 27123: Add messages to batch patrons modification

With this patch you can write or delete circulation messages on the
batch patrons modification form

Test plan:
1)Go to Home > Tools > Batch patron modification
2)Write one or more card numbers to access the modification form
3)Notice the input you can interact with
4)Apply patch and repeat 1) & 2)
5)Notice the new input 'Message:' which appeared down the form
6)Write a message and choose a type (opac or intranet)
7)Save & notice the new 'Message' column filled up with what you wrote
  before
8)To delete: back on the modification form - check the checkbox beside
  the input - save again

Signed-off-by: Valerie Burnett <valerie@bywatersolutions.com>
---
 .../prog/en/modules/tools/modborrowers.tt     | 22 ++++++++++
 tools/modborrowers.pl                         | 41 ++++++++++++++++++-
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/modborrowers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/modborrowers.tt
index 46ef2f417d8..68b6ef6adee 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/modborrowers.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/modborrowers.tt
@@ -60,6 +60,7 @@
             <main>
                 [% INCLUDE 'messages.inc' %]
 
+
                 [% IF ( op == 'show_form' ) %]
                     <h1>Batch patron modification</h1>
                     <form id="patron_batchmod_form" method="post" enctype="multipart/form-data" action="/cgi-bin/koha/tools/modborrowers.pl">
@@ -262,6 +263,7 @@
                                                 [% END %]
                                                 <th>Circulation note</th>
                                                 <th>OPAC note</th>
+                                                <th>Message</th>
                                                 <th>Restriction expiration</th>
                                                 <th>Restriction comment</th>
                                                 [% FOREACH attrh IN attributes_header %]
@@ -308,6 +310,18 @@
                                                     [% END %]
                                                     <td>[% borrower.borrowernotes | $raw | html_line_break %]</td>
                                                     <td>[% borrower.opacnote | html %]</td>
+                                                    <td>
+                                                        [% FOREACH patron_message IN borrower.patron_messages %]
+                                                            [% patron_message.message | html %] <br>
+                                                            <strong>
+                                                            [% IF (patron_message.message_type == 'B') %]
+                                                                ( OPAC )
+                                                            [% ELSE %]
+                                                                ( Staff )
+                                                            [% END %]
+                                                            </strong><br><br>
+                                                        [% END %]
+                                                    </td>
                                                     <td data-order="[% borrower.debarred | html %]">[% borrower.debarred | $KohaDates %]</td>
                                                     <td>[% borrower.debarredcomment | html %]</td>
                                                     [% FOREACH pa IN borrower.patron_attributes %]
@@ -360,6 +374,7 @@
                                                 [% CASE 'dateexpiry' %]<span>Expiry date:</span>
                                                 [% CASE 'borrowernotes' %]<span>Circulation note:</span>
                                                 [% CASE 'opacnote' %]<span>OPAC note:</span>
+                                                [% CASE "message" %]<span>Message:</span>
                                                 [% CASE 'debarred' %]<span>Restriction expiration:</span>
                                                 [% CASE 'debarredcomment' %]<span>Restriction comment:</span>
                                                 [% CASE 'password_expiration_date' %]<span>Password expiration date:</span>
@@ -368,6 +383,13 @@
                                             [% IF ( field.type == 'text' ) %]
                                                 <input type="text" id="[%field.name | html %]" name="[% field.name | html %]" value="" />
                                             [% END %]
+                                            [% IF ( field.type == 'message_type' ) %]
+                                                <input type="text" name="[% field.name | html %]" value="" />
+                                                <select name="add_message_type" id="message_type">
+                                                    <option value="L">Staff - Internal note</option>
+                                                    <option value="B">OPAC</option>
+                                                </select>
+                                            [% END %]
                                             [% IF ( field.type == 'select' ) %]
                                                 [% IF field.option.size %]
                                                     <select id="[%field.name | html %]" name="[% field.name | html %]" >
diff --git a/tools/modborrowers.pl b/tools/modborrowers.pl
index 6492d2f07b2..babd3eb6d11 100755
--- a/tools/modborrowers.pl
+++ b/tools/modborrowers.pl
@@ -31,6 +31,7 @@ use C4::Auth qw( get_template_and_user );
 use C4::Koha qw( GetAuthorisedValues );
 use C4::Members;
 use C4::Output qw( output_html_with_http_headers );
+use C4::Context;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::List::Patron qw( GetPatronLists );
 use Koha::Libraries;
@@ -38,6 +39,7 @@ use Koha::Patron::Categories;
 use Koha::Patron::Debarments qw( AddDebarment DelDebarment );
 use Koha::Patrons;
 use List::MoreUtils qw(uniq);
+use Koha::Patron::Messages;
 
 my $input = CGI->new;
 my $op = $input->param('op') || 'show_form';
@@ -111,6 +113,12 @@ if ( $op eq 'cud-show' || $op eq 'show' ) {
             if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
                 my $borrower = $patron->unblessed;
                 my $attributes = $patron->extended_attributes;
+                my $patron_messages = Koha::Patron::Messages->search(
+                    {
+                        'me.borrowernumber' => $patron->borrowernumber,
+                    }
+                );
+                $borrower->{patron_messages} = $patron_messages->as_list;
                 $borrower->{patron_attributes} = $attributes->as_list;
                 $borrower->{patron_attributes_count} = $attributes->count;
                 $max_nb_attr = $borrower->{patron_attributes_count} if $borrower->{patron_attributes_count} > $max_nb_attr;
@@ -322,6 +330,12 @@ if ( $op eq 'cud-show' || $op eq 'show' ) {
             mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
         }
         ,
+        {
+            name => "message",
+            type => "message_type",
+            mandatory => ( grep /message/, @mandatoryFields ) ? 1 : 0,
+        }
+        ,
         {
             name => "debarred",
             type => "date",
@@ -392,7 +406,11 @@ if ( $op eq 'cud-do' ) {
                    }
                 };
             }
-
+            # If 'message' or 'add_message_type' is defined then delete both at the same time
+            if ( grep { $_ eq 'message' } @disabled) {
+                my $patron = Koha::Patrons->find( $borrowernumber );
+                $patron->messages()->delete();
+            }
             $infos->{borrowernumber} = $borrowernumber;
             eval { $patron->set($infos)->store; };
             if ( $@ ) { # FIXME We could provide better error handling here
@@ -433,7 +451,22 @@ if ( $op eq 'cud-do' ) {
                 push @errors, { error => $@ } if $@;
             }
         }
+
+        # Handle patron messages
+        my $message          = $input->param('message');
+        my $branchcode       = C4::Context::mybranch;
+        my $message_type     = $input->param('add_message_type');
+
+        Koha::Patron::Message->new(
+            {
+                borrowernumber => $borrowernumber,
+                branchcode     => $branchcode,
+                message_type   => $message_type,
+                message        => $message,
+            }
+        )->store;
     }
+
     $op = "show_results"; # We have process modifications, the user want to view its
 
     # Construct the results list
@@ -444,6 +477,12 @@ if ( $op eq 'cud-do' ) {
         if ( $patron ) {
             my $category_description = $patron->category->description;
             my $borrower = $patron->unblessed;
+            my $patron_messages = Koha::Patron::Messages->search(
+                    {
+                        'me.borrowernumber' => $patron->borrowernumber,
+                    }
+                );
+            $borrower->{patron_messages} = $patron_messages->as_list;
             $borrower->{category_description} = $category_description;
             my $attributes = $patron->extended_attributes;
             $borrower->{patron_attributes} = $attributes->as_list;
-- 
2.39.5