Class: Omie::Company

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

Overview

This class abstracts the Company resource from Omie (Ref: Cliente) which is used for all kinds of companies in Omie, mainly Clients and Suppliers. It aims at providing abstractions to the endpoints described in /.

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

Constant Summary collapse

CALLS =
{
  list: 'ListarClientes',
  create: 'IncluirCliente',
  update: 'AlterarCliente',
  find: 'ConsultarCliente',
  delete: 'ExcluirCliente',
  upsert: 'UpsertCliente',
  associate: 'AssociarCodIntCliente'
}.freeze
URI =
'/v1/geral/clientes/'

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

#bairroObject

Returns the value of attribute bairro.



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

def bairro
  @bairro
end

#cepObject

Returns the value of attribute cep.



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

def cep
  @cep
end

#cidadeObject

Returns the value of attribute cidade.



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

def cidade
  @cidade
end

#cnpj_cpfObject

Returns the value of attribute cnpj_cpf.



28
29
30
# File 'lib/omie/company.rb', line 28

def cnpj_cpf
  @cnpj_cpf
end

#codigo_cliente_integracaoObject

Returns the value of attribute codigo_cliente_integracao.



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

def codigo_cliente_integracao
  @codigo_cliente_integracao
end

#codigo_cliente_omieObject

Returns the value of attribute codigo_cliente_omie.



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

def codigo_cliente_omie
  @codigo_cliente_omie
end

#codigo_paisObject

Returns the value of attribute codigo_pais.



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

def codigo_pais
  @codigo_pais
end

#complementoObject

Returns the value of attribute complemento.



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

def complemento
  @complemento
end

#contatoObject

Returns the value of attribute contato.



28
29
30
# File 'lib/omie/company.rb', line 28

def contato
  @contato
end

#emailObject

Returns the value of attribute email.



28
29
30
# File 'lib/omie/company.rb', line 28

def email
  @email
end

#enderecoObject

Returns the value of attribute endereco.



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

def endereco
  @endereco
end

#endereco_numeroObject

Returns the value of attribute endereco_numero.



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

def endereco_numero
  @endereco_numero
end

#estadoObject

Returns the value of attribute estado.



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

def estado
  @estado
end

#inativoObject

Returns the value of attribute inativo.



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

def inativo
  @inativo
end

#inscricao_estadualObject

Returns the value of attribute inscricao_estadual.



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

def inscricao_estadual
  @inscricao_estadual
end

#nome_fantasiaObject

Returns the value of attribute nome_fantasia.



28
29
30
# File 'lib/omie/company.rb', line 28

def nome_fantasia
  @nome_fantasia
end

#razao_socialObject

Returns the value of attribute razao_social.



28
29
30
# File 'lib/omie/company.rb', line 28

def razao_social
  @razao_social
end

Class Method Details

.associate(codigo_cliente_omie, codigo_cliente_integracao) ⇒ Object

Associate the local entry with an existing entry at Omie AssociarCodIntCliente. Omie will find the existing entry through the #codigo_cliente_omie and updates its #codigo_cliente_integracao

Parameters:

  • codigo_cliente_omie (String)

    The id of the existing entry at Omie

  • codigo_cliente_integracao (String)

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



128
129
130
131
132
133
134
135
# File 'lib/omie/company.rb', line 128

def self.associate(codigo_cliente_omie, codigo_cliente_integracao)
  params = {
    codigo_cliente_integracao: codigo_cliente_integracao,
    codigo_cliente_omie: codigo_cliente_omie
  }

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

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

Record a new company using the IncluirCliente call and returns an instance of Omie::Company with the data from the created company.

Parameters:

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

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

Returns:

Raises:



46
47
48
# File 'lib/omie/company.rb', line 46

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

.find(params) ⇒ Omie::Company?

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

Parameters:

  • params (Hash)

    a hash containing the search attribute to locate the company

Returns:

  • (Omie::Company)

    the found company

  • (nil)

    in case of no company found



84
85
86
87
88
# File 'lib/omie/company.rb', line 84

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

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

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

Parameters:

Returns:



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

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

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

  response = request(URI, CALLS[:list], options)
  response['clientes_cadastro'].map { |client| Omie::Company.new(client) }
rescue Omie::RequestError
  []
end

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

Updates an existing company using the AlterarCliente call and returns an instance of the updated company. Omie will use either the #codigo_cliente_integracao or the #codigo_cliente_omie 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 company and the attributes/values to be updated.

Returns:

Raises:

  • (Omie::RequestError)

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



66
67
68
# File 'lib/omie/company.rb', line 66

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

Instance Method Details

#add_tag(tag = nil) ⇒ Omie::Company

Add a new tag method to formatted into Omie`s structure. It does not duplicate entries.

Returns:



168
169
170
171
172
173
174
175
176
# File 'lib/omie/company.rb', line 168

def add_tag(tag = nil)
  if tag && !tag_values.include?(tag)
    tags << {
      tag: tag
    }
    tag_values << tag
  end
  self
end

#associate_entryBoolean

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

Returns:

  • (Boolean)


207
208
209
210
# File 'lib/omie/company.rb', line 207

def associate_entry
  Omie::Company.associate(codigo_cliente_omie, codigo_cliente_integracao)
  true
end

#saveOmie::Company

Save the company.

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

Returns:



185
186
187
188
189
190
191
192
193
194
# File 'lib/omie/company.rb', line 185

def save
  company = if saved?
              Omie::Company.update(as_json.except('tag_values'))
            else
              Omie::Company.create(as_json.except('tag_values'))
            end

  self.codigo_cliente_omie = company.codigo_cliente_omie if company
  company
end

#saved?Boolean

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

Returns:

  • (Boolean)


200
201
202
# File 'lib/omie/company.rb', line 200

def saved?
  !codigo_cliente_omie.blank?
end

#tag_valuesArray<String>

Get method for tag_values attribute.

Returns:

  • (Array<String>)

    list containing only the values of #tags



158
159
160
161
# File 'lib/omie/company.rb', line 158

def tag_values
  @tag_values ||= []
  @tag_values
end

#tagsArray<Hash>

Get method for tags attribute.

Returns:

  • (Array<Hash>)

    list of hashes containing the tags’ information with the following structure => “tag value”



142
143
144
145
# File 'lib/omie/company.rb', line 142

def tags
  @tags ||= []
  @tags
end

#tags=(value) ⇒ Object

Set method for tags attribute to be used for mass assignment of attribuites returned from Omie. It also sets #tag_values.



149
150
151
152
# File 'lib/omie/company.rb', line 149

def tags=(value)
  @tags = value
  @tag_values = @tags.map { |t| t[:tag] }
end