feat(02-02): JournalEntry models + JournalEntryService
- Add JournalEntryStatus and JournalEntrySource enums to Prisma schema - Add JournalEntry model with maker-checker fields, self-referential reversal relation, and audit timestamps - Add JournalEntryLine model with debit/credit Decimal(15,2) fields - Update Account model with journalEntryLines back-relation - Update User model with createdJournalEntries and approvedJournalEntries back-relations - Run migration: 20260304150817_add_journal_entry_models - Add journalEntry and journalEntryLine to TENANT_SCOPED_MODELS in prisma-tenant.ts - Create JournalEntryService with createEntry, approveEntry, reverseEntry, getAccountBalance, getTrialBalance - Enforce debit=credit balance using integer cents comparison (avoids float issues) - SYSTEM source entries auto-posted; MANUAL entries start as DRAFT for maker-checker
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "JournalEntryStatus" AS ENUM ('DRAFT', 'PENDING_APPROVAL', 'APPROVED', 'POSTED', 'REVERSED');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "JournalEntrySource" AS ENUM ('SYSTEM', 'MANUAL');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "JournalEntry" (
|
||||
"id" TEXT NOT NULL,
|
||||
"tenantId" TEXT NOT NULL,
|
||||
"entryNumber" TEXT NOT NULL,
|
||||
"date" TIMESTAMP(3) NOT NULL,
|
||||
"description" TEXT NOT NULL,
|
||||
"source" "JournalEntrySource" NOT NULL,
|
||||
"status" "JournalEntryStatus" NOT NULL DEFAULT 'DRAFT',
|
||||
"referenceType" TEXT,
|
||||
"referenceId" TEXT,
|
||||
"reversesEntryId" TEXT,
|
||||
"createdById" TEXT NOT NULL,
|
||||
"approvedById" TEXT,
|
||||
"approvedAt" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "JournalEntry_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "JournalEntryLine" (
|
||||
"id" TEXT NOT NULL,
|
||||
"tenantId" TEXT NOT NULL,
|
||||
"journalEntryId" TEXT NOT NULL,
|
||||
"accountId" TEXT NOT NULL,
|
||||
"debit" DECIMAL(15,2) NOT NULL DEFAULT 0,
|
||||
"credit" DECIMAL(15,2) NOT NULL DEFAULT 0,
|
||||
"description" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "JournalEntryLine_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "JournalEntry_reversesEntryId_key" ON "JournalEntry"("reversesEntryId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntry_tenantId_idx" ON "JournalEntry"("tenantId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntry_tenantId_date_idx" ON "JournalEntry"("tenantId", "date");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntry_referenceType_referenceId_idx" ON "JournalEntry"("referenceType", "referenceId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "JournalEntry_tenantId_entryNumber_key" ON "JournalEntry"("tenantId", "entryNumber");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntryLine_tenantId_idx" ON "JournalEntryLine"("tenantId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntryLine_accountId_idx" ON "JournalEntryLine"("accountId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "JournalEntryLine_journalEntryId_idx" ON "JournalEntryLine"("journalEntryId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "JournalEntry" ADD CONSTRAINT "JournalEntry_reversesEntryId_fkey" FOREIGN KEY ("reversesEntryId") 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 "JournalEntry" ADD CONSTRAINT "JournalEntry_approvedById_fkey" FOREIGN KEY ("approvedById") REFERENCES "User"("id") ON DELETE SET NULL 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_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -68,6 +68,19 @@ enum BillingType {
|
||||
POSTPAID
|
||||
}
|
||||
|
||||
enum JournalEntryStatus {
|
||||
DRAFT
|
||||
PENDING_APPROVAL
|
||||
APPROVED
|
||||
POSTED
|
||||
REVERSED
|
||||
}
|
||||
|
||||
enum JournalEntrySource {
|
||||
SYSTEM
|
||||
MANUAL
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// MODELS
|
||||
// =============================================================================
|
||||
@@ -119,6 +132,8 @@ model Account {
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
journalEntryLines JournalEntryLine[]
|
||||
|
||||
/// Account codes must be unique within a tenant
|
||||
@@unique([tenantId, code])
|
||||
/// RLS-ready index — always present on tenant-scoped models
|
||||
@@ -252,6 +267,11 @@ model User {
|
||||
/// Accounting periods this user has closed (as administrator)
|
||||
closedPeriods AccountingPeriod[]
|
||||
|
||||
/// Journal entries this user created (maker)
|
||||
createdJournalEntries JournalEntry[] @relation("JournalEntryCreatedBy")
|
||||
/// Journal entries this user approved (checker)
|
||||
approvedJournalEntries JournalEntry[] @relation("JournalEntryApprovedBy")
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@ -260,3 +280,68 @@ model User {
|
||||
/// RLS-ready index — always present on tenant-scoped models
|
||||
@@index([tenantId])
|
||||
}
|
||||
|
||||
/// A JournalEntry records a double-entry accounting transaction.
|
||||
/// All financial events in the system flow through this model.
|
||||
/// Entries are IMMUTABLE — no updates or deletes. Corrections use reversing entries.
|
||||
model JournalEntry {
|
||||
id String @id @default(uuid())
|
||||
tenantId String
|
||||
/// Auto-generated sequential identifier per tenant per year (e.g., "JE-2026-0001")
|
||||
entryNumber String
|
||||
/// The accounting date (when the economic event occurred, not necessarily createdAt)
|
||||
date DateTime
|
||||
description String
|
||||
source JournalEntrySource
|
||||
status JournalEntryStatus @default(DRAFT)
|
||||
/// Optional reference to source record (e.g., "Invoice", "Payment")
|
||||
referenceType String?
|
||||
/// ID of the source record (e.g., the invoice UUID)
|
||||
referenceId String?
|
||||
/// If this entry reverses another entry, this points to the original
|
||||
reversesEntryId String? @unique
|
||||
reversesEntry JournalEntry? @relation("JournalEntryReversal", fields: [reversesEntryId], references: [id])
|
||||
/// If this entry has been reversed, this points to the reversing entry (back-reference)
|
||||
reversedByEntry JournalEntry? @relation("JournalEntryReversal")
|
||||
/// The user who created the entry (maker)
|
||||
createdById String
|
||||
createdBy User @relation("JournalEntryCreatedBy", fields: [createdById], references: [id])
|
||||
/// The user who approved the entry (checker — for MANUAL entries)
|
||||
approvedById String?
|
||||
approvedBy User? @relation("JournalEntryApprovedBy", fields: [approvedById], references: [id])
|
||||
approvedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
lines JournalEntryLine[]
|
||||
|
||||
/// Entry numbers must be unique within a tenant
|
||||
@@unique([tenantId, entryNumber])
|
||||
/// RLS-ready index — always present on tenant-scoped models
|
||||
@@index([tenantId])
|
||||
@@index([tenantId, date])
|
||||
@@index([referenceType, referenceId])
|
||||
}
|
||||
|
||||
/// A single line (debit or credit) within a JournalEntry.
|
||||
/// Every entry must have at least 2 lines, and sum(debit) = sum(credit).
|
||||
model JournalEntryLine {
|
||||
id String @id @default(uuid())
|
||||
tenantId String
|
||||
journalEntryId String
|
||||
journalEntry JournalEntry @relation(fields: [journalEntryId], references: [id])
|
||||
accountId String
|
||||
account Account @relation(fields: [accountId], references: [id])
|
||||
/// Debit amount for this line (0 if this is a credit line)
|
||||
debit Decimal @default(0) @db.Decimal(15, 2)
|
||||
/// Credit amount for this line (0 if this is a debit line)
|
||||
credit Decimal @default(0) @db.Decimal(15, 2)
|
||||
/// Optional line-level memo
|
||||
description String?
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
/// RLS-ready index — always present on tenant-scoped models
|
||||
@@index([tenantId])
|
||||
@@index([accountId])
|
||||
@@index([journalEntryId])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user