127 lines
4.7 KiB
PHP
127 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\EtsyConnection;
|
|
use App\Services\Bambuddy;
|
|
use App\Services\DryerClient;
|
|
use App\Services\Etsy;
|
|
use App\Services\Parcel2Go;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\View\View;
|
|
use Throwable;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __invoke(Request $request, Etsy $etsy, Bambuddy $bambuddy, Parcel2Go $parcel2Go, DryerClient $dryer): View
|
|
{
|
|
$isAdmin = $request->user()->role === 'admin';
|
|
$etsyStatus = $isAdmin ? 'Not connected' : null;
|
|
$etsyConnected = false;
|
|
$receipts = [];
|
|
$listingCount = null;
|
|
$shippingStatus = $isAdmin ? 'Not configured' : null;
|
|
$shipments = [];
|
|
|
|
if ($isAdmin && ($connection = EtsyConnection::first()) && $etsy->configured()) {
|
|
try {
|
|
$receipts = $etsy->receipts($connection)['results'] ?? [];
|
|
$etsyStatus = 'Live · '.$connection->shop_name;
|
|
$etsyConnected = true;
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$etsyStatus = 'Temporarily unavailable';
|
|
}
|
|
|
|
try {
|
|
$listings = $etsy->listings($connection);
|
|
$listingCount = (int) ($listings['count'] ?? count($listings['results'] ?? []));
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
}
|
|
}
|
|
|
|
if ($isAdmin && filled(config('services.parcel2go.client_id')) && filled(config('services.parcel2go.client_secret'))) {
|
|
try {
|
|
$shipments = array_slice($parcel2Go->fetch('me/orders/detail')['Orders'] ?? [], 0, 4);
|
|
$shippingStatus = 'Live account';
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$shippingStatus = 'Temporarily unavailable';
|
|
}
|
|
}
|
|
|
|
$printers = [];
|
|
$printerStatus = 'Not configured';
|
|
if ($bambuddy->configured()) {
|
|
try {
|
|
$printers = $bambuddy->printers();
|
|
$printerStatus = 'Live from BamBuddy';
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$printerStatus = 'Temporarily unavailable';
|
|
}
|
|
}
|
|
|
|
$printerStates = collect($printers)->countBy(function (array $printer): string {
|
|
$live = $printer['live'] ?? [];
|
|
$state = strtoupper((string) ($live['state'] ?? $printer['status'] ?? ''));
|
|
|
|
if ($live === []) {
|
|
return 'Unavailable';
|
|
}
|
|
if (($live['connected'] ?? false) === false) {
|
|
return 'Offline';
|
|
}
|
|
if (in_array($state, ['RUNNING', 'PRINTING'], true)) {
|
|
return 'Printing';
|
|
}
|
|
if (in_array($state, ['IDLE', 'READY', 'FINISH'], true)) {
|
|
return 'Idle';
|
|
}
|
|
|
|
return 'Other';
|
|
});
|
|
|
|
$dryerState = 'Not configured';
|
|
if (filled(config('services.dryer.api_key'))) {
|
|
try {
|
|
$dryerState = ucfirst((string) ($dryer->status()['state'] ?? 'Unknown'));
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$dryerState = 'Unavailable';
|
|
}
|
|
}
|
|
|
|
$days = collect(range(13, 0))->map(fn (int $offset): Carbon => now()->startOfDay()->subDays($offset));
|
|
$orderTrend = $days->map(fn (Carbon $day): int => collect($receipts)->filter(
|
|
fn (array $receipt): bool => isset($receipt['create_timestamp'])
|
|
&& Carbon::createFromTimestamp((int) $receipt['create_timestamp'])->isSameDay($day),
|
|
)->count())->all();
|
|
|
|
return view('dashboard', [
|
|
'isAdmin' => $isAdmin,
|
|
'receipts' => array_slice($receipts, 0, 5),
|
|
'awaitingDispatch' => count($receipts),
|
|
'listingCount' => $listingCount,
|
|
'etsyStatus' => $etsyStatus,
|
|
'etsyConnected' => $etsyConnected,
|
|
'shipments' => $shipments,
|
|
'shippingStatus' => $shippingStatus,
|
|
'printers' => $printers,
|
|
'printerStatus' => $printerStatus,
|
|
'printingCount' => $printerStates->get('Printing', 0),
|
|
'printerChart' => $printerStates->map(fn (int $count, string $state): array => [
|
|
'label' => $state,
|
|
'count' => $count,
|
|
])->values()->all(),
|
|
'dryerState' => $dryerState,
|
|
'orderChart' => [
|
|
'labels' => $days->map(fn (Carbon $day): string => $day->format('d M'))->all(),
|
|
'series' => $orderTrend,
|
|
],
|
|
]);
|
|
}
|
|
}
|