class Colppy::Customer

Constants

ATTRIBUTES_MAPPER
DATA_KEYS_SETTERS
PROTECTED_DATA_KEYS

Attributes

id[R]
name[R]

Public Class Methods

all(client, company) click to toggle source
# File lib/colppy/resources/customer.rb, line 37
def all(client, company)
  list(client, company)
end
get(client, company, id) click to toggle source
# File lib/colppy/resources/customer.rb, line 58
def get(client, company, id)
  response = client.call(
    :customer,
    :read,
    extended_parameters(client, company, { idCliente: id })
  )
  if response[:success]
    new(response[:data].merge(client: client, company: company))
  else
    response
  end
end
list(client, company, parameters = {}) click to toggle source
# File lib/colppy/resources/customer.rb, line 41
def list(client, company, parameters = {})
  call_parameters = base_params.merge(parameters)
  response = client.call(
    :customer,
    :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
new(client: nil, company: nil, **params) click to toggle source
Calls superclass method Colppy::Resource::new
# File lib/colppy/resources/customer.rb, line 91
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(:idCliente) || params.delete(:id)
  @name = params.delete(:RazonSocial) || params.delete(:name)
  super(params)
end

Private Class Methods

base_params() click to toggle source
# File lib/colppy/resources/customer.rb, line 80
def base_params
  {
    filter:[],
    order: [{
      field: "RazonSocial",
      dir: "asc"
    }]
  }
end
extended_parameters(client, company, parameters) click to toggle source
# File lib/colppy/resources/customer.rb, line 73
def extended_parameters(client, company, parameters)
  [ client.session_params,
    company.params,
    parameters
  ].inject(&:merge)
end

Public Instance Methods

name=(new_name) click to toggle source
# File lib/colppy/resources/customer.rb, line 131
def name=(new_name)
  @name = new_name
end
new?() click to toggle source
# File lib/colppy/resources/customer.rb, line 105
def new?
  id.nil? || id.empty?
end
save() click to toggle source
# File lib/colppy/resources/customer.rb, line 109
def save
  ensure_client_valid! && ensure_company_valid!

  response = @client.call(
    :customer,
    operation,
    save_parameters
  )
  if response[:success]
    response_data = response[:data]
    case operation
    when :create
      @id = response_data[:idCliente]
    when :update
      @data[:NombreFantasia] = response_data[:nombreCliente]
    end
    self
  else
    false
  end
end

Private Instance Methods

attr_inspect() click to toggle source
# File lib/colppy/resources/customer.rb, line 137
def attr_inspect
  [:id, :name]
end
general_info_params() click to toggle source
# File lib/colppy/resources/customer.rb, line 153
def general_info_params
  {
    idUsuario: @client.username,
    idCliente: id || "",
    idEmpresa: @company.id,
    NombreFantasia: @data[:fantasy_name] || "",
    RazonSocial: @name || "",
    CUIT: @data[:cuit] || "",
    dni: @data[:dni] || "",
    DirPostal: @data[:address] || "",
    DirPostalCiudad: @data[:address_city] || "",
    DirPostalCodigoPostal: @data[:address_zipcode] || "",
    DirPostalProvincia: @data[:address_state] || "",
    DirPostalPais: @data[:address_country] || "Argentina",
    Telefono: @data[:phone_number] || "",
    Email: @data[:email] || ""
  }
end
operation() click to toggle source
# File lib/colppy/resources/customer.rb, line 141
def operation
  new? ? :create : :update
end
other_info_params() click to toggle source
# File lib/colppy/resources/customer.rb, line 172
def other_info_params
  {
    Activo: @data[:active] || "1",
    FechaAlta: @data[:FechaAlta] || "",
    DirFiscal: @data[:legal_address] || "",
    DirFiscalCiudad: @data[:legal_address_city] || "",
    DirFiscalCodigoPostal: @data[:legal_address_zipcode] || "",
    DirFiscalProvincia: @data[:legal_address_state] || "",
    DirFiscalPais: @data[:legal_address_country] || "Argentina",
    idCondicionPago: @data[:payment_condition_id] || "0",
    idCondicionIva: @data[:tax_condition_id] || "",
    porcentajeIVA: @data[:tax] || "21",
    idPlanCuenta: @data[:idPlanCuenta] || "",
    CuentaCredito: @data[:CuentaCredito] || "",
    DirEnvio: @data[:DirEnvio] || "",
    DirEnvioCiudad: @data[:DirEnvioCiudad] || "",
    DirEnvioCodigoPostal: @data[:DirEnvioCodigoPostal] || "",
    DirEnvioProvincia: @data[:DirEnvioProvincia] || "",
    DirEnvioPais: @data[:DirEnvioPais] || ""
  }
end
save_parameters() click to toggle source
# File lib/colppy/resources/customer.rb, line 145
def save_parameters
  [
    @client.session_params,
    info_general: general_info_params,
    info_otra: other_info_params
  ].inject(&:merge)
end