app/models/unidom/price/price.rb in unidom-price-2.0 vs app/models/unidom/price/price.rb in unidom-price-2.0.1

- old
+ new

@@ -1,56 +1,62 @@ # Price 是价格。 # pricing: 基价(Base)、折扣成分、额外收费成分、厂商建议价。 # charging: 一次性收费、重复性收费、使用率收费。 -class Unidom::Price::Price < Unidom::Price::ApplicationRecord +module Unidom + module Price - self.table_name = 'unidom_prices' + class Price < ApplicationRecord - include Unidom::Common::Concerns::ModelExtension - include ProgneTapera::EnumCode + self.table_name = 'unidom_prices' - validates :amount, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1_000_000_000 } + include Unidom::Common::Concerns::ModelExtension + include ProgneTapera::EnumCode - belongs_to :priced, polymorphic: true - belongs_to :pricer, polymorphic: true + validates :amount, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1_000_000_000 } - scope :priced_by, ->(pricer) { where pricer: pricer } - scope :priced_is, ->(priced) { where priced: priced } + belongs_to :priced, polymorphic: true + belongs_to :pricer, polymorphic: true - code :calculation, Unidom::Price::Calculation - code :charging, Unidom::Price::Charging - code :pricing, Unidom::Price::Pricing + scope :priced_by, ->(pricer) { where pricer: pricer } + scope :priced_is, ->(priced) { where priced: priced } - ## - # 将物品 priced 定价为 amount , 定价者是 pricer。 - def self.price!(priced, amount: 0, pricer: nil, calculation_code: 'AMNT', pricing_code: 'BASE', charging_code: 'ONCE', opened_at: Time.now) + code :calculation, Unidom::Price::Calculation + code :charging, Unidom::Price::Charging + code :pricing, Unidom::Price::Pricing - 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 + ## + # 将物品 priced 定价为 amount , 定价者是 pricer。 + def self.price!(priced, amount: 0, pricer: nil, calculation_code: 'AMNT', pricing_code: 'BASE', charging_code: 'ONCE', opened_at: Time.now) - 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 = '' + 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 - 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 -end unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Price::Price' + end unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Price::Price' + + end +end