From 9faff7b54047aed81ba9cd47fd85e34c1fa12700 Mon Sep 17 00:00:00 2001 From: Imanuel Bertrand Date: Mon, 25 Sep 2023 14:28:09 +0200 Subject: [PATCH] Fix dunning mails --- Controller/Adminhtml/Dunning/SendMail.php | 20 +++++++++- Helper/Config.php | 9 ++++- Helper/Dunning.php | 5 +++ Model/Dunning.php | 39 +++++++++++-------- etc/adminhtml/system.xml | 27 ++++--------- etc/config.xml | 1 + view/frontend/email/dunning_1_template.html | 14 +++++++ view/frontend/email/dunning_2_template.html | 14 +++++++ view/frontend/email/reminder_1_template.html | 14 +++++++ view/frontend/email/reminder_2_template.html | 14 +++++++ .../frontend/email/dunning_1_template.html | 7 ---- .../frontend/email/dunning_2_template.html | 7 ---- .../frontend/email/reminder_1_template.html | 7 ---- .../frontend/email/reminder_2_template.html | 7 ---- 14 files changed, 119 insertions(+), 66 deletions(-) create mode 100644 view/frontend/email/dunning_1_template.html create mode 100644 view/frontend/email/dunning_2_template.html create mode 100644 view/frontend/email/reminder_1_template.html create mode 100644 view/frontend/email/reminder_2_template.html delete mode 100644 view/view/frontend/email/dunning_1_template.html delete mode 100644 view/view/frontend/email/dunning_2_template.html delete mode 100644 view/view/frontend/email/reminder_1_template.html delete mode 100644 view/view/frontend/email/reminder_2_template.html diff --git a/Controller/Adminhtml/Dunning/SendMail.php b/Controller/Adminhtml/Dunning/SendMail.php index 764141f..5bc0c7a 100644 --- a/Controller/Adminhtml/Dunning/SendMail.php +++ b/Controller/Adminhtml/Dunning/SendMail.php @@ -3,22 +3,36 @@ namespace Ibertrand\BankSync\Controller\Adminhtml\Dunning; use Exception; +use Ibertrand\BankSync\Logger\Logger; use Ibertrand\BankSync\Model\DunningRepository; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; +use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Exception\NoSuchEntityException; class SendMail extends Action { private DunningRepository $dunningRepository; + private Logger $logger; - public function __construct(Context $context, DunningRepository $dunningRepository) + /** + * @param Context $context + * @param DunningRepository $dunningRepository + * @param Logger $logger + */ + public function __construct(Context $context, DunningRepository $dunningRepository, Logger $logger) { parent::__construct($context); $this->dunningRepository = $dunningRepository; + $this->logger = $logger; } + /** + * @return Redirect + * @throws NoSuchEntityException + */ public function execute() { $dunningId = $this->getRequest()->getParam('id'); @@ -30,6 +44,7 @@ public function execute() $this->messageManager->addErrorMessage(__('There was an error sending the mail.')); } } catch (Exception $e) { + $this->logger->error($e->getMessage() . "\n" . $e->getTraceAsString()); $this->messageManager->addErrorMessage(_('There was an error sending the mail: ') . $e->getMessage()); } @@ -39,6 +54,9 @@ public function execute() return $redirect; } + /** + * @return bool + */ protected function _isAllowed() { return $this->_authorization->isAllowed('Ibertrand_BankSync::sub_menu_dunnings'); diff --git a/Helper/Config.php b/Helper/Config.php index 133ab2a..116c392 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -148,7 +148,7 @@ public function isAutoSendDunningsMailEnabled(int $storeId): bool */ public function getDunningEmailTemplate(string $type, int $storeId): mixed { - return $this->scopeConfig->getValue("banksync/dunnings/types/$type/email_template", 'store', $storeId); + return $this->scopeConfig->getValue("banksync/dunnings/types/$type/mail_template", 'store', $storeId); } /** @@ -197,6 +197,11 @@ public function getDunningTypeLabel(string $type, int $storeId): mixed */ public function getDunningSenderIdentity(int $storeId): mixed { - return $this->scopeConfig->getValue("banksync/dunnings/sender_identity", 'store', $storeId); + return $this->scopeConfig->getValue("banksync/dunnings/email_identity", 'store', $storeId); + } + + public function getInvoiceDueDays():int + { + return (int)$this->scopeConfig->getValue('banksync/dunnings/due_days'); } } diff --git a/Helper/Dunning.php b/Helper/Dunning.php index 4881c9f..5fdfbe7 100644 --- a/Helper/Dunning.php +++ b/Helper/Dunning.php @@ -295,4 +295,9 @@ public function getOpenInvoices(int $storeId): InvoiceCollection return $collection; } + public function getInvoiceDueDays(): int + { + return $this->config->getInvoiceDueDays(); + } + } diff --git a/Model/Dunning.php b/Model/Dunning.php index 313fd7c..4d1c071 100644 --- a/Model/Dunning.php +++ b/Model/Dunning.php @@ -3,6 +3,7 @@ namespace Ibertrand\BankSync\Model; use Exception; +use Ibertrand\BankSync\Helper\Dunning as DunningHelper; use Ibertrand\BankSync\Logger\Logger; use Ibertrand\BankSync\Model\ResourceModel\Dunning as ResourceModel; use Magento\Framework\Data\Collection\AbstractDb; @@ -41,21 +42,21 @@ class Dunning extends AbstractModel * @var string */ protected $_eventPrefix = 'banksync_dunning_model'; - protected \Ibertrand\BankSync\Helper\Dunning $dunningHelper; + protected DunningHelper $dunningHelper; protected InvoiceRepository $invoiceRepository; protected TransportBuilder $transportBuilder; - private Logger $logger; + protected Logger $logger; public function __construct( - Context $context, - Registry $registry, - \Ibertrand\BankSync\Helper\Dunning $dunningHelper, - InvoiceRepository $invoiceRepository, - TransportBuilder $transportBuilder, - Logger $logger, - AbstractResource $resource = null, - AbstractDb $resourceCollection = null, - array $data = [], + Context $context, + Registry $registry, + DunningHelper $dunningHelper, + InvoiceRepository $invoiceRepository, + TransportBuilder $transportBuilder, + Logger $logger, + AbstractResource $resource = null, + AbstractDb $resourceCollection = null, + array $data = [], ) { parent::__construct($context, $registry, $resource, $resourceCollection, $data); $this->dunningHelper = $dunningHelper; @@ -137,16 +138,22 @@ public function sendMail(): bool $invoice = $this->getInvoice(); $order = $invoice->getOrder(); $storeId = $invoice->getStoreId(); + $this->logger->info("Store ID: $storeId"); $templateCode = $this->getEmailTemplate(); + $dueDays = $this->dunningHelper->getInvoiceDueDays(); + $invoiceDueDate = strtotime($invoice->getCreatedAt()) + $dueDays * 86400; + + $this->logger->info('Sending dunning mail to ' . $order->getCustomerEmail()); $emailTemplateVariables = [ - 'invoice' => $invoice, - 'order' => $invoice->getOrder(), - 'dunning' => $this, + 'invoice_id' => $invoice->getIncrementId(), + 'due_date' => date('d.m.Y', $invoiceDueDate), + 'store_name' => $invoice->getStore()->getFrontendName(), + 'customer_name' => $order->getCustomerName(), ]; $transport = $this->transportBuilder->setTemplateIdentifier($templateCode) - ->setTemplateOptions(['area' => 'frontend', 'store' => $invoice->getId()]) + ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId]) ->addTo($order->getCustomerEmail(), $order->getCustomerName()) ->setFromByScope($this->dunningHelper->getSenderIdentity($storeId), $storeId) ->setTemplateVars($emailTemplateVariables) @@ -157,7 +164,7 @@ public function sendMail(): bool $this->setSentAt(date('Y-m-d H:i:s')); return true; } catch (Exception $e) { - $this->logger->error('Failed to send dunning mail: ' . $e->getMessage()); + $this->logger->error('Failed to send dunning mail: ' . $e->getMessage() . "\n" . $e->getTraceAsString()); return false; } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 1b9987b..dc6a7f4 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -147,15 +147,20 @@ Magento\Config\Model\Config\Source\Yesno - + Magento\Config\Model\Config\Source\Yesno - + Magento\Config\Model\Config\Source\Email\Identity - + + + validate-greater-than-zero validate-number required-entry + In days after invoice creation + + @@ -176,10 +181,6 @@ Magento\Config\Model\Config\Source\Email\Template - - - Magento\Config\Model\Config\Source\Yesno - @@ -200,10 +201,6 @@ Magento\Config\Model\Config\Source\Email\Template - - - Magento\Config\Model\Config\Source\Yesno - @@ -224,10 +221,6 @@ Magento\Config\Model\Config\Source\Email\Template - - - Magento\Config\Model\Config\Source\Yesno - @@ -248,10 +241,6 @@ Magento\Config\Model\Config\Source\Email\Template - - - Magento\Config\Model\Config\Source\Yesno - diff --git a/etc/config.xml b/etc/config.xml index 2a82ed2..149ff3b 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -49,6 +49,7 @@ 0 0 general + 14 21 diff --git a/view/frontend/email/dunning_1_template.html b/view/frontend/email/dunning_1_template.html new file mode 100644 index 0000000..5a9dd47 --- /dev/null +++ b/view/frontend/email/dunning_1_template.html @@ -0,0 +1,14 @@ + + +

