1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?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;
}
}