Class: SunatInvoice::Item

Inherits:
Model
  • Object
show all
Includes:
Utils
Defined in:
lib/sunat_invoice/item.rb

Constant Summary

Constants included from Utils

Utils::UBL_NAMESPACES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#amount_xml, #ubl_ext

Constructor Details

#initialize(*args) ⇒ Item

Returns a new instance of Item.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sunat_invoice/item.rb', line 11

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
  super(*args)
  @taxes ||= []
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#priceObject

Returns the value of attribute price.



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

def price
  @price
end

#price_codeObject

Returns the value of attribute price_code.



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

def price_code
  @price_code
end

#quantityObject

Returns the value of attribute quantity.



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

def quantity
  @quantity
end

#taxesObject

Returns the value of attribute taxes.



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

def taxes
  @taxes
end

#unit_codeObject

Returns the value of attribute unit_code.



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

def unit_code
  @unit_code
end

Instance Method Details

#bi_valueObject



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

def bi_value
  # bi of sale = price without taxes * quantity
  (@price.to_f * @quantity.to_f).round(2)
end

#build_item(xml) ⇒ Object



67
68
69
70
71
# File 'lib/sunat_invoice/item.rb', line 67

def build_item(xml)
  xml['cac'].Item do
    xml['cbc'].Description description
  end
end

#sale_priceObject



39
40
41
42
# File 'lib/sunat_invoice/item.rb', line 39

def sale_price
  # unit price with tax
  (@price.to_f + sum_taxes).round(2)
end

#sale_taxesObject



29
30
31
32
33
34
35
36
37
# File 'lib/sunat_invoice/item.rb', line 29

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

#sum_taxesObject



44
45
46
# File 'lib/sunat_invoice/item.rb', line 44

def sum_taxes
  taxes.map(&:amount).sum
end

#taxes_xml(xml, currency) ⇒ Object



73
74
75
76
77
# File 'lib/sunat_invoice/item.rb', line 73

def taxes_xml(xml, currency)
  taxes&.each do |tax|
    tax.xml(xml, currency)
  end
end

#xml(xml, index, currency) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sunat_invoice/item.rb', line 48

def xml(xml, index, currency)
  xml['cac'].InvoiceLine do
    xml['cbc'].ID(index + 1)
    xml['cbc'].InvoicedQuantity(@quantity, unitCode: unit_code)
    amount_xml(xml['cbc'], 'LineExtensionAmount', bi_value, currency)
    xml['cac'].PricingReference do
      xml['cac'].AlternativeConditionPrice do
        amount_xml(xml['cbc'], 'PriceAmount', sale_price, currency)
        xml['cbc'].PriceTypeCode price_code
      end
    end
    taxes_xml(xml, currency)
    build_item(xml)
    xml['cac'].Price do
      amount_xml(xml['cbc'], 'PriceAmount', price, currency)
    end
  end
end