From 41e98328c8b7214ac916b84daf00045940bd7194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 11 Jan 2017 17:22:15 +0100 Subject: [PATCH] Charge SCVMM's vm only until it is retired. --- app/models/chargeback/consumption.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/chargeback/consumption.rb b/app/models/chargeback/consumption.rb index 9ea8bc205f2..36bf4880298 100644 --- a/app/models/chargeback/consumption.rb +++ b/app/models/chargeback/consumption.rb @@ -8,10 +8,12 @@ def consumed_hours_in_interval # Why we need this? # 1) We cannot charge for hours until the resources existed (vm provisioned in the middle of month) # 2) We cannot charge for future hours (i.e. weekly report on Monday, should charge just monday) + # 3) We cannot charge for hours after the resource has been retired. @consumed_hours_in_interval ||= begin consuption_start = [@start_time, resource.try(:created_on)].compact.max - consumption_end = [Time.current, @end_time].min - (consumption_end - consuption_start).round / 1.hour + consumption_end = [Time.current, @end_time, resource.try(:retires_on)].compact.min + consumed = (consumption_end - consuption_start).round / 1.hour + consumed > 0 ? consumed : 0 end end