VertexCalculationExtensionLoader.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Sales\Api\Data\InvoiceExtensionFactory;
  8. use Magento\Sales\Api\OrderAddressRepositoryInterface;
  9. use Magento\Sales\Api\OrderRepositoryInterface;
  10. use Magento\Sales\Model\Order;
  11. use Magento\Sales\Model\Order\Invoice;
  12. /**
  13. * Handles loading and assignment of necessary data for Tax Calculation purposes
  14. */
  15. class VertexCalculationExtensionLoader
  16. {
  17. /** @var OrderAddressRepositoryInterface */
  18. private $addressRepository;
  19. /** @var InvoiceExtensionFactory */
  20. private $invoiceExtensionFactory;
  21. /** @var OrderRepositoryInterface */
  22. private $orderRepository;
  23. /**
  24. * @param InvoiceExtensionFactory $invoiceExtensionFactory
  25. * @param OrderAddressRepositoryInterface $addressRepository
  26. * @param OrderRepositoryInterface $orderRepository
  27. */
  28. public function __construct(
  29. InvoiceExtensionFactory $invoiceExtensionFactory,
  30. OrderAddressRepositoryInterface $addressRepository,
  31. OrderRepositoryInterface $orderRepository
  32. ) {
  33. $this->invoiceExtensionFactory = $invoiceExtensionFactory;
  34. $this->addressRepository = $addressRepository;
  35. $this->orderRepository = $orderRepository;
  36. }
  37. /**
  38. * Load the addresses and Order for an Invoice onto the Vertex Tax Calculation extension attributes
  39. *
  40. * This is necessary as there are two different ways an Invoice comes across the wire with respect to the addresses
  41. * attached to it:
  42. *
  43. * The first way is when it is an existing Order that is invoiced. In this scenario, the Billing Address ID and the
  44. * Shipping Address ID are set on the Invoice.
  45. *
  46. * The second way is when it is a newly placed Order on the frontend, with an online payment method that captures
  47. * payment and triggers an invoice. In this scenario, the Order's addresses are not yet in the database and are not
  48. * yet tied to the Invoice.
  49. *
  50. * Additionally, during the second scenario we do not want to load up the Order through the repository, as it can
  51. * cause additional issues. In this scenario, we want to use the Order that is already attached to the Invoice.
  52. *
  53. * @param Invoice $originalInvoice
  54. * @return Invoice
  55. */
  56. public function loadOnInvoice(Invoice $originalInvoice)
  57. {
  58. $invoice = clone $originalInvoice;
  59. if (!$invoice->getExtensionAttributes()) {
  60. $invoice->setExtensionAttributes($this->invoiceExtensionFactory->create());
  61. } else {
  62. $invoice->setExtensionAttributes(clone $originalInvoice->getExtensionAttributes());
  63. }
  64. try {
  65. $order = $invoice->getOrder() ?: $this->orderRepository->get($invoice->getOrderId());
  66. } catch (\Exception $e) {
  67. $order = null;
  68. }
  69. $invoice->getExtensionAttributes()
  70. ->setVertexTaxCalculationOrder($order);
  71. if ($order && $order->getBillingAddress()) {
  72. $invoice->getExtensionAttributes()
  73. ->setVertexTaxCalculationBillingAddress($order->getBillingAddress());
  74. } elseif ($invoice->getBillingAddressId()) {
  75. $invoice->getExtensionAttributes()
  76. ->setVertexTaxCalculationBillingAddress(
  77. $this->addressRepository->get($invoice->getBillingAddressId())
  78. );
  79. }
  80. if ($order instanceof Order && $order->getShippingAddress()) {
  81. $invoice->getExtensionAttributes()
  82. ->setVertexTaxCalculationShippingAddress($order->getShippingAddress());
  83. } elseif ($invoice->getShippingAddressId()) {
  84. $invoice->getExtensionAttributes()
  85. ->setVertexTaxCalculationShippingAddress(
  86. $this->addressRepository->get($invoice->getShippingAddressId())
  87. );
  88. }
  89. return $invoice;
  90. }
  91. }