109 行
3.9 KiB
SQL
109 行
3.9 KiB
SQL
-- CreateEnum
|
|
CREATE TYPE "SalaryItemType" AS ENUM ('earning', 'deduction');
|
|
|
|
-- CreateEnum
|
|
CREATE TYPE "PayslipStatus" AS ENUM ('draft', 'issued');
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "salary_items" (
|
|
"id" UUID NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"item_type" "SalaryItemType" NOT NULL,
|
|
"sort_order" INTEGER NOT NULL,
|
|
"enabled" BOOLEAN NOT NULL DEFAULT true,
|
|
"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 "salary_items_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "employee_salaries" (
|
|
"id" UUID NOT NULL,
|
|
"employee_id" UUID NOT NULL,
|
|
"salary_item_id" UUID NOT NULL,
|
|
"monthly_amount" DECIMAL(12,2) NOT NULL,
|
|
"effective_date" DATE NOT NULL,
|
|
"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 "employee_salaries_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "payslips" (
|
|
"id" UUID NOT NULL,
|
|
"employee_id" UUID NOT NULL,
|
|
"pay_month" DATE NOT NULL,
|
|
"status" "PayslipStatus" NOT NULL DEFAULT 'draft',
|
|
"gross_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
|
|
"deduction_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
|
|
"net_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
|
|
"issued_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 "payslips_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "payslip_items" (
|
|
"id" UUID NOT NULL,
|
|
"payslip_id" UUID NOT NULL,
|
|
"salary_item_id" UUID NOT NULL,
|
|
"amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
|
|
|
|
CONSTRAINT "payslip_items_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "salary_items_name_key" ON "salary_items"("name");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_salary_items_enabled_sort" ON "salary_items"("enabled", "sort_order");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "employee_salaries_employee_item_effective_ux" ON "employee_salaries"("employee_id", "salary_item_id", "effective_date");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_employee_salaries_employee_effective" ON "employee_salaries"("employee_id", "effective_date");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_employee_salaries_salary_item" ON "employee_salaries"("salary_item_id");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "payslips_employee_month_ux" ON "payslips"("employee_id", "pay_month");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_payslips_month_status" ON "payslips"("pay_month", "status");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_payslips_employee_month" ON "payslips"("employee_id", "pay_month");
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "payslip_items_payslip_salary_item_ux" ON "payslip_items"("payslip_id", "salary_item_id");
|
|
|
|
-- CreateIndex
|
|
CREATE INDEX "idx_payslip_items_salary_item" ON "payslip_items"("salary_item_id");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "employee_salaries" ADD CONSTRAINT "employee_salaries_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "employee_salaries" ADD CONSTRAINT "employee_salaries_salary_item_id_fkey" FOREIGN KEY ("salary_item_id") REFERENCES "salary_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "payslips" ADD CONSTRAINT "payslips_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "payslip_items" ADD CONSTRAINT "payslip_items_payslip_id_fkey" FOREIGN KEY ("payslip_id") REFERENCES "payslips"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "payslip_items" ADD CONSTRAINT "payslip_items_salary_item_id_fkey" FOREIGN KEY ("salary_item_id") REFERENCES "salary_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|