Class: Spree::Reimbursement

Inherits:
Object
  • Object
show all
Includes:
NumberIdentifier, Emails, Webhooks::HasWebhooks
Defined in:
app/models/spree/reimbursement.rb,
app/models/spree/reimbursement/emails.rb

Defined Under Namespace

Modules: Emails, ReimbursementTypeValidator Classes: Credit, IncompleteReimbursementError, ReimbursementTypeEngine

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Emails

#send_reimbursement_email

Class Method Details

.build_from_customer_return(customer_return) ⇒ Object



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

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



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

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



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

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


99
100
101
102
103
# File 'app/models/spree/reimbursement.rb', line 99

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

#perform!(performer = nil) ⇒ Object



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

def perform!(performer = nil)
  reimbursement_tax_calculator.call(self)
  reload
  update!(total: calculated_total, performed_by: performer)

  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



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

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

#simulateObject



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

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

  reimbursement_performer.simulate(self)
end

#unpaid_amountObject



105
106
107
# File 'app/models/spree/reimbursement.rb', line 105

def unpaid_amount
  total - paid_amount
end