Schema: - New Company model (name, gstNumber, address, telephone, mobile, email, invoiceAddress, isActive) - PurchaseOrder.companyId FK (optional, SET NULL on company delete) - Migration: 20260530000003_add_company Admin: - /admin/companies page with full CRUD (create, edit, deactivate, delete) - Companies table shows name, GST, contact details, status - Companies link added to Admin section of sidebar (Briefcase icon) PO forms (new / edit / import / manager-edit): - Company dropdown appears at the top of Order Information when companies exist - Pre-populated with first active company; selection persisted to DB via companyId Import form: - parseSheet() now extracts companyName from Excel row 1 (col A) - Import preview auto-matches detected company name against known companies - Shows detected name as a hint; user can override before saving Export (PDF + XLSX): - Company constants (CO_NAME, CO_ADDR, CO_TEL, INV_ADDR, INV_GST) are now derived from the linked Company record when present, falling back to the original Pelagia Marine hardcoded defaults when no company is set Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
148 lines
6.1 KiB
TypeScript
148 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { AdminDialog } from "@/components/ui/admin-dialog";
|
|
import { createCompany, updateCompany } from "./actions";
|
|
|
|
type CompanyRow = {
|
|
id: string;
|
|
name: string;
|
|
gstNumber: string | null;
|
|
address: string | null;
|
|
telephone: string | null;
|
|
mobile: string | null;
|
|
email: string | null;
|
|
invoiceAddress: string | null;
|
|
isActive: boolean;
|
|
};
|
|
|
|
const INPUT = "w-full rounded-lg border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20";
|
|
const LABEL = "block text-xs font-medium text-neutral-700 mb-1";
|
|
|
|
function CompanyFormFields({ company }: { company?: CompanyRow }) {
|
|
return (
|
|
<div className="space-y-3">
|
|
<div>
|
|
<label className={LABEL}>Company Name *</label>
|
|
<input name="name" defaultValue={company?.name} required className={INPUT} placeholder="e.g. Pelagia Marine Services Pvt. Ltd." />
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div>
|
|
<label className={LABEL}>GST Number</label>
|
|
<input name="gstNumber" defaultValue={company?.gstNumber ?? ""} className={INPUT} placeholder="e.g. 27AAHCP5787B1Z6" />
|
|
</div>
|
|
<div>
|
|
<label className={LABEL}>Email</label>
|
|
<input name="email" type="email" defaultValue={company?.email ?? ""} className={INPUT} placeholder="accounts@company.com" />
|
|
</div>
|
|
<div>
|
|
<label className={LABEL}>Telephone</label>
|
|
<input name="telephone" defaultValue={company?.telephone ?? ""} className={INPUT} placeholder="+91-22-1234 5678" />
|
|
</div>
|
|
<div>
|
|
<label className={LABEL}>Mobile</label>
|
|
<input name="mobile" defaultValue={company?.mobile ?? ""} className={INPUT} placeholder="+91 98765 43210" />
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className={LABEL}>Address</label>
|
|
<textarea name="address" defaultValue={company?.address ?? ""} rows={2} className={INPUT} placeholder="Office address" />
|
|
</div>
|
|
<div>
|
|
<label className={LABEL}>Invoice Address <span className="font-normal text-neutral-400">(shown on exported POs)</span></label>
|
|
<textarea name="invoiceAddress" defaultValue={company?.invoiceAddress ?? ""} rows={2} className={INPUT} placeholder="Full address as it should appear on invoices/POs" />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function AddCompanyButton() {
|
|
const router = useRouter();
|
|
const [open, setOpen] = useState(false);
|
|
const [pending, setPending] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
e.preventDefault(); setPending(true); setError("");
|
|
const result = await createCompany(new FormData(e.currentTarget));
|
|
if ("error" in result) { setError(result.error); setPending(false); }
|
|
else { setPending(false); setOpen(false); router.refresh(); }
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<button onClick={() => setOpen(true)}
|
|
className="rounded-lg bg-primary-600 px-4 py-2.5 text-sm font-semibold text-white hover:bg-primary-700 transition-colors">
|
|
+ Add Company
|
|
</button>
|
|
<AdminDialog title="Add Company" open={open} onClose={() => setOpen(false)}>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<CompanyFormFields />
|
|
{error && <p className="text-sm text-danger-700 bg-danger-50 rounded-lg px-3 py-2">{error}</p>}
|
|
<div className="flex justify-end gap-3 pt-1">
|
|
<button type="button" onClick={() => setOpen(false)}
|
|
className="rounded-lg border border-neutral-300 px-4 py-2 text-sm font-medium text-neutral-700 hover:bg-neutral-50">Cancel</button>
|
|
<button type="submit" disabled={pending}
|
|
className="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700 disabled:opacity-60">
|
|
{pending ? "Creating…" : "Create Company"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</AdminDialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function EditCompanyButton({
|
|
company,
|
|
open: controlledOpen,
|
|
onOpenChange,
|
|
}: {
|
|
company: CompanyRow;
|
|
open?: boolean;
|
|
onOpenChange?: (v: boolean) => void;
|
|
}) {
|
|
const router = useRouter();
|
|
const [internalOpen, setInternalOpen] = useState(false);
|
|
const [pending, setPending] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
const isControlled = controlledOpen !== undefined;
|
|
const open = isControlled ? controlledOpen : internalOpen;
|
|
const setOpen = isControlled ? (onOpenChange ?? (() => {})) : setInternalOpen;
|
|
|
|
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
e.preventDefault(); setPending(true); setError("");
|
|
const fd = new FormData(e.currentTarget);
|
|
fd.set("id", company.id);
|
|
const result = await updateCompany(fd);
|
|
if ("error" in result) { setError(result.error); setPending(false); }
|
|
else { setPending(false); setOpen(false); router.refresh(); }
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{!isControlled && (
|
|
<button onClick={() => setOpen(true)}
|
|
className="rounded border border-primary-200 bg-primary-50 px-2.5 py-1 text-xs font-medium text-primary-700 hover:bg-primary-100 transition-colors">
|
|
Edit
|
|
</button>
|
|
)}
|
|
<AdminDialog title={`Edit — ${company.name}`} open={open} onClose={() => setOpen(false)}>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<CompanyFormFields company={company} />
|
|
{error && <p className="text-sm text-danger-700 bg-danger-50 rounded-lg px-3 py-2">{error}</p>}
|
|
<div className="flex justify-end gap-3 pt-1">
|
|
<button type="button" onClick={() => setOpen(false)}
|
|
className="rounded-lg border border-neutral-300 px-4 py-2 text-sm font-medium text-neutral-700 hover:bg-neutral-50">Cancel</button>
|
|
<button type="submit" disabled={pending}
|
|
className="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700 disabled:opacity-60">
|
|
{pending ? "Saving…" : "Save Changes"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</AdminDialog>
|
|
</>
|
|
);
|
|
}
|