Skip to content

Instantly share code, notes, and snippets.

@felipecabargas
Created June 9, 2014 06:20
Show Gist options
  • Save felipecabargas/d544a10624f720bfce2b to your computer and use it in GitHub Desktop.
Save felipecabargas/d544a10624f720bfce2b to your computer and use it in GitHub Desktop.
LastConsumption script
@last_consumption = ConsumptionSummary.last
@bo_size = BranchOffice.sum(:size)
if @last_consumption.nil? #If nil we have to calculate all
@report = ElectricityConsumption.select("month, year, SUM(net_total) money, SUM(energy) energy, SUM(net_total) money_m2 , SUM(energy) energy_m2").group("month, year").order("year, month asc")
@report.each do |report|
cs = ConsumptionSummary.new
cs.attributes = {
year: report.attributes["year"],
month: report.attributes["month"],
money: report.attributes["money"],
energy: report.attributes["energy"],
money_m2: report.attributes["money_m2"].to_f / @bo_size.to_f,
energy_m2: report.attributes["energy_m2"].to_f / @bo_size.to_f
}
cs.save!
end
else
registry = @last_consumption
if registry.month == 12
registry.month = 1
registry.year += 1
else
registry.month += 1
end
@report = ElectricityConsumption.select("month, year, SUM(net_total) money, SUM(energy) energy, SUM(net_total) money_m2 , SUM(energy) energy_m2").where("month = ? AND year = ?",@last_consumption.month,@last_consumption.year).group("month,year").first
if [email protected]?
cs = ConsumptionSummary.new
cs.attributes = {
year: @report.attributes["year"],
month: @report.attributes["month"],
money: @report.attributes["money"],
energy: @report.attributes["energy"],
money_m2: @report.attributes["money_m2"].to_f / @bo_size.to_f,
energy_m2: @report.attributes["energy_m2"].to_f / @bo_size.to_f
}
cs.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment