add rounding method for Toman price in ProductVariant model

This commit is contained in:
Parsa Nazer
2026-01-06 09:25:47 +03:30
parent d60f5089a7
commit 20ef2fe5fe
+7 -2
View File
@@ -446,6 +446,10 @@ class ProductVariant(DirtyFieldsMixin, models.Model):
except Exception: except Exception:
return 0 return 0
def round_toman(self, value):
"""Round to nearest 1000 toman (e.g., 89123 -> 89000, 89500 -> 90000)"""
return round(value / 1000) * 1000
def set_or_update_price(self, dollor_price=None): def set_or_update_price(self, dollor_price=None):
if not dollor_price: if not dollor_price:
dollor_object, _ = DollorModel.objects.get_or_create( dollor_object, _ = DollorModel.objects.get_or_create(
@@ -468,7 +472,8 @@ class ProductVariant(DirtyFieldsMixin, models.Model):
# Always recalculate Toman price using stored dollar price and current rate # Always recalculate Toman price using stored dollar price and current rate
if self.price_in_dollor: if self.price_in_dollor:
toman_price = self.price_in_dollor * Decimal(str(dollor_price)) # toman_price = self.price_in_dollor * Decimal(str(dollor_price))
toman_price = Decimal(str(self.price_in_dollor)) * Decimal(str(dollor_price))
else: else:
# Fallback if price_in_dollor is not set yet # Fallback if price_in_dollor is not set yet
toman_price = self.input_price toman_price = self.input_price
@@ -476,7 +481,7 @@ class ProductVariant(DirtyFieldsMixin, models.Model):
toman_price = self.input_price toman_price = self.input_price
self.price_in_dollor = None self.price_in_dollor = None
self.price = max(int(toman_price), self.min_price) self.price = max(self.round_toman(toman_price), self.min_price)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.set_or_update_price() self.set_or_update_price()