Moved task summary calculation to the Customer model.
This commit is contained in:
parent
c7cc4312b3
commit
614e73bad1
38
stoptime.rb
38
stoptime.rb
|
@ -20,6 +20,7 @@ Camping.goes :StopTime
|
||||||
|
|
||||||
unless defined? BASE_DIR
|
unless defined? BASE_DIR
|
||||||
BASE_DIR = Pathname.new(__FILE__).dirname.expand_path + "public"
|
BASE_DIR = Pathname.new(__FILE__).dirname.expand_path + "public"
|
||||||
|
|
||||||
# Set the default date(/time) format.
|
# Set the default date(/time) format.
|
||||||
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
|
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
|
||||||
:default => "%Y-%m-%d %H:%M",
|
:default => "%Y-%m-%d %H:%M",
|
||||||
|
@ -28,9 +29,11 @@ unless defined? BASE_DIR
|
||||||
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
|
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
|
||||||
:default => "%Y-%m-%d",
|
:default => "%Y-%m-%d",
|
||||||
:month_and_year => "%B %Y")
|
:month_and_year => "%B %Y")
|
||||||
|
|
||||||
|
# FIXME: this should be configurable.
|
||||||
|
HourlyRate = 20.00
|
||||||
end
|
end
|
||||||
|
|
||||||
HourlyRate = 20.00
|
|
||||||
|
|
||||||
module StopTime
|
module StopTime
|
||||||
|
|
||||||
|
@ -45,6 +48,25 @@ module StopTime::Models
|
||||||
class Customer < Base
|
class Customer < Base
|
||||||
has_many :tasks
|
has_many :tasks
|
||||||
has_many :time_entries, :through => :tasks
|
has_many :time_entries, :through => :tasks
|
||||||
|
|
||||||
|
def task_summary(month)
|
||||||
|
# FIXME: ensure that month is a DateTime/Time object.
|
||||||
|
time_entries = self.time_entries.all(:conditions =>
|
||||||
|
["start > ? AND end < ?", month, month.at_end_of_month])
|
||||||
|
|
||||||
|
tasks = time_entries.inject({}) do |tasks, entry|
|
||||||
|
time = (entry.end - entry.start)/1.hour
|
||||||
|
if tasks.has_key? entry.task
|
||||||
|
tasks[entry.task][0] += time
|
||||||
|
tasks[entry.task][2] += time * HourlyRate
|
||||||
|
else
|
||||||
|
tasks[entry.task] = [time, HourlyRate, time * HourlyRate]
|
||||||
|
end
|
||||||
|
tasks
|
||||||
|
end
|
||||||
|
|
||||||
|
return tasks
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Task < Base
|
class Task < Base
|
||||||
|
@ -54,6 +76,7 @@ module StopTime::Models
|
||||||
|
|
||||||
class TimeEntry < Base
|
class TimeEntry < Base
|
||||||
belongs_to :task
|
belongs_to :task
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class StopTimeTables < V 1.0
|
class StopTimeTables < V 1.0
|
||||||
|
@ -175,18 +198,7 @@ module StopTime::Controllers
|
||||||
@number = number[5..-1].to_i
|
@number = number[5..-1].to_i
|
||||||
|
|
||||||
@customer = Customer.find(customer_id)
|
@customer = Customer.find(customer_id)
|
||||||
time_entries = @customer.time_entries.all(:conditions =>
|
@tasks = @customer.task_summary(@month)
|
||||||
["start > ? AND end < ?", @month, @month.at_end_of_month])
|
|
||||||
@tasks = time_entries.inject({}) do |tasks, entry|
|
|
||||||
time = (entry.end - entry.start)/1.hour
|
|
||||||
if tasks.has_key? entry.task
|
|
||||||
tasks[entry.task][0] += time
|
|
||||||
tasks[entry.task][2] += time * HourlyRate
|
|
||||||
else
|
|
||||||
tasks[entry.task] = [time, HourlyRate, time * HourlyRate]
|
|
||||||
end
|
|
||||||
tasks
|
|
||||||
end
|
|
||||||
|
|
||||||
render :invoice
|
render :invoice
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue