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, page, preference, #preferences

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.build_from_customer_return(customer_return) ⇒ Object



65
66
67
68
69
70
71
# File '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)


148
149
150
# File 'app/models/spree/reimbursement.rb', line 148

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

#any_exchanges?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/spree/reimbursement.rb', line 144

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

#calculated_totalObject



78
79
80
81
82
# File '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 'app/models/spree/reimbursement.rb', line 74

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


84
85
86
87
88
# File '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
118
119
120
121
122
123
124
125
126
127
# File '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
    if reimbursement_success_hooks.any?
      Spree::Deprecation.warn \
        "reimbursement_success_hooks are deprecated. Please remove them " \
        "and subscribe to `reimbursement_reimbursed` event instead", caller(1)
    end
    reimbursement_success_hooks.each { |hook| hook.call self }
  else
    errored!
    Spree::Event.fire 'reimbursement_errored', reimbursement: self
    if reimbursement_failure_hooks.any?
      Spree::Deprecation.warn \
        "reimbursement_failure_hooks are deprecated. Please remove them " \
        "and subscribe to `reimbursement_errored` event instead", caller(1)
    end
    reimbursement_failure_hooks.each { |hook| hook.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



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

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



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

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

#simulate(created_by: nil) ⇒ Object



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

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

#store_credit_categorySpree::StoreCreditCategory

The returned category is used as the category for Spree::Reimbursement::Credit.default_creditable_class.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/spree/reimbursement.rb', line 170

def store_credit_category
  if Spree::Config.use_legacy_store_credit_reimbursement_category_name
    Spree::Deprecation.warn("Using the legacy reimbursement_category_name is deprecated. "\
      "Set Spree::Config.use_legacy_store_credit_reimbursement_category_name to false to use "\
      "the new version instead.", caller)

    name = Spree::StoreCreditCategory.reimbursement_category_name
    return Spree::StoreCreditCategory.find_by(name: name) || Spree::StoreCreditCategory.first
  end

  Spree::StoreCreditCategory.find_by(name: Spree::StoreCreditCategory::REIMBURSEMENT)
end

#unpaid_amountObject



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

def unpaid_amount
  total - paid_amount
end