Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
6ddf94d5d2
commit
172400600e
@@ -884,31 +884,49 @@ function ImportPage() {
|
||||
if (extId) ctx.caseByExt.set(String(extId), data.id);
|
||||
return data.id;
|
||||
};
|
||||
const ensurePreInvoicedInvoice = async (clientId: string, caseId: string): Promise<string | null> => {
|
||||
const cached = placeholderInvoiceByClient.get(clientId);
|
||||
// Look up an existing invoice by number for this client, or create a
|
||||
// new "sent" invoice with that exact number to bucket imported items.
|
||||
const invoiceByClientAndNumber = new Map<string, string>(); // `${clientId}::${number}` -> invoiceId
|
||||
const ensureNamedInvoice = async (
|
||||
clientId: string,
|
||||
caseId: string,
|
||||
invoiceNumber: string,
|
||||
): Promise<string | null> => {
|
||||
const cacheKey = `${clientId}::${invoiceNumber}`;
|
||||
const cached = invoiceByClientAndNumber.get(cacheKey);
|
||||
if (cached) return cached;
|
||||
const invNumber = `IMPORT-PREBILLED-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
|
||||
// First try existing invoice with this number for this client
|
||||
const { data: existing } = await supabase
|
||||
.from("invoices")
|
||||
.select("id")
|
||||
.eq("client_id", clientId)
|
||||
.eq("invoice_number", invoiceNumber)
|
||||
.maybeSingle();
|
||||
if (existing?.id) {
|
||||
invoiceByClientAndNumber.set(cacheKey, existing.id);
|
||||
return existing.id;
|
||||
}
|
||||
const { data, error } = await supabase
|
||||
.from("invoices")
|
||||
.insert({
|
||||
client_id: clientId,
|
||||
case_id: caseId,
|
||||
invoice_number: invNumber,
|
||||
status: "void",
|
||||
invoice_number: invoiceNumber,
|
||||
status: "sent",
|
||||
issue_date: new Date().toISOString().slice(0, 10),
|
||||
subtotal: 0,
|
||||
tax: 0,
|
||||
total: 0,
|
||||
notes: "Placeholder invoice for items imported as already invoiced.",
|
||||
notes: "Auto-created from import to hold items linked to this invoice number.",
|
||||
created_by: user.id,
|
||||
} as any)
|
||||
.select("id")
|
||||
.single();
|
||||
if (error || !data) {
|
||||
errors.push(`Auto-create placeholder invoice: ${error?.message ?? "unknown"}`);
|
||||
errors.push(`Auto-create invoice "${invoiceNumber}": ${error?.message ?? "unknown"}`);
|
||||
return null;
|
||||
}
|
||||
placeholderInvoiceByClient.set(clientId, data.id);
|
||||
invoiceByClientAndNumber.set(cacheKey, data.id);
|
||||
return data.id;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user