mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-19 07:08:54 +01:00
Adds Convert > Transfer to the transaction menu. The transfer form opens prefilled on the side matching the transaction type (expense = from, income = to); saving creates the transfer and deletes the original. Not available for transfers or plan/recurrence transactions.
223 lines
7.3 KiB
Python
223 lines
7.3 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 _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(
|
|
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(
|
|
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(
|
|
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",
|
|
)
|