Sha256: 0d0900d58d7d7f7ffee05dc81a083902266679079fcde8214f5e6b73d7be2015

Contents?: true

Size: 1.96 KB

Versions: 32

Compression:

Stored size: 1.96 KB

Contents

module Spree
  class CustomerReturn < Spree::Base
    include Spree::Core::NumberGenerator.new(prefix: 'CR', length: 9)
    belongs_to :stock_location

    has_many :reimbursements, inverse_of: :customer_return
    has_many :return_authorizations, through: :return_items
    has_many :return_items, inverse_of: :customer_return

    after_create :process_return!

    validates :return_items, :stock_location, presence: true
    validate :must_have_return_authorization, on: :create
    validate :return_items_belong_to_same_order

    accepts_nested_attributes_for :return_items

    extend DisplayMoney
    money_methods pre_tax_total: { currency: Spree::Config[:currency] }

    self.whitelisted_ransackable_attributes = ['number']

    delegate :id, to: :order, prefix: true, allow_nil: true

    def completely_decided?
      !return_items.undecided.exists?
    end

    def fully_reimbursed?
      completely_decided? && return_items.accepted.includes(:reimbursement).all? { |return_item| return_item.reimbursement.try(:reimbursed?) }
    end

    # Temporarily tie a customer_return to one order
    def order
      return nil if return_items.blank?
      return_items.first.inventory_unit.order
    end


    def pre_tax_total
      return_items.sum(:pre_tax_amount)
    end

    private

    def inventory_units
      return_items.flat_map(&:inventory_unit)
    end

    def must_have_return_authorization
      if item = return_items.find { |ri| ri.return_authorization.blank? }
        errors.add(:base, Spree.t(:missing_return_authorization, item_name: item.inventory_unit.variant.name))
      end
    end

    def process_return!
      return_items.each(&:receive!)
      order.return! if order.all_inventory_units_returned?
    end

    def return_items_belong_to_same_order
      if return_items.any? { |return_item| return_item.inventory_unit.order_id != order_id }
        errors.add(:base, Spree.t(:return_items_cannot_be_associated_with_multiple_orders))
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
spree_core-3.2.9 app/models/spree/customer_return.rb
spree_core-3.1.14 app/models/spree/customer_return.rb
spree_core-3.2.8 app/models/spree/customer_return.rb
spree_core-3.1.13 app/models/spree/customer_return.rb
spree_core-3.2.7 app/models/spree/customer_return.rb
spree_core-3.1.12 app/models/spree/customer_return.rb
spree_core-3.2.6 app/models/spree/customer_return.rb
spree_core-3.1.11 app/models/spree/customer_return.rb
spree_core-3.1.10 app/models/spree/customer_return.rb
spree_core-3.2.5 app/models/spree/customer_return.rb
spree_core-3.1.9 app/models/spree/customer_return.rb
spree_core-3.2.4 app/models/spree/customer_return.rb
spree_core-3.1.8 app/models/spree/customer_return.rb
spree_core-3.1.7 app/models/spree/customer_return.rb
spree_core-3.2.3 app/models/spree/customer_return.rb
spree_core-3.2.2 app/models/spree/customer_return.rb
spree_core-3.1.6 app/models/spree/customer_return.rb
spree_core-3.1.5 app/models/spree/customer_return.rb
spree_core-3.2.1 app/models/spree/customer_return.rb
spree_core-3.2.0 app/models/spree/customer_return.rb