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

#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



122
123
124
125
126
127
128
129
# File 'lib/omie/company.rb', line 122

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.

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



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(page = 1, per_page = 50) ⇒ 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.



102
103
104
105
106
107
108
109
# File 'lib/omie/company.rb', line 102

def self.list(page = 1, per_page = 50)
  params = { pagina: page, registros_por_pagina: per_page }

  response = request(URI, CALLS[:list], params)
  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.

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.



162
163
164
165
166
167
168
169
170
# File 'lib/omie/company.rb', line 162

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.



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

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.



179
180
181
182
183
184
185
186
187
188
# File 'lib/omie/company.rb', line 179

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



194
195
196
# File 'lib/omie/company.rb', line 194

def saved?
  !codigo_cliente_omie.blank?
end

#tag_valuesArray<String>

Get method for tag_values attribute.



152
153
154
155
# File 'lib/omie/company.rb', line 152

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

#tagsArray<Hash>

Get method for tags attribute.



136
137
138
139
# File 'lib/omie/company.rb', line 136

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.



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

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