From bfd0cc45a3bc9c15cea304630660544bf45aecfb Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 27 Oct 2024 21:54:02 -0300 Subject: [PATCH] feat: add option to archive accounts --- app/apps/accounts/forms.py | 10 +++++++- .../migrations/0005_account_archived.py | 18 +++++++++++++++ ...e_archived_account_is_archived_and_more.py | 23 +++++++++++++++++++ app/apps/accounts/models.py | 7 +++++- app/apps/accounts/views/balance.py | 4 +++- .../net_worth/utils/calculate_net_worth.py | 10 ++++---- app/apps/transactions/forms.py | 13 +++++++---- app/templates/accounts/fragments/list.html | 6 +++-- 8 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 app/apps/accounts/migrations/0005_account_archived.py create mode 100644 app/apps/accounts/migrations/0006_rename_archived_account_is_archived_and_more.py diff --git a/app/apps/accounts/forms.py b/app/apps/accounts/forms.py index 662f5a7..ba83092 100644 --- a/app/apps/accounts/forms.py +++ b/app/apps/accounts/forms.py @@ -60,7 +60,14 @@ class AccountForm(forms.ModelForm): class Meta: model = Account - fields = ["name", "group", "currency", "exchange_currency", "is_asset"] + fields = [ + "name", + "group", + "currency", + "exchange_currency", + "is_asset", + "is_archived", + ] widgets = { "currency": TomSelect(), "exchange_currency": TomSelect(), @@ -76,6 +83,7 @@ class AccountForm(forms.ModelForm): "name", "group", Switch("is_asset"), + Switch("is_archived"), "currency", "exchange_currency", ) diff --git a/app/apps/accounts/migrations/0005_account_archived.py b/app/apps/accounts/migrations/0005_account_archived.py new file mode 100644 index 0000000..c6c6fb0 --- /dev/null +++ b/app/apps/accounts/migrations/0005_account_archived.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.2 on 2024-10-28 00:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0004_account_group'), + ] + + operations = [ + migrations.AddField( + model_name='account', + name='archived', + field=models.BooleanField(default=False, help_text="Archived accounts don't show up nor count towards your net worth", verbose_name='Archived'), + ), + ] diff --git a/app/apps/accounts/migrations/0006_rename_archived_account_is_archived_and_more.py b/app/apps/accounts/migrations/0006_rename_archived_account_is_archived_and_more.py new file mode 100644 index 0000000..210e6cb --- /dev/null +++ b/app/apps/accounts/migrations/0006_rename_archived_account_is_archived_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.2 on 2024-10-28 00:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0005_account_archived'), + ] + + operations = [ + migrations.RenameField( + model_name='account', + old_name='archived', + new_name='is_archived', + ), + migrations.AlterField( + model_name='account', + name='is_asset', + field=models.BooleanField(default=False, help_text='Asset accounts count towards your Net Worth, but not towards your month.', verbose_name='Asset account'), + ), + ] diff --git a/app/apps/accounts/models.py b/app/apps/accounts/models.py index c975b4a..b2ccb19 100644 --- a/app/apps/accounts/models.py +++ b/app/apps/accounts/models.py @@ -43,11 +43,16 @@ class Account(models.Model): is_asset = models.BooleanField( default=False, - verbose_name=_("Is an asset account?"), + verbose_name=_("Asset account"), help_text=_( "Asset accounts count towards your Net Worth, but not towards your month." ), ) + is_archived = models.BooleanField( + default=False, + verbose_name=_("Archived"), + help_text=_("Archived accounts don't show up nor count towards your net worth"), + ) class Meta: verbose_name = _("Account") diff --git a/app/apps/accounts/views/balance.py b/app/apps/accounts/views/balance.py index 1817b2e..d92c768 100644 --- a/app/apps/accounts/views/balance.py +++ b/app/apps/accounts/views/balance.py @@ -38,7 +38,9 @@ def account_reconciliation(request): "prefix": account.currency.prefix, "current_balance": get_account_balance(account), } - for account in Account.objects.all().select_related("currency", "group") + for account in Account.objects.filter(is_archived=False).select_related( + "currency", "group" + ) ] if request.method == "POST": diff --git a/app/apps/net_worth/utils/calculate_net_worth.py b/app/apps/net_worth/utils/calculate_net_worth.py index a80f38c..54c75c7 100644 --- a/app/apps/net_worth/utils/calculate_net_worth.py +++ b/app/apps/net_worth/utils/calculate_net_worth.py @@ -40,9 +40,11 @@ def calculate_account_net_worth(): ) # Main query to fetch all account data - accounts_data = Account.objects.annotate( - balance=Coalesce(Subquery(balance_subquery), Decimal("0")) - ).select_related("currency", "exchange_currency", "group") + accounts_data = ( + Account.objects.filter(is_archived=False) + .annotate(balance=Coalesce(Subquery(balance_subquery), Decimal("0"))) + .select_related("currency", "exchange_currency", "group") + ) account_net_worth = {ungrouped_id: {"name": _("Ungrouped"), "accounts": {}}} @@ -209,7 +211,7 @@ def calculate_historical_currency_net_worth(): def calculate_historical_account_balance(): # Get all accounts - accounts = Account.objects.all() + accounts = Account.objects.filter(is_archived=False) # Get the date range date_range = Transaction.objects.filter(is_paid=True).aggregate( diff --git a/app/apps/transactions/forms.py b/app/apps/transactions/forms.py index 0a048a6..486ab53 100644 --- a/app/apps/transactions/forms.py +++ b/app/apps/transactions/forms.py @@ -42,6 +42,11 @@ class TransactionForm(forms.ModelForm): required=False, label=_("Tags"), ) + account = forms.ModelChoiceField( + queryset=Account.objects.filter(is_archived=False), + label=_("Account"), + widget=TomSelect(clear_button=False, group_by="group"), + ) reference_date = MonthYearFormField(label=_("Reference Date"), required=False) class Meta: @@ -140,12 +145,12 @@ class TransactionForm(forms.ModelForm): class TransferForm(forms.Form): from_account = forms.ModelChoiceField( - queryset=Account.objects.all(), + queryset=Account.objects.filter(is_archived=False), label=_("From Account"), widget=TomSelect(clear_button=False, group_by="group"), ) to_account = forms.ModelChoiceField( - queryset=Account.objects.all(), + queryset=Account.objects.filter(is_archived=False), label=_("To Account"), widget=TomSelect(clear_button=False, group_by="group"), ) @@ -330,7 +335,7 @@ class TransferForm(forms.Form): class InstallmentPlanForm(forms.ModelForm): account = forms.ModelChoiceField( - queryset=Account.objects.all(), + queryset=Account.objects.filter(is_archived=False), label=_("Account"), widget=TomSelect(clear_button=False, group_by="group"), ) @@ -506,7 +511,7 @@ class TransactionCategoryForm(forms.ModelForm): class RecurringTransactionForm(forms.ModelForm): account = forms.ModelChoiceField( - queryset=Account.objects.all(), + queryset=Account.objects.filter(is_archived=False), label=_("Account"), widget=TomSelect(clear_button=False, group_by="group"), ) diff --git a/app/templates/accounts/fragments/list.html b/app/templates/accounts/fragments/list.html index 2961ae3..cad0838 100644 --- a/app/templates/accounts/fragments/list.html +++ b/app/templates/accounts/fragments/list.html @@ -23,7 +23,8 @@ {% translate 'Name' %} {% translate 'Currency' %} {% translate 'Exchange Currency' %} - {% translate 'Is Asset' %} + {% translate 'Is Asset' %} + {% translate 'Archived' %} @@ -52,7 +53,8 @@ {{ account.currency }} ({{ account.currency.code }}) {% if account.exchange_currency %}{{ account.exchange_currency }} ( {{ account.exchange_currency.code }}){% else %}-{% endif %} - {% if account.is_asset %}{% endif %} + {% if account.is_asset %}{% endif %} + {% if account.is_archived %}{% endif %} {% endfor %}