Class: Colppy::Company

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

Constant Summary collapse

ATTRIBUTES_MAPPER =
{
  idPlan: :plan_id,
  activa: :active,
  fechaVencimiento: :expiration_date
}.freeze
PROTECTED_DATA_KEYS =
ATTRIBUTES_MAPPER.values

Constants included from Utils

Utils::DATA_KEYS_SETTERS

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, **params) ⇒ Company

Returns a new instance of Company.



74
75
76
77
78
79
80
# File 'lib/colppy/resources/company.rb', line 74

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

  @id = params.delete(:IdEmpresa) || params.delete(:id)
  @name = params.delete(:razonSocial) || params.delete(:name)
  super(params)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.all(client) ⇒ Object



39
40
41
# File 'lib/colppy/resources/company.rb', line 39

def all(client)
  list(client)
end

.get(client, id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/colppy/resources/company.rb', line 59

def get(client, id)
  response = client.call(
    :company,
    :read,
    client.session_params.merge({ idEmpresa: id })
  )
  if response[:success]
    new(response[:data].merge(client: client))
  else
    response
  end
end

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/colppy/resources/company.rb', line 43

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

Instance Method Details

#account_name(account_id) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/colppy/resources/company.rb', line 166

def ()
  return if available_accounts.nil? || available_accounts.empty?

  if  = available_accounts.detect{ |a| a[:account_id].to_s == .to_s }
    [:name]
  end
end

#available_accountsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/colppy/resources/company.rb', line 147

def available_accounts
  return @available_accounts unless @available_accounts.nil? || @available_accounts.empty?

  response = @client.call(
    :inventory,
    :accounts_list,
    params.merge(@client.session_params)
  )
  if response[:success]
    @available_accounts = response[:cuentas].map do ||
      {
        id: [:Id],
        account_id: [:idPlanCuenta],
        full_name: [:Descripcion],
        name: [:Descripcion].split(' - ')[1]
      }
    end
  end
end

#customer_by_id(id) ⇒ Object Also known as: customer



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

def customer_by_id(id)
  ensure_client_valid!

  Customer.get(@client, self, id)
end

#customers(params = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/colppy/resources/company.rb', line 82

def customers(params = {})
  ensure_client_valid!

  if params.empty?
    Customer.all(@client, self)
  else
    Customer.list(@client, self, params)
  end
end

#paramsObject



174
175
176
# File 'lib/colppy/resources/company.rb', line 174

def params
  { idEmpresa: @id }
end

#product_by_code(code) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/colppy/resources/company.rb', line 107

def product_by_code(code)
  ensure_client_valid!

  params = {
    filter: [
      { field: "codigo", op: "=", value: code }
    ]
  }
  response = Product.list(@client, self, params)
  response[:results].last
end

#product_by_id(id) ⇒ Object Also known as: product



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/colppy/resources/company.rb', line 118

def product_by_id(id)
  ensure_client_valid!

  params = {
    filter: [
      { field: "idItem", op: "=", value: id }
    ]
  }
  response = Product.list(@client, self, params)
  response[:results].last
end

#products(params = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/colppy/resources/company.rb', line 98

def products(params = {})
  ensure_client_valid!

  if params.empty?
    Product.all(@client, self)
  else
    Product.list(@client, self, params)
  end
end

#sell_invoice_by_id(id) ⇒ Object Also known as: sell_invoice



140
141
142
143
144
# File 'lib/colppy/resources/company.rb', line 140

def sell_invoice_by_id(id)
  ensure_client_valid!

  SellInvoice.get(@client, self, id)
end

#sell_invoices(params = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/colppy/resources/company.rb', line 131

def sell_invoices(params = {})
  ensure_client_valid!

  if params.empty?
    SellInvoice.all(@client, self)
  else
    SellInvoice.list(@client, self, params)
  end
end