mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-20 23:51:47 +01:00
Splits a transaction into named parts whose amounts add up to its amount, e.g. the items of a big purchase or the fees inside an investment contribution. Parts can be set on transactions, installment plans, recurring and quick transactions, and are copied to the transactions they generate. Transfers can't have parts. Transactions show their parts under a tab on the card and in the investment detail. The content filter matches part names, and bulk edits leave the amount of split transactions alone.
230 lines
7.6 KiB
Python
230 lines
7.6 KiB
Python
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.db import transaction as db_transaction
|
|
from django.http import Http404, HttpResponse
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
from apps.common.decorators.htmx import only_htmx
|
|
from apps.transactions.forms import (
|
|
InstallmentPlanForm,
|
|
QuickTransactionForm,
|
|
RecurringTransactionForm,
|
|
TransferForm,
|
|
)
|
|
from apps.transactions.models import Transaction
|
|
|
|
|
|
def _initial_from_transaction(transaction: Transaction):
|
|
initial = {
|
|
"account": transaction.account_id,
|
|
"type": transaction.type,
|
|
"description": transaction.description,
|
|
"notes": transaction.notes,
|
|
"category": transaction.category_id,
|
|
"tags": list(transaction.tags.values_list("name", flat=True)),
|
|
"entities": list(transaction.entities.values_list("name", flat=True)),
|
|
}
|
|
if transaction.linked_transaction_id:
|
|
to_transaction = transaction.transfer_to
|
|
initial.update(
|
|
type="TR",
|
|
to_account=to_transaction.account_id,
|
|
to_amount=to_transaction.amount,
|
|
)
|
|
return initial
|
|
|
|
|
|
def _sub_transactions_initial(transaction: Transaction):
|
|
return list(transaction.sub_transactions.values("name", "amount"))
|
|
|
|
|
|
def _get_unlinked_transaction(transaction_id):
|
|
# Transactions already generated by a plan or recurrence can't be converted again
|
|
return get_object_or_404(
|
|
Transaction,
|
|
id=transaction_id,
|
|
installment_plan__isnull=True,
|
|
recurring_transaction__isnull=True,
|
|
)
|
|
|
|
|
|
def _render_convert_form(request, form, title, transaction_id, url_name):
|
|
return render(
|
|
request,
|
|
"transactions/fragments/convert.html",
|
|
{
|
|
"form": form,
|
|
"title": title,
|
|
"form_url": reverse(url_name, kwargs={"transaction_id": transaction_id}),
|
|
},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def transaction_convert_to_quick_transaction(request, transaction_id):
|
|
# A transfer converts from its "from" side
|
|
transaction = get_object_or_404(Transaction, id=transaction_id).transfer_from
|
|
|
|
if request.method == "POST":
|
|
form = QuickTransactionForm(request.POST)
|
|
if form.is_valid():
|
|
form.save()
|
|
messages.success(request, _("Item added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
|
)
|
|
else:
|
|
form = QuickTransactionForm(
|
|
sub_transactions_initial=_sub_transactions_initial(transaction),
|
|
initial={
|
|
**_initial_from_transaction(transaction),
|
|
"is_paid": transaction.is_paid,
|
|
"amount": transaction.amount,
|
|
"mute": transaction.mute,
|
|
}
|
|
)
|
|
|
|
return _render_convert_form(
|
|
request,
|
|
form,
|
|
_("Convert into quick transaction"),
|
|
transaction_id,
|
|
"transaction_convert_to_quick_transaction",
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def transaction_convert_to_recurring_transaction(request, transaction_id):
|
|
transaction = _get_unlinked_transaction(transaction_id).transfer_from
|
|
|
|
if request.method == "POST":
|
|
form = RecurringTransactionForm(request.POST)
|
|
if form.is_valid():
|
|
with db_transaction.atomic():
|
|
form.save(source_transaction=transaction)
|
|
messages.success(request, _("Recurring Transaction added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
|
)
|
|
else:
|
|
form = RecurringTransactionForm(
|
|
sub_transactions_initial=_sub_transactions_initial(transaction),
|
|
initial={
|
|
**_initial_from_transaction(transaction),
|
|
"amount": transaction.amount,
|
|
"start_date": transaction.date,
|
|
"reference_date": transaction.reference_date,
|
|
"recurrence_interval": 1,
|
|
"recurrence_type": "month",
|
|
}
|
|
)
|
|
|
|
return _render_convert_form(
|
|
request,
|
|
form,
|
|
_("Convert into recurring transaction"),
|
|
transaction_id,
|
|
"transaction_convert_to_recurring_transaction",
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def transaction_convert_to_installment_plan(request, transaction_id):
|
|
transaction = _get_unlinked_transaction(transaction_id)
|
|
if transaction.linked_transaction_id:
|
|
raise Http404() # Installment plans don't support transfers
|
|
|
|
if request.method == "POST":
|
|
form = InstallmentPlanForm(request.POST)
|
|
if form.is_valid():
|
|
with db_transaction.atomic():
|
|
form.save(source_transaction=transaction)
|
|
messages.success(request, _("Installment Plan added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
|
)
|
|
else:
|
|
form = InstallmentPlanForm(
|
|
sub_transactions_initial=_sub_transactions_initial(transaction),
|
|
initial={
|
|
**_initial_from_transaction(transaction),
|
|
"installment_amount": transaction.amount,
|
|
"start_date": transaction.date,
|
|
"reference_date": transaction.reference_date,
|
|
}
|
|
)
|
|
|
|
return _render_convert_form(
|
|
request,
|
|
form,
|
|
_("Convert into installment plan"),
|
|
transaction_id,
|
|
"transaction_convert_to_installment_plan",
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def transaction_convert_to_transfer(request, transaction_id):
|
|
transaction = _get_unlinked_transaction(transaction_id)
|
|
if transaction.linked_transaction_id:
|
|
raise Http404() # Already a transfer
|
|
|
|
if request.method == "POST":
|
|
form = TransferForm(request.POST)
|
|
if form.is_valid():
|
|
with db_transaction.atomic():
|
|
form.save()
|
|
transaction.delete()
|
|
messages.success(request, _("Transaction converted into a transfer"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
|
)
|
|
else:
|
|
# An expense leaves its account (the "from" side), an income enters it ("to")
|
|
side = "from" if transaction.type == Transaction.Type.EXPENSE else "to"
|
|
form = TransferForm(
|
|
initial={
|
|
f"{side}_account": transaction.account_id,
|
|
f"{side}_category": transaction.category_id,
|
|
f"{side}_tags": list(transaction.tags.values_list("name", flat=True)),
|
|
f"{side}_entities": list(
|
|
transaction.entities.values_list("name", flat=True)
|
|
),
|
|
# To amount stays empty, so it follows from amount
|
|
"from_amount": transaction.amount,
|
|
"date": transaction.date,
|
|
"reference_date": transaction.reference_date,
|
|
"description": transaction.description,
|
|
"notes": transaction.notes,
|
|
"mute": transaction.mute,
|
|
"is_paid": transaction.is_paid,
|
|
}
|
|
)
|
|
|
|
return _render_convert_form(
|
|
request,
|
|
form,
|
|
_("Convert into transfer"),
|
|
transaction_id,
|
|
"transaction_convert_to_transfer",
|
|
)
|