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



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

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)


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

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

#any_exchanges?Boolean

Returns:

  • (Boolean)


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

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

#calculated_totalObject



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

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



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

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


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

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

#perform!(created_by: nil) ⇒ Object



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

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!
    reimbursement_success_hooks.each { |h| h.call self }
    send_reimbursement_email
  else
    errored!
    reimbursement_failure_hooks.each { |h| h.call self }
    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)

    the user that is performing this action



150
151
152
153
154
155
156
157
# File 'app/models/spree/reimbursement.rb', line 150

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



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

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

#simulate(created_by: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/models/spree/reimbursement.rb', line 122

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



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

def unpaid_amount
  total - paid_amount
end