Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/Ecombricks/InventoryInventorySourceSelection/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/Ecombricks/InventoryInventorySourceSelection/Model/GetInventoryRequestFromOrder.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Ecombricks\InventoryInventorySourceSelection\Model;

/**
 * Get inventory request from order
 */
class GetInventoryRequestFromOrder
{
    
    /**
     * Inventory request factory
     * 
     * @var \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory
     */
    protected $inventoryRequestFactory;
    
    /**
     * Inventory request extension factory
     * 
     * @var \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestExtensionInterfaceFactory
     */
    protected $inventoryRequestExtensionFactory;
    
    /**
     * Order repository
     * 
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;
    
    /**
     * Address interface factory
     * 
     * @var \Magento\InventorySourceSelectionApi\Api\Data\AddressInterfaceFactory
     */
    protected $addressInterfaceFactory;
    
    /**
     * Store manager
     * 
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    
    /**
     * Stock by website ID resolver
     * 
     * @var \Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface
     */
    protected $stockByWebsiteIdResolver;
    
    /**
     * Product metadata
     * 
     * @var \Magento\Framework\App\ProductMetadata 
     */
    protected $productMetadata;
    
    /**
     * Constructor
     * 
     * @param \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory $inventoryRequestFactory
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
     * @param \Magento\Framework\App\ProductMetadata $productMetadata
     */
    public function __construct(
        \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory $inventoryRequestFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
        \Magento\Framework\App\ProductMetadata $productMetadata
    )
    {
        $this->inventoryRequestFactory = $inventoryRequestFactory;
        $this->orderRepository = $orderRepository;
        $this->storeManager = $storeManager;
        $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
        $this->productMetadata = $productMetadata;
        if (version_compare($this->productMetadata->getVersion(), '2.3.1', '>=')) {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $this->inventoryRequestExtensionFactory = $objectManager->get(\Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestExtensionInterfaceFactory::class);
            $this->addressInterfaceFactory = $objectManager->get(\Magento\InventorySourceSelectionApi\Api\Data\AddressInterfaceFactory::class);
        }
    }
    
    /**
     * Get address from order
     *
     * @param \Magento\Sales\Api\Data\OrderInterface $order
     * @return null|\Magento\InventorySourceSelectionApi\Api\Data\AddressInterface
     */
    protected function getAddressFromOrder(\Magento\Sales\Api\Data\OrderInterface $order)
    {
        $shippingAddress = $order->getShippingAddress();
        if ($shippingAddress === null) {
            return null;
        }
        return $this->addressInterfaceFactory->create(
            [
                'country' => $shippingAddress->getCountryId(),
                'postcode' => $shippingAddress->getPostcode() ?? '',
                'street' => implode("\n", $shippingAddress->getStreet()),
                'region' => $shippingAddress->getRegion() ?? $shippingAddress->getRegionCode() ?? '',
                'city' => $shippingAddress->getCity() ?? '',
            ]
        );
    }
    
    /**
     * Execute
     *
     * @param int $orderId
     * @param array $requestItems
     */
    public function execute(int $orderId, array $requestItems): \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface
    {
        $order = $this->orderRepository->get($orderId);
        $store = $this->storeManager->getStore($order->getStoreId());
        $stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId());
        $inventoryRequest = $this->inventoryRequestFactory->create([ 'stockId' => $stock->getStockId(), 'items' => $requestItems, ]);
        if (version_compare($this->productMetadata->getVersion(), '2.3.1', '>=')) {
            $address = $this->getAddressFromOrder($order);
            if ($address !== null) {
                $extensionAttributes = $this->inventoryRequestExtensionFactory->create();
                $extensionAttributes->setDestinationAddress($address);
                $inventoryRequest->setExtensionAttributes($extensionAttributes);
            }
        }
        return $inventoryRequest;
    }
    
}

Spamworldpro Mini