class InvoiceProduct

Attributes

cost_invoice[R]
cost_offer[R]
hash[R]
name[R]
price[R]
returned[R]
tax[R]
tax_invoice[R]
tax_offer[R]
tax_value[R]
total_invoice[R]
unit[R]
valid[R]

Public Class Methods

new(hash, settings) click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 317
def initialize(hash, settings)
  @hash      = hash
  @name      = hash[:name]
  @price     = hash[:price]
  @unit      = hash[:unit]
  @amount    = hash[:amount]
  @settings  = settings
  if hash[:tax]
    @tax_value = hash[:tax]
  else
    @tax_value = @settings[:defaults][:tax]
  end

  fail "TAX MUST NOT BE > 1" if @tax_value > 1

  @valid = true
  calculate() unless hash.nil?
end

Public Instance Methods

amount(choice) click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 373
def amount choice
  return @sold   if choice == :invoice
  return @amount if choice == :offer
  return -1
end
calculate() click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 344
def calculate()
  return false if @hash.nil?
  @valid    = false unless @hash[:sold].nil? or @hash[:returned].nil?
  @valid    = false unless @hash[:amount] and @hash[:price]
  @sold     = @hash[:sold]
  @price    = @hash[:price].to_euro
  @amount   = @hash[:amount]
  @returned = @hash[:returned]

  if @sold
    @returned = @amount - @sold
  elsif @returned
    @sold = @amount - @returned
  else
    @sold = @amount
    @returned = 0
  end

  @hash[:cost_offer]   = @cost_offer   = (@price * @amount).to_euro
  @hash[:cost_invoice] = @cost_invoice = (@price * @sold).to_euro

  @hash[:tax_offer]    = @tax_offer    = (@cost_offer   * @tax_value)
  @hash[:tax_invoice]  = @tax_invoice  = (@cost_invoice * @tax_value)

  @hash[:total_offer]    = @total_offer    = (@cost_offer   + @tax_offer)
  @hash[:total_invoice]  = @total_invoice  = (@cost_invoice + @tax_invoice)
  self.freeze
end
cost(choice) click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 379
def cost choice
  return @cost_invoice if choice == :invoice
  return @cost_offer   if choice == :offer
  return -1.to_euro
end
to_csv(*args) click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 336
def to_csv *args
  [@name, @price, @amount, @sold, @tax_value].to_csv(*args)
end
to_s() click to toggle source
# File lib/ascii_invoicer/InvoiceProject.rb, line 340
def to_s
  "#{@amount}/#{@sold} #{@name}, #{@price} cost (#{@cost_offer}|#{@cost_invoice}) total(#{@total_offer}|#{@total_invoice} #{@tax_value}) "
end