Class: Spree::Adjustment
- Extended by:
- DisplayMoney
- Defined in:
- app/models/spree/adjustment.rb
Overview
Adjustments represent a change to the item_total
of an Order. Each adjustment has an amount
that can be either positive or negative.
Adjustments can be “opened” or “closed”. Once an adjustment is closed, it will not be automatically updated.
Boolean attributes
-
eligible?
This boolean attributes stores whether this adjustment is currently eligible for its order. Only eligible adjustments count towards the order’s adjustment total. This allows an adjustment to be preserved if it becomes ineligible so it might be reinstated.
Class Method Summary collapse
-
.in_completed_orders(excluded_orders: []) ⇒ ActiveRecord::Relation
Returns Adjustments of completed Orders.
Instance Method Summary collapse
-
#calculate_eligibility ⇒ true, false
private
Calculates based on attached promotion (if this is a promotion adjustment) whether this promotion is still eligible.
-
#cancellation? ⇒ Boolean
True when this is a cancellation adjustment (Cancellation adjustments have a UnitCancel source).
- #currency ⇒ Object
- #finalize ⇒ Object
- #finalize! ⇒ Object
-
#promotion? ⇒ Boolean
True when this is a promotion adjustment (Promotion adjustments have a PromotionAction source).
- #recalculate ⇒ BigDecimal
-
#tax? ⇒ Boolean
True when this is a tax adjustment (Tax adjustments have a TaxRate source).
- #unfinalize ⇒ Object
- #unfinalize! ⇒ Object
- #update!(*args) ⇒ Object
Methods included from DisplayMoney
Methods inherited from Base
display_includes, page, preference, #preferences
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
Class Method Details
.in_completed_orders(excluded_orders: []) ⇒ ActiveRecord::Relation
Returns Adjustments of completed Orders.
63 64 65 66 67 68 |
# File 'app/models/spree/adjustment.rb', line 63 def self.in_completed_orders(excluded_orders: []) joins(:order). merge(Spree::Order.complete). where.not(spree_orders: { id: excluded_orders }). distinct end |
Instance Method Details
#calculate_eligibility ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Calculates based on attached promotion (if this is a promotion adjustment) whether this promotion is still eligible.
151 152 153 154 155 156 157 |
# File 'app/models/spree/adjustment.rb', line 151 def calculate_eligibility if !finalized? && source && promotion? source.promotion.eligible?(adjustable, promotion_code: promotion_code) else eligible? end end |
#cancellation? ⇒ Boolean
Returns true when this is a cancellation adjustment (Cancellation adjustments have a UnitCancel source).
101 102 103 |
# File 'app/models/spree/adjustment.rb', line 101 def cancellation? source_type == 'Spree::UnitCancel' end |
#currency ⇒ Object
86 87 88 |
# File 'app/models/spree/adjustment.rb', line 86 def currency adjustable ? adjustable.currency : Spree::Config[:currency] end |
#finalize ⇒ Object
78 79 80 |
# File 'app/models/spree/adjustment.rb', line 78 def finalize update(finalized: true) end |
#finalize! ⇒ Object
70 71 72 |
# File 'app/models/spree/adjustment.rb', line 70 def finalize! update!(finalized: true) end |
#promotion? ⇒ Boolean
Returns true when this is a promotion adjustment (Promotion adjustments have a PromotionAction source).
91 92 93 |
# File 'app/models/spree/adjustment.rb', line 91 def promotion? source_type == 'Spree::PromotionAction' end |
#recalculate ⇒ BigDecimal
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/spree/adjustment.rb', line 112 def recalculate if finalized? && !tax? return amount end # If the adjustment has no source, do not attempt to re-calculate the # amount. # Some scenarios where this happens: # - Adjustments that are manually created via the admin backend # - PromotionAction adjustments where the PromotionAction was deleted # after the order was completed. if source.present? self.amount = source.compute_amount(adjustable) if promotion? self.eligible = calculate_eligibility end # Persist only if changed # This is only not a save! to avoid the extra queries to load the order # (for validations) and to touch the adjustment. update_columns(eligible: eligible, amount: amount, updated_at: Time.current) if changed? end amount end |
#tax? ⇒ Boolean
Returns true when this is a tax adjustment (Tax adjustments have a TaxRate source).
96 97 98 |
# File 'app/models/spree/adjustment.rb', line 96 def tax? source_type == 'Spree::TaxRate' end |
#unfinalize ⇒ Object
82 83 84 |
# File 'app/models/spree/adjustment.rb', line 82 def unfinalize update(finalized: false) end |
#unfinalize! ⇒ Object
74 75 76 |
# File 'app/models/spree/adjustment.rb', line 74 def unfinalize! update!(finalized: false) end |
#update!(*args) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'app/models/spree/adjustment.rb', line 138 def update!(*args) if args.empty? Spree::Deprecation.warn "Calling adjustment.update! with no arguments to recalculate amounts and eligibility is deprecated, since it conflicts with AR::Base#update! Please use adjustment.recalculate instead" recalculate else super end end |