feat(products): add support for product sales units
Add new models UnitOfMeasure and ProductSalesUnit to enable selling products in different units (e.g., bunches, kg). Update Product model with base_unit field and methods for unit conversions and availability. Extend Sale, Reservation, and OrderItem models with sales_unit fields and snapshots. Modify SaleProcessor to handle quantity conversions. Include admin interfaces for managing units. Add corresponding database migrations.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from decimal import Decimal
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from products.models import Product, ProductKit
|
||||
@@ -96,6 +97,39 @@ class OrderItem(models.Model):
|
||||
help_text="Витрина, с которой был продан товар"
|
||||
)
|
||||
|
||||
# === ПОЛЯ ДЛЯ ЕДИНИЦ ПРОДАЖИ ===
|
||||
sales_unit = models.ForeignKey(
|
||||
'products.ProductSalesUnit',
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='order_items',
|
||||
verbose_name="Единица продажи"
|
||||
)
|
||||
unit_name_snapshot = models.CharField(
|
||||
max_length=100,
|
||||
blank=True,
|
||||
default='',
|
||||
verbose_name="Название единицы (snapshot)",
|
||||
help_text="Название единицы продажи на момент заказа"
|
||||
)
|
||||
conversion_factor_snapshot = models.DecimalField(
|
||||
max_digits=15,
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name="Коэффициент конверсии (snapshot)",
|
||||
help_text="Коэффициент конверсии на момент заказа"
|
||||
)
|
||||
quantity_in_base_units = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name="Количество в базовых единицах",
|
||||
help_text="Количество в единицах хранения товара (для списания со склада)"
|
||||
)
|
||||
|
||||
# Временные метки
|
||||
created_at = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
@@ -159,11 +193,22 @@ class OrderItem(models.Model):
|
||||
|
||||
# Автоматически фиксируем цену при создании, если она не указана
|
||||
if not self.price:
|
||||
if self.product:
|
||||
# Сначала проверяем единицу продажи
|
||||
if self.sales_unit:
|
||||
self.price = self.sales_unit.actual_price
|
||||
elif self.product:
|
||||
self.price = self.product.actual_price
|
||||
elif self.kit_snapshot:
|
||||
self.price = self.kit_snapshot.actual_price
|
||||
|
||||
# Сохраняем snapshot единицы продажи
|
||||
if self.sales_unit:
|
||||
self.unit_name_snapshot = self.sales_unit.name
|
||||
self.conversion_factor_snapshot = self.sales_unit.conversion_factor
|
||||
self.quantity_in_base_units = self.sales_unit.convert_to_base(
|
||||
Decimal(self.quantity)
|
||||
)
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_total_price(self):
|
||||
|
||||
Reference in New Issue
Block a user