Sha256: 052b80675f8ef26a22d0840e70de06a7c44e8b1867f5b645ba200707d57854e2

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module Dovico
  class TimeEntryGenerator
    DEFAULT_START_HOUR = 9

    def initialize(assignments:, employee_id:)
      @assignments = assignments
      @employee_id = employee_id
    end

    def generate(start_date, end_date)
      start_date.upto(end_date).flat_map do |day|
        build_day_time_entries(day)
      end
    end

    private
    attr_accessor :assignments, :employee_id

    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

    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

    def day_assignments(date)
      special_day(date) || special_week(date) || special_month(date) || assignments["default_day"]
    end

    def special_day(date)
      assignments["special_days"] && assignments["special_days"][date.iso8601]
    end

    def special_week(date)
      assignments["special_weeks"] && assignments["special_weeks"][date.strftime("%Y-%V")]
    end

    def special_month(date)
      assignments["special_months"] && assignments["special_months"][date.strftime("%Y-%m")]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dovico-1.4.0 lib/dovico/model/time_entry_generator.rb
dovico-1.3.0 lib/dovico/model/time_entry_generator.rb