From 35e1884622740e1dfd48f3b21405f492e24e33b4 Mon Sep 17 00:00:00 2001
From: Antony Thorpe <1023740+AntonyThorpe@users.noreply.github.com>
Date: Thu, 16 May 2024 17:56:54 +1200
Subject: [PATCH] Provide an option to disable the emailing of receipts

Update Order.php
- add new private static send_receipt
- update the statusTransition method to allow for the disabling of receipt emails when orders are paid
Update docs & example config
Update OrderProcessor.php for missing config variables
---
 docs/en/02_Customisation/Emails.md            |  7 +++++--
 example_config.yml                            |  1 +
 src/Checkout/OrderProcessor.php               | 10 ++++++++++
 src/Model/Order.php                           |  8 +++++++-
 tests/php/Checkout/OrderEmailNotifierTest.php | 13 +++++++++++++
 5 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/docs/en/02_Customisation/Emails.md b/docs/en/02_Customisation/Emails.md
index c16a9b376..bde01397d 100644
--- a/docs/en/02_Customisation/Emails.md
+++ b/docs/en/02_Customisation/Emails.md
@@ -5,6 +5,9 @@ Shop emails can be customised to suit your project needs.
 There are a few yaml config options that will affect which emails are sent:
 
 ```yaml
+SilverShop\Model\Order:
+  send_receipt: false # to disable the emailing of paid invoices (default is true)
+
 SilverShop\Checkout\OrderProcessor:
   #send order confirmation when order is placed, but unpaid
   send_confirmation: true
@@ -13,6 +16,7 @@ SilverShop\Checkout\OrderProcessor:
 SilverShop\Checkout\OrderEmailNotifier:
   bcc_confirmation_to_admin: true
   bcc_receipt_to_admin: true
+  bcc_status_change_to_admin: true
 
 #Specify the 'from' address to use in email correspondence
 SilverShop\Extension\ShopConfigExtension:
@@ -35,7 +39,7 @@ en:
   ShopEmail:
     ConfirmationSubject: 'My Website Order #{OrderNo} confirmation'
     ReceiptSubject: 'My Website Order #{OrderNo} receipt'
-    CancelSubject: 'My Website Order #{OrderNo} cancelled by member'  
+    CancelSubject: 'My Website Order #{OrderNo} cancelled by member'
 ```
 
 ## Making your own Notifier
@@ -48,4 +52,3 @@ Injector:
   OrderEmailNotifier:
     class: MyCustomOrderEmailNotifier
 ```
-
diff --git a/example_config.yml b/example_config.yml
index 6a1221389..cd6e6518c 100644
--- a/example_config.yml
+++ b/example_config.yml
@@ -22,6 +22,7 @@ SilverShop\Model\Order:
   modifiers:
     - 'SilverShop\Model\Modifiers\Shipping\Simple'
     - 'SilverShop\Model\Modifiers\Tax\FlatTax'
+  # send_receipt: false  # send paid invoice to customer (default is true)
   cancel_before_payment: false
   cancel_before_processing: false
   cancel_before_sending: false
diff --git a/src/Checkout/OrderProcessor.php b/src/Checkout/OrderProcessor.php
index 2b177493f..f7e9d9a17 100644
--- a/src/Checkout/OrderProcessor.php
+++ b/src/Checkout/OrderProcessor.php
@@ -33,6 +33,16 @@ class OrderProcessor
     use Injectable;
     use Configurable;
 
+    /**
+     * @config
+     */
+    private static bool $send_admin_notification = false;
+
+    /**
+     * @config
+     */
+    private static bool $send_confirmation = false;
+
     /**
      * @var Order
      */
diff --git a/src/Model/Order.php b/src/Model/Order.php
index 939beaa7d..523e8c75d 100644
--- a/src/Model/Order.php
+++ b/src/Model/Order.php
@@ -215,6 +215,12 @@ class Order extends DataObject
      */
     private static $cancel_before_payment = true;
 
+    /**
+     * Email customer an invoice upon payment
+     * @config
+     */
+    private static bool $send_receipt = true;
+
     /**
      * Whether or not an order can be cancelled before processing
      *
@@ -845,7 +851,7 @@ protected function statusTransition($fromStatus, $toStatus)
             //all payment is settled
             $this->extend('onPaid');
 
-            if (!$this->ReceiptSent) {
+            if (!$this->ReceiptSent && static::config()->get('send_receipt')) {
                 OrderEmailNotifier::create($this)->sendReceipt();
                 $this->setField('ReceiptSent', DBDatetime::now()->Rfc2822());
             }
diff --git a/tests/php/Checkout/OrderEmailNotifierTest.php b/tests/php/Checkout/OrderEmailNotifierTest.php
index 7138ca5c2..ea39aa7c4 100644
--- a/tests/php/Checkout/OrderEmailNotifierTest.php
+++ b/tests/php/Checkout/OrderEmailNotifierTest.php
@@ -54,6 +54,19 @@ public function testReceipt()
         $this->assertEmailSent('test@example.com', 'shop-admin@example.com');
     }
 
+    public function testReceiptNoEmailSent()
+    {
+        $this->clearEmails();
+        Config::modify()->set(Order::class, 'send_receipt', false);
+        $order = $this->objFromFixture(Order::class, 'unpaid');
+        $order->setField('Status', 'Paid');
+        $order->write();
+        $this->assertNull(
+            $this->findEmail('hi@there.net', 'shop-admin@example.com'),
+            'An email is not sent when the Order class send_receipt is set to false'
+        );
+    }
+
     public function testStatusUpdate()
     {
         $this->notifier->sendStatusChange('test subject');