Skip to content

Instantly share code, notes, and snippets.

@ciastek
Created April 3, 2013 11:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ciastek/5300268 to your computer and use it in GitHub Desktop.
Export contacts from Fat Free CRM and import into Highrise. Copied from https://github.com/tractis/crm_export .
class Export
def perform
@contacts = Contact.where('id > ?', 272)
@organizations = Account.where('id > ?', 261)
@single_address_field = Setting.single_address_field
unless @contacts.blank?
require 'csv'
csv_string = CSV.generate(force_quotes: true, row_sep: "\r\n") do |csv|
csv << ['Name' , 'First name', 'Last name', 'Company', 'Title', 'Background', 'LinkedIn URL', 'Address - Work Street', 'Address - Work City', 'Address - Work State', 'Address - Work Zip', 'Address - Work Country', 'Address - Home Street', 'Address - Home City', 'Address - Home State', 'Address - Home Zip', 'Address - Home Country', 'Address - Other Street', 'Address - Other City', 'Address - Other State', 'Address - Other Zip', 'Address - Other Country', 'Phone number - Work', 'Phone number - Mobile', 'Phone number - Fax', 'Phone number - Pager', 'Phone number - Home', 'Phone number - Skype', 'Phone number - Other', 'Email address - Work', 'Email address - Home', 'Email address - Other', 'Web address - Work', 'Web address - Personal', 'Web address - Other', 'Twitter account - Personal', 'Twitter account - Business', 'Twitter account - Other', 'Instant messenger kind - Work', 'Instant messenger - Work', 'Instant messenger kind - Personal', 'Instant messenger - Personal', 'Instant messenger kind - Other', 'Instant messenger - Other', 'Customer ID', 'Referred By']
@contacts.each do |contact|
organization = get_organization(contact)
address = get_address(contact)
comment = ""
comment << get_department(contact)
comment << get_facebook(contact)
comment << get_birthday(contact)
comment << get_organization_comment(organization)
comment << get_comment(contact)
csv << [
"#{contact.first_name} #{contact.last_name}", # Name
contact.first_name, # First Name
contact.last_name, # Last Name
organization.name, # Company
contact.title, # Title
"#{comment}".strip!, # Background
contact.linkedin, # LinkedIn URL
"#{address["street1"]} #{address["street2"]}".strip!, # Address - Work Street
address["city"], # Address - Work City
address["state"], # Address - Work State
address["zipcode"], # Address - Work Zip
address["country"], # Address - Work Country
"", # Address - Home Street
"", # Address - Home City
"", # Address - Home State
"", # Address - Home Zip
"", # Address - Home Country
"", # Address - Other Street
"", # Address - Other City
"", # Address - Other State
"", # Address - Other Zip
"", # Address - Other Country
organization.phone, # Phone number - Work
contact.mobile, # Phone number - Mobile
organization.fax, # Phone number - Fax
contact.fax, # Phone number - Pager
contact.phone, # Phone number - Home
"", # Phone number - Skype
organization.toll_free_phone, # Phone number - Other
organization.email, # Email address - Work
contact.email, # Email address - Home
contact.alt_email, # Email address - Other
organization.website, # Web address - Work
contact.blog, # Web address - Personal
"", # Web address - Other
"", # Twitter account - Personal
"", # Twitter account - Business
contact.twitter, # Twitter account - Other
"", # Instant messenger kind - Work
"", # Instant messenger - Work
"", # Instant messenger kind - Personal
"", # Instant messenger - Personal
"", # Instant messenger kind - Other
"", # Instant messenger - Other
"", # Customer ID
"" # Referred By
]
end
@organizations.each do |org|
comment = get_comment(org)
csv << [
"", # Name
"", # First Name
"", # Last Name
org.name, # Company
"", # Title
"#{comment}".strip!, # Background
"", # LinkedIn URL
"", # Address - Work Street
"", # Address - Work City
"", # Address - Work State
"", # Address - Work Zip
"", # Address - Work Country
"", # Address - Home Street
"", # Address - Home City
"", # Address - Home State
"", # Address - Home Zip
"", # Address - Home Country
"", # Address - Other Street
"", # Address - Other City
"", # Address - Other State
"", # Address - Other Zip
"", # Address - Other Country
org.phone, # Phone number - Work
"", # Phone number - Mobile
org.fax, # Phone number - Fax
"", # Phone number - Pager
"", # Phone number - Home
"", # Phone number - Skype
org.toll_free_phone, # Phone number - Other
org.email, # Email address - Work
"", # Email address - Home
"", # Email address - Other
org.website, # Web address - Work
"", # Web address - Personal
"", # Web address - Other
"", # Twitter account - Personal
"", # Twitter account - Business
"", # Twitter account - Other
"", # Instant messenger kind - Work
"", # Instant messenger - Work
"", # Instant messenger kind - Personal
"", # Instant messenger - Personal
"", # Instant messenger kind - Other
"", # Instant messenger - Other
"", # Customer ID
"" # Referred By
]
end
end
File.open("#{Rails.root}/contacts.csv", 'w') do |f|
f.write(csv_string)
end
end
end
private
def get_organization(contact)
account = contact.account
if account.nil?
OpenStruct.new({
name: "",
phone: nil,
fax: nil,
toll_free_phone: nil,
email: nil,
website: nil,
background_info: nil,
})
else
OpenStruct.new({
name: account.name,
phone: account.phone,
fax: account.fax,
toll_free_phone: account.toll_free_phone,
email: account.email,
website: account.website,
background_info: account.background_info
})
end
end
def get_address(contact)
add = {}
if address = contact.business_address
if @single_address_field == true
address.full_address.blank? ? add["street1"] = "" : add["street1"] = address.full_address
add["street2"] = ""
add["city"] = ""
add["state"] = ""
add["zipcode"] = ""
add["country"] = ""
else
add["street1"] = address.street1
add["street2"] = address.street2
add["city"] = address.city
add["state"] = address.state
add["zipcode"] = address.zipcode
add["country"] = address.country
end
end
add
end
def get_department(contact)
if contact.department.blank?
""
else
"Department: #{contact.department}\n"
end
end
def get_facebook(contact)
if contact.facebook.blank?
""
else
"Facebook: #{contact.facebook}\n"
end
end
def get_birthday(contact)
if contact.born_on.blank?
""
else
"Birthday: #{contact.born_on}\n"
end
end
def get_organization_comment(organization)
if organization.background_info.blank?
""
else
"Organization Background Info:\n#{organization.background_info}\n\n"
end
end
def get_comment(entity)
if entity.background_info.blank?
comment = ""
else
comment = "Background Info:\n" + entity.background_info + "\n\n"
end
entity.comments.each { |c| comment += "#{c.updated_at}\n#{c.comment}\n\n" }
comment
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment