mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-19 07:08:54 +01:00
feat(transactions): convert a transaction into a transfer
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.
This commit is contained in:
1 parent
e552723078
commit
2a06bd4a06
4 files changed
+120
No files matched your search
@@ -312,6 +312,54 @@ class TransactionConvertViewTests(TestCase):
|
||||
set(plan.transactions.values_list("amount", flat=True)), {70}
|
||||
)
|
||||
|
||||
def test_transfer_form_prefills_side_from_type(self):
|
||||
initial = self._get("transaction_convert_to_transfer").context["form"].initial
|
||||
self.assertEqual(initial["from_account"], self.account.id)
|
||||
self.assertEqual(initial["from_tags"], ["TestTag"])
|
||||
self.assertNotIn("to_account", initial)
|
||||
|
||||
self.transaction.type = Transaction.Type.INCOME
|
||||
self.transaction.save()
|
||||
initial = self._get("transaction_convert_to_transfer").context["form"].initial
|
||||
self.assertEqual(initial["to_account"], self.account.id)
|
||||
self.assertNotIn("from_account", initial)
|
||||
self.assertEqual(initial["from_amount"], 50)
|
||||
self.assertNotIn("to_amount", initial)
|
||||
|
||||
def test_transfer_replaces_original_transaction(self):
|
||||
other = Account.objects.create(name="Savings", currency=self.currency)
|
||||
response = self._post(
|
||||
"transaction_convert_to_transfer",
|
||||
{
|
||||
"from_account": self.account.id,
|
||||
"to_account": other.id,
|
||||
"from_amount": "50",
|
||||
"date": "2030-01-15",
|
||||
"is_paid": "on",
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertFalse(Transaction.objects.filter(id=self.transaction.id).exists())
|
||||
self.assertEqual(
|
||||
Transaction.objects.filter(linked_transaction__isnull=False).count(), 2
|
||||
)
|
||||
# A transfer can't be converted into one again
|
||||
self.transaction = Transaction.objects.get(account=other)
|
||||
self.assertEqual(self._get("transaction_convert_to_transfer").status_code, 404)
|
||||
|
||||
def test_invalid_transfer_keeps_original_transaction(self):
|
||||
response = self._post(
|
||||
"transaction_convert_to_transfer",
|
||||
{
|
||||
"from_account": self.account.id,
|
||||
"to_account": self.account.id,
|
||||
"from_amount": "50",
|
||||
"date": "2030-01-15",
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(Transaction.objects.filter(id=self.transaction.id).exists())
|
||||
|
||||
def test_linked_transaction_cannot_be_converted_into_plan(self):
|
||||
self._post(
|
||||
"transaction_convert_to_installment_plan",
|
||||
|
||||
@@ -417,4 +417,9 @@ urlpatterns = [
|
||||
views.transaction_convert_to_installment_plan,
|
||||
name="transaction_convert_to_installment_plan",
|
||||
),
|
||||
path(
|
||||
"transaction/<int:transaction_id>/convert/transfer/",
|
||||
views.transaction_convert_to_transfer,
|
||||
name="transaction_convert_to_transfer",
|
||||
),
|
||||
]
|
||||
@@ -12,6 +12,7 @@ from apps.transactions.forms import (
|
||||
InstallmentPlanForm,
|
||||
QuickTransactionForm,
|
||||
RecurringTransactionForm,
|
||||
TransferForm,
|
||||
)
|
||||
from apps.transactions.models import Transaction
|
||||
|
||||
@@ -168,3 +169,54 @@ def transaction_convert_to_installment_plan(request, transaction_id):
|
||||
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",
|
||||
)
|
||||
@@ -259,6 +259,16 @@
|
||||
</div>
|
||||
</a>
|
||||
</li>
|
||||
<li class="menu-disabled" aria-disabled="true">
|
||||
<a class="flex items-center">
|
||||
<i class="fa-solid fa-money-bill-transfer fa-fw mr-2"></i>
|
||||
<div>
|
||||
{% translate 'Transfer' %}
|
||||
<div
|
||||
class="block text-base-content/60 text-xs font-medium">{% translate 'Already part of a plan or recurrence' %}</div>
|
||||
</div>
|
||||
</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li><a href="#"
|
||||
hx-get="{% url 'transaction_convert_to_recurring_transaction' transaction_id=transaction.id %}"
|
||||
@@ -282,6 +292,11 @@
|
||||
hx-target="#generic-offcanvas" hx-swap="innerHTML"><i
|
||||
class="fa-solid fa-divide fa-fw mr-2"></i>{% translate 'Installment Plan' %}</a>
|
||||
</li>
|
||||
<li><a href="#"
|
||||
hx-get="{% url 'transaction_convert_to_transfer' transaction_id=transaction.id %}"
|
||||
hx-target="#generic-offcanvas" hx-swap="innerHTML"><i
|
||||
class="fa-solid fa-money-bill-transfer fa-fw mr-2"></i>{% translate 'Transfer' %}</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</ul>
|
||||
|
||||
Reference in new issue
Block a user