29 lines
2.5 KiB
SQL
29 lines
2.5 KiB
SQL
-- Financial ledger foundation. Amounts are integer fils (1 JOD = 1000 fils).
|
|
CREATE TABLE IF NOT EXISTS `ledger_accounts` (
|
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `code` VARCHAR(80) NOT NULL,
|
|
`owner_type` ENUM('platform','teacher','school') NOT NULL, `owner_id` BIGINT UNSIGNED NULL,
|
|
`currency` CHAR(3) NOT NULL DEFAULT 'JOD', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (`id`), UNIQUE KEY `uq_ledger_account` (`code`,`owner_type`,`owner_id`,`currency`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
CREATE TABLE IF NOT EXISTS `ledger_entries` (
|
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL,
|
|
`debit_account_id` BIGINT UNSIGNED NOT NULL, `credit_account_id` BIGINT UNSIGNED NOT NULL,
|
|
`amount_fils` BIGINT UNSIGNED NOT NULL, `currency` CHAR(3) NOT NULL DEFAULT 'JOD',
|
|
`reference_type` VARCHAR(64) NOT NULL, `reference_id` VARCHAR(128) NOT NULL,
|
|
`reason` VARCHAR(500) NOT NULL, `policy_version` VARCHAR(64) NOT NULL,
|
|
`reversal_of_entry_id` BIGINT UNSIGNED NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (`id`), UNIQUE KEY `uq_ledger_entry_uuid` (`uuid`),
|
|
UNIQUE KEY `uq_ledger_reference` (`reference_type`,`reference_id`,`debit_account_id`,`credit_account_id`),
|
|
KEY `idx_ledger_debit` (`debit_account_id`), KEY `idx_ledger_credit` (`credit_account_id`),
|
|
CONSTRAINT `fk_ledger_debit` FOREIGN KEY (`debit_account_id`) REFERENCES `ledger_accounts` (`id`) ON DELETE RESTRICT,
|
|
CONSTRAINT `fk_ledger_credit` FOREIGN KEY (`credit_account_id`) REFERENCES `ledger_accounts` (`id`) ON DELETE RESTRICT,
|
|
CONSTRAINT `fk_ledger_reversal` FOREIGN KEY (`reversal_of_entry_id`) REFERENCES `ledger_entries` (`id`) ON DELETE RESTRICT
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
CREATE TABLE IF NOT EXISTS `teacher_withdrawal_holds` (
|
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `teacher_id` BIGINT UNSIGNED NOT NULL,
|
|
`amount_fils` BIGINT UNSIGNED NOT NULL, `status` ENUM('held','released','settled','cancelled') NOT NULL DEFAULT 'held',
|
|
`idempotency_key` VARCHAR(128) NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `released_at` TIMESTAMP NULL,
|
|
PRIMARY KEY (`id`), UNIQUE KEY `uq_withdrawal_hold_uuid` (`uuid`), UNIQUE KEY `uq_withdrawal_idempotency` (`teacher_id`,`idempotency_key`),
|
|
KEY `idx_withdrawal_teacher_status` (`teacher_id`,`status`), CONSTRAINT `fk_withdrawal_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teachers` (`id`) ON DELETE RESTRICT
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|