/* eslint-disable */
/* © 2026 K2B NETWORK SOLUTIONS LLC. All rights reserved.
Proprietary and confidential. Unauthorized copying, distribution, or use of
this file, via any medium, is strictly prohibited. Original work product. */
// JBREWS DMS — Policy / Goodwill Control. After-sale repairs the store eats
// must attach to a stock #, deal/customer, reason, and MANAGER approval before
// anything posts. Approving books to policy expense (609) or, if tied to the
// unit, used-vehicle policy adjustment (5015). Reporting rolls up by reason,
// salesperson, vehicle source, and technician. Reads DMS.policyAdjustments.
const pgMoney = (n) => "$" + (Number(n) || 0).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
const PG_TONE = { Pending: { bg: "#FBF3E0", fg: "#9A6700" }, Approved: { bg: "#E6F7EC", fg: "#0E7A37" }, Denied: { bg: "#FBE9E7", fg: "#B23A2E" } };
const PolicyControl = ({ onClose, go }) => {
const D = window.DMS;
const [, force] = React.useReducer(n => n + 1, 0);
React.useEffect(() => (D && D.subscribe) ? D.subscribe(force) : undefined, []);
const act = (r) => { window.k2bToast && window.k2bToast(r.msg, r.ok ? "ok" : "warn", r.ok ? 3200 : 9000); return r; };
const items = (D && D.policyAdjustments) ? D.policyAdjustments() : [];
const [modal, setModal] = React.useState(false);
const [tab, setTab] = React.useState("queue"); // queue | report
// RC24 (svcm): Approve is a server command (POST /api/fixedops/policy) — the Service Manager and the office, within the owner's limit
const [cfg, setCfg] = React.useState(null);
const [busy, setBusy] = React.useState(null);
React.useEffect(() => { let live = true; if (D && D.svcmConfig) D.svcmConfig().then((c) => { if (live) setCfg(c); }); return () => { live = false; }; }, []);
const canApprove = cfg && cfg.may ? !!cfg.may.policy : !!(D && D.fopsCanPolicy && D.fopsCanPolicy());
const limitCents = cfg && cfg.policyLimitCents ? cfg.policyLimitCents : 0;
const approve = async (p) => {
if (!window.confirm(`Approve ${p.id} — ${pgMoney(p.amount)} to ${p.tieToUnit ? "5015 (used-vehicle policy — unit cost)" : "0675 (policy work — service)"} for ${p.stock || p.customer || p.ro || p.deal}?\nThis posts the goodwill to the books now (the server does it in one step).${limitCents && p.amount * 100 > limitCents ? "\nIt is above the owner's limit of " + pgMoney(limitCents / 100) + " — the owner will have to override." : ""}`)) return;
setBusy(p.id);
const r = await D.approvePolicyAdjustment(p.id);
setBusy(null);
act(r);
};
const pending = items.filter(p => p.status === "Pending");
const approved = items.filter(p => p.status === "Approved");
const ytdExpense = approved.reduce((s, p) => s + p.amount, 0);
return (
{[["Pending Approval", pending.length, pending.length ? "var(--jb-amber, #9A6700)" : "var(--jb-green)"],
["Approved (count)", approved.length, "var(--jb-navy)"],
["Policy Expense", pgMoney(ytdExpense), "var(--k2b-maroon)"]].map(([l, v, c], i) => (
))}
{cfg && cfg.ok === false ? "" : limitCents ? `Approvals post to the books on the server. Above ${pgMoney(limitCents / 100)} the owner must override (Owner panel).` : "Approvals post to the books on the server. No goodwill limit is set (Owner panel › Service goodwill limit)."}
setTab("queue")} style={tab === "queue" ? { background: "var(--jb-navy)", color: "#fff", fontWeight: 700 } : {}}>Queue
setTab("report")} style={tab === "report" ? { background: "var(--jb-navy)", color: "#fff", fontWeight: 700 } : {}}>Report
setModal(true)}>+ Request Policy
{tab === "queue" ? (
{items.length === 0 ? (
No policy / goodwill requests. Use “+ Request Policy” to log an after-sale repair the store is covering.
) : (
{["Policy", "Vehicle / Customer", "Reason", "Amount", "Tie", "Status", ""].map((h, i) => {h} )}
{items.map((p, i) => { const t = PG_TONE[p.status] || PG_TONE.Pending; return (
{p.id}
{p.stock || p.customer || "—"}
{[p.deal, p.ro, p.salesperson].filter(Boolean).join(" · ")}
{p.reason}{p.complaint ? {p.complaint}
: null}
{pgMoney(p.amount)}
{p.tieToUnit ? "unit cost" : "expense"}
{p.status} {p.approvedBy ? {p.approvedBy}
: null}
{p.status === "Pending" ? (
<>
{canApprove && approve(p)}>{busy === p.id ? "Posting…" : "Approve"} }
{canApprove && act(D.denyPolicyAdjustment(p.id))}>Deny }
{!canApprove && awaiting the Service Manager / office }
>
) : {p.jeId ? {p.jeId} : "—"} }
); })}
)}
) : (
)}
ESC-Close
go && go("servicedept")}>Service
go && go("accounting")}>General Ledger
{modal &&
setModal(false)} />}
);
};
const PgReport = ({ items }) => {
const group = (key) => {
const m = {};
items.forEach(p => { const k = p[key] || "—"; m[k] = (m[k] || 0) + p.amount; });
return Object.keys(m).map(k => ({ k, v: m[k] })).sort((a, b) => b.v - a.v);
};
const Block = ({ title, rows }) => (
{title}
{rows.length === 0 ?
No approved policy yet.
: rows.map((r, i) => (
{r.k} {pgMoney(r.v)}
))}
);
return (
);
};
const PgRequestModal = ({ act, onClose }) => {
const D = window.DMS;
const reasons = (D && D.POLICY_REASONS) || ["Other"];
const [f, setF] = React.useState({ stock: "", deal: "", customer: "", ro: "", amount: "", reason: reasons[0], complaint: "", tech: "", salesperson: "", source: "", weOwe: false, within7: false, soldAsIs: false, tieToUnit: false });
const set = (k, v) => setF(s => ({ ...s, [k]: v }));
// pull vehicle source from inventory when stock entered
React.useEffect(() => {
if (!f.stock || !D.inventory) return;
const u = D.inventory().find(x => String(x.stock).toUpperCase() === f.stock.toUpperCase());
if (u && !f.source) setF(s => ({ ...s, source: u.acqSource || u.source || "" }));
}, [f.stock]);
const amt = parseFloat(String(f.amount).replace(/[^0-9.]/g, "")) || 0;
const submit = () => { const r = D.addPolicyAdjustment({ ...f, amount: amt }); act(r); if (r.ok) onClose(); };
const inp = { width: "100%", boxSizing: "border-box", padding: "7px 9px", border: "1px solid #C8CCD2", borderRadius: 4, fontSize: 13 };
return (
Cancel Submit for Approval >}>
Complaint / notes
set("complaint", e.target.value)} style={inp} />
{[["weOwe", "On signed We-Owe"], ["within7", "Within 7 days of sale"], ["soldAsIs", "Sold AS-IS"], ["tieToUnit", "Capitalize into unit cost"]].map(([k, lab]) => (
set(k, e.target.checked)} style={{ width: 15, height: 15 }} />{lab}
))}
On approval the server posts DR {f.tieToUnit ? "5015 used-vehicle policy adjustment" : "0675 policy work — service"} · CR 460A customer labor . The Service Manager or the office must approve (within the owner's limit) before anything posts.
);
};
const pgInpStyle = { width: "100%", boxSizing: "border-box", padding: "7px 9px", border: "1px solid #C8CCD2", borderRadius: 4, fontSize: 13 };
const PgFld = ({ label, k, mono, full, f, set }) => (
{label}
set(k, mono ? e.target.value.toUpperCase() : e.target.value)} style={{ ...pgInpStyle, fontFamily: mono ? "var(--jb-mono)" : undefined }} />
);
Object.assign(window, { PolicyControl });