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