PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Order/PacketCanceller.php +116 -46 1.5.22.3.2 View file →
@@ -10,12 +10,15 @@
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;
16 +use Packetery\Module\Framework\WpAdapter;
15 17 use Packetery\Module\MessageManager;
16 -use Packetery\Module\Options;
17 -use PacketeryNette\Http\Request;
18 +use Packetery\Module\ModuleHelper;
19 +use Packetery\Module\Options\OptionsProvider;
20 +use Packetery\Nette\Http\Request;
18 21
19 22 /**
20 23 * Class PacketCanceller
21 24 *
@@ -53,9 +56,9 @@
53 56
54 57 /**
55 58 * Options provider.
56 59 *
57 - * @var Options\Provider
60 + * @var OptionsProvider
58 61 */
59 62 private $optionsProvider;
60 63
61 64 /**
@@ -72,26 +75,37 @@
72 75 */
73 76 private $commonLogic;
74 77
75 78 /**
76 - * Constructor.
79 + * WC order actions.
77 80 *
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.
81 + * @var WcOrderActions
85 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 +
86 97 public function __construct(
87 98 Soap\Client $soapApiClient,
88 99 Log\ILogger $logger,
89 100 Repository $orderRepository,
90 101 Request $request,
91 - Options\Provider $optionsProvider,
102 + OptionsProvider $optionsProvider,
92 103 MessageManager $messageManager,
93 - PacketActionsCommonLogic $commonLogic
104 + PacketActionsCommonLogic $commonLogic,
105 + WcOrderActions $wcOrderActions,
106 + ModuleHelper $moduleHelper,
107 + WpAdapter $wpAdapter
94 108 ) {
95 109 $this->soapApiClient = $soapApiClient;
96 110 $this->logger = $logger;
97 111 $this->orderRepository = $orderRepository;
@@ -98,8 +112,11 @@
98 112 $this->request = $request;
99 113 $this->optionsProvider = $optionsProvider;
100 114 $this->messageManager = $messageManager;
101 115 $this->commonLogic = $commonLogic;
116 + $this->wcOrderActions = $wcOrderActions;
117 + $this->moduleHelper = $moduleHelper;
118 + $this->wpAdapter = $wpAdapter;
102 119 }
103 120
104 121 /**
105 122 * Process action.
@@ -108,10 +125,15 @@
108 125 */
109 126 public function processAction(): void {
110 127 $order = $this->commonLogic->getOrder();
111 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 + }
112 134
113 - if ( null === $order ) {
135 + if ( $order === null ) {
114 136 $record = new Log\Record();
115 137 $record->action = Log\Record::ACTION_PACKET_CANCEL;
116 138 $record->status = Log\Record::STATUS_ERROR;
117 139 $record->orderId = null;
@@ -124,45 +146,55 @@
124 146 $this->logger->add( $record );
125 147
126 148 $this->messageManager->flash_message( __( 'Order not found', 'packeta' ), MessageManager::TYPE_ERROR );
127 149 $this->commonLogic->redirectTo( $redirectTo, $order );
150 +
128 151 return;
129 152 }
130 153
131 154 $this->commonLogic->checkAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order );
132 155
133 - $this->cancelPacket( $order );
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 + }
134 166 $this->commonLogic->redirectTo( $redirectTo, $order );
135 167 }
136 168
137 - /**
138 - * Cancels single packet.
139 - *
140 - * @param Entity\Order $order Order ID.
141 - *
142 - * @return void
143 - */
144 - public function cancelPacket( Entity\Order $order ): void {
145 - if ( null === $order->getPacketId() ) {
146 - $record = new Log\Record();
147 - $record->action = Log\Record::ACTION_PACKET_CANCEL;
148 - $record->status = Log\Record::STATUS_ERROR;
149 - $record->orderId = $order->getNumber();
150 - $record->title = __( 'Packet cancel error', 'packeta' );
151 - $record->params = [
152 - 'orderId' => $order->getNumber(),
153 - 'packetId' => $order->getPacketId(),
154 - 'referer' => (string) $this->request->getReferer(),
155 - 'errorMessage' => 'Packet could not be cancelled',
156 - ];
169 + private function isCancellable( Entity\Order $order, ?string $packetId ): bool {
170 + if ( $packetId !== null ) {
171 + return true;
172 + }
157 173
158 - $this->logger->add( $record );
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 + ];
159 185
160 - $this->messageManager->flash_message( __( 'Packet could not be cancelled', 'packeta' ), MessageManager::TYPE_ERROR );
161 - return;
162 - }
186 + $this->logger->add( $record );
187 + $this->messageManager->flash_message( __( 'Packet could not be cancelled', 'packeta' ), MessageManager::TYPE_ERROR );
163 188
164 - $request = new Soap\Request\CancelPacket( (int) $order->getPacketId() );
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 );
165 197 $result = $this->soapApiClient->cancelPacket( $request );
166 198
167 199 if ( ! $result->hasFault() ) {
168 200 $record = new Log\Record();
@@ -171,13 +203,32 @@
171 203 $record->orderId = $order->getNumber();
172 204 $record->title = __( 'Packet cancel success', 'packeta' );
173 205 $record->params = [
174 206 'orderId' => $order->getNumber(),
175 - 'packetId' => $order->getPacketId(),
207 + 'packetId' => $packetId,
176 208 ];
177 209
178 210 $this->logger->add( $record );
179 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 + }
180 231 }
181 232
182 233 if ( $result->hasFault() ) {
183 234 $record = new Log\Record();
@@ -186,9 +237,9 @@
186 237 $record->orderId = $order->getNumber();
187 238 $record->title = __( 'Packet cancel error', 'packeta' );
188 239 $record->params = [
189 240 'orderId' => $order->getNumber(),
190 - 'packetId' => $order->getPacketId(),
241 + 'packetId' => $packetId,
191 242 'errorMessage' => $result->getFaultString(),
192 243 ];
193 244
194 245 $this->logger->add( $record );
@@ -196,14 +247,18 @@
196 247 }
197 248
198 249 $order->updateApiErrorMessage( $errorMessage );
199 250
200 - if ( $this->shouldRevertSubmission( $result ) ) {
251 + if ( $packetId === $order->getPacketId() && $this->shouldRevertSubmission( $result ) ) {
201 252 $order->setIsExported( false );
202 253 $order->setIsLabelPrinted( false );
203 254 $order->setCarrierNumber( null );
204 255 $order->setPacketStatus( null );
256 + $this->wcOrderActions->updateOrderStatus( $order->getNumber(), PacketStatus::CANCELLED );
205 257 $order->setPacketId( null );
258 + $order->setPacketTrackingUrl( null );
259 + $order->setConsignPassword( null );
260 + $order->updateApiErrorMessage( null );
206 261
207 262 if ( $result->hasFault() ) {
208 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 );
209 264 }
@@ -208,17 +263,32 @@
208 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 );
209 264 }
210 265
211 266 if ( ! $result->hasFault() ) {
212 - $this->messageManager->flash_message( __( 'Packet has been successfully canceled both in the order list and the Packeta system.', 'packeta' ), MessageManager::TYPE_SUCCESS );
267 + $this->messageManager->flash_message( __( 'Packet has been successfully canceled in the Packeta system.', 'packeta' ), MessageManager::TYPE_SUCCESS );
213 268 }
214 269 }
215 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 +
216 286 if ( ! $this->shouldRevertSubmission( $result ) ) {
217 287 $this->messageManager->flash_message( __( 'Failed to cancel packet. See Packeta log for more details.', 'packeta' ), MessageManager::TYPE_ERROR );
218 288 }
219 289
220 - $this->orderRepository->save( $order );
290 + return $this->orderRepository->save( $order );
221 291 }
222 292
223 293 /**
224 294 * Should revert local order submission to Packeta.
@@ -227,9 +297,9 @@
227 297 *
228 298 * @return bool
229 299 */
230 300 public function shouldRevertSubmission( ?Soap\Response\CancelPacket $result ): bool {
231 - if ( null === $result ) {
301 + if ( $result === null ) {
232 302 return false;
233 303 }
234 304
235 305 $revertSubmission = ! $result->hasFault();