Skip to content

Instantly share code, notes, and snippets.

@julesjans
Created August 17, 2017 08:05
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 julesjans/8a30e9cfde37226ffbdddcbde33a61f7 to your computer and use it in GitHub Desktop.
Save julesjans/8a30e9cfde37226ffbdddcbde33a61f7 to your computer and use it in GitHub Desktop.
Calculating VAT
module VatCalc
def VatCalc.vat_rate
@vat_rate ||= BigDecimal.new('20')
end
def VatCalc.vat_rate=(value)
@vat_rate = BigDecimal.new(value.to_s)
end
def VatCalc.vat_divisor
BigDecimal.new((VatCalc.vat_rate / BigDecimal('100')) + BigDecimal('1'))
end
# http://www.hmrc.gov.uk/vat/forms-rates/rates/calculating.htm
# Do we round up or down?
# http://customs.hmrc.gov.uk/channelsPortalWebApp/channelsPortalWebApp.portal?_nfpb=true&_pageLabel=pageVAT_ShowContent&id=HMCE_CL_001596&propertyType=document#P1493_117367
def incl_vat_figures
figures = {}
gross = BigDecimal.new(self.to_s).round(2)
net = (gross / VatCalc.vat_divisor).round(2)
figures[:gross] = gross
figures[:net] = net
figures[:vat] = gross - net
figures[:rate] = VatCalc.vat_rate
figures
end
def plus_vat_figures
figures = {}
net = BigDecimal.new(self.to_s).round(2)
vat = ((net * VatCalc.vat_divisor) - net).round(2)
figures[:net] = net
figures[:vat] = vat
figures[:gross] = (net + vat).round(2)
figures[:rate] = VatCalc.vat_rate
figures
end
end
require 'bigdecimal'
include VatCalc
300.23.plus_vat_figures.each {|x,y| puts "#{x}: #{y.to_f}"}
300.23.incl_vat_figures.each {|x,y| puts "#{x}: #{y.to_f}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment