Skip to content

Commit

Permalink
IVM-6 Add setting for attaching the invoice to user order email
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Sep 24, 2024
1 parent 05ac424 commit 15549c8
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 7 deletions.
8 changes: 8 additions & 0 deletions metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@
'type' => 'str',
'value' => 'ABC-%1$s',
],

// group emails
[
'group' => 'fa_invoice_emails',
'name' => \FreshAdvance\Invoice\Settings\ModuleSettings::SETTING_SEND_INVOICE_ON_USER_ORDER_EMAIL,
'type' => 'bool',
'value' => false
],
],
'events' => [
'onActivate' => '\FreshAdvance\Invoice\Transition\Core\Events::onActivate',
Expand Down
9 changes: 9 additions & 0 deletions src/Settings/ModuleSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ModuleSettings implements ModuleSettingsInterface
public const SETTING_DOCUMENT_IS_FOR_ARCHIVE = 'fa_invoice_IsForArchive';
public const SETTING_INVOICE_NUMBER_FORMAT = 'fa_invoice_InvoiceNumberFormat';
public const SETTING_INVOICE_DATE_FORMAT = 'fa_invoice_InvoiceDateFormat';
public const SETTING_SEND_INVOICE_ON_USER_ORDER_EMAIL = 'fa_invoice_SendInvoiceOnUserOrderEmail';

public function __construct(
private ModuleSettingServiceInterface $moduleSettingService
Expand Down Expand Up @@ -53,4 +54,12 @@ private function getStringSetting(string $key): string
->getString($key, Module::MODULE_ID)
->toString();
}

public function isSendInvoiceOnUserOrderEmailActive(): bool
{
return $this->moduleSettingService->getBoolean(
self::SETTING_SEND_INVOICE_ON_USER_ORDER_EMAIL,
Module::MODULE_ID
);
}
}
2 changes: 2 additions & 0 deletions src/Settings/ModuleSettingsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function isForArchive(): bool;
public function getInvoiceNumberFormat(): string;

public function getInvoiceDateFormat(): string;

public function isSendInvoiceOnUserOrderEmailActive(): bool;
}
17 changes: 12 additions & 5 deletions src/Transition/Core/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use FreshAdvance\Invoice\Document\InvoiceGeneratorInterface;
use FreshAdvance\Invoice\Service\Invoice;
use FreshAdvance\Invoice\Settings\ModuleSettingsInterface;
use FreshAdvance\Invoice\Traits\ServiceContainer;

/**
Expand All @@ -27,13 +28,17 @@ class Email extends Email_parent

public function sendOrderEmailToUser($order, $subject = null)
{
$invoiceDataService = $this->getServiceFromContainer(Invoice::class);
$generator = $this->getServiceFromContainer(InvoiceGeneratorInterface::class);
$moduleSettings = $this->getServiceFromContainer(ModuleSettingsInterface::class);

$invoiceData = $invoiceDataService->getInvoiceDataByOrderId($order->getId());
$generator->generate($invoiceData);
if ($moduleSettings->isSendInvoiceOnUserOrderEmailActive()) {
$invoiceDataService = $this->getServiceFromContainer(Invoice::class);
$generator = $this->getServiceFromContainer(InvoiceGeneratorInterface::class);

$this->attachInvoice = $invoiceData->getInvoicePath();
$invoiceData = $invoiceDataService->getInvoiceDataByOrderId($order->getId());
$generator->generate($invoiceData);

$this->attachInvoice = $invoiceData->getInvoicePath();
}

return $this->faCallParentSendOrderEmailToUser($order, $subject);
}
Expand All @@ -51,11 +56,13 @@ public function send()
return $this->faCallParentSend();
}

/** @codeCoverageIgnore not testable because of parent call */
public function faCallParentSend()
{
return parent::send();
}

/** @codeCoverageIgnore not testable because of parent call */
public function faCallParentSendOrderEmailToUser($order, mixed $subject)
{
return parent::sendOrderEmailToUser($order, $subject);
Expand Down
94 changes: 92 additions & 2 deletions tests/Integration/Transition/Core/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
namespace FreshAdvance\Invoice\Tests\Integration\Transition\Core;

use FreshAdvance\Invoice\DataType\InvoiceDataInterface;
use FreshAdvance\Invoice\Settings\ModuleSettingsInterface;
use FreshAdvance\Invoice\Transition\Core\Email;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;

/** @covers \FreshAdvance\Invoice\Transition\Core\Email */
class EmailTest extends IntegrationTestCase
{
public function testInvoiceGeneratedDuringSendOrderEmailToUserMethod(): void
public function testInvoiceGeneratedDuringSendOrderEmailToUserMethodWithOptionOn(): void
{
$sut = $this->createPartialMock(
Email::class,
Expand All @@ -32,6 +33,12 @@ public function testInvoiceGeneratedDuringSendOrderEmailToUserMethod(): void
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$generatorSpy = $this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => true
])
]
]);

$orderStub = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
Expand All @@ -51,7 +58,40 @@ public function testInvoiceGeneratedDuringSendOrderEmailToUserMethod(): void
$this->assertSame($parentOrderEmailSendResult, $orderEmailResult);
}

