实现企业银行账户与建行制单(corporate-bank-account-payment-order)

这个提交包含在:
2026-08-02 23:36:40 +08:00
父节点 f946d64539
当前提交 5261d2849b
共修改 46 个文件,包含 2751 行新增和 73 行删除
@@ -0,0 +1,78 @@
-- Create bank institution master data.
CREATE TABLE "bank_institutions" (
"id" UUID NOT NULL,
"code" VARCHAR(16) NOT NULL,
"name" VARCHAR(100) NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"archived_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"created_by" UUID,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_by" UUID,
CONSTRAINT "bank_institutions_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "bank_institutions_code_key" ON "bank_institutions"("code");
CREATE INDEX "idx_bank_institutions_status" ON "bank_institutions"("enabled", "archived_at");
-- Create legal-entity-owned company payment accounts.
CREATE TABLE "payment_accounts" (
"id" UUID NOT NULL,
"legal_entity_id" UUID NOT NULL,
"bank_institution_id" UUID NOT NULL,
"account_name" VARCHAR(120) NOT NULL,
"account_number_encrypted" TEXT NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"archived_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"created_by" UUID,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_by" UUID,
CONSTRAINT "payment_accounts_pkey" PRIMARY KEY ("id")
);
CREATE INDEX "idx_payment_accounts_legal_entity_status" ON "payment_accounts"("legal_entity_id", "enabled", "archived_at");
CREATE INDEX "idx_payment_accounts_bank_institution" ON "payment_accounts"("bank_institution_id");
CREATE UNIQUE INDEX "payment_accounts_one_active_per_legal_entity_ux"
ON "payment_accounts"("legal_entity_id")
WHERE "enabled" = true AND "archived_at" IS NULL;
ALTER TABLE "payment_accounts"
ADD CONSTRAINT "payment_accounts_legal_entity_id_fkey"
FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "payment_accounts"
ADD CONSTRAINT "payment_accounts_bank_institution_id_fkey"
FOREIGN KEY ("bank_institution_id") REFERENCES "bank_institutions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- Employee bank identity is intentionally nullable; existing free text is not guessed.
ALTER TABLE "employees" ADD COLUMN "bank_institution_id" UUID;
CREATE INDEX "idx_employees_bank_institution" ON "employees"("bank_institution_id");
ALTER TABLE "employees"
ADD CONSTRAINT "employees_bank_institution_id_fkey"
FOREIGN KEY ("bank_institution_id") REFERENCES "bank_institutions"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- Historical payment batches remain nullable and cannot be order-exported.
ALTER TABLE "payment_batches"
ADD COLUMN "legal_entity_id" UUID,
ADD COLUMN "payment_account_id" UUID,
ADD COLUMN "payment_account_snapshot_json" JSONB,
ADD COLUMN "payment_order_export_count" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "payment_order_last_exported_at" TIMESTAMP(3),
ADD COLUMN "payment_order_last_exported_by" UUID;
CREATE INDEX "idx_payment_batches_legal_entity" ON "payment_batches"("legal_entity_id");
CREATE INDEX "idx_payment_batches_payment_account" ON "payment_batches"("payment_account_id");
CREATE INDEX "idx_payment_batches_order_exported_by" ON "payment_batches"("payment_order_last_exported_by");
ALTER TABLE "payment_batches"
ADD CONSTRAINT "payment_batches_legal_entity_id_fkey"
FOREIGN KEY ("legal_entity_id") REFERENCES "legal_entities"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "payment_batches"
ADD CONSTRAINT "payment_batches_payment_account_id_fkey"
FOREIGN KEY ("payment_account_id") REFERENCES "payment_accounts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "payment_batches"
ADD CONSTRAINT "payment_batches_payment_order_last_exported_by_fkey"
FOREIGN KEY ("payment_order_last_exported_by") REFERENCES "employees"("id") ON DELETE SET NULL ON UPDATE CASCADE;