mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-17 14:21:32 +01:00
Transfers are still two transactions, now linked to each other:
- shown as one item in lists, with each side's amount, account,
category and tags ("Combine transfers" user setting, on by default)
- date, reference date, paid and mute stay in sync between sides
- edit opens the transfer form; delete, restore, duplicate and bulk
actions act on both sides
- transfers made by the transfer form now trigger rules for each side
Recurring and quick transactions gain a Transfer type, with a "To
account" and optional "To amount", generating linked transfers. A
transfer can be converted into either.
Existing transfers are left as they are, since there is no reliable
way to pair them.
171 lines
5.4 KiB
Python
171 lines
5.4 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,
|
|
)
|
|
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",
|
|
)
|