Add method to return correct price for an operation #2741

This commit is contained in:
Martin Lensment 2015-07-02 13:06:58 +03:00
parent bdbf59e6d1
commit ec5a219456
2 changed files with 103 additions and 4 deletions

View file

@ -1,6 +1,8 @@
class Pricelist < ActiveRecord::Base
include Versions # version/pricelist_version.rb
scope :valid, -> { where("valid_from <= ? AND valid_to >= ? OR valid_to IS NULL", Time.zone.now, Time.zone.now) }
monetize :price_cents
validates :price_cents, :price_currency, :price,
@ -13,10 +15,18 @@ class Pricelist < ActiveRecord::Base
after_initialize :init_values
def init_values
return unless new_record?
self.valid_from = Time.zone.now.beginning_of_year
self.valid_from = Time.zone.now.beginning_of_year unless valid_from
end
def name
"#{operation_category} #{category}"
end
class << self
def price_for(category, operation, duration)
lists = valid.where(category: category, operation_category: operation, duration: duration)
return lists.first.price if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price
end
end
end