Skip to content

Instantly share code, notes, and snippets.

@bhfailor
Created March 22, 2014 03:43
Show Gist options
  • Save bhfailor/9700767 to your computer and use it in GitHub Desktop.
Save bhfailor/9700767 to your computer and use it in GitHub Desktop.
notion of code to split a transaction between subevents
ev = Event.find(22651)
regs = ev.event_registrations
ticks = ev.tickets
# hash with reg_id as key and array of subevent_id values
hsh = {}
ticks.each do |tick|
reg_id = tick.event_registration.id
hsh[reg_id] = {} if hsh[reg_id] == nil
if hsh[reg_id][:ri] == nil then hsh[reg_id][:ri] = [tick.race.id] else hsh[reg_id][:ri] << tick.race.id end
if hsh[reg_id][] == nil then hsh[reg_id] = [tick.race.id] else hsh[reg_id] << tick.race.id end
if hsh[reg_id] == nil then hsh[reg_id] = [tick.race.id] else hsh[reg_id] << tick.race.id end
# this block needs editing
hsh[reg_id][:ri] = tick.race.id
hsh[reg_id][:init_fee] = tick.race.initial_fee ; hsh[reg_id][:cprice] = tick.custom_price
hsh[reg_id][:ri] = tick.race.id
hsh[reg_id][:init_fee] = tick.race.initial_fee ; hsh[reg_id][:cprice] = tick.custom_price
if hsh[reg_id] == nil then hsh[reg_id] = [tick.race.id] else hsh[reg_id] << tick.race.id end
end
# split the registrations into 3 parts: status: complete, status: trash && refund_processed: false, and status: trash && refund_processed: true
# this ignores registrations that are "Expired" or have a status of nil, etc.
# it also ignores registrations that are "Trash" but refund_process is nil, for example
complete_regs = {}
trash_regs_unref = {}
trash_regs_ref = {}
regs.each do |reg|
if reg.status == "Complete"
complete_regs[reg.id] = hsh[reg.id]
elsif reg.status == "Trash"
if reg.refund_processed == false
trash_regs_unref[reg.id] = hsh[reg.id]
elsif reg.refund_processed == true
trash_regs_ref[reg.id] = hsh[reg.id]
end
end
end
# now look at the complete regs and divide up the fees between the races
rev_split = {subevents: {}, unrefunded: {}, refunded: {}}
# to avoid hitting the db, hash the regs and races by id
subev_hsh = {}
reg_hsh = {}
regs.each {|reg| reg_hsh[reg.id] = reg}
ev.races.each {|subev| subev_hsh[subev.id] = subev}
# add hashes to the subevents hash
subev_hsh.keys.each do |k|
rev_split[:subevents][k] = {}
rev_split[:subevents][k][:number_of_tickets] = 0
rev_split[:subevents][k][:name] = subev_hsh[k].name
rev_split[:subevents][k][:order] = subev_hsh[k].race_group_order
rev_split[:subevents][k][:revenue] = 0.0
end
complete_regs.each do |reg_id, race_ids|
tot_rev = reg_hsh[reg_id].cost
tot_norm = 0.0
race_ids.each do |ri|
puts "#{ri} -- #{subev_hsh[ri].initial_fee} -- #{}"
tot_norm += subev_hsh[ri].initial_fee unless subev_hsh[ri].initial_fee == nil
end
scl = tot_rev/tot_norm # now can go through and scale the revenue
puts "scl = #{scl} and tot entry revenue = #{tot_rev} "
race_ids.each do |ri|
rev_split[:subevents][ri][:number_of_tickets] += 1
rev_split[:subevents][ri][:revenue] += subev_hsh[ri].initial_fee*scl unless subev_hsh[ri].initial_fee
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment