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

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Preferences::Preferable

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



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

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

#calculated_totalObject



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

def calculated_total
  # rounding every return item individually to handle edge cases for consecutive partial
  # returns where rounding might cause us to try to reimburse more than was originally billed
  return_items.map { |ri| ri.total.to_d.round(2) }.sum
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!Object



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

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_items_requiring_exchangeObject



127
128
129
# File 'app/models/spree/reimbursement.rb', line 127

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

#simulateObject



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

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

  reimbursement_performer.simulate(self)
end

#unpaid_amountObject



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

def unpaid_amount
  total - paid_amount
end