InvoiceSavedAfterObserver.php 5.7 KB

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