Release v1.5.1
This commit is contained in:
536
plugins/advanced-reporting/ReportsController.php
Normal file
536
plugins/advanced-reporting/ReportsController.php
Normal file
@@ -0,0 +1,536 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plugins\AdvancedReporting;
|
||||
|
||||
use Core\Http\Response;
|
||||
use Core\Services\ApiLayer;
|
||||
use Core\Services\Auth;
|
||||
use Core\Services\Database;
|
||||
use Core\Services\Settings;
|
||||
use Core\Views\View;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
class ReportsController
|
||||
{
|
||||
private View $view;
|
||||
private array $releaseTrackCache = [];
|
||||
private array $trackDownloadCountCache = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->view = new View(__DIR__ . '/views');
|
||||
}
|
||||
|
||||
public function adminIndex(): Response
|
||||
{
|
||||
if ($guard = $this->guard()) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$db = Database::get();
|
||||
if (!($db instanceof PDO)) {
|
||||
return new Response($this->view->render('admin/index.php', [
|
||||
'title' => 'Sales Reports',
|
||||
'tab' => 'overview',
|
||||
'filters' => $this->filters(),
|
||||
'overview' => [],
|
||||
'artist_rows' => [],
|
||||
'track_rows' => [],
|
||||
'artist_options' => [],
|
||||
'tables_ready' => false,
|
||||
'currency' => Settings::get('store_currency', 'GBP'),
|
||||
]));
|
||||
}
|
||||
|
||||
ApiLayer::ensureSchema($db);
|
||||
$filters = $this->filters();
|
||||
$tab = $filters['tab'];
|
||||
|
||||
return new Response($this->view->render('admin/index.php', [
|
||||
'title' => 'Sales Reports',
|
||||
'tab' => $tab,
|
||||
'filters' => $filters,
|
||||
'overview' => $this->overviewPayload($db, $filters),
|
||||
'artist_rows' => $tab === 'artists' ? $this->artistRows($db, $filters) : [],
|
||||
'track_rows' => $tab === 'tracks' ? $this->trackRows($db, $filters) : [],
|
||||
'artist_options' => $this->artistOptions($db),
|
||||
'tables_ready' => $this->tablesReady($db),
|
||||
'currency' => strtoupper(trim((string)Settings::get('store_currency', 'GBP'))),
|
||||
]));
|
||||
}
|
||||
|
||||
public function adminExport(): Response
|
||||
{
|
||||
if ($guard = $this->guard()) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$db = Database::get();
|
||||
if (!($db instanceof PDO)) {
|
||||
return new Response('Database unavailable', 500);
|
||||
}
|
||||
|
||||
ApiLayer::ensureSchema($db);
|
||||
$filters = $this->filters();
|
||||
$type = strtolower(trim((string)($_GET['type'] ?? 'artists')));
|
||||
if (!in_array($type, ['artists', 'tracks'], true)) {
|
||||
$type = 'artists';
|
||||
}
|
||||
|
||||
$rows = $type === 'tracks' ? $this->trackRows($db, $filters) : $this->artistRows($db, $filters);
|
||||
$stream = fopen('php://temp', 'w+');
|
||||
if ($stream === false) {
|
||||
return new Response('Unable to create export', 500);
|
||||
}
|
||||
|
||||
if ($type === 'tracks') {
|
||||
fputcsv($stream, ['Artist', 'Track', 'Release', 'Catalog', 'Units Sold', 'Units Refunded', 'Gross Revenue', 'Refunded Revenue', 'Net Revenue', 'PayPal Fees', 'Net After Fees', 'Downloads']);
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($stream, [
|
||||
(string)($row['artist_name'] ?? ''),
|
||||
(string)($row['track_display'] ?? ''),
|
||||
(string)($row['release_title'] ?? ''),
|
||||
(string)($row['catalog_no'] ?? ''),
|
||||
(int)($row['units_sold'] ?? 0),
|
||||
(int)($row['units_refunded'] ?? 0),
|
||||
number_format((float)($row['gross_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['refunded_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['net_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['payment_fees'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['net_after_fees'] ?? 0), 2, '.', ''),
|
||||
(int)($row['download_count'] ?? 0),
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
fputcsv($stream, ['Artist', 'Paid Orders', 'Units Sold', 'Units Refunded', 'Gross Revenue', 'Refunded Revenue', 'Net Revenue', 'PayPal Fees', 'Net After Fees', 'Releases', 'Tracks']);
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($stream, [
|
||||
(string)($row['artist_name'] ?? ''),
|
||||
(int)($row['paid_orders'] ?? 0),
|
||||
(int)($row['units_sold'] ?? 0),
|
||||
(int)($row['units_refunded'] ?? 0),
|
||||
number_format((float)($row['gross_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['refunded_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['net_revenue'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['payment_fees'] ?? 0), 2, '.', ''),
|
||||
number_format((float)($row['net_after_fees'] ?? 0), 2, '.', ''),
|
||||
(int)($row['release_count'] ?? 0),
|
||||
(int)($row['track_count'] ?? 0),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
rewind($stream);
|
||||
$csv = stream_get_contents($stream);
|
||||
fclose($stream);
|
||||
|
||||
return new Response((string)$csv, 200, [
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Content-Disposition' => 'attachment; filename="sales-report-' . $type . '-' . gmdate('Ymd-His') . '.csv"',
|
||||
]);
|
||||
}
|
||||
|
||||
private function guard(): ?Response
|
||||
{
|
||||
if (!Auth::check()) {
|
||||
return new Response('', 302, ['Location' => '/admin/login']);
|
||||
}
|
||||
if (!Auth::hasRole(['admin', 'manager'])) {
|
||||
return new Response('', 302, ['Location' => '/admin']);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function filters(): array
|
||||
{
|
||||
$tab = strtolower(trim((string)($_GET['tab'] ?? 'overview')));
|
||||
if (!in_array($tab, ['overview', 'artists', 'tracks'], true)) {
|
||||
$tab = 'overview';
|
||||
}
|
||||
|
||||
return [
|
||||
'tab' => $tab,
|
||||
'from' => trim((string)($_GET['from'] ?? '')),
|
||||
'to' => trim((string)($_GET['to'] ?? '')),
|
||||
'q' => trim((string)($_GET['q'] ?? '')),
|
||||
'artist_id' => max(0, (int)($_GET['artist_id'] ?? 0)),
|
||||
];
|
||||
}
|
||||
|
||||
private function tablesReady(PDO $db): bool
|
||||
{
|
||||
try {
|
||||
$probe = $db->query("SHOW TABLES LIKE 'ac_store_order_item_allocations'");
|
||||
return (bool)($probe && $probe->fetch(PDO::FETCH_NUM));
|
||||
} catch (Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function overviewPayload(PDO $db, array $filters): array
|
||||
{
|
||||
$stats = [
|
||||
'gross_revenue' => 0.0,
|
||||
'refunded_revenue' => 0.0,
|
||||
'net_revenue' => 0.0,
|
||||
'payment_fees' => 0.0,
|
||||
'net_after_fees' => 0.0,
|
||||
'paid_orders' => 0,
|
||||
'refunded_orders' => 0,
|
||||
'units_sold' => 0,
|
||||
'units_refunded' => 0,
|
||||
'top_artists' => [],
|
||||
'top_tracks' => [],
|
||||
];
|
||||
|
||||
try {
|
||||
[$whereSql, $params] = $this->dateWhere($filters, 'o');
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN o.total ELSE 0 END), 0) AS gross_revenue,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'refunded' THEN o.total ELSE 0 END), 0) AS refunded_revenue,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN COALESCE(o.payment_fee, 0) ELSE 0 END), 0) AS payment_fees,
|
||||
COUNT(DISTINCT CASE WHEN o.status = 'paid' THEN o.id END) AS paid_orders,
|
||||
COUNT(DISTINCT CASE WHEN o.status = 'refunded' THEN o.id END) AS refunded_orders
|
||||
FROM ac_store_orders o
|
||||
{$whereSql}
|
||||
");
|
||||
$stmt->execute($params);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
|
||||
$stats['gross_revenue'] = (float)($row['gross_revenue'] ?? 0);
|
||||
$stats['refunded_revenue'] = (float)($row['refunded_revenue'] ?? 0);
|
||||
$stats['payment_fees'] = (float)($row['payment_fees'] ?? 0);
|
||||
$stats['net_revenue'] = $stats['gross_revenue'] - $stats['refunded_revenue'];
|
||||
$stats['net_after_fees'] = $stats['net_revenue'] - $stats['payment_fees'];
|
||||
$stats['paid_orders'] = (int)($row['paid_orders'] ?? 0);
|
||||
$stats['refunded_orders'] = (int)($row['refunded_orders'] ?? 0);
|
||||
} catch (Throwable $e) {
|
||||
}
|
||||
|
||||
try {
|
||||
[$whereSql, $params] = $this->dateWhere($filters, 'o');
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN a.qty ELSE 0 END), 0) AS units_sold,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'refunded' THEN a.qty ELSE 0 END), 0) AS units_refunded
|
||||
FROM ac_store_order_item_allocations a
|
||||
JOIN ac_store_orders o ON o.id = a.order_id
|
||||
{$whereSql}
|
||||
");
|
||||
$stmt->execute($params);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
|
||||
$stats['units_sold'] = (int)($row['units_sold'] ?? 0);
|
||||
$stats['units_refunded'] = (int)($row['units_refunded'] ?? 0);
|
||||
} catch (Throwable $e) {
|
||||
}
|
||||
|
||||
$topFilters = $filters;
|
||||
$topFilters['limit'] = 5;
|
||||
$stats['top_artists'] = $this->artistRows($db, $topFilters);
|
||||
$stats['top_tracks'] = $this->trackRows($db, $topFilters);
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function artistRows(PDO $db, array $filters): array
|
||||
{
|
||||
[$whereSql, $params] = $this->dateWhere($filters, 'o');
|
||||
if (!empty($filters['artist_id'])) {
|
||||
$whereSql .= ($whereSql === '' ? ' WHERE ' : ' AND ') . 'alloc.artist_id = :artist_id';
|
||||
$params[':artist_id'] = (int)$filters['artist_id'];
|
||||
}
|
||||
$q = trim((string)($filters['q'] ?? ''));
|
||||
if ($q !== '') {
|
||||
$whereSql .= ($whereSql === '' ? ' WHERE ' : ' AND ') . " (ar.name LIKE :q OR r.artist_name LIKE :q OR alloc.title_snapshot LIKE :q)";
|
||||
$params[':q'] = '%' . $q . '%';
|
||||
}
|
||||
$limitSql = !empty($filters['limit']) ? ' LIMIT ' . max(1, (int)$filters['limit']) : '';
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
COALESCE(alloc.artist_id, 0) AS artist_id,
|
||||
COALESCE(NULLIF(MAX(ar.name), ''), NULLIF(MAX(r.artist_name), ''), 'Unknown Artist') AS artist_name,
|
||||
COUNT(DISTINCT CASE WHEN o.status = 'paid' THEN alloc.order_id END) AS paid_orders,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN alloc.qty ELSE 0 END), 0) AS units_sold,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'refunded' THEN alloc.qty ELSE 0 END), 0) AS units_refunded,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN alloc.gross_amount ELSE 0 END), 0) AS gross_revenue,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'refunded' THEN alloc.gross_amount ELSE 0 END), 0) AS refunded_revenue,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' AND COALESCE(NULLIF(o.payment_gross, 0), NULLIF(o.total, 0), 0) > 0 THEN COALESCE(o.payment_fee, 0) * (alloc.gross_amount / COALESCE(NULLIF(o.payment_gross, 0), NULLIF(o.total, 0))) ELSE 0 END), 0) AS payment_fees,
|
||||
COUNT(DISTINCT CASE WHEN o.status = 'paid' AND alloc.release_id IS NOT NULL THEN alloc.release_id END) AS release_count,
|
||||
COUNT(DISTINCT CASE WHEN o.status = 'paid' AND alloc.track_id IS NOT NULL THEN alloc.track_id END) AS track_count
|
||||
FROM ac_store_order_item_allocations alloc
|
||||
JOIN ac_store_orders o ON o.id = alloc.order_id
|
||||
LEFT JOIN ac_artists ar ON ar.id = alloc.artist_id
|
||||
LEFT JOIN ac_releases r ON r.id = alloc.release_id
|
||||
{$whereSql}
|
||||
GROUP BY COALESCE(alloc.artist_id, 0)
|
||||
ORDER BY
|
||||
((COALESCE(SUM(CASE WHEN o.status = 'paid' THEN alloc.gross_amount ELSE 0 END), 0) - COALESCE(SUM(CASE WHEN o.status = 'refunded' THEN alloc.gross_amount ELSE 0 END), 0)) - COALESCE(SUM(CASE WHEN o.status = 'paid' AND COALESCE(NULLIF(o.payment_gross, 0), NULLIF(o.total, 0), 0) > 0 THEN COALESCE(o.payment_fee, 0) * (alloc.gross_amount / COALESCE(NULLIF(o.payment_gross, 0), NULLIF(o.total, 0))) ELSE 0 END), 0)) DESC,
|
||||
COALESCE(SUM(CASE WHEN o.status = 'paid' THEN alloc.gross_amount ELSE 0 END), 0) DESC,
|
||||
artist_name ASC
|
||||
{$limitSql}
|
||||
");
|
||||
$stmt->execute($params);
|
||||
$rows = array_values(array_filter($stmt->fetchAll(PDO::FETCH_ASSOC) ?: [], static function (array $row): bool {
|
||||
return (float)($row['gross_revenue'] ?? 0) > 0 || (float)($row['refunded_revenue'] ?? 0) > 0;
|
||||
}));
|
||||
foreach ($rows as &$row) {
|
||||
$row['payment_fees'] = (float)($row['payment_fees'] ?? 0);
|
||||
$row['net_revenue'] = (float)($row['gross_revenue'] ?? 0) - (float)($row['refunded_revenue'] ?? 0);
|
||||
$row['net_after_fees'] = $row['net_revenue'] - $row['payment_fees'];
|
||||
}
|
||||
unset($row);
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function trackRows(PDO $db, array $filters): array
|
||||
{
|
||||
[$whereSql, $params] = $this->dateWhere($filters, 'o');
|
||||
if (!empty($filters['artist_id'])) {
|
||||
$whereSql .= ($whereSql === '' ? ' WHERE ' : ' AND ') . 'COALESCE(alloc.artist_id, r.artist_id) = :artist_id';
|
||||
$params[':artist_id'] = (int)$filters['artist_id'];
|
||||
}
|
||||
$q = trim((string)($filters['q'] ?? ''));
|
||||
if ($q !== '') {
|
||||
$whereSql .= ($whereSql === '' ? ' WHERE ' : ' AND ') . "(t.title LIKE :q OR t.mix_name LIKE :q OR ar.name LIKE :q OR r.title LIKE :q OR r.catalog_no LIKE :q)";
|
||||
$params[':q'] = '%' . $q . '%';
|
||||
}
|
||||
$limitSql = !empty($filters['limit']) ? ' LIMIT ' . max(1, (int)$filters['limit']) : '';
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
alloc.id,
|
||||
alloc.order_id,
|
||||
alloc.track_id,
|
||||
alloc.release_id,
|
||||
alloc.artist_id,
|
||||
alloc.qty,
|
||||
alloc.gross_amount,
|
||||
o.status,
|
||||
COALESCE(o.payment_fee, 0) AS payment_fee,
|
||||
COALESCE(o.payment_gross, 0) AS payment_gross,
|
||||
COALESCE(o.total, 0) AS order_total,
|
||||
COALESCE(NULLIF(MAX(t.title), ''), MAX(alloc.title_snapshot), 'Track') AS track_title,
|
||||
COALESCE(NULLIF(MAX(t.mix_name), ''), '') AS mix_name,
|
||||
COALESCE(NULLIF(MAX(ar.name), ''), NULLIF(MAX(r.artist_name), ''), 'Unknown Artist') AS artist_name,
|
||||
COALESCE(NULLIF(MAX(r.title), ''), 'Release') AS release_title,
|
||||
COALESCE(NULLIF(MAX(r.catalog_no), ''), '') AS catalog_no
|
||||
FROM ac_store_order_item_allocations alloc
|
||||
JOIN ac_store_orders o ON o.id = alloc.order_id
|
||||
LEFT JOIN ac_release_tracks t ON t.id = alloc.track_id
|
||||
LEFT JOIN ac_releases r ON r.id = COALESCE(alloc.release_id, t.release_id)
|
||||
LEFT JOIN ac_artists ar ON ar.id = COALESCE(alloc.artist_id, r.artist_id)
|
||||
{$whereSql}
|
||||
GROUP BY alloc.id
|
||||
ORDER BY alloc.id DESC
|
||||
");
|
||||
$stmt->execute($params);
|
||||
$allocations = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
||||
$rows = [];
|
||||
foreach ($allocations as $alloc) {
|
||||
$releaseId = (int)($alloc['release_id'] ?? 0);
|
||||
$trackId = (int)($alloc['track_id'] ?? 0);
|
||||
if ($trackId > 0) {
|
||||
$targets = [[
|
||||
'track_id' => $trackId,
|
||||
'artist_name' => (string)($alloc['artist_name'] ?? 'Unknown Artist'),
|
||||
'release_title' => (string)($alloc['release_title'] ?? 'Release'),
|
||||
'catalog_no' => (string)($alloc['catalog_no'] ?? ''),
|
||||
'track_title' => (string)($alloc['track_title'] ?? 'Track'),
|
||||
'mix_name' => (string)($alloc['mix_name'] ?? ''),
|
||||
'weight' => 1.0,
|
||||
]];
|
||||
} elseif ($releaseId > 0) {
|
||||
$targets = $this->releaseTracks($db, $releaseId, (string)($alloc['artist_name'] ?? 'Unknown Artist'), (string)($alloc['release_title'] ?? 'Release'), (string)($alloc['catalog_no'] ?? ''));
|
||||
if (!$targets) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
$totalWeight = 0.0;
|
||||
foreach ($targets as $target) {
|
||||
$totalWeight += max(0.0, (float)($target['weight'] ?? 0));
|
||||
}
|
||||
if ($totalWeight <= 0) {
|
||||
$totalWeight = (float)count($targets);
|
||||
foreach ($targets as &$target) {
|
||||
$target['weight'] = 1.0;
|
||||
}
|
||||
unset($target);
|
||||
}
|
||||
|
||||
foreach ($targets as $target) {
|
||||
$key = (int)($target['track_id'] ?? 0);
|
||||
if ($key <= 0) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($rows[$key])) {
|
||||
$trackTitle = trim((string)($target['track_title'] ?? 'Track'));
|
||||
$mixName = trim((string)($target['mix_name'] ?? ''));
|
||||
$rows[$key] = [
|
||||
'track_id' => $key,
|
||||
'artist_name' => (string)($target['artist_name'] ?? 'Unknown Artist'),
|
||||
'release_title' => (string)($target['release_title'] ?? 'Release'),
|
||||
'catalog_no' => (string)($target['catalog_no'] ?? ''),
|
||||
'track_title' => $trackTitle,
|
||||
'mix_name' => $mixName,
|
||||
'track_display' => $mixName !== '' ? $trackTitle . ' (' . $mixName . ')' : $trackTitle,
|
||||
'units_sold' => 0,
|
||||
'units_refunded' => 0,
|
||||
'gross_revenue' => 0.0,
|
||||
'refunded_revenue' => 0.0,
|
||||
'download_count' => $this->trackDownloadCount($db, $key),
|
||||
'payment_fees' => 0.0,
|
||||
'net_after_fees' => 0.0,
|
||||
];
|
||||
}
|
||||
$share = max(0.0, (float)($target['weight'] ?? 0)) / $totalWeight;
|
||||
$amount = (float)($alloc['gross_amount'] ?? 0) * $share;
|
||||
$qty = (int)($alloc['qty'] ?? 0);
|
||||
$feeBase = (float)($alloc['payment_gross'] ?? 0);
|
||||
if ($feeBase <= 0) {
|
||||
$feeBase = (float)($alloc['order_total'] ?? 0);
|
||||
}
|
||||
$feeShare = ((string)($alloc['status'] ?? '') === 'paid' && $feeBase > 0)
|
||||
? ((float)($alloc['payment_fee'] ?? 0) * (((float)($alloc['gross_amount'] ?? 0)) / $feeBase) * $share)
|
||||
: 0.0;
|
||||
if ((string)($alloc['status'] ?? '') === 'paid') {
|
||||
$rows[$key]['units_sold'] += $qty;
|
||||
$rows[$key]['gross_revenue'] += $amount;
|
||||
$rows[$key]['payment_fees'] += $feeShare;
|
||||
} elseif ((string)($alloc['status'] ?? '') === 'refunded') {
|
||||
$rows[$key]['units_refunded'] += $qty;
|
||||
$rows[$key]['refunded_revenue'] += $amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
$rows = array_values(array_filter($rows, static function (array $row): bool {
|
||||
return (float)($row['gross_revenue'] ?? 0) > 0 || (float)($row['refunded_revenue'] ?? 0) > 0;
|
||||
}));
|
||||
foreach ($rows as &$row) {
|
||||
$row['payment_fees'] = (float)($row['payment_fees'] ?? 0);
|
||||
$row['net_revenue'] = (float)($row['gross_revenue'] ?? 0) - (float)($row['refunded_revenue'] ?? 0);
|
||||
$row['net_after_fees'] = $row['net_revenue'] - $row['payment_fees'];
|
||||
}
|
||||
unset($row);
|
||||
usort($rows, static function (array $a, array $b): int {
|
||||
$cmp = ((float)($b['net_revenue'] ?? 0)) <=> ((float)($a['net_revenue'] ?? 0));
|
||||
if ($cmp !== 0) {
|
||||
return $cmp;
|
||||
}
|
||||
$cmp = ((float)($b['net_after_fees'] ?? 0)) <=> ((float)($a['net_after_fees'] ?? 0));
|
||||
if ($cmp !== 0) {
|
||||
return $cmp;
|
||||
}
|
||||
return strcasecmp((string)($a['track_display'] ?? ''), (string)($b['track_display'] ?? ''));
|
||||
});
|
||||
if (!empty($filters['limit'])) {
|
||||
$rows = array_slice($rows, 0, max(1, (int)$filters['limit']));
|
||||
}
|
||||
return $rows;
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function releaseTracks(PDO $db, int $releaseId, string $artistName, string $releaseTitle, string $catalogNo): array
|
||||
{
|
||||
if ($releaseId <= 0) {
|
||||
return [];
|
||||
}
|
||||
if (isset($this->releaseTrackCache[$releaseId])) {
|
||||
return $this->releaseTrackCache[$releaseId];
|
||||
}
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT
|
||||
t.id,
|
||||
t.title,
|
||||
COALESCE(t.mix_name, '') AS mix_name,
|
||||
COALESCE(sp.track_price, 0) AS track_price
|
||||
FROM ac_release_tracks t
|
||||
LEFT JOIN ac_store_track_products sp ON sp.release_track_id = t.id AND sp.is_enabled = 1
|
||||
WHERE t.release_id = :release_id
|
||||
ORDER BY t.track_no ASC, t.id ASC
|
||||
");
|
||||
$stmt->execute([':release_id' => $releaseId]);
|
||||
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
||||
$targets = [];
|
||||
foreach ($tracks as $track) {
|
||||
$targets[] = [
|
||||
'track_id' => (int)($track['id'] ?? 0),
|
||||
'artist_name' => $artistName,
|
||||
'release_title' => $releaseTitle,
|
||||
'catalog_no' => $catalogNo,
|
||||
'track_title' => (string)($track['title'] ?? 'Track'),
|
||||
'mix_name' => (string)($track['mix_name'] ?? ''),
|
||||
'weight' => max(0.0, (float)($track['track_price'] ?? 0)),
|
||||
];
|
||||
}
|
||||
$this->releaseTrackCache[$releaseId] = $targets;
|
||||
return $targets;
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function trackDownloadCount(PDO $db, int $trackId): int
|
||||
{
|
||||
if ($trackId <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (isset($this->trackDownloadCountCache[$trackId])) {
|
||||
return $this->trackDownloadCountCache[$trackId];
|
||||
}
|
||||
try {
|
||||
$stmt = $db->prepare("
|
||||
SELECT COUNT(e.id) AS download_count
|
||||
FROM ac_store_files f
|
||||
LEFT JOIN ac_store_download_events e ON e.file_id = f.id
|
||||
WHERE f.scope_type = 'track' AND f.scope_id = :track_id
|
||||
");
|
||||
$stmt->execute([':track_id' => $trackId]);
|
||||
$count = (int)(($stmt->fetch(PDO::FETCH_ASSOC) ?: [])['download_count'] ?? 0);
|
||||
$this->trackDownloadCountCache[$trackId] = $count;
|
||||
return $count;
|
||||
} catch (Throwable $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private function artistOptions(PDO $db): array
|
||||
{
|
||||
try {
|
||||
$stmt = $db->query("SELECT id, name FROM ac_artists WHERE name IS NOT NULL AND name <> '' ORDER BY name ASC");
|
||||
return $stmt ? ($stmt->fetchAll(PDO::FETCH_ASSOC) ?: []) : [];
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function dateWhere(array $filters, string $alias): array
|
||||
{
|
||||
$where = [];
|
||||
$params = [];
|
||||
$from = trim((string)($filters['from'] ?? ''));
|
||||
$to = trim((string)($filters['to'] ?? ''));
|
||||
if ($from !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $from)) {
|
||||
$where[] = $alias . '.created_at >= :from_date';
|
||||
$params[':from_date'] = $from . ' 00:00:00';
|
||||
}
|
||||
if ($to !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
|
||||
$where[] = $alias . '.created_at <= :to_date';
|
||||
$params[':to_date'] = $to . ' 23:59:59';
|
||||
}
|
||||
return [$where ? ' WHERE ' . implode(' AND ', $where) : '', $params];
|
||||
}
|
||||
}
|
||||
14
plugins/advanced-reporting/plugin.json
Normal file
14
plugins/advanced-reporting/plugin.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Advanced Reporting",
|
||||
"version": "0.1.0",
|
||||
"description": "Expanded sales reporting for the store layer.",
|
||||
"author": "AudioCore",
|
||||
"admin_nav": {
|
||||
"label": "Sales Reports",
|
||||
"url": "/admin/store/reports",
|
||||
"roles": ["admin", "manager"],
|
||||
"icon": "fa-solid fa-chart-line"
|
||||
},
|
||||
"entry": "plugin.php",
|
||||
"default_enabled": false
|
||||
}
|
||||
13
plugins/advanced-reporting/plugin.php
Normal file
13
plugins/advanced-reporting/plugin.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Core\Http\Router;
|
||||
use Plugins\AdvancedReporting\ReportsController;
|
||||
|
||||
require_once __DIR__ . '/ReportsController.php';
|
||||
|
||||
return function (Router $router): void {
|
||||
$controller = new ReportsController();
|
||||
$router->get('/admin/store/reports', [$controller, 'adminIndex']);
|
||||
$router->get('/admin/store/reports/export', [$controller, 'adminExport']);
|
||||
};
|
||||
387
plugins/advanced-reporting/views/admin/index.php
Normal file
387
plugins/advanced-reporting/views/admin/index.php
Normal file
@@ -0,0 +1,387 @@
|
||||
<?php
|
||||
$pageTitle = $title ?? 'Sales Reports';
|
||||
$tab = (string)($tab ?? 'overview');
|
||||
$filters = is_array($filters ?? null) ? $filters : [];
|
||||
$overview = is_array($overview ?? null) ? $overview : [];
|
||||
$artistRows = is_array($artist_rows ?? null) ? $artist_rows : [];
|
||||
$trackRows = is_array($track_rows ?? null) ? $track_rows : [];
|
||||
$artistOptions = is_array($artist_options ?? null) ? $artist_options : [];
|
||||
$tablesReady = (bool)($tables_ready ?? false);
|
||||
$currency = (string)($currency ?? 'GBP');
|
||||
$tabLabels = [
|
||||
'overview' => 'Overview',
|
||||
'artists' => 'Artists',
|
||||
'tracks' => 'Tracks',
|
||||
];
|
||||
$activeTabLabel = $tabLabels[$tab] ?? 'Overview';
|
||||
$overviewGross = (float)($overview['gross_revenue'] ?? 0);
|
||||
$overviewRefunded = (float)($overview['refunded_revenue'] ?? 0);
|
||||
$overviewNet = (float)($overview['net_revenue'] ?? 0);
|
||||
$overviewNetAfterFees = (float)($overview['net_after_fees'] ?? 0);
|
||||
$selectedFrom = (string)($filters['from'] ?? '');
|
||||
$selectedTo = (string)($filters['to'] ?? '');
|
||||
$selectedArtistId = (int)($filters['artist_id'] ?? 0);
|
||||
$selectedQuery = (string)($filters['q'] ?? '');
|
||||
$today = new DateTimeImmutable('today');
|
||||
$monthStart = $today->modify('first day of this month');
|
||||
$monthEnd = $today->modify('last day of this month');
|
||||
$lastMonthStart = $monthStart->modify('-1 month');
|
||||
$lastMonthEnd = $monthStart->modify('-1 day');
|
||||
$quarter = (int)floor((((int)$today->format('n')) - 1) / 3);
|
||||
$quarterStartMonth = ($quarter * 3) + 1;
|
||||
$quarterStart = new DateTimeImmutable($today->format('Y') . '-' . str_pad((string)$quarterStartMonth, 2, '0', STR_PAD_LEFT) . '-01');
|
||||
$lastQuarterEnd = $quarterStart->modify('-1 day');
|
||||
$lastQuarterStartMonth = (int)$lastQuarterEnd->format('n') - (((int)$lastQuarterEnd->format('n') - 1) % 3);
|
||||
$lastQuarterStart = new DateTimeImmutable($lastQuarterEnd->format('Y') . '-' . str_pad((string)$lastQuarterStartMonth, 2, '0', STR_PAD_LEFT) . '-01');
|
||||
$quickRanges = [
|
||||
'7d' => ['label' => 'Last 7 days', 'from' => $today->modify('-6 days')->format('Y-m-d'), 'to' => $today->format('Y-m-d')],
|
||||
'30d' => ['label' => 'Last 30 days', 'from' => $today->modify('-29 days')->format('Y-m-d'), 'to' => $today->format('Y-m-d')],
|
||||
'month' => ['label' => 'This month', 'from' => $monthStart->format('Y-m-d'), 'to' => $monthEnd->format('Y-m-d')],
|
||||
'last-month' => ['label' => 'Last month', 'from' => $lastMonthStart->format('Y-m-d'), 'to' => $lastMonthEnd->format('Y-m-d')],
|
||||
'quarter' => ['label' => 'This quarter', 'from' => $quarterStart->format('Y-m-d'), 'to' => $today->format('Y-m-d')],
|
||||
'last-quarter' => ['label' => 'Last quarter', 'from' => $lastQuarterStart->format('Y-m-d'), 'to' => $lastQuarterEnd->format('Y-m-d')],
|
||||
'ytd' => ['label' => 'Year to date', 'from' => $today->format('Y') . '-01-01', 'to' => $today->format('Y-m-d')],
|
||||
];
|
||||
$activeRangeLabel = 'Custom range';
|
||||
foreach ($quickRanges as $range) {
|
||||
if ($selectedFrom === $range['from'] && $selectedTo === $range['to']) {
|
||||
$activeRangeLabel = $range['label'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$artistExportUrl = '/admin/store/reports/export?type=artists'
|
||||
. '&from=' . rawurlencode($selectedFrom)
|
||||
. '&to=' . rawurlencode($selectedTo)
|
||||
. '&artist_id=' . $selectedArtistId
|
||||
. '&q=' . rawurlencode($selectedQuery);
|
||||
$trackExportUrl = '/admin/store/reports/export?type=tracks'
|
||||
. '&from=' . rawurlencode($selectedFrom)
|
||||
. '&to=' . rawurlencode($selectedTo)
|
||||
. '&artist_id=' . $selectedArtistId
|
||||
. '&q=' . rawurlencode($selectedQuery);
|
||||
$exportUrl = '/admin/store/reports/export?type=' . ($tab === 'tracks' ? 'tracks' : 'artists')
|
||||
. '&from=' . rawurlencode($selectedFrom)
|
||||
. '&to=' . rawurlencode($selectedTo)
|
||||
. '&artist_id=' . $selectedArtistId
|
||||
. '&q=' . rawurlencode($selectedQuery);
|
||||
ob_start();
|
||||
?>
|
||||
<section class="admin-card reports-page reports-shell">
|
||||
<div class="reports-hero">
|
||||
<div class="reports-hero-copy">
|
||||
<div class="badge">Store Analytics</div>
|
||||
<h1>Sales Reports</h1>
|
||||
<p>Revenue, allocations, and performance reporting across orders, artists, releases, and tracks.</p>
|
||||
</div>
|
||||
<div class="reports-hero-actions">
|
||||
<div class="reports-status-card">
|
||||
<span class="reports-status-label">Current View</span>
|
||||
<strong><?= htmlspecialchars($activeTabLabel, ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<small><?= $tablesReady ? 'Live allocation data ready' : 'Waiting for reporting tables' ?></small>
|
||||
</div>
|
||||
<a href="/admin/store" class="btn outline">Back to Store</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (!$tablesReady): ?>
|
||||
<div class="reports-empty-state">
|
||||
<strong>Reporting tables are not ready.</strong>
|
||||
<span>Initialize the Store plugin first so allocation and reporting tables exist.</span>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="reports-toolbar">
|
||||
<div class="reports-tabset">
|
||||
<a href="/admin/store/reports?tab=overview" class="reports-tab <?= $tab === 'overview' ? 'is-active' : '' ?>">Overview</a>
|
||||
<a href="/admin/store/reports?tab=artists" class="reports-tab <?= $tab === 'artists' ? 'is-active' : '' ?>">Artists</a>
|
||||
<a href="/admin/store/reports?tab=tracks" class="reports-tab <?= $tab === 'tracks' ? 'is-active' : '' ?>">Tracks</a>
|
||||
</div>
|
||||
<div class="reports-export-actions">
|
||||
<a href="<?= htmlspecialchars($artistExportUrl, ENT_QUOTES, 'UTF-8') ?>" class="btn outline small">Export Artists</a>
|
||||
<a href="<?= htmlspecialchars($trackExportUrl, ENT_QUOTES, 'UTF-8') ?>" class="btn outline small">Export Tracks</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="reports-range-bar">
|
||||
<div class="reports-range-copy">
|
||||
<span class="reports-range-label">Reporting Period</span>
|
||||
<strong><?= htmlspecialchars($activeRangeLabel, ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<small><?= $selectedFrom !== '' || $selectedTo !== '' ? htmlspecialchars(trim(($selectedFrom !== '' ? $selectedFrom : 'Beginning') . ' → ' . ($selectedTo !== '' ? $selectedTo : 'Today')), ENT_QUOTES, 'UTF-8') : 'No explicit date filter applied' ?></small>
|
||||
</div>
|
||||
<div class="reports-range-chips">
|
||||
<?php foreach ($quickRanges as $key => $range): ?>
|
||||
<?php $rangeUrl = '/admin/store/reports?tab=' . rawurlencode($tab) . '&from=' . rawurlencode($range['from']) . '&to=' . rawurlencode($range['to']) . '&artist_id=' . $selectedArtistId . '&q=' . rawurlencode($selectedQuery); ?>
|
||||
<a href="<?= htmlspecialchars($rangeUrl, ENT_QUOTES, 'UTF-8') ?>" class="reports-chip <?= $activeRangeLabel === $range['label'] ? 'is-active' : '' ?>"><?= htmlspecialchars($range['label'], ENT_QUOTES, 'UTF-8') ?></a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form method="get" action="/admin/store/reports" class="reports-filter-bar">
|
||||
<input type="hidden" name="tab" value="<?= htmlspecialchars($tab, ENT_QUOTES, 'UTF-8') ?>">
|
||||
<label class="reports-filter-field reports-filter-wide">
|
||||
<span>Search</span>
|
||||
<input class="input" type="text" name="q" value="<?= htmlspecialchars($selectedQuery, ENT_QUOTES, 'UTF-8') ?>" placeholder="Artist, track, release, catalog">
|
||||
</label>
|
||||
<label class="reports-filter-field">
|
||||
<span>From</span>
|
||||
<input class="input" type="date" name="from" value="<?= htmlspecialchars($selectedFrom, ENT_QUOTES, 'UTF-8') ?>">
|
||||
</label>
|
||||
<label class="reports-filter-field">
|
||||
<span>To</span>
|
||||
<input class="input" type="date" name="to" value="<?= htmlspecialchars($selectedTo, ENT_QUOTES, 'UTF-8') ?>">
|
||||
</label>
|
||||
<label class="reports-filter-field">
|
||||
<span>Artist</span>
|
||||
<select class="input" name="artist_id">
|
||||
<option value="0">All artists</option>
|
||||
<?php foreach ($artistOptions as $artist): ?>
|
||||
<option value="<?= (int)($artist['id'] ?? 0) ?>" <?= $selectedArtistId === (int)($artist['id'] ?? 0) ? 'selected' : '' ?>><?= htmlspecialchars((string)($artist['name'] ?? ''), ENT_QUOTES, 'UTF-8') ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</label>
|
||||
<div class="reports-filter-actions">
|
||||
<button type="submit" class="btn small">Apply</button>
|
||||
<a href="/admin/store/reports?tab=<?= rawurlencode($tab) ?>" class="btn outline small">Reset</a>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php if ($tab === 'overview'): ?>
|
||||
<div class="reports-kpi-grid reports-kpi-grid-minimal">
|
||||
<article class="reports-metric reports-metric-primary reports-metric-focus">
|
||||
<span>Before Fees</span>
|
||||
<strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format($overviewNet, 2) ?></strong>
|
||||
<small>Total sales before processor fees.</small>
|
||||
</article>
|
||||
<article class="reports-metric reports-metric-primary reports-metric-focus">
|
||||
<span>After Fees</span>
|
||||
<strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format($overviewNetAfterFees, 2) ?></strong>
|
||||
<small>Total sales after captured PayPal fees.</small>
|
||||
</article>
|
||||
</div>
|
||||
|
||||
<div class="reports-showcase-grid">
|
||||
<section class="reports-panel reports-ranking-panel">
|
||||
<div class="reports-panel-head">
|
||||
<div>
|
||||
<h2>Top Artists</h2>
|
||||
<p>Best performing artist allocations for the selected period.</p>
|
||||
</div>
|
||||
<span>Net After Fees</span>
|
||||
</div>
|
||||
<?php if (!$overview['top_artists']): ?>
|
||||
<div class="reports-empty-state compact">
|
||||
<strong>No artist sales in this period.</strong>
|
||||
<span>Try widening the date range or clearing the artist filter.</span>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="reports-ranking-list">
|
||||
<?php foreach ($overview['top_artists'] as $index => $row): ?>
|
||||
<article class="reports-ranking-row">
|
||||
<div class="reports-rank"><?= $index + 1 ?></div>
|
||||
<div class="reports-ranking-copy">
|
||||
<strong><?= htmlspecialchars((string)($row['artist_name'] ?? 'Unknown Artist'), ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<span><?= (int)($row['units_sold'] ?? 0) ?> sold / <?= (int)($row['paid_orders'] ?? 0) ?> paid orders / <?= (int)($row['release_count'] ?? 0) ?> releases</span>
|
||||
</div>
|
||||
<div class="reports-ranking-value"><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['net_after_fees'] ?? 0), 2) ?></div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
|
||||
<section class="reports-panel reports-ranking-panel">
|
||||
<div class="reports-panel-head">
|
||||
<div>
|
||||
<h2>Top Tracks</h2>
|
||||
<p>Derived track performance from direct, release, and bundle allocations.</p>
|
||||
</div>
|
||||
<span>Net After Fees</span>
|
||||
</div>
|
||||
<?php if (!$overview['top_tracks']): ?>
|
||||
<div class="reports-empty-state compact">
|
||||
<strong>No track sales in this period.</strong>
|
||||
<span>Track-level allocations appear once paid orders exist in the selected range.</span>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="reports-ranking-list">
|
||||
<?php foreach ($overview['top_tracks'] as $index => $row): ?>
|
||||
<article class="reports-ranking-row reports-ranking-row-track">
|
||||
<div class="reports-rank"><?= $index + 1 ?></div>
|
||||
<div class="reports-ranking-copy">
|
||||
<strong><?= htmlspecialchars((string)($row['track_display'] ?? 'Track'), ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<span><?= htmlspecialchars((string)($row['artist_name'] ?? 'Unknown Artist'), ENT_QUOTES, 'UTF-8') ?> / <?= htmlspecialchars((string)($row['catalog_no'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span>
|
||||
</div>
|
||||
<div class="reports-ranking-value"><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['net_after_fees'] ?? 0), 2) ?></div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
</div>
|
||||
<?php elseif ($tab === 'artists'): ?>
|
||||
<section class="reports-panel reports-ledger-panel">
|
||||
<div class="reports-panel-head reports-panel-head-large">
|
||||
<div>
|
||||
<h2>Artist Ledger</h2>
|
||||
<p>Grouped artist performance with paid orders, units, refunds, captured fees, and net after fees.</p>
|
||||
</div>
|
||||
<span><?= count($artistRows) ?> rows</span>
|
||||
</div>
|
||||
<?php if (!$artistRows): ?>
|
||||
<div class="reports-empty-state compact">
|
||||
<strong>No artist sales found.</strong>
|
||||
<span>Try widening the date range or clearing the artist filter.</span>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="reports-ledger-list">
|
||||
<?php foreach ($artistRows as $row): ?>
|
||||
<article class="reports-ledger-row">
|
||||
<div class="reports-ledger-main">
|
||||
<strong><?= htmlspecialchars((string)($row['artist_name'] ?? 'Unknown Artist'), ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<span><?= (int)($row['release_count'] ?? 0) ?> releases / <?= (int)($row['track_count'] ?? 0) ?> direct tracks</span>
|
||||
</div>
|
||||
<div class="reports-ledger-metrics">
|
||||
<div class="reports-stat-chip"><small>Paid Orders</small><strong><?= (int)($row['paid_orders'] ?? 0) ?></strong></div>
|
||||
<div class="reports-stat-chip"><small>Units</small><strong><?= (int)($row['units_sold'] ?? 0) ?></strong></div>
|
||||
<div class="reports-stat-chip muted"><small>Refunded Units</small><strong><?= (int)($row['units_refunded'] ?? 0) ?></strong></div>
|
||||
<div class="reports-money-stack"><small>Gross</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['gross_revenue'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack muted"><small>Refunded</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['refunded_revenue'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack muted"><small>PayPal Fees</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['payment_fees'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack emphasis"><small>Net After Fees</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['net_after_fees'] ?? 0), 2) ?></strong></div>
|
||||
</div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<?php else: ?>
|
||||
<section class="reports-panel reports-ledger-panel">
|
||||
<div class="reports-panel-head reports-panel-head-large">
|
||||
<div>
|
||||
<h2>Track Ledger</h2>
|
||||
<p>Track-level breakdown derived from direct sales, release sales, bundle allocations, and captured PayPal fees.</p>
|
||||
</div>
|
||||
<span><?= count($trackRows) ?> rows</span>
|
||||
</div>
|
||||
<?php if (!$trackRows): ?>
|
||||
<div class="reports-empty-state compact">
|
||||
<strong>No track sales found.</strong>
|
||||
<span>Track metrics will populate once paid orders exist in the selected range.</span>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="reports-ledger-list reports-ledger-list-tracks">
|
||||
<?php foreach ($trackRows as $row): ?>
|
||||
<article class="reports-ledger-row reports-ledger-row-track">
|
||||
<div class="reports-ledger-main">
|
||||
<strong><?= htmlspecialchars((string)($row['track_display'] ?? 'Track'), ENT_QUOTES, 'UTF-8') ?></strong>
|
||||
<span><?= htmlspecialchars((string)($row['artist_name'] ?? 'Unknown Artist'), ENT_QUOTES, 'UTF-8') ?> / <?= htmlspecialchars((string)($row['release_title'] ?? ''), ENT_QUOTES, 'UTF-8') ?><?= !empty($row['catalog_no']) ? ' / ' . htmlspecialchars((string)$row['catalog_no'], ENT_QUOTES, 'UTF-8') : '' ?></span>
|
||||
</div>
|
||||
<div class="reports-ledger-metrics">
|
||||
<div class="reports-stat-chip"><small>Sold</small><strong><?= (int)($row['units_sold'] ?? 0) ?></strong></div>
|
||||
<div class="reports-stat-chip muted"><small>Refunded Units</small><strong><?= (int)($row['units_refunded'] ?? 0) ?></strong></div>
|
||||
<div class="reports-stat-chip"><small>Downloads</small><strong><?= (int)($row['download_count'] ?? 0) ?></strong></div>
|
||||
<div class="reports-money-stack"><small>Gross</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['gross_revenue'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack muted"><small>Refunded</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['refunded_revenue'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack muted"><small>PayPal Fees</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['payment_fees'] ?? 0), 2) ?></strong></div>
|
||||
<div class="reports-money-stack emphasis"><small>Net After Fees</small><strong><?= htmlspecialchars($currency, ENT_QUOTES, 'UTF-8') ?> <?= number_format((float)($row['net_after_fees'] ?? 0), 2) ?></strong></div>
|
||||
</div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<style>
|
||||
.reports-shell { display:grid; gap:18px; }
|
||||
.reports-hero { display:grid; grid-template-columns:minmax(0,1fr) 280px; gap:18px; align-items:start; }
|
||||
.reports-hero-copy { display:grid; gap:10px; }
|
||||
.reports-hero-copy h1 { margin:0; font-size:34px; line-height:1; }
|
||||
.reports-hero-copy p { margin:0; max-width:760px; color:var(--muted); font-size:15px; }
|
||||
.reports-hero-actions { display:grid; gap:12px; justify-items:stretch; }
|
||||
.reports-status-card { padding:16px; border-radius:16px; border:1px solid rgba(255,255,255,.08); background:linear-gradient(180deg, rgba(255,255,255,.05), rgba(255,255,255,.015)); box-shadow:inset 0 1px 0 rgba(255,255,255,.04); }
|
||||
.reports-status-card strong { display:block; font-size:20px; margin-top:6px; }
|
||||
.reports-status-card small, .reports-status-label { color:var(--muted); display:block; }
|
||||
.reports-status-label { font-size:11px; text-transform:uppercase; letter-spacing:.16em; }
|
||||
.reports-toolbar { display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap; }
|
||||
.reports-tabset { display:flex; flex-wrap:wrap; gap:8px; }
|
||||
.reports-export-actions { display:flex; gap:8px; flex-wrap:wrap; }
|
||||
.reports-tab { padding:10px 14px; border-radius:999px; border:1px solid rgba(255,255,255,.08); color:var(--muted); text-decoration:none; font-size:12px; letter-spacing:.16em; text-transform:uppercase; background:rgba(255,255,255,.02); }
|
||||
.reports-tab.is-active { color:#0b1015; background:linear-gradient(135deg, #48d3ff, #31f0a8); border-color:transparent; box-shadow:0 12px 30px rgba(49,240,168,.18); }
|
||||
.reports-range-bar { display:grid; grid-template-columns:minmax(220px,.9fr) minmax(0,2.1fr); gap:16px; align-items:start; padding:16px; border-radius:18px; border:1px solid rgba(255,255,255,.08); background:linear-gradient(180deg, rgba(255,255,255,.035), rgba(255,255,255,.012)); }
|
||||
.reports-range-copy { display:grid; gap:4px; }
|
||||
.reports-range-label { color:var(--muted); font-size:11px; letter-spacing:.16em; text-transform:uppercase; }
|
||||
.reports-range-copy strong { font-size:22px; line-height:1.1; }
|
||||
.reports-range-copy small { color:var(--muted); }
|
||||
.reports-range-chips { display:flex; gap:8px; flex-wrap:wrap; justify-content:flex-start; align-content:flex-start; }
|
||||
.reports-chip { padding:9px 12px; border-radius:999px; border:1px solid rgba(255,255,255,.08); color:var(--muted); text-decoration:none; font-size:11px; letter-spacing:.12em; text-transform:uppercase; background:rgba(255,255,255,.02); }
|
||||
.reports-chip.is-active { color:#edf6ff; border-color:rgba(72,211,255,.28); background:rgba(72,211,255,.12); }
|
||||
.reports-filter-bar { display:grid; grid-template-columns:minmax(280px,1.6fr) repeat(3, minmax(160px, .8fr)) auto; gap:12px; align-items:end; padding:16px; border-radius:18px; border:1px solid rgba(255,255,255,.08); background:linear-gradient(180deg, rgba(255,255,255,.04), rgba(255,255,255,.015)); }
|
||||
.reports-filter-field { display:grid; gap:8px; }
|
||||
.reports-filter-field span { color:var(--muted); font-size:11px; text-transform:uppercase; letter-spacing:.14em; }
|
||||
.reports-filter-actions { display:flex; gap:8px; align-items:center; justify-content:flex-end; flex-wrap:wrap; }
|
||||
.reports-kpi-grid { display:grid; grid-template-columns:repeat(7, minmax(0,1fr)); gap:12px; }
|
||||
.reports-kpi-grid-minimal { grid-template-columns:repeat(2, minmax(0,1fr)); }
|
||||
.reports-metric { padding:18px; border-radius:18px; border:1px solid rgba(255,255,255,.08); background:linear-gradient(180deg, rgba(255,255,255,.045), rgba(255,255,255,.015)); display:grid; gap:10px; }
|
||||
.reports-metric span { color:var(--muted); font-size:11px; letter-spacing:.16em; text-transform:uppercase; }
|
||||
.reports-metric strong { font-size:28px; line-height:1; }
|
||||
.reports-metric small { color:var(--muted); font-size:12px; }
|
||||
.reports-metric-primary strong { font-size:32px; }
|
||||
.reports-metric-focus { min-height:150px; }
|
||||
.reports-showcase-grid { display:grid; grid-template-columns:1fr 1fr; gap:14px; }
|
||||
.reports-panel { padding:18px; border-radius:20px; border:1px solid rgba(255,255,255,.08); background:linear-gradient(180deg, rgba(255,255,255,.04), rgba(255,255,255,.015)); }
|
||||
.reports-panel-head { display:flex; align-items:flex-start; justify-content:space-between; gap:12px; margin-bottom:14px; }
|
||||
.reports-panel-head h2 { margin:0; font-size:22px; }
|
||||
.reports-panel-head p { margin:6px 0 0; color:var(--muted); max-width:560px; }
|
||||
.reports-panel-head span { color:var(--muted); font-size:11px; letter-spacing:.16em; text-transform:uppercase; white-space:nowrap; }
|
||||
.reports-panel-head-large { margin-bottom:18px; }
|
||||
.reports-ranking-list, .reports-ledger-list { display:grid; gap:10px; }
|
||||
.reports-ranking-row { display:grid; grid-template-columns:46px minmax(0,1fr) auto; gap:14px; align-items:center; padding:14px 16px; border-radius:16px; border:1px solid rgba(255,255,255,.07); background:rgba(255,255,255,.025); }
|
||||
.reports-rank { width:46px; height:46px; border-radius:14px; display:grid; place-items:center; font-size:20px; font-weight:800; color:#d8eef9; background:linear-gradient(180deg, rgba(72,211,255,.18), rgba(49,240,168,.08)); border:1px solid rgba(72,211,255,.18); }
|
||||
.reports-ranking-copy { display:grid; gap:4px; min-width:0; }
|
||||
.reports-ranking-copy strong { font-size:16px; }
|
||||
.reports-ranking-copy span { color:var(--muted); font-size:13px; }
|
||||
.reports-ranking-value { font-size:20px; font-weight:800; white-space:nowrap; }
|
||||
.reports-ledger-panel { display:grid; gap:12px; }
|
||||
.reports-ledger-row { display:grid; grid-template-columns:minmax(220px,1.15fr) minmax(0,2.2fr); gap:14px; align-items:start; padding:16px; border-radius:18px; border:1px solid rgba(255,255,255,.07); background:linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,.012)); }
|
||||
.reports-ledger-row-track { grid-template-columns:minmax(280px,1.25fr) minmax(0,2.3fr); }
|
||||
.reports-ledger-main { display:grid; gap:5px; align-content:center; min-width:0; }
|
||||
.reports-ledger-main strong { font-size:18px; }
|
||||
.reports-ledger-main span { color:var(--muted); font-size:13px; }
|
||||
.reports-ledger-metrics { display:grid; grid-template-columns:repeat(4, minmax(0,1fr)); gap:10px; }
|
||||
.reports-stat-chip, .reports-money-stack { padding:12px 14px; border-radius:14px; background:rgba(255,255,255,.03); border:1px solid rgba(255,255,255,.05); display:grid; gap:6px; align-content:center; }
|
||||
.reports-stat-chip small, .reports-money-stack small { color:var(--muted); font-size:10px; letter-spacing:.14em; text-transform:uppercase; }
|
||||
.reports-stat-chip strong, .reports-money-stack strong { font-size:20px; line-height:1; }
|
||||
.reports-money-stack.emphasis { background:linear-gradient(180deg, rgba(72,211,255,.12), rgba(49,240,168,.08)); border-color:rgba(72,211,255,.18); }
|
||||
.reports-stat-chip.muted, .reports-money-stack.muted { opacity:.78; }
|
||||
.reports-empty-state { padding:16px 18px; border-radius:16px; border:1px dashed rgba(255,255,255,.12); background:rgba(255,255,255,.015); display:grid; gap:6px; color:var(--muted); }
|
||||
.reports-empty-state strong { color:#edf6ff; font-size:15px; }
|
||||
.reports-empty-state.compact { min-height:132px; align-content:center; }
|
||||
@media (max-width: 1380px) {
|
||||
.reports-hero { grid-template-columns:1fr; }
|
||||
.reports-kpi-grid { grid-template-columns:repeat(3, minmax(0,1fr)); }
|
||||
.reports-range-bar { grid-template-columns:1fr; }
|
||||
.reports-filter-bar { grid-template-columns:repeat(2, minmax(0,1fr)); }
|
||||
.reports-filter-wide, .reports-filter-actions { grid-column:1 / -1; }
|
||||
.reports-filter-actions { justify-content:flex-start; }
|
||||
.reports-showcase-grid { grid-template-columns:1fr; }
|
||||
.reports-ledger-row, .reports-ledger-row-track { grid-template-columns:1fr; }
|
||||
}
|
||||
@media (max-width: 920px) {
|
||||
.reports-kpi-grid { grid-template-columns:repeat(2, minmax(0,1fr)); }
|
||||
.reports-ranking-row { grid-template-columns:40px minmax(0,1fr); }
|
||||
.reports-ranking-value { grid-column:2; }
|
||||
.reports-ledger-metrics { grid-template-columns:repeat(2, minmax(0,1fr)); }
|
||||
}
|
||||
@media (max-width: 640px) {
|
||||
.reports-kpi-grid, .reports-filter-bar, .reports-ledger-row, .reports-ledger-row-track, .reports-ledger-metrics { grid-template-columns:1fr; }
|
||||
.reports-toolbar { align-items:flex-start; }
|
||||
.reports-range-chips, .reports-export-actions { width:100%; }
|
||||
.reports-ranking-row { grid-template-columns:1fr; }
|
||||
.reports-rank { width:38px; height:38px; }
|
||||
.reports-ranking-value { grid-column:auto; }
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
$content = ob_get_clean();
|
||||
require __DIR__ . '/../../../../modules/admin/views/layout.php';
|
||||
Reference in New Issue
Block a user