| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PacketCanceller |
| 4 |
* |
| 5 |
* @package Packetery\Module\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Api\Soap; |
| 13 |
use Packetery\Core\Entity; |
| 14 |
use Packetery\Core\Log; |
| 15 |
use Packetery\Module\MessageManager; |
| 16 |
use Packetery\Module\Options; |
| 17 |
use Packetery\Nette\Http\Request; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class PacketCanceller |
| 21 |
* |
| 22 |
* @package Packetery\Module\Order |
| 23 |
*/ |
| 24 |
class PacketCanceller { |
| 25 |
|
| 26 |
/** |
| 27 |
* SOAP API Client. |
| 28 |
* |
| 29 |
* @var Soap\Client SOAP API Client. |
| 30 |
*/ |
| 31 |
private $soapApiClient; |
| 32 |
|
| 33 |
/** |
| 34 |
* Order repository. |
| 35 |
* |
| 36 |
* @var Repository |
| 37 |
*/ |
| 38 |
private $orderRepository; |
| 39 |
|
| 40 |
/** |
| 41 |
* ILogger. |
| 42 |
* |
| 43 |
* @var Log\ILogger |
| 44 |
*/ |
| 45 |
private $logger; |
| 46 |
|
| 47 |
/** |
| 48 |
* Request. |
| 49 |
* |
| 50 |
* @var Request |
| 51 |
*/ |
| 52 |
private $request; |
| 53 |
|
| 54 |
/** |
| 55 |
* Options provider. |
| 56 |
* |
| 57 |
* @var Options\Provider |
| 58 |
*/ |
| 59 |
private $optionsProvider; |
| 60 |
|
| 61 |
/** |
| 62 |
* Message manager. |
| 63 |
* |
| 64 |
* @var MessageManager |
| 65 |
*/ |
| 66 |
private $messageManager; |
| 67 |
|
| 68 |
/** |
| 69 |
* Common logic. |
| 70 |
* |
| 71 |
* @var PacketActionsCommonLogic |
| 72 |
*/ |
| 73 |
private $commonLogic; |
| 74 |
|
| 75 |
/** |
| 76 |
* Constructor. |
| 77 |
* |
| 78 |
* @param Soap\Client $soapApiClient Soap client API. |
| 79 |
* @param Log\ILogger $logger Logger. |
| 80 |
* @param Repository $orderRepository Order repository. |
| 81 |
* @param Request $request Request. |
| 82 |
* @param Options\Provider $optionsProvider Options provider. |
| 83 |
* @param MessageManager $messageManager Message manager. |
| 84 |
* @param PacketActionsCommonLogic $commonLogic Common logic. |
| 85 |
*/ |
| 86 |
public function __construct( |
| 87 |
Soap\Client $soapApiClient, |
| 88 |
Log\ILogger $logger, |
| 89 |
Repository $orderRepository, |
| 90 |
Request $request, |
| 91 |
Options\Provider $optionsProvider, |
| 92 |
MessageManager $messageManager, |
| 93 |
PacketActionsCommonLogic $commonLogic |
| 94 |
) { |
| 95 |
$this->soapApiClient = $soapApiClient; |
| 96 |
$this->logger = $logger; |
| 97 |
$this->orderRepository = $orderRepository; |
| 98 |
$this->request = $request; |
| 99 |
$this->optionsProvider = $optionsProvider; |
| 100 |
$this->messageManager = $messageManager; |
| 101 |
$this->commonLogic = $commonLogic; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Process action. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function processAction(): void { |
| 110 |
$order = $this->commonLogic->getOrder(); |
| 111 |
$redirectTo = $this->request->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); |
| 112 |
$packetId = $this->request->getQuery( PacketActionsCommonLogic::PARAM_PACKET_ID ); |
| 113 |
|
| 114 |
if ( null === $order ) { |
| 115 |
$record = new Log\Record(); |
| 116 |
$record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 117 |
$record->status = Log\Record::STATUS_ERROR; |
| 118 |
$record->orderId = null; |
| 119 |
$record->title = __( 'Packet cancel error', 'packeta' ); |
| 120 |
$record->params = [ |
| 121 |
'referer' => (string) $this->request->getReferer(), |
| 122 |
'errorMessage' => 'Order not found', |
| 123 |
]; |
| 124 |
|
| 125 |
$this->logger->add( $record ); |
| 126 |
|
| 127 |
$this->messageManager->flash_message( __( 'Order not found', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 128 |
$this->commonLogic->redirectTo( $redirectTo, $order ); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$this->commonLogic->checkAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order ); |
| 133 |
|
| 134 |
$this->cancelPacket( $order, $packetId ); |
| 135 |
$this->commonLogic->redirectTo( $redirectTo, $order ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Cancels single packet. |
| 140 |
* |
| 141 |
* @param Entity\Order $order Order ID. |
| 142 |
* @param string|null $packetId Packet ID. |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function cancelPacket( Entity\Order $order, ?string $packetId ): void { |
| 147 |
if ( null === $packetId ) { |
| 148 |
$record = new Log\Record(); |
| 149 |
$record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 150 |
$record->status = Log\Record::STATUS_ERROR; |
| 151 |
$record->orderId = $order->getNumber(); |
| 152 |
$record->title = __( 'Packet cancel error', 'packeta' ); |
| 153 |
$record->params = [ |
| 154 |
'orderId' => $order->getNumber(), |
| 155 |
'packetId' => $packetId, |
| 156 |
'referer' => (string) $this->request->getReferer(), |
| 157 |
'errorMessage' => 'Packet could not be cancelled', |
| 158 |
]; |
| 159 |
|
| 160 |
$this->logger->add( $record ); |
| 161 |
|
| 162 |
$this->messageManager->flash_message( __( 'Packet could not be cancelled', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$request = new Soap\Request\CancelPacket( $packetId ); |
| 167 |
$result = $this->soapApiClient->cancelPacket( $request ); |
| 168 |
|
| 169 |
if ( ! $result->hasFault() ) { |
| 170 |
$record = new Log\Record(); |
| 171 |
$record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 172 |
$record->status = Log\Record::STATUS_SUCCESS; |
| 173 |
$record->orderId = $order->getNumber(); |
| 174 |
$record->title = __( 'Packet cancel success', 'packeta' ); |
| 175 |
$record->params = [ |
| 176 |
'orderId' => $order->getNumber(), |
| 177 |
'packetId' => $packetId, |
| 178 |
]; |
| 179 |
|
| 180 |
$this->logger->add( $record ); |
| 181 |
$errorMessage = null; |
| 182 |
} |
| 183 |
|
| 184 |
if ( $result->hasFault() ) { |
| 185 |
$record = new Log\Record(); |
| 186 |
$record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 187 |
$record->status = Log\Record::STATUS_ERROR; |
| 188 |
$record->orderId = $order->getNumber(); |
| 189 |
$record->title = __( 'Packet cancel error', 'packeta' ); |
| 190 |
$record->params = [ |
| 191 |
'orderId' => $order->getNumber(), |
| 192 |
'packetId' => $packetId, |
| 193 |
'errorMessage' => $result->getFaultString(), |
| 194 |
]; |
| 195 |
|
| 196 |
$this->logger->add( $record ); |
| 197 |
$errorMessage = $result->getFaultString(); |
| 198 |
} |
| 199 |
|
| 200 |
$order->updateApiErrorMessage( $errorMessage ); |
| 201 |
|
| 202 |
if ( $packetId === $order->getPacketId() && $this->shouldRevertSubmission( $result ) ) { |
| 203 |
$order->setIsExported( false ); |
| 204 |
$order->setIsLabelPrinted( false ); |
| 205 |
$order->setCarrierNumber( null ); |
| 206 |
$order->setPacketStatus( null ); |
| 207 |
$order->setPacketId( null ); |
| 208 |
$order->updateApiErrorMessage( null ); |
| 209 |
|
| 210 |
if ( $result->hasFault() ) { |
| 211 |
$this->messageManager->flash_message( __( 'Packet could not be canceled in the Packeta system, packet was canceled only in the order list.', 'packeta' ), MessageManager::TYPE_SUCCESS ); |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! $result->hasFault() ) { |
| 215 |
$this->messageManager->flash_message( __( 'Packet has been successfully canceled both in the order list and the Packeta system.', 'packeta' ), MessageManager::TYPE_SUCCESS ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if ( $packetId === $order->getPacketClaimId() && $this->shouldRevertSubmission( $result ) ) { |
| 220 |
$order->setPacketClaimId( null ); |
| 221 |
$order->setPacketClaimPassword( null ); |
| 222 |
$order->updateApiErrorMessage( null ); |
| 223 |
|
| 224 |
if ( $result->hasFault() ) { |
| 225 |
$this->messageManager->flash_message( __( 'Packet claim could not be canceled in the Packeta system, packet was canceled only in the order list.', 'packeta' ), MessageManager::TYPE_SUCCESS ); |
| 226 |
} |
| 227 |
|
| 228 |
if ( ! $result->hasFault() ) { |
| 229 |
$this->messageManager->flash_message( __( 'Packet claim has been successfully canceled both in the order list and the Packeta system.', 'packeta' ), MessageManager::TYPE_SUCCESS ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! $this->shouldRevertSubmission( $result ) ) { |
| 234 |
$this->messageManager->flash_message( __( 'Failed to cancel packet. See Packeta log for more details.', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 235 |
} |
| 236 |
|
| 237 |
$this->orderRepository->save( $order ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Should revert local order submission to Packeta. |
| 242 |
* |
| 243 |
* @param Soap\Response\CancelPacket|null $result Packeta API result. |
| 244 |
* |
| 245 |
* @return bool |
| 246 |
*/ |
| 247 |
public function shouldRevertSubmission( ?Soap\Response\CancelPacket $result ): bool { |
| 248 |
if ( null === $result ) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
$revertSubmission = ! $result->hasFault(); |
| 253 |
|
| 254 |
if ( $result->hasCancelNotAllowedFault() && $this->optionsProvider->isPacketCancellationForced() ) { |
| 255 |
$revertSubmission = true; |
| 256 |
} |
| 257 |
|
| 258 |
return $revertSubmission; |
| 259 |
} |
| 260 |
} |
| 261 |
|