| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 | <?php/** * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license */namespace Temando\Shipping\ViewModel\Shipment;use Magento\Backend\Model\UrlInterface;use Magento\Framework\DataObject;use Magento\Framework\View\Element\Block\ArgumentInterface;use Temando\Shipping\Model\DispatchProviderInterface;use Temando\Shipping\Model\ResourceModel\Rma\RmaAccess;use Temando\Shipping\Model\Shipment\PackageInterface;use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;use Temando\Shipping\Model\ShipmentInterface;use Temando\Shipping\Model\ShipmentInterfaceFactory;/** * View model for shipment related information. * * @package Temando\Shipping\ViewModel * @author  Christoph Aßmann <christoph.assmann@netresearch.de> * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link    https://www.temando.com/ */class ShipmentDetails implements ArgumentInterface{    /**     * @var ShipmentInterfaceFactory     */    private $shipmentFactory;    /**     * @var ShipmentProviderInterface     */    private $shipmentProvider;    /**     * @var DispatchProviderInterface     */    private $dispatchProvider;    /**     * @var RmaAccess     */    private $rmaAccess;    /**     * @var UrlInterface     */    private $urlBuilder;    /**     * ShipmentDetails constructor.     * @param ShipmentInterfaceFactory $shipmentFactory     * @param ShipmentProviderInterface $shipmentProvider     * @param DispatchProviderInterface $dispatchProvider     * @param RmaAccess $rmaAccess     * @param UrlInterface $urlBuilder     */    public function __construct(        ShipmentInterfaceFactory $shipmentFactory,        ShipmentProviderInterface $shipmentProvider,        DispatchProviderInterface $dispatchProvider,        RmaAccess $rmaAccess,        UrlInterface $urlBuilder    ) {        $this->shipmentFactory = $shipmentFactory;        $this->shipmentProvider = $shipmentProvider;        $this->dispatchProvider = $dispatchProvider;        $this->rmaAccess = $rmaAccess;        $this->urlBuilder = $urlBuilder;    }    /**     * Get the Shipment.     *     * @return ShipmentInterface     */    private function getShipment(): ShipmentInterface    {        if ($this->shipmentProvider->getShipment()) {            return $this->shipmentProvider->getShipment();        }        if ($this->rmaAccess->getCurrentRmaShipment()) {            return $this->rmaAccess->getCurrentRmaShipment();        }        return $this->shipmentFactory->create();    }    /**     * Get the view action URL.     *     * @param string $extShipmentId     * @return string     */    public function getViewActionUrl($extShipmentId): string    {        return $this->urlBuilder->getUrl('temando/shipment/view', ['shipment_id' => $extShipmentId]);    }    /**     * Get External Shipment ID.     *     * @return string     */    public function getExtShipmentId(): string    {        $shipment = $this->getShipment();        return ($shipment ? (string) $shipment->getShipmentId() : '');    }    /**     * Get Customer Reference.     *     * @return string     */    public function getCustomerReference(): string    {        $shipment = $this->getShipment();        return ($shipment ? (string) $shipment->getCustomerReference() : '');    }    /**     * Get Shipment Status     *     * @return string     */    public function getStatus(): string    {        $shipment = $this->getShipment();        return ($shipment ? ucwords($shipment->getStatus()) : '');    }    /**     * Check if status can be shown.     *     * @return bool     */    public function showStatus(): bool    {        $shipment = $this->getShipment();        return ($shipment && $shipment->getStatus() === 'cancelled');    }    /**     * Get Shipment documentation.     *     * @return DataObject[]     */    public function getDocumentation(): array    {        if ($this->dispatchProvider->getDispatch()) {            $dispatch = $this->dispatchProvider->getDispatch();            $documentation = $dispatch->getDocumentation();        } else {            $shipment = $this->getShipment();            $documentation = $shipment->getDocumentation();        }        /** @var DataObject[] $documentation */        return $documentation ?: [];    }    /**     * Get Shipment packages.     *     * @return DataObject[]     */    public function getPackages(): array    {        /** @var DataObject[] $packages */        $packages = $this->getShipment()->getPackages();        return $packages ?: [];    }    /**     * Get Shipment items.     *     * @return DataObject[]     */    public function getItems(): array    {        $packages = $this->getShipment()->getPackages() ?: [];        $items = array_reduce($packages, function (array $items, PackageInterface $package) {            $items = array_merge($items, $package->getItems());            return $items;        }, []);        return $items;    }    /**     * Get Documentation display name.     *     * @param string $documentationType     * @return \Magento\Framework\Phrase     */    public function getDocumentationDisplayName($documentationType): \Magento\Framework\Phrase    {        $fileTypeNames = [            'nafta' => 'NAFTA',            'certificateOfOrigin' => 'Certificate Of Origin',            'cn22' => 'CN 22',            'cn23' => 'CN 23',            'codTurnInPage' => 'Cash On Delivery Turn In Page',            'commercialInvoice' => 'Commercial Invoice',            'customerInvoice' => 'Customer Invoice',            'highValueReport' => 'High Value Report',            'manifestSummary' => 'Manifest Summary',            'packageLabel' => 'Package Label',            'packageReturnLabel' => 'Package Return Label',            'packagingList' => 'Packaging List',            'proofOfDelivery' => 'Proof Of Delivery'        ];        $displayName = isset($fileTypeNames[$documentationType])            ? $fileTypeNames[$documentationType]            : $documentationType;        return __($displayName);    }    /**     * Check if Shipment is paperless.     *     * @return bool     */    public function isShipmentPaperless(): bool    {        $shipment = $this->getShipment();        if ($shipment->getShipmentId()) {            $originCountryCode = $shipment->getOriginLocation()->getCountryCode();            $destinationCountryCode = $shipment->getDestinationLocation()->getCountryCode();            if (($originCountryCode != $destinationCountryCode) && $shipment->isPaperless()) {                return true;            }        }        return false;    }}
 |