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