| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 | <?php/** * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license */namespace Temando\Shipping\ViewModel\Shipment;use Magento\Framework\View\Element\Block\ArgumentInterface;use Temando\Shipping\Model\Location\OrderAddressFactory;use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;use Temando\Shipping\Model\Shipment\CapabilityInterface;use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;use Temando\Shipping\ViewModel\DataProvider\OrderAddress as AddressRenderer;/** * View model for shipment locations. * * @package  Temando\Shipping\ViewModel * @author   Christoph Aßmann <christoph.assmann@netresearch.de> * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link     http://www.temando.com/ */class Location implements ArgumentInterface{    /**     * @var ShipmentProviderInterface     */    private $shipmentProvider;    /**     * @var RmaAccess     */    private $rmaAccess;    /**     * @var OrderAddressFactory     */    private $addressFactory;    /**     * @var AddressRenderer     */    private $addressRenderer;    /**     * Location constructor.     * @param ShipmentProviderInterface $shipmentProvider     * @param RmaAccess $rmaAccess     * @param OrderAddressFactory $addressFactory     * @param AddressRenderer $addressRenderer     */    public function __construct(        ShipmentProviderInterface $shipmentProvider,        RmaAccess $rmaAccess,        OrderAddressFactory $addressFactory,        AddressRenderer $addressRenderer    ) {        $this->shipmentProvider = $shipmentProvider;        $this->rmaAccess = $rmaAccess;        $this->addressFactory = $addressFactory;        $this->addressRenderer = $addressRenderer;    }    /**     * Detect whether delivery address is a regular address or some other     * destination type like collection point, click & collect store, etc.     *     * @return bool     */    private function isRegularAddress()    {        $shipment = $this->shipmentProvider->getShipment();        // iterate through all capabilities, conditionally switch `isRegular` flag to `false`        $isRegular = array_reduce($shipment->getCapabilities(), function ($isRegular, CapabilityInterface $capability) {            $capabilityId = $capability->getCapabilityId();            $properties = $capability->getProperties();            $isCapabilityActive = (isset($properties['required']) && $properties['required'] === true);            if ($capabilityId === 'collectionPoints' && $isCapabilityActive) {                return false;            }            return $isRegular;        }, true);        return $isRegular;    }    /**     * @return string     */    public function getShipFromAddressHtml()    {        /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */        $shipment = $this->shipmentProvider->getShipment();        if (!$shipment) {            return '';        }        $originLocation = $shipment->getOriginLocation();        $address = $this->addressFactory->createFromShipmentLocation($originLocation);        return $this->addressRenderer->getFormattedAddress($address);    }    /**     * @return string     */    public function getShipToAddressHtml()    {        /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */        $shipment = $this->shipmentProvider->getShipment();        if (!$shipment) {            return '';        }        $destinationLocation = $shipment->getDestinationLocation();        $address = $this->addressFactory->createFromShipmentLocation($destinationLocation);        return $this->addressRenderer->getFormattedAddress($address);    }    /**     * @return string     */    public function getFinalRecipientAddressHtml()    {        /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */        $shipment = $this->shipmentProvider->getShipment();        if (!$shipment) {            // no platform shipment available            return '';        }        $finalRecipientLocation = $shipment->getFinalRecipientLocation();        if (!$finalRecipientLocation) {            // final recipient property is not available            return '';        }        if ($this->isRegularAddress()) {            // destination equals final recipient, no need to display anything            return '';        }        $address = $this->addressFactory->createFromShipmentLocation($finalRecipientLocation);        return $this->addressRenderer->getFormattedAddress($address);    }    /**     * @return string     */    public function getReturnFromAddressHtml()    {        $shipment = $this->rmaAccess->getCurrentRmaShipment();        if (!$shipment) {            return '';        }        $originLocation = $this->rmaAccess->getCurrentRmaShipment()->getOriginLocation();        $address = $this->addressFactory->createFromShipmentLocation($originLocation);        return $this->addressRenderer->getFormattedAddress($address);    }    /**     * @return string     */    public function getReturnToAddressHtml()    {        $shipment = $this->rmaAccess->getCurrentRmaShipment();        if (!$shipment) {            return '';        }        $destinationLocation = $this->rmaAccess->getCurrentRmaShipment()->getDestinationLocation();        $address = $this->addressFactory->createFromShipmentLocation($destinationLocation);        return $this->addressRenderer->getFormattedAddress($address);    }    /**     * Check if shipment has a origin location.     *     * @return bool     */    public function hasOriginLocation()    {        /** @var \Temando\Shipping\Model\ShipmentInterface $shipment */        $shipment = $this->shipmentProvider->getShipment();        if (!$shipment) {            return false;        }        $originLocation = $shipment->getOriginLocation();        return $originLocation ? true : false;    }}
 |