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