Skip to content

Instantly share code, notes, and snippets.

@gramos
Last active August 29, 2015 14:11
Show Gist options
  • Save gramos/f572349f10dfc4891954 to your computer and use it in GitHub Desktop.
Save gramos/f572349f10dfc4891954 to your computer and use it in GitHub Desktop.
def self.import(file)
CSV.foreach(file, headers: true) do |row|
csv = row.to_hash
#default values
order_type = csv["Shipping Method"].to_s.include?('Pickup') || csv["Shipping Method"].to_s == '' ?
0 : 1
order_date = csv["Fulfilled at"].to_s == '' ? Time.now :
DateTime.strptime(csv["Fulfilled at"].to_s, "%m/%d/%Y %H:%M")
@partner = User.find_by(email: csv["Email"])
if @partner.nil?
name = csv["Name"].tr('#','')
business_name = csv["Shipping Name"]
@partner = User.invite!(role: :partner, skip_invitation: true, email: csv["Email"],
first_name: name)
@partner_profile = @partner.create_partner_profile!(contact_email: csv["Email"],
business_name: business_name, channel: 0)
@loaction = @partner.create_location!(address: csv["Shipping Address1"], address_line_2: csv["Shipping Address2"],
city: csv["Shipping City"], :zipcode => csv["Shipping Zip"],
phone: csv["Shipping Phone"])
end
@item = Item.create!(name: csv["Lineitem name"], quantity: csv["Lineitem quantity"],
item_category_id: 9)
@order = @partner.orders.create!(:order_type => order_type,
:order_date => order_date, notes: csv["Notes"])
@order.items << @item
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment