Files
wms-app/migrations/20260513_002_create_accounting_tables.sql
T
2026-05-13 15:11:24 +07:00

88 lines
4.7 KiB
SQL

-- db: wms2
-- ── Chart of Accounts ────────────────────────────────────────────────────────
CREATE TABLE `ac_account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`account_code` varchar(20) NOT NULL,
`account_name` varchar(255) NOT NULL,
`account_type` ENUM('asset','liability','equity','revenue','expense') NOT NULL,
`parent_code` varchar(20) DEFAULT NULL,
`is_posting` tinyint(1) NOT NULL DEFAULT 1 COMMENT '0=header only, 1=can post',
`status` tinyint(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_company_account` (`company_id`, `account_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ── Departments ───────────────────────────────────────────────────────────────
CREATE TABLE `ac_department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`dept_code` varchar(20) NOT NULL,
`dept_name` varchar(255) NOT NULL,
`description` text DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_company_dept` (`company_id`, `dept_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ── Accounting Periods ────────────────────────────────────────────────────────
CREATE TABLE `ac_period` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`period` varchar(7) NOT NULL COMMENT 'YYYY-MM',
`status` ENUM('open','closed','locked') NOT NULL DEFAULT 'open',
`closed_at` datetime DEFAULT NULL,
`locked_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_company_period` (`company_id`, `period`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ── Journal Entry Headers ─────────────────────────────────────────────────────
CREATE TABLE `ac_journal` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`je_number` varchar(50) NOT NULL,
`period` varchar(7) NOT NULL COMMENT 'YYYY-MM',
`je_date` date NOT NULL,
`description` text DEFAULT NULL,
`source_type` varchar(50) DEFAULT NULL COMMENT 'invoice, po, receipt, payment, manual',
`source_id` int(11) DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1=posted, 0=draft, -1=voided',
`created_by` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `uq_company_je` (`company_id`, `je_number`),
KEY `idx_period` (`company_id`, `period`),
KEY `idx_source` (`company_id`, `source_type`, `source_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ── Journal Entry Lines ───────────────────────────────────────────────────────
CREATE TABLE `ac_journal_line` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`journal_id` int(11) NOT NULL,
`account_code` varchar(20) NOT NULL,
`dept_id` int(11) DEFAULT NULL,
`description` text DEFAULT NULL,
`debit` decimal(15,2) NOT NULL DEFAULT 0.00,
`credit` decimal(15,2) NOT NULL DEFAULT 0.00,
PRIMARY KEY (`id`),
KEY `idx_journal` (`journal_id`),
KEY `idx_account` (`company_id`, `account_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ── Period Balance Summary ────────────────────────────────────────────────────
-- Mirrors warehouse_balance: aggregated debit/credit per account+dept+period
CREATE TABLE `ac_balance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`period` varchar(7) NOT NULL COMMENT 'YYYY-MM',
`account_code` varchar(20) NOT NULL,
`dept_id` int(11) DEFAULT NULL,
`total_debit` decimal(15,2) NOT NULL DEFAULT 0.00,
`total_credit` decimal(15,2) NOT NULL DEFAULT 0.00,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_balance` (`company_id`, `period`, `account_code`, `dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;