From a6fb08d4ade2d69293ad1e21079ab6d5033a44b2 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Tue, 7 May 2024 17:17:23 +0100
Subject: [PATCH] Bug 36758: Add trigger for assignee notification trigger

This patch adds a new notice trigger to allow notifying assigned staff
that they have been assigned a new catalog concern to action.

Test plan
1) Enable catalog concerns (either via staff or opac interfaces)
2) Report a new concern (either via staff or opac interfaces)
3) As a staff user, assign the concern to another staff user
4) Check that the new 'TICKET_ASSIGNED' notice has be queued for that
   staff user.
5) Re-assign the catalog concern to yourself
6) Note that you do not see a 'TICKET_ASSIGNED@ notice queued as you
   self-assigned.
---
 Koha/Ticket.pm | 40 ++++++++++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/Koha/Ticket.pm b/Koha/Ticket.pm
index 9beed143499..2c4586a084b 100644
--- a/Koha/Ticket.pm
+++ b/Koha/Ticket.pm
@@ -123,10 +123,13 @@ Overloaded I<store> method to trigger notices as required
 sub store {
     my ($self) = @_;
 
-    my $is_new = !$self->in_storage;
-    $self = $self->SUPER::store;
+    my $assignee;
+    if ( !$self->in_storage ) {
 
-    if ($is_new) {
+        # Store
+        $self->SUPER::store;
+        $self->discard_changes;
+        $assignee = $self->assignee;
 
         # Send patron acknowledgement
         my $acknowledgement_letter = C4::Letters::GetPreparedLetter(
@@ -164,14 +167,39 @@ sub store {
                     {
                         letter                 => $notify_letter,
                         message_transport_type => 'email',
-                        to_address             =>
-                          C4::Context->preference('CatalogerEmails'),
-                        reply_address => $self->reporter->notice_email_address,
+                        to_address             => C4::Context->preference('CatalogerEmails'),
+                        reply_address          => $self->reporter->notice_email_address,
                     }
                 );
                 C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
             }
         }
+    } else {
+        my %updated_columns = $self->_result->get_dirty_columns;
+        return $self->SUPER::store unless %updated_columns;
+
+        $assignee = ( exists $updated_columns{assignee_id} ) ? $self->assignee : undef;
+    }
+
+    # Notify assignee
+    if ( $assignee && ( $assignee->borrowernumber != C4::Context->userenv->{number} ) ) {
+        my $assigned_letter = C4::Letters::GetPreparedLetter(
+            module      => 'catalogue',
+            letter_code => 'TICKET_ASSIGNED',
+            branchcode  => $assignee->branchcode,
+            tables      => { tickets => $self->id }
+        );
+
+        if ($assigned_letter) {
+            my $message_id = C4::Letters::EnqueueLetter(
+                {
+                    letter                 => $assigned_letter,
+                    borrowernumber         => $assignee->borrowernumber,
+                    message_transport_type => 'email',
+                }
+            );
+            C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
+        }
     }
 
     return $self;
-- 
2.45.0