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
packeta / src / Packetery / Module / Order / CollectionPrint.php

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

271 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\Latte\Engine;
17 use Packetery\Module\EntityFactory;
18 use Packetery\Module\MessageManager;
19 use Packetery\Module\Plugin;
20 use Packetery\Module\Views\UrlBuilder;
21 use Packetery\Nette\Http;
22
23 /**
24 * Class LabelPrint.
25 *
26 * @package Packetery\Order
27 */
28 class CollectionPrint {
29 const ACTION_PRINT_ORDER_COLLECTION = 'packetery_print_order_collection';
30 const PAGE_SLUG = 'packeta-order-collection-print';
31
32 /**
33 * PacketeryLatte Engine.
34 *
35 * @var Engine PacketeryLatte engine.
36 */
37 private $latteEngine;
38
39 /**
40 * Http Request.
41 *
42 * @var Http\Request
43 */
44 private $httpRequest;
45
46 /**
47 * SOAP API Client.
48 *
49 * @var Client SOAP API Client.
50 */
51 private $soapApiClient;
52
53 /**
54 * Message Manager.
55 *
56 * @var MessageManager
57 */
58 private $messageManager;
59
60 /**
61 * Order repository.
62 *
63 * @var Repository
64 */
65 private $orderRepository;
66
67 /**
68 * Address factory.
69 *
70 * @var \Packetery\Module\EntityFactory\Address
71 */
72 private $addressFactory;
73
74 /**
75 * Packet actions common logic.
76 *
77 * @var PacketActionsCommonLogic
78 */
79 private $commonLogic;
80
81 /**
82 * @var UrlBuilder
83 */
84 private $urlBuilder;
85
86 public function __construct(
87 Engine $latteEngine,
88 Http\Request $httpRequest,
89 Client $soapApiClient,
90 MessageManager $messageManager,
91 EntityFactory\Address $addressFactory,
92 Repository $orderRepository,
93 PacketActionsCommonLogic $packetActionsCommonLogic,
94 UrlBuilder $urlBuilder
95 ) {
96 $this->latteEngine = $latteEngine;
97 $this->httpRequest = $httpRequest;
98 $this->soapApiClient = $soapApiClient;
99 $this->messageManager = $messageManager;
100 $this->addressFactory = $addressFactory;
101 $this->orderRepository = $orderRepository;
102 $this->commonLogic = $packetActionsCommonLogic;
103 $this->urlBuilder = $urlBuilder;
104 }
105
106 /**
107 * Generates id of transient to store order ids.
108 *
109 * @return string
110 */
111 public static function getOrderIdsTransientName(): string {
112 return 'packetery_order_collection_print_order_ids_' . wp_get_session_token();
113 }
114
115 /**
116 * Prepares form and renders template.
117 */
118 public function print(): void {
119 if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) {
120 return;
121 }
122
123 if ( get_transient( self::getOrderIdsTransientName() ) === false ) {
124 $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' );
125 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
126 }
127
128 $orderIds = get_transient( self::getOrderIdsTransientName() );
129 $orders = $this->orderRepository->getByIds( $orderIds );
130 $packetIds = [];
131 $wpOrders = [];
132
133 foreach ( $orders as $order ) {
134 if ( $order->getPacketId() === null ) {
135 continue;
136 }
137
138 $orderNumber = $order->getNumber();
139 $packetIds[ $orderNumber ] = $order->getPacketId();
140 $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber );
141 }
142
143 if ( count( $packetIds ) === 0 ) {
144 delete_transient( self::getOrderIdsTransientName() );
145 $this->messageManager->flash_message( __( 'Selected orders were not yet submitted to Packeta.', 'packeta' ), 'info' );
146 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
147 }
148
149 $shipmentResult = $this->requestShipment( $packetIds );
150 if ( $shipmentResult->hasFault() && count( $shipmentResult->getInvalidPacketIds() ) > 0 ) {
151 $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders );
152 $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() );
153 $shipmentResult = $this->requestShipment( $packetIds );
154 }
155
156 if ( $shipmentResult->hasFault() ) {
157 delete_transient( self::getOrderIdsTransientName() );
158 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
159 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
160 }
161
162 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
163 delete_transient( self::getOrderIdsTransientName() );
164 if ( $shipmentBarcodeResult->hasFault() ) {
165 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
166 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
167 }
168
169 $storeAddress = $this->addressFactory->fromWcStoreOptions();
170 $this->latteEngine->render(
171 PACKETERY_PLUGIN_DIR . '/template/order/collection-print.latte',
172 [
173 'storeAddress' => $storeAddress,
174 'storeName' => get_option( 'blogname', '' ),
175 'shipmentBarcodeText' => $shipmentResult->getSimpleBarcodeText(),
176 'shipmentBarcode' => $shipmentBarcodeResult->getImageContent(),
177 'orders' => $orders,
178 'packetIds' => $packetIds,
179 'wpOrders' => $wpOrders,
180 'orderCount' => count( $packetIds ),
181 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ),
182 'stylesheet' => $this->urlBuilder->buildAssetUrl( 'public/css/order-collection-print.css' ),
183 'translations' => [
184 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ),
185 'packetCount' => __( 'Packet count', 'packeta' ),
186 'printedAt' => __( 'Printed at', 'packeta' ),
187 'sender' => __( 'Sender', 'packeta' ),
188 'recipient' => __( 'Recipient', 'packeta' ),
189 'orderNumber' => __( 'Order number', 'packeta' ),
190 'barcode' => __( 'Barcode', 'packeta' ),
191 'created' => __( 'Created', 'packeta' ),
192 'nameAndSurname' => __( 'Name and surname', 'packeta' ),
193 'cod' => __( 'C.O.D.', 'packeta' ),
194 'pickUpPointOrCarrier' => __( 'Pickup point or carrier', 'packeta' ),
195 'end' => __( 'END', 'packeta' ),
196 ],
197 ]
198 );
199 exit;
200 }
201
202 /**
203 * Registers submenu item.
204 */
205 public function register(): void {
206 add_submenu_page(
207 \Packetery\Module\Options\Page::SLUG,
208 __( 'Print AWB', 'packeta' ),
209 __( 'Print AWB', 'packeta' ),
210 'manage_woocommerce',
211 self::PAGE_SLUG,
212 [
213 $this,
214 'print',
215 ],
216 20
217 );
218 }
219
220 /**
221 * Hides submenu item.
222 */
223 public function hideFromMenus(): void {
224 Plugin::hideSubmenuItem( self::PAGE_SLUG );
225 }
226
227 /**
228 * Request shipment.
229 *
230 * @param string[] $packetIds Packet ids.
231 *
232 * @return Response\CreateShipment
233 */
234 private function requestShipment( array $packetIds ): Response\CreateShipment {
235 $request = new Request\CreateShipment( array_values( $packetIds ) );
236
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
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 }
269 }
270 }
271