Skip to main content

Rule Computation Documentation

This code provides functions to apply rules to attendance summaries. It processes rules and applies them to determine break times, overtime, and other attendance details.

RulesApplied struct

type RulesApplied struct {
Detail *entity.RuleDetailTable
Used bool
IndexStr string // if there is order (like for breaks) string
OrderNum int // if there is order (like for breaks) int
UsedFor string // var that's impacted? like presence deduction
}
  • Detail *entity.RuleDetailTable: The rule detail.
  • Used bool: Indicates if the rule has been used.
  • IndexStr string: The index string for ordered rules (e.g., breaks).
  • OrderNum int: The order number for ordered rules (e.g., breaks).
  • UsedFor string: The variable impacted by the rule (e.g., presence deduction).

RulesHitStruct type

type RulesHitStruct map[string]RulesApplied

A map of rule IDs to RulesApplied structs.

Functions

runBreakRuleAction

func runBreakRuleAction(detail *entity.AttendanceSummariesDetailTable, ruleDetail *entity.RuleDetailTable) bool

Applies a break rule to an attendance summary detail.

  • detail *entity.AttendanceSummariesDetailTable: The attendance summary detail.
  • ruleDetail *entity.RuleDetailTable: The rule detail.

Returns true if the rule was applied, otherwise false.

runMinMaxDeduction

func runMinMaxDeduction(value, min, max, deduction float64) float64

Applies min, max, and deduction values to a given value.

  • value float64: The value to adjust.
  • min float64: The minimum value.
  • max float64: The maximum value.
  • deduction float64: The deduction value.

Returns the adjusted value.

calculateEarlyLateDetail

func calculateEarlyLateDetail(detail *entity.AttendanceSummariesDetailTable)

Calculates early or late check-in and check-out times for an attendance summary detail.

  • detail *entity.AttendanceSummariesDetailTable: The attendance summary detail.

calculateDurationDetail

func calculateDurationDetail(detail *entity.AttendanceSummariesDetailTable)

Calculates the duration for an attendance summary detail.

  • detail *entity.AttendanceSummariesDetailTable: The attendance summary detail.

FindUnusedRuleByEvent

func (r RulesHitStruct) FindUnusedRuleByEvent(eventStr string) []string

Finds unused rules by event.

  • eventStr string: The event string.

Returns a slice of rule IDs.

FindUnusedRuleByCase

func (r RulesHitStruct) FindUnusedRuleByCase(caseStr string) []string

Finds unused rules by case.

  • caseStr string: The case string.

Returns a slice of rule IDs.

FindUnusedRuleByEventAndCase

func (r RulesHitStruct) FindUnusedRuleByEventAndCase(eventStr, caseStr string) []string

Finds unused rules by event and case.

  • eventStr string: The event string.
  • caseStr string: The case string.

Returns a slice of rule IDs.

FindUnusedRuleByAction

func (r RulesHitStruct) FindUnusedRuleByAction(actionStr string) []string

Finds unused rules by action.

  • actionStr string: The action string.

Returns a slice of rule IDs.

FindRuleByAction

func (r RulesHitStruct) FindRuleByAction(actionStr string) []string

Finds rules by action.

  • actionStr string: The action string.

Returns a slice of rule IDs.

Example Usage

package main

import (
"fio-backend/entity"
"fio-backend/utils/compute"
"fmt"
)

func main() {
// Create a sample attendance summary detail
detail := &entity.AttendanceSummariesDetailTable{
CiActual: "2024-05-01T08:00:00Z",
CoActual: "2024-05-01T17:00:00Z",
ShiftStart: "2024-05-01T08:00:00Z",
ShiftEnd: "2024-05-01T17:00:00Z",
}

// Create a sample rule detail
ruleDetail := &entity.RuleDetailTable{
Action: entity.ACTION_SET_BREAK,
Case: entity.CASE_NO_SCAN,
UseValue: true,
Value: 30,
}

// Apply the break rule
ruleApplied := compute.RunBreakRuleAction(detail, ruleDetail)
fmt.Printf("Rule Applied: %v\n", ruleApplied)
fmt.Printf("Updated Detail: %+v\n", detail)

// Calculate early/late details
compute.CalculateEarlyLateDetail(detail)
fmt.Printf("Updated Detail with Early/Late: %+v\n", detail)

// Calculate duration details
compute.CalculateDurationDetail(detail)
fmt.Printf("Updated Detail with Duration: %+v\n", detail)

// Create a sample RulesHitStruct
rulesHit := compute.RulesHitStruct{
"rule_1": {
Detail: ruleDetail,
Used: false,
IndexStr: "1",
OrderNum: 1,
UsedFor: "break",
},
}

// Find unused rules by event
unusedRulesByEvent := rulesHit.FindUnusedRuleByEvent(string(entity.EVENT_BREAK))
fmt.Printf("Unused Rules by Event: %+v\n", unusedRulesByEvent)

// Find unused rules by case
unusedRulesByCase := rulesHit.FindUnusedRuleByCase(string(entity.CASE_NO_SCAN))
fmt.Printf("Unused Rules by Case: %+v\n", unusedRulesByCase)

// Find unused rules by event and case
unusedRulesByEventAndCase := rulesHit.FindUnusedRuleByEventAndCase(string(entity.EVENT_BREAK), string(entity.CASE_NO_SCAN))
fmt.Printf("Unused Rules by Event and Case: %+v\n", unusedRulesByEventAndCase)

// Find unused rules by action
unusedRulesByAction := rulesHit.FindUnusedRuleByAction(string(entity.ACTION_SET_BREAK))
fmt.Printf("Unused Rules by Action: %+v\n", unusedRulesByAction)

// Find rules by action
rulesByAction := rulesHit.FindRuleByAction(string(entity.ACTION_SET_BREAK))
fmt.Printf("Rules by Action: %+v\n", rulesByAction)
}

Improvements and Considerations

  • Error Handling: Improve error handling by returning errors instead of just logging them.
  • Code Clarity and Comments: Improve code comments to explain complex logic more clearly.
  • Unit Tests: Add unit tests to ensure the functions work correctly with various inputs and edge cases.
  • Performance Optimization: Optimize the functions for performance, especially when processing large numbers of rules and attendance details.
  • Modularization: Break down the functions into smaller, more modular functions to improve readability and maintainability.

This comprehensive documentation provides a detailed understanding of the rule computation functions and their functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for applying rules to attendance summaries.