Files
Joshua OosthuizenandClaude Sonnet 5 4acbc03b35 Initial commit: multi-company financial management system
Root company app (src/, client/) — GL foundation, AR, and AP:
- Segmented Chart of Accounts (Sage 300-style), double-entry PostingService with
  three-layer balance enforcement (app validation, CHECK constraints, deferred
  DB trigger), gap-free document numbering, RBAC with branch restrictions.
- Accounts Receivable: Customers, Invoices, Credit Notes, Receipts with
  allocation, all posting through PostingService.
- Accounts Payable: Suppliers, Supplier Invoices, Debit Notes, Payments with
  allocation, mirroring AR's posting pattern.
- Tax Codes shared between AR and AP.
- Company-app login is a token handoff from the Account Manager (RS256,
  single-use, company-scoped) rather than a local password.
- Frontend reskinned to the approved mockup: navy sidebar, dashboard, tree-view
  Chart of Accounts, Customer/Supplier 360 pages.

Account Manager (manager/, manager/client/) — central login gateway:
- Password + TOTP 2FA + Google/Microsoft OAuth, company picker, and the RS256
  handoff token issuance that the company app verifies.

Covered by unit + integration tests against a real Postgres database (GL
balance invariants, AR/AP posting correctness and allocation limits, the full
handoff exchange including replay/cross-company/expiry/forgery rejection).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tq2DbEvzHzs13jDFhLaX44
2026-09-03 13:27:19 +02:00

272 lines
10 KiB
PL/PgSQL

