Class: Unidom::Price::Price

Inherits:
ApplicationRecord show all
Includes:
ProgneTapera::EnumCode, Common::Concerns::ModelExtension
Defined in:
app/models/unidom/price/price.rb

Class Method Summary collapse

Class Method Details

.price!(priced, amount: 0, pricer: nil, calculation_code: 'AMNT', pricing_code: 'BASE', charging_code: 'ONCE', opened_at: Time.now) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/unidom/price/price.rb', line 29

def self.price!(priced, amount: 0, pricer: nil, calculation_code: 'AMNT', pricing_code: 'BASE', charging_code: 'ONCE', opened_at: Time.now)

  assert_present! :priced,           priced
  assert_present! :amount,           amount
  assert_present! :calculation_code, calculation_code
  assert_present! :pricing_code,     pricing_code
  assert_present! :charging_code,    charging_code
  assert_present! :opened_at,        opened_at

  price = priced_is(priced).calculation_coded_as(calculation_code).pricing_coded_as(pricing_code).charging_coded_as(charging_code).valid_at.alive.first
  if price.present?
    price.amount = amount
    if pricer.present?
      price.pricer = pricer
    else
      price.pricer_id   = Unidom::Common::NULL_UUID
      price.pricer_type = ''
    end
  else
    attributes = { priced: priced, amount: amount, calculation_code: calculation_code, pricing_code: pricing_code, charging_code: charging_code, opened_at: opened_at }
    if pricer.present?
      attributes[:pricer] = pricer
    else
      attributes[:pricer_id]   = Unidom::Common::NULL_UUID
      attributes[:pricer_type] = ''
    end
    create! attributes
  end
end