feat: invoices — patient billing with installments + PDF export

Backend (new `invoice` RBAC resource, granted to clinicians + reception):
- invoices table (line items + installments as JSONB), types, zod validation,
  service (CRUD + splitIntoInstallments + auto invoice numbers), org-scoped
  REST routes mounted at /api/invoices, activity logging (migration 0015)

Frontend:
- lib/invoices.ts API client + money/date helpers
- /invoices page: list with KPIs and search, create/edit dialog (searchable
  patient combobox, inline line-item editor, live total), detail sheet to split
  a bill into equal monthly installments, delete, and Download PDF
- dependency-free PDF via a print-styled window (browser "Save as PDF")
- sidebar "Invoices" entry under the Patients group; "Added by AI" badge honored

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Khalid Abdi
2026-06-14 19:58:08 +03:00
parent 9eb6353e00
commit d096c4fe9d
21 changed files with 4734 additions and 0 deletions
+1
View File
@@ -3,6 +3,7 @@ export * from "./patients.js";
export * from "./notes.js";
export * from "./appointments.js";
export * from "./prescriptions.js";
export * from "./invoices.js";
export * from "./inventory.js";
export * from "./tasks.js";
export * from "./activity.js";
+55
View File
@@ -0,0 +1,55 @@
import {
date,
index,
jsonb,
pgTable,
text,
timestamp,
uuid,
} from "drizzle-orm/pg-core";
import type {
InvoiceInstallment,
InvoiceLineItem,
InvoiceStatus,
} from "../../types/invoice.js";
import { organization, user } from "./auth.js";
// One row per patient invoice, scoped to a clinic (organization). Patient
// identity is denormalized (name/initials/file number) so the ledger renders
// without joining. Line items and installments are stored as JSONB. `issuedAt`
// defaults to today.
export const invoices = pgTable(
"invoices",
{
id: uuid("id").primaryKey().defaultRandom(),
organizationId: text("organization_id")
.notNull()
.references(() => organization.id, { onDelete: "cascade" }),
patientFileNumber: text("patient_file_number").notNull().default(""),
patientName: text("patient_name").notNull(),
patientInitials: text("patient_initials").notNull(),
number: text("number").notNull(),
issuedAt: date("issued_at").defaultNow().notNull(),
dueAt: date("due_at"),
status: text("status").$type<InvoiceStatus>().notNull(),
lineItems: jsonb("line_items").$type<InvoiceLineItem[]>().notNull(),
installments: jsonb("installments")
.$type<InvoiceInstallment[]>()
.notNull(),
notes: text("notes"),
source: text("source").$type<"manual" | "ai">().notNull().default("manual"),
createdBy: text("created_by").references(() => user.id, {
onDelete: "set null",
}),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => new Date())
.notNull(),
},
(t) => [
index("invoices_org_idx").on(t.organizationId),
index("invoices_org_file_idx").on(t.organizationId, t.patientFileNumber),
],
);