<commit_before>module Spree
  class ReturnItem < ActiveRecord::Base

    INTERMEDIATE_RECEPTION_STATUSES = %i(given_to_customer lost_in_transit shipped_wrong_item short_shipped in_transit)
    COMPLETED_RECEPTION_STATUSES = INTERMEDIATE_RECEPTION_STATUSES + [:received, :unexchanged]

    # @!scope class
    # @!attribute return_eligibility_validator
    # Configurable validator for determining whether given return item is
    # eligible for return.
    # @return [Class]
    class_attribute :return_eligibility_validator
    self.return_eligibility_validator = ReturnItem::EligibilityValidator::Default

    # @!scope class
    # @!attribute exchange_variant_engine
    # Configurable engine for determining which variants can be exchanged for a
    # given variant.
    # @return [Class]
    class_attribute :exchange_variant_engine
    self.exchange_variant_engine = ReturnItem::ExchangeVariantEligibility::SameProduct

    # @!scope class
    # @!attribute refund_amount_calculator
    # Configurable calculator for determining the amount ro refund when
    # refunding.
    # @return [Class]
    class_attribute :refund_amount_calculator
    self.refund_amount_calculator = Calculator::Returns::DefaultRefundAmount

    belongs_to :return_authorization, inverse_of: :return_items
    belongs_to :inventory_unit, inverse_of: :return_items
    belongs_to :exchange_variant, class_name: 'Spree::Variant'
    belongs_to :exchange_inventory_unit, class_name: 'Spree::InventoryUnit', inverse_of: :original_return_item
    belongs_to :customer_return, inverse_of: :return_items
    belongs_to :reimbursement, inverse_of: :return_items
    belongs_to :preferred_reimbursement_type, class_name: 'Spree::ReimbursementType'
    belongs_to :override_reimbursement_type, class_name: 'Spree::ReimbursementType'

    validate :eligible_exchange_variant
    validate :belongs_to_same_customer_order
    validate :validate_acceptance_status_for_reimbursement
    validates :inventory_unit, presence: true
    validate :validate_no_other_completed_return_items

    after_create :cancel_others, unless: :cancelled?

    scope :awaiting_return, -> { where(reception_status: 'awaiting') }
    scope :expecting_return, -> { where.not(reception_status: COMPLETED_RECEPTION_STATUSES) }
    scope :not_cancelled, -> { where.not(reception_status: 'cancelled') }
    scope :valid, -> { where.not(reception_status: %w(cancelled expired))}
    scope :not_expired, -> { where.not(reception_status: 'expired')}
    scope :received, -> { where(reception_status: 'received') }
    INTERMEDIATE_RECEPTION_STATUSES.each do |reception_status|
      scope reception_status, -> { where(reception_status: reception_status) }
    end
    scope :pending, -> { where(acceptance_status: 'pending') }
    scope :accepted, -> { where(acceptance_status: 'accepted') }
    scope :rejected, -> { where(acceptance_status: 'rejected') }
    scope :manual_intervention_required, -> { where(acceptance_status: 'manual_intervention_required') }
    scope :undecided, -> { where(acceptance_status: %w(pending manual_intervention_required)) }
    scope :decided, -> { where.not(acceptance_status: %w(pending manual_intervention_required)) }
    scope :reimbursed, -> { where.not(reimbursement_id: nil) }
    scope :not_reimbursed, -> { where(reimbursement_id: nil) }
    scope :exchange_requested, -> { where.not(exchange_variant: nil) }
    scope :exchange_processed, -> { where.not(exchange_inventory_unit: nil) }
    scope :exchange_required, -> { exchange_requested.where(exchange_inventory_unit: nil) }<commit_msg>Customer can return an item that was previously unexchanged<commit_after>module Spree
  class ReturnItem < ActiveRecord::Base

    INTERMEDIATE_RECEPTION_STATUSES = %i(given_to_customer lost_in_transit shipped_wrong_item short_shipped in_transit)
    COMPLETED_RECEPTION_STATUSES = INTERMEDIATE_RECEPTION_STATUSES + [:received, :unexchanged]

    # @!scope class
    # @!attribute return_eligibility_validator
    # Configurable validator for determining whether given return item is
    # eligible for return.
    # @return [Class]
    class_attribute :return_eligibility_validator
    self.return_eligibility_validator = ReturnItem::EligibilityValidator::Default

    # @!scope class
    # @!attribute exchange_variant_engine
    # Configurable engine for determining which variants can be exchanged for a
    # given variant.
    # @return [Class]
    class_attribute :exchange_variant_engine
    self.exchange_variant_engine = ReturnItem::ExchangeVariantEligibility::SameProduct

    # @!scope class
    # @!attribute refund_amount_calculator
    # Configurable calculator for determining the amount ro refund when
    # refunding.
    # @return [Class]
    class_attribute :refund_amount_calculator
    self.refund_amount_calculator = Calculator::Returns::DefaultRefundAmount

    belongs_to :return_authorization, inverse_of: :return_items
    belongs_to :inventory_unit, inverse_of: :return_items
    belongs_to :exchange_variant, class_name: 'Spree::Variant'
    belongs_to :exchange_inventory_unit, class_name: 'Spree::InventoryUnit', inverse_of: :original_return_item
    belongs_to :customer_return, inverse_of: :return_items
    belongs_to :reimbursement, inverse_of: :return_items
    belongs_to :preferred_reimbursement_type, class_name: 'Spree::ReimbursementType'
    belongs_to :override_reimbursement_type, class_name: 'Spree::ReimbursementType'

    validate :eligible_exchange_variant
    validate :belongs_to_same_customer_order
    validate :validate_acceptance_status_for_reimbursement
    validates :inventory_unit, presence: true
    validate :validate_no_other_completed_return_items

    after_create :cancel_others, unless: :cancelled?

    scope :awaiting_return, -> { where(reception_status: 'awaiting') }
    scope :expecting_return, -> { where.not(reception_status: COMPLETED_RECEPTION_STATUSES) }
    scope :not_cancelled, -> { where.not(reception_status: 'cancelled') }
    scope :valid, -> { where.not(reception_status: %w(cancelled expired unexchanged))}
    scope :not_expired, -> { where.not(reception_status: 'expired')}
    scope :received, -> { where(reception_status: 'received') }
    INTERMEDIATE_RECEPTION_STATUSES.each do |reception_status|
      scope reception_status, -> { where(reception_status: reception_status) }
    end
    scope :pending, -> { where(acceptance_status: 'pending') }
    scope :accepted, -> { where(acceptance_status: 'accepted') }
    scope :rejected, -> { where(acceptance_status: 'rejected') }
    scope :manual_intervention_required, -> { where(acceptance_status: 'manual_intervention_required') }
    scope :undecided, -> { where(acceptance_status: %w(pending manual_intervention_required)) }
    scope :decided, -> { where.not(acceptance_status: %w(pending manual_intervention_required)) }
    scope :reimbursed, -> { where.not(reimbursement_id: nil) }
    scope :not_reimbursed, -> { where(reimbursement_id: nil) }
    scope :exchange_requested, -> { where.not(exchange_variant: nil) }
    scope :exchange_processed, -> { where.not(exchange_inventory_unit: nil) }
    scope :exchange_required, -> { exchange_requested.where(exchange_inventory_unit: nil) }
{'fraction_non_alphanumeric': 0.09272626318711827, 'fraction_numerical': 0.0, 'mean_word_length': 5.421568627450981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2347459', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2187, 'n_words': 427}