Skip to content

Commit f3d528c

Browse files
committed
WIP feature: Started adding discounts
1 parent 60f360e commit f3d528c

File tree

9 files changed

+19985
-29457
lines changed

9 files changed

+19985
-29457
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 5.0.3 on 2024-03-29 20:00
2+
3+
import django.core.validators
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("backend", "0025_alter_invoiceonetimeschedule_stored_schedule_arn"),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name="invoice",
16+
name="discount_amount",
17+
field=models.DecimalField(decimal_places=2, default=0, max_digits=15),
18+
),
19+
migrations.AddField(
20+
model_name="invoice",
21+
name="discount_percentage",
22+
field=models.DecimalField(
23+
decimal_places=2, default=0, max_digits=5, validators=[django.core.validators.MaxValueValidator(100)]
24+
),
25+
),
26+
]

backend/models.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import decimal
2+
from decimal import Decimal
13
from uuid import uuid4
24

35
from django.contrib.auth.hashers import make_password, check_password
46
from django.contrib.auth.models import UserManager, AbstractUser, AnonymousUser
7+
from django.core.validators import MaxValueValidator
58
from django.db import models
69
from django.db.models import Count
710
from django.utils import timezone
@@ -303,6 +306,9 @@ class Invoice(models.Model):
303306
date_due = models.DateField()
304307
date_issued = models.DateField(blank=True, null=True)
305308

309+
discount_amount = models.DecimalField(max_digits=15, default=0, decimal_places=2)
310+
discount_percentage = models.DecimalField(default=0, max_digits=5, decimal_places=2, validators=[MaxValueValidator(100)])
311+
306312
class Meta:
307313
constraints = [USER_OR_ORGANIZATION_CONSTRAINT()]
308314

@@ -345,10 +351,33 @@ def get_subtotal(self):
345351
subtotal += item.get_total_price()
346352
return round(subtotal, 2)
347353

354+
def get_tax(self, amount: float = 0.00) -> float:
355+
amount = amount or self.get_subtotal()
356+
if self.vat_number:
357+
return round(amount * 0.2, 2)
358+
return 0
359+
360+
def get_percentage_amount(self, subtotal: float = 0.00) -> Decimal:
361+
total = subtotal or self.get_subtotal()
362+
363+
if self.discount_percentage > 0:
364+
return round(total * (self.discount_percentage / 100), 2)
365+
return Decimal(0)
366+
348367
def get_total_price(self):
349-
total = 0
350-
subtotal = self.get_subtotal()
351-
total = subtotal * 1.2 if self.vat_number else subtotal
368+
total = self.get_subtotal() or 0.00
369+
370+
total -= self.get_percentage_amount()
371+
372+
discount_amount = self.discount_amount
373+
374+
total -= discount_amount
375+
376+
if 0 > total:
377+
total = 0
378+
else:
379+
total -= self.get_tax(total)
380+
352381
return round(total, 2)
353382

354383

frontend/static/js/bundle.js

Lines changed: 19530 additions & 29103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)