Skip to content

Instantly share code, notes, and snippets.

@StaverDmitry
Created May 12, 2017 14:20
Show Gist options
  • Save StaverDmitry/d789630e746716b0e0daf1d40e910d16 to your computer and use it in GitHub Desktop.
Save StaverDmitry/d789630e746716b0e0daf1d40e910d16 to your computer and use it in GitHub Desktop.
def importCustomers
a = Array.new
spreadsheet = open_spreadsheet(params[:file])
header = spreadsheet.row(1)
if Settings.enquiryExpireIn.present?
@expireDate =Time.now+Settings.enquiryExpireIn.to_i.days
end
(2..spreadsheet.last_row).each do |i|
# sleep(1.0/24.0)
row = Hash[[header, spreadsheet.row(i)].transpose]
if row["Deck Level"].present?
cr = Cruise.where(:name =>row["Product"].strip).first
cd = CruiseDirection.where(:name =>row["Route"].strip).first
d = Direction.where(:cruise_id => cr.id, :cruise_direction_id => cd.id).first
if d.present?
t = Tour.where(:departure => row["Departure Date"],:direction_id => d.id).first
if t.present?
if row["Name E-Mail"].present?
c = Customer.where(:email => row["Name E-Mail"].downcase.strip).first
if c.present?
en = Enquiry.where(:id =>row["Booking Number"].to_i).first
if en.present?
qty = row["Qty"].to_i
for i in 1..qty
en.partners.create({:ship_type=>en.ship_type ,:deck=>row["Deck Level"].strip , :unit_price=>row["Sales Price"].to_f})
end
en.pax+=qty
en.outstanding+=row["Gross Amount"].to_f
en.save
a.push("updated Booking")
else
enc = Enquiry.new
enc.id = row["Booking Number"].to_i
enc.brand_id= 2
enc.tour_id =t.id
enc.customer_id=c.id
enc.user_id=2
enc.ship_type = row["Ship Type"].strip
enc.deck= row["Deck Level"].strip
enc.pax= row["Qty"].to_i
enc.unit_price= row["Sales Price"].to_f
enc.cruise_id = cr.id
enc.direction_id = d.id
enc.outstanding = row["Gross Amount"].to_f
enc.status ="Awaiting Payment"
enc.booking_status ="Enquiry"
enc.discount =""
enc.tax = 0.0
enc.is_deleted = false
enc.is_booking = false
enc.expire=@expireDate
if enc.save
paxe = enc.pax
if paxe>=2
for i in 1..paxe-1
enc.partners.create({:ship_type=>enc.ship_type ,:deck=>enc.deck , :unit_price=>enc.unit_price})
end
end
end
a.push("new Booking")
end
else
nc = Customer.new
nc.email = row["Name E-Mail"].downcase.strip
nc.password = Random.rand(12345678...99999999)
nc.first_name="first_name"
nc.last_name="last_name"
if nc.save
e = Enquiry.new
e.id = row["Booking Number"].to_i
e.brand_id= 2
e.tour_id =t.id
e.customer_id=nc.id
e.user_id=2
e.ship_type = row["Ship Type"].strip
e.deck= row["Deck Level"].strip
e.pax= row["Qty"].to_i
e.unit_price= row["Sales Price"].to_f
e.cruise_id = cr.id
e.direction_id = d.id
e.outstanding = row["Gross Amount"].to_f
e.status ="Awaiting Payment"
e.booking_status ="Enquiry"
e.discount =""
e.tax = 0.0
e.is_deleted = false
e.is_booking = false
e.expire=@expireDate
if e.save
pax = e.pax
if pax>=2
for i in 1..pax-1
e.partners.create({:ship_type=>e.ship_type ,:deck=>e.deck , :unit_price=>e.unit_price})
end
end
end
a.push("customer and enquiry created")
end
end
end
else
# if not tour exit create tour and add price
ship_deck =''
deck =''
if row["Deck Level"] = "Above Deck"
deck = 'above'
elsif row["Deck Level"]= "Below Deck"
deck = 'Lower'
end
# Check ship type
case row["Ship Type"]
when 'Premier Plus Ship'
ship_deck = "pps_#{deck}="
when 'Premier Ship'
ship_deck = "ps_#{deck}="
when 'Standard Ship'
ship_deck = "ss_#{deck}="
when 'Ensuite Ship'
ship_deck = "es_#{deck}="
when 'Sailing Yachts'
ship_deck = "sy_#{deck}="
end
nt1 = Tour.new
nt1.name = cr.name
nt1.departure = row["Departure Date"]
nt1.direction_id = d.id
nt1.send(ship_deck,"#{row["Sales Price"].to_f}")
if nt1.save
if row["Name E-Mail"].present?
c12 = Customer.where(:email => row["Name E-Mail"].downcase.strip).first
if c12.present?
en12 = Enquiry.where(:id =>row["Booking Number"].to_i).first
if en12.present?
qty12 = row["Qty"].to_i
for i in 1..qty12
en12.partners.create({:ship_type=>en12.ship_type ,:deck=>row["Deck Level"].strip , :unit_price=>row["Sales Price"].to_f})
end
en12.pax+=qty12
en12.save
a.push("updated Booking")
else
enc12 = Enquiry.new
enc12.id = row["Booking Number"].to_i
enc12.brand_id= 2
enc12.tour_id =nt1.id
enc12.customer_id=c12.id
enc12.user_id=2
enc12.ship_type = row["Ship Type"].strip
enc12.deck= row["Deck Level"].strip
enc12.pax= row["Qty"].to_i
enc12.unit_price= row["Sales Price"].to_f
enc12.cruise_id = cr.id
enc12.direction_id = d.id
enc12.outstanding = row["Gross Amount"].to_f
enc12.status ="Awaiting Payment"
enc12.booking_status ="Enquiry"
enc12.discount =""
enc12.tax = 0.0
enc12.is_deleted = false
enc12.is_booking = false
enc12.expire=@expireDate
if enc12.save
paxe12 = enc12.pax
if paxe12>=2
for i in 1..paxe12-1
enc12.partners.create({:ship_type=>enc12.ship_type ,:deck=>enc12.deck , :unit_price=>enc12.unit_price})
end
end
end
a.push("new Booking tour")
end
else
nc12 = Customer.new
nc12.email = row["Name E-Mail"].downcase.strip
nc12.password = Random.rand(12345678...99999999)
nc12.first_name="first_name"
nc12.last_name="last_name"
if nc12.save
e12 = Enquiry.new
e12.id = row["Booking Number"].to_i
e12.brand_id= 2
e12.tour_id =nt1.id
e12.customer_id=nc12.id
e12.user_id=2
e12.ship_type = row["Ship Type"].strip
e12.deck= row["Deck Level"].strip
e12.pax= row["Qty"].to_i
e12.unit_price= row["Sales Price"].to_f
e12.cruise_id = cr.id
e12.direction_id = d.id
e12.outstanding = row["Gross Amount"].to_f
e12.status ="Awaiting Payment"
e12.booking_status ="Enquiry"
e12.discount =""
e12.tax = 0.0
e12.is_deleted = false
e12.is_booking = false
e12.expire=@expireDate
if e12.save
pax112 = e12.pax
if pax112>=2
for i in 1..pax112-1
e12.partners.create({:ship_type=>e12.ship_type ,:deck=>e12.deck , :unit_price=>e12.unit_price})
end
end
end
a.push("customer and enquiry new tour")
end
end
end
end
end
else
# if not direction, create direction and tour
#check deck level
ship_deck =''
deck =''
if row["Deck Level"] = "Above Deck"
deck = 'above'
elsif row["Deck Level"]= "Below Deck"
deck = 'Lower'
end
# Check ship type
case row["Ship Type"]
when 'Premier Plus Ship'
ship_deck = "pps_#{deck}="
when 'Premier Ship'
ship_deck = "ps_#{deck}="
when 'Standard Ship'
ship_deck = "ss_#{deck}="
when 'Ensuite Ship'
ship_deck = "es_#{deck}="
when 'Sailing Yachts'
ship_deck = "sy_#{deck}="
end
nd = Direction.new
nd.cruise_id = cr.id
nd.cruise_direction_id = cd.id
if nd.save
nt = Tour.new
nt.name = cr.name
nt.departure = row["Departure Date"]
nt.direction_id = nd.id
nt.send(ship_deck,"#{row["Sales Price"].to_f}")
if nt.save
if row["Name E-Mail"].present?
c1 = Customer.where(:email => row["Name E-Mail"].downcase.strip).first
if c1.present?
en1 = Enquiry.where(:id =>row["Booking Number"].to_i).first
if en1.present?
qty1 = row["Qty"].to_i
for i in 1..qty1
en1.partners.create({:ship_type=>en1.ship_type ,:deck=>row["Deck Level"].strip , :unit_price=>row["Sales Price"].to_f})
end
en1.pax+=qty1
en1.save
a.push("updated Booking")
else
enc1 = Enquiry.new
enc1.id = row["Booking Number"].to_i
enc1.brand_id= 2
enc1.tour_id =nt.id
enc1.customer_id=c1.id
enc1.user_id=2
enc1.ship_type = row["Ship Type"].strip
enc1.deck= row["Deck Level"].strip
enc1.pax= row["Qty"].to_i
enc1.unit_price= row["Sales Price"].to_f
enc1.cruise_id = nd.cruise_id
enc1.direction_id = nd.id
enc1.outstanding = row["Gross Amount"].to_f
enc1.status ="Awaiting Payment"
enc1.booking_status ="Enquiry"
enc1.discount =""
enc1.tax = 0.0
enc1.is_deleted = false
enc1.is_booking = false
enc1.expire=@expireDate
if enc1.save
paxe1 = enc1.pax
if paxe1>=2
for i in 1..paxe1-1
enc1.partners.create({:ship_type=>enc1.ship_type ,:deck=>enc1.deck , :unit_price=>enc1.unit_price})
end
end
end
a.push("new Booking")
end
else
nc1 = Customer.new
nc1.email = row["Name E-Mail"].downcase.strip
nc1.password = Random.rand(12345678...99999999)
nc1.first_name="first_name"
nc1.last_name="last_name"
if nc1.save
e1 = Enquiry.new
e1.id = row["Booking Number"].to_i
e1.brand_id= 2
e1.tour_id =nt.id
e1.customer_id=nc1.id
e1.user_id=2
e1.ship_type = row["Ship Type"].strip
e1.deck= row["Deck Level"].strip
e1.pax= row["Qty"].to_i
e1.unit_price= row["Sales Price"].to_f
e1.cruise_id = nd.cruise_id
e1.direction_id = nd.id
e1.outstanding = row["Gross Amount"].to_f
e1.status ="Awaiting Payment"
e1.booking_status ="Enquiry"
e1.discount =""
e1.tax = 0.0
e1.is_deleted = false
e1.is_booking = false
e1.expire=@expireDate
if e1.save
pax11 = e1.pax
if pax11>=2
for i in 1..pax11-1
e1.partners.create({:ship_type=>e1.ship_type ,:deck=>e1.deck , :unit_price=>e1.unit_price})
end
end
end
a.push("customer and enquiry created")
end
end
end
end
a.push("(#{row["Booking Number"]})Direction And tour Created")
end
end
else
a.push("extras")
end
end
render json: a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment