class Vatcalc::Bill::Base

Attributes

vat_percentages[R]

Public Class Methods

for() click to toggle source
# File lib/vatcalc/bill.rb, line 163
def self.for
  BaseElement
end
new(*args) click to toggle source
Calls superclass method Vatcalc::Bill::GNVCollection::new
# File lib/vatcalc/bill.rb, line 158
def initialize(*args)
  super 
  @vat_percentages = Set.new 
end

Public Instance Methods

human_rates() click to toggle source

Output of rates in form of key is VAT Percentage and Value is the rate in decimal form {“19%”=>“69.81%”, “7%”=>“21.74%”, “0%”=>“8.45%”}

# File lib/vatcalc/bill.rb, line 206
def human_rates
  #example ((1.19 - 1.00)*100).round(2) => 19.0
  rates.inject({}){|h,(pr,v)| h[pr.to_s] = Util.human_percentage_value(v,4); h}
end
insert(gnv,quantity) click to toggle source
Calls superclass method Vatcalc::Bill::GNVCollection#insert
# File lib/vatcalc/bill.rb, line 167
def insert(gnv,quantity)
  super
  @rates = nil
  @vat_percentages << gnv.vat_p
  self
end
rates() click to toggle source

Output of rates in form of key is VAT Percentage and Value is the rate “{1.0=>0.0092, 1.19=>0.8804, 1.07=>0.1104}”

# File lib/vatcalc/bill.rb, line 177
def rates
  @rates ||= rates!
end
rates!() click to toggle source
# File lib/vatcalc/bill.rb, line 181
def rates!
  @rates = Hash.new(0.00)
  if net.to_f != 0 
    left_over = 1.00
    grouped_amounts = @collection.inject(money_hash){ |h,(gnv,q)| h[gnv.vat_p] += gnv.net * q; h}.sort

    grouped_amounts.each_with_index do |(vp,amount),i|
      if i == (grouped_amounts.length - 1)
        #last element
        @rates[vp] = left_over.round(4)
      else
        @rates[vp] = (amount / net).round(4)
        left_over -= @rates[vp]
      end
    end
  else
    max_p = @vat_percentages.max
    @rates[max_p] = 1.00 if max_p
  end
  @rates = @rates.sort.reverse.to_h #sorted by vat percentage
end