Exemple #1
0
def transfer(
        request, group, account=None, transfer_type=None, is_admin=False,
        is_owner=False):
    """Deposit, withdraw or transfer money"""

    if request.method == 'POST':
        data = request.POST
    else:
        data = None

    if transfer_type == 'transfer':
        title = _('Transfer from account')
        form = TransferForm(data, account=account)
    elif transfer_type == 'deposit':
        title = _('Deposit to account')
        form = DepositWithdrawForm(data)
    elif transfer_type == 'withdraw':
        title = _('Withdrawal from account')
        form = DepositWithdrawForm(data)
    else:
        return HttpResponseForbidden(_('Forbidden if not group admin.'))

    if request.method == 'POST' and form.is_valid():
        amount = form.cleaned_data['amount']
        details = form.cleaned_data['details'].strip()

        if details == '':
            details = None

        bank_account = group.roleaccount_set.get(
            role=RoleAccount.BANK_ACCOUNT).account

        transaction = Transaction(group=group)
        # FIXME: save() shouldn't be need if we figure out a reasonable hack
        transaction.save()

        if transfer_type == 'deposit':
            # Deposit to user account

            transaction.entry_set.add(
                TransactionEntry(account=account, credit=amount))
            transaction.entry_set.add(
                TransactionEntry(account=bank_account, debit=amount))

            transaction.set_pending(user=request.user, message=details)

        elif transfer_type == 'withdraw':
            # Withdraw from user account

            transaction.entry_set.add(
                TransactionEntry(account=account, debit=amount))
            transaction.entry_set.add(
                TransactionEntry(account=bank_account, credit=amount))

            transaction.set_pending(user=request.user, message=details)

        elif transfer_type == 'transfer':
            # Transfer from user account to other user account

            credit_account = Account.objects.get(
                id=form.cleaned_data['credit_account'])

            transaction.entry_set.add(
                TransactionEntry(account=account, debit=amount))
            transaction.entry_set.add(
                TransactionEntry(account=credit_account, credit=amount))

            transaction.set_pending(user=request.user, message=details)

            if amount <= account.normal_balance() - (group.block_limit or 0):
                transaction.set_committed(user=request.user)
            else:
                messages.info(request, _(
                    'Your transaction has been added, '
                    'but your group admin has to commit it.'))

        else:
            return HttpResponseForbidden(_('Forbidden if not group admin.'))

        messages.success(request, _('Added transaction: %s') % transaction)

        return HttpResponseRedirect(reverse(
            'account-summary', args=[account.group.slug, account.slug]))

    return render_to_response('accounting/transfer.html', {
        'is_admin': is_admin,
        'is_owner': is_owner,
        'account': account,
        'type': transfer_type,
        'title': title,
        'form': form,
        'group': group,
    }, context_instance=RequestContext(request))
Exemple #2
0
def transfer(
    request,
    group,
    account=None,
    transfer_type=None,
    is_admin=False,
    is_owner=False,
):
    """Deposit, withdraw or transfer money"""

    if request.method == "POST":
        data = request.POST
    else:
        data = None

    if transfer_type == "transfer":
        title = _("Transfer from account")
        form = TransferForm(data, account=account)
    elif transfer_type == "deposit":
        title = _("Deposit to account")
        form = DepositWithdrawForm(data)
    elif transfer_type == "withdraw":
        title = _("Withdrawal from account")
        form = DepositWithdrawForm(data)
    else:
        return HttpResponseForbidden(_("Forbidden if not group admin."))

    if request.method == "POST" and form.is_valid():
        amount = form.cleaned_data["amount"]
        details = form.cleaned_data["details"].strip()

        if details == "":
            details = None

        bank_account = group.roleaccount_set.get(
            role=RoleAccount.BANK_ACCOUNT).account

        transaction = Transaction(group=group)
        # FIXME: save() shouldn't be need if we figure out a reasonable hack
        transaction.save()

        if transfer_type == "deposit":
            # Deposit to user account
            transaction.entry_set.create(account=account, credit=amount)
            transaction.entry_set.create(account=bank_account, debit=amount)
            transaction.set_pending(user=request.user, message=details)

        elif transfer_type == "withdraw":
            # Withdraw from user account
            transaction.entry_set.create(account=account, debit=amount)
            transaction.entry_set.create(account=bank_account, credit=amount)
            transaction.set_pending(user=request.user, message=details)

        elif transfer_type == "transfer":
            # Transfer from user account to other user account
            credit_account = Account.objects.get(
                id=form.cleaned_data["credit_account"])
            transaction.entry_set.create(account=account, debit=amount)
            transaction.entry_set.create(account=credit_account, credit=amount)
            transaction.set_pending(user=request.user, message=details)

            if amount <= account.normal_balance() - (group.block_limit or 0):
                transaction.set_committed(user=request.user)
            else:
                messages.info(
                    request,
                    _("Your transaction has been added, "
                      "but your group admin has to commit it."),
                )

        else:
            return HttpResponseForbidden(_("Forbidden if not group admin."))

        messages.success(request, _("Added transaction: %s") % transaction)

        return HttpResponseRedirect(
            reverse("account-summary", args=[account.group.slug,
                                             account.slug]))

    return render(
        request,
        "accounting/transfer.html",
        {
            "is_admin": is_admin,
            "is_owner": is_owner,
            "account": account,
            "type": transfer_type,
            "title": title,
            "form": form,
            "group": group,
        },
    )