From e9d2c14dbc19a286765d41f053cb79e8bb90291e Mon Sep 17 00:00:00 2001
From: Isobel Graham <isobel.graham09@gmail.com>
Date: Thu, 30 Jun 2022 16:32:26 +0100
Subject: [PATCH] Bug 30619: Add email receipt to POS

This patch adds the ability to use email receipts with the point of sale
module.

To test:
1) Configure your Koha to enable the point of sale system.
2) Add an email template to the 'Point of sale > RECEIPT' notice.
3) Add some charges on point of sale and pay for them.
3a) You should now see a new 'Email receipt' button next to the 'Print
receipt' option.
3b) Clicking the button will display a modal to enter the anonymous
users email address
3c) Enter an email address and confirm to send the email
---
 .../intranet-tmpl/prog/en/modules/pos/pay.tt  | 41 +++++++++++--
 pos/pay.pl                                    | 58 ++++++++++++++++---
 2 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
index 905ed4c0f0..8ea2cd7801 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
@@ -51,7 +51,7 @@
 
         [% IF payment_id && !Koha.Preference('FinePaymentAutoPopup') %]
         <div class="dialog alert audio-alert-action">
-            Payment received: <a target="_blank" href="/cgi-bin/koha/pos/printreceipt.pl?action=print&accountlines_id=[% payment_id | uri %]&collected=[% collected | uri %]&change=[% change | uri %]" class="btn btn-default btn-xs"><i class="fa fa-print"></i> Print receipt</a>
+            Payment received: <a target="_blank" href="/cgi-bin/koha/pos/printreceipt.pl?action=print&accountlines_id=[% payment_id | uri %]&collected=[% collected | uri %]&change=[% change | uri %]" class="btn btn-default btn-xs"><i class="fa fa-print"></i> Print receipt</a> <a type="button" data-toggle="modal" data-target="#emailReceiptModal" class="btn btn-default btn-xs"><i class="fa fa-envelope"></i> Email receipt</a>
         </div>
         [% END %]
 
@@ -168,7 +168,40 @@
     </div>
 </div> <!-- /.row -->
 
-<!-- Modal -->
+<!-- Email receipt modal -->
+<div class="modal" id="emailReceiptModal" tabindex="-1" role="dialog" aria-labelledby="emailReceiptLabel">
+    <form id="email_form" action="/cgi-bin/koha/pos/pay.pl" method="get" enctype="multipart/form-data" class="validated">
+        <input type="hidden" name="payment_id" id="payment_id" value="[% payment_id | uri %]">
+        <input type="hidden" name="collected" id="collected" value="[% collected | uri %]">
+        <input type="hidden" name="change" id="change" value="[% change | uri %]">"
+        <div class="modal-dialog" role="document">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+                    <h4 class="modal-title" id="emailReceiptLabel">Email receipt</h4>
+                </div>
+                <div class="modal-body">
+                    <fieldset class="rows">
+                        <ol>
+                            <li>
+                                <label class="required" for="toaddr">Email address: </label>
+                                <input type="email" id="toaddr" name="toaddr" required="required">
+                                <span class="required">Required</span>
+                            </li>
+                        </ol>
+                    </fieldset> <!-- /.rows -->
+                </div> <!-- /.modal-body -->
+                <div class="modal-footer">
+                    <input type="hidden" name="action" value="send">
+                    <button type="submit" class="btn btn-default">Confirm</button>
+                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
+                </div> <!-- /.modal-footer -->
+            </div> <!-- /.modal-content -->
+        </div> <!-- /.modal-dialog -->
+    </form> <!-- /#email_form -->
+</div> <!-- /#emailReceiptModal
+
+<!-- Change modal -->
 <div id="confirm_change_form" class="modal" tabindex="-1" role="dialog" aria-hidden="true">
     <div class="modal-dialog">
         <div class="modal-content">
@@ -189,8 +222,8 @@
 </div>
 
 [% IF payment_id && Koha.Preference('FinePaymentAutoPopup') %]
-<!-- Automatic Print Receipt -->
-      <a id="printReceipt" style="display: none" href="#"></a>
+    <!-- Automatic Print Receipt -->
+    <a id="printReceipt" style="display: none" href="#"></a>
 [% END %]
 
 [% MACRO jsinclude BLOCK %]
diff --git a/pos/pay.pl b/pos/pay.pl
index b401f12495..871ffd6c19 100755
--- a/pos/pay.pl
+++ b/pos/pay.pl
@@ -23,8 +23,9 @@ use CGI;
 use JSON qw( from_json );
 
 use C4::Auth qw( get_session get_template_and_user );
-use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
+use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
+use C4::Output  qw( output_html_with_http_headers );
 
 use Koha::Account::DebitTypes;
 use Koha::AuthorisedValues;
@@ -39,16 +40,17 @@ my $session   = get_session($sessionID);
 
 my ( $template, $loggedinuser, $cookie, $user_flags ) = get_template_and_user(
     {
-        template_name   => 'pos/pay.tt',
-        query           => $input,
-        type            => 'intranet',
-        flagsrequired   => { cash_management => 'takepayment' },
+        template_name => 'pos/pay.tt',
+        query         => $input,
+        type          => 'intranet',
+        flagsrequired => { cash_management => 'takepayment' },
     }
 );
 my $logged_in_user = Koha::Patrons->find($loggedinuser) or die "Not logged in";
 
-my $library_id         = C4::Context->userenv->{'branch'};
-my $registerid         = $input->param('registerid');
+my $library_id = C4::Context->userenv->{'branch'};
+my $registerid = $input->param('registerid');
+my $action     = $input->param('action') || '';
 
 my $invoice_types =
   Koha::Account::DebitTypes->search_with_library_limits(
@@ -82,6 +84,48 @@ if ( $total_paid and $total_paid ne '0.00' ) {
     );
 }
 
+if ( $action eq 'send' ) {
+    my $payment_id = $input->param('payment_id');
+    my $change     = $input->param('change');
+    my $collected  = $input->param('collected');
+    my $toaddr     = $input->param('toaddr');
+
+    # Create our letter from the template
+    my $letter = GetPreparedLetter(
+        module                 => 'pos',
+        letter_code            => 'RECEIPT',
+        branchcode             => C4::Context->userenv->{'branch'},
+        message_transport_type => 'email',
+        tables                 => {
+            credits => $payment_id,
+        },
+        substitute => {
+            collected => $collected,
+            change    => $change
+        }
+    );
+
+    # Add letter to the queue
+    my $message_id = EnqueueLetter(
+        {
+            letter                 => $letter,
+            message_transport_type => 'email',
+            from_address => C4::Context->preference('KohaAdminEmailAddress'),
+            to_address   => $toaddr,
+        }
+    );
+
+    # Send immediately
+    SendQueuedMessages( { message_id => $message_id } );
+
+    # Set variables for template to allow printing still
+    $template->param(
+        payment_id => $payment_id,
+        collected  => $collected,
+        change     => $change
+    );
+}
+
 output_html_with_http_headers( $input, $cookie, $template->output );
 
 1;
-- 
2.37.0