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