Last active
August 18, 2017 07:31
-
-
Save serg2801/fa5d799ed48caaa8197984dd3399aa21 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
# frozen_string_literal: true | |
require 'roo' | |
class ParserXlsx | |
def initialize(company) | |
@company = company | |
file_path = company.file.path | |
open_file(file_path) | |
end | |
def open_file(file_path) | |
open_file = File.open(file_path) | |
xlsx = Roo::Spreadsheet.open(open_file, extension: :xlsx) | |
parse_xlsx(xlsx) | |
end | |
def parse_xlsx(xlsx) | |
room = nil | |
xlsx.sheet(0).each_with_index do |row, index| | |
next if index == 0 | |
unless row[0].blank? | |
room = create_or_find_room(row) unless row[0].blank? | |
next | |
end | |
create_or_find_item(room.id, row) if row[0].blank? | |
end | |
end | |
def create_or_find_room(row) | |
room = Room.find_or_initialize_by(item_group_code: row[0], company_id: @company.id) | |
room.assign_attributes(group_name: row[1], color: row[2]) | |
room.save | |
room | |
end | |
def create_or_find_item(room_id, row) | |
dimensions_format = dimensions_format(row[6], row[5], row[7]) | |
item = Item.find_or_initialize_by(room_id: room_id, item_number: row[3]) | |
item.assign_attributes(item_number: row[3], description: row[1], item_char_depth_in: row[6], | |
item_char_height_in: row[5], item_char_length_in: row[4], unit_price: row[8], | |
net_price: row[9], dimensions_inches_format: dimensions_format, | |
package_char_weight_lb: row[7]) | |
item.save | |
end | |
def dimensions_format(depth, height, weight) | |
# 63"W x 16"D x 37"H | |
weight.to_s + '"' + 'W' + ' x ' + depth.to_s + '"' + 'D' + ' x ' + height.to_s + '"' + 'H' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment