New alert service
This commit is contained in:
@@ -11,7 +11,7 @@ from typing import Dict, Any
|
||||
from decimal import Decimal
|
||||
import structlog
|
||||
|
||||
from shared.messaging.rabbitmq import RabbitMQClient
|
||||
from shared.messaging import RabbitMQClient
|
||||
from app.core.database import database_manager
|
||||
from app.repositories.stock_repository import StockRepository
|
||||
from app.repositories.stock_movement_repository import StockMovementRepository
|
||||
@@ -137,6 +137,23 @@ class DeliveryEventConsumer:
|
||||
|
||||
# Create a new stock batch entry for this delivery
|
||||
# The Stock model uses batch tracking - each delivery creates a new batch entry
|
||||
# Extract unit cost from delivery item
|
||||
unit_cost = Decimal('0')
|
||||
try:
|
||||
if 'unit_cost' in item:
|
||||
unit_cost = Decimal(str(item['unit_cost']))
|
||||
elif 'unit_price' in item:
|
||||
unit_cost = Decimal(str(item['unit_price']))
|
||||
elif 'price' in item:
|
||||
unit_cost = Decimal(str(item['price']))
|
||||
except (ValueError, TypeError, KeyError) as e:
|
||||
logger.warning("Could not extract unit cost from delivery item for stock entry",
|
||||
item_id=item.get('id'),
|
||||
error=str(e))
|
||||
|
||||
# Calculate total cost
|
||||
total_cost = unit_cost * accepted_quantity
|
||||
|
||||
stock_data = {
|
||||
'tenant_id': tenant_id,
|
||||
'ingredient_id': ingredient_id,
|
||||
@@ -153,9 +170,9 @@ class DeliveryEventConsumer:
|
||||
'received_date': datetime.fromisoformat(received_at.replace('Z', '+00:00')) if received_at else datetime.now(timezone.utc),
|
||||
'expiration_date': datetime.fromisoformat(item.get('expiry_date').replace('Z', '+00:00')) if item.get('expiry_date') else None,
|
||||
|
||||
# Cost (TODO: Get actual unit cost from delivery item if available)
|
||||
'unit_cost': Decimal('0'),
|
||||
'total_cost': Decimal('0'),
|
||||
# Cost - extracted from delivery item
|
||||
'unit_cost': unit_cost,
|
||||
'total_cost': total_cost,
|
||||
|
||||
# Production stage - default to raw ingredient for deliveries
|
||||
'production_stage': 'raw_ingredient',
|
||||
@@ -182,12 +199,26 @@ class DeliveryEventConsumer:
|
||||
from app.models.inventory import StockMovementType
|
||||
from app.schemas.inventory import StockMovementCreate
|
||||
|
||||
# Extract unit cost from delivery item or default to 0
|
||||
unit_cost = Decimal('0')
|
||||
try:
|
||||
if 'unit_cost' in item:
|
||||
unit_cost = Decimal(str(item['unit_cost']))
|
||||
elif 'unit_price' in item:
|
||||
unit_cost = Decimal(str(item['unit_price']))
|
||||
elif 'price' in item:
|
||||
unit_cost = Decimal(str(item['price']))
|
||||
except (ValueError, TypeError, KeyError) as e:
|
||||
logger.warning("Could not extract unit cost from delivery item",
|
||||
item_id=item.get('id'),
|
||||
error=str(e))
|
||||
|
||||
movement_data = StockMovementCreate(
|
||||
ingredient_id=ingredient_id,
|
||||
stock_id=stock.id,
|
||||
movement_type=StockMovementType.PURCHASE,
|
||||
quantity=float(accepted_quantity),
|
||||
unit_cost=Decimal('0'), # TODO: Get from delivery item
|
||||
unit_cost=unit_cost,
|
||||
reference_number=f"DEL-{delivery_id}",
|
||||
reason_code='delivery',
|
||||
notes=f"Delivery received from PO {po_id}. Batch: {item.get('batch_lot_number', 'N/A')}",
|
||||
|
||||
@@ -9,7 +9,7 @@ from typing import Dict, Any
|
||||
import json
|
||||
|
||||
from app.services.internal_transfer_service import InternalTransferInventoryService
|
||||
from shared.messaging.rabbitmq import RabbitMQClient
|
||||
from shared.messaging import RabbitMQClient
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user