def addTransaction(self, trade, tradeCount, assetSymbol): asset = Asset.objects.get(symbol=assetSymbol) portfolioAssetMapping = PortfolioAssetMapping.objects.get(asset=asset) timestamp = "2016-01-06" initialCount = portfolioAssetMapping.currentCount finalCount = initialCount if trade == "buy": finalCount = initialCount + tradeCount if trade == "sell": finalCount = initialCount - tradeCount portfolioAssetMapping.currentCount = finalCount assetInfo = Share(assetSymbol) price = finalCount * float(assetInfo.get_price()) transaction = Transaction(mapping=portfolioAssetMapping, trade=trade, timestamp=timestamp, initialCount=initialCount, finalCount=finalCount, tradeCount=tradeCount, price=price) transaction.save() portfolioAssetMapping.save()
def form_valid(self, form): ret = super(TopUpFormView, self).form_valid(form) t = Transaction(user=form.cleaned_data['user'], amount=form.cleaned_data['amount']) t.save() messages.success( self.request, "{}€ have been added to your balance. You now have {}€ available". format(form.cleaned_data['amount'], form.cleaned_data['user'].profile.balance)) return ret
def post(self, request): user_profile = UserProfile.objects.get(badge=request.POST.get('badge')) user = user_profile.user item = Item.objects.get(code=request.POST.get('code')) sale = Sale(user=user) sale.save() sl = SaleLine(quantity=1, item=item, sale=sale) sl.save() t = Transaction(user=user, amount=-sale.total) t.save() return HttpResponse()
def complete_transaction(request): try: # print request.body # data = json.loads(request.body)['json'] if request.POST: consumer = request.POST.get('user_id') useracc = MainUser.objects.get(id=consumer) key = request.POST.get('key') prototype = Prototype.objects.get(key=key) type = request.POST.get('type') mode = request.POST.get('mode') ## type 1 is merchant and type 2 is personal and mode 2 is self if type == 1: products = Product.objects.filter(prototype=prototype) merchant = prototype.merchant total = Decimal(0) for p in products: total += p.quantity*p.price merchant.balance += total merchant.save() ts = Transaction() ts.prototype = prototype ts.customer = useracc ts.status = 1 ts.mode = mode ts.save() if mode == 2: useracc.balance -= total useracc.save() else: pt = PersonalTransfer.objects.filter(prototype=prototype) receiver = prototype.merchant sender = useracc total = Decimal(0) for p in pt: total += p.amount receiver.balance += total receiver.save() ts = Transaction() ts.prototype = prototype ts.customer = useracc ts.status = 1 ts.mode = mode ts.save() if mode == 2: useracc.balance -= total useracc.save() gcm_push_message("You've received funds in your paypaid account!","You have got funds!",receiver.gcm) except Exception,e: print e return HttpResponse("error",status=400)
def transfer(): data = request.json payer = get_user() if payer.is_organisation == True: return jsonify({ 'error': True, 'message': 'Organisations Cannot Donate, Please Create a Personal Account' }), 403 payer_bank = BankAccount.get(BankAccount.user == payer) user = User.get(uid=data["receiver_uid"]) receiver_bank = BankAccount.get(user=user) transaction = Transaction( payer_bank=payer_bank, receiver_bank=receiver_bank, amount=data["amount"], post_ident=Post.get(Post.uid == data["post_uid"])) transaction.save() payer_bank.balance = payer_bank.balance - data["amount"] receiver_bank.balance = receiver_bank.balance + data["amount"] payer_bank.save() receiver_bank.save() if payer.is_organisation: label = "income" trans = Transaction.select().where( Transaction.receiver_bank == receiver_bank) else: label = "expenses" trans = Transaction.select().where( Transaction.payer_bank == payer_bank) result = {} result["balance"] = payer_bank.balance trans_dicts = [] for t in trans: trans_dicts.append(t.to_dict) result[label] = trans_dicts return jsonify({"message": "Transaction Success", "payload": result})
def store_db_table(name): status = {} account = Account.query.filter_by(name=name).first_or_404() status['transactions_before'] = account.total_transactions_count dbtransactions = [ dbtransaction.to_dict() for dbtransaction in account.dbtransactions ] status['dbtransactions_to_be_added'] = len(dbtransactions) dbcnt = 0 for i in range(len(dbtransactions)): new_transaction = Transaction( date=dbtransactions[i]['date'].date(), amount=dbtransactions[i]['amount'], type=dbtransactions[i]['type'], description=dbtransactions[i]['description'] + " " + dbtransactions[i]['beneficiary'], category=" ", subcategory=" ", tag=" ", status=True, accountid=account.id, payee="not provided") try: new_transaction.save() DbTransaction.query.get(dbtransactions[i]['tid']).delete() dbcnt = dbcnt + 1 status['dbtransactions_added'] = dbcnt status['transactions_after'] = account.total_transactions_count except: current_app.error("Error occured while storing DB transaction") return render_template('upload.html', account=account, status=report) report = "Added %d DB Transactions to %d previous Transactions!" % ( status['dbtransactions_added'], status['transactions_before']) return render_template('upload.html', account=account, status=report)
def form_valid(self, form): ret = super(SaleFormView, self).form_valid(form) sl = SaleLine(quantity=1, item=form.cleaned_data['item'], sale=self.object) sl.save() t = Transaction(user=self.object.user, amount=-self.object.total) t.save() self.request.session['userid'] = self.object.user.pk if self.object.user.profile.balance >= 0: messages.success( self.request, "{0}€ have been substracted from your balance. You now have {1}€ available" .format(self.object.total, self.object.user.profile.balance)) else: messages.warning( self.request, "{0}€ have been substracted from your balance. Your balance is negative! ({1}€)" .format(self.object.total, self.object.user.profile.balance)) return ret
def account(name): account = Account.query.filter_by(name=name).first_or_404() # get transaction types ttypechoices = [ (ttp.id, ttp.ttype) for ttp in TransactionType.query.order_by(TransactionType.ttype).all() ] catchoices = [(cat.id, cat.category) for cat in CategorySettings.query.filter_by( account_id=account.id).all()] # user has not yet created any settings for categories if catchoices == []: catchoices = [ (cat.id, cat.cattype) for cat in CategoryType.query.order_by(CategoryType.cattype).all() ] subcatchoices = [(subcat.id, subcat.subcategory) for subcat in SubCategorySettings.query.filter_by( account_id=account.id).all()] # user has not yet created any settings for subcategories if subcatchoices == []: subcatchoices = [(subcat.id, subcat.subcattype) for subcat in SubCategoryType.query.order_by( SubCategoryType.subcattype).all()] form = TransactionForm() form.type.choices = ttypechoices form.category.choices = catchoices form.subcategory.choices = subcatchoices if form.validate_on_submit() and form.submit.data: current_app.logger.info('Creating new transaction') try: ttype_name = TransactionType.query.filter_by( id=form.type.data).first().ttype cattype_name = [ cattype for catid, cattype in catchoices if catid == form.category.data ][0] subcattype_name = [ subcattype for subcatid, subcattype in subcatchoices if subcatid == form.subcategory.data ][0] new_transaction = Transaction(date=form.date.data, amount=form.amount.data, type=ttype_name, description=form.description.data, category=cattype_name, subcategory=subcattype_name, tag=form.tag.data, status=form.status.data, accountid=account.id, payee=form.payee.data) new_transaction.save() return redirect(url_for("main.account", name=name)) #check except: current_app.logger.info('Transaction form could not be completed') else: current_app.logger.info('Transaction form could not be validated') return render_template('transaction_overview.html', account=account, form=form)