<commit_before>require 'delegate'

class OrderUpdater < SimpleDelegator
  # TODO: This logic adapted from Spree 2.4, remove when we get there
  # Handles state updating in a much more logical way than < 2.4
  # Specifically, doesn't depend on payments.last to determine payment state
  # Also swapped: == 0 for .zero?, .size == 0 for empty? and .size > 0 for !empty?
  # See:
  # https://github.com/spree/spree/commit/38b8456183d11fc1e00e395e7c9154c76ef65b85
  # https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
  def update_payment_state
    last_state = order.payment_state
    if payments.present? && payments.valid.empty?
      order.payment_state = 'failed'
    elsif order.state == 'canceled' && order.payment_total.zero?
      order.payment_state = 'void'
    else
      # This part added so that we don't need to override order.outstanding_balance
      balance = order.outstanding_balance
      balance = -1 * order.payment_total if canceled_and_paid_for?
      order.payment_state = 'balance_due' if balance > 0
      order.payment_state = 'credit_owed' if balance < 0
      order.payment_state = 'paid' if balance.zero?

      # Original logic
      # order.payment_state = 'balance_due' if order.outstanding_balance > 0
      # order.payment_state = 'credit_owed' if order.outstanding_balance < 0
      # order.payment_state = 'paid' if !order.outstanding_balance?
    end
    order.state_changed('payment') if last_state != order.payment_state
    order.payment_state
  end

  def before_save_hook
    shipping_address_from_distributor
  end

  private

  # Taken from order.outstanding_balance in Spree 2.4
  # See: https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
  def canceled_and_paid_for?
    order.canceled? && order.payments.present? && !order.payments.completed.empty?
  end

  def shipping_address_from_distributor
    shipments.each do |shipment|
      shipping_method = shipment.shipping_method
      next if shipping_method.require_ship_address

      order.ship_address = order.distributor.address
    end
  end
end<commit_msg>Add doc to method<commit_after>require 'delegate'

class OrderUpdater < SimpleDelegator
  # TODO: This logic adapted from Spree 2.4, remove when we get there
  # Handles state updating in a much more logical way than < 2.4
  # Specifically, doesn't depend on payments.last to determine payment state
  # Also swapped: == 0 for .zero?, .size == 0 for empty? and .size > 0 for !empty?
  # See:
  # https://github.com/spree/spree/commit/38b8456183d11fc1e00e395e7c9154c76ef65b85
  # https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
  def update_payment_state
    last_state = order.payment_state
    if payments.present? && payments.valid.empty?
      order.payment_state = 'failed'
    elsif order.state == 'canceled' && order.payment_total.zero?
      order.payment_state = 'void'
    else
      # This part added so that we don't need to override order.outstanding_balance
      balance = order.outstanding_balance
      balance = -1 * order.payment_total if canceled_and_paid_for?
      order.payment_state = 'balance_due' if balance > 0
      order.payment_state = 'credit_owed' if balance < 0
      order.payment_state = 'paid' if balance.zero?

      # Original logic
      # order.payment_state = 'balance_due' if order.outstanding_balance > 0
      # order.payment_state = 'credit_owed' if order.outstanding_balance < 0
      # order.payment_state = 'paid' if !order.outstanding_balance?
    end
    order.state_changed('payment') if last_state != order.payment_state
    order.payment_state
  end

  def before_save_hook
    shipping_address_from_distributor
  end

  private

  # Taken from order.outstanding_balance in Spree 2.4
  # See: https://github.com/spree/spree/commit/7b264acff7824f5b3dc6651c106631d8f30b147a
  def canceled_and_paid_for?
    order.canceled? && order.payments.present? && !order.payments.completed.empty?
  end

  # Sets the distributor's address as shipping address of the order for those
  # shipments using a shipping method that doesn't require address, such us
  # a pickup.
  def shipping_address_from_distributor
    shipments.each do |shipment|
      shipping_method = shipment.shipping_method
      next if shipping_method.require_ship_address

      order.ship_address = order.distributor.address
    end
  end
end
{'fraction_non_alphanumeric': 0.08113636363636363, 'fraction_numerical': 0.042727272727272725, 'mean_word_length': 3.7373519913885898, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 6, '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': '5187283', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1397, 'n_words': 345}