Class: Omie::Product

Inherits:
BaseResource show all
Defined in:
lib/omie/product.rb

Overview

This class abstracts the product resource from Omie (Ref: Produto) It aims at providing abstractions to the endpoints described in /.

The class methods of Omie::Product usually performs requests to Omie API and manipulate product objects that contain the returned values. Attributes’ names are equal to the Portuguese names described in the API documentation.

Constant Summary collapse

CALLS =
{
  list: 'ListarProdutos',
  create: 'IncluirProduto',
  update: 'AlterarProduto',
  find: 'ConsultarProduto',
  delete: 'ExcluirProduto',
  simple: 'UpsertProduto',
  associate: 'AssociarCodIntProduto'
}.freeze
INTERNAL_MODELS =
{
  recomendacoes_fiscais: Omie::TaxRecommendation
}.freeze
URI =
'/v1/geral/produtos/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResource

#initialize, request, request_and_initialize, #update_attributes

Constructor Details

This class inherits a constructor from Omie::BaseResource

Instance Attribute Details

#codigoObject

They do not keep the same name style =(



31
32
33
# File 'lib/omie/product.rb', line 31

def codigo
  @codigo
end

#codigo_produtoObject

Returns the value of attribute codigo_produto.



30
31
32
# File 'lib/omie/product.rb', line 30

def codigo_produto
  @codigo_produto
end

#codigo_produto_integracaoObject

Returns the value of attribute codigo_produto_integracao.



30
31
32
# File 'lib/omie/product.rb', line 30

def codigo_produto_integracao
  @codigo_produto_integracao
end

#descricaoObject

Returns the value of attribute descricao.



30
31
32
# File 'lib/omie/product.rb', line 30

def descricao
  @descricao
end

#descricao_statusObject

Returns the value of attribute descricao_status.



29
30
31
# File 'lib/omie/product.rb', line 29

def descricao_status
  @descricao_status
end

#ncmObject

Returns the value of attribute ncm.



29
30
31
# File 'lib/omie/product.rb', line 29

def ncm
  @ncm
end

#recomendacoes_fiscaisObject



32
33
34
# File 'lib/omie/product.rb', line 32

def recomendacoes_fiscais
  @recomendacoes_fiscais
end

#tipoItemObject

They do not keep the same name style =(



31
32
33
# File 'lib/omie/product.rb', line 31

def tipoItem
  @tipoItem
end

#unidadeObject

Returns the value of attribute unidade.



29
30
31
# File 'lib/omie/product.rb', line 29

def unidade
  @unidade
end

#valor_unitarioObject

Returns the value of attribute valor_unitario.



29
30
31
# File 'lib/omie/product.rb', line 29

def valor_unitario
  @valor_unitario
end

Class Method Details

.associate(codigo_produto, codigo_produto_integracao) ⇒ Object

Associate the local entry with an existing entry at Omie AssociarCodIntProduto. Omie will find the existing entry through the #codigo_produto and updates its #codigo_produto_integracao

Parameters:

  • codigo_produto (String)

    The id of the existing entry at Omie

  • codigo_produto_integracao (String)

    The integration id to be used by the existing entry - usually a local id.



131
132
133
134
135
136
137
138
# File 'lib/omie/product.rb', line 131

def self.associate(codigo_produto, codigo_produto_integracao)
  params = {
    codigo_produto: codigo_produto,
    codigo_produto_integracao: codigo_produto_integracao
  }

  request(URI, CALLS[:associate], params)
end

.create(params = {}) ⇒ Omie::Product

Record a new product using the IncluirProduto call and returns an instance of Omie::Product with the data from the created product.

Parameters:

  • params (Hash) (defaults to: {})

    a hash containing the data to be recorded in the product based on the available attributes of this class

Returns:

Raises:



47
48
49
# File 'lib/omie/product.rb', line 47

def self.create(params = {})
  request_and_initialize(URI, CALLS[:create], params)
end

.find(params) ⇒ Omie::Product?

Search for a product using the ConsultarProduto call and returns an instance of the found product or nil otherwise. One may use either the #codigo_cliente_omie or #codigo_cliente_integracao to search for product

Parameters:

  • params (Hash)

    a hash containing the search attribute to locate the product

Returns:

  • (Omie::Product)

    the found product

  • (nil)

    in case of no product found



85
86
87
88
89
# File 'lib/omie/product.rb', line 85

def self.find(params)
  request_and_initialize(URI, CALLS[:find], params)
rescue Omie::RequestError
  nil
end

.list(options = {}) ⇒ Array<Omie::Product>

Get a paginated list of companies recorded in Omie by using the Listarprodutos. You may change the params to get other pages of records.

Parameters:

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/omie/product.rb', line 102

def self.list(options = {})
  default = {
    pagina: 1, registros_por_pagina: 50,
    apenas_importado_api: 'N', filtrar_apenas_omiepdv: 'N'
  }

  default.each do |k, v|
    options[k] = v unless options.key?(k)
  end

  response = request(URI, CALLS[:list], options)
  response['produto_servico_cadastro'].map do |product|
    Omie::Product.new(product)
  end
rescue Omie::RequestError
  []
end

.update(params = {}) ⇒ Omie::Product

Update an existing product using the AlterarProduto call and returns an instance of the updated product. Omie will use either the #codigo_produto_integracao or the #codigo_produto to identify the entry to be changed. It will change only the informed attributes in params.

Parameters:

  • params (Hash) (defaults to: {})

    a hash containing the search attribute to locate the product and the attributes/values to be updated.

Returns:

Raises:

  • (Omie::RequestError)

    in case of failed requests due to failed validations or when the product was not found.



67
68
69
# File 'lib/omie/product.rb', line 67

def self.update(params = {})
  request_and_initialize(URI, CALLS[:update], params)
end

Instance Method Details

#associate_entryBoolean

Updates the omie entry with the local id for integration purposes.

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/omie/product.rb', line 169

def associate_entry
  Omie::Product.associate(codigo_produto, codigo_produto_integracao)
  true
end

#saveOmie::Product

Save the product.

If the product is new a record is created on Omie, otherwise the existing record gets updated.

Returns:



147
148
149
150
151
152
153
154
155
156
# File 'lib/omie/product.rb', line 147

def save
  product = if saved?
              Omie::Product.update(as_json)
            else
              Omie::Product.create(as_json)
            end

  self.codigo_produto = product.codigo_produto if product
  product
end

#saved?Boolean

Check whether the object has a related record on Omie based on the #codigo_produto attribute

Returns:

  • (Boolean)


162
163
164
# File 'lib/omie/product.rb', line 162

def saved?
  !codigo_produto.blank?
end