Class: Vatcalc::Bill::Base

Inherits:
GNVCollection show all
Defined in:
lib/vatcalc/bill.rb

Instance Attribute Summary collapse

Attributes inherited from GNVCollection

#collection, #currency

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GNVCollection

#+, #<<, #each, #each_gnv, #vat_splitted

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



158
159
160
161
# File 'lib/vatcalc/bill.rb', line 158

def initialize(*args)
  super 
  @vat_percentages = Set.new 
end

Instance Attribute Details

#vat_percentagesObject (readonly)

Returns the value of attribute vat_percentages.



156
157
158
# File 'lib/vatcalc/bill.rb', line 156

def vat_percentages
  @vat_percentages
end

Class Method Details

.forObject



163
164
165
# File 'lib/vatcalc/bill.rb', line 163

def self.for
  BaseElement
end

Instance Method Details

#human_ratesObject

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



206
207
208
209
# 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) ⇒ Object



167
168
169
170
171
172
# File 'lib/vatcalc/bill.rb', line 167

def insert(gnv,quantity)
  super
  @rates = nil
  @vat_percentages << gnv.vat_p
  self
end

#ratesObject

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



177
178
179
# File 'lib/vatcalc/bill.rb', line 177

def rates
  @rates ||= rates!
end

#rates!Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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