Class: Charge

Inherits:
Adjustment show all
Defined in:
app/models/charge.rb

Instance Method Summary collapse

Methods inherited from Adjustment

#amount, #update_amount

Instance Method Details

#calculate_adjustmentObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/charge.rb', line 4

def calculate_adjustment
  if adjustment_source
    case secondary_type
    when "TaxCharge"
      calculate_tax_charge
    when "ShippingCharge"
      calculate_shipping_charge
    else
      super
    end
  end
end

#calculate_shipping_chargeObject

Calculates shipping cost using calculators from shipping_rates and shipping_method shipping_method calculator is used when there’s no corresponding shipping_rate calculator

shipping costs are calculated for each shipping_category - so if order have items from 3 shipping categories, shipping cost will triple. You can alter this behaviour by overwriting this method in your site extension



32
33
34
35
# File 'app/models/charge.rb', line 32

def calculate_shipping_charge
  return unless shipping_method = adjustment_source.shipping_method
  shipping_method.calculate_cost(adjustment_source)
end

#calculate_tax_chargeObject



17
18
19
20
21
22
23
24
# File 'app/models/charge.rb', line 17

def calculate_tax_charge
  return Calculator::Vat.calculate_tax(order) if order.shipment.address.blank? and Spree::Config[:show_price_inc_vat]
  return unless order.shipment.address
  zones = Zone.match(order.shipment.address)
  tax_rates = zones.map{|zone| zone.tax_rates}.flatten.uniq
  calculated_taxes = tax_rates.map{|tax_rate| tax_rate.calculate_tax(order)}
  return(calculated_taxes.sum)
end