def post(self, request): form = self.form(request.POST) try: User.objects.get(username=request.POST["username"]) return render( request, self.template, {"registration_error": "Username already taken.", "register_form": self.form()} ) except: if form.is_valid(): user = form.save() group = Group.objects.get(name="Investor") user.groups.add(group) transaction = Transaction( user=user, amount=100000, category="DEPOSIT", description="Welcome gift from Flex." ) transaction.save() return redirect("/login/") else: return render( request, self.template, { "registration_error": "Invalid input - Please populate all fields on the form.", "register_form": self.form(), }, )
def transfer_money(db: Session, *, source_wallet: Wallet, destination_wallet: Wallet, amount: Decimal) -> None: source_wallet.balance -= amount db.add(Transaction(wallet=source_wallet, amount=-float(amount))) destination_wallet.balance += amount db.add(Transaction(wallet=destination_wallet, amount=float(amount))) db.commit()
def post(self, request): serializer = TransactionSeraializer(data=request.data) print(request.data) print(serializer) if serializer.is_valid(): from_ac_num = serializer.validated_data.get("from_ac_num") to_ac_num = serializer.validated_data.get("to_ac_num") amount = serializer.validated_data.get("amount") debit_credit = serializer.validated_data.get("debit_credit") emails = serializer.validated_data.get('email') acc = Accounts.objects.get(ac_num=from_ac_num) if acc: transfer = Transaction(from_ac_num=acc, to_ac_num=to_ac_num, amount=amount, debit_credit=debit_credit) transfer.save() date = transfer.transaction_date if debit_credit == 'Debited': if acc.balance > amount: acc.balance -= amount acc.save() msg = f'Your a/c no.1000 has been credited for Rs.{amount}.00 ' \ f'on {date} towords beneficiary a/c no.{to_ac_num} and avail.bal INR {acc.balance}' send_mail('BankApp', msg, settings.EMAIL_HOST_USER, [emails], fail_silently=False) return Response({ "Hello user": str(amount) + ' has been debited and available balance:' + str(acc.balance) }) else: return Response('no available balance') elif debit_credit == 'Credited': acc.balance += amount acc.save() msg = f'Your a/c no.1000 has been credited for Rs.{amount}.00 ' \ f'on {date} towords beneficiary a/c no.{to_ac_num} and avail.bal INR {acc.balance}' send_mail('BankApp', msg, settings.EMAIL_HOST_USER, [emails], fail_silently=False) return Response({ "Hello user": str(amount) + ' has been credited and available balance:' + str(acc.balance) }) else: return Response('no accout') else: return Response('failed')
def deposit(): account = None prev_balance = None tr_amount = None if request.method == 'POST': acnt_id = request.form.get('acnt_id') tr_amount = request.form.get('tr_amount') if not tr_amount.isnumeric(): flash("Not a valid amount", "danger") else: account = db.session.query(Account).filter( Account.acnt_id == int(acnt_id)).first() prev_balance = account.acnt_balance account.acnt_balance += int(tr_amount) account.acnt_last_tr_date = datetime.now() account.acnt_message = '₹. {} Deposited'.format(tr_amount) trnsctn = Transaction(tr_amount=int(tr_amount), tr_type="Credit (Deposit)", tr_date=account.acnt_last_tr_date, tr_src=int(acnt_id), tr_trgt=-1) db.session.add(trnsctn) db.session.commit() flash("Amount deposited successfully", category="success") else: if current_user.role == 'customer': return redirect(url_for('error_403')) acnt_id = request.args.get('acnt_id') return render_template('deposit.html', page='account_details', tr_amount=tr_amount, acnt_id=acnt_id, prev_balance=prev_balance, account=account)
def create_account(): form = AccountForm() cust_ids = db.session.query(Customer.cust_id).all() if form.validate_on_submit(): cust_id = int(request.form.get('cust_id')) acnt_type = form.acnt_type.data deposit_amnt = form.deposit_amnt.data acnt_last_tr_date = datetime.now() acnt_status = 'Active' acnt_message = 'Account Created' account = Account(cust_id=cust_id, acnt_type=acnt_type, acnt_balance=deposit_amnt, acnt_last_tr_date=acnt_last_tr_date, acnt_status=acnt_status, acnt_message=acnt_message) db.session.add(account) db.session.flush() # In order to get the primary key ID trnsctn = Transaction(tr_amount=deposit_amnt, tr_type="Credit (A/C Opened)", tr_date=acnt_last_tr_date, tr_src=account.acnt_id, tr_trgt=-1) db.session.add(trnsctn) db.session.commit() flash('Account creation initiated successfull !.', category='info') return redirect(url_for('customer_home')) else: if current_user.role == 'teller': return redirect(url_for('error_403')) return render_template('create_account.html', page='create_account', form=form, cust_ids=cust_ids)
def create_users_and_sell_contracts(self): print("Creating users, giving them a deposit of $50,000 and selling each one contract...") for contract in self.contracts: username = fake.user_name() user = User(username=username) user.set_password("password") try: user.save() group = Group.objects.get(name='Investor') user.groups.add(group) except: user = User.objects.get(username=username) group = Group.objects.get(name='Investor') user.groups.add(group) deposit = Transaction(user = user, amount = 50000, category = 'DEPOSIT', description = "Deposit to account") deposit.save() time = timezone.now().replace(year = contract.bond.dated_date.year, month = contract.bond.dated_date.month, day = contract.bond.dated_date.day) contract_sale = Trade(buyer = user, seller = self.owner, contract = contract, price = 1, time = time) contract_sale.save() sale = Transaction(user = user, amount = -(contract_sale.price * contract_sale.contract.face), time = contract_sale.time, category = 'PURCHASE', description = "Initial purchase of one contract") sale.save() for cashflow in self.cashflows: interest_revenue = Transaction(user = user, amount = cashflow['amount'], time = cashflow['date'], category = "INTEREST", description = "Interest revenue on FLEX contract " + str(contract.id)) interest_revenue.save()
def transaction(): form = TransactionForm() if form.validate_on_submit(): recipient_user = User.query.filter_by( account_number=form.recipient.data).first() transaction = Transaction(sender=current_user.account_number, recipient=recipient_user.account_number, amount=float(form.amount.data)) # remove the amount from the senders checking and add it to the recipients checking acount current_user.checking -= float(form.amount.data) recipient_user.checking += float(form.amount.data) # Add the newly created transaction record to the database and commit the change in user amounts db.session.add(transaction) db.session.commit() flash( f"Successfully sent ${form.amount.data} to {form.recipient.data}!", "success") return redirect(url_for("home")) return render_template("transaction.html", form=form, title="Send Money")
def withdraw(): account = None prev_balance = None tr_amount = None if request.method == 'POST': acnt_id = request.form.get('acnt_id') tr_amount = request.form.get('tr_amount') if not tr_amount.isnumeric(): flash("Not a valid amount", "danger") else: account = db.session.query(Account).filter( Account.acnt_id == int(acnt_id)).first() prev_balance = account.acnt_balance if prev_balance < int(tr_amount): flash("Insufficient balance ! Choose a smaller amount", category="warning") else: account.acnt_balance -= int(tr_amount) account.acnt_last_tr_date = datetime.now() account.acnt_message = '₹. {} Withdrawn'.format(tr_amount) trnsctn = Transaction(tr_amount=int(tr_amount), tr_type="Debit (Withdraw)", tr_date=account.acnt_last_tr_date, tr_src=int(acnt_id), tr_trgt=-1) db.session.add(trnsctn) db.session.commit() flash("Amount withdrawn successfully", category="success") else: if current_user.role == 'customer': return redirect(url_for('error_403')) acnt_id = request.args.get('acnt_id') return render_template('withdraw.html', page='account_details', tr_amount=tr_amount, acnt_id=acnt_id, prev_balance=prev_balance, account=account)
def transfer(): tr_history = None if request.method == 'POST': cust_id = request.form.get('cust_id') src_acnt = request.form.get('src_acnt') trgt_acnt = request.form.get('trgt_acnt') tr_amount = request.form.get('tr_amount') if src_acnt == '-1': flash("Select source account to initiate transfer", category="danger") elif trgt_acnt == '-1': flash("Select target account to initiate transfer", category="danger") elif not tr_amount.isnumeric(): flash("Not a valid amount", category="danger") else: tr_amount = int(tr_amount) account_src = db.session.query(Account).filter( Account.acnt_id == int(src_acnt)).first() if tr_amount > account_src.acnt_balance: flash("Insufficient balance ! Choose a smaller amount", category="warning") else: account_src.acnt_balance -= tr_amount account_trgt = db.session.query(Account).filter( Account.acnt_id == int(trgt_acnt)).first() account_trgt.acnt_balance += tr_amount account_src.acnt_message = '-₹. {} Transfered'.format( tr_amount) account_trgt.acnt_message = '+₹. {} Transfered'.format( tr_amount) account_src.acnt_last_tr_date = datetime.now() account_trgt.acnt_last_tr_date = datetime.now() trnsctn = Transaction(tr_amount=int(tr_amount), tr_type="Debit (Transfer)", tr_date=account_src.acnt_last_tr_date, tr_src=int(src_acnt), tr_trgt=int(trgt_acnt)) db.session.add(trnsctn) db.session.commit() flash("Amount transfered successfully !", category="success") tr_history = {} tr_history['src'] = { 'acnt_id': src_acnt, 'prev_bal': account_src.acnt_balance + tr_amount, 'tr_amount': tr_amount, 'crnt_bal': account_src.acnt_balance } tr_history['trgt'] = { 'acnt_id': trgt_acnt, 'prev_bal': account_trgt.acnt_balance - tr_amount, 'tr_amount': tr_amount, 'crnt_bal': account_trgt.acnt_balance } if current_user.role == 'customer': return redirect(url_for('error_403')) cust_id = request.args.get('cust_id') src_account = db.session.query( Account.acnt_id, Account.acnt_type).filter(Account.cust_id == cust_id).all() src_acnt_ids = [acnt[0] for acnt in src_account] trgt_account = db.session.query(Account.acnt_id).filter( Account.acnt_id.notin_(src_acnt_ids)).all() return render_template('transfer.html', page='account_details', cust_id=cust_id, src_account=src_account, trgt_account=trgt_account, tr_history=tr_history)