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
This commit is contained in:
@@ -0,0 +1,353 @@
|
||||
-- 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;
|
||||
@@ -0,0 +1,54 @@
|
||||
-- Layer 2 + Layer 3 of the double-entry enforcement design (see the implementation plan,
|
||||
-- section "Double-Entry Enforcement"). Layer 1 is PostingService.postEntry() in
|
||||
-- src/modules/gl/posting.service.ts, which is the primary guard. These are the DB-level
|
||||
-- backstops that make an unbalanced journal impossible to persist even via a bypass of
|
||||
-- that service (raw SQL, a future bug, a manual DB edit).
|
||||
--
|
||||
-- Not expressible in Prisma's schema.prisma DSL, so this is applied as hand-written SQL
|
||||
-- inside an otherwise-empty Prisma migration. Source copy kept at
|
||||
-- prisma/manual-sql/add_journal_balance_trigger.sql for reference.
|
||||
|
||||
-- Layer 2: row-level guards on JournalEntryLine.
|
||||
ALTER TABLE "JournalEntryLine"
|
||||
ADD CONSTRAINT chk_line_nonnegative
|
||||
CHECK (debit >= 0 AND credit >= 0);
|
||||
|
||||
ALTER TABLE "JournalEntryLine"
|
||||
ADD CONSTRAINT chk_line_debit_xor_credit
|
||||
CHECK ((debit > 0 AND credit = 0) OR (credit > 0 AND debit = 0));
|
||||
|
||||
-- Layer 3: deferred trigger that re-sums debits/credits per journal_entry_id at commit
|
||||
-- and rejects the transaction if they don't match. This is the real hard guarantee.
|
||||
CREATE OR REPLACE FUNCTION trg_check_journal_entry_balance() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
affected_entry_id INTEGER;
|
||||
total_debit NUMERIC(18,2);
|
||||
total_credit NUMERIC(18,2);
|
||||
BEGIN
|
||||
affected_entry_id := COALESCE(NEW."journalEntryId", OLD."journalEntryId");
|
||||
|
||||
SELECT COALESCE(SUM(debit), 0), COALESCE(SUM(credit), 0)
|
||||
INTO total_debit, total_credit
|
||||
FROM "JournalEntryLine"
|
||||
WHERE "journalEntryId" = affected_entry_id;
|
||||
|
||||
IF total_debit <> total_credit THEN
|
||||
RAISE EXCEPTION 'JournalEntry % does not balance: debits % != credits %',
|
||||
affected_entry_id, total_debit, total_credit;
|
||||
END IF;
|
||||
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE CONSTRAINT TRIGGER journal_entry_line_balance_check
|
||||
AFTER INSERT OR UPDATE OR DELETE ON "JournalEntryLine"
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION trg_check_journal_entry_balance();
|
||||
|
||||
-- Extra guard: at most one AccountSegmentDefinition may be flagged as the branch segment.
|
||||
-- Prisma's schema DSL can't express a partial unique index, so it's added here.
|
||||
CREATE UNIQUE INDEX one_branch_segment_only
|
||||
ON "AccountSegmentDefinition" (("isBranchSegment"))
|
||||
WHERE "isBranchSegment" = true;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "CompanySettings" ADD COLUMN "autoProvisionOnHandoff" BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "User" ALTER COLUMN "passwordHash" DROP NOT NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "HandoffTokenUse" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"jti" TEXT NOT NULL,
|
||||
"usedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "HandoffTokenUse_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "HandoffTokenUse_jti_key" ON "HandoffTokenUse"("jti");
|
||||
@@ -0,0 +1,271 @@
|
||||
-- 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;
|
||||
@@ -0,0 +1,237 @@
|
||||
-- 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;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
Reference in New Issue
Block a user