class Dovico::TimeEntryGenerator

Constants

DEFAULT_START_HOUR

Attributes

assignments[RW]
employee_id[RW]

Public Class Methods

new(assignments:, employee_id:) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 5
def initialize(assignments:, employee_id:)
  @assignments = assignments
  @employee_id = employee_id
end

Public Instance Methods

generate(start_date, end_date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 10
def generate(start_date, end_date)
  start_date.upto(end_date).flat_map do |day|
    build_day_time_entries(day)
  end
end

Private Instance Methods

build_day_time_entries(day) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 19
def build_day_time_entries(day)
  time_entries = []
  start_date = Time.parse(day.to_s).advance(hours: DEFAULT_START_HOUR)

  day_assignments = day_assignments(day)
  day_assignments.each do |assignment|
    time_entry = build_time_entry(assignment, start_date)
    time_entries << time_entry

    start_date = start_date.advance(hours: time_entry.total_hours.to_f)
  end
  time_entries
end
build_time_entry(assignment, start_date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 33
def build_time_entry(assignment, start_date)
  project_id  = assignment["project_id"]
  task_id     = assignment["task_id"]
  hours       = assignment["hours"]
  stop_date   = start_date.advance(hours: hours)
  start_time  = sprintf "%02d%02d", start_date.hour, start_date.min
  stop_time   = sprintf "%02d%02d", stop_date.hour, stop_date.min

  TimeEntry.new(
    employee_id: employee_id,
    project_id:  project_id,
    task_id:     task_id,
    date:        start_date.to_date.to_s,
    total_hours: hours,
    start_time:  start_time,
    stop_time:   stop_time,
  )
end
day_assignments(date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 52
def day_assignments(date)
  special_day(date) || special_week(date) || special_month(date) || assignments["default_day"]
end
special_day(date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 56
def special_day(date)
  assignments["special_days"] && assignments["special_days"][date.iso8601]
end
special_month(date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 64
def special_month(date)
  assignments["special_months"] && assignments["special_months"][date.strftime("%Y-%m")]
end
special_week(date) click to toggle source
# File lib/dovico/model/time_entry_generator.rb, line 60
def special_week(date)
  assignments["special_weeks"] && assignments["special_weeks"][date.strftime("%Y-%V")]
end