Class: SunatInvoice::Item

Inherits:
Line show all
Defined in:
lib/sunat_invoice/item.rb

Direct Known Subclasses

CreditNoteLine, DebitNoteLine

Constant Summary

Constants included from Utils

Utils::COMMON_NAMESPACES, Utils::CREDIT_NOTE_NAMESPACES, Utils::DAILY_SUMMARY_NAMESPACES, Utils::DEBIT_NOTE_NAMESPACES, Utils::INVOICE_NAMESPACES, Utils::SUMMARY_NAMESPACES, Utils::TRADE_NAMESPACES, Utils::VOIDED_NAMESPACES

Instance Attribute Summary collapse

Attributes inherited from Line

#taxes

Instance Method Summary collapse

Methods inherited from Line

#build_taxes_xml

Methods included from Utils

#amount_xml, #ubl_ext

Methods inherited from Model

#to_hash

Constructor Details

#initialize(*args) ⇒ Item

Returns a new instance of Item.



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def description
  @description
end

#priceObject

Returns the value of attribute price.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def price
  @price
end

#price_codeObject

Returns the value of attribute price_code.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def price_code
  @price_code
end

#price_included_taxObject

Returns the value of attribute price_included_tax.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def price_included_tax
  @price_included_tax
end

#quantityObject

Returns the value of attribute quantity.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def quantity
  @quantity
end

#unit_codeObject

Returns the value of attribute unit_code.



7
8
9
# File 'lib/sunat_invoice/item.rb', line 7

def unit_code
  @unit_code
end

Instance Method Details

#bi_valueObject



49
50
51
52
53
# 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_nameObject



24
25
26
# File 'lib/sunat_invoice/item.rb', line 24

def line_tag_name
  'InvoiceLine'
end

#quantity_tag_nameObject



28
29
30
# File 'lib/sunat_invoice/item.rb', line 28

def quantity_tag_name
  'InvoicedQuantity'
end

#sale_priceObject



65
66
67
68
# 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_taxesObject



55
56
57
58
59
60
61
62
63
# 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_priceObject



43
44
45
46
47
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# 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