Skip to content

Instantly share code, notes, and snippets.

@workmad3
Forked from EminenceHC/controller.rb
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save workmad3/f33692386ec72425cdc0 to your computer and use it in GitHub Desktop.
Save workmad3/f33692386ec72425cdc0 to your computer and use it in GitHub Desktop.
<tr>
<td><%= hours[:paylocity_id] %></td>
<td>E</td>
<td>REG</td>
<td><%= hours[:regular_hours] %></td>
</tr>
<tr>
<td><%= hours[:paylocity_id] %></td>
<td>E</td>
<td>OT</td>
<td><%= hours[:overtime_hours] %></td>
</tr>
def payroll
pay_period
if params['pay_period'].present?
start_date = params['pay_period'].to_date
else
start_date = @pay_period
end
@reg = MultipleWeekTimeCardSummary.new(start_date, 2)
end
class MultipleWeekTimeCardSummary
def initialize(start_date, number_of_weeks)
@start_date = start_date.beginning_of_week
@week_summaries = number_of_weeks.times.map{|i| OneWeekTimeCardSummary.new(@start_date + i.weeks)}
end
def each(&blk)
enum = Enumerator.new do |y|
f = @week_summaries[0].each
f.zip(*@week_summaries[1..-1].map(&:each)).each do |summaries|
hours = {
user_id: nil,
paylocity_id: nil,
regular_hours: 0,
overtime_hours: 0,
holiday_hours: 0,
paid_time_off: 0
}
summaries.each do |s|
hours[:user_id] = s[:user_id]
hours[:paylocity_id] = s[:paylocity_id]
hours[:regular_hours] += s[:regular_hours]
hours[:overtime_hours] += s[:overtime_hours]
hours[:holiday_hours] += s[:holiday_hours]
hours[:paid_time_off] += s[:paid_time_off]
end
y << hours
end
end
if blk
enum.each(&blk)
else
enum
end
end
end
class OneWeekTimeCardSummary
def initialize(start_date)
@start_date = start_date.beginning_of_week
@end_date = @start_date.end_of_week
end
def results
q = DailyTimeCard.joins(:user)
q = q.where(users: {active: true, id: User.with_role(:employee)})
q = q.where(date: @start_date..@end_date)
q = q.group(:user_id).select("daily_time_cards.user_id, SUM(daily_time_cards.regular_hours) AS regular_hours, MIN(daily_time_cards.date) AS date")
q = q.group(:user_id).select("daily_time_cards.user_id, SUM(daily_time_cards.overtime_hours) AS overtime_hours, MIN(daily_time_cards.date) AS date")
q = q.group(:user_id).select("daily_time_cards.user_id, SUM(daily_time_cards.paid_time_off) AS paid_time_off, MIN(daily_time_cards.date) AS date")
q = q.group(:user_id).select("daily_time_cards.user_id, SUM(daily_time_cards.holiday_hours) AS holiday_hours, MIN(daily_time_cards.date) AS date")
q
end
def each(&blk)
enum = Enumerator.new do |y|
results.each do |r|
hours = {
user_id: r[:user_id],
paylocity_id: r.user.loginable.paylocity_id,
regular_hours: r[:regular_hours],
overtime_hours: r[:overtime_hours],
holiday_hours: r[:holiday_hours],
paid_time_off: r[:paid_time_off]
}
regular_hours = hours[:regular_hours] + hours[:overtime_hours]
if regular_hours > 40
hours[:regular_hours] = 40
hours[:overtime_hours] += regular_hours - 40
end
y << hours
end
end
if blk
enum.each(&blk)
else
enum
end
end
end
<table class="table table-bordered table-striped table-condensed" style="float:left;">
<% @reg.each do |hours| %>
<%= render "summary_line", hours: hours %>
<% end %>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment