Class: Colppy::SellInvoice

Inherits:
Invoice show all
Defined in:
lib/colppy/resources/sell_invoice.rb

Constant Summary collapse

VALID_RECEIPT_TYPES =
%w(4 6 8 NCV)
ATTRIBUTES_MAPPER =
{
  descripcion: :description,
  fechaFactura: :invoice_date,
  idCondicionPago: :payment_condition_id, # validate
  fechaPago: :payment_date,
  idEmpresa: :company_id,
  idCliente: :customer_id,
  idEstadoAnterior: :previous_status_id, # validate
  idEstadoFactura: :status_id, # validate
  idMoneda: :currency_id,
  idTipoComprobante: :receipt_type_id, # validate
  idTipoFactura: :invoice_type_id, # validate
  idUsuario: :user_id,
  labelfe: :electronic_bill,
  netoGravado: :total_taxed,
  netoNoGravado: :total_nontaxed,
  nroFactura1: :invoice_number1,
  nroFactura2: :invoice_number2,
  percepcionIIBB: :iibb_perception,
  percepcionIVA: :tax_perception,
  tipoFactura: :invoice_type, # validate
  totalFactura: :total,
  totalIVA: :total_taxes,
  totalpagadofactura: :total_payed,
  valorCambio: :exchange_rate,
  nroRepeticion: :repetition_number,
  periodoRep: :repetition_period,
  nroVencimiento: :expiration_number,
  tipoVencimiento: :expiration_type,
  fechaFin: :end_date
}.freeze
PROTECTED_DATA_KEYS =
[:id, :company_id, :customer_id, :number, :cae].freeze
DATA_KEYS_SETTERS =
(ATTRIBUTES_MAPPER.values - PROTECTED_DATA_KEYS).freeze

Constants inherited from Invoice

Invoice::VALID_INVOICE_TYPES, Invoice::VALID_PAYMENT_CONDITIONS, Invoice::VALID_STATUS_ID

Instance Attribute Summary collapse

Attributes inherited from Resource

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Invoice

#add_item, #add_payment, #remove_item

Methods inherited from Resource

#inspect

Methods included from Utils

rename_params_hash

Constructor Details

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

Returns a new instance of SellInvoice.



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

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

  @id = params.delete(:idFactura) || params.delete(:id)
  @number = params.delete(:nroFactura) || params.delete(:number)
  @cae = params.delete(:cae)

  @items = parse_items(params.delete(:itemsFactura))
  @payments = parse_payments(params.delete(:ItemsCobro))
  @taxes_totals = parse_taxes_totals(params.delete(:totalesiva))

  super(params)
end

Instance Attribute Details

#caeObject (readonly)

Returns the value of attribute cae.



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

def cae
  @cae
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.all(client, company) ⇒ Object



41
42
43
# File 'lib/colppy/resources/sell_invoice.rb', line 41

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

.get(client, company, id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/colppy/resources/sell_invoice.rb', line 62

def get(client, company, id)
  response = client.call(
    :sell_invoice,
    :read,
    extended_parameters(client, company, { idFactura: id })
  )
  if response[:success]
    new(extended_response(response, client, company))
  else
    response
  end
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/colppy/resources/sell_invoice.rb', line 45

def list(client, company, parameters = {})
  call_parameters = base_params.merge(parameters)
  response = client.call(
    :sell_invoice,
    :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



143
144
145
146
147
# File 'lib/colppy/resources/sell_invoice.rb', line 143

def []=(key, value)
  ensure_editability!

  super
end

#customer=(new_customer) ⇒ Object



149
150
151
# File 'lib/colppy/resources/sell_invoice.rb', line 149

def customer=(new_customer)
  @customer = new_customer if new_customer.is_a?(Colppy::Customer)
end

#editable?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/colppy/resources/sell_invoice.rb', line 129

def editable?
  cae.nil? || cae.empty?
end

#new?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/colppy/resources/sell_invoice.rb', line 125

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

#saveObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/colppy/resources/sell_invoice.rb', line 153

def save
  ensure_editability!
  ensure_client_valid!
  ensure_payment_setup!

  response = @client.call(
    :sell_invoice,
    :create,
    save_parameters
  )
  if response[:success]
    @cae = response[:cae]
    @id = response[:idfactura]
    @number = response[:nroFactura]
    @data[:invoice_date] = response[:fechaFactura]
    @data[:url] = response[:UrlFacturaPdf]
    self
  else
    false
  end
end

#total_chargedObject



139
140
141
# File 'lib/colppy/resources/sell_invoice.rb', line 139

def total_charged
  @items.map(&:total_charged).inject(0,:+)
end