Sha256: c314a11f7f8424adb2cf20c83d22aa9e81270e934f2d7943b88f1e31f0af53e0

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

class Taxedo::Receipt
  attr_accessor :currency
  attr_accessor :equation_type

  def initialize(region_id, amount)
    @region_id = region_id
    @amount = amount
    @taxes = []

    load_translations
  end

  def add_tax(id, rate)
    tax = Taxedo::Tax.new(id)
    tax.name = tax_name(id)
    tax.rate = rate
    tax.source_amount = get_source_amount

    @taxes << tax
  end

  def get_source_amount
    if @taxes.empty? or self.equation_type == 'separated'
      @amount
    else
      @taxes.last.subtotal
    end
  end

  def language
    defined?(I18n) ? I18n.locale.to_s : 'fr'
  end

  def subtotal
    @amount
  end

  def t(path)
    text = @translations

    path.split('.').each do |p|
      raise "TAXEDO: No #{language} translation for #{path}." if not text
      text = text[p]
    end

    return text.to_s
  end

  def taxes
    @taxes
  end

  def total
    @total = subtotal
    @taxes.each { |t| @total += t.amount }

    return @total
  end

  def to_hash
    Taxedo::Builder::Hash.new(self).generate
  end

  def to_html(options={})
    options = { columns: 0, custom_content: '', template: :table }.merge(options)

    return Taxedo::Builder::Html.new(self).generate(options[:template], options)
  end

  def to_json
    Taxedo::Builder::Json.new(self).generate
  end

  def to_text
    Taxedo::Builder::Text.new(self).generate
  end

  private

  def load_translations
    @translations = YAML.load_file(Taxedo.base_languages_path + "#{language}.yml")['taxedo'][language]
  end

  def tax_name(tax_id)
    t("regions.#{@region_id}.taxes.#{tax_id}.short")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
taxedo-0.0.6 lib/taxedo/receipt.rb
taxedo-0.0.5 lib/taxedo/receipt.rb