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