It looks like you're working on a complex trigger or stored procedure to manage employee punch data, including in-time and out-time, based on the shift schedule and timing rules. The goal is to ensure that the in_time and out_time are correctly updated or inserted based on the current punch time and existing records in the mach_punch table. However, you mentioned that the comparison between in_time and out_time is not working as expected.
Let's break down the issue and try to address it. The main problem, as I understand, is that when a new punch is inserted or updated, it should correctly update the in_time and out_time fields in the mach_punch table. For example:
- If the current punch time is before the existing
in_time, it should update the in_time.
- If the current punch time is after the existing
out_time, it should update the out_time.
- If the punch time is within the existing
in_time and out_time, it should handle it based on the logic (e.g., update attendance percentage).
Key Points to Address:
- Comparison of
in_time and out_time: The logic should correctly identify whether the new punch time is before the existing in_time, after the existing out_time, or within the range.
- Handling
in_time and out_time updates: If the new punch time is before the existing in_time, update in_time. If it's after the existing out_time, update out_time.
- Trans_type Handling: The logic for
trans_type = 'I' (in-time) and trans_type = 'O' (out-time) should be clearly separated.
Issues in the Current Code:
From the code you provided, there are a few areas where the comparison and update logic for in_time and out_time might not be working as expected:
-
No Clear Logic for Swapping in_time and out_time: If a punch time is before the existing in_time, it should update in_time, and if it's after out_time, it should update out_time. However, the current logic seems to be more focused on percentages (day_pr, att_start) rather than directly comparing and updating in_time and out_time.
-
Redundant Conditions: There are multiple nested conditions that might be overcomplicating the logic, especially when dealing with in_time and out_time.
-
Trans_type Handling: The logic for trans_type = 'I' and trans_type = 'O' is not clearly separated from the logic when trans_type is NULL. This can lead to confusion.
Simplified and Corrected Logic:
Let’s refactor the logic to ensure that the in_time and out_time are compared and updated correctly. We can break it down into the following steps:
-
If trans_type is NULL:
- Determine if the current punch time is for in_time or out_time based on the shift timing.
- If no record exists for the employee on that date and shift, insert a new record with
in_time or out_time.
- If a record exists, update
in_time or out_time based on the current punch time.
-
If trans_type = 'I' (In-time):
- Update the
in_time for the existing record.
-
If trans_type = 'O' (Out-time):
- Update the
out_time for the existing record.
Refactored Code:
Below is the refactored version of your trigger logic. I’ll focus on the core part where in_time and out_time are compared and updated.
1. When trans_type is NULL:
- Determine if the current punch time is closer to in_time or out_time based on the shift schedule.
- Update or insert the record accordingly.
2. When trans_type = 'I' or trans_type = 'O':
- Directly update
in_time or out_time based on the trans_type.
Here’s the refactored trigger logic:
DELIMITER //
CREATE TRIGGER your_trigger_name
AFTER INSERT ON your_punch_table
FOR EACH ROW
BEGIN
DECLARE emp_cd VARCHAR(10) DEFAULT NULL;
DECLARE punch_dt DATE;
DECLARE punch_shift VARCHAR(10) DEFAULT NULL;
DECLARE mp_id INT DEFAULT NULL;
DECLARE mp_type VARCHAR(1) DEFAULT NULL;
DECLARE in_time DATETIME;
DECLARE out_time DATETIME;
DECLARE att_start DECIMAL(11,2);
DECLARE day_pr DECIMAL(11,2);
DECLARE in_percent DECIMAL(11,2);
DECLARE out_percent DECIMAL(11,2);
DECLARE pre_calc_date DATE;
DECLARE post_calc_date DATE;
-- Calculate previous and next day
SET pre_calc_date = DATE_SUB(NEW.date, INTERVAL 1 DAY);
SET post_calc_date = DATE_ADD(NEW.date, INTERVAL 1 DAY);
-- Get employee details and shift information
SELECT
em.EMP_NUMBER,
CASE
WHEN CAST(CONCAT(NEW.date, ' ', NEW.time) AS DATETIME)
BETWEEN a.ls_punch_start_time
AND IF(a.ls_punch_end_time > a.cs_punch_start_time,
DATE_SUB(a.cs_punch_start_time, INTERVAL 1 SECOND),
a.ls_punch_end_time)
THEN DATE(a.ls_punch_start_time)
ELSE DATE(a.cs_punch_start_time)
END AS punch_dt,
CASE
WHEN CAST(CONCAT(NEW.date, ' ', NEW.time) AS DATETIME)
BETWEEN a.ls_punch_start_time
AND IF(a.ls_punch_end_time > a.cs_punch_start_time,
DATE_SUB(a.cs_punch_start_time, INTERVAL 1 SECOND),
a.ls_punch_end_time)
THEN a.last_shift
ELSE a.curr_shift
END AS punch_shift
INTO
emp_cd,
punch_dt,
punch_shift
FROM
(
SELECT
em.EMP_NUMBER,
IFNULL(esp.new_shift, em.SHIFT) AS last_shift,
CASE
WHEN esp.new_shift IS NOT NULL AND sm.next_day = 1
THEN CONCAT(pre_calc_date, ' ', sm.punch_start_time)
WHEN esp.new_shift IS NOT NULL
THEN CONCAT(NEW.date, ' ', sm.punch_start_time)
WHEN sm2.next_day = 1
THEN CONCAT(pre_calc_date, ' ', sm2.punch_start_time)
ELSE CONCAT(NEW.date, ' ', sm2.punch_start_time)
END AS ls_punch_start_time,
CASE
WHEN esp.new_shift IS NOT NULL AND sm.next_day = 1
THEN CONCAT(NEW.date, ' ', sm.punch_end_time)
WHEN esp.new_shift IS NOT NULL
THEN CONCAT(pre_calc_date, ' ', sm.punch_end_time)
WHEN sm2.next_day = 1
THEN CONCAT(NEW.date, ' ', sm2.punch_end_time)
ELSE CONCAT(pre_calc_date, ' ', sm2.punch_end_time)
END AS ls_punch_end_time,
IFNULL(esp1.new_shift, em.SHIFT) AS curr_shift,
CONCAT(NEW.date, ' ', COALESCE(sm1.punch_start_time, sm2.punch_start_time)) AS cs_punch_start_time,
CASE
WHEN esp1.new_shift IS NOT NULL AND sm1.next_day = 1
THEN CONCAT(post_calc_date, ' ', sm1.punch_end_time)
WHEN esp1.new_shift IS NOT NULL
THEN CONCAT(NEW.date, ' ', sm1.punch_end_time)
WHEN sm2.next_day = 1
THEN CONCAT(post_calc_date, ' ', sm2.punch_end_time)
ELSE CONCAT(NEW.date, ' ', sm2.punch_end_time)
END AS cs_punch_end_time
FROM
emp_master em
LEFT JOIN
emp_shift_plan esp ON esp.emp_cd = em.EMP_NUMBER
AND pre_calc_date BETWEEN esp.from_dt AND esp.to_dt
AND esp.app_by IS NOT NULL
LEFT JOIN
shift_master sm ON sm.shift_code = esp.new_shift
LEFT JOIN
emp_shift_plan esp1 ON esp1.emp_cd = em.EMP_NUMBER
AND NEW.date BETWEEN esp1.from_dt AND esp1.to_dt
AND esp1.app_by IS NOT NULL
LEFT JOIN
shift_master sm1 ON sm1.shift_code = esp1.new_shift
LEFT JOIN
shift_master sm2 ON em.SHIFT = sm2.shift_code
WHERE
em.CARD_NO = NEW.punchingcode
AND em.ACTIVE_STATUS = 'Y'
) a
LIMIT 1;
-- If employee is not found, exit the trigger
IF emp_cd IS NULL THEN
LEAVE;
END IF;
-- Get the latest mach_punch record for the employee, date, and shift
SELECT
mp.id,
mp.att_start,
mp.type,
mp.punch_trans,
mp.in_time,
mp.out_time
INTO
mp_id,
att_start,
mp_type,
punch_trans,
in_time,
out_time
FROM
mach_punch mp
WHERE
mp.date = punch_dt
AND mp.shift = punch_shift
AND mp.emp_cd = emp_cd
ORDER BY
mp.date DESC
LIMIT 1;
-- If no record exists, insert a new one
IF mp_id IS NULL THEN
-- Determine if the current time is in_time or out_time based on shift timing
SELECT
hdt.present_percent,
hdt.out_present_percent
INTO
in_percent,
out_percent
FROM
hrm_day_timing hdt
WHERE
hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time;
-- Assume if present_percent is set, it's an in_time, otherwise out_time
IF in_percent > 0 THEN
INSERT INTO mach_punch (
emp_cd,
card_cd,
date,
shift,
in_time,
att_start,
day_pr
) VALUES (
emp_cd,
NEW.punchingcode,
punch_dt,
punch_shift,
CONCAT(NEW.date, ' ', NEW.time),
in_percent,
in_percent
);
ELSE
INSERT INTO mach_punch (
emp_cd,
card_cd,
date,
shift,
out_time,
att_start,
day_pr
) VALUES (
emp_cd,
NEW.punchingcode,
punch_dt,
punch_shift,
CONCAT(NEW.date, ' ', NEW.time),
out_percent,
out_percent
);
END IF;
ELSE
-- Record exists, update in_time or out_time
SET @current_punch_time = CONCAT(NEW.date, ' ', NEW.time);
-- If trans_type is provided, update based on trans_type
IF NEW.trans_type IS NOT NULL THEN
IF NEW.trans_type = 'I' THEN
-- Update in_time
UPDATE mach_punch
SET
in_time = @current_punch_time,
att_start = (
SELECT hdt.present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
),
day_pr = (
SELECT hdt.present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
)
WHERE id = mp_id;
ELSEIF NEW.trans_type = 'O' THEN
-- Update out_time
UPDATE mach_punch
SET
out_time = @current_punch_time,
day_pr = (
att_start - (
SELECT hdt.out_present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
)
)
WHERE id = mp_id;
END IF;
ELSE
-- trans_type is NULL, determine in_time or out_time
IF @current_punch_time < in_time THEN
-- Update in_time if current punch is before in_time
UPDATE mach_punch
SET
in_time = @current_punch_time,
att_start = (
SELECT hdt.present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
),
day_pr = (
SELECT hdt.present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
)
WHERE id = mp_id;
ELSEIF @current_punch_time > out_time THEN
-- Update out_time if current punch is after out_time
UPDATE mach_punch
SET
out_time = @current_punch_time,
day_pr = (
att_start - (
SELECT hdt.out_present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
)
)
WHERE id = mp_id;
ELSE
-- Punch time is between in_time and out_time, update day_pr
UPDATE mach_punch
SET
day_pr = (
att_start - (
SELECT hdt.out_present_percent
FROM hrm_day_timing hdt
WHERE hdt.shift = punch_shift
AND NEW.time BETWEEN hdt.from_time AND hdt.to_time
)
)
WHERE id = mp_id;
END IF;
END IF;
END IF;
END //
DELIMITER ;
Explanation of Changes:
-
Determine in_time or out_time when trans_type is NULL:
- If the current punch time is before the existing
in_time, update in_time.
- If the current punch time is after the existing
out_time, update out_time.
- If it’s within the range, update the attendance percentage (
day_pr).
-
Handle trans_type = 'I' and trans_type = 'O':
- If
trans_type = 'I', directly update in_time.
- If
trans_type = 'O', directly update out_time.
-
Insert a New Record if None Exists:
- If no record is found for the employee on that date and shift, a new record is inserted with either
in_time or out_time based on the shift timing.
Example:
Testing the Logic:
You can test the logic with the following scenarios:
- Insert a punch before the existing
in_time.
- Insert a punch after the existing
out_time.
- Insert a punch within the existing
in_time and out_time.
- Use
trans_type = 'I' or trans_type = 'O' to update in_time or out_time directly.
Example Queries to Test:
1. Insert a punch before in_time:
INSERT INTO your_punch_table (punchingcode, date, time) VALUES ('12345', '2025-07-21', '08:00:00');
2. Insert a punch after out_time:
INSERT INTO your_punch_table (punchingcode, date, time) VALUES ('12345', '2025-07-21', '19:00:00');
3. Insert with trans_type = 'I':
INSERT INTO your_punch_table (punchingcode, date, time, trans_type) VALUES ('12345', '2025-07-21', '08:30:00', 'I');
4. Insert with trans_type = 'O':
INSERT INTO your_punch_table (punchingcode, date, time, trans_type) VALUES ('12345', '2025-07-21', '18:30:00', 'O');
Conclusion:
This refactored trigger should correctly compare and update in_time and out_time based on the new punch time. If there are any specific edge cases or additional requirements, feel free to provide more details, and I can further refine the logic.