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:
2026-01-02 02:09:44 +03:00
parent ca308ae2a2
commit 5b68f14bb4
11 changed files with 764 additions and 15 deletions

View File

@@ -0,0 +1,55 @@
# Generated by Django 5.0.10 on 2026-01-01 21:29
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('orders', '0002_initial'),
('products', '0006_populate_unit_of_measure'),
]
operations = [
migrations.AddField(
model_name='historicalorderitem',
name='conversion_factor_snapshot',
field=models.DecimalField(blank=True, decimal_places=6, help_text='Коэффициент конверсии на момент заказа', max_digits=15, null=True, verbose_name='Коэффициент конверсии (snapshot)'),
),
migrations.AddField(
model_name='historicalorderitem',
name='quantity_in_base_units',
field=models.DecimalField(blank=True, decimal_places=6, help_text='Количество в единицах хранения товара (для списания со склада)', max_digits=10, null=True, verbose_name='Количество в базовых единицах'),
),
migrations.AddField(
model_name='historicalorderitem',
name='sales_unit',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='products.productsalesunit', verbose_name='Единица продажи'),
),
migrations.AddField(
model_name='historicalorderitem',
name='unit_name_snapshot',
field=models.CharField(blank=True, default='', help_text='Название единицы продажи на момент заказа', max_length=100, verbose_name='Название единицы (snapshot)'),
),
migrations.AddField(
model_name='orderitem',
name='conversion_factor_snapshot',
field=models.DecimalField(blank=True, decimal_places=6, help_text='Коэффициент конверсии на момент заказа', max_digits=15, null=True, verbose_name='Коэффициент конверсии (snapshot)'),
),
migrations.AddField(
model_name='orderitem',
name='quantity_in_base_units',
field=models.DecimalField(blank=True, decimal_places=6, help_text='Количество в единицах хранения товара (для списания со склада)', max_digits=10, null=True, verbose_name='Количество в базовых единицах'),
),
migrations.AddField(
model_name='orderitem',
name='sales_unit',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='order_items', to='products.productsalesunit', verbose_name='Единица продажи'),
),
migrations.AddField(
model_name='orderitem',
name='unit_name_snapshot',
field=models.CharField(blank=True, default='', help_text='Название единицы продажи на момент заказа', max_length=100, verbose_name='Название единицы (snapshot)'),
),
]

View File

@@ -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):