Improve the frontend modals

This commit is contained in:
Urtzi Alfaro
2025-10-27 16:33:26 +01:00
parent 61376b7a9f
commit 858d985c92
143 changed files with 9289 additions and 2306 deletions

View File

@@ -10,6 +10,7 @@ import uuid
from typing import List, Dict, Any, Optional
from uuid import UUID
from datetime import datetime, timedelta, timezone
from decimal import Decimal
import structlog
from apscheduler.triggers.cron import CronTrigger
from sqlalchemy import text
@@ -19,6 +20,8 @@ from shared.alerts.templates import format_item_message
from app.repositories.stock_repository import StockRepository
from app.repositories.stock_movement_repository import StockMovementRepository
from app.repositories.inventory_alert_repository import InventoryAlertRepository
from app.schemas.inventory import StockMovementCreate
from app.models.inventory import StockMovementType
logger = structlog.get_logger()
@@ -780,18 +783,35 @@ class InventoryAlertService(BaseAlertService, AlertServiceMixin):
# 1. Mark the stock batch as expired
await stock_repo.mark_batch_as_expired(stock.id, tenant_id)
# 2. Create waste stock movement
await movement_repo.create_automatic_waste_movement(
# 2. Get current stock level before this movement
current_stock = await stock_repo.get_total_stock_by_ingredient(tenant_id, stock.ingredient_id)
quantity_before = current_stock['total_available']
quantity_after = quantity_before - stock.current_quantity
# 3. Create waste stock movement with proper quantity tracking
await movement_repo.create_movement(
movement_data=StockMovementCreate(
tenant_id=tenant_id,
ingredient_id=stock.ingredient_id,
stock_id=stock.id,
movement_type=StockMovementType.WASTE,
quantity=stock.current_quantity,
unit_cost=Decimal(str(stock.unit_cost)) if stock.unit_cost else None,
quantity_before=quantity_before,
quantity_after=quantity_after,
reference_number=f"AUTO-EXPIRE-{stock.batch_number or stock.id}",
reason_code='expired',
notes=f"Lote automáticamente marcado como caducado. Vencimiento: {effective_expiration_date.strftime('%Y-%m-%d')}",
movement_date=datetime.now(),
created_by=None
),
tenant_id=tenant_id,
ingredient_id=stock.ingredient_id,
stock_id=stock.id,
quantity=stock.current_quantity,
unit_cost=float(stock.unit_cost) if stock.unit_cost else None,
batch_number=stock.batch_number,
expiration_date=effective_expiration_date,
created_by=None # Automatic system operation
created_by=None
)
# 4. Update the stock quantity to 0 (moved to waste)
await stock_repo.update_stock_to_zero(stock.id, tenant_id)
# 3. Update the stock quantity to 0 (moved to waste)
await stock_repo.update_stock_to_zero(stock.id, tenant_id)