public function testInvoiceAttachedIfGeneratedDuringSendOrderEmailMethod(): void
public function testInvoiceNotGeneratedDuringSendOrderEmailToUserMethodWithOptionOff(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSendOrderEmailToUser', 'getServiceFromContainer']
);
$sut->method('faCallParentSendOrderEmailToUser')->willReturn($parentOrderEmailSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataServiceSpy = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$generatorSpy = $this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => false
])
],
]);

$invoiceDataServiceSpy->expects($this->never())->method('getInvoiceDataByOrderId');
$generatorSpy->expects($this->never())->method('generate');

$orderStub = $this->createStub(\OxidEsales\Eshop\Application\Model\Order::class);
$orderEmailResult = $sut->sendOrderEmailToUser($orderStub, "some subject");

$this->assertSame($parentOrderEmailSendResult, $orderEmailResult);
}

public function testInvoiceAttachedIfGeneratedDuringSendOrderEmailMethodWithOptionOn(): void
{
$sut = $this->createPartialMock(
Email::class,
Expand All @@ -67,6 +107,12 @@ public function testInvoiceAttachedIfGeneratedDuringSendOrderEmailMethod(): void
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => true
])
]
]);

$order = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
Expand Down Expand Up @@ -94,4 +140,48 @@ public function testInvoiceAttachedIfGeneratedDuringSendOrderEmailMethod(): void
// check second send will not trigger the attachment again
$sut->send();
}

public function testInvoiceNotAttachedIfGeneratedDuringSendOrderEmailMethodWithOptionOff(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSend', 'faCallParentSendOrderEmailToUser', 'getServiceFromContainer', 'addAttachment']
);
$sut->method('faCallParentSend')->willReturn($parentSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataService = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => false
])
]
]);

$order = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
'getId' => $orderId = uniqid()
]);

$invoiceDataService->method('getInvoiceDataByOrderId')
->with($orderId)
->willReturn(
$this->createConfiguredMock(InvoiceDataInterface::class, [
'getInvoicePath' => $invoicePath = 'example.pdf'
])
);

$sut->sendOrderEmailToUser($order, "some subject");

$sut->expects($this->never())->method('addAttachment');

$sendResult = $sut->send();
$this->assertSame($parentSendResult, $sendResult);
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Settings/ModuleSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public function testGetInvoiceDateFormat(): void
$this->assertSame($value, $sut->getInvoiceDateFormat());
}

/** @dataProvider booleanDataProvider */
public function testIsSendInvoiceOnUserOrderEmailActive(bool $value): void
{
$mssMock = $this->createMock(ModuleSettingServiceInterface::class);
$mssMock->method('getBoolean')->willReturnMap([
[ModuleSettings::SETTING_SEND_INVOICE_ON_USER_ORDER_EMAIL, Module::MODULE_ID, $value]
]);

$sut = new ModuleSettings($mssMock);
$this->assertSame($value, $sut->isSendInvoiceOnUserOrderEmailActive());
}

public function booleanDataProvider(): array
{
return [
Expand Down
4 changes: 4 additions & 0 deletions views/admin_twig/de/fainvoice_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
'SHOP_MODULE_GROUP_fa_invoice_numbering' => 'Rechnungsnummerierung',
'SHOP_MODULE_fa_invoice_InvoiceNumberUpdate' => 'Bestellrechnungsnummer bei Rechnungserstellung aktualisieren',
'SHOP_MODULE_fa_invoice_InvoiceNumberFormat' => 'Rechnungsnummerformat',

# Module settings invoice mails
'SHOP_MODULE_GROUP_fa_invoice_emails' => 'E-Mail-Optionen',
'SHOP_MODULE_fa_invoice_SendInvoiceOnUserOrderEmail' => 'Rechnung generieren und an Bestellbestätigungs-E-Mail des Kunden anhängen',
]);
4 changes: 4 additions & 0 deletions views/admin_twig/en/fainvoice_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
'SHOP_MODULE_GROUP_fa_invoice_numbering' => 'Invoice numbering',
'SHOP_MODULE_fa_invoice_InvoiceNumberUpdate' => 'Update order invoice number on invoice creation',
'SHOP_MODULE_fa_invoice_InvoiceNumberFormat' => 'Invoice number format',

# Module settings invoice mails
'SHOP_MODULE_GROUP_fa_invoice_emails' => 'Email options',
'SHOP_MODULE_fa_invoice_SendInvoiceOnUserOrderEmail' => 'Generate and attach invoice to Customer order confirmation email',
]);
4 changes: 4 additions & 0 deletions views/admin_twig/lt/fainvoice_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
'SHOP_MODULE_GROUP_fa_invoice_numbering' => 'Invoice numbering',
'SHOP_MODULE_fa_invoice_InvoiceNumberUpdate' => 'Update order invoice number on invoice creation',
'SHOP_MODULE_fa_invoice_InvoiceNumberFormat' => 'Invoice number format',

# Module settings invoice mails
'SHOP_MODULE_GROUP_fa_invoice_emails' => 'Email options',
'SHOP_MODULE_fa_invoice_SendInvoiceOnUserOrderEmail' => 'Generate and attach invoice to Customer order confirmation email',
]);

0 comments on commit 15549c8

Please sign in to comment.