Inventory.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryShippingAdminUi\Block\Adminhtml\Shipment;
  8. use Magento\Backend\Block\Template;
  9. use Magento\Backend\Block\Template\Context;
  10. use Magento\Framework\Registry;
  11. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  12. use Magento\Sales\Model\Order\Shipment;
  13. /**
  14. * Display selected source on shipment creation page
  15. *
  16. * @api
  17. */
  18. class Inventory extends Template
  19. {
  20. /**
  21. * @var Registry
  22. */
  23. private $registry;
  24. /**
  25. * @var SourceRepositoryInterface
  26. */
  27. private $sourceRepository;
  28. /**
  29. * Inventory constructor.
  30. * @param Context $context
  31. * @param Registry $registry
  32. * @param SourceRepositoryInterface $sourceRepository
  33. * @param array $data
  34. */
  35. public function __construct(
  36. Context $context,
  37. Registry $registry,
  38. SourceRepositoryInterface $sourceRepository,
  39. array $data = []
  40. ) {
  41. parent::__construct($context, $data);
  42. $this->registry = $registry;
  43. $this->sourceRepository = $sourceRepository;
  44. }
  45. /**
  46. * Retrieve shipment model instance
  47. *
  48. * @return Shipment
  49. */
  50. public function getShipment()
  51. {
  52. return $this->registry->registry('current_shipment');
  53. }
  54. /**
  55. * Retrieve source code from shipment
  56. *
  57. * @return null|string
  58. */
  59. public function getSourceCode()
  60. {
  61. $shipment = $this->getShipment();
  62. $extensionAttributes = $shipment->getExtensionAttributes();
  63. if ($sourceCode = $extensionAttributes->getSourceCode()) {
  64. return $sourceCode;
  65. }
  66. return null;
  67. }
  68. /**
  69. * Get source name by code
  70. *
  71. * @param $sourceCode
  72. * @return mixed
  73. * @throws \Magento\Framework\Exception\NoSuchEntityException
  74. */
  75. public function getSourceName(string $sourceCode): string
  76. {
  77. return $this->sourceRepository->get($sourceCode)->getName();
  78. }
  79. }