PluginProbe
Packeta / 1.5.3
Packeta v1.5.3
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
packeta / src / Packetery / Module / Order / CollectionPrint.php

CollectionPrint.php in Packeta 1.5.3, at src/Packetery/Module/Order/CollectionPrint.php

270 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LabelPrint.
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Core\Api\Soap\Client;
13 use Packetery\Core\Api\Soap\Request;
14 use Packetery\Core\Api\Soap\Response;
15 use Packetery\Core\Entity\Order;
16 use Packetery\Module\EntityFactory;
17 use Packetery\Module\MessageManager;
18 use Packetery\Module\Plugin;
19 use PacketeryLatte\Engine;
20 use PacketeryNette\Http;
21
22 /**
23 * Class LabelPrint.
24 *
25 * @package Packetery\Order
26 */
27 class CollectionPrint {
28 const ACTION_PRINT_ORDER_COLLECTION = 'packetery_print_order_collection';
29 const PAGE_SLUG = 'packeta-order-collection-print';
30
31 /**
32 * PacketeryLatte Engine.
33 *
34 * @var Engine PacketeryLatte engine.
35 */
36 private $latteEngine;
37
38 /**
39 * Http Request.
40 *
41 * @var Http\Request
42 */
43 private $httpRequest;
44
45 /**
46 * SOAP API Client.
47 *
48 * @var Client SOAP API Client.
49 */
50 private $soapApiClient;
51
52 /**
53 * Message Manager.
54 *
55 * @var MessageManager
56 */
57 private $messageManager;
58
59 /**
60 * Order repository.
61 *
62 * @var Repository
63 */
64 private $orderRepository;
65
66 /**
67 * Address factory.
68 *
69 * @var \Packetery\Module\EntityFactory\Address
70 */
71 private $addressFactory;
72
73 /**
74 * LabelPrint constructor.
75 *
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.
82 */
83 public function __construct(
84 Engine $latteEngine,
85 Http\Request $httpRequest,
86 Client $soapApiClient,
87 MessageManager $messageManager,
88 EntityFactory\Address $addressFactory,
89 Repository $orderRepository
90 ) {
91 $this->latteEngine = $latteEngine;
92 $this->httpRequest = $httpRequest;
93 $this->soapApiClient = $soapApiClient;
94 $this->messageManager = $messageManager;
95 $this->addressFactory = $addressFactory;
96 $this->orderRepository = $orderRepository;
97 }
98
99 /**
100 * Generates id of transient to store order ids.
101 *
102 * @return string
103 */
104 public static function getOrderIdsTransientName(): string {
105 return 'packetery_order_collection_print_order_ids_' . wp_get_session_token();
106 }
107
108 /**
109 * Prepares form and renders template.
110 */
111 public function print(): void {
112 if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) {
113 return;
114 }
115
116 if ( ! get_transient( self::getOrderIdsTransientName() ) ) {
117 $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' );
118 if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
119 exit;
120 }
121 }
122
123 $orderIds = get_transient( self::getOrderIdsTransientName() );
124 $orders = $this->orderRepository->getByIds( $orderIds );
125 $packetIds = [];
126 $wpOrders = [];
127
128 foreach ( $orders as $order ) {
129 if ( null === $order->getPacketId() ) {
130 continue;
131 }
132
133 $orderNumber = $order->getNumber();
134 $packetIds[ $orderNumber ] = $order->getPacketId();
135 $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber );
136 }
137
138 if ( ! $packetIds ) {
139 delete_transient( self::getOrderIdsTransientName() );
140 $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 }
144 }
145
146 $shipmentResult = $this->requestShipment( $packetIds );
147 if ( $shipmentResult->hasFault() && $shipmentResult->getInvalidPacketIds() ) {
148 $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders );
149 $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() );
150 $shipmentResult = $this->requestShipment( $packetIds );
151 }
152
153 if ( $shipmentResult->hasFault() ) {
154 delete_transient( self::getOrderIdsTransientName() );
155 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
156 if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
157 exit;
158 }
159 }
160
161 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
162 delete_transient( self::getOrderIdsTransientName() );
163 if ( $shipmentBarcodeResult->hasFault() ) {
164 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
165 if ( wp_safe_redirect( 'edit.php?post_type=shop_order' ) ) {
166 exit;
167 }
168 }
169
170 $storeAddress = $this->addressFactory->fromWcStoreOptions();
171 $this->latteEngine->render(
172 PACKETERY_PLUGIN_DIR . '/template/order/collection-print.latte',
173 [
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' => [
185 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ),
186 'packetCount' => __( 'Packet count', 'packeta' ),
187 'printedAt' => __( 'Printed at', 'packeta' ),
188 'sender' => __( 'Sender', 'packeta' ),
189 'recipient' => __( 'Recipient', 'packeta' ),
190 'orderNumber' => __( 'Order number', 'packeta' ),
191 'barcode' => __( 'Barcode', 'packeta' ),
192 'created' => __( 'Created', 'packeta' ),
193 'nameAndSurname' => __( 'Name and surname', 'packeta' ),
194 'cod' => __( 'C.O.D.', 'packeta' ),
195 'pickUpPointOrCarrier' => __( 'Pickup point or carrier', 'packeta' ),
196 'end' => __( 'END', 'packeta' ),
197 ],
198 ]
199 );
200 exit;
201 }
202
203 /**
204 * Registers submenu item.
205 */
206 public function register(): void {
207 add_submenu_page(
208 \Packetery\Module\Options\Page::SLUG,
209 __( 'Print AWB', 'packeta' ),
210 __( 'Print AWB', 'packeta' ),
211 'manage_woocommerce',
212 self::PAGE_SLUG,
213 [
214 $this,
215 'print',
216 ],
217 20
218 );
219 }
220
221 /**
222 * Hides submenu item.
223 */
224 public function hideFromMenus(): void {
225 Plugin::hideSubmenuItem( self::PAGE_SLUG );
226 }
227
228 /**
229 * Request shipment.
230 *
231 * @param array $packetIds Packet ids.
232 *
233 * @return Response\CreateShipment
234 */
235 private function requestShipment( array $packetIds ): Response\CreateShipment {
236 $request = new Request\CreateShipment( array_values( $packetIds ) );
237 return $this->soapApiClient->createShipment( $request );
238 }
239
240 /**
241 * Request barcode image.
242 *
243 * @param string $barcode Barcode.
244 *
245 * @return Response\BarcodePng
246 */
247 private function requestBarcodePng( string $barcode ): Response\BarcodePng {
248 $request = new Request\BarcodePng( $barcode );
249 return $this->soapApiClient->barcodePng( $request );
250 }
251
252 /**
253 * Saves API error message to order.
254 *
255 * @param Response\CreateShipment $createShipmentResponse CreateShipment response.
256 * @param Order[] $orders Indexed array of Order entities.
257 *
258 * @return void
259 */
260 private function logApiErrorMessageFromCreateShipmentResponse( Response\CreateShipment $createShipmentResponse, array $orders ): void {
261 $invalidPacketIds = $createShipmentResponse->getInvalidPacketIds();
262 foreach ( $orders as $order ) {
263 if ( in_array( $order->getPacketId(), $invalidPacketIds, true ) ) {
264 $order->updateApiErrorMessage( $createShipmentResponse->getFaultString() );
265 $this->orderRepository->save( $order );
266 }
267 }
268 }
269 }
270