Skip to content

Commit

Permalink
Fix dunning mails
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanuelBertrand committed Sep 25, 2023
1 parent 49da357 commit 9faff7b
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 66 deletions.
20 changes: 19 additions & 1 deletion Controller/Adminhtml/Dunning/SendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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());
}

Expand All @@ -39,6 +54,9 @@ public function execute()
return $redirect;
}

/**
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ibertrand_BankSync::sub_menu_dunnings');
Expand Down
9 changes: 7 additions & 2 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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');
}
}
5 changes: 5 additions & 0 deletions Helper/Dunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,9 @@ public function getOpenInvoices(int $storeId): InvoiceCollection
return $collection;
}

public function getInvoiceDueDays(): int
{
return $this->config->getInvoiceDueDays();
}

}
39 changes: 23 additions & 16 deletions Model/Dunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
Expand Down
27 changes: 8 additions & 19 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,20 @@
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="auto_send_mail" translate="label,comment" type="select" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="auto_send_mail" translate="label,comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Automatically send dunnings via mail</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="email_identity" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="email_identity" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Email Sender</label>
<source_model>Magento\Config\Model\Config\Source\Email\Identity</source_model>
</field>
<group id="types" translate="label,comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="due_days" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Payment due date</label>
<validate>validate-greater-than-zero validate-number required-entry</validate>
<comment>In days after invoice creation</comment>
</field>
<group id="types" translate="label,comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Types</label>
<group id="reminder_1" translate="label,comment" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Reminder 1</label>
Expand All @@ -176,10 +181,6 @@
<label>Mail Template</label>
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
</field>
<field id="attach_pdf" translate="label,comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Attach PDF to mail</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="reminder_2" translate="label,comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Reminder 2</label>
Expand All @@ -200,10 +201,6 @@
<label>Mail Template</label>
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
</field>
<field id="attach_pdf" translate="label,comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Attach PDF to mail</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="dunning_1" translate="label,comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Dunning 1</label>
Expand All @@ -224,10 +221,6 @@
<label>Mail Template</label>
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
</field>
<field id="attach_pdf" translate="label,comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Attach PDF to mail</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="dunning_2" translate="label,comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Dunning 2</label>
Expand All @@ -248,10 +241,6 @@
<label>Mail Template</label>
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
</field>
<field id="attach_pdf" translate="label,comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Attach PDF to mail</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</group>
</group>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<enabled>0</enabled>
<auto_send_mail>0</auto_send_mail>
<email_identity>general</email_identity>
<due_days>14</due_days>
<types>
<reminder_1>
<delay>21</delay>
Expand Down
14 changes: 14 additions & 0 deletions view/frontend/email/dunning_1_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--@subject {{trans "Payment reminder"}} @-->
<!--@vars {
"var invoice_id": "Invoice Identifier",
"var due_date": "Invoice due date",
"var store_name": "Store name",
"var customer_name": "Customer Name",
} @-->
<p>Dear {{var customer_name}},</p>
<p>We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.<br>
We would appreciate if you could check this matter and make the payment as early as possible.<br>
If you have already sent the payment, please disregard this reminder.</p>
<p>Should you need more information on this, please feel free to get in touch with us.</p>
<p>Thank you for your prompt attention to this matter.</p>
<p>Best Regards,<br>{[var store_name}}</p>
14 changes: 14 additions & 0 deletions view/frontend/email/dunning_2_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--@subject {{trans "Payment reminder"}} @-->
<!--@vars {
"var invoice_id": "Invoice Identifier",
"var due_date": "Invoice due date",
"var store_name": "Store name",
"var customer_name": "Customer Name",
} @-->
<p>Dear {{var customer_name}},</p>
<p>We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.<br>
We would appreciate if you could check this matter and make the payment as early as possible.<br>
If you have already sent the payment, please disregard this reminder.</p>
<p>Should you need more information on this, please feel free to get in touch with us.</p>
<p>Thank you for your prompt attention to this matter.</p>
<p>Best Regards,<br>{[var store_name}}</p>
14 changes: 14 additions & 0 deletions view/frontend/email/reminder_1_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--@subject {{trans "Payment reminder"}} @-->
<!--@vars {
"var invoice_id": "Invoice Identifier",
"var due_date": "Invoice due date",
"var store_name": "Store name",
"var customer_name": "Customer Name"
} @-->
<p>Dear {{var customer_name}},</p>
<p>We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.<br>
We would appreciate if you could check this matter and make the payment as early as possible.<br>
If you have already sent the payment, please disregard this reminder.</p>
<p>Should you need more information on this, please feel free to get in touch with us.</p>
<p>Thank you for your prompt attention to this matter.</p>
<p>Best Regards,<br>{{var store_name}}</p>
14 changes: 14 additions & 0 deletions view/frontend/email/reminder_2_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--@subject {{trans "Payment reminder"}} @-->
<!--@vars {
"var invoice_id": "Invoice Identifier",
"var due_date": "Invoice due date",
"var store_name": "Store name",
"var customer_name": "Customer Name",
} @-->
<p>Dear {{var customer_name}},</p>
<p>We noticed that we have yet to receive payment for Invoice #{{var invoice_id}} which was due on {{var due_date}}.<br>
We would appreciate if you could check this matter and make the payment as early as possible.<br>
If you have already sent the payment, please disregard this reminder.</p>
<p>Should you need more information on this, please feel free to get in touch with us.</p>
<p>Thank you for your prompt attention to this matter.</p>
<p>Best Regards,<br>{[var store_name}}</p>
7 changes: 0 additions & 7 deletions view/view/frontend/email/dunning_1_template.html

This file was deleted.

7 changes: 0 additions & 7 deletions view/view/frontend/email/dunning_2_template.html

This file was deleted.

7 changes: 0 additions & 7 deletions view/view/frontend/email/reminder_1_template.html

This file was deleted.

7 changes: 0 additions & 7 deletions view/view/frontend/email/reminder_2_template.html

This file was deleted.

0 comments on commit 9faff7b

Please sign in to comment.