Skip to main content

NewAttendanceSummariesComputeFromExisting Function

This Go function is designed to process attendance summary data, either by copying existing data or applying overwrites based on specified rules and conditions. The function recalculates attendance summaries while handling shift changes, overwrites, and related details.


Function Definition

func NewAttendanceSummariesComputeFromExisting(
summaries []*entity.AttendanceSummariesTable,
curUser *entity.UserProfileTable,
force, cacheFollowForce bool,
cache *caching.AllCacheStruct,
) []AttendanceSummariesCompute

Parameters

1. summaries []*entity.AttendanceSummariesTable

  • Description: A slice of existing attendance summaries for a specific date.
  • Purpose: Provides the base data that the function processes.

2. curUser *entity.UserProfileTable

  • Description: The current user's profile data.
  • Purpose: Provides user-specific details needed for attendance computation.

3. force bool

  • Description: Flag to force recalculation.
  • Purpose: Ensures summaries are recalculated, even if existing data is present.

4. cacheFollowForce bool

  • Description: Flag to force reloading of cached data.
  • Purpose: Determines if cached values are to be refreshed during computation.

5. cache *caching.AllCacheStruct

  • Description: A struct containing cached data.
  • Purpose: Provides quick access to preloaded shifts, schedules, and other related data.

Return Value

  • Type: []AttendanceSummariesCompute
  • Description: A slice of computed attendance summaries, reflecting any updates or overwrites.

Key Concepts

1. Shift Changes

  • Detects and applies changes to shifts based on overwrites in the summary details.
  • If a shift change is detected, it removes outdated details and recalculates based on the new shift.

2. Overwrites

  • Applies overwrites to both headers and details of attendance summaries.
  • Overwrite conditions are identified through specific fields such as SummaryType and OverwriteSummaryID.

3. Force Reset

  • When force is true, all non-excluded fields in the summaries and their details are reset before recalculation.

4. Auto-Shift vs. Manual Schedule

  • Handles scenarios where shifts are determined automatically (auto-shift) or manually (based on a fixed schedule).
  • Rechecks calendar schedules when shifts are not auto-calculated.

5. Cache Utilization

  • Utilizes cached data for shifts and schedules to optimize performance.
  • Refreshes cache when cacheFollowForce is true.

Steps and Workflow

Step 1: Initialization

  • Checks if summaries is empty.
  • Extracts the date from the first summary and initializes control variables.

Step 2: Process Each Summary

  • Loops through each summary:
    • Copies existing data into a temporary variable (tmp).
    • Segregates summary details into:
      • Non-overwrite details
      • Overwrite details

Step 3: Detect and Apply Shift Changes

  • Detects shift changes based on overwrites.
  • Updates the shift and recalculates break details if a change is detected.

Step 4: Rebuild Attendance Details

  • Processes overwrite details, applying changes to corresponding non-overwrite details.

Step 5: Handle Calendar Schedules

  • For non-auto-shift scenarios, aligns summaries with calendar schedules and adds any missing shifts.

Step 6: Return Updated Summaries

  • Appends computed summaries to the result array and returns.

Key Dependencies

  • Utility Functions

    • ResetSummaryHeaderOrDetailExclude
    • CopyNonZeroFieldsOnly
    • getCalendarSchedule
    • createBreakDetails
    • createSummaryFromCalendarDetail
    • findSummaryComputeByShift
  • External Entities

    • entity.AttendanceSummariesTable
    • entity.UserProfileTable
    • caching.AllCacheStruct

Error Handling

  • Logs issues like:
    • Empty summaries array.
    • Missing calendar schedules when summaries already exist.
    • Mismatched schedule IDs between summaries and calendar schedules.

Example Usage

summaries := []*entity.AttendanceSummariesTable{ /* populated summaries */ }
user := &entity.UserProfileTable{ /* user data */ }
cache := &caching.AllCacheStruct{ /* cache data */ }
force := true
cacheFollowForce := false

result := NewAttendanceSummariesComputeFromExisting(summaries, user, force, cacheFollowForce, cache)

Notes

  • Ensure summaries and cache are populated to avoid unexpected behavior.
  • Proper handling of overwrite details is crucial for maintaining data integrity.