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