TaxRegistrationBuilder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Model\Api\Data;
  7. use Magento\Customer\Api\Data\AddressInterface;
  8. use Magento\Sales\Api\Data\OrderAddressInterface;
  9. use Vertex\Data\TaxRegistration;
  10. use Vertex\Data\TaxRegistrationFactory;
  11. /**
  12. * Builds a TaxRegistration object for use with the Vertex SDK
  13. */
  14. class TaxRegistrationBuilder
  15. {
  16. /** @var TaxRegistrationFactory */
  17. private $taxRegistrationFactory;
  18. /**
  19. * @param TaxRegistrationFactory $taxRegistrationFactory
  20. */
  21. public function __construct(TaxRegistrationFactory $taxRegistrationFactory)
  22. {
  23. $this->taxRegistrationFactory = $taxRegistrationFactory;
  24. }
  25. /**
  26. * Generate a VAT TaxRegistration from a Customer Address
  27. *
  28. * @param AddressInterface $address
  29. * @return TaxRegistration
  30. * @throws \InvalidArgumentException When address without VAT is specified
  31. */
  32. public function buildFromCustomerAddress(AddressInterface $address)
  33. {
  34. if (!$address->getVatId()) {
  35. throw new \InvalidArgumentException('Address does not contain VAT');
  36. }
  37. /** @var TaxRegistration $registration */
  38. $registration = $this->taxRegistrationFactory->create();
  39. $registration->setRegistrationNumber($address->getVatId())
  40. ->setImpositionType('VAT');
  41. if ($address->getCountryId()) {
  42. $registration->setCountryCode($address->getCountryId());
  43. }
  44. return $registration;
  45. }
  46. /**
  47. * Generate a VAT TaxRegistration from an Order Address
  48. *
  49. * @param OrderAddressInterface $address
  50. * @return TaxRegistration
  51. * @throws \InvalidArgumentException When address without VAT is specified
  52. */
  53. public function buildFromOrderAddress(OrderAddressInterface $address)
  54. {
  55. if (!$address->getVatId()) {
  56. throw new \InvalidArgumentException('Address does not contain VAT');
  57. }
  58. /** @var TaxRegistration $registration */
  59. $registration = $this->taxRegistrationFactory->create();
  60. $registration->setRegistrationNumber($address->getVatId())
  61. ->setImpositionType('VAT');
  62. if ($address->getCountryId()) {
  63. $registration->setCountryCode($address->getCountryId());
  64. }
  65. return $registration;
  66. }
  67. }