| @@ -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,25 +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; | |
| 21 | +use Packetery\Module\Transients; | |
| 22 | +use Packetery\Nette\Http; | |
| 23 | 23 | |
| 24 | -/** | |
| 25 | - * Class LabelPrint. | |
| 26 | - * | |
| 27 | - * @package Packetery\Order | |
| 28 | - */ | |
| 29 | 24 | class LabelPrint { |
| 25 | + | |
| 30 | 26 | public const ACTION_PACKETA_LABELS = 'print_packeta_labels'; |
| 31 | 27 | public const ACTION_CARRIER_LABELS = 'print_carrier_labels'; |
| 32 | 28 | public const LABEL_TYPE_PARAM = 'label_type'; |
| 33 | 29 | public const MENU_SLUG = 'label-print'; |
| @@ -32,93 +28,93 @@ | ||
| 32 | 28 | public const LABEL_TYPE_PARAM = 'label_type'; |
| 33 | 29 | public const MENU_SLUG = 'label-print'; |
| 34 | 30 | |
| 35 | 31 | /** |
| 36 | - * PacketeryLatte Engine. | |
| 37 | - * | |
| 38 | - * @var Engine PacketeryLatte engine. | |
| 32 | + * @var Engine | |
| 39 | 33 | */ |
| 40 | 34 | private $latteEngine; |
| 41 | 35 | |
| 42 | 36 | /** |
| 43 | - * Options Provider | |
| 44 | - * | |
| 45 | - * @var Provider | |
| 37 | + * @var OptionsProvider | |
| 46 | 38 | */ |
| 47 | 39 | private $optionsProvider; |
| 48 | 40 | |
| 49 | 41 | /** |
| 50 | - * Form factory. | |
| 51 | - * | |
| 52 | - * @var FormFactory Form factory. | |
| 53 | - */ | |
| 54 | - private $formFactory; | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Http Request. | |
| 58 | - * | |
| 59 | 42 | * @var Http\Request |
| 60 | 43 | */ |
| 61 | 44 | private $httpRequest; |
| 62 | 45 | |
| 63 | 46 | /** |
| 64 | - * SOAP API Client. | |
| 65 | - * | |
| 66 | - * @var Client SOAP API Client. | |
| 47 | + * @var Client | |
| 67 | 48 | */ |
| 68 | 49 | private $soapApiClient; |
| 69 | 50 | |
| 70 | 51 | /** |
| 71 | - * Message Manager. | |
| 72 | - * | |
| 73 | 52 | * @var MessageManager |
| 74 | 53 | */ |
| 75 | 54 | private $messageManager; |
| 76 | 55 | |
| 77 | 56 | /** |
| 78 | - * Logger. | |
| 79 | - * | |
| 80 | 57 | * @var Log\ILogger |
| 81 | 58 | */ |
| 82 | 59 | private $logger; |
| 83 | 60 | |
| 84 | 61 | /** |
| 85 | - * Order repository. | |
| 86 | - * | |
| 87 | 62 | * @var Repository |
| 88 | 63 | */ |
| 89 | 64 | private $orderRepository; |
| 90 | 65 | |
| 91 | 66 | /** |
| 92 | - * LabelPrint constructor. | |
| 93 | - * | |
| 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. | |
| 67 | + * @var PacketActionsCommonLogic | |
| 102 | 68 | */ |
| 69 | + private $packetActionsCommonLogic; | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * @var ModuleHelper | |
| 73 | + */ | |
| 74 | + private $moduleHelper; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * @var WpAdapter | |
| 78 | + */ | |
| 79 | + private $wpAdapter; | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * @var CarrierLabelService | |
| 83 | + */ | |
| 84 | + private $carrierLabelService; | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * @var LabelPrintParametersService | |
| 88 | + */ | |
| 89 | + private $labelPrintParametersService; | |
| 90 | + | |
| 103 | 91 | public function __construct( |
| 104 | 92 | Engine $latteEngine, |
| 105 | - Provider $optionsProvider, | |
| 106 | - FormFactory $formFactory, | |
| 93 | + OptionsProvider $optionsProvider, | |
| 107 | 94 | Http\Request $httpRequest, |
| 108 | 95 | Client $soapApiClient, |
| 109 | 96 | MessageManager $messageManager, |
| 110 | 97 | Log\ILogger $logger, |
| 111 | - Repository $orderRepository | |
| 98 | + Repository $orderRepository, | |
| 99 | + PacketActionsCommonLogic $packetActionsCommonLogic, | |
| 100 | + ModuleHelper $moduleHelper, | |
| 101 | + WpAdapter $wpAdapter, | |
| 102 | + CarrierLabelService $carrierLabelService, | |
| 103 | + LabelPrintParametersService $labelPrintParametersService | |
| 112 | 104 | ) { |
| 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; | |
| 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; | |
| 121 | 117 | } |
| 122 | 118 | |
| 123 | 119 | /** |
| 124 | 120 | * Generates id of transient to store order ids. |
| @@ -125,9 +121,9 @@ | ||
| 125 | 121 | * |
| 126 | 122 | * @return string |
| 127 | 123 | */ |
| 128 | 124 | public static function getOrderIdsTransientName(): string { |
| 129 | - return 'packetery_label_print_order_ids_' . wp_get_session_token(); | |
| 125 | + return Transients::LABEL_PRINT_ORDER_IDS_PREFIX . wp_get_session_token(); | |
| 130 | 126 | } |
| 131 | 127 | |
| 132 | 128 | /** |
| 133 | 129 | * Generates id of transient to store back link. |
| @@ -134,9 +130,9 @@ | ||
| 134 | 130 | * |
| 135 | 131 | * @return string |
| 136 | 132 | */ |
| 137 | 133 | public static function getBackLinkTransientName(): string { |
| 138 | - return 'packetery_label_print_back_link_' . wp_get_session_token(); | |
| 134 | + return Transients::LABEL_PRINT_BACK_LINK_PREFIX . wp_get_session_token(); | |
| 139 | 135 | } |
| 140 | 136 | |
| 141 | 137 | /** |
| 142 | 138 | * Prepares form and renders template. |
| @@ -141,15 +137,19 @@ | ||
| 141 | 137 | /** |
| 142 | 138 | * Prepares form and renders template. |
| 143 | 139 | */ |
| 144 | 140 | public function render(): void { |
| 145 | - $form = $this->createForm( $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ) ); | |
| 141 | + $form = $this->labelPrintParametersService->createForm( $this->optionsProvider->getLabelMaxOffset( $this->labelPrintParametersService->getLabelFormat() ) ); | |
| 146 | 142 | |
| 147 | 143 | $count = 0; |
| 148 | 144 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 149 | - $orderIds = $this->getPacketIdsFromTransient( $isCarrierLabels ); | |
| 150 | - if ( $orderIds ) { | |
| 151 | - $count = count( $orderIds ); | |
| 145 | + | |
| 146 | + $orderIdsTransient = $this->getOrderIdsTransient(); | |
| 147 | + if ( $orderIdsTransient !== null ) { | |
| 148 | + $orderIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); | |
| 149 | + $count = count( $orderIds ); | |
| 150 | + } else { | |
| 151 | + $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ); | |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | $this->latteEngine->render( |
| 155 | 155 | PACKETERY_PLUGIN_DIR . '/template/order/label-print.latte', |
| @@ -157,27 +157,21 @@ | ||
| 157 | 157 | 'form' => $form, |
| 158 | 158 | 'count' => $count, |
| 159 | 159 | 'backLink' => get_transient( self::getBackLinkTransientName() ), |
| 160 | 160 | 'flashMessages' => $this->messageManager->renderToString( MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ), |
| 161 | + 'translations' => [ | |
| 162 | + 'packeta' => __( 'Packeta', 'packeta' ), | |
| 163 | + 'labelPrinting' => __( 'Print labels', 'packeta' ), | |
| 164 | + // translators: %s is count. | |
| 165 | + 'numberOfLabelsToPrint' => __( 'Number of labels to print: %s', 'packeta' ), | |
| 166 | + | |
| 167 | + 'back' => __( 'Back', 'packeta' ), | |
| 168 | + 'printLabels' => __( 'Print labels', 'packeta' ), | |
| 169 | + ], | |
| 161 | 170 | ] |
| 162 | 171 | ); |
| 163 | 172 | } |
| 164 | 173 | |
| 165 | - /** | |
| 166 | - * Gets label format for current job. | |
| 167 | - * | |
| 168 | - * @return string | |
| 169 | - */ | |
| 170 | - private function getLabelFormat(): string { | |
| 171 | - $packetaLabelFormat = ( $this->optionsProvider->get_packeta_label_format() ?? '' ); | |
| 172 | - $carrierLabelFormat = ( $this->optionsProvider->get_carrier_label_format() ?? '' ); | |
| 173 | - | |
| 174 | - return ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ? $carrierLabelFormat : $packetaLabelFormat ); | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * Outputs pdf. | |
| 179 | - */ | |
| 180 | 174 | public function outputLabelsPdf(): void { |
| 181 | 175 | if ( $this->httpRequest->getQuery( 'page' ) !== self::MENU_SLUG ) { |
| 182 | 176 | return; |
| 183 | 177 | } |
| @@ -183,60 +177,79 @@ | ||
| 183 | 177 | } |
| 184 | 178 | |
| 185 | 179 | $fallbackToPacketaLabel = false; |
| 186 | 180 | $isCarrierLabels = ( $this->httpRequest->getQuery( self::LABEL_TYPE_PARAM ) === self::ACTION_CARRIER_LABELS ); |
| 187 | - $idParam = $this->httpRequest->getQuery( 'id' ); | |
| 181 | + $orderIdParam = $this->httpRequest->getQuery( 'id' ) !== null ? (int) $this->httpRequest->getQuery( 'id' ) : null; | |
| 188 | 182 | $packetIdParam = $this->httpRequest->getQuery( 'packet_id' ); |
| 189 | - if ( null !== $idParam && null !== $packetIdParam ) { | |
| 183 | + if ( $orderIdParam !== null && $packetIdParam !== null ) { | |
| 190 | 184 | $fallbackToPacketaLabel = true; |
| 191 | - $packetIds = [ $idParam => $packetIdParam ]; | |
| 185 | + $packetIds = [ $orderIdParam => $packetIdParam ]; | |
| 192 | 186 | } else { |
| 193 | - if ( ! get_transient( self::getOrderIdsTransientName() ) ) { | |
| 194 | - $this->messageManager->flash_message( __( 'noOrdersSelected', 'packetery' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, self::MENU_SLUG ); | |
| 195 | - | |
| 187 | + $orderIdsTransient = $this->getOrderIdsTransient(); | |
| 188 | + if ( $orderIdsTransient === null ) { | |
| 196 | 189 | return; |
| 197 | 190 | } |
| 198 | 191 | |
| 199 | - $packetIds = $this->getPacketIdsFromTransient( $isCarrierLabels ); | |
| 192 | + $packetIds = $this->getPacketIdsFromTransient( $orderIdsTransient, $isCarrierLabels ); | |
| 200 | 193 | } |
| 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; | |
| 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 | + ); | |
| 205 | 207 | } |
| 208 | + | |
| 209 | + $this->packetActionsCommonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); | |
| 210 | + | |
| 206 | 211 | return; |
| 207 | 212 | } |
| 208 | 213 | |
| 209 | - $maxOffset = $this->optionsProvider->getLabelMaxOffset( $this->getLabelFormat() ); | |
| 210 | - $form = $this->createForm( $maxOffset ); | |
| 211 | - $offsetParam = $this->httpRequest->getQuery( 'offset' ); | |
| 212 | - if ( 0 === $maxOffset ) { | |
| 213 | - $offset = 0; | |
| 214 | - } elseif ( null !== $offsetParam ) { | |
| 215 | - $offset = (int) $offsetParam; | |
| 216 | - } elseif ( $form->isSubmitted() ) { | |
| 217 | - $data = $form->getValues( 'array' ); | |
| 218 | - $offset = $data['offset']; | |
| 219 | - } else { | |
| 214 | + $offset = $this->labelPrintParametersService->getOffset(); | |
| 215 | + if ( $offset === null ) { | |
| 220 | 216 | return; |
| 221 | 217 | } |
| 222 | 218 | |
| 223 | - if ( $isCarrierLabels ) { | |
| 224 | - $response = $this->requestCarrierLabels( $offset, $packetIds ); | |
| 225 | - if ( $fallbackToPacketaLabel && $response->hasFault() ) { | |
| 226 | - $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 | + ); | |
| 227 | 243 | } |
| 228 | - } else { | |
| 229 | - $response = $this->requestPacketaLabels( $offset, $packetIds ); | |
| 244 | + | |
| 245 | + $this->flashMessageAndRedirect( $message, $orderIdParam ); | |
| 246 | + | |
| 247 | + return; | |
| 230 | 248 | } |
| 231 | - if ( ! $response || $response->hasFault() ) { | |
| 232 | - $message = ( null !== $response && $response->hasFault() ) ? | |
| 233 | - __( 'labelPrintFailedMoreInfoInLog', 'packetery' ) : | |
| 234 | - __( 'youSelectedOrdersThatWereNotSubmitted', 'packetery' ); | |
| 235 | - $this->messageManager->flash_message( $message, MessageManager::TYPE_ERROR ); | |
| 236 | - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) { | |
| 237 | - exit; | |
| 238 | - } | |
| 249 | + | |
| 250 | + foreach ( $packetIds as $orderId => $packetId ) { | |
| 251 | + $this->addLabelCreationInfoToWcOrderNote( $orderId, $packetId, $response ); | |
| 239 | 252 | } |
| 240 | 253 | |
| 241 | 254 | header( 'Content-Type: application/pdf' ); |
| 242 | 255 | header( 'Content-Transfer-Encoding: Binary' ); |
| @@ -248,40 +261,16 @@ | ||
| 248 | 261 | exit; |
| 249 | 262 | } |
| 250 | 263 | |
| 251 | 264 | /** |
| 252 | - * Creates offset setting form. | |
| 253 | - * | |
| 254 | - * @param int $maxOffset Maximal offset. | |
| 255 | - * | |
| 256 | - * @return Form | |
| 257 | - */ | |
| 258 | - public function createForm( int $maxOffset ): Form { | |
| 259 | - $form = $this->formFactory->create(); | |
| 260 | - | |
| 261 | - $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 ) ); | |
| 265 | - } | |
| 266 | - $form->addSelect( | |
| 267 | - 'offset', | |
| 268 | - __( 'labelsOffset', 'packetery' ), | |
| 269 | - $availableOffsets | |
| 270 | - )->checkDefaultValue( false ); | |
| 271 | - | |
| 272 | - return $form; | |
| 273 | - } | |
| 274 | - | |
| 275 | - /** | |
| 276 | 265 | * Registers submenu item. |
| 277 | 266 | */ |
| 278 | 267 | public function register(): void { |
| 279 | 268 | add_submenu_page( |
| 280 | - \Packetery\Module\Options\Page::SLUG, | |
| 281 | - __( 'printLabels', 'packetery' ), | |
| 282 | - __( 'printLabels', 'packetery' ), | |
| 283 | - 'manage_options', | |
| 269 | + DashboardPage::SLUG, | |
| 270 | + __( 'Print labels', 'packeta' ), | |
| 271 | + __( 'Print labels', 'packeta' ), | |
| 272 | + 'manage_woocommerce', | |
| 284 | 273 | self::MENU_SLUG, |
| 285 | 274 | array( |
| 286 | 275 | $this, |
| 287 | 276 | 'render', |
| @@ -297,45 +286,69 @@ | ||
| 297 | 286 | Plugin::hideSubmenuItem( self::MENU_SLUG ); |
| 298 | 287 | } |
| 299 | 288 | |
| 300 | 289 | /** |
| 301 | - * Prepares labels. | |
| 302 | - * | |
| 303 | - * @param int $offset Offset value. | |
| 304 | - * @param array $packetIds Packet ids. | |
| 305 | - * | |
| 306 | - * @return Response\PacketsLabelsPdf|null | |
| 290 | + * @param int $offset | |
| 291 | + * @param string[] $packetIds | |
| 307 | 292 | */ |
| 308 | - private function requestPacketaLabels( int $offset, array $packetIds ): ?Response\PacketsLabelsPdf { | |
| 309 | - $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 ); | |
| 310 | 295 | $response = $this->soapApiClient->packetsLabelsPdf( $request ); |
| 311 | - // TODO: is possible to merge following part of requestPacketaLabels and requestCarrierLabels? | |
| 312 | - if ( ! $response->hasFault() ) { | |
| 313 | - foreach ( array_keys( $packetIds ) as $orderId ) { | |
| 314 | - $order = $this->orderRepository->getById( $orderId ); | |
| 315 | - $order->setIsLabelPrinted( true ); | |
| 316 | - $this->orderRepository->save( $order ); | |
| 296 | + | |
| 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; | |
| 317 | 307 | } |
| 318 | 308 | |
| 319 | - $record = new Log\Record(); | |
| 320 | - $record->action = Log\Record::ACTION_LABEL_PRINT; | |
| 321 | - $record->status = Log\Record::STATUS_SUCCESS; | |
| 322 | - $record->title = __( 'labelPrintSuccessLogTitle', 'packetery' ); | |
| 309 | + if ( $order->isPacketClaim( $packetId ) ) { | |
| 310 | + $isClaimAssistantLabel = true; | |
| 311 | + } | |
| 312 | + | |
| 313 | + $record = new Log\Record(); | |
| 314 | + $record->action = $isClaimAssistantLabel ? Log\Record::ACTION_CLAIM_LABEL_PRINT : Log\Record::ACTION_LABEL_PRINT; | |
| 315 | + $record->orderId = $orderId; | |
| 316 | + | |
| 317 | + if ( ! $response->hasFault() ) { | |
| 318 | + if ( $isClaimAssistantLabel === false ) { | |
| 319 | + $order->setIsLabelPrinted( true ); | |
| 320 | + } | |
| 321 | + | |
| 322 | + $record->status = Log\Record::STATUS_SUCCESS; | |
| 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 | + } | |
| 328 | + } else { | |
| 329 | + $record->status = Log\Record::STATUS_ERROR; | |
| 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 | + } | |
| 335 | + $record->params = [ | |
| 336 | + 'packetId' => $packetId, | |
| 337 | + 'isPacketIdInvalid' => $response->hasInvalidPacketId( (string) $packetId ), | |
| 338 | + 'request' => [ | |
| 339 | + 'packetIds' => $request->getPacketIds(), | |
| 340 | + 'format' => $request->getFormat(), | |
| 341 | + 'offset' => $request->getOffset(), | |
| 342 | + ], | |
| 343 | + 'errorMessage' => $response->getFaultString(), | |
| 344 | + ]; | |
| 345 | + } | |
| 346 | + | |
| 323 | 347 | $this->logger->add( $record ); |
| 324 | - } else { | |
| 325 | - $record = new Log\Record(); | |
| 326 | - $record->action = Log\Record::ACTION_LABEL_PRINT; | |
| 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 | - $this->logger->add( $record ); | |
| 348 | + | |
| 349 | + $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 350 | + $this->orderRepository->save( $order ); | |
| 338 | 351 | } |
| 339 | 352 | |
| 340 | 353 | return $response; |
| 341 | 354 | } |
| @@ -340,46 +353,50 @@ | ||
| 340 | 353 | return $response; |
| 341 | 354 | } |
| 342 | 355 | |
| 343 | 356 | /** |
| 344 | - * Prepares carrier labels. | |
| 345 | - * | |
| 346 | - * @param int $offset Offset value. | |
| 347 | - * @param array $packetIds Packet ids. | |
| 348 | - * | |
| 349 | - * @return Response\PacketsCourierLabelsPdf | |
| 357 | + * @param int $offset | |
| 358 | + * @param array[] $packetIdsWithCourierNumbers | |
| 350 | 359 | */ |
| 351 | - private function requestCarrierLabels( int $offset, array $packetIds ): Response\PacketsCourierLabelsPdf { | |
| 352 | - $packetIdsWithCourierNumbers = $this->getPacketIdsWithCourierNumbers( $packetIds ); | |
| 353 | - $request = new Request\PacketsCourierLabelsPdf( array_values( $packetIdsWithCourierNumbers ), $this->getLabelFormat(), $offset ); | |
| 354 | - $response = $this->soapApiClient->packetsCarrierLabelsPdf( $request ); | |
| 355 | - if ( ! $response->hasFault() ) { | |
| 356 | - foreach ( array_keys( $packetIdsWithCourierNumbers ) as $orderId ) { | |
| 357 | - $order = $this->orderRepository->getById( $orderId ); | |
| 358 | - $order->setIsLabelPrinted( true ); | |
| 359 | - $order->setCarrierNumber( $packetIdsWithCourierNumbers[ $orderId ]['courierNumber'] ); | |
| 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 ); | |
| 363 | + | |
| 364 | + foreach ( $packetIdsWithCourierNumbers as $orderId => $pairItem ) { | |
| 365 | + $record = new Log\Record(); | |
| 366 | + $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; | |
| 367 | + $record->orderId = $orderId; | |
| 368 | + $order = $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 369 | + | |
| 370 | + if ( ! $response->hasFault() ) { | |
| 371 | + if ( $order !== null ) { | |
| 372 | + $order->setIsLabelPrinted( true ); | |
| 373 | + } | |
| 374 | + | |
| 375 | + $record->status = Log\Record::STATUS_SUCCESS; | |
| 376 | + $record->title = __( 'Carrier label has been printed successfully.', 'packeta' ); | |
| 377 | + } else { | |
| 378 | + $record->status = Log\Record::STATUS_ERROR; | |
| 379 | + $record->title = __( 'Carrier label could not be printed.', 'packeta' ); | |
| 380 | + $record->params = [ | |
| 381 | + 'packetId' => $pairItem['packetId'], | |
| 382 | + 'courierNumber' => $pairItem['courierNumber'], | |
| 383 | + 'isPacketIdInvalid' => $response->hasInvalidPacketId( (string) $pairItem['packetId'] ), | |
| 384 | + 'isCourierNumberInvalid' => $response->hasInvalidCourierNumber( (string) $pairItem['courierNumber'] ), | |
| 385 | + 'request' => [ | |
| 386 | + 'packetIdsWithCourierNumbers' => $request->getPacketIdsWithCourierNumbers(), | |
| 387 | + 'format' => $request->getFormat(), | |
| 388 | + 'offset' => $request->getOffset(), | |
| 389 | + ], | |
| 390 | + 'errorMessage' => $response->getFaultString(), | |
| 391 | + ]; | |
| 392 | + } | |
| 393 | + $this->logger->add( $record ); | |
| 394 | + | |
| 395 | + if ( $order !== null ) { | |
| 396 | + $order->updateApiErrorMessage( $response->getFaultString() ); | |
| 360 | 397 | $this->orderRepository->save( $order ); |
| 361 | 398 | } |
| 362 | - | |
| 363 | - $record = new Log\Record(); | |
| 364 | - $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; | |
| 365 | - $record->status = Log\Record::STATUS_SUCCESS; | |
| 366 | - $record->title = __( 'carrierLabelPrintSuccessLogTitle', 'packetery' ); | |
| 367 | - $this->logger->add( $record ); | |
| 368 | - } else { | |
| 369 | - $record = new Log\Record(); | |
| 370 | - $record->action = Log\Record::ACTION_CARRIER_LABEL_PRINT; | |
| 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 | - $this->logger->add( $record ); | |
| 382 | 399 | } |
| 383 | 400 | |
| 384 | 401 | return $response; |
| 385 | 402 | } |
| @@ -389,24 +406,20 @@ | ||
| 389 | 406 | * |
| 390 | 407 | * @return string |
| 391 | 408 | */ |
| 392 | 409 | private function getFilename(): string { |
| 393 | - return 'packeta_labels_' . strtolower( str_replace( ' ', '_', $this->getLabelFormat() ) ) . '.pdf'; | |
| 410 | + return 'packeta_labels_' . strtolower( str_replace( ' ', '_', $this->labelPrintParametersService->getLabelFormat() ) ) . '.pdf'; | |
| 394 | 411 | } |
| 395 | 412 | |
| 396 | 413 | /** |
| 397 | - * Gets saved packet ids. | |
| 398 | - * | |
| 399 | - * @param bool $isCarrierLabels Are carrier labels requested?. | |
| 400 | - * | |
| 414 | + * @param string[] $orderIds Order IDs from transient. | |
| 401 | 415 | * @return string[] |
| 402 | 416 | */ |
| 403 | - private function getPacketIdsFromTransient( bool $isCarrierLabels ): array { | |
| 404 | - $orderIds = get_transient( self::getOrderIdsTransientName() ); | |
| 417 | + private function getPacketIdsFromTransient( array $orderIds, bool $isCarrierLabels ): array { | |
| 405 | 418 | $orders = $this->orderRepository->getByIds( $orderIds ); |
| 406 | 419 | $packetIds = []; |
| 407 | 420 | foreach ( $orders as $order ) { |
| 408 | - if ( null === $order->getPacketId() ) { | |
| 421 | + if ( $order->getPacketId() === null ) { | |
| 409 | 422 | continue; |
| 410 | 423 | } |
| 411 | 424 | if ( ! $isCarrierLabels || $order->isExternalCarrier() ) { |
| 412 | 425 | $packetIds[ $order->getNumber() ] = $order->getPacketId(); |
| @@ -416,43 +429,128 @@ | ||
| 416 | 429 | return $packetIds; |
| 417 | 430 | } |
| 418 | 431 | |
| 419 | 432 | /** |
| 420 | - * Gets carrier packet numbers from API. | |
| 421 | - * | |
| 422 | - * @param string[] $packetIds List of packet ids. | |
| 423 | - * | |
| 424 | - * @return array[] | |
| 433 | + * @return string[]|null | |
| 425 | 434 | */ |
| 426 | - private function getPacketIdsWithCourierNumbers( array $packetIds ): array { | |
| 427 | - $pairs = []; | |
| 428 | - foreach ( $packetIds as $orderId => $packetId ) { | |
| 429 | - $request = new Request\PacketCourierNumber( $packetId ); | |
| 430 | - $response = $this->soapApiClient->packetCourierNumber( $request ); | |
| 431 | - if ( $response->hasFault() ) { | |
| 432 | - if ( $response->hasWrongPassword() ) { | |
| 433 | - $this->messageManager->flash_message( __( 'pleaseSetProperPassword', 'packetery' ), MessageManager::TYPE_ERROR ); | |
| 435 | + private function getOrderIdsTransient(): ?array { | |
| 436 | + $orderIds = get_transient( self::getOrderIdsTransientName() ); | |
| 437 | + if ( is_array( $orderIds ) ) { | |
| 438 | + return $orderIds; | |
| 439 | + } | |
| 434 | 440 | |
| 435 | - return []; | |
| 436 | - } | |
| 441 | + return null; | |
| 442 | + } | |
| 437 | 443 | |
| 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 = [ | |
| 443 | - 'packetId' => $request->getPacketId(), | |
| 444 | - 'errorMessage' => $response->getFaultString(), | |
| 445 | - ]; | |
| 446 | - $this->logger->add( $record ); | |
| 447 | - continue; | |
| 444 | + /** | |
| 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' ); | |
| 448 | 465 | } |
| 449 | - $pairs[ $orderId ] = [ | |
| 450 | - 'packetId' => $packetId, | |
| 451 | - 'courierNumber' => $response->getNumber(), | |
| 452 | - ]; | |
| 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' ); | |
| 453 | 470 | } |
| 454 | 471 | |
| 455 | - return $pairs; | |
| 472 | + $wcOrder->add_order_note( | |
| 473 | + sprintf( | |
| 474 | + $message, | |
| 475 | + $this->moduleHelper->createHtmlLink( $trackingUrl, $linkText ) | |
| 476 | + ) | |
| 477 | + ); | |
| 478 | + $wcOrder->save(); | |
| 456 | 479 | } |
| 457 | 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 | |
| 525 | + * | |
| 526 | + * @return Response\PacketsCourierLabelsPdf|Response\PacketsLabelsPdf|null | |
| 527 | + */ | |
| 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 | + } | |
| 538 | + | |
| 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 | + ); | |
| 545 | + | |
| 546 | + return null; | |
| 547 | + } | |
| 548 | + | |
| 549 | + $response = $this->requestCarrierLabels( $offset, $packetIdsWithCourierNumbers ); | |
| 550 | + if ( $fallbackToPacketaLabel === true && $response->hasFault() ) { | |
| 551 | + return $this->requestPacketaLabels( $offset, $packetIds ); | |
| 552 | + } | |
| 553 | + | |
| 554 | + return $response; | |
| 555 | + } | |
| 458 | 556 | } |