| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class DetailCommonLogic. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity\Order; |
| 13 |
use Packetery\Core\Entity\PickupPoint; |
| 14 |
use Packetery\Module; |
| 15 |
use Packetery\Module\ContextResolver; |
| 16 |
use Packetery\Module\Framework\WcAdapter; |
| 17 |
use Packetery\Module\Options\OptionsProvider; |
| 18 |
use Packetery\Module\Shipping\ShippingProvider; |
| 19 |
use Packetery\Nette\Http; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class DetailCommonLogic. |
| 23 |
* |
| 24 |
* @package Packetery |
| 25 |
*/ |
| 26 |
class DetailCommonLogic { |
| 27 |
|
| 28 |
/** |
| 29 |
* Order. |
| 30 |
* |
| 31 |
* @var Order|null |
| 32 |
*/ |
| 33 |
private $order; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var ContextResolver |
| 37 |
*/ |
| 38 |
private $contextResolver; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Http\Request |
| 42 |
*/ |
| 43 |
private $request; |
| 44 |
|
| 45 |
/** |
| 46 |
* Order repository. |
| 47 |
* |
| 48 |
* @var Repository |
| 49 |
*/ |
| 50 |
private $orderRepository; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var OptionsProvider |
| 54 |
*/ |
| 55 |
private $optionsProvider; |
| 56 |
|
| 57 |
/** |
| 58 |
* @var WcAdapter |
| 59 |
*/ |
| 60 |
private $wcAdapter; |
| 61 |
|
| 62 |
public function __construct( |
| 63 |
ContextResolver $contextResolver, |
| 64 |
Http\Request $request, |
| 65 |
Repository $orderRepository, |
| 66 |
OptionsProvider $optionsProvider, |
| 67 |
WcAdapter $wcAdapter |
| 68 |
) { |
| 69 |
$this->contextResolver = $contextResolver; |
| 70 |
$this->request = $request; |
| 71 |
$this->orderRepository = $orderRepository; |
| 72 |
$this->optionsProvider = $optionsProvider; |
| 73 |
$this->wcAdapter = $wcAdapter; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets order. |
| 78 |
* |
| 79 |
* @return Order|null |
| 80 |
*/ |
| 81 |
public function getOrder(): ?Order { |
| 82 |
if ( $this->order !== null ) { |
| 83 |
return $this->order; |
| 84 |
} |
| 85 |
|
| 86 |
$orderId = $this->getOrderId(); |
| 87 |
if ( $orderId === null ) { |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
$this->order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); |
| 92 |
|
| 93 |
return $this->order; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Gets order ID. |
| 98 |
* |
| 99 |
* @return int|null |
| 100 |
*/ |
| 101 |
public function getOrderId(): ?int { |
| 102 |
global $post; |
| 103 |
|
| 104 |
if ( $this->contextResolver->isOrderDetailPage() === false ) { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
$idParam = $this->request->getQuery( 'id' ); |
| 109 |
if ( $idParam !== null && Module\ModuleHelper::isHposEnabled() ) { |
| 110 |
return (int) $idParam; |
| 111 |
} |
| 112 |
|
| 113 |
return (int) $post->ID; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks if Packeta order detail is displayed. |
| 118 |
* |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
public function isPacketeryOrder(): bool { |
| 122 |
$orderId = $this->getOrderId(); |
| 123 |
if ( $orderId === null ) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
$wcOrder = $this->orderRepository->getWcOrderById( $orderId ); |
| 128 |
if ( $wcOrder === null ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! ShippingProvider::wcOrderHasOurMethod( $wcOrder ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Tells if pickup point info should be displayed. |
| 141 |
* |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
public function shouldDisplayPickupPointInfo(): bool { |
| 145 |
return ( |
| 146 |
! $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() || |
| 147 |
$this->wcAdapter->shipToBillingAddressOnly() |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Determines if a Packeta order should be displayed. |
| 153 |
*/ |
| 154 |
public function shouldHidePacketaInfo( Order $order ): bool { |
| 155 |
$isPickupPointInfoVisible = $this->shouldDisplayPickupPointInfo() && $order->getPickupPoint() instanceof PickupPoint; |
| 156 |
|
| 157 |
return ( ! $isPickupPointInfoVisible ) && $order->isExported() === false; |
| 158 |
} |
| 159 |
} |
| 160 |
|