def filter_by_active_package(member): # print member, type(member) if type(member) == Members: child_id = member.child_id elif type(member) == User: child_id = member.id return get_package(child_id)
def wrapped_f(user, last_date, next_date, dry, date): res = func(user, last_date, next_date, dry=dry, date=date) calc, _ = res binary_payout, l_cf, r_cf = calc if not dry and date: # print "generating binary transaction" pkg = get_package(user) user_BN_wallet = Wallet.objects.filter(owner=user, wallet_type='BN').first() avicrypto_user = User.objects.get( username='******', email='*****@*****.**') avicrypto_wallet = Wallet.objects.filter(owner=avicrypto_user, wallet_type='AW').first() bn_status = 'C' if pkg.binary_enable else 'P' bn_txn = Transactions.objects.create( sender_wallet=avicrypto_wallet, reciever_wallet=user_BN_wallet, amount=binary_payout, tx_type="binary", status=bn_status) bn_txn.created_at = date bn_txn.save(update_fields=['created_at']) print "This Binary Txn type is.." pdb.pprint.pprint(bn_txn.status) # assert Transactions.objects.all() # print "transaction generated" return res
def calc_weekly(user, last_date, next_date, dry=True): # calculate number of weeks passed since last_date before next_date # print "for user %s" %(user.username) pkg = get_package(user) user_doj = pkg.created_at.date() num_weeks = 0 # user_doj = user.date_joined.date() # user_doj = date(user_doj.year, user_doj.month, user_doj.day) # old_date_time = pkg.created_at if pkg.created_at > last_date else last_date old_date_time = greater_date(pkg.created_at, last_date) new_date = next_date.date() old_date = old_date_time.date() if old_date_time < next_date: delta = next_date - old_date_time num_weeks = floor(delta.days / 7.0) # pkg = get_package(user) # payout = (pkg.package.payout/100.) * pkg.package.price # res = (round((payout * num_weeks), 2), 'direct') res = payout, _ = calc_daily(user, old_date_time, next_date) else: res = payout, _ = (0.0, 'direct') if num_weeks and dry == False: divide_conquer( range(int(num_weeks)), 0, int(num_weeks) - 1, lambda num: gen_txn_weekly(num, old_date, new_date, user, payout)) return res
def calc_daily(user, last_date, next_date): pkg = get_package(user) active_date = pkg.created_at.date() # last_dt = greater_date(active_date, date(last_date.year, last_date.month, last_date.day)) if last_date < next_date: delta = next_date - last_date days = floor(delta.days) final_amt = round( (pkg.package.payout / 100.0) * pkg.package.price * days / 7.0, 2) return (final_amt, 'direct') return (0.0, 'direct')
def wrapped_f(*args, **kw): """ Checks if user has active package else returns end state for relevant investment """ print "inside is_eligible. calling %s"%func.__name__ user, last_date, next_date = args[:3] pkg = get_package(user) if pkg: # print "calling %s with attr: %s, %s, %s" %(func.__name__, user.username, last_date, next_date) return func(*args, **kw) else: return ((0.0, 0.0, 0.0), 'end') if 'binary' in func.__name__ else (0.0, 'end')
def calc_direct(user, last_date, next_date, dry=True): """calculate the direct: - For the cycle with time T for all T i.e. last_date <= T < next_date - filter users with (doj = T) and with active package - calculate their package price - sum * direct_payout % """ pkg = get_package(user) direct_payout = pkg.package.directout l_sum = calc_leg(user, last_date, next_date, leg='l', dry=dry) r_sum = calc_leg(user, last_date, next_date, leg='r', dry=dry) final_amt = round((l_sum + r_sum) * direct_payout / 100.0, 2) return (final_amt, 'binary')
def wrapped_f(sponsor_id, member, last_date, next_date, dry): # put on func = valid_payout_user res = func(sponsor_id, member, last_date, next_date, dry=dry) if res and dry == False: # print "generating direct transaction. sponsor_id is %s"%sponsor_id p = Profile.objects.get(user_auto_id=sponsor_id) sponsor_user = p.user doj = UTC.normalize(member.child_id.date_joined) pkg = get_package(sponsor_user) assert type(member.child_id) == User child_pkg = get_package(member.child_id) # print "child_pkg is ", child_pkg, member.child_id.username, dr = (pkg.package.directout / 100.0) * child_pkg.package.price user_DR_wallet = Wallet.objects.filter(owner=sponsor_user, wallet_type='DR').first() avicrypto_user = User.objects.get( username='******', email='*****@*****.**') avicrypto_wallet = Wallet.objects.filter(owner=avicrypto_user, wallet_type='AW').first() dr_txn = Transactions.objects.create( sender_wallet=avicrypto_wallet, reciever_wallet=user_DR_wallet, amount=dr, tx_type="direct", status="C") dr_txn.created_at = child_pkg.package.created_at dr_txn.save(update_fields=['created_at']) # assert Transactions.objects.all() # print "Txn asserted" # calc_binary(sponsor_user, last_date, next_date, dry=False, # date=child_pkg.package.created_at) return res
def valid_payout_user(sponsor_id, member, last_date, next_date, dry=True): """Filter users that have their Date of Joining between last payout and next payout day - params: dry: For dry run, no side-effect function if True. If False, generates a Direct Type Transaction. Defaults to True. """ doj = UTC.normalize(member.child_id.date_joined) # check if member is active pkg = get_package(member.child_id) # check if member falls within this cycle. # check if is a direct sponsor try: return (last_date <= doj < next_date) and (member.child_id.profile.sponser_id.profile.user_auto_id == sponsor_id) and pkg except: return False
def valid_payout_user_direct(sponsor_id, m, last_date, next_date, sponsor_pkg): doj = UTC.normalize(member.child_id.date_joined) # check if member is active pkg = get_package(member.child_id) # check if member falls within this cycle. # check if is a direct sponsor try: if pkg: return (last_date <= doj < next_date) and (member.child_id.profile.sponser_id.profile.user_auto_id == sponsor_id) and pkg >= sponsor_pkg return False except Exception as e: print e.message return False
def calc_aggregate_right(user, sponsor_user, last_date, next_date): """Find the aggregate sum of all packages in right leg""" if user: right_user = get_right(user) pkg = get_package(user) if pkg: price = pkg.package.price if is_sponsored_by(user, sponsor_user) else 0 return price + calc_aggregate_left( right_user, sponsor_user, last_date, next_date) + calc_aggregate_right( right_user, sponsor_user, last_date, next_date) return 0.0 return 0.0
def wrapped_f(user, last_date, next_date): """ Checks if user falls within valid date range else bypasses to its children 0 """ if user: pkg = get_package(user) if pkg: doj = UTC.normalize(pkg.created_at) # doj = UTC.normalize(user.date_joined) if last_date <= doj < next_date: #print "in if is valid_date " #print "func name is {}".format(func.__name__) return func(user, last_date, next_date) elif func.__name__ == "get_left_right_agg": return (0.0, 0.0) # elif doj == next_date: # return 0.0 # else: # print "in else is valid_date " # print "last_date <= doj < next_date is: {} <= {} < {}".format(last_date, doj, next_date) # print "user valid date check", user.username # return get_left_right_agg(user, last_date, next_date) return 0.0
def binary_wet(user, last_date, next_date): """ - Do recursive Traverse left and right - Find if last_date <= Pkg activation date falls < next_date - If so, calc_binary - Once exhausted, exit """ # calculate if binary has atleast one direct pair # It can happen that binary gets calculated and yet there is not direct pair. # In that case binary gets set to txn type='P' viz Pending pairs = get_direct_pair(user, last_date, next_date) pkg = get_package(user) if pairs: pkg.binary_enable = True pkg.save() assert pkg.binary_enable assert pairs # switch all Pending Transactions to Confirmed status user_BN_wallet = Wallet.objects.filter(owner=user, wallet_type='BN').first() avicrypto_user = User.objects.get(username='******', email='*****@*****.**') avicrypto_wallet = Wallet.objects.filter(owner=avicrypto_user, wallet_type='AW').first() bn_status = 'C' if pkg.binary_enable else 'P' # set txns's status to 'Confirmed' from Pending Transactions.objects.filter(sender_wallet=avicrypto_wallet, reciever_wallet=user_BN_wallet, tx_type="binary", status='P').update(status=bn_status) sponsor_id = user.profile.user_auto_id return rec_binary(user, last_date, next_date, user, sponsor_id)
def rec_binary(user, last_date, next_date, sponsor_user, sponsor_id): if not user: return l_user = get_left(user) r_user = get_right(user) members = Members.objects.filter(parent_id=user.id) # filter paying sponsored members by left and right branch filtered_members = filter_by_sponsor(sponsor_id, last_date, next_date, members, dry=True) [ calc_binary(sponsor_user, last_date, next_date, dry=False, date=getattr(get_package(m.child_id), 'created_at', None)) for m in filtered_members ] rec_binary(l_user, last_date, next_date, sponsor_user, sponsor_id) rec_binary(r_user, last_date, next_date, sponsor_user, sponsor_id) return [], 'end'
def filter_by_sponsor_direct(sponsor_id, last_date, next_date, members, dry=True): sponsor_profile = Profile.objects.get(user_auto_id=sponsor_id) sponsor_pkg = get_package(sponsor_profile.user) return [m for m in members if valid_payout_user_direct(sponsor_id, m, last_date, next_date, sponsor_pkg)]