def commit(self): ''' Perform the transaction. If debit fails (e.g. not enough balance), transaction fails ''' try: self.__debit_account.debit(self.__amount) self.__credit_account.credit(self.__amount) except InsufficientFunds: msg = "Transaction failed. Not sufficient funds on debit account." detail_msg = "ID: %d - Debit account: %s - Credit account: %s - Amount %d" %(\ self.__id, self.__debit_account.get_account_holder(),\ self.__credit_account.get_account_holder(), self.__amount) logger.warn(msg) logger.debug(detail_msg) raise TransactionFailed(msg)
def apply(self, trust_account, target_account, data_accessor, date): #get the current balance current_balance = target_account.get_balance() if (current_balance == 0): logger.info("Policy cannot be applied: current balance is 0.") return 0; # get the average balance average_balance = target_account.account_history.get_average_balance() logger.info("Average balance for account %s: %d", target_account.get_account_holder(), average_balance) #get all transactions for this account in the observation period transactions = data_accessor.\ get_transactions_in_period(self.observation_start, self.observation_end, target_account) #how much will be adjusted amount = average_balance * self.adjustment_rate #quids_level is just a variable to store the return value from this #method, and could be negative or positive. quids_level = amount #the sum of all transaction amounts in the observation period total_transaction_amounts_in_period = 0 #no transactions have been found if (transactions is not None): logger.debug("Transactions are available.") for i in transactions: total_transaction_amounts_in_period += i.get_amount() logger.debug("Total transactions in period for %s: %d", target_account.get_account_holder(), total_transaction_amounts_in_period) else: logger.warn("Transaction are None!") #if this rule applies, quids will be detracted if ((total_transaction_amounts_in_period < average_balance) and amount >0) : #we cannot detract more than there is on the current balance if ( current_balance < amount): amount = current_balance #create the transaction and commit ta = Transaction(trust_account, target_account, amount, date) ta.commit() logger.info("Policy applied. Removed %d quids from user account %s.", amount, target_account.get_account_holder()) quids_level = -1*amount #so actually we have a negative amount #if this rule applies, there's nothing to do (this is arbitrary and #may be total nonsense) elif ( (total_transaction_amounts_in_period == average_balance) or (amount == 0) ): logger.info("Policy applied: No changes necessary.") quids_level = 0 #if this rule applies, quies will be injected else: # (total_transaction_amounts_in_period > average_balance): #first the trust needs to CREATE the quids in wants to inject trust_account.credit(amount) #then create the transaction and commit ta = Transaction(target_account, trust_account, amount, date) ta.commit() logger.info("Policy applied. Injected %d quids to user account %s.", amount, target_account.get_account_holder()) return quids_level
def __init__(self, holder): self.__accountnumber = self.__generate_account_number() self.__accountholder = holder self.__balance = 0 self.account_history = AccountHistory(90) logger.debug("Created account number %d for %s ", self.__accountnumber, holder)