Files
events-hub/backend/prisma/migrations/20261103111645_invoicing/migration.sql
T
joshuaandClaude Opus 5.5 6fce0fe90e Billing: invoices, approval, receipts, statements, licence changes (1.7.0)
- Invoices drafted 14 days before each licence's invoice date and emailed
  to the alert email for approval; approved ones go out on their date from
  08:00 SA time. Drafts can be edited, recalculated, sent early, put back
  to draft or voided; one-off invoices too.
- Annual tier review from the 12-month event-income band; approving a tier
  change emails a notice of licence change, applied when the invoice goes out.
  Notices can also be sent by hand.
- Founding churches (35% off as its own line), payments with receipts,
  statements for any dates.
- Every billing email is styled HTML with a PDF (invoice / tax invoice,
  receipt, statement, licence change) and logged per customer.
- Settings -> Billing: business and bank details, VAT (off: plain invoices;
  on: VAT-inclusive tax invoices), timing and numbering.
- Migration 20261103111645_invoicing; pdfkit 0.17.2; tests/billing.test.js;
  scripts/sample-billing-pdfs.js.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:42:45 +02:00

170 lines
5.5 KiB
SQL

-- CreateEnum
CREATE TYPE "BillingCycle" AS ENUM ('none', 'monthly', 'yearly');
-- CreateEnum
CREATE TYPE "InvoiceStatus" AS ENUM ('draft', 'approved', 'sent', 'void');
-- AlterTable
ALTER TABLE "Customer" ADD COLUMN "billingAddress" TEXT,
ADD COLUMN "billingEmail" TEXT,
ADD COLUMN "founding" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "foundingNote" TEXT,
ADD COLUMN "foundingSince" DATE;
-- AlterTable
ALTER TABLE "Licence" ADD COLUMN "billingCycle" "BillingCycle" NOT NULL DEFAULT 'none',
ADD COLUMN "billingStart" DATE,
ADD COLUMN "customMonthlyCents" INTEGER,
ADD COLUMN "nextInvoiceDate" DATE,
ADD COLUMN "reviewDate" DATE;
-- CreateTable
CREATE TABLE "Invoice" (
"id" TEXT NOT NULL,
"number" TEXT NOT NULL,
"customerId" TEXT NOT NULL,
"licenceId" TEXT,
"status" "InvoiceStatus" NOT NULL DEFAULT 'draft',
"issueDate" DATE NOT NULL,
"dueDate" DATE NOT NULL,
"periodStart" DATE,
"periodEnd" DATE,
"cycle" "BillingCycle" NOT NULL DEFAULT 'monthly',
"plan" TEXT,
"incomeBand" TEXT,
"reviewed" BOOLEAN NOT NULL DEFAULT false,
"founding" BOOLEAN NOT NULL DEFAULT false,
"planChange" JSONB,
"lines" JSONB NOT NULL,
"totalCents" INTEGER NOT NULL,
"vatCents" INTEGER NOT NULL DEFAULT 0,
"vatRate" DOUBLE PRECISION,
"notes" TEXT,
"warnings" TEXT[] DEFAULT ARRAY[]::TEXT[],
"recipientEmail" TEXT,
"approvedAt" TIMESTAMP(3),
"approvedById" TEXT,
"approvalEmailedAt" TIMESTAMP(3),
"overdueEmailedAt" TIMESTAMP(3),
"sentAt" TIMESTAMP(3),
"sendError" TEXT,
"voidedAt" TIMESTAMP(3),
"voidReason" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Invoice_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Payment" (
"id" TEXT NOT NULL,
"receiptNumber" TEXT NOT NULL,
"customerId" TEXT NOT NULL,
"invoiceId" TEXT,
"amountCents" INTEGER NOT NULL,
"paidOn" DATE NOT NULL,
"method" TEXT NOT NULL DEFAULT 'eft',
"reference" TEXT,
"notes" TEXT,
"receiptSentAt" TIMESTAMP(3),
"createdById" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Payment_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "LicenceChange" (
"id" TEXT NOT NULL,
"number" TEXT NOT NULL,
"customerId" TEXT NOT NULL,
"licenceId" TEXT,
"invoiceId" TEXT,
"fromPlan" TEXT NOT NULL,
"toPlan" TEXT NOT NULL,
"fromMonthlyCents" INTEGER,
"toMonthlyCents" INTEGER,
"effectiveDate" DATE NOT NULL,
"reason" TEXT,
"incomeBand" TEXT,
"appliedAt" TIMESTAMP(3),
"sentAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "LicenceChange_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "BillingEmail" (
"id" TEXT NOT NULL,
"customerId" TEXT,
"kind" TEXT NOT NULL,
"refId" TEXT,
"to" TEXT NOT NULL,
"subject" TEXT NOT NULL,
"error" TEXT,
"sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "BillingEmail_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "DocumentCounter" (
"key" TEXT NOT NULL,
"value" INTEGER NOT NULL DEFAULT 0,
CONSTRAINT "DocumentCounter_pkey" PRIMARY KEY ("key")
);
-- CreateIndex
CREATE UNIQUE INDEX "Invoice_number_key" ON "Invoice"("number");
-- CreateIndex
CREATE INDEX "Invoice_status_issueDate_idx" ON "Invoice"("status", "issueDate");
-- CreateIndex
CREATE INDEX "Invoice_customerId_idx" ON "Invoice"("customerId");
-- CreateIndex
CREATE UNIQUE INDEX "Invoice_licenceId_periodStart_key" ON "Invoice"("licenceId", "periodStart");
-- CreateIndex
CREATE UNIQUE INDEX "Payment_receiptNumber_key" ON "Payment"("receiptNumber");
-- CreateIndex
CREATE INDEX "Payment_customerId_paidOn_idx" ON "Payment"("customerId", "paidOn");
-- CreateIndex
CREATE UNIQUE INDEX "LicenceChange_number_key" ON "LicenceChange"("number");
-- CreateIndex
CREATE INDEX "LicenceChange_licenceId_appliedAt_idx" ON "LicenceChange"("licenceId", "appliedAt");
-- CreateIndex
CREATE INDEX "BillingEmail_customerId_sentAt_idx" ON "BillingEmail"("customerId", "sentAt");
-- AddForeignKey
ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_licenceId_fkey" FOREIGN KEY ("licenceId") REFERENCES "Licence"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Payment" ADD CONSTRAINT "Payment_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Payment" ADD CONSTRAINT "Payment_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "Invoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "LicenceChange" ADD CONSTRAINT "LicenceChange_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "LicenceChange" ADD CONSTRAINT "LicenceChange_licenceId_fkey" FOREIGN KEY ("licenceId") REFERENCES "Licence"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "LicenceChange" ADD CONSTRAINT "LicenceChange_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "Invoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "BillingEmail" ADD CONSTRAINT "BillingEmail_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE SET NULL ON UPDATE CASCADE;