Skip to content

Instantly share code, notes, and snippets.

@pyrat
Created May 21, 2009 09:52
Show Gist options
  • Save pyrat/115377 to your computer and use it in GitHub Desktop.
Save pyrat/115377 to your computer and use it in GitHub Desktop.
namespace :synchronize do
desc "Synchronizing EFP and Edflats databases"
task(:synchronize => :environment) do
props = Property.find(:all)
@bookings = Array.new
if props.length > 0
props.each do |prop|
enquiries = Enquiry.find(:all, :conditions=>["property_id=? AND status='BOOKING_CONFIRMED' AND ISNULL(synced_at)", prop.id])
enquiries.each do |enq|
@bookings.push(enq.booking)
end
end
end
if @bookings.length > 0
@bookings.each do |book|
book_ref = book.reference.slice(0,10)
booked = Guest.find(:all, :conditions=>["Booking_Ref=?", book_ref])
enquiry = Enquiry.find(book.enquiry_id)
if booked.length == 0
property = Property.find(:first, :conditions=>["id=?", enquiry.property_id])
if !property.nil?
if !property.web_property?
ActiveRecord::Base.establish_connection(:efp)
Eprop = Property.find(:first, :conditions=>["Prop=?", book_ref.slice(0,3)])
if !Eprop.nil?
address = Address.find(:first, :conditions=>["enquiry_id=?", enquiry.id])
guest = Guest.new
income = LogIncome.new
guest.Taken_on = book.updated_at.strftime("%Y-%m-%d")
income.Received_on = book.updated_at.strftime("%Y-%m-%d")
guest.Booking_Ref = book_ref
guest.Prop = Eprop.id
guest.Nights = enquiry.nights
guest.People = enquiry.people
source = Agent.find_by_name("EFP")
guest.Source = source.id
guest.Charge = enquiry.cost.to_i
transaction = BookingTransaction.find(:first, :conditions=>["booking_id=?", book.id])
income.Banked = transaction.amount.to_i
income.Transaction_ID = transaction.confirmation.to_s
income.Cart_ID = book.reference
income.CC_chrg = Conf.val('booking_fee')
income.Net_Amt = transaction.amount.to_i - income.CC_chrg.to_i
rpaid = RPaidBy.find_by_name("Worldpay")
income.HowEFP_paid = rpaid.id
income.Booking_Ref = book_ref
income.Prop_LL = Eprop.id
paidin = Person.find_by_name("Guest")
income.Paidinby = paidin.id
inc_type = IncomeType.find_by_name("Deposit")
income.Income = inc_type.id
guest.Damage=Eprop.Prop_DD.to_i
guest.Agent_fee=0
guest.DuetoEFP=enquiry.cost.to_i+Eprop.Prop_DD.to_i
guest.Name = enquiry.name
guest.email = enquiry.email
guest.Home_Phone = enquiry.phone
if !address.nil?
guest.Home_Add1 = address.address1.to_s+' '+address.address2.to_s
guest.Home_Add2 = address.postcode.to_s
guest.Home_Add3 = address.city.to_s
guest.Home_Add4 = address.country.to_s
end
guest.Arrive_D = enquiry.arrival_date
guest.Depart_D = enquiry.arrival_date.advance(:days => enquiry.nights)
guest.Flat_Config = ''
guest.Meet_at = enquiry.arrival_date.to_s+' '+enquiry.arrival_time.to_s
guest.Create_D = Date.today.strftime("%Y-%m-%d")
guest.save!
income.Bank_Stmt = 0
income.Ledger = 0
income.Append_toR = 0
income.save!
enquiry.synced_at = Time.now
enquiry.save!
end
else
#income entry now
income = LogIncome.new
income.Received_on = book.updated_at.strftime("%Y-%m-%d")
transaction = BookingTransaction.find(:first, :conditions=>["booking_id=?", book.id])
income.Banked = transaction.amount.to_i
income.Transaction_ID = transaction.confirmation.to_s
income.Cart_ID = book.reference
income.CC_chrg = Conf.val('booking_fee')
income.Net_Amt = transaction.amount.to_i - income.CC_chrg.to_i
rpaid = RPaidBy.find_by_name("Worldpay")
income.HowEFP_paid = rpaid.id
income.Booking_Ref = ''
income.Prop_LL = 0
Person.establish_connection(:efp)
paidin = Person.find_by_name("Guest")
income.Paidinby = paidin.id
inc_type = IncomeType.find_by_name("Web booking")
income.Income = inc_type.id
income.Bank_Stmt = 0
income.Ledger = 0
income.Append_toR = 0
income.save!
enquiry.synced_at = Time.now
enquiry.save!
end
end
else
enquiry.synced_at = Time.now
enquiry.save!
end
end
end
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment