Send monthly reports

This commit is contained in:
Aart van Halteren
2023-02-21 22:43:22 +01:00
parent ccb8589a4b
commit 534e8dc294
4 changed files with 182 additions and 12 deletions

View File

@@ -67,6 +67,16 @@ class Reading < ActiveRecord::Base
def day(date)
Reading.where("created_at > :begin AND created_at < :end", { :begin => date.to_date.beginning_of_day, :end => date.to_date.end_of_day})
end
def in_month(date)
hour_start = date.beginning_of_month.beginning_of_day
hour_end = date.end_of_month.end_of_day
# can't get cost for the future
hour_end = Time.now if hour_end > Time.now
Reading.where("created_at > :begin AND created_at < :end", { :begin => hour_start, :end => hour_end})
end
def max_charge_kwh=(kwh)
@@max_charge_kwh = kwh
@@ -78,12 +88,19 @@ class Reading < ActiveRecord::Base
# 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
# calculate net_consumption (= consumption-production)
def net_consumed_high_and_low_for_diff(d)
net_consumed_high = (d[:total_kwh_consumed_high].nil? || d[:total_kwh_produced_high].nil?) ? nil : d[:total_kwh_consumed_high]-d[:total_kwh_produced_high]
net_consumed_low = (d[:total_kwh_consumed_low].nil? || d[:total_kwh_produced_low].nil?) ? nil : d[:total_kwh_consumed_low]-d[:total_kwh_produced_low]
return net_consumed_high, net_consumed_low
end
def diff_on(date)
readings_on = day(date)
@@ -106,5 +123,22 @@ 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_in_month(date)
hour_start = date.beginning_of_month.beginning_of_day
hour_end = date.end_of_month.end_of_day
# can't get cost for the future
hour_end = Time.now if hour_end > Time.now
readings_in_month = Reading.where("created_at > :begin AND created_at < :end", { :begin => hour_start, :end => hour_end})
last = readings_in_month.last
if last
last.diff(readings_in_month.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