Sha256: 2b640c78528e565fb39a0447d8b64a81d6449f9a08553665d32786db1a6fd3d3

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

##
# Order Adjustment 是订单调整项。

class Unidom::Order::OrderAdjustment < Unidom::Order::ApplicationRecord

  self.table_name = 'unidom_order_adjustments'

  include Unidom::Common::Concerns::ModelExtension
  include ProgneTapera::EnumCode

  validates :amount, presence: true, numericality: { less_than: 1000000000, greater_than: -1000000000 }

  belongs_to :adjusted, polymorphic: true

  scope :adjusted_is, ->(adjusted) { where adjusted: adjusted }

  code :adjustment_factor, Unidom::Order::AdjustmentFactor

  def self.adjust!(adjusted, amount: 0, due_to: 'FRGT', opened_at: Time.now)
    query      = adjusted_is(adjusted).adjustment_factor_coded_as(due_to).valid_at(now: opened_at).alive
    adjustment = query.first
    if adjustment.present?
      if 0==amount
        adjustment.soft_destroy
      else
        adjustment.amount = amount
        adjustment.save!
      end
    else
      adjustment = query.create! amount: amount, opened_at: opened_at
    end
    adjustment
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unidom-order-1.4.1 app/models/unidom/order/order_adjustment.rb
unidom-order-1.4 app/models/unidom/order/order_adjustment.rb