| @@ -12,12 +12,15 @@ | ||
| 12 | 12 | use Packetery\Core\Api\Soap\Client; |
| 13 | 13 | use Packetery\Core\Api\Soap\Request; |
| 14 | 14 | use Packetery\Core\Api\Soap\Response; |
| 15 | 15 | use Packetery\Core\Entity\Order; |
| 16 | +use Packetery\Latte\Engine; | |
| 17 | +use Packetery\Module\Dashboard\DashboardPage; | |
| 16 | 18 | use Packetery\Module\EntityFactory; |
| 17 | 19 | use Packetery\Module\MessageManager; |
| 18 | 20 | use Packetery\Module\Plugin; |
| 19 | -use Packetery\Latte\Engine; | |
| 21 | +use Packetery\Module\Transients; | |
| 22 | +use Packetery\Module\Views\UrlBuilder; | |
| 20 | 23 | use Packetery\Nette\Http; |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * Class LabelPrint. |
| @@ -24,8 +27,9 @@ | ||
| 24 | 27 | * |
| 25 | 28 | * @package Packetery\Order |
| 26 | 29 | */ |
| 27 | 30 | class CollectionPrint { |
| 31 | + | |
| 28 | 32 | const ACTION_PRINT_ORDER_COLLECTION = 'packetery_print_order_collection'; |
| 29 | 33 | const PAGE_SLUG = 'packeta-order-collection-print'; |
| 30 | 34 | |
| 31 | 35 | /** |
| @@ -77,18 +81,12 @@ | ||
| 77 | 81 | */ |
| 78 | 82 | private $commonLogic; |
| 79 | 83 | |
| 80 | 84 | /** |
| 81 | - * LabelPrint constructor. | |
| 82 | - * | |
| 83 | - * @param Engine $latteEngine Latte Engine. | |
| 84 | - * @param Http\Request $httpRequest Http Request. | |
| 85 | - * @param Client $soapApiClient SOAP API Client. | |
| 86 | - * @param MessageManager $messageManager Message Manager. | |
| 87 | - * @param EntityFactory\Address $addressFactory Address factory. | |
| 88 | - * @param Repository $orderRepository Order repository. | |
| 89 | - * @param PacketActionsCommonLogic $packetActionsCommonLogic Common logic. | |
| 85 | + * @var UrlBuilder | |
| 90 | 86 | */ |
| 87 | + private $urlBuilder; | |
| 88 | + | |
| 91 | 89 | public function __construct( |
| 92 | 90 | Engine $latteEngine, |
| 93 | 91 | Http\Request $httpRequest, |
| 94 | 92 | Client $soapApiClient, |
| @@ -94,9 +92,10 @@ | ||
| 94 | 92 | Client $soapApiClient, |
| 95 | 93 | MessageManager $messageManager, |
| 96 | 94 | EntityFactory\Address $addressFactory, |
| 97 | 95 | Repository $orderRepository, |
| 98 | - PacketActionsCommonLogic $packetActionsCommonLogic | |
| 96 | + PacketActionsCommonLogic $packetActionsCommonLogic, | |
| 97 | + UrlBuilder $urlBuilder | |
| 99 | 98 | ) { |
| 100 | 99 | $this->latteEngine = $latteEngine; |
| 101 | 100 | $this->httpRequest = $httpRequest; |
| 102 | 101 | $this->soapApiClient = $soapApiClient; |
| @@ -103,8 +102,9 @@ | ||
| 103 | 102 | $this->messageManager = $messageManager; |
| 104 | 103 | $this->addressFactory = $addressFactory; |
| 105 | 104 | $this->orderRepository = $orderRepository; |
| 106 | 105 | $this->commonLogic = $packetActionsCommonLogic; |
| 106 | + $this->urlBuilder = $urlBuilder; | |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * Generates id of transient to store order ids. |
| @@ -111,9 +111,9 @@ | ||
| 111 | 111 | * |
| 112 | 112 | * @return string |
| 113 | 113 | */ |
| 114 | 114 | public static function getOrderIdsTransientName(): string { |
| 115 | - return 'packetery_order_collection_print_order_ids_' . wp_get_session_token(); | |
| 115 | + return Transients::ORDER_COLLECTION_PRINT_ORDER_IDS_PREFIX . wp_get_session_token(); | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * Prepares form and renders template. |
| @@ -122,9 +122,9 @@ | ||
| 122 | 122 | if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) { |
| 123 | 123 | return; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if ( ! get_transient( self::getOrderIdsTransientName() ) ) { | |
| 126 | + if ( get_transient( self::getOrderIdsTransientName() ) === false ) { | |
| 127 | 127 | $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' ); |
| 128 | 128 | $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); |
| 129 | 129 | } |
| 130 | 130 | |
| @@ -133,9 +133,9 @@ | ||
| 133 | 133 | $packetIds = []; |
| 134 | 134 | $wpOrders = []; |
| 135 | 135 | |
| 136 | 136 | foreach ( $orders as $order ) { |
| 137 | - if ( null === $order->getPacketId() ) { | |
| 137 | + if ( $order->getPacketId() === null ) { | |
| 138 | 138 | continue; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | $orderNumber = $order->getNumber(); |
| @@ -142,9 +142,9 @@ | ||
| 142 | 142 | $packetIds[ $orderNumber ] = $order->getPacketId(); |
| 143 | 143 | $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber ); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - if ( ! $packetIds ) { | |
| 146 | + if ( count( $packetIds ) === 0 ) { | |
| 147 | 147 | delete_transient( self::getOrderIdsTransientName() ); |
| 148 | 148 | $this->messageManager->flash_message( __( 'Selected orders were not yet submitted to Packeta.', 'packeta' ), 'info' ); |
| 149 | 149 | $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); |
| 150 | 150 | } |
| @@ -149,9 +149,9 @@ | ||
| 149 | 149 | $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID ); |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | $shipmentResult = $this->requestShipment( $packetIds ); |
| 153 | - if ( $shipmentResult->hasFault() && $shipmentResult->getInvalidPacketIds() ) { | |
| 153 | + if ( $shipmentResult->hasFault() && count( $shipmentResult->getInvalidPacketIds() ) > 0 ) { | |
| 154 | 154 | $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders ); |
| 155 | 155 | $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() ); |
| 156 | 156 | $shipmentResult = $this->requestShipment( $packetIds ); |
| 157 | 157 | } |
| @@ -181,9 +181,9 @@ | ||
| 181 | 181 | 'packetIds' => $packetIds, |
| 182 | 182 | 'wpOrders' => $wpOrders, |
| 183 | 183 | 'orderCount' => count( $packetIds ), |
| 184 | 184 | 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ), |
| 185 | - 'stylesheet' => Plugin::buildAssetUrl( 'public/order-collection-print.css' ), | |
| 185 | + 'stylesheet' => $this->urlBuilder->buildAssetUrl( 'public/css/order-collection-print.css' ), | |
| 186 | 186 | 'translations' => [ |
| 187 | 187 | 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ), |
| 188 | 188 | 'packetCount' => __( 'Packet count', 'packeta' ), |
| 189 | 189 | 'printedAt' => __( 'Printed at', 'packeta' ), |
| @@ -206,9 +206,9 @@ | ||
| 206 | 206 | * Registers submenu item. |
| 207 | 207 | */ |
| 208 | 208 | public function register(): void { |
| 209 | 209 | add_submenu_page( |
| 210 | - \Packetery\Module\Options\Page::SLUG, | |
| 210 | + DashboardPage::SLUG, | |
| 211 | 211 | __( 'Print AWB', 'packeta' ), |
| 212 | 212 | __( 'Print AWB', 'packeta' ), |
| 213 | 213 | 'manage_woocommerce', |
| 214 | 214 | self::PAGE_SLUG, |
| @@ -229,14 +229,15 @@ | ||
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | 231 | * Request shipment. |
| 232 | 232 | * |
| 233 | - * @param array $packetIds Packet ids. | |
| 233 | + * @param string[] $packetIds Packet ids. | |
| 234 | 234 | * |
| 235 | 235 | * @return Response\CreateShipment |
| 236 | 236 | */ |
| 237 | 237 | private function requestShipment( array $packetIds ): Response\CreateShipment { |
| 238 | 238 | $request = new Request\CreateShipment( array_values( $packetIds ) ); |
| 239 | + | |
| 239 | 240 | return $this->soapApiClient->createShipment( $request ); |
| 240 | 241 | } |
| 241 | 242 | |
| 242 | 243 | /** |
| @@ -247,8 +248,9 @@ | ||
| 247 | 248 | * @return Response\BarcodePng |
| 248 | 249 | */ |
| 249 | 250 | private function requestBarcodePng( string $barcode ): Response\BarcodePng { |
| 250 | 251 | $request = new Request\BarcodePng( $barcode ); |
| 252 | + | |
| 251 | 253 | return $this->soapApiClient->barcodePng( $request ); |
| 252 | 254 | } |
| 253 | 255 | |
| 254 | 256 | /** |