Skip to content

Instantly share code, notes, and snippets.

@boriscy
Last active March 2, 2017 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boriscy/3fa36b3a3245b513faa58d84d7e09025 to your computer and use it in GitHub Desktop.
Save boriscy/3fa36b3a3245b513faa58d84d7e09025 to your computer and use it in GitHub Desktop.
class Option < ActiveRecord::Base
validates :key , :presence => {:message => '<span style= "color: red">Es necesario este Campo</span>'}
validates :key, :uniqueness => { :message => '<span style= "color: red">Ya se establecio el valor de este parametro.</span>'}
enum prime: [:no,:yes]
enum quote: [:quote_1]
VALUES_BY_DEFAULT = %w(shopping_cart_default_value
shopping_cart_max_value
minimun_weight_product
limit_quantity_product
maximun_number_product_cart
costo_aereo_forwarder_miami
peso_volumetrico_denominador
margin_error_weight
average_tax_usa
impuesto_promedio_iva
tipo_cambio
costo_seguro
fund_tushop
apply_days_shopping_cart
dimensiones_defecto
only_prime
telefono_tushop
celular_tushop
cost_products_not_prime
additional_pound_cost
time_delivery_from_miami
apply_round_quarter_weight
show_courier
max_weight_tushop_postal
max_price_tushop_postal
arancel_ga_tushop_postal
additional_pound_cost_tushop_postal
apply_pagosnet_creditcard
impuesto_iva_recuperable
apply_ga_global_values
arancel_ga_global
show_advertised_main
apply_free_price_weight
free_price_weight
minimun_products_for_free_price_weight
shipping_charges_default
convert_lb_kg
price_per_kg
ga_iva_handling_aspa
comision_aspa
quote_using
cost_administrate_weight
comision_minimum_price
comision_maximum_price
price_comision_minimum
price_comision_maximum
percentage_comision
margin_error_volumen
switch_cart_default_cero
first_shopping_cart_default_value
nit_company_tushopbolivia
costo_flete_aereo_calculo_cif
weight_manipuleo_average
cost_administrate_weight_low
pendiente_manipuleo
costo_min_manipuleo
peso_constante)
TYPE_OF_VALUE = {'shopping_cart_default_value' => '$us',
'shopping_cart_max_value' => '$us',
'minimun_weight_product' => 'Libra',
'limit_quantity_product' => 'Maxima Cantidad',
'maximun_number_product_cart' => 'Maxima Cantidad',
'costo_aereo_forwarder_miami' => '$us/libras',
'peso_volumetrico_denominador' => 'in/libras',
'margin_error_weight' => '',
'average_tax_usa' => '%',
'impuesto_promedio_iva' => '%',
'tipo_cambio' => 'Bs.',
'costo_seguro' => '%',
'fund_tushop' => '%',
'apply_days_shopping_cart' => 'Dias',
'dimensiones_defecto' =>'pulgadas',
'time_delivery_from_miami'=>'Dias',
'max_weight_tushop_postal'=>'Libras',
'max_price_tushop_postal'=> '$us',
'arancel_ga_tushop_postal'=> '%',
'additional_pound_cost_tushop_postal'=> '$us/libras',
'impuesto_iva_recuperable' => '%',
'arancel_ga_global' => '%',
'free_price_weight' => '%',
'shipping_charges_default' => '$us',
'convert_lb_kg' => 'kg/Lb',
'price_per_kg' => '$us',
'ga_iva_handling_aspa' => '%',
'comision_aspa' => '%',
'cost_administrate_weight' => '$us',
'comision_minimum_price' => '$us',
'comision_maximum_price' => '$us',
'price_comision_minimum' => '$us',
'price_comision_maximum' => '$us',
'percentage_comision' => '%',
'first_shopping_cart_default_value' => '$us.',
'weight_manipuleo_average' => 'Libras',
'cost_administrate_weight_low' => '$us',
'costo_min_manipuleo'=> '$us',
'peso_constante' => 'Libras'}
#________________ INTUIT PARAMETER __________#
# TOKEN_INTUIT = ''
# TOKEN_SECRET_INTUIT = ''
# REALMID_INTUIT = ''
#________________ PARAMETER QUOTE 2 __________#
COST_ADMINISTRATE_WEIGHT = 1.5
WEIGHT_MANIPULEO_AVERAGE = 20
COST_ADMINISTRATE_WEIGHT_LOW = 0.49
PENDIENTE_MANIPULEO = 0.04
COSTO_MIN_MANIPULEO = 0.5
PESO_CONSTANTE = 25
COMISION_MINIMUM_PRICE = 40
COMISION_MAXIMUM_PRICE = 500
PRICE_COMISION_MINIMUM = 3.99
PRICE_COMISION_MAXIMUM = 49.99
PERCENTAGE_COMISION = 10
# __ new parameter (2017 feb)
COSTO_FLETE_AEREO_CALCULO_CIF = 0.95
#_____________________________________________#
SHIPPING_CHARGES_DEFAULT = 4.99
SHOPPING_CART_DEFAULT_VALUE = 9.99
SHOPPING_CART_MAX_VALUE = 2000
MINIMUN_WEIGHT_PRODUCT = 0.25
LIMIT_QUANTITY_PRODUCT = 5
MAXIMUN_NUMBER_PRODUCT_CART = 10
COSTO_AEREO_FORWARDER_MIAMI = 1.91
PESO_VOLUMETRICO_DENOMINADOR = 166
MARGIN_ERROR_WEIGHT = 1.00
MARGIN_ERROR_VOLUMEN = 1.00
AVERAGE_TAX_USA = 1.10
IMPUESTO_PROMEDIO_IVA = 14.94
TIPO_CAMBIO = 6.96
COSTO_SEGURO = 1.00
FUND_TUSHOP = 2.00
COST_PRODUCTS_NOT_PRIME = 1.00
ADDITIONAL_POUND_COST = 1.00
IMPUESTO_IVA_RECUPERABLE = 13
AMAZON_VOLUMEN_FACTOR = 1
APPLY_DAYS_SHOPPING_CART = 25
DIMENSIONES_DEFECTO = 10
ONLY_PRIME = Option.primes[:yes]
TELEFONO_TUSHOP = '2-393939'
CELULAR_TUSHOP = '787 66498'
NIT_COMPANY_TUSHOPBOLIVIA = '2354882016'
TIME_DELIVERY_FROM_MIAMI = '7-14 dias'
APPLY_ROUND_QUARTER_WEIGHT = Option.primes[:yes]
SHOW_COURIER = Option.primes[:yes]
MAX_WEIGHT_TUSHOP_POSTAL = 4.4
MAX_PRICE_TUSHOP_POSTAL = 100
ARANCEL_GA_TUSHOP_POSTAL = 0
ADDITIONAL_POUND_COST_TUSHOP_POSTAL = 7.99
APPLY_PAGOSNET_CREDITCARD = Option.primes[:yes]
APPLY_GA_GLOBAL_VALUES = Option.primes[:no]
ARANCEL_GA_GLOBAL = 7.5
SHOW_ADVERTISED_MAIN = Option.primes[:yes]
MINIMUN_PRODUCTS_FOR_FREE_PRICE_WEIGHT = 2
APPLY_FREE_PRICE_WEIGHT = Option.primes[:no]
FREE_PRICE_WEIGHT = 0.1
#_______________ cotizador
QUOTE_USING = Option.quotes[:quote_1]
# ________________________ PARAMETER COTIZADOR 2 __________________________#
CONVERT_LB_KG = 2.2
PRICE_PER_KG = 4.75
GA_IVA_HANDLING_ASPA = 13.057
COMISION_ASPA = 32
# ______________________ New user descuentos ____________________ #
SWITCH_CART_DEFAULT_CERO = Option.primes[:yes]
FIRST_SHOPPING_CART_DEFAULT_VALUE = 0
def self.value_for(name)
if name.to_sym == :limit_quantity_product || name.to_sym == :maximun_number_product_cart
Option.where( key: name ).first.value.to_i
elsif name.to_sym == :telefono_tushop || name.to_sym == :celular_tushop || name.to_sym == :time_delivery_from_miami || name.to_sym == :nit_company_tushopbolivia
Option.where( key: name ).first.description
else
Option.where( key: name ).first.value
end
rescue
"Option::#{name.upcase.to_s}".constantize
end
def self.type_for(name)
TYPE_OF_VALUE[name.to_s]
rescue
nil
end
def self.max_price_cart
Option.value_for(:shopping_cart_max_value)
end
def self.shopping_cart_price_default(user_id = nil)
if !user_id.nil?
user = User.find(user_id)
if user.shopping_carts.empty? && Option.value_for(:switch_cart_default_cero) == 1
return self.value_for(:first_shopping_cart_default_value)
end
end
return self.value_for(:shopping_cart_default_value)
rescue
return self.value_for(:shopping_cart_default_value)
end
# Option.shopping_cart_price_default(current_user.id rescue nil)
def self.find_init(name)
Option.where( key: name ).first_or_initialize
end
def self.admit_products_prime?
Option.value_for(:only_prime) == 1
end
def self.i18n_quotes
result = {}
self.quotes.keys.each do |key|
result[I18n.t "option_quotes.#{key}"] = self.quotes[key]
end
return result
end
def self.data_cotizador
result = [:minimun_weight_product,
:peso_volumetrico_denominador,
:margin_error_weight,
:amazon_volumen_factor,
:shipping_charges_default,
:costo_aereo_forwarder_miami,
:costo_seguro,
:fund_tushop,
#:impuesto_iva_recuperable,
# :additional_pound_cost,
:impuesto_promedio_iva,
:cost_administrate_weight,
:costo_flete_aereo_calculo_cif]
return result
end
def self.data_manipuleo_admin
result = [:pendiente_manipuleo,
:costo_min_manipuleo,
:peso_constante]
return result
end
def self.parameter_comision
result = [:comision_minimum_price,
:comision_maximum_price,
:price_comision_minimum,
:price_comision_maximum,
:percentage_comision]
return result
end
# def self.data_cotizador_cart
# result = [:minimun_weight_product,
# :peso_volumetrico_denominador,
# :margin_error_weight,
# :impuesto_iva_recuperable,
# :shipping_charges_default]
#
# return result
# end
#
# def self.data_cotizador_item
# result = [:costo_aereo_forwarder_miami,
# :costo_seguro,
# :fund_tushop,
# :impuesto_promedio_iva,
# :additional_pound_cost]
#
# return result
# end
def self.free_price_tushop
result = [:apply_free_price_weight,
:minimun_products_for_free_price_weight,
:free_price_weight]
return result
end
def self.price_off_new_users
result = [:switch_cart_default_cero,
:first_shopping_cart_default_value]
return result
end
def self.parameter_cart
result = [:shopping_cart_default_value,
:shopping_cart_max_value,
:limit_quantity_product,
:maximun_number_product_cart,
:tipo_cambio,
:apply_days_shopping_cart]
return result
end
def self.data_tushop
result = [:time_delivery_from_miami,
:apply_round_quarter_weight,
:apply_pagosnet_creditcard,
:show_advertised_main,
:telefono_tushop,
:celular_tushop,
:nit_company_tushopbolivia]
return result
end
def self.data_iva_ga_global
result = [:apply_ga_global_values,
:arancel_ga_global]
return result
end
def self.type_switch?(value_name)
value_name.to_sym == :only_prime ||
value_name.to_sym == :show_courier ||
value_name.to_sym == :apply_round_quarter_weight ||
value_name.to_sym == :apply_pagosnet_creditcard ||
value_name.to_sym == :show_advertised_main ||
value_name.to_sym == :apply_ga_global_values ||
value_name.to_sym == :apply_free_price_weight ||
value_name.to_sym == :switch_cart_default_cero
end
def self.type_text?(value_name)
value_name.to_sym == :telefono_tushop ||
value_name.to_sym == :celular_tushop ||
value_name.to_sym == :time_delivery_from_miami ||
value_name.to_sym == :nit_company_tushopbolivia
end
end
class QuickbooksInvoice
attr_accessor :service
def initialize
intuit_token = Option.find_init(:token_intuit)
token_secret_intuit = Option.find_init(:token_secret_intuit)
realmId_intuit = Option.find_init(:realmId_intuit)
access_token = OAuth::AccessToken.new(QB_OAUTH_CONSUMER,
intuit_token.description,
token_secret_intuit.description)
@service = Quickbooks::Service::Invoice.new
@service.access_token = access_token
@service.company_id = realmId_intuit.description
# @service.query(nil, per_page:1)
return @service
rescue Exception => e
raise '>>> Error authorize token invoice'
end
#___________ ITEMs Field-> {price_unit=299($2.99), quantity, description}
def create_invoice(customer_name, customer_email, items = [])
# _____________________ INITIAL INVOICE _______________________
invoice = Quickbooks::Model::Invoice.new
# _________________________________ CUSTOMER
customer_service = QuickbooksCustomer.new
customer_id = customer_service.create_find(customer_name.to_s,
customer_email.to_s)
# _________________________________
items.each do |item|
# item_unit_price = item[:price_unit]
# item_total_price = item[:price_total]
# _________________________________ ITEM TYPE
itemtype_service = QuickbooksItem.new
itemtype_id = itemtype_service.create_find_service(item[:name_service].to_s,
item[:description_service].to_s)
line_item = Quickbooks::Model::InvoiceLineItem.new
line_item.amount = item[:price_total]
line_item.description = item[:description].to_s
line_item.sales_item! do |detail|
detail.unit_price = item[:price_unit]
detail.quantity = item[:quantity].to_i
detail.item_id = itemtype_id
end
# _______________ INCLUDE ITEM IN INVOICE
invoice.line_items << line_item
end
# ______________ ASIGNED INVOICE VALUES
invoice.customer_id = customer_id
invoice.txn_date = Date.today
due_date_day = Option.value_for(:apply_days_shopping_cart).to_f + 1
invoice.due_date = due_date_day.days.from_now.utc.strftime('%Y-%m-%d')
# _______________ GENERATE INVOICE
invoice_create = @service.create(invoice)
return invoice_create.id.to_i
# _______________________________
rescue Exception => e
raise ">>>Error: '#{ e.message }' >>>Backtrace; '#{ (e.backtrace.first rescue nil) }'"
end
def customer_search(id_invoice)
invoice_result = service.find_by(:id, id_invoice)
customer_id = invoice_result.first.customer_ref.value.to_i
return customer_id
rescue
raise 'Error Invoice ID not found'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment