| @@ -1,10 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Class LabelPrint. | |
| 4 | - * | |
| 5 | - * @package Packetery\Order | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | declare( strict_types=1 ); |
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| @@ -9,28 +4,26 @@ | ||
| 9 | 4 | |
| 10 | 5 | namespace Packetery\Module\Order; |
| 11 | 6 | |
| 12 | 7 | use Packetery\Core\Api\Soap\Client; |
| 8 | +use Packetery\Core\Api\Soap\ILabelResponse; | |
| 13 | 9 | use Packetery\Core\Api\Soap\Request; |
| 14 | 10 | use Packetery\Core\Api\Soap\Response; |
| 15 | 11 | use Packetery\Core\Log; |
| 16 | -use Packetery\Core\Entity\Order; | |
| 17 | -use Packetery\Module\Exception\InvalidCarrierException; | |
| 18 | -use Packetery\Module\FormFactory; | |
| 12 | +use Packetery\Latte\Engine; | |
| 13 | +use Packetery\Module\Dashboard\DashboardPage; | |
| 14 | +use Packetery\Module\Framework\WpAdapter; | |
| 15 | +use Packetery\Module\Labels\CarrierLabelService; | |
| 16 | +use Packetery\Module\Labels\LabelPrintParametersService; | |
| 19 | 17 | use Packetery\Module\MessageManager; |
| 20 | -use Packetery\Module\Options\Provider; | |
| 18 | +use Packetery\Module\ModuleHelper; | |
| 19 | +use Packetery\Module\Options\OptionsProvider; | |
| 21 | 20 | use Packetery\Module\Plugin; |
| 22 | -use Packetery\Latte\Engine; | |
| 23 | -use Packetery\Nette\Forms\Form; | |
| 21 | +use Packetery\Module\Transients; | |
| 24 | 22 | use Packetery\Nette\Http; |
| 25 | -use Packetery\Module; | |
| 26 | 23 | |
| 27 | -/** | |
| 28 | - * Class LabelPrint. | |
| 29 | - * | |
| 30 | - * @package Packetery\Order | |
| 31 | - */ | |
| 32 | 24 | class LabelPrint { |
| 25 | + | |
| 33 | 26 | public const ACTION_PACKETA_LABELS = 'print_packeta_labels'; |
| 34 | 27 | public const ACTION_CARRIER_LABELS = 'print_carrier_labels'; |
| 35 | 28 | public const LABEL_TYPE_PARAM = 'label_type'; |
| 36 | 29 | public const MENU_SLUG = 'label-print'; |
| @@ -35,113 +28,93 @@ | ||
| 35 | 28 | public const LABEL_TYPE_PARAM = 'label_type'; |
| 36 | 29 | public const MENU_SLUG = 'label-print'; |
| 37 | 30 | |
| 38 | 31 | /** |
| 39 | - * PacketeryLatte Engine. | |
| 40 | - * | |
| 41 | - * @var Engine PacketeryLatte engine. | |
| 32 | + * @var Engine | |
| 42 | 33 | */ |
| 43 | 34 | private $latteEngine; |
| 44 | 35 | |
| 45 | 36 | /** |
| 46 | - * Options Provider | |
| 47 | - * | |
| 48 | - * @var Provider | |
| 37 | + * @var OptionsProvider | |
| 49 | 38 | */ |
| 50 | 39 | private $optionsProvider; |
| 51 | 40 | |
| 52 | 41 | /** |
| 53 | - * Form factory. | |
| 54 | - * | |
| 55 | - * @var FormFactory Form factory. | |
| 56 | - */ | |
| 57 | - private $formFactory; | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Http Request. | |
| 61 | - * | |
| 62 | 42 | * @var Http\Request |
| 63 | 43 | */ |
| 64 | 44 | private $httpRequest; |
| 65 | 45 | |
| 66 | 46 | /** |
| 67 | - * SOAP API Client. | |
| 68 | - * | |
| 69 | - * @var Client SOAP API Client. | |
| 47 | + * @var Client | |
| 70 | 48 | */ |
| 71 | 49 | private $soapApiClient; |
| 72 | 50 | |
| 73 | 51 | /** |
| 74 | - * Message Manager. | |
| 75 | - * | |
| 76 | 52 | * @var MessageManager |
| 77 | 53 | */ |
| 78 | 54 | private $messageManager; |
| 79 | 55 | |
| 80 | 56 | /** |
| 81 | - * Logger. | |
| 82 | - * | |
| 83 | 57 | * @var Log\ILogger |
| 84 | 58 | */ |
| 85 | 59 | private $logger; |
| 86 | 60 | |
| 87 | 61 | /** |
| 88 | - * Order repository. | |
| 89 | - * | |
| 90 | 62 | * @var Repository |
| 91 | 63 | */ |
| 92 | 64 | private $orderRepository; |
| 93 | 65 | |
| 94 | 66 | /** |
| 95 | - * Log page. | |
| 96 | - * | |
| 97 | - * @var Module\Log\Page | |
| 67 | + * @var PacketActionsCommonLogic | |
| 98 | 68 | */ |
| 99 | - private $logPage; | |
| 69 | + private $packetActionsCommonLogic; | |
| 100 | 70 | |
| 101 | 71 | /** |
| 102 | - * Packet actions common logic. | |
| 103 | - * | |
| 104 | - * @var PacketActionsCommonLogic | |
| 72 | + * @var ModuleHelper | |
| 105 | 73 | */ |
| 106 | - private $packetActionsCommonLogic; | |
| 74 | + private $moduleHelper; | |
| 107 | 75 | |
| 108 | 76 | /** |
| 109 | - * LabelPrint constructor. | |
| 110 | - * | |
| 111 | - * @param Engine $latteEngine Latte Engine. | |
| 112 | - * @param Provider $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 Module\Log\Page $logPage Log page. | |
| 120 | - * @param PacketActionsCommonLogic $packetActionsCommonLogic Packet actions common logic. | |
| 77 | + * @var WpAdapter | |
| 121 | 78 | */ |
| 79 | + private $wpAdapter; | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * @var CarrierLabelService | |
| 83 | + */ | |
| 84 | + private $carrierLabelService; | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * @var LabelPrintParametersService | |
| 88 | + */ | |
| 89 | + private $labelPrintParametersService; | |
| 90 | + | |
| 122 | 91 | public function __construct( |
| 123 | 92 | Engine $latteEngine, |
| 124 | - Provider $optionsProvider, | |
| 125 | - FormFactory $formFactory, | |
| 93 | + OptionsProvider $optionsProvider, | |
| 126 | 94 | Http\Request $httpRequest, |
| 127 | 95 | Client $soapApiClient, |
| 128 | 96 | MessageManager $messageManager, |
| 129 | 97 | Log\ILogger $logger, |
| 130 | 98 | Repository $orderRepository, |
| 131 | - Module\Log\Page $logPage, | |
| 132 | - PacketActionsCommonLogic $packetActionsCommonLogic | |
| 99 | + PacketActionsCommonLogic $packetActionsCommonLogic, | |
| 100 | + ModuleHelper $moduleHelper, | |
| 101 | + WpAdapter $wpAdapter, | |
| 102 | + CarrierLabelService $carrierLabelService, | |
| 103 | + LabelPrintParametersService $labelPrintParametersService | |
| 133 | 104 | ) { |
| 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->logPage = $logPage; | |
| 143 | - $this->packetActionsCommonLogic = $packetActionsCommonLogic; | |
| 105 | + $this->latteEngine = $latteEngine; | |
| 106 | + $this->optionsProvider = $optionsProvider; | |
| 107 | + $this->httpRequest = $httpRequest; | |
| 108 | + $this->soapApiClient = $soapApiClient; | |
| 109 | + $this->messageManager = $messageManager; | |
| 110 | + $this->logger = $logger; | |
| 111 | + $this->orderRepository = $orderRepository; | |
| 112 | + $this->packetActionsCommonLogic = $packetActionsCommonLogic; | |
| 113 | + $this->moduleHelper = $moduleHelper; | |
| 114 | + $this->wpAdapter = $wpAdapter; | |
| 115 | + $this->carrierLabelService = $carrierLabelService; | |
| 116 | + $this->labelPrintParametersService = $labelPrintParametersService; | |
| 144 | 117 | } |
| 145 | 118 | |
| 146 | 119 | /** |
| 147 | 120 | * Generates id of transient to store order ids. |
| @@ -148,9 +121,9 @@ | ||
| 148 | 121 | * |
| 149 | 122 | * @return string |
| 150 | 123 | */ |
| 151 | 124 | public static function getOrderIdsTransientName(): string { |
| 152 | - return 'packetery_label_print_order_ids_' . wp_get_session_token(); | |
| 125 | + return Transients::LABEL_PRINT_ORDER_IDS_PREFIX . wp_get_session_token(); | |
| 153 | 126 | } |
| 154 | 127 | |
| 155 | 128 | /** |
| 156 | 129 | * Generates id of transient to store back link. |
| @@ -161,33 +134,18 @@ | ||
| 161 | 134 | return 'packetery_label_print_back_link_' . wp_get_session_token(); |
| 162 | 135 | } |
| 163 | 136 | |
| 164 | 137 | /** |
| 165 | - * Gets label type. | |
| 166 | - * | |
| 167 | - * @param Order $order Order. | |
| 168 | - * | |
| 169 | - * @return string | |
| 170 | - */ | |
| 171 | - public function getLabelFormatByOrder( Order $order ) { | |
| 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 | - /** | |
| 180 | 138 | * Prepares form and renders template. |
| 181 | 139 | */ |
| 182 | 140 | public function render(): void { |
| 183 | - $form = $this->createForm( $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ) ); | |
| 141 | + $form = $this->labelPrintParametersService->createForm( $this->optionsProvider->getLabelMaxOffset( $this->labelPrintParametersService->getLabelFormat() ) ); | |
| 184 | 142 | |
| 185 | 143 | $count = 0; |
| 186 | 144 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 187 | 145 | |
| 188 | 146 | $orderIdsTransient = $this->getOrderIdsTransient(); |
| 189 | - if ( $orderIdsTransient ) { | |
| 147 | + if ( $orderIdsTransient !== null ) { | |
| 190 | 148 | $orderIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); |
| 191 | 149 | $count = count( $orderIds ); |
| 192 | 150 | } else { |
| 193 | 151 | $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ); |
| @@ -212,23 +170,8 @@ | ||
| 212 | 170 | ] |
| 213 | 171 | ); |
| 214 | 172 | } |
| 215 | 173 | |
| 216 | - /** | |
| 217 | - * Gets label format for current job. | |
| 218 | - * | |
| 219 | - * @return string | |
| 220 | - */ | |
| 221 | - private function getLabelFormat(): string { | |
| 222 | - $packetaLabelFormat = $this->optionsProvider->get_packeta_label_format(); | |
| 223 | - $carrierLabelFormat = $this->optionsProvider->get_carrier_label_format(); | |
| 224 | - | |
| 225 | - return ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ? $carrierLabelFormat : $packetaLabelFormat ); | |
| 226 | - } | |
| 227 | - | |
| 228 | - /** | |
| 229 | - * Outputs pdf. | |
| 230 | - */ | |
| 231 | 174 | public function outputLabelsPdf(): void { |
| 232 | 175 | if ( $this->httpRequest->getQuery( 'page' ) !== self::MENU_SLUG ) { |
| 233 | 176 | return; |
| 234 | 177 | } |
| @@ -234,63 +177,81 @@ | ||
| 234 | 177 | } |
| 235 | 178 | |
| 236 | 179 | $fallbackToPacketaLabel = false; |
| 237 | 180 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 238 | - $idParam = $this->httpRequest->getQuery( 'id' ); | |
| 181 | + $orderIdParam = $this->httpRequest->getQuery( 'id' ) !== null ? (int) $this->httpRequest->getQuery( 'id' ) : null; | |
| 239 | 182 | $packetIdParam = $this->httpRequest->getQuery( 'packet_id' ); |
| 240 | - if ( null !== $idParam && null !== $packetIdParam ) { | |
| 183 | + if ( $orderIdParam !== null && $packetIdParam !== null ) { | |
| 241 | 184 | $fallbackToPacketaLabel = true; |
| 242 | - $packetIds = [ $idParam => $packetIdParam ]; | |
| 185 | + $packetIds = [ $orderIdParam => $packetIdParam ]; | |
| 243 | 186 | } else { |
| 244 | 187 | $orderIdsTransient = $this->getOrderIdsTransient(); |
| 245 | - if ( ! $orderIdsTransient ) { | |
| 188 | + if ( $orderIdsTransient === null ) { | |
| 246 | 189 | return; |
| 247 | 190 | } |
| 248 | 191 | |
| 249 | 192 | $packetIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); |
| 250 | 193 | } |
| 251 | - if ( ! $packetIds ) { | |
| 252 | - $this->messageManager->flash_message( __( 'No suitable orders were selected', 'packeta' ), 'info' ); | |
| 194 | + $packetIds = $this->labelPrintParametersService->removeExternalCarrierPacketIds( $packetIds, $isCarrierLabels, $fallbackToPacketaLabel ); | |
| 195 | + | |
| 196 | + if ( count( $packetIds ) === 0 ) { | |
| 197 | + if ( $isCarrierLabels === true ) { | |
| 198 | + $this->messageManager->flash_message( | |
| 199 | + $this->wpAdapter->__( 'No orders have been selected for Packeta carriers', 'packeta' ), | |
| 200 | + 'info' | |
| 201 | + ); | |
| 202 | + } else { | |
| 203 | + $this->messageManager->flash_message( | |
| 204 | + $this->wpAdapter->__( 'No orders have been selected for Packeta pick-up points', 'packeta' ), | |
| 205 | + 'info' | |
| 206 | + ); | |
| 207 | + } | |
| 208 | + | |
| 253 | 209 | $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); |
| 210 | + | |
| 254 | 211 | return; |
| 255 | 212 | } |
| 256 | 213 | |
| 257 | - $maxOffset = $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ); | |
| 258 | - $form = $this->createForm( $maxOffset ); | |
| 259 | - $offsetParam = $this->httpRequest->getQuery( 'offset' ); | |
| 260 | - if ( 0 === $maxOffset ) { | |
| 261 | - $offset = 0; | |
| 262 | - } elseif ( null !== $offsetParam ) { | |
| 263 | - $offset = (int) $offsetParam; | |
| 264 | - } elseif ( $form->isSubmitted() ) { | |
| 265 | - $data = $form->getValues( 'array' ); | |
| 266 | - $offset = $data['offset']; | |
| 267 | - } else { | |
| 214 | + $offset = $this->labelPrintParametersService->getOffset(); | |
| 215 | + if ( $offset === null ) { | |
| 268 | 216 | return; |
| 269 | 217 | } |
| 270 | 218 | |
| 271 | - if ( $isCarrierLabels ) { | |
| 272 | - $response = $this->requestCarrierLabels( $offset, $packetIds ); | |
| 273 | - if ( $fallbackToPacketaLabel && $response->hasFault() ) { | |
| 274 | - $response = $this->requestPacketaLabels( $offset, $packetIds ); | |
| 275 | - } | |
| 276 | - } else { | |
| 277 | - $response = $this->requestPacketaLabels( $offset, $packetIds ); | |
| 219 | + $response = $this->getResponse( $isCarrierLabels, $packetIds, $fallbackToPacketaLabel, $orderIdParam, $offset ); | |
| 220 | + if ( $response === null ) { | |
| 221 | + return; | |
| 278 | 222 | } |
| 279 | - if ( ! $response || $response->hasFault() ) { | |
| 280 | - $message = ( null !== $response && $response->hasFault() ) ? | |
| 281 | - __( 'Label printing failed, you can find more information in the Packeta log.', 'packeta' ) : | |
| 282 | - __( 'You selected orders that were not submitted yet', 'packeta' ); | |
| 283 | - $this->messageManager->flash_message( $message, MessageManager::TYPE_ERROR ); | |
| 284 | 223 | |
| 285 | - $redirectTo = $this->httpRequest->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); | |
| 286 | - if ( PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL === $redirectTo && null !== $idParam ) { | |
| 287 | - $this->packetActionsCommonLogic->redirectTo( $redirectTo, $this->orderRepository->getById( (int) $idParam, true ) ); | |
| 224 | + if ( $response->hasFault() ) { | |
| 225 | + if ( $fallbackToPacketaLabel === true && count( $packetIds ) === 1 ) { | |
| 226 | + $message = sprintf( | |
| 227 | + // translators: %s represents shipment tracking number | |
| 228 | + $this->wpAdapter->__( 'Label printing for shipment %s failed, you can find more information in the Packeta log.', 'packeta' ), | |
| 229 | + array_shift( $packetIds ) | |
| 230 | + ); | |
| 231 | + } elseif ( $isCarrierLabels === true ) { | |
| 232 | + $message = sprintf( | |
| 233 | + // translators: %s represents error message | |
| 234 | + $this->wpAdapter->__( 'Carrier label printing failed, you can find more information in the Packeta log. Error: %s', 'packeta' ), | |
| 235 | + $response->getFaultString() | |
| 236 | + ); | |
| 237 | + } else { | |
| 238 | + $message = sprintf( | |
| 239 | + // translators: %s represents error message | |
| 240 | + $this->wpAdapter->__( 'Label printing failed, you can find more information in the Packeta log. Error: %s', 'packeta' ), | |
| 241 | + $response->getFaultString() | |
| 242 | + ); | |
| 288 | 243 | } |
| 289 | - $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); | |
| 244 | + | |
| 245 | + $this->flashMessageAndRedirect( $message, $orderIdParam ); | |
| 246 | + | |
| 290 | 247 | return; |
| 291 | 248 | } |
| 292 | 249 | |
| 250 | + foreach ( $packetIds as $orderId => $packetId ) { | |
| 251 | + $this->addLabelCreationInfoToWcOrderNote( $orderId, $packetId, $response ); | |
| 252 | + } | |
| 253 | + | |
| 293 | 254 | header( 'Content-Type: application/pdf' ); |
| 294 | 255 | header( 'Content-Transfer-Encoding: Binary' ); |
| 295 | 256 | header( 'Content-Length: ' . strlen( $response->getPdfContents() ) ); |
| 296 | 257 | header( 'Content-Disposition: attachment; filename="' . $this->getFilename() . '"' ); |
| @@ -300,41 +261,13 @@ | ||
| 300 | 261 | exit; |
| 301 | 262 | } |
| 302 | 263 | |
| 303 | 264 | /** |
| 304 | - * Creates offset setting form. | |
| 305 | - * | |
| 306 | - * @param int $maxOffset Maximal offset. | |
| 307 | - * @param string|null $name Form name. | |
| 308 | - * | |
| 309 | - * @return Form | |
| 310 | - */ | |
| 311 | - public function createForm( int $maxOffset, string $name = null ): Form { | |
| 312 | - $form = $this->formFactory->create( $name ); | |
| 313 | - | |
| 314 | - $availableOffsets = []; | |
| 315 | - for ( $i = 0; $i <= $maxOffset; $i ++ ) { | |
| 316 | - $availableOffsets[ $i ] = ( 0 === $i ? | |
| 317 | - __( "don't skip any field on a print sheet", 'packeta' ) : | |
| 318 | - // translators: %s is offset. | |
| 319 | - sprintf( __( 'skip %s fields on first sheet', 'packeta' ), $i ) | |
| 320 | - ); | |
| 321 | - } | |
| 322 | - $form->addSelect( | |
| 323 | - 'offset', | |
| 324 | - __( 'Skip fields', 'packeta' ), | |
| 325 | - $availableOffsets | |
| 326 | - )->checkDefaultValue( false ); | |
| 327 | - | |
| 328 | - return $form; | |
| 329 | - } | |
| 330 | - | |
| 331 | - /** | |
| 332 | 265 | * Registers submenu item. |
| 333 | 266 | */ |
| 334 | 267 | public function register(): void { |
| 335 | 268 | add_submenu_page( |
| 336 | - \Packetery\Module\Options\Page::SLUG, | |
| 269 | + DashboardPage::SLUG, | |
| 337 | 270 | __( 'Print labels', 'packeta' ), |
| 338 | 271 | __( 'Print labels', 'packeta' ), |
| 339 | 272 | 'manage_woocommerce', |
| 340 | 273 | self::MENU_SLUG, |
| @@ -353,32 +286,23 @@ | ||
| 353 | 286 | Plugin::hideSubmenuItem( self::MENU_SLUG ); |
| 354 | 287 | } |
| 355 | 288 | |
| 356 | 289 | /** |
| 357 | - * Prepares labels. | |
| 358 | - * | |
| 359 | - * @param int $offset Offset value. | |
| 360 | - * @param array $packetIds Packet ids. | |
| 361 | - * | |
| 362 | - * @return Response\PacketsLabelsPdf|null | |
| 290 | + * @param int $offset | |
| 291 | + * @param string[] $packetIds | |
| 363 | 292 | */ |
| 364 | - private function requestPacketaLabels( int $offset, array $packetIds ): ?Response\PacketsLabelsPdf { | |
| 365 | - $request = new Request\PacketsLabelsPdf( array_values( $packetIds ), $this->getLabelFormat(), $offset ); | |
| 293 | + private function requestPacketaLabels( int $offset, array $packetIds ): Response\PacketsLabelsPdf { | |
| 294 | + $request = new Request\PacketsLabelsPdf( array_values( $packetIds ), $this->labelPrintParametersService->getLabelFormat(), $offset ); | |
| 366 | 295 | $response = $this->soapApiClient->packetsLabelsPdf( $request ); |
| 367 | - // TODO: is possible to merge following part of requestPacketaLabels and requestCarrierLabels? | |
| 368 | 296 | |
| 369 | 297 | foreach ( $packetIds as $orderId => $packetId ) { |
| 370 | 298 | $record = new Log\Record(); |
| 371 | 299 | $record->action = Log\Record::ACTION_LABEL_PRINT; |
| 372 | 300 | $record->orderId = $orderId; |
| 373 | - try { | |
| 374 | - $order = $this->orderRepository->getById( $orderId ); | |
| 375 | - } catch ( InvalidCarrierException $exception ) { | |
| 376 | - $order = null; | |
| 377 | - } | |
| 301 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 378 | 302 | |
| 379 | 303 | if ( ! $response->hasFault() ) { |
| 380 | - if ( null !== $order ) { | |
| 304 | + if ( $order !== null ) { | |
| 381 | 305 | $order->setIsLabelPrinted( true ); |
| 382 | 306 | } |
| 383 | 307 | |
| 384 | 308 | $record->status = Log\Record::STATUS_SUCCESS; |
| @@ -399,9 +323,9 @@ | ||
| 399 | 323 | } |
| 400 | 324 | |
| 401 | 325 | $this->logger->add( $record ); |
| 402 | 326 | |
| 403 | - if ( null !== $order ) { | |
| 327 | + if ( $order !== null ) { | |
| 404 | 328 | $order->updateApiErrorMessage( $response->getFaultString() ); |
| 405 | 329 | $this->orderRepository->save( $order ); |
| 406 | 330 | } |
| 407 | 331 | } |
| @@ -409,34 +333,24 @@ | ||
| 409 | 333 | return $response; |
| 410 | 334 | } |
| 411 | 335 | |
| 412 | 336 | /** |
| 413 | - * Prepares carrier labels. | |
| 414 | - * | |
| 415 | - * @param int $offset Offset value. | |
| 416 | - * @param array $packetIds Packet ids. | |
| 417 | - * | |
| 418 | - * @return Response\PacketsCourierLabelsPdf | |
| 337 | + * @param int $offset | |
| 338 | + * @param array[] $packetIdsWithCourierNumbers | |
| 419 | 339 | */ |
| 420 | - private function requestCarrierLabels( int $offset, array $packetIds ): Response\PacketsCourierLabelsPdf { | |
| 421 | - $packetIdsWithCourierNumbers = $this->getPacketIdsWithCourierNumbers( $packetIds ); | |
| 422 | - $request = new Request\PacketsCourierLabelsPdf( array_values( $packetIdsWithCourierNumbers ), $this->getLabelFormat(), $offset ); | |
| 423 | - $response = $this->soapApiClient->packetsCarrierLabelsPdf( $request ); | |
| 340 | + private function requestCarrierLabels( int $offset, array $packetIdsWithCourierNumbers ): Response\PacketsCourierLabelsPdf { | |
| 341 | + $request = new Request\PacketsCourierLabelsPdf( array_values( $packetIdsWithCourierNumbers ), $this->labelPrintParametersService->getLabelFormat(), $offset ); | |
| 342 | + $response = $this->soapApiClient->packetsCarrierLabelsPdf( $request ); | |
| 424 | 343 | |
| 425 | 344 | foreach ( $packetIdsWithCourierNumbers as $orderId => $pairItem ) { |
| 426 | 345 | $record = new Log\Record(); |
| 427 | 346 | $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; |
| 428 | 347 | $record->orderId = $orderId; |
| 429 | - try { | |
| 430 | - $order = $this->orderRepository->getById( $orderId ); | |
| 431 | - } catch ( InvalidCarrierException $exception ) { | |
| 432 | - $order = null; | |
| 433 | - } | |
| 348 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 434 | 349 | |
| 435 | 350 | if ( ! $response->hasFault() ) { |
| 436 | - if ( null !== $order ) { | |
| 351 | + if ( $order !== null ) { | |
| 437 | 352 | $order->setIsLabelPrinted( true ); |
| 438 | - $order->setCarrierNumber( $pairItem['courierNumber'] ); | |
| 439 | 353 | } |
| 440 | 354 | |
| 441 | 355 | $record->status = Log\Record::STATUS_SUCCESS; |
| 442 | 356 | $record->title = __( 'Carrier label has been printed successfully.', 'packeta' ); |
| @@ -457,9 +371,9 @@ | ||
| 457 | 371 | ]; |
| 458 | 372 | } |
| 459 | 373 | $this->logger->add( $record ); |
| 460 | 374 | |
| 461 | - if ( null !== $order ) { | |
| 375 | + if ( $order !== null ) { | |
| 462 | 376 | $order->updateApiErrorMessage( $response->getFaultString() ); |
| 463 | 377 | $this->orderRepository->save( $order ); |
| 464 | 378 | } |
| 465 | 379 | } |
| @@ -472,17 +386,13 @@ | ||
| 472 | 386 | * |
| 473 | 387 | * @return string |
| 474 | 388 | */ |
| 475 | 389 | private function getFilename(): string { |
| 476 | - return 'packeta_labels_' . strtolower( str_replace( ' ', '_', $this->getLabelFormat() ) ) . '.pdf'; | |
| 390 | + return 'packeta_labels_' . strtolower( str_replace( ' ', '_', $this->labelPrintParametersService->getLabelFormat() ) ) . '.pdf'; | |
| 477 | 391 | } |
| 478 | 392 | |
| 479 | 393 | /** |
| 480 | - * Gets saved packet ids. | |
| 481 | - * | |
| 482 | - * @param array $orderIds Order IDs from transient. | |
| 483 | - * @param bool $isCarrierLabels Are carrier labels requested?. | |
| 484 | - * | |
| 394 | + * @param string[] $orderIds Order IDs from transient. | |
| 485 | 395 | * @return string[] |
| 486 | 396 | */ |
| 487 | 397 | private function getPacketIdsFromTransient( array $orderIds, bool $isCarrierLabels ): array { |
| 488 | 398 | $orders = $this->orderRepository->getByIds( $orderIds ); |
| @@ -487,9 +397,9 @@ | ||
| 487 | 397 | private function getPacketIdsFromTransient( array $orderIds, bool $isCarrierLabels ): array { |
| 488 | 398 | $orders = $this->orderRepository->getByIds( $orderIds ); |
| 489 | 399 | $packetIds = []; |
| 490 | 400 | foreach ( $orders as $order ) { |
| 491 | - if ( null === $order->getPacketId() ) { | |
| 401 | + if ( $order->getPacketId() === null ) { | |
| 492 | 402 | continue; |
| 493 | 403 | } |
| 494 | 404 | if ( ! $isCarrierLabels || $order->isExternalCarrier() ) { |
| 495 | 405 | $packetIds[ $order->getNumber() ] = $order->getPacketId(); |
| @@ -499,11 +409,9 @@ | ||
| 499 | 409 | return $packetIds; |
| 500 | 410 | } |
| 501 | 411 | |
| 502 | 412 | /** |
| 503 | - * Gets order IDs transient. | |
| 504 | - * | |
| 505 | - * @return array|null | |
| 413 | + * @return string[]|null | |
| 506 | 414 | */ |
| 507 | 415 | private function getOrderIdsTransient(): ?array { |
| 508 | 416 | $orderIds = get_transient( self::getOrderIdsTransientName() ); |
| 509 | 417 | if ( is_array( $orderIds ) ) { |
| @@ -513,53 +421,88 @@ | ||
| 513 | 421 | return null; |
| 514 | 422 | } |
| 515 | 423 | |
| 516 | 424 | /** |
| 517 | - * Gets carrier packet numbers from API. | |
| 425 | + * Saves information about the creation of the label in the order note. | |
| 426 | + */ | |
| 427 | + private function addLabelCreationInfoToWcOrderNote( int $orderId, string $packetId, ?ILabelResponse $response ): void { | |
| 428 | + $order = $this->orderRepository->findById( $orderId ); | |
| 429 | + $wcOrder = $this->orderRepository->getWcOrderById( $orderId ); | |
| 430 | + if ( $wcOrder === null || $order === null ) { | |
| 431 | + return; | |
| 432 | + } | |
| 433 | + | |
| 434 | + $linkText = $order->getPacketBarcode(); | |
| 435 | + $trackingUrl = $order->getPacketTrackingUrl(); | |
| 436 | + if ( $response instanceof Response\PacketsLabelsPdf ) { | |
| 437 | + if ( $order->isPacketClaim( $packetId ) ) { | |
| 438 | + // translators: %s represents a packet tracking link. | |
| 439 | + $message = __( 'Packeta: Label for packet claim %s has been created', 'packeta' ); | |
| 440 | + $linkText = $order->getPacketClaimBarcode(); | |
| 441 | + $trackingUrl = $order->getPacketClaimTrackingUrl(); | |
| 442 | + } else { | |
| 443 | + // translators: %s represents a packet tracking link. | |
| 444 | + $message = __( 'Packeta: Label for packet %s has been created', 'packeta' ); | |
| 445 | + } | |
| 446 | + } else { | |
| 447 | + // Response is of type Response\PacketsCourierLabelsPdf or null. | |
| 448 | + // translators: %s represents a packet tracking link. | |
| 449 | + $message = __( 'Packeta: Carrier label for packet %s has been created', 'packeta' ); | |
| 450 | + } | |
| 451 | + | |
| 452 | + $wcOrder->add_order_note( | |
| 453 | + sprintf( | |
| 454 | + $message, | |
| 455 | + $this->moduleHelper->createHtmlLink( $trackingUrl, $linkText ) | |
| 456 | + ) | |
| 457 | + ); | |
| 458 | + $wcOrder->save(); | |
| 459 | + } | |
| 460 | + | |
| 461 | + public function flashMessageAndRedirect( string $message, ?int $orderId ): void { | |
| 462 | + $this->messageManager->flash_message( $message, MessageManager::TYPE_ERROR ); | |
| 463 | + | |
| 464 | + $redirectTo = $this->httpRequest->getQuery( PacketActionsCommonLogic::PARAM_REDIRECT_TO ); | |
| 465 | + if ( $redirectTo === PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL && $orderId !== null ) { | |
| 466 | + $this->packetActionsCommonLogic->redirectTo( $redirectTo, $this->orderRepository->findById( $orderId ) ); | |
| 467 | + } | |
| 468 | + $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); | |
| 469 | + } | |
| 470 | + | |
| 471 | + /** | |
| 472 | + * @param bool $isCarrierLabels | |
| 473 | + * @param array<string, string> $packetIds | |
| 474 | + * @param bool $fallbackToPacketaLabel | |
| 475 | + * @param int|null $orderId | |
| 476 | + * @param int $offset | |
| 518 | 477 | * |
| 519 | - * @param string[] $packetIds List of packet ids. | |
| 520 | - * | |
| 521 | - * @return array[] | |
| 478 | + * @return Response\PacketsCourierLabelsPdf|Response\PacketsLabelsPdf|null | |
| 522 | 479 | */ |
| 523 | - private function getPacketIdsWithCourierNumbers( array $packetIds ): array { | |
| 524 | - $pairs = []; | |
| 525 | - foreach ( $packetIds as $orderId => $packetId ) { | |
| 526 | - $request = new Request\PacketCourierNumber( $packetId ); | |
| 527 | - $response = $this->soapApiClient->packetCourierNumber( $request ); | |
| 528 | - if ( $response->hasFault() ) { | |
| 529 | - if ( $response->hasWrongPassword() ) { | |
| 530 | - $this->messageManager->flash_message( __( 'Please set a proper API password.', 'packeta' ), MessageManager::TYPE_ERROR ); | |
| 480 | + private function getResponse( | |
| 481 | + bool $isCarrierLabels, | |
| 482 | + array $packetIds, | |
| 483 | + bool $fallbackToPacketaLabel, | |
| 484 | + ?int $orderId, | |
| 485 | + int $offset | |
| 486 | + ) { | |
| 487 | + if ( $isCarrierLabels === false ) { | |
| 488 | + return $this->requestPacketaLabels( $offset, $packetIds ); | |
| 489 | + } | |
| 531 | 490 | |
| 532 | - return []; | |
| 533 | - } | |
| 491 | + $packetIdsWithCourierNumbers = $this->carrierLabelService->getPacketIdsWithCourierNumbers( $packetIds ); | |
| 492 | + if ( $fallbackToPacketaLabel === false && $packetIdsWithCourierNumbers === [] ) { | |
| 493 | + $this->flashMessageAndRedirect( | |
| 494 | + (string) $this->wpAdapter->__( 'Carrier label printing failed, you can find more information in the Packeta log.', 'packeta' ), | |
| 495 | + $orderId | |
| 496 | + ); | |
| 534 | 497 | |
| 535 | - $record = new Log\Record(); | |
| 536 | - $record->action = Log\Record::ACTION_CARRIER_NUMBER_RETRIEVING; | |
| 537 | - $record->status = Log\Record::STATUS_ERROR; | |
| 538 | - $record->title = __( 'Carrier number could not be retrieved.', 'packeta' ); | |
| 539 | - $record->params = [ | |
| 540 | - 'packetId' => $request->getPacketId(), | |
| 541 | - 'errorMessage' => $response->getFaultString(), | |
| 542 | - ]; | |
| 543 | - $record->orderId = $orderId; | |
| 544 | - $this->logger->add( $record ); | |
| 545 | - try { | |
| 546 | - $order = $this->orderRepository->getById( $orderId ); | |
| 547 | - } catch ( InvalidCarrierException $exception ) { | |
| 548 | - $order = null; | |
| 549 | - } | |
| 550 | - if ( null !== $order ) { | |
| 551 | - $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 552 | - $this->orderRepository->save( $order ); | |
| 553 | - } | |
| 554 | - continue; | |
| 555 | - } | |
| 556 | - $pairs[ $orderId ] = [ | |
| 557 | - 'packetId' => $packetId, | |
| 558 | - 'courierNumber' => $response->getNumber(), | |
| 559 | - ]; | |
| 498 | + return null; | |
| 560 | 499 | } |
| 561 | 500 | |
| 562 | - return $pairs; | |
| 501 | + $response = $this->requestCarrierLabels( $offset, $packetIdsWithCourierNumbers ); | |
| 502 | + if ( $fallbackToPacketaLabel === true && $response->hasFault() ) { | |
| 503 | + return $this->requestPacketaLabels( $offset, $packetIds ); | |
| 504 | + } | |
| 505 | + | |
| 506 | + return $response; | |
| 563 | 507 | } |
| 564 | - | |
| 565 | 508 | } |