add price_in_dollor field and update related models and serializers

This commit is contained in:
Parsa Nazer
2025-12-20 10:27:04 +03:30
parent 9845df4763
commit 25153d6b7a
5 changed files with 110 additions and 17 deletions
+31 -14
View File
@@ -6,6 +6,8 @@ import requests
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from home.models import ShowCaseSlider
from decimal import Decimal
from dirtyfields import DirtyFieldsMixin
@@ -119,9 +121,9 @@ class SubCategoryModel(models.Model):
class DollorModel(models.Model):
price = models.FloatField(null=True, blank=True, verbose_name='قیمت دلار')
defualt_price = models.FloatField(
null=True, blank=True, default=80000.0, verbose_name='قیمت دستی')
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='قیمت دلار')
defualt_price = models.DecimalField(
max_digits=10, decimal_places=2, null=True, blank=True, default=80000.0, verbose_name='قیمت دستی')
# these fields will avoid dublicate of this model
unique = (('unique', 'unique'),)
unique_filed = models.CharField(
@@ -354,32 +356,35 @@ class DetailModel(models.Model):
]
class ProductVariant(models.Model):
class ProductVariant(DirtyFieldsMixin, models.Model):
product = models.ForeignKey(
ProductModel, on_delete=models.CASCADE, related_name='variants', verbose_name='محصول')
product_attributes = models.ManyToManyField(
AttributeValue, verbose_name='ویژگی‌ها', related_name='variant')
in_stock = models.PositiveIntegerField(
default=0, verbose_name='تعداد موجود')
price = models.PositiveIntegerField(
price = models.PositiveBigIntegerField(
verbose_name='قیمت محاسبه شده', blank=True, null=True)
input_price = models.PositiveIntegerField(
default=0, verbose_name='قیمت ورودی')
min_price = models.PositiveIntegerField(
input_price = models.PositiveBigIntegerField(
default=0, verbose_name='قیمت محصول', help_text='قیمت ورودی محصول')
min_price = models.PositiveBigIntegerField(
verbose_name='قیمت کف', help_text='این قیمت برای کف قیمتی محصول در نظر گرفته میشود')
profit = models.BigIntegerField(
profit = models.PositiveBigIntegerField(
default=0, verbose_name='سود (تومان)', help_text='مقدار سود به ازای هر واحد به تومان')
special_discount_percent = models.SmallIntegerField(
default=0, verbose_name='درصد تخفیف ویژه', help_text='درصدی که از سود برای محاسبه تخفیف ویژه استفاده می‌شود')
currency_type = (
('dollor', 'دلار'),
('toman', 'تومان'),
('dollor', 'با نوسان دلاری'),
('toman', 'بدون نوسان'),
)
in_pack_items = models.ManyToManyField(
InPackItems, blank=True, verbose_name='ایتم های داخل پک')
sell = models.IntegerField(default=0, verbose_name='فروش')
currency = models.CharField(
verbose_name='نوع ارز', max_length=20, choices=currency_type)
verbose_name='نوع نوسان', max_length=20, choices=currency_type)
price_in_dollor = models.DecimalField(
max_digits=12, decimal_places=5, blank=True, null=True,
verbose_name='قیمت به دلار', help_text='قیمت محصول به دلار (محاسبه خودکار)')
discount = models.SmallIntegerField(default=0, verbose_name='تخفیف')
color = models.CharField(
verbose_name='رنگ', max_length=7, blank=True, null=True)
@@ -451,12 +456,24 @@ class ProductVariant(models.Model):
if self.currency == 'toman':
toman_price = self.input_price
# Clear dollar price when currency is toman
self.price_in_dollor = None
elif self.currency == 'dollor':
toman_price = self.input_price * dollor_price
# Only recalculate dollar price if input_price has changed
if self.is_dirty(check_relationship=False) and 'input_price' in self.get_dirty_fields(check_relationship=False):
self.price_in_dollor = Decimal(str(self.input_price)) / Decimal(str(dollor_price))
# Always recalculate Toman price using stored dollar price and current rate
if self.price_in_dollor:
toman_price = self.price_in_dollor * Decimal(str(dollor_price))
else:
# Fallback if price_in_dollor is not set yet
toman_price = self.input_price
else:
toman_price = self.input_price
self.price_in_dollor = None
self.price = max(toman_price, self.min_price)
self.price = max(int(toman_price), self.min_price)
def save(self, *args, **kwargs):
self.set_or_update_price()