diff --git a/prisma/migrations/20250724191659_create_pay_period_view/migration.sql b/prisma/migrations/20250724191659_create_pay_period_view/migration.sql index c58f9c8..4e6ba73 100644 --- a/prisma/migrations/20250724191659_create_pay_period_view/migration.sql +++ b/prisma/migrations/20250724191659_create_pay_period_view/migration.sql @@ -1,47 +1,32 @@ --- This is an empty migration. -- Date d'ancrage fix. Période 1 = 2023-12-17 => 2023-12-30 -WITH anchor AS ( - SELECT '2023-12-17'::date AS anchor_date -), - - ---definition des bornes d'archivage et de visionnement futur ( 6 mois avant la periode actuelle et 1 mois apres la periode actuelle) -With bounds As ( +-- Definition des bornes d'archivage et de visionnement futur ( 6 mois avant la periode actuelle et 1 mois apres la periode actuelle) +-- Label pour affichage +-- Determination des dates d'une periode +CREATE OR REPLACE VIEW pay_period AS +WITH + anchor AS ( + SELECT '2023-12-17'::date AS anchor_date + ), + bounds AS ( SELECT - (now()::date - INTERVAL ' 6 months')::date AS start_bound, - (now()::date - INTERVAL ' 1 month')::date AS end_bound, - anchor.anchor_date + (now()::date - INTERVAL '6 months')::date AS start_bound, + (now()::date + INTERVAL '1 month')::date AS end_bound, + anchor.anchor_date FROM anchor -), - ---determination des dates d'une periode -series AS ( + ), + series AS ( SELECT - generate_series( - bounds.start_bound, - bounds.end_bound, - '14 days' - ) AS period_start - bounds.anchor_date + generate_series(bounds.start_bound, bounds.end_bound, '14 days') AS period_start, + bounds.anchor_date FROM bounds -) - ---creation de la vue -CREATE OR REPACE VIEW "pay_period" AS + ) SELECT - - ((row_number() OVER (ORDER BY period_start) - 1) % 26) + 1 - AS period_number, - - period_start AS start_date, --date de début de la periode - period_start + INTERVAL '13 days' AS end_date, -- date de fin de la periode - - EXTRACT(YEAR FROM period_start)::int AS year, - - --label pour affichage - || ' → ' - || to_char(period_start + INTERVAL '13 days', 'YYYY-MM-DD') + ((row_number() OVER (ORDER BY period_start) - 1) % 26) + 1 AS period_number, + period_start AS start_date, + period_start + INTERVAL '13 days' AS end_date, + EXTRACT(YEAR FROM period_start)::int AS year, + period_start || ' → ' || + to_char(period_start + INTERVAL '13 days', 'YYYY-MM-DD') AS label - -FROM series; -OERDER BY period_start; \ No newline at end of file +FROM series +ORDER BY period_start; diff --git a/prisma/migrations/20250728181507_archive_and_minor_fixes/migration.sql b/prisma/migrations/20250728181507_archive_and_minor_fixes/migration.sql new file mode 100644 index 0000000..512140f --- /dev/null +++ b/prisma/migrations/20250728181507_archive_and_minor_fixes/migration.sql @@ -0,0 +1,181 @@ +/* + Warnings: + + - You are about to drop the `ExpenseCodes` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `Expenses` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "Expenses" DROP CONSTRAINT "Expenses_expense_code_id_fkey"; + +-- DropForeignKey +ALTER TABLE "Expenses" DROP CONSTRAINT "Expenses_timesheet_id_fkey"; + +-- AlterTable +ALTER TABLE "employees" ADD COLUMN "supervisor_id" INTEGER, +ALTER COLUMN "first_work_day" SET DATA TYPE DATE, +ALTER COLUMN "last_work_day" SET DATA TYPE DATE; + +-- AlterTable +ALTER TABLE "leave_requests" ALTER COLUMN "start_date_time" SET DATA TYPE DATE, +ALTER COLUMN "end_date_time" SET DATA TYPE DATE; + +-- AlterTable +ALTER TABLE "shifts" ALTER COLUMN "date" SET DATA TYPE DATE, +ALTER COLUMN "start_time" SET DATA TYPE TIME(0), +ALTER COLUMN "end_time" SET DATA TYPE TIME(0); + +-- DropTable +DROP TABLE "ExpenseCodes"; + +-- DropTable +DROP TABLE "Expenses"; + +-- CreateTable +CREATE TABLE "employees_archive" ( + "id" SERIAL NOT NULL, + "employee_id" INTEGER NOT NULL, + "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "user_id" UUID NOT NULL, + "first_name" TEXT NOT NULL, + "last_name" TEXT NOT NULL, + "external_payroll_id" INTEGER NOT NULL, + "company_code" INTEGER NOT NULL, + "first_Work_Day" DATE NOT NULL, + "last_work_day" DATE NOT NULL, + "supervisor_id" INTEGER, + + CONSTRAINT "employees_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "customers_archive" ( + "id" SERIAL NOT NULL, + "customer_id" INTEGER NOT NULL, + "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "user_id" UUID NOT NULL, + "invoice_id" INTEGER, + + CONSTRAINT "customers_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "leave_requests_archive" ( + "id" SERIAL NOT NULL, + "leave_request_id" INTEGER NOT NULL, + "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "employee_id" INTEGER NOT NULL, + "leave_type" "leave_types" NOT NULL, + "start_date_time" DATE NOT NULL, + "end_date_time" DATE, + "comment" TEXT NOT NULL, + "approval_status" "leave_approval_status" NOT NULL, + + CONSTRAINT "leave_requests_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "timesheets_archive" ( + "id" SERIAL NOT NULL, + "timesheet_id" INTEGER NOT NULL, + "archive_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "employee_id" INTEGER NOT NULL, + "is_approved" BOOLEAN NOT NULL, + + CONSTRAINT "timesheets_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "shifts_archive" ( + "id" SERIAL NOT NULL, + "shift_id" INTEGER NOT NULL, + "archive_at" TIMESTAMP(3) NOT NULL, + "timesheet_id" INTEGER NOT NULL, + "shift_code_id" INTEGER NOT NULL, + "description" TEXT, + "date" DATE NOT NULL, + "start_time" TIME(0) NOT NULL, + "end_time" TIME(0) NOT NULL, + + CONSTRAINT "shifts_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "expenses" ( + "id" SERIAL NOT NULL, + "timesheet_id" INTEGER NOT NULL, + "expense_code_id" INTEGER NOT NULL, + "date" DATE NOT NULL, + "amount" MONEY NOT NULL, + "attachement" TEXT, + "description" TEXT, + "is_approved" BOOLEAN NOT NULL DEFAULT false, + "supervisor_comment" TEXT, + + CONSTRAINT "expenses_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "expenses_archive" ( + "id" SERIAL NOT NULL, + "expense_id" INTEGER NOT NULL, + "timesheet_id" INTEGER NOT NULL, + "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "expense_code_id" INTEGER NOT NULL, + "date" DATE NOT NULL, + "amount" MONEY NOT NULL, + "attachement" TEXT, + "description" TEXT, + "is_approved" BOOLEAN NOT NULL, + "supervisor_comment" TEXT, + + CONSTRAINT "expenses_archive_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "expense_codes" ( + "id" SERIAL NOT NULL, + "expense_type" TEXT NOT NULL, + "bank_code" TEXT NOT NULL, + + CONSTRAINT "expense_codes_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "customers_archive_invoice_id_key" ON "customers_archive"("invoice_id"); + +-- AddForeignKey +ALTER TABLE "employees" ADD CONSTRAINT "employees_supervisor_id_fkey" FOREIGN KEY ("supervisor_id") REFERENCES "employees"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "employees_archive" ADD CONSTRAINT "employees_archive_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "employees_archive" ADD CONSTRAINT "employees_archive_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "employees_archive" ADD CONSTRAINT "employees_archive_supervisor_id_fkey" FOREIGN KEY ("supervisor_id") REFERENCES "employees"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "customers_archive" ADD CONSTRAINT "customers_archive_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "customers"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "customers_archive" ADD CONSTRAINT "customers_archive_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "leave_requests_archive" ADD CONSTRAINT "leave_requests_archive_leave_request_id_fkey" FOREIGN KEY ("leave_request_id") REFERENCES "leave_requests"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "timesheets_archive" ADD CONSTRAINT "timesheets_archive_timesheet_id_fkey" FOREIGN KEY ("timesheet_id") REFERENCES "timesheets"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "shifts_archive" ADD CONSTRAINT "shifts_archive_shift_id_fkey" FOREIGN KEY ("shift_id") REFERENCES "shifts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "expenses" ADD CONSTRAINT "expenses_timesheet_id_fkey" FOREIGN KEY ("timesheet_id") REFERENCES "timesheets"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "expenses" ADD CONSTRAINT "expenses_expense_code_id_fkey" FOREIGN KEY ("expense_code_id") REFERENCES "expense_codes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "expenses_archive" ADD CONSTRAINT "expenses_archive_expense_id_fkey" FOREIGN KEY ("expense_id") REFERENCES "expenses"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e1ec7b7..7ca504c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -6,6 +6,7 @@ generator client { provider = "prisma-client-js" + previewFeatures = ["views"] } datasource db { @@ -128,12 +129,12 @@ model LeaveRequestsArchive { } //pay-period vue -model PayPeriods { +view PayPeriods { period_number Int @id - start_date DateTime @db.Date - end_date DateTime @db.Date - year Int - label String + start_date DateTime? @db.Date + end_date DateTime? @db.Date + year Int? + label String? @@map("pay_period") }