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
354 lines
12 KiB
SQL
354 lines
12 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "FiscalYearStatus" AS ENUM ('OPEN', 'LOCKED', 'CLOSED');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "PeriodStatus" AS ENUM ('OPEN', 'CLOSED');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "SegmentFormat" AS ENUM ('NUMERIC', 'ALPHA', 'ALPHANUMERIC');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "GLAccountType" AS ENUM ('ASSET', 'LIABILITY', 'EQUITY', 'INCOME', 'EXPENSE');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "DebitCredit" AS ENUM ('DEBIT', 'CREDIT');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "ControlAccountType" AS ENUM ('AR', 'AP', 'STOCK', 'TAX', 'BANK', 'RETAINED_EARNINGS');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "JournalType" AS ENUM ('GENERAL', 'SALES', 'PURCHASES', 'CASH_RECEIPT', 'CASH_PAYMENT', 'STOCK', 'BANK', 'CUSTOM');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "SourceModule" AS ENUM ('GL', 'AR', 'AP', 'STOCK', 'SYSTEM');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "JournalEntryStatus" AS ENUM ('DRAFT', 'POSTED', 'REVERSED');
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "CompanySettings" (
|
|
"id" INTEGER NOT NULL DEFAULT 1,
|
|
"companyName" TEXT NOT NULL,
|
|
"baseCurrencyCode" TEXT NOT NULL DEFAULT 'ZAR',
|
|
"fiscalYearStartMonth" INTEGER NOT NULL DEFAULT 1,
|
|
"currentFiscalYear" INTEGER NOT NULL,
|
|
"accountCodeSeparator" TEXT NOT NULL DEFAULT '-',
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "CompanySettings_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "FiscalYear" (
|
|
"id" SERIAL NOT NULL,
|
|
"year" INTEGER NOT NULL,
|
|
"startDate" TIMESTAMP(3) NOT NULL,
|
|
"endDate" TIMESTAMP(3) NOT NULL,
|
|
"status" "FiscalYearStatus" NOT NULL DEFAULT 'OPEN',
|
|
|
|
CONSTRAINT "FiscalYear_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "FiscalPeriod" (
|
|
"id" SERIAL NOT NULL,
|
|
"fiscalYearId" INTEGER NOT NULL,
|
|
"periodNumber" INTEGER NOT NULL,
|
|
"startDate" TIMESTAMP(3) NOT NULL,
|
|
"endDate" TIMESTAMP(3) NOT NULL,
|
|
"status" "PeriodStatus" NOT NULL DEFAULT 'OPEN',
|
|
|
|
CONSTRAINT "FiscalPeriod_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "AccountSegmentDefinition" (
|
|
"id" SERIAL NOT NULL,
|
|
"segmentKey" TEXT NOT NULL,
|
|
"label" TEXT NOT NULL,
|
|
"order" INTEGER NOT NULL,
|
|
"length" INTEGER NOT NULL,
|
|
"format" "SegmentFormat" NOT NULL DEFAULT 'NUMERIC',
|
|
"isBranchSegment" BOOLEAN NOT NULL DEFAULT false,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
CONSTRAINT "AccountSegmentDefinition_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "AccountSegmentValue" (
|
|
"id" SERIAL NOT NULL,
|
|
"segmentDefId" INTEGER NOT NULL,
|
|
"code" TEXT NOT NULL,
|
|
"description" TEXT NOT NULL,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
CONSTRAINT "AccountSegmentValue_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "BranchProfile" (
|
|
"id" SERIAL NOT NULL,
|
|
"segmentValueId" INTEGER NOT NULL,
|
|
"address" TEXT,
|
|
"contactName" TEXT,
|
|
"isConsolidatable" BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
CONSTRAINT "BranchProfile_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "GLAccount" (
|
|
"id" SERIAL NOT NULL,
|
|
"accountCode" TEXT NOT NULL,
|
|
"accountName" TEXT NOT NULL,
|
|
"accountType" "GLAccountType" NOT NULL,
|
|
"normalBalance" "DebitCredit" NOT NULL,
|
|
"isControlAccount" BOOLEAN NOT NULL DEFAULT false,
|
|
"controlType" "ControlAccountType",
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
"isPostable" BOOLEAN NOT NULL DEFAULT true,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "GLAccount_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "GLAccountSegmentValue" (
|
|
"id" SERIAL NOT NULL,
|
|
"glAccountId" INTEGER NOT NULL,
|
|
"segmentDefId" INTEGER NOT NULL,
|
|
"segmentValueId" INTEGER NOT NULL,
|
|
|
|
CONSTRAINT "GLAccountSegmentValue_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Journal" (
|
|
"id" SERIAL NOT NULL,
|
|
"code" TEXT NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"journalType" "JournalType" NOT NULL,
|
|
"defaultBranchSegmentValueId" INTEGER,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
CONSTRAINT "Journal_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "JournalEntry" (
|
|
"id" SERIAL NOT NULL,
|
|
"entryNumber" TEXT NOT NULL,
|
|
"journalId" INTEGER NOT NULL,
|
|
"fiscalPeriodId" INTEGER NOT NULL,
|
|
"entryDate" TIMESTAMP(3) NOT NULL,
|
|
"description" TEXT NOT NULL,
|
|
"sourceModule" "SourceModule" NOT NULL,
|
|
"sourceType" TEXT,
|
|
"sourceId" INTEGER,
|
|
"status" "JournalEntryStatus" NOT NULL DEFAULT 'POSTED',
|
|
"reversalOfId" INTEGER,
|
|
"totalDebits" DECIMAL(18,2) NOT NULL,
|
|
"totalCredits" DECIMAL(18,2) NOT NULL,
|
|
"createdById" INTEGER NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "JournalEntry_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "JournalEntryLine" (
|
|
"id" SERIAL NOT NULL,
|
|
"journalEntryId" INTEGER NOT NULL,
|
|
"lineNumber" INTEGER NOT NULL,
|
|
"glAccountId" INTEGER NOT NULL,
|
|
"debit" DECIMAL(18,2) NOT NULL DEFAULT 0,
|
|
"credit" DECIMAL(18,2) NOT NULL DEFAULT 0,
|
|
"description" TEXT,
|
|
"branchSegmentValueId" INTEGER,
|
|
"arInvoiceId" INTEGER,
|
|
"apInvoiceId" INTEGER,
|
|
"stockMovementId" INTEGER,
|
|
"currencyCode" TEXT NOT NULL DEFAULT 'BASE',
|
|
"exchangeRateToBase" DECIMAL(18,6) NOT NULL DEFAULT 1,
|
|
|
|
CONSTRAINT "JournalEntryLine_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Sequence" (
|
|
"id" SERIAL NOT NULL,
|
|
"key" TEXT NOT NULL,
|
|
"lastValue" INTEGER NOT NULL DEFAULT 0,
|
|
|
|
CONSTRAINT "Sequence_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "User" (
|
|
"id" SERIAL NOT NULL,
|
|
"email" TEXT NOT NULL,
|
|
"passwordHash" TEXT NOT NULL,
|
|
"fullName" TEXT NOT NULL,
|
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Role" (
|
|
"id" SERIAL NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
|
|
CONSTRAINT "Role_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "Permission" (
|
|
"id" SERIAL NOT NULL,
|
|
"key" TEXT NOT NULL,
|
|
|
|
CONSTRAINT "Permission_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "RolePermission" (
|
|
"roleId" INTEGER NOT NULL,
|
|
"permissionId" INTEGER NOT NULL,
|
|
|
|
CONSTRAINT "RolePermission_pkey" PRIMARY KEY ("roleId","permissionId")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "UserRole" (
|
|
"userId" INTEGER NOT NULL,
|
|
"roleId" INTEGER NOT NULL,
|
|
|
|
CONSTRAINT "UserRole_pkey" PRIMARY KEY ("userId","roleId")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "UserBranchRestriction" (
|
|
"id" SERIAL NOT NULL,
|
|
"userId" INTEGER NOT NULL,
|
|
"branchSegmentValueId" INTEGER NOT NULL,
|
|
|
|
CONSTRAINT "UserBranchRestriction_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "FiscalYear_year_key" ON "FiscalYear"("year");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "FiscalPeriod_fiscalYearId_periodNumber_key" ON "FiscalPeriod"("fiscalYearId", "periodNumber");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "AccountSegmentDefinition_segmentKey_key" ON "AccountSegmentDefinition"("segmentKey");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "AccountSegmentDefinition_order_idx" ON "AccountSegmentDefinition"("order");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "AccountSegmentValue_segmentDefId_code_key" ON "AccountSegmentValue"("segmentDefId", "code");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "BranchProfile_segmentValueId_key" ON "BranchProfile"("segmentValueId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "GLAccount_accountCode_key" ON "GLAccount"("accountCode");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "GLAccount_accountType_idx" ON "GLAccount"("accountType");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "GLAccountSegmentValue_glAccountId_segmentDefId_key" ON "GLAccountSegmentValue"("glAccountId", "segmentDefId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Journal_code_key" ON "Journal"("code");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "JournalEntry_entryNumber_key" ON "JournalEntry"("entryNumber");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "JournalEntry_reversalOfId_key" ON "JournalEntry"("reversalOfId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "JournalEntry_fiscalPeriodId_idx" ON "JournalEntry"("fiscalPeriodId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "JournalEntry_sourceModule_sourceType_sourceId_idx" ON "JournalEntry"("sourceModule", "sourceType", "sourceId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "JournalEntryLine_journalEntryId_idx" ON "JournalEntryLine"("journalEntryId");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "JournalEntryLine_glAccountId_idx" ON "JournalEntryLine"("glAccountId");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Sequence_key_key" ON "Sequence"("key");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Role_name_key" ON "Role"("name");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "Permission_key_key" ON "Permission"("key");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "UserBranchRestriction_userId_branchSegmentValueId_key" ON "UserBranchRestriction"("userId", "branchSegmentValueId");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "FiscalPeriod" ADD CONSTRAINT "FiscalPeriod_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "AccountSegmentValue" ADD CONSTRAINT "AccountSegmentValue_segmentDefId_fkey" FOREIGN KEY ("segmentDefId") REFERENCES "AccountSegmentDefinition"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "BranchProfile" ADD CONSTRAINT "BranchProfile_segmentValueId_fkey" FOREIGN KEY ("segmentValueId") REFERENCES "AccountSegmentValue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "GLAccountSegmentValue" ADD CONSTRAINT "GLAccountSegmentValue_glAccountId_fkey" FOREIGN KEY ("glAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "GLAccountSegmentValue" ADD CONSTRAINT "GLAccountSegmentValue_segmentDefId_fkey" FOREIGN KEY ("segmentDefId") REFERENCES "AccountSegmentDefinition"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "GLAccountSegmentValue" ADD CONSTRAINT "GLAccountSegmentValue_segmentValueId_fkey" FOREIGN KEY ("segmentValueId") REFERENCES "AccountSegmentValue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntry" ADD CONSTRAINT "JournalEntry_journalId_fkey" FOREIGN KEY ("journalId") REFERENCES "Journal"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntry" ADD CONSTRAINT "JournalEntry_fiscalPeriodId_fkey" FOREIGN KEY ("fiscalPeriodId") REFERENCES "FiscalPeriod"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntry" ADD CONSTRAINT "JournalEntry_reversalOfId_fkey" FOREIGN KEY ("reversalOfId") REFERENCES "JournalEntry"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntry" ADD CONSTRAINT "JournalEntry_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntryLine" ADD CONSTRAINT "JournalEntryLine_journalEntryId_fkey" FOREIGN KEY ("journalEntryId") REFERENCES "JournalEntry"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "JournalEntryLine" ADD CONSTRAINT "JournalEntryLine_glAccountId_fkey" FOREIGN KEY ("glAccountId") REFERENCES "GLAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "RolePermission" ADD CONSTRAINT "RolePermission_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "RolePermission" ADD CONSTRAINT "RolePermission_permissionId_fkey" FOREIGN KEY ("permissionId") REFERENCES "Permission"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "UserRole" ADD CONSTRAINT "UserRole_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "UserRole" ADD CONSTRAINT "UserRole_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "UserBranchRestriction" ADD CONSTRAINT "UserBranchRestriction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|