Skip to content

Commit

Permalink
calculate the discount field (amount - discount)
Browse files Browse the repository at this point in the history
also calculate the discount field (amount - discount)
  • Loading branch information
hoosnick authored Sep 8, 2023
2 parents 03b86d3 + 1030250 commit feace0d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/payme/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
auto_created=True, primary_key=True,
serialize=False, verbose_name='ID'
)),
('discount', models.FloatField(blank=True, null=True)),
('discount', models.BigIntegerField(blank=True, null=True)),
('title', models.CharField(max_length=255)),
('price', models.BigIntegerField(default=0)),
('count', models.IntegerField(default=1)),
Expand Down
48 changes: 39 additions & 9 deletions lib/payme/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Item(models.Model):
Item class \
That's used for managing order items
"""
discount = models.FloatField(null=True, blank=True)
discount = models.BigIntegerField(null=True, blank=True)
title = models.CharField(max_length=255)
price = models.BigIntegerField(default=0)
count = models.IntegerField(default=1)
Expand All @@ -50,7 +50,7 @@ class Item(models.Model):
vat_percent = models.IntegerField(default=0, null=True, blank=True)

def __str__(self) -> str:
return f"[{self.id}] {self.title} ({self.count} pc.) x {self.price:,}"
return f"[{self.id}] {self.title} ({self.count} pc.) x {self.price:,} UZS"


class OrderDetail(models.Model):
Expand All @@ -69,17 +69,47 @@ class OrderDetail(models.Model):
@property
def get_items_display(self):
# pylint: disable=missing-function-docstring
return ', '.join([items.title for items in self.items.all()])
item_display = [f'[{self.pk}]']

for fld in self._meta.get_fields():
if not isinstance(fld, models.ManyToOneRel):
continue

if not issubclass(fld.related_model, BaseOrder):
continue

related_order = fld.get_accessor_name()
orders = getattr(self, related_order).values()

order_id = orders[0].get('id') if orders else 'None'

item_display.append(f'FOR ORDER - {order_id}')
if self.shipping:
item_display.append(f'ADDRESS: {self.shipping.title}')
item_display.append(f'AMOUNT: {self.get_total_items_price} UZS')

return ' '.join(item_display)

@property
def get_total_items_price(self) -> int:
# pylint: disable=missing-function-docstring
shipping_price = self.shipping.price if self.shipping else 0
return shipping_price + self.items.all().aggregate(
items = self.items.all().aggregate(
total_price=models.Sum(
models.F('price') * models.F('count')
models.Case(
models.When(
discount__isnull=True,
then=models.F('price') * models.F('count')
),
default=(
(models.F('price') * models.F('count')) -
models.F('discount')
),
output_field=models.BigIntegerField()
)
)
)['total_price']
)
shipping_price = self.shipping.price if self.shipping else 0
return f"{shipping_price + items['total_price']:,}"

def __str__(self) -> str:
return f"{self.get_items_display}"
Expand Down Expand Up @@ -116,10 +146,10 @@ class BaseOrder(models.Model, metaclass=DisallowOverrideMetaclass):
@property
def amount(self):
# pylint: disable=missing-function-docstring
return self.detail.get_total_items_price
return self.detail.get_total_items_price if self.detail else 0

def __str__(self):
return f"ORDER ID: {self.id} - AMOUNT: {self.amount:,}"
return f"ORDER ID: {self.id} - AMOUNT: {self.amount} UZS"

class Meta:
# pylint: disable=missing-class-docstring
Expand Down

0 comments on commit feace0d

Please sign in to comment.