Class: Colppy::Product

Inherits:
Resource show all
Defined in:
lib/colppy/resources/product.rb

Constant Summary collapse

ATTRIBUTES_MAPPER =
{
  idItem: :id,
  idEmpresa: :company_id,
  codigo: :code,
  descripcion: :name,
  detalle: :detail,
  precioVenta: :sell_price,
  ultimoPrecioCompra: :last_purchase_price,
  ctaInventario: :inventory_account,
  ctaCostoVentas: :sales_costs_account,
  ctaIngresoVentas: :sales_account,
  iva: :tax,
  tipoItem: :item_type,
  unidadMedida: :measure_unit,
  minimo: :minimum_stock,
  disponibilidad: :availability
}.freeze
PROTECTED_DATA_KEYS =
[:id, :company_id, :name, :code].freeze
DATA_KEYS_SETTERS =
(ATTRIBUTES_MAPPER.values - PROTECTED_DATA_KEYS).freeze

Instance Attribute Summary collapse

Attributes inherited from Resource

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#[]=, #inspect

Methods included from Utils

rename_params_hash

Constructor Details

#initialize(client: nil, company: nil, **params) ⇒ Product

Returns a new instance of Product.



68
69
70
71
72
73
74
75
76
77
# File 'lib/colppy/resources/product.rb', line 68

def initialize(client: nil, company: nil, **params)
  @client = client if client && client.is_a?(Colppy::Client)
  @company = company if company && company.is_a?(Colppy::Company)

  @id = params.delete(:idItem) || params.delete(:id)
  @name = params.delete(:descripcion) || params.delete(:name)
  @code = params.delete(:codigo) || params.delete(:code)

  super(params)
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



3
4
5
# File 'lib/colppy/resources/product.rb', line 3

def code
  @code
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/colppy/resources/product.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/colppy/resources/product.rb', line 3

def name
  @name
end

Class Method Details

.all(client, company) ⇒ Object



26
27
28
# File 'lib/colppy/resources/product.rb', line 26

def all(client, company)
  list(client, company)
end

.list(client, company, parameters = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/colppy/resources/product.rb', line 30

def list(client, company, parameters = {})
  call_parameters = base_params.merge(parameters)
  response = client.call(
    :product,
    :list,
    extended_parameters(client, company, call_parameters)
  )
  if response[:success]
    results = response[:data].map do |params|
      new(params.merge(client: client, company: company))
    end
    parse_list_response(results, response[:total].to_i, call_parameters)
  else
    response
  end
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/colppy/resources/product.rb', line 91

def exist?
  !new?
end

#new?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/colppy/resources/product.rb', line 87

def new?
  @id.nil? || @id.empty?
end

#sales_accountObject



123
124
125
# File 'lib/colppy/resources/product.rb', line 123

def 
  check_mandatory_account!(:sales_account)
end

#saveObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/colppy/resources/product.rb', line 103

def save
  ensure_client_valid! && ensure_company_valid!

  response = @client.call(
    :product,
    operation,
    save_parameters
  )
  if response[:success]
    response_data = response[:data]
    case operation
    when :create
      @id = response_data[:idItem]
    end
    self
  else
    false
  end
end