Shipping.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Request;
  7. use Magento\Quote\Api\Data\AddressInterface;
  8. use Vertex\Data\LineItemInterface;
  9. use Vertex\Data\LineItemInterfaceFactory;
  10. use Vertex\Tax\Model\Calculation\VertexCalculator;
  11. use Vertex\Tax\Model\Config;
  12. use Vertex\Tax\Model\Repository\TaxClassNameRepository;
  13. /**
  14. * Shipping data formatter for Vertex API Calls
  15. */
  16. class Shipping
  17. {
  18. const LINE_ITEM_ID = VertexCalculator::VERTEX_SHIPPING_LINE_ITEM_ID;
  19. /** @var Config */
  20. private $config;
  21. /** @var LineItemInterfaceFactory */
  22. private $lineItemFactory;
  23. /** @var TaxClassNameRepository */
  24. private $taxClassNameRepository;
  25. /**
  26. * @param Config $config
  27. * @param TaxClassNameRepository $taxClassNameRepository
  28. * @param LineItemInterfaceFactory $lineItemFactory
  29. */
  30. public function __construct(
  31. Config $config,
  32. TaxClassNameRepository $taxClassNameRepository,
  33. LineItemInterfaceFactory $lineItemFactory
  34. ) {
  35. $this->config = $config;
  36. $this->taxClassNameRepository = $taxClassNameRepository;
  37. $this->lineItemFactory = $lineItemFactory;
  38. }
  39. /**
  40. * Create properly formatted Line Item data for the Order Shipping
  41. *
  42. * @param AddressInterface $taxAddress
  43. * @param string|null $scopeCode
  44. * @return LineItemInterface
  45. */
  46. public function getFormattedShippingLineItemData(AddressInterface $taxAddress, $scopeCode = null)
  47. {
  48. /** @var LineItemInterface $item */
  49. $item = $this->lineItemFactory->create();
  50. $item->setProductCode(substr($taxAddress->getShippingMethod(), 0, Config::MAX_CHAR_PRODUCT_CODE_ALLOWED));
  51. $item->setProductClass(
  52. substr(
  53. $this->taxClassNameRepository->getById(
  54. $this->config->getShippingTaxClassId($scopeCode)
  55. ),
  56. 0,
  57. Config::MAX_CHAR_PRODUCT_CODE_ALLOWED
  58. )
  59. );
  60. $item->setQuantity(1);
  61. $rate = $taxAddress->getShippingRateByCode($taxAddress->getShippingMethod());
  62. if (!$rate && $taxAddress->getShippingMethod()) {
  63. $taxAddress->setCollectShippingRates(true)->collectShippingRates();
  64. }
  65. foreach ($taxAddress->getAllShippingRates() as $rateCandidate) {
  66. if ($rateCandidate->getCode() === $taxAddress->getShippingMethod()) {
  67. $rate = $rateCandidate;
  68. break;
  69. }
  70. }
  71. $unitPrice = $rate ? $rate->getPrice() : 0;
  72. $extendedPrice = $unitPrice ? $unitPrice - $taxAddress->getBaseShippingDiscountAmount() : 0;
  73. $item->setUnitPrice($unitPrice);
  74. $item->setExtendedPrice($extendedPrice);
  75. $item->setLineItemId(static::LINE_ITEM_ID);
  76. return $item;
  77. }
  78. }