1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Sales\Model\Order\Invoice;
use Magento\Store\Model\ScopeInterface;
use Vertex\Services\Invoice\ResponseInterface;
use Vertex\Tax\Model\Api\Data\InvoiceRequestBuilder;
use Vertex\Tax\Model\Config;
use Vertex\Tax\Model\ConfigurationValidator;
use Vertex\Tax\Model\CountryGuard;
use Vertex\Tax\Model\InvoiceSentRegistry;
use Vertex\Tax\Model\TaxInvoice;
use Vertex\Tax\Model\VertexTaxAttributeManager;
/**
* Observes when an Invoice is issued to fire off data to the Vertex Tax Log
*/
class InvoiceSavedAfterObserver implements ObserverInterface
{
/** @var VertexCalculationExtensionLoader */
private $vertexExtensionLoader;
/** @var Config */
private $config;
/** @var ConfigurationValidator */
private $configValidator;
/** @var CountryGuard */
private $countryGuard;
/** @var GiftwrapExtensionLoader */
private $extensionLoader;
/** @var InvoiceRequestBuilder */
private $invoiceRequestBuilder;
/** @var InvoiceSentRegistry */
private $invoiceSentRegistry;
/** @var ManagerInterface */
private $messageManager;
/** @var TaxInvoice */
private $taxInvoice;
/** @var VertexTaxAttributeManager */
private $attributeManager;
/** @var bool */
private $showSuccessMessage;
/**
* @param Config $config
* @param CountryGuard $countryGuard
* @param TaxInvoice $taxInvoice
* @param ManagerInterface $messageManager
* @param InvoiceSentRegistry $invoiceSentRegistry
* @param ConfigurationValidator $configValidator
* @param InvoiceRequestBuilder $invoiceRequestBuilder
* @param GiftwrapExtensionLoader $extensionLoader
* @param VertexTaxAttributeManager $attributeManager
* @param VertexCalculationExtensionLoader $vertexExtensionLoader
* @param bool $showSuccessMessage
*/
public function __construct(
Config $config,
CountryGuard $countryGuard,
TaxInvoice $taxInvoice,
ManagerInterface $messageManager,
InvoiceSentRegistry $invoiceSentRegistry,
ConfigurationValidator $configValidator,
InvoiceRequestBuilder $invoiceRequestBuilder,
GiftwrapExtensionLoader $extensionLoader,
VertexTaxAttributeManager $attributeManager,
VertexCalculationExtensionLoader $vertexExtensionLoader,
$showSuccessMessage = false
) {
$this->config = $config;
$this->countryGuard = $countryGuard;
$this->taxInvoice = $taxInvoice;
$this->messageManager = $messageManager;
$this->invoiceSentRegistry = $invoiceSentRegistry;
$this->configValidator = $configValidator;
$this->invoiceRequestBuilder = $invoiceRequestBuilder;
$this->extensionLoader = $extensionLoader;
$this->attributeManager = $attributeManager;
$this->showSuccessMessage = $showSuccessMessage;
$this->vertexExtensionLoader = $vertexExtensionLoader;
}
/**
* Commit an invoice to the Vertex Tax Log
*
* Only when Request by Invoice Creation is turned on
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
/** @var Invoice $invoice */
$invoice = $observer->getEvent()->getInvoice();
$storeId = $invoice->getStoreId();
if (!$this->config->isVertexActive($storeId) || !$this->config->isTaxCalculationEnabled($storeId)) {
return;
}
$this->extensionLoader->loadOnInvoice($invoice);
/** @var \Magento\Sales\Model\Order $order */
$order = $invoice->getOrder();
/** @var boolean $isInvoiceSent */
$isInvoiceSent = $this->invoiceSentRegistry->hasInvoiceBeenSentToVertex($invoice);
/** @var boolean $requestByInvoice */
$requestByInvoice = $this->config->requestByInvoiceCreation($invoice->getStore());
/** @var boolean $canService */
$canService = $this->countryGuard->isOrderServiceableByVertex($order);
/** @var boolean $configValid */
$configValid = $this->configValidator->execute(ScopeInterface::SCOPE_STORE, $invoice->getStoreId(), true)
->isValid();
if (!$isInvoiceSent && $requestByInvoice && $canService && $configValid) {
// During checkout for authorize & capture, the invoice will not have the address IDs
$invoice = $this->vertexExtensionLoader->loadOnInvoice($invoice);
$request = $this->invoiceRequestBuilder->buildFromInvoice($invoice);
$response = $this->taxInvoice->sendInvoiceRequest($request, $invoice->getOrder());
$this->processResponse($response, $invoice);
}
}
/**
* Process response
*
* @param null|ResponseInterface $response
* @param Invoice $invoice
* @return void
*/
private function processResponse($response, $invoice)
{
if ($response) {
$this->attributeManager->saveAllVertexAttributes($response->getLineItems());
$this->invoiceSentRegistry->setInvoiceHasBeenSentToVertex($invoice);
$this->addSuccessMessage();
}
}
/**
* Notify administrator that the order has been committed to the tax log
*
* @return void
*/
private function addSuccessMessage()
{
if ($this->showSuccessMessage) {
$this->messageManager->addSuccessMessage(__('The Vertex invoice has been sent.')->render());
}
}
}