class Colppy::SellInvoice
Constants
- ATTRIBUTES_MAPPER
- DATA_KEYS_SETTERS
- PROTECTED_DATA_KEYS
- VALID_RECEIPT_TYPES
Attributes
cae[R]
id[R]
items[R]
number[R]
url[R]
Public Class Methods
all(client, company)
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 41 def all(client, company) list(client, company) end
get(client, company, id)
click to toggle source
# 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 = {})
click to toggle source
# 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
new(client: nil, company: nil, customer: nil, **params)
click to toggle source
Calls superclass method
Colppy::Resource::new
# 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
Private Class Methods
base_params()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 84 def base_params { filter:[], order: { field: ["nroFactura"], order: "desc" } } end
extended_parameters(client, company, parameters)
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 77 def extended_parameters(client, company, parameters) [ client.session_params, company.params, parameters ].inject(&:merge) end
extended_response(response, client, company)
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 94 def extended_response(response, client, company) [ response[:infofactura], itemsFactura: response[:itemsFactura], url: response[:UrlFacturaPdf], client: client, company: company ].inject(&:merge) end
Public Instance Methods
[]=(key, value)
click to toggle source
Calls superclass method
Colppy::Resource#[]=
# File lib/colppy/resources/sell_invoice.rb, line 143 def []=(key, value) ensure_editability! super end
customer=(new_customer)
click to toggle source
# 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?()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 129 def editable? cae.nil? || cae.empty? end
new?()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 125 def new? id.nil? || id.empty? end
save()
click to toggle source
# 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_charged()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 139 def total_charged @items.map(&:total_charged).inject(0,:+) end
Private Instance Methods
attr_inspect()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 177 def attr_inspect [:id, :number, :cae] end
base_charged_amounts()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 306 def base_charged_amounts { total_taxed: 0.00, total_nontaxed: 0.00, total: 0.00, tax_total: 0.00, tax_breakdown: {} } end
calculate_charged_amounts()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 274 def calculate_charged_amounts if @items.nil? || @items.empty? raise DataError.new("In order to save an SellInvoice you should at least have one item in it. Add one with .add_item()") else charged_details = @items.each_with_object(base_charged_amounts) do |item, result| tax = item.tax total = item.total_charged if tax > 0 dividend = tax.to_f divisor = (100.0 + dividend).to_f item_tax_amount = (( total * dividend ) / divisor).round(2) item_taxed_amount = (total - item_tax_amount).round(2) result[:tax_total] += item_tax_amount result[:total_taxed] += item_taxed_amount tax_breakdown_data = TaxTotal.add_to_tax_breakdown( tax, item_tax_amount, item_taxed_amount, result[:tax_breakdown][tax] ) result[:tax_breakdown][tax] = tax_breakdown_data else result[:total_nontaxed] += total end result[:total] += total end @taxes_totals = parse_taxes_totals(charged_details[:tax_breakdown].values) charged_details end end
company_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 186 def company_id return @company.id if @company @data[:company_id] || "" end
customer_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 181 def customer_id return @customer.id if @customer @data[:customer_id] || "" end
ensure_editability!()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 316 def ensure_editability! unless editable? raise ResourceError.new("You cannot change any value of this invoice, because it's already processed by AFIP. You should create a new one instead") end end
ensure_payment_setup!()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 322 def ensure_payment_setup! if status_id == "Cobrada" && (@payments.nil? || @payments.empty?) raise ResourceError.new("You cannot save this invoice, because it's doesn't have any payment associated to it and the status is #{@data[:status_id]}. Add one calling .add_payment({})") end end
general_params(charged_amounts)
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 236 def general_params(charged_amounts) { idCliente: customer_id, idEmpresa: company_id, idUsuario: @client.username, descripcion: @data[:description] || "", fechaFactura: valid_date(@data[:invoice_date]), idCondicionPago: payment_condition_id, fechaPago: valid_date(@data[:payment_date]), idEstadoAnterior: previous_status_id, idEstadoFactura: status_id, idFactura: @id || "", idMoneda: @data[:currency_id] || "1", idTipoComprobante: receipt_type_id, idTipoFactura: invoice_type_id, netoGravado: charged_amounts[:total_taxed] || 0.00, netoNoGravado: charged_amounts[:total_non_taxed] || 0.00, nroFactura1: @data[:invoice_number1] || "0001", nroFactura2: @data[:invoice_number2] || "00000000", percepcionIVA: @data[:tax_perception] || 0.00, percepcionIIBB: @data[:iibb_perception] || 0.00, totalFactura: charged_amounts[:total] || 0.00, totalIVA: charged_amounts[:tax_total] || 0.00, valorCambio: @data[:exchange_rate] || "1", nroRepeticion: @data[:repetition_number] || "1", periodoRep: @data[:repetition_period] || "1", nroVencimiento: @data[:expiration_number] || "0", tipoVencimiento: @data[:expiration_type] || "1", fechaFin: @data[:end_date] || "" }.tap do |params| params[:tipoFactura] = invoice_type if invoice_type if status_id == "Cobrada" params[:totalpagadofactura] = @payments.map(&:amount).inject(0,:+) end params[:labelfe] = "Factura Electrónica" if @data[:electronic_bill] end end
invoice_type()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 210 def invoice_type if receipt_type_id == "8" "Contado" end end
invoice_type_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 207 def invoice_type_id validate_type!(:invoice_type_id, VALID_INVOICE_TYPES) end
payment_condition_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 191 def payment_condition_id validate_type!(:payment_condition_id, VALID_PAYMENT_CONDITIONS) end
previous_status_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 194 def previous_status_id if status = @data[:previous_status_id] validate_type!(status, VALID_STATUS_ID) else "" end end
receipt_type_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 204 def receipt_type_id validate_type!(:receipt_type_id, VALID_RECEIPT_TYPES) end
save_parameters()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 225 def save_parameters charged_amounts = calculate_charged_amounts [ @client.session_params, general_params(charged_amounts), itemsFactura: @items.map(&:save_parameters), ItemsCobro: @payments.map(&:save_parameters), totalesiva: @taxes_totals.map(&:save_parameters) ].inject(&:merge) end
status_id()
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 201 def status_id validate_type!(:status_id, VALID_STATUS_ID) end
validate_type!(data_key, valid_types)
click to toggle source
# File lib/colppy/resources/sell_invoice.rb, line 215 def validate_type!(data_key, valid_types) type = @data[data_key] if type && valid_types.include?(type) type else raise DataError.new("The #{data_key} is invalid. The value should be any of this ones: #{valid_types.join(", ")}") "" end end