Class: Spree::Reimbursement

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/reimbursement.rb

Defined Under Namespace

Modules: ReimbursementTypeValidator Classes: Credit, IncompleteReimbursementError, ReimbursementTypeEngine

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.build_from_customer_return(customer_return) ⇒ Object



70
71
72
73
74
75
76
# File 'app/models/spree/reimbursement.rb', line 70

def build_from_customer_return(customer_return)
  order = customer_return.order
  order.reimbursements.build({
    customer_return: customer_return,
    return_items: customer_return.return_items.accepted.not_reimbursed
  })
end

Instance Method Details

#all_exchanges?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/spree/reimbursement.rb', line 133

def all_exchanges?
  return_items.all?(&:exchange_processed?)
end

#any_exchanges?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/spree/reimbursement.rb', line 129

def any_exchanges?
  return_items.any?(&:exchange_processed?)
end

#calculated_totalObject



83
84
85
86
87
# File 'app/models/spree/reimbursement.rb', line 83

def calculated_total
  # rounding down to handle edge cases for consecutive partial returns where rounding
  # might cause us to try to reimburse more than was originally billed
  return_items.to_a.sum(&:total).to_d.round(2, :down)
end

#display_totalObject



79
80
81
# File 'app/models/spree/reimbursement.rb', line 79

def display_total
  Spree::Money.new(total, { currency: order.currency })
end


89
90
91
92
93
# File 'app/models/spree/reimbursement.rb', line 89

def paid_amount
  reimbursement_models.sum do |model|
    model.total_amount_reimbursed_for(self)
  end
end

#perform!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/spree/reimbursement.rb', line 99

def perform!
  reimbursement_tax_calculator.call(self)
  reload
  update!(total: calculated_total)

  reimbursement_performer.perform(self)

  if unpaid_amount_within_tolerance?
    reimbursed!
    reimbursement_success_hooks.each { |h| h.call self }
    send_reimbursement_email
  else
    errored!
    reimbursement_failure_hooks.each { |h| h.call self }
    raise IncompleteReimbursementError, Spree.t("validation.unpaid_amount_not_zero", amount: unpaid_amount)
  end
end

#return_allvoid

This method returns an undefined value.

Accepts all return items, saves the reimbursement, and performs the reimbursement



141
142
143
144
145
# File 'app/models/spree/reimbursement.rb', line 141

def return_all
  return_items.each(&:accept!)
  save!
  perform!
end

#return_items_requiring_exchangeObject



125
126
127
# File 'app/models/spree/reimbursement.rb', line 125

def return_items_requiring_exchange
  return_items.select(&:exchange_required?)
end

#simulateObject



117
118
119
120
121
122
123
# File 'app/models/spree/reimbursement.rb', line 117

def simulate
  reimbursement_simulator_tax_calculator.call(self)
  reload
  update!(total: calculated_total)

  reimbursement_performer.simulate(self)
end

#unpaid_amountObject



95
96
97
# File 'app/models/spree/reimbursement.rb', line 95

def unpaid_amount
  total - paid_amount
end