| @@ -9,18 +9,21 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | 12 | use Packetery\Core\Api\Soap\Client; |
| 13 | +use Packetery\Core\Api\Soap\ILabelResponse; | |
| 13 | 14 | use Packetery\Core\Api\Soap\Request; |
| 14 | 15 | use Packetery\Core\Api\Soap\Response; |
| 16 | +use Packetery\Core\Entity\Order; | |
| 15 | 17 | use Packetery\Core\Log; |
| 18 | +use Packetery\Latte\Engine; | |
| 16 | 19 | use Packetery\Module\FormFactory; |
| 17 | 20 | use Packetery\Module\MessageManager; |
| 18 | -use Packetery\Module\Options\Provider; | |
| 21 | +use Packetery\Module\ModuleHelper; | |
| 22 | +use Packetery\Module\Options\OptionsProvider; | |
| 19 | 23 | use Packetery\Module\Plugin; |
| 20 | -use PacketeryLatte\Engine; | |
| 21 | -use PacketeryNette\Forms\Form; | |
| 22 | -use PacketeryNette\Http; | |
| 24 | +use Packetery\Nette\Forms\Form; | |
| 25 | +use Packetery\Nette\Http; | |
| 23 | 26 | |
| 24 | 27 | /** |
| 25 | 28 | * Class LabelPrint. |
| 26 | 29 | * |
| @@ -41,9 +44,9 @@ | ||
| 41 | 44 | |
| 42 | 45 | /** |
| 43 | 46 | * Options Provider |
| 44 | 47 | * |
| 45 | - * @var Provider | |
| 48 | + * @var OptionsProvider | |
| 46 | 49 | */ |
| 47 | 50 | private $optionsProvider; |
| 48 | 51 | |
| 49 | 52 | /** |
| @@ -88,37 +91,57 @@ | ||
| 88 | 91 | */ |
| 89 | 92 | private $orderRepository; |
| 90 | 93 | |
| 91 | 94 | /** |
| 95 | + * Packet actions common logic. | |
| 96 | + * | |
| 97 | + * @var PacketActionsCommonLogic | |
| 98 | + */ | |
| 99 | + private $packetActionsCommonLogic; | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * ModuleHelper. | |
| 103 | + * | |
| 104 | + * @var ModuleHelper | |
| 105 | + */ | |
| 106 | + private $moduleHelper; | |
| 107 | + | |
| 108 | + /** | |
| 92 | 109 | * LabelPrint constructor. |
| 93 | 110 | * |
| 94 | - * @param Engine $latteEngine Latte Engine. | |
| 95 | - * @param Provider $optionsProvider Options provider. | |
| 96 | - * @param FormFactory $formFactory Form factory. | |
| 97 | - * @param Http\Request $httpRequest Http Request. | |
| 98 | - * @param Client $soapApiClient SOAP API Client. | |
| 99 | - * @param MessageManager $messageManager Message Manager. | |
| 100 | - * @param Log\ILogger $logger Logger. | |
| 101 | - * @param Repository $orderRepository Order repository. | |
| 111 | + * @param Engine $latteEngine Latte Engine. | |
| 112 | + * @param OptionsProvider $optionsProvider Options provider. | |
| 113 | + * @param FormFactory $formFactory Form factory. | |
| 114 | + * @param Http\Request $httpRequest Http Request. | |
| 115 | + * @param Client $soapApiClient SOAP API Client. | |
| 116 | + * @param MessageManager $messageManager Message Manager. | |
| 117 | + * @param Log\ILogger $logger Logger. | |
| 118 | + * @param Repository $orderRepository Order repository. | |
| 119 | + * @param PacketActionsCommonLogic $packetActionsCommonLogic Packet actions common logic. | |
| 120 | + * @param ModuleHelper $moduleHelper ModuleHelper. | |
| 102 | 121 | */ |
| 103 | 122 | public function __construct( |
| 104 | 123 | Engine $latteEngine, |
| 105 | - Provider $optionsProvider, | |
| 124 | + OptionsProvider $optionsProvider, | |
| 106 | 125 | FormFactory $formFactory, |
| 107 | 126 | Http\Request $httpRequest, |
| 108 | 127 | Client $soapApiClient, |
| 109 | 128 | MessageManager $messageManager, |
| 110 | 129 | Log\ILogger $logger, |
| 111 | - Repository $orderRepository | |
| 130 | + Repository $orderRepository, | |
| 131 | + PacketActionsCommonLogic $packetActionsCommonLogic, | |
| 132 | + ModuleHelper $moduleHelper | |
| 112 | 133 | ) { |
| 113 | - $this->latteEngine = $latteEngine; | |
| 114 | - $this->optionsProvider = $optionsProvider; | |
| 115 | - $this->formFactory = $formFactory; | |
| 116 | - $this->httpRequest = $httpRequest; | |
| 117 | - $this->soapApiClient = $soapApiClient; | |
| 118 | - $this->messageManager = $messageManager; | |
| 119 | - $this->logger = $logger; | |
| 120 | - $this->orderRepository = $orderRepository; | |
| 134 | + $this->latteEngine = $latteEngine; | |
| 135 | + $this->optionsProvider = $optionsProvider; | |
| 136 | + $this->formFactory = $formFactory; | |
| 137 | + $this->httpRequest = $httpRequest; | |
| 138 | + $this->soapApiClient = $soapApiClient; | |
| 139 | + $this->messageManager = $messageManager; | |
| 140 | + $this->logger = $logger; | |
| 141 | + $this->orderRepository = $orderRepository; | |
| 142 | + $this->packetActionsCommonLogic = $packetActionsCommonLogic; | |
| 143 | + $this->moduleHelper = $moduleHelper; | |
| 121 | 144 | } |
| 122 | 145 | |
| 123 | 146 | /** |
| 124 | 147 | * Generates id of transient to store order ids. |
| @@ -138,8 +161,23 @@ | ||
| 138 | 161 | return 'packetery_label_print_back_link_' . wp_get_session_token(); |
| 139 | 162 | } |
| 140 | 163 | |
| 141 | 164 | /** |
| 165 | + * Gets label type. | |
| 166 | + * | |
| 167 | + * @param Order $order Order. | |
| 168 | + * | |
| 169 | + * @return string | |
| 170 | + */ | |
| 171 | + public function getLabelFormatByOrder( Order $order ): string { | |
| 172 | + if ( $order->isExternalCarrier() ) { | |
| 173 | + return $this->optionsProvider->get_carrier_label_format(); | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $this->optionsProvider->get_packeta_label_format(); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 142 | 180 | * Prepares form and renders template. |
| 143 | 181 | */ |
| 144 | 182 | public function render(): void { |
| 145 | 183 | $form = $this->createForm( $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ) ); |
| @@ -145,11 +183,15 @@ | ||
| 145 | 183 | $form = $this->createForm( $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ) ); |
| 146 | 184 | |
| 147 | 185 | $count = 0; |
| 148 | 186 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 149 | - $orderIds = $this->getPacketIdsFromTransient( $isCarrierLabels ); | |
| 150 | - if ( $orderIds ) { | |
| 151 | - $count = count( $orderIds ); | |
| 187 | + | |
| 188 | + $orderIdsTransient = $this->getOrderIdsTransient(); | |
| 189 | + if ( $orderIdsTransient !== null ) { | |
| 190 | + $orderIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); | |
| 191 | + $count = count( $orderIds ); | |
| 192 | + } else { | |
| 193 | + $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ); | |
| 152 | 194 | } |
| 153 | 195 | |
| 154 | 196 | $this->latteEngine->render( |
| 155 | 197 | PACKETERY_PLUGIN_DIR . '/template/order/label-print.latte', |
| @@ -157,8 +199,17 @@ | ||
| 157 | 199 | 'form' => $form, |
| 158 | 200 | 'count' => $count, |
| 159 | 201 | 'backLink' => get_transient( self::getBackLinkTransientName() ), |
| 160 | 202 | 'flashMessages' => $this->messageManager->renderToString( MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ), |
| 203 | + 'translations' => [ | |
| 204 | + 'packeta' => __( 'Packeta', 'packeta' ), | |
| 205 | + 'labelPrinting' => __( 'Print labels', 'packeta' ), | |
| 206 | + // translators: %s is count. | |
| 207 | + 'numberOfLabelsToPrint' => __( 'Number of labels to print: %s', 'packeta' ), | |
| 208 | + | |
| 209 | + 'back' => __( 'Back', 'packeta' ), | |
| 210 | + 'printLabels' => __( 'Print labels', 'packeta' ), | |
| 211 | + ], | |
| 161 | 212 | ] |
| 162 | 213 | ); |
| 163 | 214 | } |
| 164 | 215 | |
| @@ -167,10 +218,10 @@ | ||
| 167 | 218 | * |
| 168 | 219 | * @return string |
| 169 | 220 | */ |
| 170 | 221 | private function getLabelFormat(): string { |
| 171 | - $packetaLabelFormat = ( $this->optionsProvider->get_packeta_label_format() ?? '' ); | |
| 172 | - $carrierLabelFormat = ( $this->optionsProvider->get_carrier_label_format() ?? '' ); | |
| 222 | + $packetaLabelFormat = $this->optionsProvider->get_packeta_label_format(); | |
| 223 | + $carrierLabelFormat = $this->optionsProvider->get_carrier_label_format(); | |
| 173 | 224 | |
| 174 | 225 | return ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ? $carrierLabelFormat : $packetaLabelFormat ); |
| 175 | 226 | } |
| 176 | 227 | |
| @@ -183,27 +234,25 @@ | ||
| 183 | 234 | } |
| 184 | 235 | |
| 185 | 236 | $fallbackToPacketaLabel = false; |
| 186 | 237 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 187 | - $idParam = $this->httpRequest->getQuery( 'id' ); | |
| 238 | + $idParam = $this->httpRequest->getQuery( 'id' ) !== null ? (int) $this->httpRequest->getQuery( 'id' ) : null; | |
| 188 | 239 | $packetIdParam = $this->httpRequest->getQuery( 'packet_id' ); |
| 189 | - if ( null !== $idParam && null !== $packetIdParam ) { | |
| 240 | + if ( $idParam !== null && $packetIdParam !== null ) { | |
| 190 | 241 | $fallbackToPacketaLabel = true; |
| 191 | 242 | $packetIds = [ $idParam => $packetIdParam ]; |
| 192 | 243 | } else { |
| 193 | - if ( ! get_transient( self::getOrderIdsTransientName() ) ) { | |
| 194 | - $this->messageManager->flash_message( __( 'noOrdersSelected', 'packetery' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ); | |
| 195 | - | |
| 244 | + $orderIdsTransient = $this->getOrderIdsTransient(); | |
| 245 | + if ( $orderIdsTransient === null ) { | |
| 196 | 246 | return; |
| 197 | 247 | } |
| 198 | 248 | |
| 199 | - $packetIds = $this->getPacketIdsFromTransient( $isCarrierLabels ); | |
| 249 | + $packetIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); | |
| 200 | 250 | } |
| 201 | - if ( ! $packetIds ) { | |
| 202 | - $this->messageManager->flash_message( __( 'noSuitableOrdersSelected', 'packetery' ), 'info' ); | |
| 203 | - if ( wp_safe_redirect( add_query_arg( [ 'post_type' => 'shop_order' ], admin_url( 'edit.php' ) ) ) ) { | |
| 204 | - exit; | |
| 205 | - } | |
| 251 | + if ( count( $packetIds ) === 0 ) { | |
| 252 | + $this->messageManager->flash_message( __( 'No suitable orders were selected', 'packeta' ), 'info' ); | |
| 253 | + $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); | |
| 254 | + | |
| 206 | 255 | return; |
| 207 | 256 | } |
| 208 | 257 | |
| 209 | 258 | $maxOffset = $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ); |
| @@ -208,11 +257,11 @@ | ||
| 208 | 257 | |
| 209 | 258 | $maxOffset = $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ); |
| 210 | 259 | $form = $this->createForm( $maxOffset ); |
| 211 | 260 | $offsetParam = $this->httpRequest->getQuery( 'offset' ); |
| 212 | - if ( 0 === $maxOffset ) { | |
| 261 | + if ( $maxOffset === 0 ) { | |
| 213 | 262 | $offset = 0; |
| 214 | - } elseif ( null !== $offsetParam ) { | |
| 263 | + } elseif ( $offsetParam !== null ) { | |
| 215 | 264 | $offset = (int) $offsetParam; |
| 216 | 265 | } elseif ( $form->isSubmitted() ) { |
| 217 | 266 | $data = $form->getValues( 'array' ); |
| 218 | 267 | $offset = $data['offset']; |
| @@ -227,18 +276,25 @@ | ||
| 227 | 276 | } |
| 228 | 277 | } else { |
| 229 | 278 | $response = $this->requestPacketaLabels( $offset, $packetIds ); |
| 230 | 279 | } |
| 231 | - if ( ! $response || $response->hasFault() ) { | |
| 232 | - $message = ( null !== $response && $response->hasFault() ) ? | |
| 233 | - __( 'labelPrintFailedMoreInfoInLog', 'packetery' ) : | |
| 234 | - __( 'youSelectedOrdersThatWereNotSubmitted', 'packetery' ); | |
| 280 | + if ( $response->hasFault() ) { | |
| 281 | + $message = __( 'Label printing failed, you can find more information in the Packeta log.', 'packeta' ); | |
| 235 | 282 | $this->messageManager->flash_message( $message, MessageManager::TYPE_ERROR ); |
| 236 | - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) { | |
| 237 | - exit; | |
| 283 | + | |
| 284 | + $redirectTo = $this->httpRequest->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); | |
| 285 | + if ( $redirectTo === PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL && $idParam !== null ) { | |
| 286 | + $this->packetActionsCommonLogic->redirectTo( $redirectTo, $this->orderRepository->findById( $idParam ) ); | |
| 238 | 287 | } |
| 288 | + $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); | |
| 289 | + | |
| 290 | + return; | |
| 239 | 291 | } |
| 240 | 292 | |
| 293 | + foreach ( $packetIds as $orderId => $packetId ) { | |
| 294 | + $this->addLabelCreationInfoToWcOrderNote( $orderId, $packetId, $response ); | |
| 295 | + } | |
| 296 | + | |
| 241 | 297 | header( 'Content-Type: application/pdf' ); |
| 242 | 298 | header( 'Content-Transfer-Encoding: Binary' ); |
| 243 | 299 | header( 'Content-Length: ' . strlen( $response->getPdfContents() ) ); |
| 244 | 300 | header( 'Content-Disposition: attachment; filename="' . $this->getFilename() . '"' ); |
| @@ -250,23 +306,27 @@ | ||
| 250 | 306 | |
| 251 | 307 | /** |
| 252 | 308 | * Creates offset setting form. |
| 253 | 309 | * |
| 254 | - * @param int $maxOffset Maximal offset. | |
| 310 | + * @param int $maxOffset Maximal offset. | |
| 311 | + * @param string|null $name Form name. | |
| 255 | 312 | * |
| 256 | 313 | * @return Form |
| 257 | 314 | */ |
| 258 | - public function createForm( int $maxOffset ): Form { | |
| 259 | - $form = $this->formFactory->create(); | |
| 315 | + public function createForm( int $maxOffset, ?string $name = null ): Form { | |
| 316 | + $form = $this->formFactory->create( $name ); | |
| 260 | 317 | |
| 261 | 318 | $availableOffsets = []; |
| 262 | - for ( $i = 0; $i <= $maxOffset; $i ++ ) { | |
| 263 | - // translators: %s is offset. | |
| 264 | - $availableOffsets[ $i ] = ( 0 === $i ? __( 'dontSkipAnyField', 'packetery' ) : sprintf( __( 'skip%sFields', 'packetery' ), $i ) ); | |
| 319 | + for ( $i = 0; $i <= $maxOffset; $i++ ) { | |
| 320 | + $availableOffsets[ $i ] = ( $i === 0 ? | |
| 321 | + __( "don't skip any field on a print sheet", 'packeta' ) : | |
| 322 | + // translators: %s is offset. | |
| 323 | + sprintf( __( 'skip %s fields on first sheet', 'packeta' ), $i ) | |
| 324 | + ); | |
| 265 | 325 | } |
| 266 | 326 | $form->addSelect( |
| 267 | 327 | 'offset', |
| 268 | - __( 'labelsOffset', 'packetery' ), | |
| 328 | + __( 'Skip fields', 'packeta' ), | |
| 269 | 329 | $availableOffsets |
| 270 | 330 | )->checkDefaultValue( false ); |
| 271 | 331 | |
| 272 | 332 | return $form; |
| @@ -277,11 +337,11 @@ | ||
| 277 | 337 | */ |
| 278 | 338 | public function register(): void { |
| 279 | 339 | add_submenu_page( |
| 280 | 340 | \Packetery\Module\Options\Page::SLUG, |
| 281 | - __( 'printLabels', 'packetery' ), | |
| 282 | - __( 'printLabels', 'packetery' ), | |
| 283 | - 'manage_options', | |
| 341 | + __( 'Print labels', 'packeta' ), | |
| 342 | + __( 'Print labels', 'packeta' ), | |
| 343 | + 'manage_woocommerce', | |
| 284 | 344 | self::MENU_SLUG, |
| 285 | 345 | array( |
| 286 | 346 | $this, |
| 287 | 347 | 'render', |
| @@ -299,44 +359,53 @@ | ||
| 299 | 359 | |
| 300 | 360 | /** |
| 301 | 361 | * Prepares labels. |
| 302 | 362 | * |
| 303 | - * @param int $offset Offset value. | |
| 304 | - * @param array $packetIds Packet ids. | |
| 363 | + * @param int $offset Offset value. | |
| 364 | + * @param string[] $packetIds Packet ids. | |
| 305 | 365 | * |
| 306 | - * @return Response\PacketsLabelsPdf|null | |
| 366 | + * @return Response\PacketsLabelsPdf | |
| 307 | 367 | */ |
| 308 | - private function requestPacketaLabels( int $offset, array $packetIds ): ?Response\PacketsLabelsPdf { | |
| 368 | + private function requestPacketaLabels( int $offset, array $packetIds ): Response\PacketsLabelsPdf { | |
| 309 | 369 | $request = new Request\PacketsLabelsPdf( array_values( $packetIds ), $this->getLabelFormat(), $offset ); |
| 310 | 370 | $response = $this->soapApiClient->packetsLabelsPdf( $request ); |
| 311 | 371 | // TODO: is possible to merge following part of requestPacketaLabels and requestCarrierLabels? |
| 312 | - $record = new Log\Record(); | |
| 313 | - $record->action = Log\Record::ACTION_LABEL_PRINT; | |
| 314 | - if ( ! $response->hasFault() ) { | |
| 315 | - foreach ( array_keys( $packetIds ) as $orderId ) { | |
| 316 | - $order = $this->orderRepository->getById( $orderId ); | |
| 317 | - if ( null === $order ) { // Collection was already limited, so we do not need to log this. | |
| 318 | - continue; | |
| 372 | + | |
| 373 | + foreach ( $packetIds as $orderId => $packetId ) { | |
| 374 | + $record = new Log\Record(); | |
| 375 | + $record->action = Log\Record::ACTION_LABEL_PRINT; | |
| 376 | + $record->orderId = $orderId; | |
| 377 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 378 | + | |
| 379 | + if ( ! $response->hasFault() ) { | |
| 380 | + if ( $order !== null ) { | |
| 381 | + $order->setIsLabelPrinted( true ); | |
| 319 | 382 | } |
| 320 | - $order->setIsLabelPrinted( true ); | |
| 383 | + | |
| 384 | + $record->status = Log\Record::STATUS_SUCCESS; | |
| 385 | + $record->title = __( 'Label has been printed successfully.', 'packeta' ); | |
| 386 | + } else { | |
| 387 | + $record->status = Log\Record::STATUS_ERROR; | |
| 388 | + $record->title = __( 'Label could not be printed.', 'packeta' ); | |
| 389 | + $record->params = [ | |
| 390 | + 'packetId' => $packetId, | |
| 391 | + 'isPacketIdInvalid' => $response->hasInvalidPacketId( (string) $packetId ), | |
| 392 | + 'request' => [ | |
| 393 | + 'packetIds' => $request->getPacketIds(), | |
| 394 | + 'format' => $request->getFormat(), | |
| 395 | + 'offset' => $request->getOffset(), | |
| 396 | + ], | |
| 397 | + 'errorMessage' => $response->getFaultString(), | |
| 398 | + ]; | |
| 399 | + } | |
| 400 | + | |
| 401 | + $this->logger->add( $record ); | |
| 402 | + | |
| 403 | + if ( $order !== null ) { | |
| 404 | + $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 321 | 405 | $this->orderRepository->save( $order ); |
| 322 | 406 | } |
| 323 | - | |
| 324 | - $record->status = Log\Record::STATUS_SUCCESS; | |
| 325 | - $record->title = __( 'labelPrintSuccessLogTitle', 'packetery' ); | |
| 326 | - } else { | |
| 327 | - $record->status = Log\Record::STATUS_ERROR; | |
| 328 | - $record->title = __( 'labelPrintErrorLogTitle', 'packetery' ); | |
| 329 | - $record->params = [ | |
| 330 | - 'request' => [ | |
| 331 | - 'packetIds' => implode( ',', $request->getPacketIds() ), | |
| 332 | - 'format' => $request->getFormat(), | |
| 333 | - 'offset' => $request->getOffset(), | |
| 334 | - ], | |
| 335 | - 'errorMessage' => $response->getFaultString(), | |
| 336 | - ]; | |
| 337 | 407 | } |
| 338 | - $this->logger->add( $record ); | |
| 339 | 408 | |
| 340 | 409 | return $response; |
| 341 | 410 | } |
| 342 | 411 | |
| @@ -342,10 +411,10 @@ | ||
| 342 | 411 | |
| 343 | 412 | /** |
| 344 | 413 | * Prepares carrier labels. |
| 345 | 414 | * |
| 346 | - * @param int $offset Offset value. | |
| 347 | - * @param array $packetIds Packet ids. | |
| 415 | + * @param int $offset Offset value. | |
| 416 | + * @param string[] $packetIds Packet ids. | |
| 348 | 417 | * |
| 349 | 418 | * @return Response\PacketsCourierLabelsPdf |
| 350 | 419 | */ |
| 351 | 420 | private function requestCarrierLabels( int $offset, array $packetIds ): Response\PacketsCourierLabelsPdf { |
| @@ -351,36 +420,46 @@ | ||
| 351 | 420 | private function requestCarrierLabels( int $offset, array $packetIds ): Response\PacketsCourierLabelsPdf { |
| 352 | 421 | $packetIdsWithCourierNumbers = $this->getPacketIdsWithCourierNumbers( $packetIds ); |
| 353 | 422 | $request = new Request\PacketsCourierLabelsPdf( array_values( $packetIdsWithCourierNumbers ), $this->getLabelFormat(), $offset ); |
| 354 | 423 | $response = $this->soapApiClient->packetsCarrierLabelsPdf( $request ); |
| 355 | - $record = new Log\Record(); | |
| 356 | - $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; | |
| 357 | - if ( ! $response->hasFault() ) { | |
| 358 | - foreach ( array_keys( $packetIdsWithCourierNumbers ) as $orderId ) { | |
| 359 | - $order = $this->orderRepository->getById( $orderId ); | |
| 360 | - if ( null === $order ) { // Collection was already limited, so we do not need to log this. | |
| 361 | - continue; | |
| 424 | + | |
| 425 | + foreach ( $packetIdsWithCourierNumbers as $orderId => $pairItem ) { | |
| 426 | + $record = new Log\Record(); | |
| 427 | + $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; | |
| 428 | + $record->orderId = $orderId; | |
| 429 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 430 | + | |
| 431 | + if ( ! $response->hasFault() ) { | |
| 432 | + if ( $order !== null ) { | |
| 433 | + $order->setIsLabelPrinted( true ); | |
| 434 | + $order->setCarrierNumber( $pairItem['courierNumber'] ); | |
| 362 | 435 | } |
| 363 | - $order->setIsLabelPrinted( true ); | |
| 364 | - $order->setCarrierNumber( $packetIdsWithCourierNumbers[ $orderId ]['courierNumber'] ); | |
| 436 | + | |
| 437 | + $record->status = Log\Record::STATUS_SUCCESS; | |
| 438 | + $record->title = __( 'Carrier label has been printed successfully.', 'packeta' ); | |
| 439 | + } else { | |
| 440 | + $record->status = Log\Record::STATUS_ERROR; | |
| 441 | + $record->title = __( 'Carrier label could not be printed.', 'packeta' ); | |
| 442 | + $record->params = [ | |
| 443 | + 'packetId' => $pairItem['packetId'], | |
| 444 | + 'courierNumber' => $pairItem['courierNumber'], | |
| 445 | + 'isPacketIdInvalid' => $response->hasInvalidPacketId( (string) $pairItem['packetId'] ), | |
| 446 | + 'isCourierNumberInvalid' => $response->hasInvalidCourierNumber( (string) $pairItem['courierNumber'] ), | |
| 447 | + 'request' => [ | |
| 448 | + 'packetIdsWithCourierNumbers' => $request->getPacketIdsWithCourierNumbers(), | |
| 449 | + 'format' => $request->getFormat(), | |
| 450 | + 'offset' => $request->getOffset(), | |
| 451 | + ], | |
| 452 | + 'errorMessage' => $response->getFaultString(), | |
| 453 | + ]; | |
| 454 | + } | |
| 455 | + $this->logger->add( $record ); | |
| 456 | + | |
| 457 | + if ( $order !== null ) { | |
| 458 | + $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 365 | 459 | $this->orderRepository->save( $order ); |
| 366 | 460 | } |
| 367 | - | |
| 368 | - $record->status = Log\Record::STATUS_SUCCESS; | |
| 369 | - $record->title = __( 'carrierLabelPrintSuccessLogTitle', 'packetery' ); | |
| 370 | - } else { | |
| 371 | - $record->status = Log\Record::STATUS_ERROR; | |
| 372 | - $record->title = __( 'carrierLabelPrintErrorLogTitle', 'packetery' ); | |
| 373 | - $record->params = [ | |
| 374 | - 'request' => [ | |
| 375 | - 'packetIdsWithCourierNumbers' => $request->getPacketIdsWithCourierNumbers(), | |
| 376 | - 'format' => $request->getFormat(), | |
| 377 | - 'offset' => $request->getOffset(), | |
| 378 | - ], | |
| 379 | - 'errorMessage' => $response->getFaultString(), | |
| 380 | - ]; | |
| 381 | 461 | } |
| 382 | - $this->logger->add( $record ); | |
| 383 | 462 | |
| 384 | 463 | return $response; |
| 385 | 464 | } |
| 386 | 465 | |
| @@ -395,18 +474,18 @@ | ||
| 395 | 474 | |
| 396 | 475 | /** |
| 397 | 476 | * Gets saved packet ids. |
| 398 | 477 | * |
| 399 | - * @param bool $isCarrierLabels Are carrier labels requested?. | |
| 478 | + * @param string[] $orderIds Order IDs from transient. | |
| 479 | + * @param bool $isCarrierLabels Are carrier labels requested? | |
| 400 | 480 | * |
| 401 | 481 | * @return string[] |
| 402 | 482 | */ |
| 403 | - private function getPacketIdsFromTransient( bool $isCarrierLabels ): array { | |
| 404 | - $orderIds = get_transient( self::getOrderIdsTransientName() ); | |
| 483 | + private function getPacketIdsFromTransient( array $orderIds, bool $isCarrierLabels ): array { | |
| 405 | 484 | $orders = $this->orderRepository->getByIds( $orderIds ); |
| 406 | 485 | $packetIds = []; |
| 407 | 486 | foreach ( $orders as $order ) { |
| 408 | - if ( null === $order->getPacketId() ) { | |
| 487 | + if ( $order->getPacketId() === null ) { | |
| 409 | 488 | continue; |
| 410 | 489 | } |
| 411 | 490 | if ( ! $isCarrierLabels || $order->isExternalCarrier() ) { |
| 412 | 491 | $packetIds[ $order->getNumber() ] = $order->getPacketId(); |
| @@ -416,13 +495,27 @@ | ||
| 416 | 495 | return $packetIds; |
| 417 | 496 | } |
| 418 | 497 | |
| 419 | 498 | /** |
| 499 | + * Gets order IDs transient. | |
| 500 | + * | |
| 501 | + * @return string[]|null | |
| 502 | + */ | |
| 503 | + private function getOrderIdsTransient(): ?array { | |
| 504 | + $orderIds = get_transient( self::getOrderIdsTransientName() ); | |
| 505 | + if ( is_array( $orderIds ) ) { | |
| 506 | + return $orderIds; | |
| 507 | + } | |
| 508 | + | |
| 509 | + return null; | |
| 510 | + } | |
| 511 | + | |
| 512 | + /** | |
| 420 | 513 | * Gets carrier packet numbers from API. |
| 421 | 514 | * |
| 422 | 515 | * @param string[] $packetIds List of packet ids. |
| 423 | 516 | * |
| 424 | - * @return array[] | |
| 517 | + * @return array<int, array{packetId: string, courierNumber: string}> | |
| 425 | 518 | */ |
| 426 | 519 | private function getPacketIdsWithCourierNumbers( array $packetIds ): array { |
| 427 | 520 | $pairs = []; |
| 428 | 521 | foreach ( $packetIds as $orderId => $packetId ) { |
| @@ -429,22 +522,29 @@ | ||
| 429 | 522 | $request = new Request\PacketCourierNumber( $packetId ); |
| 430 | 523 | $response = $this->soapApiClient->packetCourierNumber( $request ); |
| 431 | 524 | if ( $response->hasFault() ) { |
| 432 | 525 | if ( $response->hasWrongPassword() ) { |
| 433 | - $this->messageManager->flash_message( __( 'pleaseSetProperPassword', 'packetery' ), MessageManager::TYPE_ERROR ); | |
| 526 | + $this->messageManager->flash_message( __( 'Please set a proper API password.', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 434 | 527 | |
| 435 | 528 | return []; |
| 436 | 529 | } |
| 437 | 530 | |
| 438 | - $record = new Log\Record(); | |
| 439 | - $record->action = Log\Record::ACTION_CARRIER_NUMBER_RETRIEVING; | |
| 440 | - $record->status = Log\Record::STATUS_ERROR; | |
| 441 | - $record->title = __( 'carrierNumberRetrievingErrorLogTitle', 'packetery' ); | |
| 442 | - $record->params = [ | |
| 531 | + $record = new Log\Record(); | |
| 532 | + $record->action = Log\Record::ACTION_CARRIER_NUMBER_RETRIEVING; | |
| 533 | + $record->status = Log\Record::STATUS_ERROR; | |
| 534 | + $record->title = __( 'Carrier number could not be retrieved.', 'packeta' ); | |
| 535 | + $record->params = [ | |
| 443 | 536 | 'packetId' => $request->getPacketId(), |
| 444 | 537 | 'errorMessage' => $response->getFaultString(), |
| 445 | 538 | ]; |
| 539 | + $record->orderId = $orderId; | |
| 446 | 540 | $this->logger->add( $record ); |
| 541 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 542 | + if ( $order !== null ) { | |
| 543 | + $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 544 | + $this->orderRepository->save( $order ); | |
| 545 | + } | |
| 546 | + | |
| 447 | 547 | continue; |
| 448 | 548 | } |
| 449 | 549 | $pairs[ $orderId ] = [ |
| 450 | 550 | 'packetId' => $packetId, |
| @@ -454,5 +554,47 @@ | ||
| 454 | 554 | |
| 455 | 555 | return $pairs; |
| 456 | 556 | } |
| 457 | 557 | |
| 558 | + /** | |
| 559 | + * Saves information about the creation of the label in the order note. | |
| 560 | + * | |
| 561 | + * @param int $orderId Order id. | |
| 562 | + * @param string $packetId Packet. | |
| 563 | + * @param ILabelResponse $response Response. | |
| 564 | + * | |
| 565 | + * @return void | |
| 566 | + */ | |
| 567 | + private function addLabelCreationInfoToWcOrderNote( int $orderId, string $packetId, ILabelResponse $response ): void { | |
| 568 | + $order = $this->orderRepository->findById( $orderId ); | |
| 569 | + $wcOrder = $this->orderRepository->getWcOrderById( $orderId ); | |
| 570 | + if ( $wcOrder === null || $order === null ) { | |
| 571 | + return; | |
| 572 | + } | |
| 573 | + | |
| 574 | + $linkText = $order->getPacketBarcode(); | |
| 575 | + $trackingUrl = $order->getPacketTrackingUrl(); | |
| 576 | + if ( $response instanceof Response\PacketsLabelsPdf ) { | |
| 577 | + if ( $order->isPacketClaim( $packetId ) ) { | |
| 578 | + // translators: %s represents a packet tracking link. | |
| 579 | + $message = __( 'Packeta: Label for packet claim %s has been created', 'packeta' ); | |
| 580 | + $linkText = $order->getPacketClaimBarcode(); | |
| 581 | + $trackingUrl = $order->getPacketClaimTrackingUrl(); | |
| 582 | + } else { | |
| 583 | + // translators: %s represents a packet tracking link. | |
| 584 | + $message = __( 'Packeta: Label for packet %s has been created', 'packeta' ); | |
| 585 | + } | |
| 586 | + } else { | |
| 587 | + // Response is of type Response\PacketsCourierLabelsPdf. | |
| 588 | + // translators: %s represents a packet tracking link. | |
| 589 | + $message = __( 'Packeta: Carrier label for packet %s has been created', 'packeta' ); | |
| 590 | + } | |
| 591 | + | |
| 592 | + $wcOrder->add_order_note( | |
| 593 | + sprintf( | |
| 594 | + $message, | |
| 595 | + $this->moduleHelper->createHtmlLink( $trackingUrl, $linkText ) | |
| 596 | + ) | |
| 597 | + ); | |
| 598 | + $wcOrder->save(); | |
| 599 | + } | |
| 458 | 600 | } |