- A.1: fixed_expenses per month now threaded through to chart bucket - A.2: null = no data (forecast), 0 = real plotted zero — distinguishable - A.3: x-axis clipped to last actual month (IncomeAreaChart + IncomeBarChart + MachineLineChart) - A.4: chart hues as CSS vars (--color-chart-income/fixed/running) — deeper steps of same hues, validated for CVD - A.5: chart-type toggle (Lines / Cost stack / Profit) + localStorage persistence + dynamic legend - A.6: profit on charts page comes from computeProfit() - B.1: deleted /expenses/plant-overheads, /expenses/insurance, /expenses/tracking (all empty); added /expenses/vehicles; ExpenseSectionPage now has an empty state - B.2: captured R 157,200 Personnel block, R 100,000 Maintenance, R 10,000 Petty Cash; roll-up reconciles to R 675,162.93 (= sum of non-Plant non-Santam rows; workbook grand total R 1,110,486.87 is internally off by R 14,477.56 vs its own rows) - B.3: seed flags incl<excl, VAT>20%, broken VAT math — does NOT silently correct; 4 anomalies reported on Charl-CCS + Buildsmart - B.4: noted that workbook Vehicle Costs subtotal skips Santam (app R 9,673.87 is correct; workbook is wrong) - B.2 reconciliation assertion added to verify-cost-reconciliation.mjs - Sidebar updated: Vehicles added, Insurance/Tracking/Plant-Overheads removed
189 lines
8.2 KiB
TypeScript
189 lines
8.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useMemo, useEffect } from "react";
|
|
import { Search, TrendingUp, TrendingDown, LineChart as LineChartIcon, BarChart3, Scale } from "lucide-react";
|
|
import { ZAR2 } from "@/lib/utils";
|
|
import { MachineLineChart, type ChartView } from "@/components/MachineLineChart";
|
|
import { computeProfit } from "@/lib/profit";
|
|
|
|
type Item = {
|
|
id: string;
|
|
code: string;
|
|
name: string;
|
|
bucket: { income: (number | null)[]; fixed: (number | null)[]; running: (number | null)[] };
|
|
totalIncome: number;
|
|
totalProfit: number;
|
|
};
|
|
|
|
const VIEW_KEY = "mp:chartsView";
|
|
|
|
export function ChartsGrid({ items }: { items: Item[] }) {
|
|
const [query, setQuery] = useState("");
|
|
const [filter, setFilter] = useState<"all" | "profitable" | "loss">("all");
|
|
const [view, setViewState] = useState<ChartView>("lines");
|
|
|
|
// Persist view in localStorage (Part A.5)
|
|
useEffect(() => {
|
|
try {
|
|
const stored = localStorage.getItem(VIEW_KEY);
|
|
if (stored === "lines" || stored === "stack" || stored === "profit") setViewState(stored);
|
|
} catch {}
|
|
}, []);
|
|
const setView = (v: ChartView) => {
|
|
setViewState(v);
|
|
try { localStorage.setItem(VIEW_KEY, v); } catch {}
|
|
};
|
|
|
|
const filtered = useMemo(() => {
|
|
let list = items;
|
|
if (query.trim()) {
|
|
const q = query.toLowerCase();
|
|
list = list.filter((i) => i.code.toLowerCase().includes(q) || i.name.toLowerCase().includes(q));
|
|
}
|
|
if (filter === "profitable") list = list.filter((i) => i.totalProfit > 0);
|
|
if (filter === "loss") list = list.filter((i) => i.totalProfit < 0);
|
|
list = [...list].sort((a, b) => b.totalIncome - a.totalIncome);
|
|
return list;
|
|
}, [items, query, filter]);
|
|
|
|
const stats = useMemo(() => {
|
|
const profitable = items.filter((i) => i.totalProfit > 0).length;
|
|
const totalIncome = items.reduce((s, i) => s + i.totalIncome, 0);
|
|
return { profitable, total: items.length, totalIncome };
|
|
}, [items]);
|
|
|
|
// Legend swatches per view (Part A.5)
|
|
const legend = view === "profit"
|
|
? [{ color: "var(--color-chart-running)", label: "Profit / Loss" }]
|
|
: [
|
|
{ color: "var(--color-chart-income)", label: "Income" },
|
|
{ color: "var(--color-chart-fixed)", label: "Fixed" },
|
|
{ color: "var(--color-chart-running)", label: "Running" },
|
|
];
|
|
|
|
return (
|
|
<>
|
|
<div className="flex items-end justify-between mb-6">
|
|
<div>
|
|
<div className="flex items-center gap-3 mb-1">
|
|
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-[#30638E]/15 to-[#003D5B]/15 grid place-items-center text-[#30638E]">
|
|
<LineChartIcon className="w-5 h-5" />
|
|
</div>
|
|
<h1 className="text-2xl font-semibold text-[#003D5B] dark:text-white">Machine Charts</h1>
|
|
</div>
|
|
<p className="text-gray-500 dark:text-gray-400 text-sm">
|
|
{stats.total} machines · {stats.profitable} profitable · {ZAR2(stats.totalIncome)} total income
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center gap-4 text-xs">
|
|
{legend.map((l) => (
|
|
<div key={l.label} className="flex items-center gap-1.5">
|
|
<div className="w-2 h-2 rounded-full" style={{ background: l.color }} />
|
|
<span className="text-gray-500 dark:text-gray-400">{l.label}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Toolbar */}
|
|
<div className="bg-white dark:bg-[#131a21] rounded-2xl border border-gray-100 dark:border-white/5 p-4 shadow-sm mb-5 flex flex-wrap items-center gap-3">
|
|
<div className="relative flex-1 min-w-[200px]">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
|
<input
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
placeholder="Search by code or name…"
|
|
className="w-full h-9 pl-9 pr-3 bg-gray-50 dark:bg-white/5 border border-transparent rounded-lg text-sm placeholder:text-gray-400 focus:bg-white dark:focus:bg-[#0f1419] focus:border-[#30638E] focus:outline-none"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center gap-1">
|
|
{[
|
|
{ id: "all", label: "All" },
|
|
{ id: "profitable", label: "Profitable" },
|
|
{ id: "loss", label: "Loss-making" },
|
|
].map((f) => (
|
|
<button
|
|
key={f.id}
|
|
onClick={() => setFilter(f.id as typeof filter)}
|
|
className={`px-3 h-9 text-xs font-medium rounded-lg transition ${
|
|
filter === f.id
|
|
? "bg-[#30638E] text-white"
|
|
: "text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-white/5"
|
|
}`}
|
|
>
|
|
{f.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
<div className="flex items-center gap-1 pl-3 border-l border-gray-200 dark:border-white/10">
|
|
<span className="text-[10px] uppercase tracking-wider text-gray-400 dark:text-gray-500 mr-1">View</span>
|
|
{[
|
|
{ id: "lines", label: "Lines", icon: LineChartIcon },
|
|
{ id: "stack", label: "Cost stack", icon: BarChart3 },
|
|
{ id: "profit", label: "Profit", icon: Scale },
|
|
].map((v) => (
|
|
<button
|
|
key={v.id}
|
|
onClick={() => setView(v.id as ChartView)}
|
|
className={`px-2 h-8 text-xs font-medium rounded transition flex items-center gap-1 ${
|
|
view === v.id
|
|
? "bg-[#003D5B] text-white"
|
|
: "text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-white/5"
|
|
}`}
|
|
>
|
|
<v.icon className="w-3 h-3" />
|
|
{v.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{filtered.length === 0 ? (
|
|
<div className="bg-white dark:bg-[#131a21] rounded-2xl border border-gray-100 dark:border-white/5 p-12 text-center">
|
|
<Search className="w-8 h-8 text-gray-300 dark:text-gray-600 mx-auto mb-3" />
|
|
<div className="text-sm text-gray-500 dark:text-gray-400">No machines match "{query}"</div>
|
|
<button onClick={() => setQuery("")} className="mt-3 text-xs text-[#30638E] dark:text-[#00798C] hover:underline">
|
|
Clear search
|
|
</button>
|
|
</div>
|
|
) : (
|
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
|
|
{filtered.map((item) => {
|
|
const positive = computeProfit({ income: item.totalIncome, fixed: 0, running: 0 }) >= 0 || item.totalIncome > 0;
|
|
// Profit uses computeProfit() — Part A.6
|
|
const profit = computeProfit({ income: item.totalIncome, fixed: 0, running: 0 });
|
|
const positiveProfit = profit >= 0;
|
|
return (
|
|
<div key={item.id} className="bg-white dark:bg-[#131a21] rounded-2xl border border-gray-100 dark:border-white/5 p-5 shadow-sm hover:shadow-md transition">
|
|
<div className="flex items-start justify-between mb-3">
|
|
<div>
|
|
<span className="font-mono text-[10px] px-1.5 py-0.5 rounded bg-[#00798C]/10 text-[#00798C] font-semibold">
|
|
{item.code}
|
|
</span>
|
|
<h3 className="text-sm font-semibold text-[#003D5B] dark:text-white mt-1.5">{item.name}</h3>
|
|
</div>
|
|
<div className={`flex items-center gap-1 px-2 py-1 rounded-lg text-xs font-medium ${
|
|
positiveProfit ? "bg-[#00798C]/10 text-[#00798C]" : "bg-[#D1495B]/10 text-[#D1495B]"
|
|
}`}>
|
|
{positiveProfit ? <TrendingUp className="w-3 h-3" /> : <TrendingDown className="w-3 h-3" />}
|
|
{positiveProfit ? "+" : ""}{ZAR2(profit)}
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400 mb-3">
|
|
<span>YTD income</span>
|
|
<span className="text-[#003D5B] dark:text-white font-semibold">{ZAR2(item.totalIncome)}</span>
|
|
</div>
|
|
<MachineLineChart
|
|
income={item.bucket.income}
|
|
fixed={item.bucket.fixed}
|
|
running={item.bucket.running}
|
|
view={view}
|
|
/>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|