Class: Colppy::Product

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

Constant Summary

Constants inherited from Resource

Resource::VALID_INVOICE_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#inspect

Constructor Details

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

Returns a new instance of Product.



48
49
50
51
52
53
54
55
# File 'lib/colppy/resources/product.rb', line 48

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)
  @name = params.delete(:descripcion)
  @sku = params.delete(:codigo)
  @data = params
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
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

#skuObject

Returns the value of attribute sku.



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

def sku
  @sku
end

Class Method Details

.all(client, company) ⇒ Object



6
7
8
# File 'lib/colppy/resources/product.rb', line 6

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

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/colppy/resources/product.rb', line 10

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

#[]=(key, value) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/colppy/resources/product.rb', line 89

def []=(key, value)
  key_sym = key.to_sym
  if protected_data_keys.include?(key_sym)
    raise ResourceError.new("You cannot change any of this values: #{protected_data_keys.join(", ")} manually")
  end
  @data[key_sym] = value
end

#new?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/colppy/resources/product.rb', line 57

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

#params_for_invoiceObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/colppy/resources/product.rb', line 97

def params_for_invoice
  {
    idItem: id || "",
    minimo: @data[:minimo] || "0",
    codigo: sku || "",
    tipoItem: @data[:tipoItem] || "P",
    unidadMedida: @data[:unidadMedida] || "u",
    Descripcion: name || "",
    ImporteUnitario: @data[:precioVenta],
    IVA: @data[:iva] || "21",
    Comentario: @data[:detalle] || "",
    idPlanCuenta: @data[:ctaIngresoVentas]
  }
end

#saveObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/colppy/resources/product.rb', line 61

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