Skip to content

Commit

Permalink
adds checkbox to reverse the vat on the admin product page
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Rüger committed Aug 22, 2011
1 parent 8f599b7 commit ce0430f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
27 changes: 27 additions & 0 deletions app/models/admin/products_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Admin::ProductsHelper.module_eval do
# returns the price of the product to show for display purposes
def product_price(product_or_variant, options={})
options.assert_valid_keys(:format_as_currency, :show_vat_text)
options.reverse_merge! :format_as_currency => true, :show_vat_text => Spree::Config[:show_price_inc_vat]

amount = product_or_variant.price
amount += Calculator::Vat.calculate_tax_on(product_or_variant) if Spree::Config[:show_price_inc_vat]
options.delete(:format_as_currency) ? format_price(amount, options) : amount
end
end

Admin::ProductsController.class_eval do
before_filter :vat_fix , :only => :update

def vat_fix
return unless params[:price_includes_vat]
taxid = params[:product][:tax_category_id]
rate = TaxRate.find( taxid ) if taxid
rate = Calculator::Vat.default_rates.first unless rate
rate = TaxRate.first unless rate
price = params[:product][:price].to_f
price = price / (1 + rate.amount)
puts "Adjusted price by #{rate.amount} to #{price} (was #{params[:product][:price]})"
params[:product][:price] = price
end
end
12 changes: 0 additions & 12 deletions app/models/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,3 @@ def create_tax_charge!
end
end
end

Admin::ProductsHelper.module_eval do
# returns the price of the product to show for display purposes
def product_price(product_or_variant, options={})
options.assert_valid_keys(:format_as_currency, :show_vat_text)
options.reverse_merge! :format_as_currency => true, :show_vat_text => Spree::Config[:show_price_inc_vat]

amount = product_or_variant.price
amount += Calculator::Vat.calculate_tax_on(product_or_variant) if Spree::Config[:show_price_inc_vat]
options.delete(:format_as_currency) ? format_price(amount, options) : amount
end
end
4 changes: 3 additions & 1 deletion lib/spree_vat_fix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def self.activate
end
class Hooks < Spree::ThemeSupport::HookListener
insert_before :admin_product_form_right do
"Price incl vat <%=product_price(@product)%>"
"<%=product_price(@product)%> Reverse <%= t('vat')%>
<input type='checkbox' name='price_includes_vat' value='true' checked='true'/> "
end
end

0 comments on commit ce0430f

Please sign in to comment.