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

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

272 lines 8.1 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 Packetery\Latte\Engine;
20 use Packetery\Nette\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 * Packet actions common logic.
75 *
76 * @var PacketActionsCommonLogic
77 */
78 private $commonLogic;
79
80 /**
81 * LabelPrint constructor.
82 *
83 * @param Engine $latteEngine Latte Engine.
84 * @param Http\Request $httpRequest Http Request.
85 * @param Client $soapApiClient SOAP API Client.
86 * @param MessageManager $messageManager Message Manager.
87 * @param EntityFactory\Address $addressFactory Address factory.
88 * @param Repository $orderRepository Order repository.
89 * @param PacketActionsCommonLogic $packetActionsCommonLogic Common logic.
90 */
91 public function __construct(
92 Engine $latteEngine,
93 Http\Request $httpRequest,
94 Client $soapApiClient,
95 MessageManager $messageManager,
96 EntityFactory\Address $addressFactory,
97 Repository $orderRepository,
98 PacketActionsCommonLogic $packetActionsCommonLogic
99 ) {
100 $this->latteEngine = $latteEngine;
101 $this->httpRequest = $httpRequest;
102 $this->soapApiClient = $soapApiClient;
103 $this->messageManager = $messageManager;
104 $this->addressFactory = $addressFactory;
105 $this->orderRepository = $orderRepository;
106 $this->commonLogic = $packetActionsCommonLogic;
107 }
108
109 /**
110 * Generates id of transient to store order ids.
111 *
112 * @return string
113 */
114 public static function getOrderIdsTransientName(): string {
115 return 'packetery_order_collection_print_order_ids_' . wp_get_session_token();
116 }
117
118 /**
119 * Prepares form and renders template.
120 */
121 public function print(): void {
122 if ( $this->httpRequest->getQuery( 'page' ) !== self::PAGE_SLUG ) {
123 return;
124 }
125
126 if ( ! get_transient( self::getOrderIdsTransientName() ) ) {
127 $this->messageManager->flash_message( __( 'No orders were selected', 'packeta' ), 'info' );
128 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
129 }
130
131 $orderIds = get_transient( self::getOrderIdsTransientName() );
132 $orders = $this->orderRepository->getByIds( $orderIds );
133 $packetIds = [];
134 $wpOrders = [];
135
136 foreach ( $orders as $order ) {
137 if ( null === $order->getPacketId() ) {
138 continue;
139 }
140
141 $orderNumber = $order->getNumber();
142 $packetIds[ $orderNumber ] = $order->getPacketId();
143 $wpOrders[ $orderNumber ] = $this->orderRepository->getWcOrderById( (int) $orderNumber );
144 }
145
146 if ( ! $packetIds ) {
147 delete_transient( self::getOrderIdsTransientName() );
148 $this->messageManager->flash_message( __( 'Selected orders were not yet submitted to Packeta.', 'packeta' ), 'info' );
149 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
150 }
151
152 $shipmentResult = $this->requestShipment( $packetIds );
153 if ( $shipmentResult->hasFault() && $shipmentResult->getInvalidPacketIds() ) {
154 $this->logApiErrorMessageFromCreateShipmentResponse( $shipmentResult, $orders );
155 $packetIds = array_diff( $packetIds, $shipmentResult->getInvalidPacketIds() );
156 $shipmentResult = $this->requestShipment( $packetIds );
157 }
158
159 if ( $shipmentResult->hasFault() ) {
160 delete_transient( self::getOrderIdsTransientName() );
161 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
162 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
163 }
164
165 $shipmentBarcodeResult = $this->requestBarcodePng( $shipmentResult->getBarcode() );
166 delete_transient( self::getOrderIdsTransientName() );
167 if ( $shipmentBarcodeResult->hasFault() ) {
168 $this->messageManager->flash_message( __( 'Unexpected error', 'packeta' ), MessageManager::TYPE_ERROR );
169 $this->commonLogic->redirectTo( PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID );
170 }
171
172 $storeAddress = $this->addressFactory->fromWcStoreOptions();
173 $this->latteEngine->render(
174 PACKETERY_PLUGIN_DIR . '/template/order/collection-print.latte',
175 [
176 'storeAddress' => $storeAddress,
177 'storeName' => get_option( 'blogname', '' ),
178 'shipmentBarcodeText' => $shipmentResult->getSimpleBarcodeText(),
179 'shipmentBarcode' => $shipmentBarcodeResult->getImageContent(),
180 'orders' => $orders,
181 'packetIds' => $packetIds,
182 'wpOrders' => $wpOrders,
183 'orderCount' => count( $packetIds ),
184 'printedAt' => ( new \DateTimeImmutable() )->setTimezone( wp_timezone() ),
185 'stylesheet' => Plugin::buildAssetUrl( 'public/order-collection-print.css' ),
186 'translations' => [
187 'handoverPacketsHeading' => __( 'Handover packets', 'packeta' ),
188 'packetCount' => __( 'Packet count', 'packeta' ),
189 'printedAt' => __( 'Printed at', 'packeta' ),
190 'sender' => __( 'Sender', 'packeta' ),
191 'recipient' => __( 'Recipient', 'packeta' ),
192 'orderNumber' => __( 'Order number', 'packeta' ),
193 'barcode' => __( 'Barcode', 'packeta' ),
194 'created' => __( 'Created', 'packeta' ),
195 'nameAndSurname' => __( 'Name and surname', 'packeta' ),
196 'cod' => __( 'C.O.D.', 'packeta' ),
197 'pickUpPointOrCarrier' => __( 'Pickup point or carrier', 'packeta' ),
198 'end' => __( 'END', 'packeta' ),
199 ],
200 ]
201 );
202 exit;
203 }
204
205 /**
206 * Registers submenu item.
207 */
208 public function register(): void {
209 add_submenu_page(
210 \Packetery\Module\Options\Page::SLUG,
211 __( 'Print AWB', 'packeta' ),
212 __( 'Print AWB', 'packeta' ),
213 'manage_woocommerce',
214 self::PAGE_SLUG,
215 [
216 $this,
217 'print',
218 ],
219 20
220 );
221 }
222
223 /**
224 * Hides submenu item.
225 */
226 public function hideFromMenus(): void {
227 Plugin::hideSubmenuItem( self::PAGE_SLUG );
228 }
229
230 /**
231 * Request shipment.
232 *
233 * @param array $packetIds Packet ids.
234 *
235 * @return Response\CreateShipment
236 */
237 private function requestShipment( array $packetIds ): Response\CreateShipment {
238 $request = new Request\CreateShipment( array_values( $packetIds ) );
239 return $this->soapApiClient->createShipment( $request );
240 }
241
242 /**
243 * Request barcode image.
244 *
245 * @param string $barcode Barcode.
246 *
247 * @return Response\BarcodePng
248 */
249 private function requestBarcodePng( string $barcode ): Response\BarcodePng {
250 $request = new Request\BarcodePng( $barcode );
251 return $this->soapApiClient->barcodePng( $request );
252 }
253
254 /**
255 * Saves API error message to order.
256 *
257 * @param Response\CreateShipment $createShipmentResponse CreateShipment response.
258 * @param Order[] $orders Indexed array of Order entities.
259 *
260 * @return void
261 */
262 private function logApiErrorMessageFromCreateShipmentResponse( Response\CreateShipment $createShipmentResponse, array $orders ): void {
263 $invalidPacketIds = $createShipmentResponse->getInvalidPacketIds();
264 foreach ( $orders as $order ) {
265 if ( in_array( $order->getPacketId(), $invalidPacketIds, true ) ) {
266 $order->updateApiErrorMessage( $createShipmentResponse->getFaultString() );
267 $this->orderRepository->save( $order );
268 }
269 }
270 }
271 }
272