| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class GridExtender. |
| 4 |
* |
| 5 |
* @package Packetery\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Helper; |
| 13 |
use Packetery\Module\Carrier; |
| 14 |
use PacketeryLatte\Engine; |
| 15 |
use PacketeryNette\Http\Request; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class GridExtender. |
| 19 |
* |
| 20 |
* @package Packetery\Order |
| 21 |
*/ |
| 22 |
class GridExtender { |
| 23 |
/** |
| 24 |
* Generic Helper. |
| 25 |
* |
| 26 |
* @var Helper |
| 27 |
*/ |
| 28 |
private $helper; |
| 29 |
|
| 30 |
/** |
| 31 |
* Carrier repository. |
| 32 |
* |
| 33 |
* @var Carrier\Repository |
| 34 |
*/ |
| 35 |
private $carrierRepository; |
| 36 |
|
| 37 |
/** |
| 38 |
* Latte Engine. |
| 39 |
* |
| 40 |
* @var Engine |
| 41 |
*/ |
| 42 |
private $latteEngine; |
| 43 |
|
| 44 |
/** |
| 45 |
* Http Request. |
| 46 |
* |
| 47 |
* @var Request |
| 48 |
*/ |
| 49 |
private $httpRequest; |
| 50 |
|
| 51 |
/** |
| 52 |
* Controller router. |
| 53 |
* |
| 54 |
* @var ControllerRouter |
| 55 |
*/ |
| 56 |
private $orderControllerRouter; |
| 57 |
|
| 58 |
/** |
| 59 |
* Order repository. |
| 60 |
* |
| 61 |
* @var Repository |
| 62 |
*/ |
| 63 |
private $orderRepository; |
| 64 |
|
| 65 |
/** |
| 66 |
* GridExtender constructor. |
| 67 |
* |
| 68 |
* @param Helper $helper Helper. |
| 69 |
* @param Carrier\Repository $carrierRepository Carrier repository. |
| 70 |
* @param Engine $latteEngine Latte Engine. |
| 71 |
* @param Request $httpRequest Http Request. |
| 72 |
* @param ControllerRouter $orderControllerRouter Order controller router. |
| 73 |
* @param Repository $orderRepository Order repository. |
| 74 |
*/ |
| 75 |
public function __construct( |
| 76 |
Helper $helper, |
| 77 |
Carrier\Repository $carrierRepository, |
| 78 |
Engine $latteEngine, |
| 79 |
Request $httpRequest, |
| 80 |
ControllerRouter $orderControllerRouter, |
| 81 |
Repository $orderRepository |
| 82 |
) { |
| 83 |
$this->helper = $helper; |
| 84 |
$this->carrierRepository = $carrierRepository; |
| 85 |
$this->latteEngine = $latteEngine; |
| 86 |
$this->httpRequest = $httpRequest; |
| 87 |
$this->orderControllerRouter = $orderControllerRouter; |
| 88 |
$this->orderRepository = $orderRepository; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Adds custom filtering links to order grid. |
| 93 |
* |
| 94 |
* @param array $var Array of html links. |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function addFilterLinks( array $var ): array { |
| 99 |
$latteParams = [ |
| 100 |
'link' => add_query_arg( |
| 101 |
[ |
| 102 |
'post_type' => 'shop_order', |
| 103 |
'filter_action' => 'packetery_filter_link', |
| 104 |
'packetery_to_submit' => '1', |
| 105 |
'packetery_to_print' => false, |
| 106 |
], |
| 107 |
admin_url( 'edit.php' ) |
| 108 |
), |
| 109 |
'title' => __( 'packetaOrdersToSubmit', 'packetery' ), |
| 110 |
'orderCount' => $this->orderRepository->countOrdersToSubmit(), |
| 111 |
'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ), |
| 112 |
]; |
| 113 |
$var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); |
| 114 |
|
| 115 |
$latteParams = [ |
| 116 |
'link' => add_query_arg( |
| 117 |
[ |
| 118 |
'post_type' => 'shop_order', |
| 119 |
'filter_action' => 'packetery_filter_link', |
| 120 |
'packetery_to_submit' => false, |
| 121 |
'packetery_to_print' => '1', |
| 122 |
], |
| 123 |
admin_url( 'edit.php' ) |
| 124 |
), |
| 125 |
'title' => __( 'packetaOrdersToPrint', 'packetery' ), |
| 126 |
'orderCount' => $this->orderRepository->countOrdersToPrint(), |
| 127 |
'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ), |
| 128 |
]; |
| 129 |
$var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); |
| 130 |
|
| 131 |
return $var; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Adds select to order grid. |
| 136 |
*/ |
| 137 |
public function renderOrderTypeSelect(): void { |
| 138 |
$linkFilters = []; |
| 139 |
|
| 140 |
if ( null !== $this->httpRequest->getQuery( 'packetery_to_submit' ) ) { |
| 141 |
$linkFilters['packetery_to_submit'] = '1'; |
| 142 |
} |
| 143 |
|
| 144 |
if ( null !== $this->httpRequest->getQuery( 'packetery_to_print' ) ) { |
| 145 |
$linkFilters['packetery_to_print'] = '1'; |
| 146 |
} |
| 147 |
|
| 148 |
$this->latteEngine->render( |
| 149 |
PACKETERY_PLUGIN_DIR . '/template/order/type-select.latte', |
| 150 |
[ |
| 151 |
'packeteryOrderType' => $this->httpRequest->getQuery( 'packetery_order_type' ), |
| 152 |
'linkFilters' => $linkFilters, |
| 153 |
] |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Fills custom order list columns. |
| 159 |
* |
| 160 |
* @param string $column Current order column name. |
| 161 |
*/ |
| 162 |
public function fillCustomOrderListColumns( string $column ): void { |
| 163 |
global $post; |
| 164 |
$wcOrder = wc_get_order( $post->ID ); |
| 165 |
$order = $this->orderRepository->getByWcOrder( $wcOrder ); |
| 166 |
if ( null === $order ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
switch ( $column ) { |
| 171 |
case 'packetery_destination': |
| 172 |
$pickupPoint = $order->getPickupPoint(); |
| 173 |
if ( null !== $pickupPoint ) { |
| 174 |
$pointName = $pickupPoint->getName(); |
| 175 |
$pointId = $pickupPoint->getId(); |
| 176 |
$country = strtolower( $wcOrder->get_shipping_country() ); |
| 177 |
$internalCountries = array_keys( $this->carrierRepository->getZpointCarriers() ); |
| 178 |
if ( in_array( $country, $internalCountries, true ) ) { |
| 179 |
echo esc_html( "$pointName ($pointId)" ); |
| 180 |
} else { |
| 181 |
echo esc_html( $pointName ); |
| 182 |
} |
| 183 |
break; |
| 184 |
} |
| 185 |
$homeDeliveryCarrier = $this->carrierRepository->getById( (int) $order->getCarrierId() ); |
| 186 |
if ( $homeDeliveryCarrier ) { |
| 187 |
$homeDeliveryCarrierEntity = new Carrier\Entity( $homeDeliveryCarrier ); |
| 188 |
echo esc_html( $homeDeliveryCarrierEntity->getFinalName() ); |
| 189 |
} |
| 190 |
break; |
| 191 |
case 'packetery_packet_id': |
| 192 |
$packetId = $order->getPacketId(); |
| 193 |
if ( $packetId ) { |
| 194 |
echo '<a href="' . esc_attr( $this->helper->get_tracking_url( $packetId ) ) . '" target="_blank">Z' . esc_html( $packetId ) . '</a>'; |
| 195 |
} |
| 196 |
break; |
| 197 |
case 'packetery': |
| 198 |
$packetSubmitUrl = add_query_arg( [], $this->orderControllerRouter->getRouteUrl( Controller::PATH_SUBMIT_TO_API ) ); |
| 199 |
$printLink = add_query_arg( |
| 200 |
[ |
| 201 |
'page' => LabelPrint::MENU_SLUG, |
| 202 |
LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ), |
| 203 |
'id' => $order->getNumber(), |
| 204 |
'packet_id' => $order->getPacketId(), |
| 205 |
'offset' => 0, |
| 206 |
], |
| 207 |
admin_url( 'admin.php' ) |
| 208 |
); |
| 209 |
$this->latteEngine->render( |
| 210 |
PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte', |
| 211 |
[ |
| 212 |
'order' => $order, |
| 213 |
'hasOrderWeight' => ( null !== $order->getWeight() && $order->getWeight() > 0 ), |
| 214 |
'packetSubmitUrl' => $packetSubmitUrl, |
| 215 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 216 |
'printLink' => $printLink, |
| 217 |
] |
| 218 |
); |
| 219 |
break; |
| 220 |
// TODO: Packet status sync. |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Gets code text translated. |
| 226 |
* |
| 227 |
* @param string|null $packetStatus Packet status. |
| 228 |
* |
| 229 |
* @return string|null |
| 230 |
*/ |
| 231 |
public function getPacketStatusTranslated( ?string $packetStatus ): string { |
| 232 |
switch ( $packetStatus ) { |
| 233 |
case 'received data': |
| 234 |
return __( 'packetStatusReceivedData', 'packetery' ); |
| 235 |
case 'arrived': |
| 236 |
return __( 'packetStatusArrived', 'packetery' ); |
| 237 |
case 'prepared for departure': |
| 238 |
return __( 'packetStatusPreparedForDeparture', 'packetery' ); |
| 239 |
case 'departed': |
| 240 |
return __( 'packetStatusDeparted', 'packetery' ); |
| 241 |
case 'ready for pickup': |
| 242 |
return __( 'packetStatusReadyForPickup', 'packetery' ); |
| 243 |
case 'handed to carrier': |
| 244 |
return __( 'packetStatusHandedToCarrier', 'packetery' ); |
| 245 |
case 'delivered': |
| 246 |
return __( 'packetStatusDelivered', 'packetery' ); |
| 247 |
case 'posted back': |
| 248 |
return __( 'packetStatusPostedBack', 'packetery' ); |
| 249 |
case 'returned': |
| 250 |
return __( 'packetStatusReturned', 'packetery' ); |
| 251 |
case 'cancelled': |
| 252 |
return __( 'packetStatusCancelled', 'packetery' ); |
| 253 |
case 'collected': |
| 254 |
return __( 'packetStatusCollected', 'packetery' ); |
| 255 |
case 'unknown': |
| 256 |
return __( 'packetStatusUnknown', 'packetery' ); |
| 257 |
} |
| 258 |
|
| 259 |
return (string) $packetStatus; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Add order list columns. |
| 264 |
* |
| 265 |
* @param string[] $columns Order list columns. |
| 266 |
* |
| 267 |
* @return string[] All columns. |
| 268 |
*/ |
| 269 |
public function addOrderListColumns( array $columns ): array { |
| 270 |
$new_columns = array(); |
| 271 |
|
| 272 |
foreach ( $columns as $column_name => $column_info ) { |
| 273 |
$new_columns[ $column_name ] = $column_info; |
| 274 |
|
| 275 |
if ( 'order_total' === $column_name ) { |
| 276 |
// TODO: Packet status sync. |
| 277 |
$new_columns['packetery'] = __( 'Packeta', 'packetery' ); |
| 278 |
$new_columns['packetery_packet_id'] = __( 'Barcode', 'packetery' ); |
| 279 |
$new_columns['packetery_destination'] = __( 'Pick up point or carrier', 'packetery' ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return $new_columns; |
| 284 |
} |
| 285 |
} |
| 286 |
|