mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-23 17:04:26 +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.
174 lines
5.0 KiB
Python
174 lines
5.0 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.forms import model_to_dict
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.utils import timezone
|
|
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 QuickTransactionForm
|
|
from apps.transactions.models import QuickTransaction, transaction_created
|
|
from apps.transactions.models import Transaction
|
|
|
|
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_index(request):
|
|
return render(
|
|
request,
|
|
"quick_transactions/pages/index.html",
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_list(request):
|
|
quick_transactions = QuickTransaction.objects.all().order_by("name")
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/list.html",
|
|
context={"quick_transactions": quick_transactions},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def quick_transaction_add(request):
|
|
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()
|
|
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/add.html",
|
|
{"form": form},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET", "POST"])
|
|
def quick_transaction_edit(request, quick_transaction_id):
|
|
quick_transaction = get_object_or_404(QuickTransaction, id=quick_transaction_id)
|
|
|
|
if request.method == "POST":
|
|
form = QuickTransactionForm(request.POST, instance=quick_transaction)
|
|
if form.is_valid():
|
|
form.save()
|
|
messages.success(request, _("Item updated successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
else:
|
|
form = QuickTransactionForm(instance=quick_transaction)
|
|
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/edit.html",
|
|
{"form": form, "quick_transaction": quick_transaction},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["DELETE"])
|
|
def quick_transaction_delete(request, quick_transaction_id):
|
|
quick_transaction = get_object_or_404(QuickTransaction, id=quick_transaction_id)
|
|
|
|
quick_transaction.delete()
|
|
|
|
messages.success(request, _("Item deleted successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transactions_create_menu(request):
|
|
quick_transactions = QuickTransaction.objects.all().order_by("name")
|
|
return render(
|
|
request,
|
|
"quick_transactions/fragments/create_menu.html",
|
|
context={"quick_transactions": quick_transactions},
|
|
)
|
|
|
|
|
|
@only_htmx
|
|
@login_required
|
|
@require_http_methods(["GET"])
|
|
def quick_transaction_add_as_transaction(request, quick_transaction_id):
|
|
quick_transaction: QuickTransaction = get_object_or_404(
|
|
QuickTransaction, id=quick_transaction_id
|
|
)
|
|
today = timezone.localdate(timezone.now())
|
|
|
|
quick_transaction_data = model_to_dict(
|
|
quick_transaction,
|
|
exclude=[
|
|
"id",
|
|
"name",
|
|
"owner",
|
|
"account",
|
|
"category",
|
|
"tags",
|
|
"entities",
|
|
"internal_id",
|
|
"to_account",
|
|
"to_amount",
|
|
],
|
|
)
|
|
|
|
new_transaction = Transaction(**quick_transaction_data)
|
|
new_transaction.type = quick_transaction.transaction_type
|
|
new_transaction.account = quick_transaction.account
|
|
new_transaction.category = quick_transaction.category
|
|
|
|
new_transaction.date = today
|
|
new_transaction.reference_date = today.replace(day=1)
|
|
with db_transaction.atomic():
|
|
new_transaction.save()
|
|
new_transaction.tags.set(quick_transaction.tags.all())
|
|
new_transaction.entities.set(quick_transaction.entities.all())
|
|
new_transaction.copy_sub_transactions_from(quick_transaction)
|
|
to_transaction = quick_transaction.complete_transfer(new_transaction)
|
|
|
|
transaction_created.send(sender=new_transaction)
|
|
if to_transaction:
|
|
transaction_created.send(sender=to_transaction)
|
|
|
|
messages.success(request, _("Transaction added successfully"))
|
|
|
|
return HttpResponse(
|
|
status=204,
|
|
headers={
|
|
"HX-Trigger": "updated, hide_offcanvas",
|
|
},
|
|
)
|
|
|