diff --git a/app/Http/Controllers/AnalysisController.php b/app/Http/Controllers/AnalysisController.php index 5c0f78d..62e7ac9 100644 --- a/app/Http/Controllers/AnalysisController.php +++ b/app/Http/Controllers/AnalysisController.php @@ -17,14 +17,14 @@ class AnalysisController extends Controller public function __invoke() { $stats = [ - 'total_sales' => $this->getStats(fn() => Order::count()), - 'total_revenue' => $this->getStats(fn() => Order::sum('total')), - 'total_expenses' => $this->getStats(fn() => Expense::sum('amount')), - 'cogs' => $this->getStats(fn() => Order::sum('hpp')), - 'total_discount' => $this->getStats(fn() => Order::sum('discount')), - 'total_purchases' => $this->getStats(fn() => Purchase::sum('total')), - 'products_sold' => $this->getStats(fn() => OrderItem::sum('qty')), - 'total_customers' => $this->getStats(fn() => Order::distinct('customer_name')->count('customer_name')), + 'total_sales' => $this->getStats(fn () => Order::count()), + 'total_revenue' => $this->getStats(fn () => Order::sum('total')), + 'total_expenses' => $this->getStats(fn () => Expense::sum('amount')), + 'cogs' => $this->getStats(fn () => Order::sum('hpp')), + 'total_discount' => $this->getStats(fn () => Order::sum('discount')), + 'total_purchases' => $this->getStats(fn () => Purchase::sum('total')), + 'products_sold' => $this->getStats(fn () => OrderItem::sum('qty')), + 'total_customers' => $this->getStats(fn () => Order::distinct('customer_name')->count('customer_name')), ]; $stats['aov'] = $this->calculateAov($stats['total_revenue'], $stats['total_sales']); @@ -114,7 +114,7 @@ public function __invoke() ->limit(5) ->get(); - $ordersByHourRaw = DB::table('orders') + $salesByHourRaw = DB::table('orders') ->select( DB::raw('HOUR(created_at) as hour'), DB::raw('COUNT(*) as total') @@ -122,11 +122,11 @@ public function __invoke() ->groupBy(DB::raw('HOUR(created_at)')) ->get(); - $ordersByHour = collect(range(0, 23))->map(function ($hour) use ($ordersByHourRaw) { - $found = $ordersByHourRaw->firstWhere('hour', $hour); + $salesByHour = collect(range(0, 23))->map(function ($hour) use ($salesByHourRaw) { + $found = $salesByHourRaw->firstWhere('hour', $hour); return [ - 'hour' => str_pad($hour, 2, '0', STR_PAD_LEFT) . ':00', + 'hour' => str_pad($hour, 2, '0', STR_PAD_LEFT).':00', 'total' => $found ? (int) $found->total : 0, ]; }); @@ -134,7 +134,7 @@ public function __invoke() return Inertia::render('analysis', [ 'stats' => $stats, 'salesByMonth' => $salesByMonth, - 'ordersByHour' => $ordersByHour, + 'salesByHour' => $salesByHour, 'paymentMethods' => $paymentMethods, 'orderStatuses' => $orderStatuses, 'orderChannels' => $orderChannels, diff --git a/resources/js/components/charts/orders-by-hour-chart.tsx b/resources/js/components/charts/orders-by-hour-chart.tsx deleted file mode 100644 index 1508dd4..0000000 --- a/resources/js/components/charts/orders-by-hour-chart.tsx +++ /dev/null @@ -1,84 +0,0 @@ -"use client" - -import { Area, AreaChart, CartesianGrid, XAxis } from "recharts"; - -import { - Card, - CardContent, - CardDescription, - CardHeader, - CardTitle, -} from "@/components/ui/card"; -import type { ChartConfig } from "@/components/ui/chart"; -import { - ChartContainer, - ChartLegend, - ChartLegendContent, - ChartTooltip, - ChartTooltipContent -} from "@/components/ui/chart"; - -export const description = "Menampilkan aktivitas toko berdasarkan pesanan yang masuk per jam." - -const chartConfig = { - total: { - label: "Total Pesanan", - color: "var(--chart-1)", - }, -} satisfies ChartConfig - -export function OrdersByHourChart({ data }: { data: any[] }) { - return ( - - -
- Jam Sibuk - - Menampilkan aktivitas toko berdasarkan pesanan yang masuk per jam. - -
-
- - - - - - - - - - - - } - /> - - } /> - - - -
- ) -} diff --git a/resources/js/components/charts/sales-by-hour-chart.tsx b/resources/js/components/charts/sales-by-hour-chart.tsx index 2c505d2..c8fe01f 100644 --- a/resources/js/components/charts/sales-by-hour-chart.tsx +++ b/resources/js/components/charts/sales-by-hour-chart.tsx @@ -17,11 +17,9 @@ import { ChartTooltipContent } from "@/components/ui/chart" -import type {ChartConfig} from "@/components/ui/chart"; +import type { ChartConfig } from "@/components/ui/chart"; -export const description = "Menampilkan aktivitas toko berdasarkan pesanan yang masuk." - -const chartConfig = { +const defaultChartConfig = { today: { label: "Hari Ini", color: "var(--chart-1)", @@ -32,48 +30,50 @@ const chartConfig = { }, } satisfies ChartConfig -export function SalesByHourChart({ data }: { data: any[] }) { +interface SalesByHourChartProps { + data: any[]; + config?: ChartConfig; + title?: string; + description?: string; +} + +export function SalesByHourChart({ + data, + config = defaultChartConfig, + title = "Jam Sibuk", + description = "Menampilkan aktivitas toko berdasarkan pesanan yang masuk per jam." +}: SalesByHourChartProps) { + const keys = Object.keys(config); + return (
- Jam Sibuk - - Menampilkan aktivitas toko berdasarkan pesanan yang masuk. - + {title} + {description}
- - - - - - - - + {keys.map((key) => ( + + + + + ))} } /> - - + {/* Render Areas in reverse order so first key in config is on top */} + {[...keys].reverse().map((key) => ( + + ))} } /> diff --git a/resources/js/pages/analysis.tsx b/resources/js/pages/analysis.tsx index 3c7fb18..ac6d99b 100644 --- a/resources/js/pages/analysis.tsx +++ b/resources/js/pages/analysis.tsx @@ -4,11 +4,11 @@ import type { AnalysisPageProps } from '@/types'; import { Head, usePage } from '@inertiajs/react'; import { CustomBarChart } from '../components/charts/bar-chart'; import { CustomPieChart } from '../components/charts/pie-chart'; -import { OrdersByHourChart } from '../components/charts/orders-by-hour-chart'; +import { SalesByHourChart } from '../components/charts/sales-by-hour-chart'; import { RevenueVsPurchasesChart } from '../components/charts/revenue-vs-purchases-chart'; export default function Analisa() { - const { stats, salesByMonth, ordersByHour, paymentMethods, orderStatuses, orderChannels, topProducts, topCustomers } = + const { stats, salesByMonth, salesByHour, paymentMethods, orderStatuses, orderChannels, topProducts, topCustomers } = usePage().props; return ( @@ -51,7 +51,15 @@ export default function Analisa() { - +