Dear {{var customer_name}},

+

We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.
+ We would appreciate if you could check this matter and make the payment as early as possible.
+ If you have already sent the payment, please disregard this reminder.

+

Should you need more information on this, please feel free to get in touch with us.

+

Thank you for your prompt attention to this matter.

+

Best Regards,
{[var store_name}}

\ No newline at end of file diff --git a/view/frontend/email/dunning_2_template.html b/view/frontend/email/dunning_2_template.html new file mode 100644 index 0000000..5a9dd47 --- /dev/null +++ b/view/frontend/email/dunning_2_template.html @@ -0,0 +1,14 @@ + + +

Dear {{var customer_name}},

+

We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.
+ We would appreciate if you could check this matter and make the payment as early as possible.
+ If you have already sent the payment, please disregard this reminder.

+

Should you need more information on this, please feel free to get in touch with us.

+

Thank you for your prompt attention to this matter.

+

Best Regards,
{[var store_name}}

\ No newline at end of file diff --git a/view/frontend/email/reminder_1_template.html b/view/frontend/email/reminder_1_template.html new file mode 100644 index 0000000..30ac45a --- /dev/null +++ b/view/frontend/email/reminder_1_template.html @@ -0,0 +1,14 @@ + + +

Dear {{var customer_name}},

+

