feat: add option to archive accounts

This commit is contained in:
Herculino Trotta
2024-10-27 21:54:02 -03:00
parent 9f371c2693
commit bfd0cc45a3
8 changed files with 78 additions and 13 deletions
+9 -1
View File
@@ -60,7 +60,14 @@ class AccountForm(forms.ModelForm):
class Meta: class Meta:
model = Account model = Account
fields = ["name", "group", "currency", "exchange_currency", "is_asset"] fields = [
"name",
"group",
"currency",
"exchange_currency",
"is_asset",
"is_archived",
]
widgets = { widgets = {
"currency": TomSelect(), "currency": TomSelect(),
"exchange_currency": TomSelect(), "exchange_currency": TomSelect(),
@@ -76,6 +83,7 @@ class AccountForm(forms.ModelForm):
"name", "name",
"group", "group",
Switch("is_asset"), Switch("is_asset"),
Switch("is_archived"),
"currency", "currency",
"exchange_currency", "exchange_currency",
) )
@@ -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'),
),
]
@@ -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'),
),
]
+6 -1
View File
@@ -43,11 +43,16 @@ class Account(models.Model):
is_asset = models.BooleanField( is_asset = models.BooleanField(
default=False, default=False,
verbose_name=_("Is an asset account?"), verbose_name=_("Asset account"),
help_text=_( help_text=_(
"Asset accounts count towards your Net Worth, but not towards your month." "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: class Meta:
verbose_name = _("Account") verbose_name = _("Account")
+3 -1
View File
@@ -38,7 +38,9 @@ def account_reconciliation(request):
"prefix": account.currency.prefix, "prefix": account.currency.prefix,
"current_balance": get_account_balance(account), "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": if request.method == "POST":
@@ -40,9 +40,11 @@ def calculate_account_net_worth():
) )
# Main query to fetch all account data # Main query to fetch all account data
accounts_data = Account.objects.annotate( accounts_data = (
balance=Coalesce(Subquery(balance_subquery), Decimal("0")) Account.objects.filter(is_archived=False)
).select_related("currency", "exchange_currency", "group") .annotate(balance=Coalesce(Subquery(balance_subquery), Decimal("0")))
.select_related("currency", "exchange_currency", "group")
)
account_net_worth = {ungrouped_id: {"name": _("Ungrouped"), "accounts": {}}} account_net_worth = {ungrouped_id: {"name": _("Ungrouped"), "accounts": {}}}
@@ -209,7 +211,7 @@ def calculate_historical_currency_net_worth():
def calculate_historical_account_balance(): def calculate_historical_account_balance():
# Get all accounts # Get all accounts
accounts = Account.objects.all() accounts = Account.objects.filter(is_archived=False)
# Get the date range # Get the date range
date_range = Transaction.objects.filter(is_paid=True).aggregate( date_range = Transaction.objects.filter(is_paid=True).aggregate(
+9 -4
View File
@@ -42,6 +42,11 @@ class TransactionForm(forms.ModelForm):
required=False, required=False,
label=_("Tags"), 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) reference_date = MonthYearFormField(label=_("Reference Date"), required=False)
class Meta: class Meta:
@@ -140,12 +145,12 @@ class TransactionForm(forms.ModelForm):
class TransferForm(forms.Form): class TransferForm(forms.Form):
from_account = forms.ModelChoiceField( from_account = forms.ModelChoiceField(
queryset=Account.objects.all(), queryset=Account.objects.filter(is_archived=False),
label=_("From Account"), label=_("From Account"),
widget=TomSelect(clear_button=False, group_by="group"), widget=TomSelect(clear_button=False, group_by="group"),
) )
to_account = forms.ModelChoiceField( to_account = forms.ModelChoiceField(
queryset=Account.objects.all(), queryset=Account.objects.filter(is_archived=False),
label=_("To Account"), label=_("To Account"),
widget=TomSelect(clear_button=False, group_by="group"), widget=TomSelect(clear_button=False, group_by="group"),
) )
@@ -330,7 +335,7 @@ class TransferForm(forms.Form):
class InstallmentPlanForm(forms.ModelForm): class InstallmentPlanForm(forms.ModelForm):
account = forms.ModelChoiceField( account = forms.ModelChoiceField(
queryset=Account.objects.all(), queryset=Account.objects.filter(is_archived=False),
label=_("Account"), label=_("Account"),
widget=TomSelect(clear_button=False, group_by="group"), widget=TomSelect(clear_button=False, group_by="group"),
) )
@@ -506,7 +511,7 @@ class TransactionCategoryForm(forms.ModelForm):
class RecurringTransactionForm(forms.ModelForm): class RecurringTransactionForm(forms.ModelForm):
account = forms.ModelChoiceField( account = forms.ModelChoiceField(
queryset=Account.objects.all(), queryset=Account.objects.filter(is_archived=False),
label=_("Account"), label=_("Account"),
widget=TomSelect(clear_button=False, group_by="group"), widget=TomSelect(clear_button=False, group_by="group"),
) )
+4 -2
View File
@@ -23,7 +23,8 @@
<th scope="col" class="col">{% translate 'Name' %}</th> <th scope="col" class="col">{% translate 'Name' %}</th>
<th scope="col" class="col">{% translate 'Currency' %}</th> <th scope="col" class="col">{% translate 'Currency' %}</th>
<th scope="col" class="col">{% translate 'Exchange Currency' %}</th> <th scope="col" class="col">{% translate 'Exchange Currency' %}</th>
<th scope="col" class="col-auto">{% translate 'Is Asset' %}</th> <th scope="col" class="col">{% translate 'Is Asset' %}</th>
<th scope="col" class="col">{% translate 'Archived' %}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@@ -52,7 +53,8 @@
<td class="col-auto">{{ account.currency }} ({{ account.currency.code }})</td> <td class="col-auto">{{ account.currency }} ({{ account.currency.code }})</td>
<td class="col-auto">{% if account.exchange_currency %}{{ account.exchange_currency }} ( <td class="col-auto">{% if account.exchange_currency %}{{ account.exchange_currency }} (
{{ account.exchange_currency.code }}){% else %}-{% endif %}</td> {{ account.exchange_currency.code }}){% else %}-{% endif %}</td>
<td class="col-auto text-center">{% if account.is_asset %}<i class="fa-solid fa-circle text-success"></i>{% endif %}</td> <td class="col">{% if account.is_asset %}<i class="fa-solid fa-solid fa-check text-success"></i>{% endif %}</td>
<td class="col">{% if account.is_archived %}<i class="fa-solid fa-solid fa-check text-success"></i>{% endif %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>