def transaction_add(request,firm_id,ledger_id): if request.method == 'POST': amount = request.POST['amount'] v_no = request.POST['voucher'] description = request.POST['description'] type = request.POST['type'] date = request.POST['date'] voucher = Transaction.objects.filter(ledger__firm__id=int(firm_id),voucher__voucher_no=int(v_no)) if len(voucher) > 0 : amount = 0.0 type = 'Credit' firm = Firm.objects.get(id=int(firm_id)) firm_id = int(firm_id) ledger = Ledger.objects.get(id=int(ledger_id)) transactions = Transaction.objects.filter(ledger__firm_id=firm_id, ledger__name=ledger.name) transactions = sorted(transactions, key=lambda x: x.created,reverse=True) for transaction in transactions: if transaction.type == 'Credit': amount += transaction.amount else: amount -= transaction.amount if amount < 0.0: amount = float(0 - amount) type = 'Debit' return render(request, 'transaction/ledger_details.html', {'transactions': transactions, 'name': firm.name, 'year': firm.year,'id':firm.id, 'error':'Voucher number already exists', 'ledger': ledger, 'amount': amount, 'type': type }) ledger = Ledger.objects.get(id=int(ledger_id)) transaction = Transaction() transaction.ledger_id = ledger.id voucher = Voucher() voucher.voucher_no = v_no voucher.save() transaction.voucher_id = voucher.id transaction.amount = float(amount) transaction.voucher_type = type transaction.description = description transaction.save() if type=='Impress' or type == 'Expense': transaction.type = 'Credit' cash_ledger = Ledger.objects.filter(name="Cash",firm_id=int(firm_id))[0] cash = Transaction() voucher = Voucher() voucher.voucher_no = -1 voucher.save() cash.voucher_id = voucher.id cash.ledger_id = cash_ledger.id cash.amount = float(amount) cash.type = 'Debit' cash.save() else: transaction.type = 'Debit' cash_ledger = Ledger.objects.filter(name="Cash", firm_id=int(firm_id))[0] cash = Transaction() voucher = Voucher() voucher.voucher_no = -1 voucher.save() cash.voucher_id = voucher.id cash.ledger_id = cash_ledger.id cash.amount = float(amount) cash.type = 'Credit' cash.save() if date != '': transaction.created = date transaction.save() amount = 0.0 type = 'Credit' firm = Firm.objects.get(id=int(firm_id)) firm_id = int(firm_id) ledger = Ledger.objects.get(id=int(ledger_id)) transactions = Transaction.objects.filter(ledger__firm__id=firm_id, ledger__name=ledger.name) transactions = sorted(transactions, key=lambda x: x.created,reverse=True) for transaction in transactions: if transaction.type == 'Credit': amount += transaction.amount else: amount -= transaction.amount if amount < 0.0: amount = float(0 - amount) type = 'Debit' return render(request, 'transaction/ledger_details.html', {'transactions': transactions, 'name': firm.name, 'year': firm.year, 'id': firm.id, 'ledger': ledger, 'amount': amount, 'type': type, 'error':"Success" }) else: url = "/home/" + str(firm_id) + "/ledger_home" return redirect(url)
def add_journal(request,firm_id): if request.method == 'POST': ledger_main = request.POST['ledger_main'] try: ledger = Ledger.objects.filter(firm_id=int(firm_id),name=ledger_main)[0] except Exception as e: print(e) return render(request, 'transaction/add_journal.html', {'id': int(firm_id), 'message': 'Main Ledger does not exist'}) description_main = request.POST['description_main'] type_main = request.POST['type_main'] names = request.POST.getlist('name') vouchers = request.POST.getlist('voucher') dates = request.POST.getlist('date') del vouchers[len(names)-1] amounts = request.POST.getlist('amount') descriptions = request.POST.getlist('description') transactions = Transaction.objects.filter(ledger__firm__id=int(firm_id),voucher__voucher_no__in=vouchers) if len(transactions) > 0 : return render(request, 'transaction/add_journal.html', {'id': int(firm_id), 'message': 'A Voucher Number already exists'}) for i in range(len(names)-1): #check if all ledgers are valid try: ledger = Ledger.objects.filter(firm_id=int(firm_id),name=names[i])[0] except Exception as e: print(e) return render(request,'transaction/add_journal.html', {'id': int(firm_id),'message':'Ledger does not exist'}) print('ready to save journal entry') amount = 0.0 type = 'Credit' if type_main != 'Debit' : type = 'Debit' for i in range(len(vouchers)): amount += float(amounts[i]) ledger = Ledger.objects.filter(firm_id=int(firm_id),name=names[i])[0] transaction = Transaction() transaction.ledger_id = ledger.id transaction.type = type transaction.description = descriptions[i] voucher = Voucher() voucher.voucher_no = vouchers[i] voucher.save() transaction.voucher_id = voucher.id transaction.amount = amounts[i] transaction.save() if dates[i] != '': transaction.created = dates[i] transaction.save() ledger = Ledger.objects.filter(firm_id=int(firm_id), name=ledger_main)[0] transaction = Transaction() transaction.ledger_id = ledger.id transaction.type = type_main transaction.description = description_main voucher = Voucher() voucher.voucher_no = -1 voucher.save() transaction.voucher_id = voucher.id transaction.amount = amount transaction.save() return render(request, 'transaction/add_journal.html', {'id': int(firm_id), 'message': 'Success. Transactions have been saved'}) else: if request.user.is_authenticated(): return render(request,'transaction/add_journal.html',{'id':int(firm_id)}) else: return redirect('/login')