Git Product home page Git Product logo

vat_helpers's Introduction

Description

Use this plugin to validate VAT numbers for european countries as well as to get the tax rate depending on the country of the buyer.

Basic Usage

To validate a model field as a valid intracom VAT number :

validates :vat_number, :vat => true

It may be optional like this :

validates :vat_number, :vat => true, :allow_blank => true

If you want the VAT number’s country to match another kind of country field, use the ‘country_method’ option :

validates :vat_number, :vat => { :country_method => :my_method }

where it is supposed that your model has a method named ‘my_method’ which returns the country ISO code you want to match. The available country codes available for Europa are: DE, AT, BE, BG, CY, DK, ES, EE, FI, FR, EL, HU, IE, IT, LV, LT, LU, MT, NL, PL, PT, GB, RO, SK, SI, SE and CZ.

Example :

class Invoice < ActiveRecord::Base
  validates :vat_number, :vat => { :country_method => :country_code }

  # Logic to return the country code
  def country_code
    case country.downcase
      when 'belgium' then 'BE'
      when 'france'  then 'FR'
      when 'sweden'  then 'SE'
      # ...
      else nil
    end
  end
end

Validating the Number at VIES

It’s also possible to have the VAT number validated against Europa’s VIES webservice. Simply use the :vies option like this :

validates :vat_number, :vat => { :vies => true }

Keep in mind that the webservice might be offline at some time for some countries. Check the VIES FAQ for more information about that.

Getting the tax rate

First set the country of the seller in your config/application.rb file

config.vta_applied_for = 'FR' # or 'BE', etc....

Note: by default, the value of the vta_applied_for option is set to ‘FR’

Let’s review the different cases for a seller in France:

  1. the seller is outside Europe

VatHelpers::Rate.get('US') # returns 0.0
  1. the buyer is in France

VatHelpers::Rate.get('FR') # returns 19.6
  1. the buyer is in Europe without a vat number

VatHelpers::Rate.get('BE') # returns 19.6
  1. the buyer is in Europe with a vat number

VatHelpers::Rate.get('BE', { :vat_number => 'a_valid_vat_number' }) # returns 0.0
  1. the buyer is in Martinique, Guadeloupe or la Réunion

VatHelpers::Rate.get('MQ') # returns 8.5
  1. the buyer is in French Guyana

VatHelpers::Rate.get('GF') # returns 0.0

Installation

In your project’s Gemfile :

gem ‘vat_helpers’, :git => ‘git://github.com/did/vat_helpers.git’

Tests

If you want to run the specs :

rake spec

Credits

This plugin in released under MIT license by Aurélien Malisart (see MIT-LICENSE file).

Enhanced by Didier Lafforgue ([email protected])

© aurelien.malisart.be

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.