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.



43
44
45
46
47
48
49
# File 'lib/colppy/resources/company.rb', line 43

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.



12
13
14
# File 'lib/colppy/resources/company.rb', line 12

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/colppy/resources/company.rb', line 12

def name
  @name
end

Class Method Details

.all(client) ⇒ Object



22
23
24
# File 'lib/colppy/resources/company.rb', line 22

def all(client)
  list(client)
end

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/colppy/resources/company.rb', line 26

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



135
136
137
138
139
140
141
# File 'lib/colppy/resources/company.rb', line 135

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/colppy/resources/company.rb', line 116

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



60
61
62
63
64
# File 'lib/colppy/resources/company.rb', line 60

def customer_by_id(id)
  ensure_client_valid!

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

#customers(params = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/colppy/resources/company.rb', line 51

def customers(params = {})
  ensure_client_valid!

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

#paramsObject



143
144
145
# File 'lib/colppy/resources/company.rb', line 143

def params
  { idEmpresa: @id }
end

#product_by_code(code) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/colppy/resources/company.rb', line 76

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/colppy/resources/company.rb', line 87

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



67
68
69
70
71
72
73
74
75
# File 'lib/colppy/resources/company.rb', line 67

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



109
110
111
112
113
# File 'lib/colppy/resources/company.rb', line 109

def sell_invoice_by_id(id)
  ensure_client_valid!

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

#sell_invoices(params = {}) ⇒ Object



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

def sell_invoices(params = {})
  ensure_client_valid!

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