PluginProbe
Packeta / 1.4.1
Packeta v1.4.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 1.4.1, at src/Packetery/Module/Order/CollectionPrint.php

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