Created
August 22, 2009 02:30
-
-
Save sprite2005/172594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_phone_transaction | |
# Billing Interval [seconds] | |
billing_interval = AppConfig.billing_interval.to_i | |
actual_billable_time = ((transaction_object.billable_time + billing_interval) / billing_interval) * billing_interval | |
if time_left? < 0 | |
# Rounds billable_time down to actual billed_time to avoid inconsitencies when user getting charged for being a few seconds over call time when going to refill menu and vendor disconnects | |
if transaction_object.billed_time != transaction_object.billable_time | |
transaction_object.billable_time = transaction_object.billed_time | |
transaction_object.save | |
end | |
return self | |
end | |
# We need to update the billing | |
if transaction_object.billable_time > transaction_object.billed_time | |
# Update from the transaction object | |
@vendor = receiver_user_transactions.first.user | |
@client = sender_user_transactions.first.user | |
# Amount client spends in cents | |
client_amount = (transaction_object.rate_cents * actual_billable_time * -1 / 60).to_i | |
client_phone_fee = (transaction_object.client_connection_charge_cents * actual_billable_time * -1 / 60).to_i | |
# Amount vendor receives | |
vendor_amount = ((AppConfig.vendor_payout * actual_billable_time * transaction_object.rate_cents) / 60).to_i | |
vendor_phone_fee = (transaction_object.vendor_connection_charge_cents * actual_billable_time * -1 / 60).to_i | |
# Vendor side referals | |
tier1_amount = (vendor_amount * AppConfig.tier1_payout).to_i | |
tier2_amount = (vendor_amount * AppConfig.tier2_payout).to_i | |
total_payout = (vendor_amount + tier1_amount * tier1_user_transactions.count + tier2_amount * tier2_user_transactions.count).to_i | |
system_amount = ((client_amount * -1) - total_payout).to_i | |
Transaction.transaction do | |
rt = receiver_user_transactions.first | |
rt.amount = Money.new(vendor_amount, transaction_object.currency) | |
rt.save! | |
st = sender_user_transactions.first | |
st.amount = Money.new(client_amount, transaction_object.currency) | |
st.save! | |
# Phone Billing | |
# Vendor Amount | |
rupf = receiver_user_phone_fee_transactions.first | |
rupf.amount = Money.new(vendor_phone_fee, transaction_object.currency) | |
rupf.save! | |
# Client Amount | |
supf = sender_user_phone_fee_transactions.first | |
supf.amount = Money.new(client_phone_fee, transaction_object.currency) | |
supf.save! | |
# System Amount | |
spf = system_phone_fee_transactions.first | |
spf.amount = Money.new((client_phone_fee + vendor_phone_fee) * -1, transaction_object.currency) | |
spf.save! | |
# End of Phone BIlling | |
tier1_user_transactions.each do |ut| | |
ut.amount = Money.new(tier1_amount, transaction_object.currency) | |
ut.save! | |
end | |
tier2_user_transactions.each do |ut| | |
ut.amount = Money.new(tier2_amount, transaction_object.currency) | |
ut.save! | |
end | |
system = system_user_transactions.first | |
system.amount = Money.new(system_amount, transaction_object.currency) | |
system.save! | |
transaction_object.billed_time = actual_billable_time | |
transaction_object.save! | |
save! | |
end | |
user_transactions.each do |ut| | |
ut.user.update_balance | |
end | |
end | |
self | |
end | |
def time_left? | |
# Takes the amount of minutes you can afford rounding to nearest billing interval, then adds on remaining time from previous purchased billing interval... | |
# Yes... it is extremely confusing but it should work | |
billing_interval = AppConfig.billing_interval.to_i | |
((sender_user_transactions.first.user.account_balance_cents.to_f / (transaction_object.rate_cents.to_f + transaction_object.client_connection_charge_cents)* 60).to_i / billing_interval * billing_interval) + (transaction_object.billed_time - transaction_object.billable_time) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment