123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?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());
- }
- }
- }
|