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

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.build_from_customer_return(customer_return) ⇒ Object


57
58
59
60
61
62
63
# File 'app/models/spree/reimbursement.rb', line 57

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)

122
123
124
# File 'app/models/spree/reimbursement.rb', line 122

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

#any_exchanges?Boolean

Returns:

  • (Boolean)

118
119
120
# File 'app/models/spree/reimbursement.rb', line 118

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

#calculated_totalObject


70
71
72
73
74
# File 'app/models/spree/reimbursement.rb', line 70

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


66
67
68
# File 'app/models/spree/reimbursement.rb', line 66

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

76
77
78
79
80
# File 'app/models/spree/reimbursement.rb', line 76

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

#perform!(created_by:) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/spree/reimbursement.rb', line 86

def perform!(created_by:)
  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
  else
    errored!
    Spree::Event.fire 'reimbursement_errored', reimbursement: self
  end

  if errored?
    raise IncompleteReimbursementError, I18n.t("spree.validation.unpaid_amount_not_zero", amount: unpaid_amount)
  end
end

#return_all(created_by:) ⇒ 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


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

def return_all(created_by:)
  return_items.each(&:accept!)
  save!
  perform!(created_by: created_by)
end

#return_items_requiring_exchangeObject


114
115
116
# File 'app/models/spree/reimbursement.rb', line 114

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

#simulate(created_by:) ⇒ Object


106
107
108
109
110
111
112
# File 'app/models/spree/reimbursement.rb', line 106

def simulate(created_by:)
  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.


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

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

#unpaid_amountObject


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

def unpaid_amount
  total - paid_amount
end