-- CreateEnum
CREATE TYPE "ARDocumentStatus" AS ENUM ('DRAFT', 'POSTED', 'VOID');
-- AlterEnum
-- Splits the generic TAX control type into TAX_OUTPUT/TAX_INPUT (AR needs Output Tax
-- Payable, AP will need Input Tax Receivable — a single "TAX" value was ambiguous for
-- control-account lookup). Existing rows are remapped by their seeded account code
-- (2100 = Output Tax Payable, 2200 = Input Tax Receivable) rather than dropped blind.
BEGIN;
CREATE TYPE "ControlAccountType_new" AS ENUM ('AR', 'AP', 'STOCK', 'TAX_OUTPUT', 'TAX_INPUT', 'BANK', 'RETAINED_EARNINGS');
ALTER TABLE "GLAccount" ALTER COLUMN "controlType" TYPE "ControlAccountType_new" USING (
CASE
WHEN "controlType"::text = 'TAX' AND "accountCode" LIKE '2100-%' THEN 'TAX_OUTPUT'
WHEN "controlType"::text = 'TAX' AND "accountCode" LIKE '2200-%' THEN 'TAX_INPUT'
ELSE "controlType"::text
END::"ControlAccountType_new"
);
ALTER TYPE "ControlAccountType" RENAME TO "ControlAccountType_old";
ALTER TYPE "ControlAccountType_new" RENAME TO "ControlAccountType";
DROP TYPE "ControlAccountType_old";
COMMIT;
-- CreateTable
CREATE TABLE "TaxCode" (
"id" SERIAL NOT NULL,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
"ratePercent" DECIMAL(6,3) NOT NULL,
"isActive" BOOLEAN NOT NULL DEFAULT true,
CONSTRAINT "TaxCode_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Customer" (
"id" SERIAL NOT NULL,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT,
"phone" TEXT,
"address" TEXT,
"taxNumber" TEXT,
"branchSegmentValueId" INTEGER NOT NULL,
"creditLimit" DECIMAL(18,2) NOT NULL DEFAULT 0,
"paymentTermsDays" INTEGER NOT NULL DEFAULT 30,
"isActive" BOOLEAN NOT NULL DEFAULT true,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Customer_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARInvoice" (
"id" SERIAL NOT NULL,
"invoiceNumber" TEXT NOT NULL,
"customerId" INTEGER NOT NULL,
"branchSegmentValueId" INTEGER NOT NULL,
"invoiceDate" TIMESTAMP(3) NOT NULL,
"dueDate" TIMESTAMP(3) NOT NULL,
"reference" TEXT,
"status" "ARDocumentStatus" NOT NULL DEFAULT 'DRAFT',
"subtotal" DECIMAL(18,2) NOT NULL,
"taxTotal" DECIMAL(18,2) NOT NULL,
"total" DECIMAL(18,2) NOT NULL,
"balance" DECIMAL(18,2) NOT NULL,
"journalEntryId" INTEGER,
"createdById" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "ARInvoice_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARInvoiceLine" (
"id" SERIAL NOT NULL,
"invoiceId" INTEGER NOT NULL,
"lineNumber" INTEGER NOT NULL,
"description" TEXT NOT NULL,
"quantity" DECIMAL(18,4) NOT NULL DEFAULT 1,
"unitPrice" DECIMAL(18,4) NOT NULL,
"revenueGLAccountId" INTEGER NOT NULL,
"taxCodeId" INTEGER,
"lineSubtotal" DECIMAL(18,2) NOT NULL,
"lineTax" DECIMAL(18,2) NOT NULL,
"lineTotal" DECIMAL(18,2) NOT NULL,
CONSTRAINT "ARInvoiceLine_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARCreditNote" (
"id" SERIAL NOT NULL,
"creditNoteNumber" TEXT NOT NULL,
"customerId" INTEGER NOT NULL,
"branchSegmentValueId" INTEGER NOT NULL,
"creditNoteDate" TIMESTAMP(3) NOT NULL,
"reference" TEXT,
"reason" TEXT,
"status" "ARDocumentStatus" NOT NULL DEFAULT 'DRAFT',
"subtotal" DECIMAL(18,2) NOT NULL,
"taxTotal" DECIMAL(18,2) NOT NULL,
"total" DECIMAL(18,2) NOT NULL,
"balance" DECIMAL(18,2) NOT NULL,
"journalEntryId" INTEGER,
"createdById" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "ARCreditNote_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARCreditNoteLine" (
"id" SERIAL NOT NULL,
"creditNoteId" INTEGER NOT NULL,
"lineNumber" INTEGER NOT NULL,
"description" TEXT NOT NULL,
"quantity" DECIMAL(18,4) NOT NULL DEFAULT 1,
"unitPrice" DECIMAL(18,4) NOT NULL,
"revenueGLAccountId" INTEGER NOT NULL,
"taxCodeId" INTEGER,
"lineSubtotal" DECIMAL(18,2) NOT NULL,
"lineTax" DECIMAL(18,2) NOT NULL,
"lineTotal" DECIMAL(18,2) NOT NULL,
CONSTRAINT "ARCreditNoteLine_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARReceipt" (
"id" SERIAL NOT NULL,
"receiptNumber" TEXT NOT NULL,
"customerId" INTEGER NOT NULL,
"branchSegmentValueId" INTEGER NOT NULL,
"receiptDate" TIMESTAMP(3) NOT NULL,
"amount" DECIMAL(18,2) NOT NULL,
"unallocatedAmount" DECIMAL(18,2) NOT NULL,
"bankGLAccountId" INTEGER NOT NULL,
"reference" TEXT,
"status" "ARDocumentStatus" NOT NULL DEFAULT 'DRAFT',
"journalEntryId" INTEGER,
"createdById" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "ARReceipt_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ARAllocation" (
"id" SERIAL NOT NULL,
"invoiceId" INTEGER NOT NULL,
"receiptId" INTEGER,
"creditNoteId" INTEGER,
"amount" DECIMAL(18,2) NOT NULL,
"allocatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "ARAllocation_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "TaxCode_code_key" ON "TaxCode"("code");
-- CreateIndex
CREATE UNIQUE INDEX "Customer_code_key" ON "Customer"("code");
-- CreateIndex
CREATE INDEX "Customer_branchSegmentValueId_idx" ON "Customer"("branchSegmentValueId");
-- CreateIndex
CREATE UNIQUE INDEX "ARInvoice_invoiceNumber_key" ON "ARInvoice"("invoiceNumber");
-- CreateIndex
CREATE INDEX "ARInvoice_customerId_idx" ON "ARInvoice"("customerId");
-- CreateIndex
CREATE INDEX "ARInvoice_branchSegmentValueId_idx" ON "ARInvoice"("branchSegmentValueId");
-- CreateIndex
CREATE INDEX "ARInvoice_status_idx" ON "ARInvoice"("status");
-- CreateIndex
CREATE INDEX "ARInvoiceLine_invoiceId_idx" ON "ARInvoiceLine"("invoiceId");
-- CreateIndex
CREATE UNIQUE INDEX "ARCreditNote_creditNoteNumber_key" ON "ARCreditNote"("creditNoteNumber");
-- CreateIndex
CREATE INDEX "ARCreditNote_customerId_idx" ON "ARCreditNote"("customerId");
-- CreateIndex
CREATE INDEX "ARCreditNote_branchSegmentValueId_idx" ON "ARCreditNote"("branchSegmentValueId");
-- CreateIndex
CREATE INDEX "ARCreditNote_status_idx" ON "ARCreditNote"("status");
-- CreateIndex
CREATE INDEX "ARCreditNoteLine_creditNoteId_idx" ON "ARCreditNoteLine"("creditNoteId");
-- CreateIndex
CREATE UNIQUE INDEX "ARReceipt_receiptNumber_key" ON "ARReceipt"("receiptNumber");
-- CreateIndex
CREATE INDEX "ARReceipt_customerId_idx" ON "ARReceipt"("customerId");
-- CreateIndex
CREATE INDEX "ARReceipt_branchSegmentValueId_idx" ON "ARReceipt"("branchSegmentValueId");
-- CreateIndex
CREATE INDEX "ARReceipt_status_idx" ON "ARReceipt"("status");
-- CreateIndex
CREATE INDEX "ARAllocation_invoiceId_idx" ON "ARAllocation"("invoiceId");
-- CreateIndex
CREATE INDEX "ARAllocation_receiptId_idx" ON "ARAllocation"("receiptId");
-- CreateIndex
CREATE INDEX "ARAllocation_creditNoteId_idx" ON "ARAllocation"("creditNoteId");
-- AddForeignKey
ALTER TABLE "JournalEntryLine" ADD CONSTRAINT "JournalEntryLine_arInvoiceId_fkey" FOREIGN KEY ("arInvoiceId") REFERENCES "ARInvoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Customer" ADD CONSTRAINT "Customer_branchSegmentValueId_fkey" FOREIGN KEY ("branchSegmentValueId") REFERENCES "AccountSegmentValue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARInvoice" ADD CONSTRAINT "ARInvoice_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARInvoice" ADD CONSTRAINT "ARInvoice_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARInvoiceLine" ADD CONSTRAINT "ARInvoiceLine_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "ARInvoice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARInvoiceLine" ADD CONSTRAINT "ARInvoiceLine_revenueGLAccountId_fkey" FOREIGN KEY ("revenueGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARInvoiceLine" ADD CONSTRAINT "ARInvoiceLine_taxCodeId_fkey" FOREIGN KEY ("taxCodeId") REFERENCES "TaxCode"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARCreditNote" ADD CONSTRAINT "ARCreditNote_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARCreditNote" ADD CONSTRAINT "ARCreditNote_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARCreditNoteLine" ADD CONSTRAINT "ARCreditNoteLine_creditNoteId_fkey" FOREIGN KEY ("creditNoteId") REFERENCES "ARCreditNote"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARCreditNoteLine" ADD CONSTRAINT "ARCreditNoteLine_revenueGLAccountId_fkey" FOREIGN KEY ("revenueGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARCreditNoteLine" ADD CONSTRAINT "ARCreditNoteLine_taxCodeId_fkey" FOREIGN KEY ("taxCodeId") REFERENCES "TaxCode"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARReceipt" ADD CONSTRAINT "ARReceipt_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARReceipt" ADD CONSTRAINT "ARReceipt_bankGLAccountId_fkey" FOREIGN KEY ("bankGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARReceipt" ADD CONSTRAINT "ARReceipt_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARAllocation" ADD CONSTRAINT "ARAllocation_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "ARInvoice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARAllocation" ADD CONSTRAINT "ARAllocation_receiptId_fkey" FOREIGN KEY ("receiptId") REFERENCES "ARReceipt"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ARAllocation" ADD CONSTRAINT "ARAllocation_creditNoteId_fkey" FOREIGN KEY ("creditNoteId") REFERENCES "ARCreditNote"("id") ON DELETE SET NULL ON UPDATE CASCADE;