Class: Spree::Reimbursement

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/reimbursement.rb,
app/models/spree/reimbursement/credit.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

#admin_form_preference_names, #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



73
74
75
76
77
78
79
# File 'app/models/spree/reimbursement.rb', line 73

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)


146
147
148
# File 'app/models/spree/reimbursement.rb', line 146

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

#any_exchanges?Boolean

Returns:

  • (Boolean)


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

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

#calculated_totalObject



86
87
88
89
90
# File 'app/models/spree/reimbursement.rb', line 86

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



82
83
84
# File 'app/models/spree/reimbursement.rb', line 82

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


92
93
94
95
96
# File 'app/models/spree/reimbursement.rb', line 92

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

#perform!(created_by: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/spree/reimbursement.rb', line 102

def perform!(created_by: nil)
  unless created_by
    Spree::Deprecation.warn("Calling #perform on #{self} without created_by is deprecated")
  end
  reimbursement_tax_calculator.call(self)
  reload
  update!(total: calculated_total)

  reimbursement_performer.perform(self, created_by: created_by)

  if unpaid_amount_within_tolerance?
    reimbursed!
    Spree::Event.fire 'reimbursement_reimbursed', reimbursement: self
    reimbursement_success_hooks.each { |h| h.call self }
  else
    errored!
    Spree::Event.fire 'reimbursement_errored', reimbursement: self
    reimbursement_failure_hooks.each { |h| h.call self }
  end

  if errored?
    raise IncompleteReimbursementError, I18n.t("spree.validation.unpaid_amount_not_zero", amount: unpaid_amount)
  end
end

#return_all(created_by: nil) ⇒ void

This method returns an undefined value.

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

Parameters:

  • created_by (Spree.user_class) (defaults to: nil)

    the user that is performing this action



155
156
157
158
159
160
161
162
# File 'app/models/spree/reimbursement.rb', line 155

def return_all(created_by: nil)
  unless created_by
    Spree::Deprecation.warn("Calling #return_all on #{self} without created_by is deprecated")
  end
  return_items.each(&:accept!)
  save!
  perform!(created_by: created_by)
end

#return_items_requiring_exchangeObject



138
139
140
# File 'app/models/spree/reimbursement.rb', line 138

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

#simulate(created_by: nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'app/models/spree/reimbursement.rb', line 127

def simulate(created_by: nil)
  unless created_by
    Spree::Deprecation.warn("Calling #simulate on #{self} without created_by is deprecated")
  end
  reimbursement_simulator_tax_calculator.call(self)
  reload
  update!(total: calculated_total)

  reimbursement_performer.simulate(self, created_by: created_by)
end

#unpaid_amountObject



98
99
100
# File 'app/models/spree/reimbursement.rb', line 98

def unpaid_amount
  total - paid_amount
end