Update Fix
This commit is contained in:
179
resources/js/admin/tailadmin/pages/Pos/LaporanHarian.tsx
Normal file
179
resources/js/admin/tailadmin/pages/Pos/LaporanHarian.tsx
Normal file
@@ -0,0 +1,179 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import PageMeta from "../../components/common/PageMeta";
|
||||
import { apiGet } from "../../lib/api";
|
||||
import { useToastOnError } from "../../hooks/useToastOnError";
|
||||
|
||||
type Row = {
|
||||
tanggal: string;
|
||||
transaksi: number;
|
||||
pendapatan: number;
|
||||
item_terjual: number;
|
||||
};
|
||||
|
||||
type Summary = {
|
||||
pendapatan: number;
|
||||
transaksi: number;
|
||||
item_terjual: number;
|
||||
};
|
||||
|
||||
export default function LaporanHarian() {
|
||||
const today = new Date();
|
||||
const toDefault = today.toISOString().slice(0, 10);
|
||||
const fromDefault = new Date(today.getTime() - 14 * 86400000)
|
||||
.toISOString()
|
||||
.slice(0, 10);
|
||||
|
||||
const [from, setFrom] = useState(fromDefault);
|
||||
const [to, setTo] = useState(toDefault);
|
||||
const [rows, setRows] = useState<Row[]>([]);
|
||||
const [summary, setSummary] = useState<Summary>({
|
||||
pendapatan: 0,
|
||||
transaksi: 0,
|
||||
item_terjual: 0,
|
||||
});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useToastOnError(error);
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await apiGet<{ data: Row[]; summary: Summary }>(
|
||||
`/admin-api/laporan/harian?from=${from}&to=${to}`
|
||||
);
|
||||
setRows(res.data);
|
||||
setSummary(res.summary);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Gagal memuat laporan");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageMeta title="Laporan Harian" description="Ringkasan pendapatan harian" />
|
||||
<div className="space-y-4">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<h1 className="text-2xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Laporan Harian
|
||||
</h1>
|
||||
<div className="flex flex-col gap-2 sm:flex-row">
|
||||
<input
|
||||
type="date"
|
||||
value={from}
|
||||
onChange={(e) => setFrom(e.target.value)}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-900"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={to}
|
||||
onChange={(e) => setTo(e.target.value)}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-900"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={load}
|
||||
className="h-10 rounded-xl bg-blue-600 px-4 text-sm font-semibold text-white hover:bg-blue-700"
|
||||
>
|
||||
Tampilkan
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
const url = `/admin-api/laporan/harian/export?from=${encodeURIComponent(
|
||||
from
|
||||
)}&to=${encodeURIComponent(to)}`;
|
||||
window.location.href = url;
|
||||
}}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-900"
|
||||
>
|
||||
Export Excel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="rounded-xl bg-red-50 px-4 py-3 text-sm text-red-700 dark:bg-red-500/10 dark:text-red-300">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid gap-3 sm:grid-cols-3">
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Pendapatan</div>
|
||||
<div className="mt-1 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
Rp {summary.pendapatan.toLocaleString("id-ID")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Transaksi</div>
|
||||
<div className="mt-1 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
{summary.transaksi}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-xs text-gray-500">Item Terjual</div>
|
||||
<div className="mt-1 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
{summary.item_terjual}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
{loading ? (
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Memuat...</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Tanggal</th>
|
||||
<th className="py-2 pr-4">Transaksi</th>
|
||||
<th className="py-2 pr-4">Item</th>
|
||||
<th className="py-2 pr-4">Pendapatan</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((r) => (
|
||||
<tr key={r.tanggal} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.tanggal}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.transaksi}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.item_terjual}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-800 dark:text-white/90">
|
||||
Rp {r.pendapatan.toLocaleString("id-ID")}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{rows.length === 0 && !loading && (
|
||||
<tr>
|
||||
<td
|
||||
colSpan={4}
|
||||
className="py-6 text-center text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
Tidak ada data.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user