We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.
+ We would appreciate if you could check this matter and make the payment as early as possible.
+ If you have already sent the payment, please disregard this reminder.

+

Should you need more information on this, please feel free to get in touch with us.

+

Thank you for your prompt attention to this matter.

+

Best Regards,
{{var store_name}}

\ No newline at end of file diff --git a/view/frontend/email/reminder_2_template.html b/view/frontend/email/reminder_2_template.html new file mode 100644 index 0000000..5a9dd47 --- /dev/null +++ b/view/frontend/email/reminder_2_template.html @@ -0,0 +1,14 @@ + + +

Dear {{var customer_name}},

+

We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.
+ We would appreciate if you could check this matter and make the payment as early as possible.
+ If you have already sent the payment, please disregard this reminder.

+

Should you need more information on this, please feel free to get in touch with us.

+

Thank you for your prompt attention to this matter.

+

Best Regards,
{[var store_name}}

\ No newline at end of file diff --git a/view/view/frontend/email/dunning_1_template.html b/view/view/frontend/email/dunning_1_template.html deleted file mode 100644 index 5dcedbd..0000000 --- a/view/view/frontend/email/dunning_1_template.html +++ /dev/null @@ -1,7 +0,0 @@ -

Dear Customer,

-

We noticed that we have yet to receive payment for Invoice #{invoice_id} which was due on {due_date}. - We would appreciate if you could check this matter and make the payment as early as possible.

-

If you have already sent the payment, please disregard this reminder.

-

Should you need more information on this, please feel free to get in touch with us.

-

Thank you for your prompt attention to this matter.

-

Best Regards,
Your Company

\ No newline at end of file diff --git a/view/view/frontend/email/dunning_2_template.html b/view/view/frontend/email/dunning_2_template.html deleted file mode 100644 index 5dcedbd..0000000 --- a/view/view/frontend/email/dunning_2_template.html +++ /dev/null @@ -1,7 +0,0 @@ -

Dear Customer,

-

We noticed that we have yet to receive payment for Invoice #{invoice_id} which was due on {due_date}. - We would appreciate if you could check this matter and make the payment as early as possible.

-

If you have already sent the payment, please disregard this reminder.

-

Should you need more information on this, please feel free to get in touch with us.

-

Thank you for your prompt attention to this matter.

-

Best Regards,
Your Company

\ No newline at end of file diff --git a/view/view/frontend/email/reminder_1_template.html b/view/view/frontend/email/reminder_1_template.html deleted file mode 100644 index 5dcedbd..0000000 --- a/view/view/frontend/email/reminder_1_template.html +++ /dev/null @@ -1,7 +0,0 @@ -

Dear Customer,

-

We noticed that we have yet to receive payment for Invoice #{invoice_id} which was due on {due_date}. - We would appreciate if you could check this matter and make the payment as early as possible.

-

If you have already sent the payment, please disregard this reminder.

-

Should you need more information on this, please feel free to get in touch with us.

-

Thank you for your prompt attention to this matter.

-

Best Regards,
Your Company

\ No newline at end of file diff --git a/view/view/frontend/email/reminder_2_template.html b/view/view/frontend/email/reminder_2_template.html deleted file mode 100644 index 5dcedbd..0000000 --- a/view/view/frontend/email/reminder_2_template.html +++ /dev/null @@ -1,7 +0,0 @@ -

Dear Customer,

-

We noticed that we have yet to receive payment for Invoice #{invoice_id} which was due on {due_date}. - We would appreciate if you could check this matter and make the payment as early as possible.

-

If you have already sent the payment, please disregard this reminder.

-

Should you need more information on this, please feel free to get in touch with us.

-

Thank you for your prompt attention to this matter.

-

Best Regards,
Your Company

\ No newline at end of file