feat(inventory): add support for selling in negative stock
Implement functionality to allow sales even when stock is insufficient, tracking pending quantities and resolving them when new stock arrives via incoming documents. This includes new fields in Sale model (is_pending_cost, pending_quantity), updates to batch manager for negative write-offs, and signal handlers for automatic processing. - Add is_pending_cost and pending_quantity fields to Sale model - Modify write_off_by_fifo to support allow_negative flag and return pending quantity - Update incoming document service to allocate pending sales to new batches - Enhance sale processor and signals to handle pending sales - Remove outdated tests.py file - Add migration for new Sale fields
This commit is contained in:
@@ -203,6 +203,13 @@ class IncomingDocumentService:
|
||||
batches_created.append(stock_batch)
|
||||
total_cost += item.total_cost
|
||||
|
||||
# Обрабатываем ожидающие продажи "в минус" для этого товара
|
||||
cls._process_pending_sales(
|
||||
product=item.product,
|
||||
warehouse=document.warehouse,
|
||||
new_batch=stock_batch
|
||||
)
|
||||
|
||||
# Обновляем или создаем запись в Stock
|
||||
stock, _ = Stock.objects.get_or_create(
|
||||
product=item.product,
|
||||
@@ -224,6 +231,58 @@ class IncomingDocumentService:
|
||||
'total_cost': total_cost
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _process_pending_sales(cls, product, warehouse, new_batch):
|
||||
"""
|
||||
Привязать ожидающие продажи "в минус" к новой партии по FIFO.
|
||||
Себестоимость берётся из этой партии.
|
||||
|
||||
Args:
|
||||
product: объект Product
|
||||
warehouse: объект Warehouse
|
||||
new_batch: объект StockBatch (только что созданная партия)
|
||||
"""
|
||||
from inventory.models import Sale, SaleBatchAllocation
|
||||
|
||||
# Ожидающие продажи по дате (старые первыми - FIFO)
|
||||
pending_sales = Sale.objects.filter(
|
||||
product=product,
|
||||
warehouse=warehouse,
|
||||
is_pending_cost=True,
|
||||
pending_quantity__gt=0
|
||||
).order_by('date')
|
||||
|
||||
available_qty = new_batch.quantity
|
||||
|
||||
for sale in pending_sales:
|
||||
if available_qty <= 0:
|
||||
break
|
||||
|
||||
qty_to_allocate = min(sale.pending_quantity, available_qty)
|
||||
|
||||
# Создаем SaleBatchAllocation с себестоимостью из приёмки
|
||||
SaleBatchAllocation.objects.create(
|
||||
sale=sale,
|
||||
batch=new_batch,
|
||||
quantity=qty_to_allocate,
|
||||
cost_price=new_batch.cost_price
|
||||
)
|
||||
|
||||
# Уменьшаем pending в Sale
|
||||
sale.pending_quantity -= qty_to_allocate
|
||||
if sale.pending_quantity <= 0:
|
||||
sale.is_pending_cost = False
|
||||
sale.save(update_fields=['pending_quantity', 'is_pending_cost'])
|
||||
|
||||
# Уменьшаем партию (товар уже был "продан" ранее)
|
||||
new_batch.quantity -= qty_to_allocate
|
||||
available_qty -= qty_to_allocate
|
||||
|
||||
# Сохраняем партию с оставшимся количеством
|
||||
if new_batch.quantity <= 0:
|
||||
new_batch.is_active = False
|
||||
new_batch.save(update_fields=['quantity', 'is_active'])
|
||||
|
||||
@classmethod
|
||||
@transaction.atomic
|
||||
def cancel_document(cls, document):
|
||||
|
||||
Reference in New Issue
Block a user