Class: Spree::Reimbursement

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

Class Method Details

.build_from_customer_return(customer_return) ⇒ Object



65
66
67
68
69
70
71
# File 'core/app/models/spree/reimbursement.rb', line 65

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)


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

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

#any_exchanges?Boolean

Returns:

  • (Boolean)


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

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

#calculated_totalObject



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

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



74
75
76
# File 'core/app/models/spree/reimbursement.rb', line 74

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


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

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

#perform!(created_by: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'core/app/models/spree/reimbursement.rb', line 94

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)

    the user that is performing this action



147
148
149
150
151
152
153
154
# File 'core/app/models/spree/reimbursement.rb', line 147

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



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

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

#simulate(created_by: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'core/app/models/spree/reimbursement.rb', line 119

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



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

def unpaid_amount
  total - paid_amount
end