Skip to content

Commit 5cc8d58

Browse files
committed
Calculate customer current amount
1 parent d07d6c3 commit 5cc8d58

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

account/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@admin.register(Customer)
66
class CustomerAdmin(admin.ModelAdmin):
7-
pass
7+
list_display = ['user', 'current_amount']
88

99

1010
@admin.register(Transaction)

account/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class Customer(models.Model):
1111
def __str__(self):
1212
return f"{self.user}"
1313

14+
def re_calculate_current_amount(self):
15+
self.current_amount = 0
16+
for t in self.transaction_set.all():
17+
if t.type == Transaction.Type.INCOME:
18+
self.current_amount += t.amount
19+
else:
20+
self.current_amount -= t.amount
21+
self.save()
22+
1423

1524
class Transaction(models.Model):
1625

@@ -26,3 +35,7 @@ class Type(models.TextChoices):
2635

2736
def __str__(self):
2837
return f"{self.type} [{self.amount}]"
38+
39+
def save(self, *args, **kwargs):
40+
super().save(*args, **kwargs)
41+
self.customer.re_calculate_current_amount()

0 commit comments

Comments
 (0)