calculate-break
sidebar_position: 4
CalculateBreaks Function
Purpose
The CalculateBreaks
function is responsible for calculating and updating break information within an attendance summary. It processes breaks based on shift configurations, attendance scan logs, and predefined rules. The function ensures accurate computation of break details such as start times, end times, durations, and any related overtime or discrepancies.
Function Signature
func CalculateBreaks(summary *AttendanceSummariesCompute, scanLogs []*entity.AttendanceLogTable)
Parameters
summary
(*AttendanceSummariesCompute
):A pointer to an attendance summary object that contains shift details, scan points, and computation rules.
scanLogs
([]*entity.AttendanceLogTable
):A slice of attendance log entries used as raw data for the calculation.
Key Functionality
- Preliminary Checks:
- If no shift is defined in
summary.Shift
, the computation is skipped.
- If no shift is defined in
- Iterate Over Shift Breaks:
- For each break defined in
summary.Shift.ShiftBreaks
, calculate the break details. - The break is matched with the corresponding scan points based on its order number.
- For each break defined in
- Existing Details Validation:
- Verify if a matching summary detail exists for the current break.
- If the break ID doesn't match, an error is logged for debugging.
- Scan Point Matching:
- Use scan points to determine the actual start (
CiActual
) and end (CoActual
) times for the break. - If no scan data is available, fallback to applicable rules.
- Use scan points to determine the actual start (
- Rule-Based Calculations:
- Apply rules for incomplete or missing scan data to estimate break times and durations.
- Mark rules as used once they are applied.
- Early, Late, and Duration Calculations:
- Compute early arrivals, late starts, and overall break durations.
- Handle overtime and its impact on breaks.
- Overwrite Handling:
- If the break details have manual overrides, ensure these values are applied accurately.
- Final Updates:
- Clear unused rules and reset overtime fields if no applicable rules are found.
- Log the completion of calculations.
Notable Concepts
Key Variables
ScanKeyIn
andScanKeyOut
:Identifiers for the "break-in" and "break-out" scan events, dynamically constructed based on break order.
tmp
:Temporary storage for the break's computed details.
RulesHit
:A collection of unused rules that may be applied for handling missing or incomplete scan data.
Rule Application
- Rules are used to fill in missing scan details or adjust calculations for discrepancies.
- Each rule can only be used once per break to ensure consistent computation.
Error Logging
- Errors such as mismatched IDs or missing summary details are logged to help with debugging and validation.
Example Use Case
Given a shift with three breaks defined and a set of scan logs:
- The function matches scan logs to breaks based on predefined order.
- If scans are missing or incomplete, rules are applied to estimate missing data.
- Results include start/end times, duration, and any overtime incurred for each break.
Remarks
**Dependencies:**The function relies on other utilities (utils.IntToString
,timedate.TimeToString
, etc.) and custom types likeAttendanceSummariesCompute
.
**Debugging:**Includes extensive debug logging, useful during development but may need optimization for production.
**Extensibility:**Rules and handling logic can be customized or expanded for more complex use cases.
Future Improvements
- Enhance error handling to include recovery mechanisms for mismatched data.
- Refactor the function for better readability and separation of concerns.
- Add unit tests to validate the functionality comprehensively.