-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGetTimesheet.sql
99 lines (76 loc) · 3.25 KB
/
GetTimesheet.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
DELIMITER $$
DROP PROCEDURE IF EXISTS `GetTimesheet`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetTimesheet` (IN `UserId` VARCHAR(20), IN `StartDate` DATE, IN `EndDate` DATE) MODIFIES SQL DATA
BEGIN
-- Calculates the total worked time and total overtime, including currently open jobs.
-- See method used in overview.sql
-- DROP TABLE IF EXISTS jobDurations;
CREATE TEMPORARY TABLE jobDurations (recordDate DATE, jobId VARCHAR(20), duration INT, overtimeDuration INT);
-- test
-- SELECT * FROM jobDurations;
-- handle open records first
INSERT INTO jobDurations (recordDate, jobId, duration, overtimeDuration)
SELECT
recordDate,
timeLog.jobId,
TIME_TO_SEC(TIMEDIFF(CURRENT_TIME, clockOnTime)),
CalcOvertimeDuration(clockOnTime, CURRENT_TIME, CURRENT_DATE)
FROM timeLog WHERE clockOffTime IS NULL
AND timeLog.userId=UserId
AND recordDate >= StartDate
AND recordDate <= EndDate;
-- test
-- SELECT * FROM jobDurations;
-- Then the rest of the records
INSERT INTO jobDurations (recordDate, jobId, duration, overtimeDuration)
SELECT
recordDate,
timeLog.jobId,
TIME_TO_SEC(TIMEDIFF(clockOffTime, clockOnTime)),
CalcOvertimeDuration(clockOnTime, clockOffTime, recordDate)
FROM timeLog WHERE clockOffTime IS NOT NULL
AND timeLog.userId=UserId
AND recordDate >= StartDate
AND recordDate <= EndDate;
-- test
-- SELECT * FROM jobDurations;
CREATE INDEX IDX_durations_date_jobId ON jobDurations(recordDate, jobId);
SELECT paramValue INTO @allowMultipleClockOn
FROM config WHERE paramName = "allowMultipleClockOn" LIMIT 1;
-- Note that if multiple jobs may be clocked onto simultaneously by a
-- single user, then the total worked time and overtime is considered
-- to be undefined.
IF @allowMultipleClockOn = "true" THEN
SET @totalDuration = -1;
SET @totalOvertimeDuration = -1;
ELSE
SELECT SUM(duration) INTO @totalDuration FROM jobDurations;
SELECT SUM(overtimeDuration) INTO @totalOvertimeDuration FROM jobDurations;
END IF;
-- Create a list of unique IDs. This is returned as the first of two results sets.
SELECT DISTINCT jobDurations.jobId FROM jobDurations ORDER BY jobId ASC;
-- This is for getting Job Names
SELECT
jobDurations.jobId,
jobs.jobName
FROM jobDurations
LEFT JOIN jobs
ON jobDurations.jobId = jobs.jobId
GROUP BY jobDurations.jobId;
-- This is for getting Product Ids
SELECT
jobDurations.jobId,
jobs.productId
FROM jobDurations
LEFT JOIN jobs
ON jobDurations.jobId = jobs.jobId
GROUP BY jobId;
-- This is for getting Aggregate Times
SELECT jobId, SUM(duration) AS workedDuration, SUM(overtimeDuration) AS overtimeDuration FROM jobDurations GROUP BY jobId;
-- select the times from the table, ordered appropriately. This following result set is
-- processed into the rows and columns of a time sheet in the PHP code that called
-- this procedure.
SELECT recordDate, jobId, SUM(duration) AS workedDuration, SUM(overtimeDuration) AS overtimeDuration FROM jobDurations GROUP BY recordDate, jobId ORDER BY recordDate;
SELECT @totalDuration, @totalOvertimeDuration;
END$$
DELIMITER ;;