Skip to content

Instantly share code, notes, and snippets.

@czj
Created March 25, 2014 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czj/9766239 to your computer and use it in GitHub Desktop.
Save czj/9766239 to your computer and use it in GitHub Desktop.
module LineWithTaxes
extend ActiveSupport::Concern
included do
validates_length_of :label, within: 2..254
validates_length_of :comment, within: 0..254, allow_blank: true
validates_numericality_of :amount_with_taxes, :amount_without_taxes
validates_numericality_of :quantity, minimum: 1, only_integer: true
validates_numericality_of :ecotax, minimum: 0
normalize_attributes :label, with: :squish
normalize_attributes :comment
end
# Amount of taxes for each unit of this line (VAT amount = TVA in France)
# We don't store percentage to avoid rounding errors
def taxes
amount_with_taxes - amount_without_taxes
end
# Total amount of VAT for this line
def total_taxes
taxes * quantity
end
# French "TTC" = "toutes taxes comprises" amount for this line
def total_with_taxes
amount_with_taxes * quantity
end
# French "HT" = "hors taxes" amount for this line
def total_without_taxes
amount_without_taxes * quantity
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment