Sha256: 539ed662bdef597100ad434069c45618aaf3fd452cfe98ed6628c0a05beea9b6

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

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: 1_000_000_000, greater_than: -1_000_000_000 }

  belongs_to :adjusted, polymorphic: true

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

  code :adjustment_factor, Unidom::Order::AdjustmentFactor

  ##
  # 对订单或者订单项 adjusted 进行调整。调整金额为 amount ,缺省为 0 。调整原因是 due_to ,缺省是 FRGT 。调整时间是 opened_at ,缺省为当前时间。如:
  # Unidom::Order::OrderAdjustment.adjust! order, amount: 7.90, due_to: 'LTAX'
  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 unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Order::OrderAdjustment'

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unidom-order-1.5.4 app/models/unidom/order/order_adjustment.rb
unidom-order-1.5.3 app/models/unidom/order/order_adjustment.rb
unidom-order-1.5.2 app/models/unidom/order/order_adjustment.rb
unidom-order-1.5.1 app/models/unidom/order/order_adjustment.rb