PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Order/CollectionPrint.php +36 -32 1.5.02.1 View file →
@@ -12,13 +12,16 @@
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 PacketeryLatte\Engine;
20 -use PacketeryNette\Http;
21 +use Packetery\Module\Transients;
22 +use Packetery\Module\Views\UrlBuilder;
23 +use Packetery\Nette\Http;
21 24
22 25 /**
23 26 * Class LabelPrint.
24 27 *
@@ -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 /**
@@ -70,17 +74,19 @@
70 74 */
71 75 private $addressFactory;
72 76
73 77 /**
74 - * LabelPrint constructor.
78 + * Packet actions common logic.
75 79 *
76 - * @param Engine $latteEngine Latte Engine.
77 - * @param Http\Request $httpRequest Http Request.
78 - * @param Client $soapApiClient SOAP API Client.
79 - * @param MessageManager $messageManager Message Manager.
80 - * @param EntityFactory\Address $addressFactory Address factory.
81 - * @param Repository $orderRepository Order repository.
80 + * @var PacketActionsCommonLogic
82 81 */
82 + private $commonLogic;
83 +
84 + /**
85 + * @var UrlBuilder
86 + */
87 + private $urlBuilder;
88 +
83 89 public function __construct(
84 90 Engine $latteEngine,
85 91 Http\Request $httpRequest,
86 92 Client $soapApiClient,
@@ -85,9 +91,11 @@
85 91 Http\Request $httpRequest,
86 92 Client $soapApiClient,
87 93 MessageManager $messageManager,
88 94 EntityFactory\Address $addressFactory,
89 - Repository $orderRepository
95 + Repository $orderRepository,
96 + PacketActionsCommonLogic $packetActionsCommonLogic,
97 + UrlBuilder $urlBuilder
90 98 ) {
91 99 $this->latteEngine = $latteEngine;
92 100 $this->httpRequest = $httpRequest;
93 101 $this->soapApiClient = $soapApiClient;
@@ -93,8 +101,10 @@
93 101 $this->soapApiClient = $soapApiClient;
94 102 $this->messageManager = $messageManager;
95 103 $this->addressFactory = $addressFactory;
96 104 $this->orderRepository = $orderRepository;
105 + $this->commonLogic = $packetActionsCommonLogic;
106 + $this->urlBuilder = $urlBuilder;
97 107 }
98 108
99 109 /**
100 110 * Generates id of transient to store order ids.
@@ -101,9 +111,9 @@
101 111 *
102 112 * @return string
103 113 */
104 114 public static function getOrderIdsTransientName(): string {
105 - return 'packetery_order_collection_print_order_ids_' . wp_get_session_token();
115 + return Transients::ORDER_COLLECTION_PRINT_ORDER_IDS_PREFIX . wp_get_session_token();
106 116 }
107 117
108 118 /**
109 119 * Prepares form and renders template.
@@ -112,13 +122,11 @@
112 122 if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) {
113 123 return;
114 124 }
115 125
116 - if ( ! get_transient( self::getOrderIdsTransientName() ) ) {
126 + if ( get_transient( self::getOrderIdsTransientName() ) === false ) {
117 127 $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' );
118 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
119 - exit;
120 - }
128 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
121 129 }
122 130
123 131 $orderIds = get_transient( self::getOrderIdsTransientName() );
124 132 $orders = $this->orderRepository->getByIds( $orderIds );
@@ -125,9 +133,9 @@
125 133 $packetIds = [];
126 134 $wpOrders = [];
127 135
128 136 foreach ( $orders as $order ) {
129 - if ( null === $order->getPacketId() ) {
137 + if ( $order->getPacketId() === null ) {
130 138 continue;
131 139 }
132 140
133 141 $orderNumber = $order->getNumber();
@@ -134,18 +142,16 @@
134 142 $packetIds[ $orderNumber ] = $order->getPacketId();
135 143 $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber );
136 144 }
137 145
138 - if ( ! $packetIds ) {
146 + if ( count( $packetIds ) === 0 ) {
139 147 delete_transient( self::getOrderIdsTransientName() );
140 148 $this->messageManager->flash_message( __( 'Selected orders were not yet submitted to Packeta.', 'packeta' ), 'info' );
141 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
142 - exit;
143 - }
149 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
144 150 }
145 151
146 152 $shipmentResult = $this->requestShipment( $packetIds );
147 - if ( $shipmentResult->hasFault() && $shipmentResult->getInvalidPacketIds() ) {
153 + if ( $shipmentResult->hasFault() && count( $shipmentResult->getInvalidPacketIds() ) > 0 ) {
148 154 $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders );
149 155 $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() );
150 156 $shipmentResult = $this->requestShipment( $packetIds );
151 157 }
@@ -152,11 +158,9 @@
152 158
153 159 if ( $shipmentResult->hasFault() ) {
154 160 delete_transient( self::getOrderIdsTransientName() );
155 161 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
156 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
157 - exit;
158 - }
162 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
159 163 }
160 164
161 165 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
162 166 delete_transient( self::getOrderIdsTransientName() );
@@ -161,11 +165,9 @@
161 165 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
162 166 delete_transient( self::getOrderIdsTransientName() );
163 167 if ( $shipmentBarcodeResult->hasFault() ) {
164 168 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
165 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
166 - exit;
167 - }
169 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
168 170 }
169 171
170 172 $storeAddress = $this->addressFactory->fromWcStoreOptions();
171 173 $this->latteEngine->render(
@@ -179,9 +181,9 @@
179 181 'packetIds' => $packetIds,
180 182 'wpOrders' => $wpOrders,
181 183 'orderCount' => count( $packetIds ),
182 184 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ),
183 - 'stylesheet' => Plugin::buildAssetUrl( 'public/order-collection-print.css' ),
185 + 'stylesheet' => $this->urlBuilder->buildAssetUrl( 'public/css/order-collection-print.css' ),
184 186 'translations' => [
185 187 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ),
186 188 'packetCount' => __( 'Packet count', 'packeta' ),
187 189 'printedAt' => __( 'Printed at', 'packeta' ),
@@ -204,12 +206,12 @@
204 206 * Registers submenu item.
205 207 */
206 208 public function register(): void {
207 209 add_submenu_page(
208 - \Packetery\Module\Options\Page::SLUG,
210 + DashboardPage::SLUG,
209 211 __( 'Print AWB', 'packeta' ),
210 212 __( 'Print AWB', 'packeta' ),
211 - 'manage_options',
213 + 'manage_woocommerce',
212 214 self::PAGE_SLUG,
213 215 [
214 216 $this,
215 217 'print',
@@ -227,14 +229,15 @@
227 229
228 230 /**
229 231 * Request shipment.
230 232 *
231 - * @param array $packetIds Packet ids.
233 + * @param string[] $packetIds Packet ids.
232 234 *
233 235 * @return Response\CreateShipment
234 236 */
235 237 private function requestShipment( array $packetIds ): Response\CreateShipment {
236 238 $request = new Request\CreateShipment( array_values( $packetIds ) );
239 +
237 240 return $this->soapApiClient->createShipment( $request );
238 241 }
239 242
240 243 /**
@@ -245,8 +248,9 @@
245 248 * @return Response\BarcodePng
246 249 */
247 250 private function requestBarcodePng( string $barcode ): Response\BarcodePng {
248 251 $request = new Request\BarcodePng( $barcode );
252 +
249 253 return $this->soapApiClient->barcodePng( $request );
250 254 }
251 255
252 256 /**
@@ -259,9 +263,9 @@
259 263 */
260 264 private function logApiErrorMessageFromCreateShipmentResponse( Response\CreateShipment $createShipmentResponse, array $orders ): void {
261 265 $invalidPacketIds = $createShipmentResponse->getInvalidPacketIds();
262 266 foreach ( $orders as $order ) {
263 - if ( in_array( (int) $order->getPacketId(), $invalidPacketIds, true ) ) {
267 + if ( in_array( $order->getPacketId(), $invalidPacketIds, true ) ) {
264 268 $order->updateApiErrorMessage( $createShipmentResponse->getFaultString() );
265 269 $this->orderRepository->save( $order );
266 270 }
267 271 }