def add_transfer(data): #print(data) pocket = Pocket.query.get(int(data['pocketid'])) category = Category.query.get(int(data['categoryid'])) category.last_active = datetime.utcnow() amount = category.type * abs(int(data['amount'])) detail = str(data['detail']) pocket.balance += amount actual = Abalance() actual._pocket = pocket actual.balance = pocket.balance actual.timestamp = datetime.utcnow() transfer = Transfer() transfer._pocket = pocket transfer.amount = amount transfer.timestamp = datetime.utcnow() transfer._category = category transfer.detail = detail db.session.add(transfer) db.session.add(actual) db.session.commit() return True
def transfer_funds(from_wallet, to_wallet, amount, comment): if from_wallet is not None: if from_wallet.balance < amount: raise BillingException('Not enough funds') if to_wallet is None: raise BillingException('Receiver cannot be null') if comment is None or comment == '': raise BillingException('Missing comment') try: amount = int(amount) except: raise BillingException('Incorrect transfer amount') if amount < 0: raise BillingException('Cannot transfer negative funds') try: with transaction.atomic(): transfer = Transfer() transfer.amount = amount transfer.comment = comment transfer.sender = from_wallet transfer.receiver = to_wallet transfer.save() if from_wallet is not None: from_wallet.balance -= amount from_wallet.save() to_wallet.balance += amount to_wallet.save() except IntegrityError as e: raise BillingException('Transaction integrity error: {}'.format(e)) return True