def get_already_cashed_bonuses(self): """ Get already cashed bonuses return: cashed bonuses list """ filter_by = {'user': self.user, 'transaction_type': 'CASHIN'} return [c.bonus_cashed.id for c in Wallet.objects(**filter_by)]
def get_bonus_money(self): """ Gets the total bonus money in the wallet return: bonus money amount """ filter_by = {'user': self.user, 'currency': 'BNS', 'id__nin': self.get_already_cashed_bonuses()} return Wallet.objects(**filter_by).sum('value')
def count_wagered_money(self): """ Sum of the total wagered money since the last cash-in return: wagered money """ # get total wagered money filter_by = {'user': self.user, 'currency': 'EUR', 'transaction_type': 'BET'} bets = Wallet.objects(**filter_by).only('value') bets = sum([abs(b.value) for b in bets]) # get total cashed in bonus requirements filter_by = {'user': self.user, 'transaction_type': 'CASHIN', 'value__gt': 0} cashins = Wallet.objects(**filter_by) cashins = sum([c.bonus.value * c.bonus.wagering_requirement for c in cashins]) return bets - cashins
def cash_in(self): """ Convert to real money the bonuses that met the wagering req. """ # get casheable bonuses filter_by = {'user': self.user, 'transaction_type': 'BONUS', 'id__nin': self.get_already_cashed_bonuses()} bonus = Wallet.objects(**filter_by).first() if bonus: # get wagered money wg = self.count_wagered_money() # WR formula is met? if bonus.bonus.value * bonus.bonus.wagering_requirement <= wg: # how much money from that bonus is going to be cashed? bns = self.get_bonus_money() # for the case that te player earned more bns than the bonus cashin_amount = bns if bns <= bonus.bonus else bonus.bonus.value transaction = {'transaction_type': 'CASHIN', 'currency': 'EUR', 'value': cashin_amount, 'bonus': bonus.bonus, 'bonus_cashed': bonus.id, 'user': self.user} # save cashin transaction self.add_to_wallet(transaction) # adjust bns diff if there is one (this need to be improved) if cashin_amount < bonus.bonus.value: transaction = {'transaction_type': 'CASHIN', 'currency': 'BNS', 'value': bonus.bonus.value - cashin_amount, 'bonus': bonus.bonus, 'bonus_cashed': bonus.id, 'user': self.user} # save cashin transaction self.add_to_wallet(transaction) # check if there is an other casheable bonus self.cash_in()
def get_real_money(self): """ Gets the total bonus money in the wallet return: bonus money amount """ return Wallet.objects(user=self.user, currency='EUR').sum('value')