Update Fix
This commit is contained in:
169
resources/js/admin/tailadmin/pages/Pos/LaporanBulanan.tsx
Normal file
169
resources/js/admin/tailadmin/pages/Pos/LaporanBulanan.tsx
Normal file
@@ -0,0 +1,169 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import PageMeta from "../../components/common/PageMeta";
|
||||
import { apiGet } from "../../lib/api";
|
||||
import { useToastOnError } from "../../hooks/useToastOnError";
|
||||
|
||||
type Row = {
|
||||
bulan: string;
|
||||
transaksi: number;
|
||||
pendapatan: number;
|
||||
item_terjual: number;
|
||||
};
|
||||
|
||||
type Summary = {
|
||||
pendapatan: number;
|
||||
transaksi: number;
|
||||
item_terjual: number;
|
||||
};
|
||||
|
||||
export default function LaporanBulanan() {
|
||||
const thisYear = new Date().getFullYear();
|
||||
const [year, setYear] = useState(String(thisYear));
|
||||
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/bulanan?year=${year}`
|
||||
);
|
||||
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 Bulanan" description="Ringkasan pendapatan bulanan" />
|
||||
<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 Bulanan
|
||||
</h1>
|
||||
<div className="flex flex-col gap-2 sm:flex-row">
|
||||
<input
|
||||
type="number"
|
||||
value={year}
|
||||
onChange={(e) => setYear(e.target.value)}
|
||||
className="h-10 w-[140px] rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-900"
|
||||
min={2000}
|
||||
max={2100}
|
||||
/>
|
||||
<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/bulanan/export?year=${encodeURIComponent(
|
||||
year
|
||||
)}`;
|
||||
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">Bulan</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.bulan} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.bulan}
|
||||
</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