implement Gehaltsaufstellung dyflexis data access

This commit is contained in:
Marcel Arndt
2025-11-18 13:32:36 +01:00
parent 4abe9e44d8
commit 4cf79f3094
18 changed files with 1015 additions and 1107 deletions
@@ -16,6 +16,20 @@ SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--
-- *not* creating schema, since initdb creates it
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON SCHEMA public IS '';
--
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
--
@@ -120,7 +134,7 @@ CREATE TABLE public."Anomaly" (
CREATE TABLE public."AttendanceRegistration" (
"attendanceRegistrationId" integer NOT NULL,
"employeeId" integer NOT NULL,
"personnelNumber" text NOT NULL,
"personnelNumber" text,
"dateTime" timestamp(3) without time zone NOT NULL,
event text NOT NULL
);
@@ -151,7 +165,8 @@ CREATE TABLE public."AttendanceRegistrationStaging" (
"attendanceRegistrationId" integer NOT NULL,
"employeeId" integer NOT NULL,
"dateTime" timestamp(3) without time zone NOT NULL,
event text NOT NULL
event text NOT NULL,
"personnelNumber" text DEFAULT ''::text
);
@@ -286,6 +301,19 @@ CREATE TABLE public."EmployeeDyflexisStaging" (
);
--
-- Name: Holiday; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public."Holiday" (
id uuid DEFAULT gen_random_uuid() NOT NULL,
date timestamp(3) without time zone NOT NULL,
locked boolean NOT NULL,
"excludedFromPayroll" boolean NOT NULL,
"employeeId" integer NOT NULL
);
--
-- Name: Patient; Type: TABLE; Schema: public; Owner: -
--
@@ -411,6 +439,22 @@ CREATE TABLE public."PlannedTimeStaging" (
);
--
-- Name: Sickdays; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public."Sickdays" (
id uuid DEFAULT gen_random_uuid() NOT NULL,
start timestamp(3) without time zone NOT NULL,
"end" timestamp(3) without time zone NOT NULL,
hours double precision NOT NULL,
days integer NOT NULL,
note text,
locked boolean NOT NULL,
"employeeId" integer NOT NULL
);
--
-- Name: SolutionTarget; Type: TABLE; Schema: public; Owner: -
--
@@ -540,7 +584,10 @@ CREATE TABLE public."Tour" (
"createdAt" timestamp(3) without time zone,
"deletedAt" timestamp(3) without time zone,
"ticketId" uuid,
"updatedAt" timestamp(3) without time zone
"updatedAt" timestamp(3) without time zone,
"companyName" text,
"overRTW" boolean DEFAULT false NOT NULL,
schwerlast boolean DEFAULT false NOT NULL
);
@@ -616,7 +663,10 @@ CREATE TABLE public."TourSource" (
"fahrerName" text,
fahrer text,
color text,
"lastUpdate" text
"lastUpdate" text,
"companyName" text,
"overRTW" text,
schwerlast text
);
@@ -674,7 +724,10 @@ CREATE TABLE public."TourStaging" (
"revenueDeviation" double precision DEFAULT 0 NOT NULL,
"consumptionCosts" double precision DEFAULT 0 NOT NULL,
"createdAt" timestamp(3) without time zone,
"updatedAt" timestamp(3) without time zone
"updatedAt" timestamp(3) without time zone,
"companyName" text,
"overRTW" boolean DEFAULT false NOT NULL,
schwerlast boolean DEFAULT false NOT NULL
);
@@ -860,6 +913,14 @@ ALTER TABLE ONLY public."Employee"
ADD CONSTRAINT "Employee_pkey" PRIMARY KEY (id);
--
-- Name: Holiday Holiday_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."Holiday"
ADD CONSTRAINT "Holiday_pkey" PRIMARY KEY (id);
--
-- Name: PatientSource PatientSource_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -908,6 +969,14 @@ ALTER TABLE ONLY public."PlannedTime"
ADD CONSTRAINT "PlannedTime_pkey" PRIMARY KEY (id);
--
-- Name: Sickdays Sickdays_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."Sickdays"
ADD CONSTRAINT "Sickdays_pkey" PRIMARY KEY (id);
--
-- Name: SolutionTargetStep SolutionTargetStep_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -12,90 +12,136 @@ SELECT
e."personTransportCertificate"
FROM "DyflexisEmployee" as de
INNER JOIN "Employee" as e ON de.id::text = e."personnelNumber"
WHERE de."contractStart" < CURRENT_DATE AND de."contractEnd" > (CURRENT_DATE - INTERVAL '2 months');
WHERE de."contractStart" < CURRENT_DATE AND de."contractEnd" > (CURRENT_DATE - INTERVAL '2 months')
AND de.surname <> 'Shahrasebhi'
ORDER BY de.firstname ASC;
-- name: GetWorkSummaryForEmployeeInPeriod :one
WITH wt AS (
SELECT
COUNT(*) as days,
COALESCE(sum(duration), 0)::bigint as minutes
-- name: GetWeeklyTimeSummaryForEmployee :many
WITH all_time_entries AS (
SELECT
DATE_TRUNC('week', "startDate") AS week_start,
SUM("duration" / 60.0) as worked_hours,
count(DISTINCT DATE_TRUNC('day', "startDate"))::float as worked_days,
0::float AS sick_hours,
0::float AS holiday_days
FROM "WorkTime"
WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date
)
SELECT
wt.days,
(minutes / 60.0)::float as hours,
wt.minutes
FROM wt;
WHERE
"startDate" BETWEEN @start_date AND @end_date
AND "userId"::int = @employee_id
AND "startDate" IS NOT NULL
GROUP BY
DATE_TRUNC('week', "startDate")
-- name: ListPlannedTimesForEmployeeInPeriod :many
SELECT
DISTINCT ON (day)
"startDate"::DATE as day,
"startDate",
"endDate",
department,
pause,
duration
FROM "PlannedTime"
WHERE
"userId" = @employee_id AND
"startDate" > @start_date AND
"startDate" < @end_date AND
department <> 'Fortbildung' AND
note IS NULL
ORDER BY day, "startDate" ASC;
UNION ALL
-- name: ListWorkTimesForEmployeeInPeriod :many
SELECT * FROM "WorkTime"
WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date;
-- name: GetSicknessSummaryForEmployeeInPeriod :one
WITH pt AS (
SELECT
COUNT(*) as days,
COALESCE(sum(duration), 0)::bigint as minutes
FROM "PlannedTime"
WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date AND note = 'sick'
DATE_TRUNC('week', "start") AS week_start,
0::float as worked_hours,
0::float as worked_days,
SUM("hours") AS sick_hours,
0::float AS holiday_days
FROM "Sickdays"
WHERE
"start" BETWEEN @start_date AND @end_date
AND "employeeId" = @employee_id
AND "start" IS NOT NULL
GROUP BY
DATE_TRUNC('week', "start")
UNION ALL
SELECT
DATE_TRUNC('week', "date") AS week_start,
0::float as worked_hours,
0::float as worked_days,
0::float AS sick_hours,
count(*)::float AS holiday_days
FROM "Holiday"
WHERE
"date" BETWEEN @start_date AND @end_date
AND "employeeId" = @employee_id
AND "date" IS NOT NULL
GROUP BY
DATE_TRUNC('week', "date")
)
SELECT
pt.days,
(minutes / 60)::float as hours,
pt.minutes
FROM pt;
week_start::timestamp,
SUM(worked_hours)::float AS total_worked_hours,
SUM(worked_days)::float AS total_worked_days,
SUM(sick_hours)::float AS total_sick_hours,
SUM(holiday_days)::float AS total_holiday_days
FROM all_time_entries
GROUP BY
week_start
ORDER BY
week_start;
-- name: GetEmployeeDyflexis :one
SELECT * FROM "DyflexisEmployee"
WHERE
id = $1 AND
"contractStart" < CURRENT_DATE AND
"contractEnd" > (CURRENT_DATE - INTERVAL '2 months')
LIMIT 1;
-- name: EvaluatePunctualityForEmployee :one
WITH
planned_start_times AS (
SELECT
DISTINCT ON ("startDate"::DATE)
"startDate"::DATE AS plan_day,
"startDate" AS planned_start_time
FROM "PlannedTime" as p
WHERE
p."userId"::int = @employee_id::bigint
AND p."startDate" BETWEEN @start_date AND @end_date
AND p."startDate" IS NOT NULL
AND p.department <> 'Fortbildung'
AND p.note IS NULL
ORDER BY
"startDate"::DATE, "startDate" ASC
),
actual_start_times AS (
SELECT
DISTINCT ON ("startDate"::DATE)
"startDate"::DATE AS work_day,
"startDate" AS actual_start_time
FROM "WorkTime"
WHERE
"userId"::int = @employee_id::bigint
AND "startDate" BETWEEN @start_date AND @end_date
AND "startDate" IS NOT NULL
ORDER BY
"startDate"::DATE, "startDate" ASC
),
daily_comparison AS (
SELECT
p.plan_day,
p.planned_start_time,
a.actual_start_time,
(a.actual_start_time IS NOT NULL AND a.actual_start_time <= p.planned_start_time) AS was_punctual
FROM planned_start_times p
LEFT JOIN actual_start_times a ON p.plan_day = a.work_day
)
SELECT
COUNT(CASE WHEN was_punctual THEN 1 END) AS punctual_days
FROM daily_comparison;
-- name: FetchDrivenToursInInterval :many
SELECT * FROM "Tour"
WHERE "check" <> 3
AND "deletedAt" IS NULL
AND (
"patientId" IS NULL
OR "patientId" NOT IN (
'b7424477-a4b2-42f7-b7e7-a4bef24f6979',
'3554e1a9-9c07-45e5-b53c-692a77e4fa9a'
)
)
-- ID's for Pause and Feierabend respectively
AND (
"carName" IS NULL
OR "carName" NOT IN (
'Storno',
'Abgabe',
'Löschen'
)
)
AND "startDate" >= @start_date
AND "startDate" <= @end_date
AND ("driverId" = @employee_id OR "codriverId" = @employee_id)
ORDER BY "startDate" DESC;
-- name: IsEmployeesDepartmentKTW :one
SELECT COALESCE(
(
bool_or(department LIKE '%KTW%')
AND
bool_and(department <> 'Leitstelle')
),
false
)::bool as meets_requirements
FROM "PlannedTime"
WHERE
"userId"::int = @employee_id::bigint
AND "startDate" BETWEEN @start_date AND @end_date
AND department IS NOT NULL;
-- name: CountVerpflegungspauschale :one
SELECT
count(*)
FROM "WorkTime"
WHERE
duration > 480
AND "startDate" BETWEEN @start_date AND @end_date
AND "userId"::int = @employee_id::bigint;
-- name: CountTourengeldAmountInPeriod :one
WITH ToursInPeriod AS (
@@ -159,23 +205,13 @@ HighActivityDays AS (
GREATEST(0, tour_amount - 6) AS tourengeld
FROM
DailyCounts
),
TourenGeld AS (
SELECT
SUM(tourengeld)::float as tourengeld
FROM
HighActivityDays
)
SELECT
SUM(tourengeld)::float as tourengeld
FROM
HighActivityDays;
-- name: GetEmployeeByName :one
SELECT
dy.id as dyflexis_id,
dl.id as dispolive_id,
dy."contractSalaryHour"
FROM "DyflexisEmployee" as dy
INNER JOIN
"Employee" as dl ON dy.id::text = dl."personnelNumber"
WHERE
TRIM(dy.firstname) = @firstname AND
TRIM(dy.surname) = @lastname AND
"contractStart" < CURRENT_DATE AND
"contractEnd" > (CURRENT_DATE - INTERVAL '2 months')
ORDER BY dy."contractEnd" DESC;
COALESCE(tourengeld, 0.0)
FROM TourenGeld;