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

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

274 lines 7.8 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\Dashboard\DashboardPage;
18 use Packetery\Module\EntityFactory;
19 use Packetery\Module\MessageManager;
20 use Packetery\Module\Plugin;
21 use Packetery\Module\Transients;
22 use Packetery\Module\Views\UrlBuilder;
23 use Packetery\Nette\Http;
24
25 /**
26 * Class LabelPrint.
27 *
28 * @package Packetery\Order
29 */
30 class CollectionPrint {
31
32 const ACTION_PRINT_ORDER_COLLECTION = 'packetery_print_order_collection';
33 const PAGE_SLUG = 'packeta-order-collection-print';
34
35 /**
36 * PacketeryLatte Engine.
37 *
38 * @var Engine PacketeryLatte engine.
39 */
40 private $latteEngine;
41
42 /**
43 * Http Request.
44 *
45 * @var Http\Request
46 */
47 private $httpRequest;
48
49 /**
50 * SOAP API Client.
51 *
52 * @var Client SOAP API Client.
53 */
54 private $soapApiClient;
55
56 /**
57 * Message Manager.
58 *
59 * @var MessageManager
60 */
61 private $messageManager;
62
63 /**
64 * Order repository.
65 *
66 * @var Repository
67 */
68 private $orderRepository;
69
70 /**
71 * Address factory.
72 *
73 * @var \Packetery\Module\EntityFactory\Address
74 */
75 private $addressFactory;
76
77 /**
78 * Packet actions common logic.
79 *
80 * @var PacketActionsCommonLogic
81 */
82 private $commonLogic;
83
84 /**
85 * @var UrlBuilder
86 */
87 private $urlBuilder;
88
89 public function __construct(
90 Engine $latteEngine,
91 Http\Request $httpRequest,
92 Client $soapApiClient,
93 MessageManager $messageManager,
94 EntityFactory\Address $addressFactory,
95 Repository $orderRepository,
96 PacketActionsCommonLogic $packetActionsCommonLogic,
97 UrlBuilder $urlBuilder
98 ) {
99 $this->latteEngine = $latteEngine;
100 $this->httpRequest = $httpRequest;
101 $this->soapApiClient = $soapApiClient;
102 $this->messageManager = $messageManager;
103 $this->addressFactory = $addressFactory;
104 $this->orderRepository = $orderRepository;
105 $this->commonLogic = $packetActionsCommonLogic;
106 $this->urlBuilder = $urlBuilder;
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 Transients::ORDER_COLLECTION_PRINT_ORDER_IDS_PREFIX . 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() ) === false ) {
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 ( $order->getPacketId() === null ) {
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 ( count( $packetIds ) === 0 ) {
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() && count( $shipmentResult->getInvalidPacketIds() ) > 0 ) {
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' => $this->urlBuilder->buildAssetUrl( 'public/css/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 DashboardPage::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 string[] $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
240 return $this->soapApiClient->createShipment( $request );
241 }
242
243 /**
244 * Request barcode image.
245 *
246 * @param string $barcode Barcode.
247 *
248 * @return Response\BarcodePng
249 */
250 private function requestBarcodePng( string $barcode ): Response\BarcodePng {
251 $request = new Request\BarcodePng( $barcode );
252
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 }
272 }
273 }
274