pelagia-portal/App/prisma/migrations/20260531000002_po_payment_date/migration.sql
Hardik add0f3c19c feat(payments): compulsory payment date when Accounts records payment
- New PurchaseOrder.paymentDate field (migration 20260531000002)
- Backfill: existing POs use paidAt, else the earliest payment action date
- Accounts must enter a payment date with the payment reference
- Date input pre-selected to today, max=today (no future dates)
- Validated server-side (required + not in future) in processPaymentSchema
- paymentDate stored on both full and partial payments; paidAt set from it
- Shown on PO detail (Payment Date) and payment history (prefers paymentDate)
- Integration tests updated; added future-date rejection test

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 08:59:25 +05:30

22 lines
879 B
SQL

-- Add user-entered payment date to PurchaseOrder
ALTER TABLE "PurchaseOrder" ADD COLUMN "paymentDate" TIMESTAMP(3);
-- Backfill 1: fully-paid POs already carry paidAt — use it as the payment date
UPDATE "PurchaseOrder"
SET "paymentDate" = "paidAt"
WHERE "paidAt" IS NOT NULL AND "paymentDate" IS NULL;
-- Backfill 2: POs that have a payment reference but no payment date yet
-- (e.g. partially-paid) — use the date the payment reference was first recorded,
-- i.e. the earliest PAYMENT_SENT / PARTIAL_PAYMENT_CONFIRMED action.
UPDATE "PurchaseOrder" po
SET "paymentDate" = sub."firstPaymentActionAt"
FROM (
SELECT "poId", MIN("createdAt") AS "firstPaymentActionAt"
FROM "POAction"
WHERE "actionType" IN ('PAYMENT_SENT', 'PARTIAL_PAYMENT_CONFIRMED')
GROUP BY "poId"
) sub
WHERE po."id" = sub."poId"
AND po."paymentDate" IS NULL
AND po."paymentRef" IS NOT NULL;