product varient change list bug fix order item change quantity and remove
This commit is contained in:
+11
-8
@@ -1,8 +1,8 @@
|
||||
from django.db import models
|
||||
from account.models import User, UserAddressModel
|
||||
from product.models import ProductModel
|
||||
from product.models import ProductModel, ProductVariant
|
||||
from django.utils import timezone
|
||||
|
||||
from .execptions import DiscountNotAvailableError
|
||||
class DiscountCode(models.Model):
|
||||
name = models.CharField(max_length=50, verbose_name='کد تخفیف')
|
||||
percent = models.DecimalField(max_digits=4, decimal_places=2, verbose_name='درصد')
|
||||
@@ -43,13 +43,15 @@ class OrderModel(models.Model):
|
||||
verbose_name = 'سفارش'
|
||||
verbose_name_plural = 'سفارشات'
|
||||
|
||||
def total_without_tax(self):
|
||||
return sum(item.total() for item in self.items.all())
|
||||
# def total_without_tax(self):
|
||||
# return sum(item.total() for item in self.items.all())
|
||||
|
||||
|
||||
def total_with_discount(self):
|
||||
total_with_item_discount = sum(item.total_with_discount() for item in self.items.all())
|
||||
if self.discount_code and self.discount_code.is_valid():
|
||||
if self.discount_code:
|
||||
if not self.discount_code.is_valid():
|
||||
raise DiscountNotAvailableError('این کد تخفیف دیگر معتبر نیست')
|
||||
discount_percent = self.discount_code.percent
|
||||
return total_with_item_discount * ((100 - discount_percent) / 100)
|
||||
return total_with_item_discount
|
||||
@@ -59,7 +61,7 @@ class OrderModel(models.Model):
|
||||
return self.total_without_tax() * 0.2
|
||||
|
||||
def total(self):
|
||||
return self.total_without_tax + self.tax()
|
||||
return self.total_with_discount() + self.tax()
|
||||
|
||||
def remove_order_item(self, item_pk, quantity):
|
||||
pass
|
||||
@@ -75,7 +77,7 @@ class OrderModel(models.Model):
|
||||
class OrderItemModel(models.Model):
|
||||
order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, related_name='items', verbose_name='سفارش')
|
||||
quantity = models.SmallIntegerField(verbose_name="تعداد")
|
||||
product = models.ForeignKey(ProductModel, on_delete=models.PROTECT, verbose_name="محصول")
|
||||
product = models.ForeignKey(ProductVariant, on_delete=models.PROTECT, verbose_name="محصول")
|
||||
class Meta:
|
||||
verbose_name = 'محصول خریداری شده'
|
||||
verbose_name_plural = 'محصولات خریداری شده'
|
||||
@@ -85,5 +87,6 @@ class OrderItemModel(models.Model):
|
||||
|
||||
def total_with_discount(self):
|
||||
return self.quantity * self.product.get_toman_price_after_discount()
|
||||
|
||||
def __str__(self):
|
||||
return f'ایتم سبد خرید محصول: {self.product} کاربر {self.order.user}'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user