class SunatInvoice::Item

Attributes

description[RW]
price[RW]
price_code[RW]
price_included_tax[RW]
quantity[RW]
unit_code[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/sunat_invoice/item.rb, line 10
def initialize(*args)
  # * quantity - quantity of item
  # * description - name or description of product or service
  # * price - unit price without taxes
  # * price_code - type unit price (Catalogs::CATALOG_16)
  # * unit_code - unit of measure
  #   UN/ECE rec 20- Unit Of Measure
  #   http://www.unece.org/fileadmin/DAM/cefact/recommendations/rec20/rec20_rev3_Annex2e.pdf
  # * taxes - An array of SunatInvoice::Tax
  # * price_included_tax - price with taxes
  super(*args)
  @taxes ||= []
end

Public Instance Methods

bi_value() click to toggle source
# File lib/sunat_invoice/item.rb, line 49
def bi_value
  # bi of sale = price without taxes * quantity
  set_price
  (@price.to_f * @quantity.to_f).round(2)
end
line_tag_name() click to toggle source
# File lib/sunat_invoice/item.rb, line 24
def line_tag_name
  'InvoiceLine'
end
quantity_tag_name() click to toggle source
# File lib/sunat_invoice/item.rb, line 28
def quantity_tag_name
  'InvoicedQuantity'
end
sale_price() click to toggle source
# File lib/sunat_invoice/item.rb, line 65
def sale_price
  # unit price with tax
  @price_included_tax || (@price.to_f + sum_taxes).round(2)
end
sale_taxes() click to toggle source
# File lib/sunat_invoice/item.rb, line 55
def sale_taxes
  # generate and object with taxes sum by type
  sums = {}
  taxes.each do |tax|
    sums[tax.tax_type] ||= 0
    sums[tax.tax_type] = (tax.amount.to_f * quantity.to_f).round(2)
  end
  sums
end
set_price() click to toggle source
# File lib/sunat_invoice/item.rb, line 43
def set_price
  return unless @price_included_tax

  @price ||= (@price_included_tax - sum_taxes).round(2)
end
xml(xml, index, currency) click to toggle source
# File lib/sunat_invoice/item.rb, line 32
def xml(xml, index, currency)
  xml['cac'].send(line_tag_name) do
    build_basic_line_xml(xml, index)
    amount_xml(xml['cbc'], 'LineExtensionAmount', bi_value, currency)
    build_pricing_reference(xml, currency)
    build_taxes_xml(xml, currency)
    build_item(xml)
    build_price(xml, currency)
  end
end

Private Instance Methods

build_basic_line_xml(xml, index) click to toggle source
# File lib/sunat_invoice/item.rb, line 76
def build_basic_line_xml(xml, index)
  xml['cbc'].ID(index + 1)
  xml['cbc'].send(quantity_tag_name, quantity, unitCode: unit_code)
end
build_item(xml) click to toggle source
# File lib/sunat_invoice/item.rb, line 81
def build_item(xml)
  xml['cac'].Item do
    xml['cbc'].Description description
  end
end
build_price(xml, currency) click to toggle source
# File lib/sunat_invoice/item.rb, line 96
def build_price(xml, currency)
  xml['cac'].Price do
    amount_xml(xml['cbc'], 'PriceAmount', price, currency)
  end
end
build_pricing_reference(xml, currency) click to toggle source
# File lib/sunat_invoice/item.rb, line 87
def build_pricing_reference(xml, currency)
  xml['cac'].PricingReference do
    xml['cac'].AlternativeConditionPrice do
      amount_xml(xml['cbc'], 'PriceAmount', sale_price, currency)
      xml['cbc'].PriceTypeCode price_code
    end
  end
end
sum_taxes() click to toggle source
# File lib/sunat_invoice/item.rb, line 72
def sum_taxes
  taxes.map(&:amount).sum
end