Class: Spree::Variant::VatPriceGenerator

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

Overview

This class generates gross prices for all countries that have VAT configured. The prices will include their respective VAT rates. It will also generate an export (net) price for any country that doesn't have VAT.

Examples:

The admin is configured to show German gross prices
(Spree::Config.admin_vat_country_iso == "DE")

There is VATs configured for Germany (19%) and Finland (24%).
The VAT price generator is run on a variant with a base (German) price of 10.00.
The Price Generator will generate:
  - A price for Finland (country_id == "FI"): 10.42
  - A price for any other country (country_id == nil): 8.40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant) ⇒ VatPriceGenerator

Returns a new instance of VatPriceGenerator



19
20
21
# File 'app/models/spree/variant/vat_price_generator.rb', line 19

def initialize(variant)
  @variant = variant
end

Instance Attribute Details

#variantObject (readonly)

Returns the value of attribute variant



17
18
19
# File 'app/models/spree/variant/vat_price_generator.rb', line 17

def variant
  @variant
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree/variant/vat_price_generator.rb', line 23

def run
  # Early return if there is no VAT rates in the current store.
  return if !variant.tax_category || variant_vat_rates.empty?

  country_isos_requiring_price.each do |country_iso|
    # Don't re-create the default price
    next if variant.default_price && variant.default_price.country_iso == country_iso

    foreign_price = find_or_initialize_price_by(country_iso, variant.default_price.currency)

    foreign_price.amount = variant.default_price.net_amount * (1 + vat_for_country_iso(country_iso))
  end
end