PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
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 +55 -41 1.5.22.3.2 View file →
@@ -12,13 +12,17 @@
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;
20 +use Packetery\Module\Options\OptionsProvider;
18 21 use Packetery\Module\Plugin;
19 -use PacketeryLatte\Engine;
20 -use PacketeryNette\Http;
22 +use Packetery\Module\Transients;
23 +use Packetery\Module\Views\UrlBuilder;
24 +use Packetery\Nette\Http;
21 25
22 26 /**
23 27 * Class LabelPrint.
24 28 *
@@ -24,8 +28,9 @@
24 28 *
25 29 * @package Packetery\Order
26 30 */
27 31 class CollectionPrint {
32 +
28 33 const ACTION_PRINT_ORDER_COLLECTION = 'packetery_print_order_collection';
29 34 const PAGE_SLUG = 'packeta-order-collection-print';
30 35
31 36 /**
@@ -70,17 +75,24 @@
70 75 */
71 76 private $addressFactory;
72 77
73 78 /**
74 - * LabelPrint constructor.
79 + * Packet actions common logic.
75 80 *
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.
81 + * @var PacketActionsCommonLogic
82 82 */
83 + private $commonLogic;
84 +
85 + /**
86 + * @var UrlBuilder
87 + */
88 + private $urlBuilder;
89 +
90 + /**
91 + * @var OptionsProvider
92 + */
93 + private $optionsProvider;
94 +
83 95 public function __construct(
84 96 Engine $latteEngine,
85 97 Http\Request $httpRequest,
86 98 Client $soapApiClient,
@@ -85,9 +97,12 @@
85 97 Http\Request $httpRequest,
86 98 Client $soapApiClient,
87 99 MessageManager $messageManager,
88 100 EntityFactory\Address $addressFactory,
89 - Repository $orderRepository
101 + Repository $orderRepository,
102 + PacketActionsCommonLogic $packetActionsCommonLogic,
103 + UrlBuilder $urlBuilder,
104 + OptionsProvider $optionsProvider
90 105 ) {
91 106 $this->latteEngine = $latteEngine;
92 107 $this->httpRequest = $httpRequest;
93 108 $this->soapApiClient = $soapApiClient;
@@ -93,8 +108,11 @@
93 108 $this->soapApiClient = $soapApiClient;
94 109 $this->messageManager = $messageManager;
95 110 $this->addressFactory = $addressFactory;
96 111 $this->orderRepository = $orderRepository;
112 + $this->commonLogic = $packetActionsCommonLogic;
113 + $this->urlBuilder = $urlBuilder;
114 + $this->optionsProvider = $optionsProvider;
97 115 }
98 116
99 117 /**
100 118 * Generates id of transient to store order ids.
@@ -101,9 +119,9 @@
101 119 *
102 120 * @return string
103 121 */
104 122 public static function getOrderIdsTransientName(): string {
105 - return 'packetery_order_collection_print_order_ids_' . wp_get_session_token();
123 + return Transients::ORDER_COLLECTION_PRINT_ORDER_IDS_PREFIX . wp_get_session_token();
106 124 }
107 125
108 126 /**
109 127 * Prepares form and renders template.
@@ -112,13 +130,11 @@
112 130 if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) {
113 131 return;
114 132 }
115 133
116 - if ( ! get_transient( self::getOrderIdsTransientName() ) ) {
134 + if ( get_transient( self::getOrderIdsTransientName() ) === false ) {
117 135 $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' );
118 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
119 - exit;
120 - }
136 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
121 137 }
122 138
123 139 $orderIds = get_transient( self::getOrderIdsTransientName() );
124 140 $orders = $this->orderRepository->getByIds( $orderIds );
@@ -125,9 +141,9 @@
125 141 $packetIds = [];
126 142 $wpOrders = [];
127 143
128 144 foreach ( $orders as $order ) {
129 - if ( null === $order->getPacketId() ) {
145 + if ( $order->getPacketId() === null ) {
130 146 continue;
131 147 }
132 148
133 149 $orderNumber = $order->getNumber();
@@ -134,18 +150,16 @@
134 150 $packetIds[ $orderNumber ] = $order->getPacketId();
135 151 $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber );
136 152 }
137 153
138 - if ( ! $packetIds ) {
154 + if ( count( $packetIds ) === 0 ) {
139 155 delete_transient( self::getOrderIdsTransientName() );
140 156 $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 - }
157 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
144 158 }
145 159
146 160 $shipmentResult = $this->requestShipment( $packetIds );
147 - if ( $shipmentResult->hasFault() && $shipmentResult->getInvalidPacketIds() ) {
161 + if ( $shipmentResult->hasFault() && count( $shipmentResult->getInvalidPacketIds() ) > 0 ) {
148 162 $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders );
149 163 $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() );
150 164 $shipmentResult = $this->requestShipment( $packetIds );
151 165 }
@@ -152,11 +166,9 @@
152 166
153 167 if ( $shipmentResult->hasFault() ) {
154 168 delete_transient( self::getOrderIdsTransientName() );
155 169 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
156 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
157 - exit;
158 - }
170 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
159 171 }
160 172
161 173 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
162 174 delete_transient( self::getOrderIdsTransientName() );
@@ -161,11 +173,9 @@
161 173 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
162 174 delete_transient( self::getOrderIdsTransientName() );
163 175 if ( $shipmentBarcodeResult->hasFault() ) {
164 176 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
165 - if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
166 - exit;
167 - }
177 + $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
168 178 }
169 179
170 180 $storeAddress = $this->addressFactory->fromWcStoreOptions();
171 181 $this->latteEngine->render(
@@ -170,19 +180,20 @@
170 180 $storeAddress = $this->addressFactory->fromWcStoreOptions();
171 181 $this->latteEngine->render(
172 182 PACKETERY_PLUGIN_DIR . '/template/order/collection-print.latte',
173 183 [
174 - 'storeAddress' => $storeAddress,
175 - 'storeName' => get_option( 'blogname', '' ),
176 - 'shipmentBarcodeText' => $shipmentResult->getSimpleBarcodeText(),
177 - 'shipmentBarcode' => $shipmentBarcodeResult->getImageContent(),
178 - 'orders' => $orders,
179 - 'packetIds' => $packetIds,
180 - 'wpOrders' => $wpOrders,
181 - 'orderCount' => count( $packetIds ),
182 - 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ),
183 - 'stylesheet' => Plugin::buildAssetUrl( 'public/order-collection-print.css' ),
184 - 'translations' => [
184 + 'storeAddress' => $storeAddress,
185 + 'storeName' => get_option( 'blogname', '' ),
186 + 'shipmentBarcodeText' => $shipmentResult->getSimpleBarcodeText(),
187 + 'shipmentBarcode' => $shipmentBarcodeResult->getImageContent(),
188 + 'orders' => $orders,
189 + 'packetIds' => $packetIds,
190 + 'wpOrders' => $wpOrders,
191 + 'orderCount' => count( $packetIds ),
192 + 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ),
193 + 'stylesheet' => $this->urlBuilder->buildAssetUrl( 'public/css/order-collection-print.css' ),
194 + 'showConsignPasswordForZBox' => $this->optionsProvider->isShowConsignPasswordForZBoxEnabled(),
195 + 'translations' => [
185 196 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ),
186 197 'packetCount' => __( 'Packet count', 'packeta' ),
187 198 'printedAt' => __( 'Printed at', 'packeta' ),
188 199 'sender' => __( 'Sender', 'packeta' ),
@@ -188,8 +199,9 @@
188 199 'sender' => __( 'Sender', 'packeta' ),
189 200 'recipient' => __( 'Recipient', 'packeta' ),
190 201 'orderNumber' => __( 'Order number', 'packeta' ),
191 202 'barcode' => __( 'Barcode', 'packeta' ),
203 + 'zboxConsignPassword' => __( 'Consignment code', 'packeta' ),
192 204 'created' => __( 'Created', 'packeta' ),
193 205 'nameAndSurname' => __( 'Name and surname', 'packeta' ),
194 206 'cod' => __( 'C.O.D.', 'packeta' ),
195 207 'pickUpPointOrCarrier' => __( 'Pickup point or carrier', 'packeta' ),
@@ -204,9 +216,9 @@
204 216 * Registers submenu item.
205 217 */
206 218 public function register(): void {
207 219 add_submenu_page(
208 - \Packetery\Module\Options\Page::SLUG,
220 + DashboardPage::SLUG,
209 221 __( 'Print AWB', 'packeta' ),
210 222 __( 'Print AWB', 'packeta' ),
211 223 'manage_woocommerce',
212 224 self::PAGE_SLUG,
@@ -227,14 +239,15 @@
227 239
228 240 /**
229 241 * Request shipment.
230 242 *
231 - * @param array $packetIds Packet ids.
243 + * @param string[] $packetIds Packet ids.
232 244 *
233 245 * @return Response\CreateShipment
234 246 */
235 247 private function requestShipment( array $packetIds ): Response\CreateShipment {
236 248 $request = new Request\CreateShipment( array_values( $packetIds ) );
249 +
237 250 return $this->soapApiClient->createShipment( $request );
238 251 }
239 252
240 253 /**
@@ -245,8 +258,9 @@
245 258 * @return Response\BarcodePng
246 259 */
247 260 private function requestBarcodePng( string $barcode ): Response\BarcodePng {
248 261 $request = new Request\BarcodePng( $barcode );
262 +
249 263 return $this->soapApiClient->barcodePng( $request );
250 264 }
251 265
252 266 /**
@@ -259,9 +273,9 @@
259 273 */
260 274 private function logApiErrorMessageFromCreateShipmentResponse( Response\CreateShipment $createShipmentResponse, array $orders ): void {
261 275 $invalidPacketIds = $createShipmentResponse->getInvalidPacketIds();
262 276 foreach ( $orders as $order ) {
263 - if ( in_array( (int) $order->getPacketId(), $invalidPacketIds, true ) ) {
277 + if ( in_array( $order->getPacketId(), $invalidPacketIds, true ) ) {
264 278 $order->updateApiErrorMessage( $createShipmentResponse->getFaultString() );
265 279 $this->orderRepository->save( $order );
266 280 }
267 281 }