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
238 lines
8.8 KiB
SQL
238 lines
8.8 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "APDocumentStatus" AS ENUM ('DRAFT', 'POSTED', 'VOID');
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Supplier" (
|
|
"id" SERIAL NOT NULL,
|
|
"code" TEXT NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"email" TEXT,
|
|
"phone" TEXT,
|
|
"address" TEXT,
|
|
"taxNumber" TEXT,
|
|
"branchSegmentValueId" INTEGER NOT NULL,
|
|
"paymentTermsDays" INTEGER NOT NULL DEFAULT 30,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "Supplier_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APInvoice" (
|
|
"id" SERIAL NOT NULL,
|
|
"invoiceNumber" TEXT NOT NULL,
|
|
"supplierReference" TEXT,
|
|
"supplierId" INTEGER NOT NULL,
|
|
"branchSegmentValueId" INTEGER NOT NULL,
|
|
"invoiceDate" TIMESTAMP(3) NOT NULL,
|
|
"dueDate" TIMESTAMP(3) NOT NULL,
|
|
"status" "APDocumentStatus" 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 "APInvoice_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APInvoiceLine" (
|
|
"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,
|
|
"expenseGLAccountId" INTEGER NOT NULL,
|
|
"taxCodeId" INTEGER,
|
|
"lineSubtotal" DECIMAL(18,2) NOT NULL,
|
|
"lineTax" DECIMAL(18,2) NOT NULL,
|
|
"lineTotal" DECIMAL(18,2) NOT NULL,
|
|
|
|
CONSTRAINT "APInvoiceLine_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APDebitNote" (
|
|
"id" SERIAL NOT NULL,
|
|
"debitNoteNumber" TEXT NOT NULL,
|
|
"supplierId" INTEGER NOT NULL,
|
|
"branchSegmentValueId" INTEGER NOT NULL,
|
|
"debitNoteDate" TIMESTAMP(3) NOT NULL,
|
|
"reference" TEXT,
|
|
"reason" TEXT,
|
|
"status" "APDocumentStatus" 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 "APDebitNote_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APDebitNoteLine" (
|
|
"id" SERIAL NOT NULL,
|
|
"debitNoteId" 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,
|
|
"expenseGLAccountId" INTEGER NOT NULL,
|
|
"taxCodeId" INTEGER,
|
|
"lineSubtotal" DECIMAL(18,2) NOT NULL,
|
|
"lineTax" DECIMAL(18,2) NOT NULL,
|
|
"lineTotal" DECIMAL(18,2) NOT NULL,
|
|
|
|
CONSTRAINT "APDebitNoteLine_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APPayment" (
|
|
"id" SERIAL NOT NULL,
|
|
"paymentNumber" TEXT NOT NULL,
|
|
"supplierId" INTEGER NOT NULL,
|
|
"branchSegmentValueId" INTEGER NOT NULL,
|
|
"paymentDate" TIMESTAMP(3) NOT NULL,
|
|
"amount" DECIMAL(18,2) NOT NULL,
|
|
"unallocatedAmount" DECIMAL(18,2) NOT NULL,
|
|
"bankGLAccountId" INTEGER NOT NULL,
|
|
"reference" TEXT,
|
|
"status" "APDocumentStatus" NOT NULL DEFAULT 'DRAFT',
|
|
"journalEntryId" INTEGER,
|
|
"createdById" INTEGER NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "APPayment_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "APAllocation" (
|
|
"id" SERIAL NOT NULL,
|
|
"invoiceId" INTEGER NOT NULL,
|
|
"paymentId" INTEGER,
|
|
"debitNoteId" INTEGER,
|
|
"amount" DECIMAL(18,2) NOT NULL,
|
|
"allocatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "APAllocation_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Supplier_code_key" ON "Supplier"("code");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "Supplier_branchSegmentValueId_idx" ON "Supplier"("branchSegmentValueId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "APInvoice_invoiceNumber_key" ON "APInvoice"("invoiceNumber");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APInvoice_supplierId_idx" ON "APInvoice"("supplierId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APInvoice_branchSegmentValueId_idx" ON "APInvoice"("branchSegmentValueId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APInvoice_status_idx" ON "APInvoice"("status");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APInvoiceLine_invoiceId_idx" ON "APInvoiceLine"("invoiceId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "APDebitNote_debitNoteNumber_key" ON "APDebitNote"("debitNoteNumber");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APDebitNote_supplierId_idx" ON "APDebitNote"("supplierId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APDebitNote_branchSegmentValueId_idx" ON "APDebitNote"("branchSegmentValueId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APDebitNote_status_idx" ON "APDebitNote"("status");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APDebitNoteLine_debitNoteId_idx" ON "APDebitNoteLine"("debitNoteId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "APPayment_paymentNumber_key" ON "APPayment"("paymentNumber");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APPayment_supplierId_idx" ON "APPayment"("supplierId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APPayment_branchSegmentValueId_idx" ON "APPayment"("branchSegmentValueId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APPayment_status_idx" ON "APPayment"("status");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APAllocation_invoiceId_idx" ON "APAllocation"("invoiceId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APAllocation_paymentId_idx" ON "APAllocation"("paymentId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "APAllocation_debitNoteId_idx" ON "APAllocation"("debitNoteId");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntryLine" ADD CONSTRAINT "JournalEntryLine_apInvoiceId_fkey" FOREIGN KEY ("apInvoiceId") REFERENCES "APInvoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "Supplier" ADD CONSTRAINT "Supplier_branchSegmentValueId_fkey" FOREIGN KEY ("branchSegmentValueId") REFERENCES "AccountSegmentValue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APInvoice" ADD CONSTRAINT "APInvoice_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "Supplier"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APInvoice" ADD CONSTRAINT "APInvoice_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APInvoiceLine" ADD CONSTRAINT "APInvoiceLine_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "APInvoice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APInvoiceLine" ADD CONSTRAINT "APInvoiceLine_expenseGLAccountId_fkey" FOREIGN KEY ("expenseGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APInvoiceLine" ADD CONSTRAINT "APInvoiceLine_taxCodeId_fkey" FOREIGN KEY ("taxCodeId") REFERENCES "TaxCode"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APDebitNote" ADD CONSTRAINT "APDebitNote_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "Supplier"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APDebitNote" ADD CONSTRAINT "APDebitNote_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APDebitNoteLine" ADD CONSTRAINT "APDebitNoteLine_debitNoteId_fkey" FOREIGN KEY ("debitNoteId") REFERENCES "APDebitNote"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APDebitNoteLine" ADD CONSTRAINT "APDebitNoteLine_expenseGLAccountId_fkey" FOREIGN KEY ("expenseGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APDebitNoteLine" ADD CONSTRAINT "APDebitNoteLine_taxCodeId_fkey" FOREIGN KEY ("taxCodeId") REFERENCES "TaxCode"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APPayment" ADD CONSTRAINT "APPayment_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "Supplier"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APPayment" ADD CONSTRAINT "APPayment_bankGLAccountId_fkey" FOREIGN KEY ("bankGLAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APPayment" ADD CONSTRAINT "APPayment_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APAllocation" ADD CONSTRAINT "APAllocation_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "APInvoice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APAllocation" ADD CONSTRAINT "APAllocation_paymentId_fkey" FOREIGN KEY ("paymentId") REFERENCES "APPayment"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "APAllocation" ADD CONSTRAINT "APAllocation_debitNoteId_fkey" FOREIGN KEY ("debitNoteId") REFERENCES "APDebitNote"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|