Class: FE::Document::Item

Inherits:
Element
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/facturacr/document/item.rb

Constant Summary collapse

UNITS =
%w[ Al Alc Cm I Os Spe St Sp m kg s A K mol cd   m/s m/s² 1/m kg/m³ A/m² A/m mol/m³ cd/m² 1 rad sr Hz N Pa J W C V F  S Wb T H °C lm
lx Bq Gy Sv kat Pa·s N·m N/m rad/s rad/s² W/m² J/K J/(kg·K) J/kg W/(m·K) J/m³ V/m C/m³ C/m² F/m H/m J/mol J/(mol·K)
C/kg Gy/s W/sr W/(m²·sr) kat/m³ min h d º ´ ´´ L t Np B eV u ua Unid Gal g Km ln cm mL mm Oz Otros].freeze
SERVICE_UNITS =
%w[Al Alc Os Spe Sp St min h I Cm]
CODE_TYPES =
{
  '01' => 'Código del producto del vendedor',
  '02' => 'Código del producto del comprador',
  '03' => 'Código del producto asignado por la industria',
  '04' => 'Código uso interno',
  '99' => 'Otros'
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Element

#document, #version

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Item

Returns a new instance of Item.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/facturacr/document/item.rb', line 41

def initialize(args = {})
  @document_type = args[:document_type]
  @line_number = args[:line_number]
  @code = args[:code]
  @comercial_code_type = args[:comercial_code_type].presence || '01'
  @comercial_code = args[:comercial_code]
  @quantity = args[:quantity]
  @unit = args[:unit]
  @description = args[:description]
  @unit_price = args[:unit_price]
  @total = args[:total]
  @discount = args[:discount]
  @discount_reason = args[:discount_reason]
  @subtotal = args[:subtotal]
  @taxes = args[:taxes] || []
  @net_total = args[:net_total]
  @exoneration = args[:exoneration]
  @net_tax = args[:net_tax]
  @tariff_item = args[:tariff_item]
  @taxable_base = args[:taxable_base]
  @issued_date = args[:issued_date]
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def code
  @code
end

#comercial_codeObject

Returns the value of attribute comercial_code.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def comercial_code
  @comercial_code
end

#comercial_code_typeObject

Returns the value of attribute comercial_code_type.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def comercial_code_type
  @comercial_code_type
end

#descriptionObject

Returns the value of attribute description.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def description
  @description
end

#discountObject

Returns the value of attribute discount.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def discount
  @discount
end

#discount_reasonObject

Returns the value of attribute discount_reason.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def discount_reason
  @discount_reason
end

#document_typeObject

Returns the value of attribute document_type.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def document_type
  @document_type
end

#exonerationObject

Returns the value of attribute exoneration.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def exoneration
  @exoneration
end

#line_numberObject

Returns the value of attribute line_number.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def line_number
  @line_number
end

#net_taxObject

Returns the value of attribute net_tax.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def net_tax
  @net_tax
end

#net_totalObject

Returns the value of attribute net_total.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def net_total
  @net_total
end

#quantityObject

Returns the value of attribute quantity.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def quantity
  @quantity
end

#subtotalObject

Returns the value of attribute subtotal.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def subtotal
  @subtotal
end

#tariff_itemObject

Returns the value of attribute tariff_item.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def tariff_item
  @tariff_item
end

#taxable_baseObject

Returns the value of attribute taxable_base.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def taxable_base
  @taxable_base
end

#taxesObject

Returns the value of attribute taxes.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def taxes
  @taxes
end

#totalObject

Returns the value of attribute total.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def total
  @total
end

#unitObject

Returns the value of attribute unit.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def unit
  @unit
end

#unit_priceObject

Returns the value of attribute unit_price.



18
19
20
# File 'lib/facturacr/document/item.rb', line 18

def unit_price
  @unit_price
end

Instance Method Details

#build_xml(node, document) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/facturacr/document/item.rb', line 64

def build_xml(node, document)
  @document = document
  @document_type = document.document_type
  raise FE::Error.new("item invalid: #{ errors.messages.map{|k,v| "#{k}=#{v.join(". ")}"}.join("; ")}",class: self.class, messages: errors.messages) unless valid?

  node = Nokogiri::XML::Builder.new if node.nil?
  node.LineaDetalle do |x|
    x.NumeroLinea @line_number

    x.PartidaArancelaria @tariff_item if @tariff_item.present? && document.version_43?

    if document.version_43?
      x.Codigo @code if @code.present?
    end

    if @comercial_code.present? && document.version_43?
      x.CodigoComercial do |x2|
        x2.Tipo @comercial_code_type
        x2.Codigo @comercial_code
      end
    end

    if @comercial_code.present? && document.version_42?
      x.Codigo do |x2|
        x2.Tipo @comercial_code_type
        x2.Codigo @comercial_code
      end
    end
    x.Cantidad @quantity
    x.UnidadMedida @unit
    x.Detalle @description
    x.PrecioUnitario @unit_price
    x.MontoTotal @total

    if document.version_42?
      x.MontoDescuento @discount if @discount.present?
      x.NaturalezaDescuento @discount_reason if @discount_reason.present?
    end
    if @discount.present? && document.version_43?
      x.Descuento do |x2|
        x2.MontoDescuento @discount
        x2.NaturalezaDescuento @discount_reason
      end
    end

    x.SubTotal @subtotal

    x.BaseImponible @taxable_base if @taxable_base.present? && document.version_43?
    @taxes.each do |tax|
      tax.build_xml(x,document)
    end

    x.ImpuestoNeto @net_tax if @net_tax.present?
    x.MontoTotalLinea @net_total
  end

end

#calculations_ok?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/facturacr/document/item.rb', line 124

def calculations_ok?
  errors.add :total, :invalid_amount, message: 'invalid amount' if (@total - (@quantity * @unit_price).round(5)).abs > 1
end

#code_is_mandatory?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/facturacr/document/item.rb', line 128

def code_is_mandatory?
  if Time.zone.now >= Time.zone.parse("2020-12-01").beginning_of_day
    if @issued_date.present? && @issued_date < Time.zone.parse("2020-12-01").beginning_of_day
      false
    else
      true
    end
  else
    false
  end
end