mirror of
https://github.com/internetee/registry.git
synced 2025-07-02 17:23:34 +02:00
Add method to return correct price for an operation #2741
This commit is contained in:
parent
bdbf59e6d1
commit
ec5a219456
2 changed files with 103 additions and 4 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue