Class: Spree::ItemAdjustments

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/item_adjustments.rb

Overview

Manage (recalculate) item (LineItem or Shipment) adjustments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ ItemAdjustments

Returns a new instance of ItemAdjustments.



8
9
10
# File 'app/models/spree/item_adjustments.rb', line 8

def initialize(item)
  @item = item
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



4
5
6
# File 'app/models/spree/item_adjustments.rb', line 4

def item
  @item
end

Instance Method Details

#best_promotion_adjustmentObject



61
62
63
# File 'app/models/spree/item_adjustments.rb', line 61

def best_promotion_adjustment
  @best_promotion_adjustment ||= adjustments.promotion.eligible.reorder("amount ASC, created_at DESC").first
end

#choose_best_promotion_adjustmentObject

Picks one (and only one) promotion to be eligible for this order This promotion provides the most discount, and if two promotions have the same amount, then it will pick the latest one.



54
55
56
57
58
59
# File 'app/models/spree/item_adjustments.rb', line 54

def choose_best_promotion_adjustment
  if best_promotion_adjustment
    other_promotions = self.adjustments.promotion.where("id NOT IN (?)", best_promotion_adjustment.id)
    other_promotions.update_all(:eligible => false)
  end
end

#updateObject



12
13
14
15
# File 'app/models/spree/item_adjustments.rb', line 12

def update
  update_adjustments if item.persisted?
  item
end

#update_adjustmentsObject

TODO this should be probably the place to calculate proper item taxes values after promotions are applied



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/item_adjustments.rb', line 19

def update_adjustments
  # Promotion adjustments must be applied first, then tax adjustments.
  # This fits the criteria for VAT tax as outlined here:
  # http://www.hmrc.gov.uk/vat/managing/charging/discounts-etc.htm#1
  #
  # It also fits the criteria for sales tax as outlined here:
  # http://www.boe.ca.gov/formspubs/pub113/
  # 
  # Tax adjustments come in not one but *two* exciting flavours:
  # Included & additional

  # Included tax adjustments are those which are included in the price.
  # These ones should not effect the eventual total price.
  #
  # Additional tax adjustments are the opposite; effecting the final total. 
  promotion_total = adjustments.promotion.reload.map(&:update!).compact.sum
  unless promotion_total == 0
    choose_best_promotion_adjustment
  end
  promo_total = best_promotion_adjustment.try(:amount).to_f
  included_tax_total = adjustments.tax.included.reload.map(&:update!).compact.sum
  additional_tax_total = adjustments.tax.additional.reload.map(&:update!).compact.sum

  item.update_columns(
    :promo_total => promo_total,
    :included_tax_total => included_tax_total,
    :additional_tax_total => additional_tax_total,
    :adjustment_total => promo_total + additional_tax_total,
    :updated_at => Time.now,
  )
end