diff --git a/calicut_textiles/public/python/payroll_entry.py b/calicut_textiles/public/python/payroll_entry.py index f7e0346..57eeb66 100644 --- a/calicut_textiles/public/python/payroll_entry.py +++ b/calicut_textiles/public/python/payroll_entry.py @@ -200,16 +200,16 @@ def load_checkins(employees, start, end): def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): settings = frappe.get_single("Calicut Textiles Settings") - ot_threshold = settings.threshold_overtime_minutes or 0 - early_threshold = settings.threshold_early_minutes or 0 - + threshold = settings.threshold_overtime_minutes or 0 excluded_shift = settings.shift ot_component = settings.ot_component + + early_threshold = (settings.threshold_early_minutes or 0) * 2 early_component = settings.early_component for emp in employees: - # Skip if already processed + # Skip if OT already created if frappe.db.exists( "Additional Salary", { @@ -217,8 +217,8 @@ def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): "salary_component": ot_component, "payroll_date": pe.end_date, "custom_is_overtime": 1, - "docstatus": 1, - }, + "docstatus": 1 + } ): continue @@ -226,14 +226,14 @@ def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): if not emp_doc: continue - if ( - not emp_doc.default_shift - or emp_doc.default_shift == excluded_shift - or emp_doc.employment_type == "Part-time" - ): + shift_name = emp_doc.default_shift + if not shift_name or shift_name == excluded_shift: continue - shift = frappe.get_doc("Shift Type", emp_doc.default_shift) + if emp_doc.employment_type == "Part-time": + continue + + shift = frappe.get_doc("Shift Type", shift_name) shift_hours = get_shift_hours(shift) if shift_hours <= 0: continue @@ -251,41 +251,53 @@ def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): in_time = times[0] out_time = times[-1] - shift_start, shift_end = shift_bounds(shift, date) - - # ================= LATE / EARLY (ALL DAYS) ================= - - # Late entry - if in_time > shift_start: - late_minutes = minutes(in_time - shift_start) - if late_minutes > early_threshold: - total_early_late_minutes += late_minutes - - # Early exit - if out_time < shift_end: - early_exit_minutes = minutes(shift_end - out_time) - if early_exit_minutes > early_threshold: - total_early_late_minutes += early_exit_minutes - - # ================= OVERTIME ================= - + # ---------------- HOLIDAY LOGIC ---------------- if date in holidays: - # Holiday → full worked duration is OT worked_minutes = minutes(out_time - in_time) - if worked_minutes > ot_threshold: + if worked_minutes > 0: total_ot_minutes += worked_minutes - else: - # Normal day → only after shift end - if out_time > shift_end: - ot_minutes = minutes(out_time - shift_end) - if ot_minutes > ot_threshold: - total_ot_minutes += ot_minutes + continue # CRITICAL: skip early/late completely + # ------------------------------------------------ + + shift_start, shift_end = shift_bounds(shift, date) + + normal_start = shift_start - timedelta(minutes=threshold) + normal_end = shift_end + timedelta(minutes=threshold) + + normal_lateearly_start = shift_start + timedelta(minutes=early_threshold) + normal_lateearly_end = shift_end - timedelta(minutes=early_threshold) + + # ---------------- OVERTIME ---------------- + if in_time < normal_start: + total_ot_minutes += threshold + minutes(normal_start - in_time) + + if out_time > normal_end: + total_ot_minutes += threshold + minutes(out_time - normal_end) + # -------------------------------------------- + + # ----------- EARLY / LATE (NON-HOLIDAY ONLY) ----------- + print("In Time:", in_time, "Normal Late Early Start:", normal_lateearly_start) + if in_time > normal_lateearly_start: + print("Adding Late Minutes") + total_early_late_minutes += early_threshold + minutes(in_time - normal_lateearly_start) + if out_time < normal_lateearly_end: + print("Adding Early Minutes") + total_early_late_minutes += early_threshold + minutes(normal_lateearly_end - out_time) + print("Total Early/Late Minutes so far:", total_early_late_minutes) + # for row in rows: + # if row.custom_late_early: + # print(row.custom_late_early, row.time) + # custom_minutes = int(row.custom_late_early) + # if custom_minutes > early_threshold: + # print("Adding", custom_minutes) + # total_early_late_minutes += custom_minutes + # ------------------------------------------------------ rate = get_per_minute_salary( emp, pe.start_date, pe.end_date, - shift_hours, + shift_hours ) if total_ot_minutes > 0: @@ -294,7 +306,7 @@ def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): pe.end_date, total_ot_minutes, round(rate * total_ot_minutes, 2), - ot_component, + ot_component ) if total_early_late_minutes > 0: @@ -303,7 +315,7 @@ def create_overtime(pe, employees, employee_map, checkin_map, holiday_map): pe.end_date, total_early_late_minutes, round(rate * total_early_late_minutes, 2), - early_component, + early_component ) # =====================================================