-
-
Save danil/5364689 to your computer and use it in GitHub Desktop.
This file contains 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 CML2 | |
# Includes to ProductCategory model | |
module ProductCategory | |
def import node_set | |
::ProductCategory.delete_all | |
::ProductCategory.import_categories \ | |
node_set.xpath('/КоммерческаяИнформация/Классификатор/Группы') | |
end | |
def import_categories node_set, parent = nil | |
node_set.xpath('./Группа').each do |category| | |
product_category = ::ProductCategory.create( | |
name: category.xpath('./Наименование').first.content, | |
uid: category.xpath('./Ид').first.content, | |
product_category: parent | |
) | |
::ProductCategory.import_categories \ | |
category.xpath('./Группы'), product_category | |
end | |
end | |
end | |
# Includes to Product model | |
module Product | |
def import file_name | |
import_dir = "#{Rails.root}/tmp/1c_imports/1c_import_#{Time.now.to_i}" | |
`unzip #{file_name} -d #{import_dir}` | |
node_set = Nokogiri::XML(File.new("#{import_dir}/import.xml")) | |
# Import categories | |
::ProductCategory.import node_set | |
# Import products | |
::Product.delete_all | |
ActiveRecord::Base.connection.execute('TRUNCATE TABLE product_categories_products;') | |
node_set.xpath('/КоммерческаяИнформация/Каталог/Товары/Товар').each do |product| | |
image_file_path = product.xpath('./Картинка').first.try(:content) | |
product_object = ::Product.create( | |
uid: product.xpath('./Ид').first.try(:content), | |
article: product.xpath('./Артикул').first.try(:content), | |
name: product.xpath('./Наименование').first.try(:content), | |
description: product.xpath('./Описание').first.try(:content), | |
image: image_file_path.blank? ? nil : File.open("#{import_dir}/#{image_file_path}") | |
) | |
product.xpath('./Группы/Ид').each do |category_uid| | |
if category = ::ProductCategory.find_by_uid(category_uid.content) | |
category.products << product_object | |
end | |
end | |
end | |
::Price.import import_dir | |
end | |
end | |
# Includes to PriceType model | |
module PriceType | |
def import node_set | |
::PriceType.delete_all | |
node_set.xpath('/КоммерческаяИнформация/ПакетПредложений/ТипыЦен/ТипЦены').each do |price_type| | |
::PriceType.create( | |
uid: price_type.xpath('./Ид').first.try(:content), | |
name: price_type.xpath('./Наименование').first.try(:content), | |
currency: price_type.xpath('./Валюта').first.try(:content), | |
include_dns: price_type.xpath('./Налог/УчтеноВСумме').first.try(:content) == 'true' | |
) | |
end | |
end | |
end | |
# Includes to Price model | |
module Price | |
def import import_dir | |
node_set = Nokogiri::XML(File.new("#{import_dir}/offers.xml")) | |
::PriceType.import node_set | |
node_set.xpath('/КоммерческаяИнформация/ПакетПредложений/Предложения/Предложение').each do |price_set| | |
uid = price_set.xpath('./Ид').first.try(:content) | |
product = ::Product.find_by_uid(uid) | |
product.quantity = price_set.xpath('./Количество').first.try(:content) | |
product.save | |
price_set.xpath('./Цены/Цена').each do |price_node| | |
price_type = ::PriceType.find_by_uid(price_node.xpath('./ИдТипаЦены').first.try(:content)) | |
::Price.create( | |
price_type: price_type, | |
product: product, | |
name: price_node.xpath('./Представление').first.try(:content), | |
price: price_node.xpath('./ЦенаЗаЕдиницу').first.try(:content).to_f, | |
unit: price_node.xpath('./Единица').first.try(:content), | |
ratio: price_node.xpath('./Коэффициент').first.try(:content).to_f | |
) | |
end | |
end | |
end | |
end | |
end |
This file contains 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
class Exchange < ActiveRecord::Base | |
def process | |
Product.import self.file_path | |
self.success = true | |
save | |
end | |
end |
This file contains 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
class ExchangesController < ApplicationController | |
def index | |
case params[:mode] | |
when 'checkauth' | |
authenticate_or_request_with_http_basic do |username, password| | |
if username == 'username' && password == 'secret' | |
cookie_name = "simko_#{Time.now.to_i}" | |
cookie_value = Digest::SHA1.hexdigest(Time.now.to_s + 'dfjhguadfg7fsad7fgadsufjaifgasfu') | |
Exchange.create cookie_value: cookie_value, success: false | |
render :text => "success\nsimko_1c_import\n#{cookie_value}" | |
return | |
end | |
end | |
when 'init' | |
render :text => "zip=yes\nfile_limit=1000000000" | |
return | |
else | |
if exchange = Exchange.find_by_cookie_value(cookies[:simko_1c_import]) | |
if params[:mode] == 'file' | |
file_name = "#{Rails.root}/tmp/1c_imports/#{Time.now.to_i}_#{params[:filename]}" | |
# Passenger hack http://code.google.com/p/phusion-passenger/issues/detail?id=471#c26 | |
request.env['rack.input'].read(1) | |
request.env['rack.input'].rewind | |
File.open(file_name, 'wb') { |f| f.write request.raw_post } | |
exchange.file_path = file_name | |
exchange.save | |
exchange.delay.process | |
end | |
render :text => 'success' | |
return | |
end | |
end | |
render :text => 'failure' | |
end | |
alias create index | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment