Refactor to prepare for getting monthly overview

This commit is contained in:
Aart van Halteren
2023-02-20 18:13:50 +01:00
parent e74f488668
commit ccb8589a4b
3 changed files with 69 additions and 14 deletions

View File

@@ -76,6 +76,14 @@ class Reading < ActiveRecord::Base
@@max_charge_kwh
end
# do not make distinction between high and low consumption/production
def consumed_and_produced_for_diff(d)
usage_kwh = (d[:total_kwh_consumed_high].nil? || d[:total_kwh_consumed_low].nil?) ? nil : d[:total_kwh_consumed_high] + d[:total_kwh_consumed_low]
return_kwh = (d[:total_kwh_produced_high].nil? || d[:total_kwh_produced_low].nil?) ? nil : d[:total_kwh_produced_high] + d[:total_kwh_produced_low]
return usage_kwh, return_kwh
end
def diff_on(date)
readings_on = day(date)
@@ -87,5 +95,16 @@ class Reading < ActiveRecord::Base
{ :total_kwh_consumed_high => 0, :total_kwh_consumed_low => 0, :total_kwh_produced_high => 0, :total_kwh_produced_low => 0, :total_m3_gas_consumed => 0 }
end
end
def diff_between(from_date, to_date)
readings_on_days = days(from_date, to_date)
first = readings_on_days.first
last = readings_on_days.last
if last
last.diff(first)
else
{ :total_kwh_consumed_high => 0, :total_kwh_consumed_low => 0, :total_kwh_produced_high => 0, :total_kwh_produced_low => 0, :total_m3_gas_consumed => 0 }
end
end
end
end