Created
July 28, 2014 13:49
-
-
Save brunoadacosta/88fb670a06b7dbdf693f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SupplierIntegration | |
class FastShop < SupplierIntegration::Interface | |
def self.synchronize_products(supplier_id) | |
supplier = Supplier.find(supplier_id) | |
last_date = supplier.update_products.last ? supplier.update_products.last.date : get_today_date | |
current_date = get_today_date | |
update_products_catalog | |
supplier.new_catalog_update(current_date) | |
Resque.enqueue(AddNewProductsToCampaignJob, supplier.id) | |
end | |
def self.check_availability(order_id) | |
stars_order = Order.find(order_id) | |
contract = stars_order.campaign.meta[:fastshop_contract] | |
order = FastshopCatalog::Entity::Order.new | |
order.contract_code = contract | |
order.zip_code = stars_order.address_zipcode.gsub(/[^0-9]/,'') | |
order.document = stars_order.participant.document_number.gsub(/[^0-9]/,'') | |
order.number = stars_order.address_house_number | |
order.delivery_type = "P" | |
order.partner_order_number = stars_order.id.to_s | |
order.items = [] | |
total = 0.0 | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
next unless item.product.supplier.name == "FastShop" | |
order_item = FastshopCatalog::Entity::OrderItem.new | |
order_item.sku = item.product.sku | |
order_item.quantity = item.amount.nil? ? 1 : item.amount.to_i | |
order_item.price = ApplicationController.helpers.number_with_precision(item.product.product_price.to_f, :precision => 2).gsub(',', '.') | |
order.items << order_item | |
total = total + item.product.product_price | |
end | |
order.total_amount = ApplicationController.helpers.number_with_precision(total, :precision => 2).gsub(',', '.') | |
check_availability_service = FastshopCatalog::CheckAvailabilityService.new | |
begin | |
response = check_availability_service.check(order) | |
rescue FastshopCatalog::ServiceException => e | |
if e.code == 4 | |
participant_sigin_up(stars_order.participant, contract) | |
self.check_availability(order_id) | |
else | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
next unless item.product.supplier.name == "FastShop" | |
item.processed_by_supplier(ToOrderItem::Status::SUPPLIER_ERROR, {:request => "", :response => response, :code => e.code, :message => e.description }) | |
end | |
end | |
end | |
end | |
def self.send_order(order_id) | |
stars_order = Order.find(order_id) | |
has_fastshop_items = false | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
has_fastshop_items = true if item.product.supplier.name == "FastShop" | |
end | |
return unless has_fastshop_items | |
check_availability(order_id) | |
contract = stars_order.campaign.meta[:fastshop_contract] | |
order = FastshopCatalog::Entity::Order.new | |
order.contract_code = contract | |
order.zip_code = stars_order.address_zipcode.gsub(/[^0-9]/,'') | |
order.document = stars_order.participant.document_number.gsub(/[^0-9]/,'') | |
order.number = stars_order.address_house_number | |
order.delivery_type = "P" | |
order.partner_order_number = stars_order.id.to_s | |
order.items = [] | |
total = 0.0 | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
next unless item.product.supplier.name == "FastShop" | |
order_item = FastshopCatalog::Entity::OrderItem.new | |
order_item.sku = item.product.sku | |
order_item.quantity = item.amount | |
order_item.price = ApplicationController.helpers.number_with_precision(item.product.product_price.to_f, :precision => 2).gsub(',', '.') | |
order.items << order_item | |
total = total + item.product.product_price | |
end | |
order.total_amount = ApplicationController.helpers.number_with_precision(total, :precision => 2).gsub(',', '.') | |
order_placement = FastshopCatalog::OrderPlacementService.new | |
begin | |
response = order_placement.place_order(order) | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
next unless item.product.supplier.name == "FastShop" | |
item.processed_by_supplier(ToOrderItem::Status::PROCESSED, {:request => "", :response => response.to_s, :code => "0", :message => "OK" }) | |
end | |
rescue FastshopCatalog::ServiceException => e | |
if e.code != 41 | |
stars_order.order_items.with_supplier.not_send.each do |item| | |
next unless item.product.supplier.name == "FastShop" | |
item.processed_by_supplier(ToOrderItem::Status::SUPPLIER_ERROR, {:request => "", :response => response, :code => e.code, :message => e.description }) | |
end | |
end | |
end | |
end | |
def self.participant_sigin_up(participant_stars, contract) | |
participant = self.get_fastshop_participant(participant_stars, contract) | |
participant_service = FastshopCatalog::ParticipantService.new | |
begin | |
participant_service.insert(participant) | |
rescue FastshopCatalog::ServiceException => e | |
p "FODEU" | |
end | |
end | |
def self.send_checkout(item, session_key) | |
end | |
def self.update_order_item(order_item_id) | |
end | |
def self.check_stock(product, supplier_integration_id, amount=1) | |
true | |
end | |
private | |
def self.get_product(sku) | |
product = Product.find_by_sku(sku) | |
product.nil? ? Product.new : product | |
end | |
def self.get_category(category_name) | |
category_name = "Fast - #{category_name}" | |
category_exist = Category.find_by_name(category_name) | |
category_exist.nil? ? Category.create( :name => category_name ) : category_exist | |
end | |
def self.get_today_date | |
today = Date.today | |
today.strftime("%Y%m%d") | |
end | |
def self.pt_format(date) | |
date = date.to_s | |
pt_date = "#{date[6..7]}/#{date[4..5]}/#{date[0..3]}" | |
end | |
def self.float_to_int(value) | |
value.to_f * 100 | |
end | |
def self.get_voltage(voltage) | |
voltage = "<strong>Voltagem: #{voltage}</strong> <br/><br/>" | |
end | |
def self.get_address(contract, cep) | |
dne_service = FastshopCatalog::ExternalDneService.new | |
result = dne_service.query(contract, cep) | |
end | |
def self.get_fastshop_participant(participant_stars, contract) | |
address = get_address(contract, participant_stars.address_zipcode.gsub("-", "")) | |
participant = FastshopCatalog::Entity::Participant.new | |
participant.contract_code = contract | |
participant.document = participant_stars.document_number.gsub(/[^0-9]/,'') | |
participant.name = participant_stars.name | |
#year, month, day | |
if participant_stars.birthday.nil? | |
datetime = (Date.today - 23.years).to_datetime | |
else | |
participant_stars.birthday.to_datetime unless participant_stars.birthday.nil? | |
end | |
participant.birth_date = "/Date(#{(datetime.to_f*1000).to_i}-#{datetime.utc_offset})/" | |
participant.gender = participant_stars.gender || "M" | |
participant.category = 'StarsPremium' | |
prefix = participant_stars.phone1.gsub("(", "").gsub(")", "").gsub("-", "").slice(0, 2).strip | |
participant.home_phone_prefix = '11' | |
telephone = participant_stars.phone1.gsub("(", "").gsub(")", "").gsub("-", "").slice(2, 10).strip | |
participant.home_phone = telephone | |
participant.work_phone_prefix = '11' | |
participant.work_phone = telephone | |
participant.mobile_phone_prefix = '11' | |
participant.mobile_phone = telephone | |
participant.email = participant_stars.email | |
participant.status = 'A' | |
participant.operation_type = '0' | |
participant.address = FastshopCatalog::Entity::Address.new | |
participant.address.description = 'Principal' | |
participant.address.home_type = 1 | |
participant.address.address_type = address["TipoEndereco"] | |
participant.address.address = address["Endereco"] | |
participant.address.number = participant_stars.address_house_number.slice(0, 10) | |
participant.address.complement = participant_stars.address_complement.slice(0, 20) | |
participant.address.neighborhood = address["Bairro"] | |
participant.address.city = address["Cidade"] | |
participant.address.state = address["Estado"] | |
participant.address.zip_code = participant_stars.address_zipcode.gsub("-", "") | |
# Don't use directly the setters, use this helper, home_type which will set | |
# both home_type_id and home_type_description. | |
participant.address.home_type = FastshopCatalog::Entity::Address::HOME_TYPE_COMPANY | |
participant.business_unity = FastshopCatalog::Entity::BusinessUnity.new | |
participant.business_unity.company_document = '15598035000141' | |
participant.business_unity.company_name = 'StarsPremium' | |
participant.business_unity.trading_name = 'StarsPremium' | |
participant | |
end | |
def self.update_products_catalog | |
#muda status de todos os produtos | |
supplier = Supplier.find_by_name "FastShop" | |
Product.where(:supplier_id => supplier.id).update_all(:status => ToProduct::Status::INACTIVE) | |
catalog_service = FastshopCatalog::CatalogService.new | |
contract = "1234567890" | |
contract = "1014231328" if Rails.env.production? | |
catalogs = catalog_service.search(contract) | |
catalogs.each do |catalog| | |
qtd = 1 | |
qtd = 2 if catalog["ProdutoSkus"].count > 1 and catalog["ProdutoSkus"].first["Descricao"] == "Voltagem" | |
(1..qtd).each do |i| | |
sku = catalog["ProdutoSkus"][i-1]["Sku"] | |
departamento = catalog["Departamentos"].count == 0 ? 'Outros' : catalog["Departamentos"].first["Nome"] | |
category = get_category(departamento) | |
product = get_product(sku) | |
product.category = category | |
product.supplier = supplier | |
product.name = catalog["Nome"].strip | |
product.price = float_to_int(catalog["PrecoVenda"]) | |
product.product_price = catalog["PrecoVenda"] | |
description = catalog["Descricao"] | |
description = get_voltage(catalog["ProdutoSkus"][i-1]["Nome"]) + description if qtd == 2 | |
product.description = description | |
product.sku = sku | |
product.status = ToProduct::Status::ACTIVE | |
# photo | |
if product.new_record? | |
begin | |
images = catalog["ImagemGrande"].split(";") | |
uri = images.first | |
file = Tempfile.open(File.basename(uri), :encoding => 'ascii-8bit') | |
file.write open(uri).read | |
product.photo1 = file | |
if images.size > 1 | |
uri = images.second | |
file = Tempfile.open(File.basename(uri), :encoding => 'ascii-8bit') | |
file.write open(uri).read | |
product.photo2 = file | |
end | |
rescue | |
end | |
end | |
product.save | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment