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