| @@ -10,11 +10,13 @@ | ||
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | 12 | use Packetery\Core\Api\Soap; |
| 13 | 13 | use Packetery\Core\Entity; |
| 14 | +use Packetery\Core\Entity\PacketStatus; | |
| 14 | 15 | use Packetery\Core\Log; |
| 15 | 16 | use Packetery\Module\MessageManager; |
| 16 | -use Packetery\Module\Options; | |
| 17 | +use Packetery\Module\ModuleHelper; | |
| 18 | +use Packetery\Module\Options\OptionsProvider; | |
| 17 | 19 | use Packetery\Nette\Http\Request; |
| 18 | 20 | |
| 19 | 21 | /** |
| 20 | 22 | * Class PacketCanceller |
| @@ -53,9 +55,9 @@ | ||
| 53 | 55 | |
| 54 | 56 | /** |
| 55 | 57 | * Options provider. |
| 56 | 58 | * |
| 57 | - * @var Options\Provider | |
| 59 | + * @var OptionsProvider | |
| 58 | 60 | */ |
| 59 | 61 | private $optionsProvider; |
| 60 | 62 | |
| 61 | 63 | /** |
| @@ -72,8 +74,22 @@ | ||
| 72 | 74 | */ |
| 73 | 75 | private $commonLogic; |
| 74 | 76 | |
| 75 | 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 | + /** | |
| 76 | 92 | * Constructor. |
| 77 | 93 | * |
| 78 | 94 | * @param Soap\Client $soapApiClient Soap client API. |
| 79 | 95 | * @param Log\ILogger $logger Logger. |
| @@ -78,11 +94,13 @@ | ||
| 78 | 94 | * @param Soap\Client $soapApiClient Soap client API. |
| 79 | 95 | * @param Log\ILogger $logger Logger. |
| 80 | 96 | * @param Repository $orderRepository Order repository. |
| 81 | 97 | * @param Request $request Request. |
| 82 | - * @param Options\Provider $optionsProvider Options provider. | |
| 98 | + * @param OptionsProvider $optionsProvider Options provider. | |
| 83 | 99 | * @param MessageManager $messageManager Message manager. |
| 84 | 100 | * @param PacketActionsCommonLogic $commonLogic Common logic. |
| 101 | + * @param WcOrderActions $wcOrderActions WC order actions. | |
| 102 | + * @param ModuleHelper $moduleHelper ModuleHelper. | |
| 85 | 103 | */ |
| 86 | 104 | public function __construct( |
| 87 | 105 | Soap\Client $soapApiClient, |
| 88 | 106 | Log\ILogger $logger, |
| @@ -87,11 +105,13 @@ | ||
| 87 | 105 | Soap\Client $soapApiClient, |
| 88 | 106 | Log\ILogger $logger, |
| 89 | 107 | Repository $orderRepository, |
| 90 | 108 | Request $request, |
| 91 | - Options\Provider $optionsProvider, | |
| 109 | + OptionsProvider $optionsProvider, | |
| 92 | 110 | MessageManager $messageManager, |
| 93 | - PacketActionsCommonLogic $commonLogic | |
| 111 | + PacketActionsCommonLogic $commonLogic, | |
| 112 | + WcOrderActions $wcOrderActions, | |
| 113 | + ModuleHelper $moduleHelper | |
| 94 | 114 | ) { |
| 95 | 115 | $this->soapApiClient = $soapApiClient; |
| 96 | 116 | $this->logger = $logger; |
| 97 | 117 | $this->orderRepository = $orderRepository; |
| @@ -98,8 +118,10 @@ | ||
| 98 | 118 | $this->request = $request; |
| 99 | 119 | $this->optionsProvider = $optionsProvider; |
| 100 | 120 | $this->messageManager = $messageManager; |
| 101 | 121 | $this->commonLogic = $commonLogic; |
| 122 | + $this->wcOrderActions = $wcOrderActions; | |
| 123 | + $this->moduleHelper = $moduleHelper; | |
| 102 | 124 | } |
| 103 | 125 | |
| 104 | 126 | /** |
| 105 | 127 | * Process action. |
| @@ -110,9 +132,9 @@ | ||
| 110 | 132 | $order = $this->commonLogic->getOrder(); |
| 111 | 133 | $redirectTo = $this->request->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); |
| 112 | 134 | $packetId = $this->request->getQuery( PacketActionsCommonLogic::PARAM_PACKET_ID ); |
| 113 | 135 | |
| 114 | - if ( null === $order ) { | |
| 136 | + if ( $order === null ) { | |
| 115 | 137 | $record = new Log\Record(); |
| 116 | 138 | $record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 117 | 139 | $record->status = Log\Record::STATUS_ERROR; |
| 118 | 140 | $record->orderId = null; |
| @@ -125,8 +147,9 @@ | ||
| 125 | 147 | $this->logger->add( $record ); |
| 126 | 148 | |
| 127 | 149 | $this->messageManager->flash_message( __( 'Order not found', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 128 | 150 | $this->commonLogic->redirectTo( $redirectTo, $order ); |
| 151 | + | |
| 129 | 152 | return; |
| 130 | 153 | } |
| 131 | 154 | |
| 132 | 155 | $this->commonLogic->checkAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order ); |
| @@ -143,9 +166,9 @@ | ||
| 143 | 166 | * |
| 144 | 167 | * @return void |
| 145 | 168 | */ |
| 146 | 169 | public function cancelPacket( Entity\Order $order, ?string $packetId ): void { |
| 147 | - if ( null === $packetId ) { | |
| 170 | + if ( $packetId === null ) { | |
| 148 | 171 | $record = new Log\Record(); |
| 149 | 172 | $record->action = Log\Record::ACTION_PACKET_CANCEL; |
| 150 | 173 | $record->status = Log\Record::STATUS_ERROR; |
| 151 | 174 | $record->orderId = $order->getNumber(); |
| @@ -159,8 +182,9 @@ | ||
| 159 | 182 | |
| 160 | 183 | $this->logger->add( $record ); |
| 161 | 184 | |
| 162 | 185 | $this->messageManager->flash_message( __( 'Packet could not be cancelled', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 186 | + | |
| 163 | 187 | return; |
| 164 | 188 | } |
| 165 | 189 | |
| 166 | 190 | $request = new Soap\Request\CancelPacket( $packetId ); |
| @@ -178,8 +202,27 @@ | ||
| 178 | 202 | ]; |
| 179 | 203 | |
| 180 | 204 | $this->logger->add( $record ); |
| 181 | 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 | + } | |
| 182 | 225 | } |
| 183 | 226 | |
| 184 | 227 | if ( $result->hasFault() ) { |
| 185 | 228 | $record = new Log\Record(); |
| @@ -203,9 +246,11 @@ | ||
| 203 | 246 | $order->setIsExported( false ); |
| 204 | 247 | $order->setIsLabelPrinted( false ); |
| 205 | 248 | $order->setCarrierNumber( null ); |
| 206 | 249 | $order->setPacketStatus( null ); |
| 250 | + $this->wcOrderActions->updateOrderStatus( $order->getNumber(), PacketStatus::CANCELLED ); | |
| 207 | 251 | $order->setPacketId( null ); |
| 252 | + $order->setPacketTrackingUrl( null ); | |
| 208 | 253 | $order->updateApiErrorMessage( null ); |
| 209 | 254 | |
| 210 | 255 | if ( $result->hasFault() ) { |
| 211 | 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 ); |
| @@ -217,8 +262,9 @@ | ||
| 217 | 262 | } |
| 218 | 263 | |
| 219 | 264 | if ( $packetId === $order->getPacketClaimId() && $this->shouldRevertSubmission( $result ) ) { |
| 220 | 265 | $order->setPacketClaimId( null ); |
| 266 | + $order->setPacketClaimTrackingUrl( null ); | |
| 221 | 267 | $order->setPacketClaimPassword( null ); |
| 222 | 268 | $order->updateApiErrorMessage( null ); |
| 223 | 269 | |
| 224 | 270 | if ( $result->hasFault() ) { |
| @@ -244,9 +290,9 @@ | ||
| 244 | 290 | * |
| 245 | 291 | * @return bool |
| 246 | 292 | */ |
| 247 | 293 | public function shouldRevertSubmission( ?Soap\Response\CancelPacket $result ): bool { |
| 248 | - if ( null === $result ) { | |
| 294 | + if ( $result === null ) { | |
| 249 | 295 | return false; |
| 250 | 296 | } |
| 251 | 297 | |
| 252 | 298 | $revertSubmission = ! $result->hasFault(); |