/* Copyright © 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 — Buyer's Order (BO). // Clean sectioned layout: Buyer + Trade | Vehicle + Pricing + Protection + // Balance | actions. All fields are real inputs that persist to localStorage; // the Desking Tool pushes figures here (incl. the selected scenario's // warranty / GAP products with their codes) via saveJbForm("buyersOrder"). const BO_HELP = `Buyer's Order (BO)\n\nESC Finished — return to previous screen\nF1 Help\nF2 Customer — open Deal Review\nF3 Vehicle — open Stock-In\nF4 Trade — pull latest appraisal\nF5 Recalculate balance\nF6 Print order\nF7 Finance Deal (the deal pushed from the desk)\nF8 Save`; const BO_FORM = "buyersOrder"; /* section head with accent bar */ const BOHead = ({ children, first }) => (
{children}
); /* aligned label/field row */ const BORow = ({ label, children }) => (
{label} {children}
); /* right-aligned money row for the pricing block */ const BOMoney = ({ label, field, sel, strong }) => (
{label}
); const BuyersOrder = ({ onClose, go }) => { // `tick` keys the form body so JBFields remount and re-read persisted // values after programmatic loads (F4-Trade, desk push, F5-Recalc). const [tick, rerender] = React.useReducer(x => x + 1, 0); const d = window.loadJbForm ? window.loadJbForm(BO_FORM) : {}; // B-08: a LEASE has no "balance to finance" — the customer owes the amount // due at signing and a monthly payment (incl. tax); both come from the desk's // lease engine push (dueAtSigning / monthlyPayment / leaseTerm). const isLease = /lease/i.test(String(d.dealType || "")); const boNum = (f, k) => parseFloat(String(f[k] ?? "").replace(/[^0-9.\-]/g, "")) || 0; const bo$ = (n) => "$" + (+n || 0).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }); // Protection products: plan 1 = the legacy serviceContract fields; plan2..plan5 // (Code / Name / Price) are pushed by the desk alongside the JSON `products`. const boPlanPrice = (f, n) => n === 1 ? boNum(f, "serviceContract") : boNum(f, `plan${n}Price`); const boPlansTotal = (f) => { let t = 0; for (let n = 1; n <= 5; n++) t += boPlanPrice(f, n); return t; }; const computeBalance = () => { const f = window.loadJbForm(BO_FORM); const num = (k) => boNum(f, k); const gross = num("salePrice") + num("docFee") + num("license") + num("salesTax") + boPlansTotal(f) + num("gapIns") + num("lenderFeeCust"); // RC27 (dsk): + a lender fee the purchaser pays (desk › Profit panel) const credits = num("tradeAllow") + num("rebate") + num("cashDown"); return Math.max(0, gross - credits + num("tradePayoff")); }; const balanceFmt = bo$(computeBalance()); const negEquity = Math.max(0, boNum(d, "tradePayoff") - boNum(d, "tradeAllow")); const leaseDue = isLease ? boNum(d, "dueAtSigning") : 0; const leaseMonthly = isLease ? boNum(d, "monthlyPayment") : 0; // K2B-003 round 2 (D3): the CASH deal open in the DMS for this order (same stock #) — its balance is the Cash Deal's: // total due less the payments actually recorded. A desk "cash down" is only proposed until it is paid. const cashBO = !isLease && window.k2bBoCashDeal ? window.k2bBoCashDeal(d) : null; const printNow = () => { if (!window.k2bPrintBuyersOrder) { window.k2bToast && window.k2bToast("Print module not ready. Reload page.", "warn"); return; } window.k2bPrintBuyersOrder(); window.k2bToast && window.k2bToast("Print dialog opened — Buyer's Order sent to printer.", "ok"); }; const saveNow = () => { window.saveJbForm(BO_FORM, { orderNum: d.orderNum || ("BO-" + String(Date.now()).slice(-7)), savedAt: new Date().toISOString() }); window.k2bToast && window.k2bToast("Buyer's Order saved to file. Available from Deal Review.", "ok"); rerender(); }; const loadTrade = () => { const ap = (window.DMS && window.DMS.appraisals && window.DMS.appraisals()[0]) || null; if (!ap) { window.k2bToast && window.k2bToast("No saved appraisals — run Trade Appraisal first.", "warn"); return; } const f2 = (n) => (+n || 0).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }); window.saveJbForm(BO_FORM, { tradeVin: (ap.vin || "").toUpperCase(), tradeYrMkMdl: [ap.year, ap.make, ap.model].filter(Boolean).join(" ").toUpperCase(), tradeMiles: String(ap.mileage || ""), tradeAllow: f2(ap.allowance), tradePayoff: f2(ap.payoff), ...(ap.lender || ap.payoffLender || ap.lienholder ? { tradeLender: String(ap.lender || ap.payoffLender || ap.lienholder).toUpperCase(), tradeLien: String(ap.lender || ap.payoffLender || ap.lienholder).toUpperCase() } : {}), negEquity: f2(Math.max(0, (+ap.payoff || 0) - (+ap.allowance || 0))), }); rerender(); window.k2bToast && window.k2bToast(`Trade loaded from appraisal — ${[ap.year, ap.make, ap.model].filter(Boolean).join(" ")} · allowance $${f2(ap.allowance)}.`, "ok", 3200); }; const card = { background: "var(--jb-surface, #FFF)", border: "1px solid var(--jb-border, #D5D9DF)", borderRadius: 8, padding: "12px 14px" }; return ( // Grows to its content (the trade + protection blocks got taller) and scrolls // inside the viewport — never a fixed 680px box that hides the balance strip.
{/* context strip — where these figures came from */}
{d.orderNum || "Draft"} {d.scenario ? · Desked option {d.scenario}{d.amtFinanced ? <> · Amt financed ${d.amtFinanced} : null} : · Figures push in from the Desking Tool, or type directly.}
{d.savedAt ? saved {new Date(d.savedAt).toLocaleString()} : null}
{/* Body = [scrolling cards | fixed action column]. Overlap fix: the action column sits OUTSIDE the scroll area and never shrinks, so no field can slide under it; the two card columns wrap on narrow windows and every field shrinks to its card (bo-fields). */}
{/* LEFT — Buyer + Trade */}
Buyer
Trade In window.saveJbFormField && window.saveJbFormField(BO_FORM, "tradeLien", v)} /> {negEquity > 0 ? (
Negative equity (financed) {bo$(negEquity)}
) : (boNum(d, "tradeAllow") - boNum(d, "tradePayoff")) > 0 ? (
Trade equity (applied as down) {bo$(boNum(d, "tradeAllow") - boNum(d, "tradePayoff"))}
) : null}
{/* MIDDLE — Vehicle + Pricing + Protection + Balance */}
Vehicle Purchased go && go("inventory")}>Lookup Odometer
Pricing {/* RC27 (dsk): customer-facing lines the desk's Profit panel sends — never a cost the store pays */} {boNum(d, "downAssist") > 0 &&
of which: down payment assistance from the dealer{bo$(boNum(d, "downAssist"))}
} {boNum(d, "downAssist") > 0 &&
cash paid by the purchaser{bo$(Math.max(0, boNum(d, "cashDown") - boNum(d, "downAssist")))}
} {boNum(d, "lenderFeeCust") > 0 &&
Lender fee (paid by the purchaser){bo$(boNum(d, "lenderFeeCust"))}
}
Protection Products (from desked option{d.scenario ? " " + d.scenario : ""}) {/* Plan 1 = the legacy serviceContract / serviceContractCode fields; plans 2-3 (and 4-5 when the desk sent them) come as planNCode / planNName / planNPrice. Empty slots show a dash so it is obvious nothing was sold there. */}
{["", "Code", "Plan", ""].map((h, i) => {h})} {[1, 2, 3, 4, 5].filter(n => n <= 3 || boPlanPrice(d, n) || (n === 1 ? d.serviceContractCode : d[`plan${n}Code`])).map(n => { const codeField = n === 1 ? "serviceContractCode" : `plan${n}Code`; const priceField = n === 1 ? "serviceContract" : `plan${n}Price`; const has = !!(boPlanPrice(d, n) || d[codeField]); return ( Plan {n} {n === 1 ? : <> } ); })}
GAP Insurance
Plans {bo$(boPlansTotal(d))}+ GAP {bo$(boNum(d, "gapIns"))}= {bo$(boPlansTotal(d) + boNum(d, "gapIns"))}
{isLease ? (
LEASE Due at Signing {bo$(leaseDue)}
Monthly incl. tax {bo$(leaseMonthly)}{d.leaseTerm ? × {d.leaseTerm} : null} rerender()}>↻
) : cashBO ? (
Balance Due — cash deal total {bo$(cashBO.fig.totalDue)} − received {cashBO.fig.legacy ? "(not recorded)" : bo$(cashBO.fig.received)}
{bo$(cashBO.fig.balanceDue)} rerender()}>↻
) : (
Balance to Finance
{balanceFmt} rerender()}>↻
)}
{/* RIGHT — actions (own column; scrolls on short screens) */}
ESC-Finished alert(BO_HELP)}>F1-Help go && go("deal")}>F2-Customer go && go("stockin")}>F3-Vehicle F4-Trade rerender()}>F5-Recalculate 🖨 F6-Print Order { // ONE PATH: this only navigates to the deal already pushed from the desk. let id = null; try { id = localStorage.getItem("jb_fw_deal"); } catch (e) {} if (!id) { window.k2bToast && window.k2bToast("No deal open — start in the CRM and Desk It.", "warn", 3800); return; } go && go("finance"); }}>F7-Finance Deal ▸ F8-Save
go && go("forms")}>📋 All Forms… window.k2bPrintBillOfSale && window.k2bPrintBillOfSale()}>🖨 Bill of Sale window.k2bPrintOdometer && window.k2bPrintOdometer()}>🖨 Odometer window.k2bPrintPrivacy && window.k2bPrintPrivacy()}>🖨 Privacy
{ if (!window.confirm("Clear all fields on this Buyer's Order?")) return; try { const raw = localStorage.getItem("jbrews-form-data-v2"); const all = raw ? JSON.parse(raw) : {}; delete all[BO_FORM]; localStorage.setItem("jbrews-form-data-v2", JSON.stringify(all)); } catch (e) {} window.k2bToast && window.k2bToast("Buyer's Order cleared.", "info"); rerender(); }}>Clear All
); }; Object.assign(window, { BuyersOrder });