/* =========================================================
   VERIFICACIÓN STORY #9
   Expedición -> EnTransito + reservas de stock
   ========================================================= */

SET NOCOUNT ON;

DECLARE @ExpeditionNumber VARCHAR(50) = 'X-00000054'; -- cambiar

DECLARE @ExpeditionId INT;

SELECT @ExpeditionId = id
FROM dbo.PhLSM_ExpeditionHeaders
WHERE expeditionnumber = @ExpeditionNumber;

IF @ExpeditionId IS NULL
BEGIN
    RAISERROR('Expedición no encontrada',16,1);
    RETURN;
END

PRINT 'ExpeditionId: ' + CAST(@ExpeditionId AS VARCHAR);

-------------------------------------------------------------
-- 1. CABECERA
-------------------------------------------------------------
PRINT '===== CABECERA =====';

SELECT
    id,
    expeditionnumber,
    issuedate,
    status,
    observations
FROM dbo.PhLSM_ExpeditionHeaders
WHERE id = @ExpeditionId;


-------------------------------------------------------------
-- 2. DETALLES
-------------------------------------------------------------
PRINT '===== DETALLES =====';

SELECT
    d.id,
    d.expedition_id,
    d.product_id,
    d.stockitem_id,
    d.quantity,
    d.batch,
    d.serial,
    d.expiration
FROM dbo.PhLSM_ExpeditionDetails d
WHERE d.expedition_id = @ExpeditionId
ORDER BY d.id;


-------------------------------------------------------------
-- 3. RESERVAS CREADAS
-------------------------------------------------------------
PRINT '===== RESERVAS =====';

SELECT
    r.id,
    r.source_type,
    r.source_id,
    r.stockitem_id,
    r.reserved_quantity,
    r.status,
    r.createdat
FROM dbo.PhLSM_StockReservation r
WHERE r.source_type = 1
AND r.source_id = @ExpeditionId
ORDER BY r.id;


-------------------------------------------------------------
-- 4. STOCK ITEMS AFECTADOS
-------------------------------------------------------------
PRINT '===== STOCK ITEMS =====';

SELECT
    si.id,
    si.product_id,
    si.location_id,
    si.quantity,
    si.reserved_quantity,
    (si.quantity - si.reserved_quantity) AS available
FROM dbo.PhLSM_StockItem si
WHERE si.id IN
(
    SELECT stockitem_id
    FROM dbo.PhLSM_ExpeditionDetails
    WHERE expedition_id = @ExpeditionId
);


-------------------------------------------------------------
-- 5. VALIDACIÓN DETALLE VS RESERVA
-------------------------------------------------------------
PRINT '===== VALIDACION =====';

SELECT
    d.id AS detail_id,
    d.stockitem_id,
    d.quantity AS requested,
    r.reserved_quantity,
    CASE
        WHEN r.id IS NULL THEN 'FALTA RESERVA'
        WHEN r.reserved_quantity <> d.quantity THEN 'CANTIDAD DISTINTA'
        ELSE 'OK'
    END AS resultado
FROM dbo.PhLSM_ExpeditionDetails d
LEFT JOIN dbo.PhLSM_StockReservation r
    ON r.source_id = d.expedition_id
    AND r.stockitem_id = d.stockitem_id
    AND r.source_type = 1
    AND r.status = 1
WHERE d.expedition_id = @ExpeditionId;


-------------------------------------------------------------
-- 6. CHEQUEO DE DUPLICADOS
-------------------------------------------------------------
PRINT '===== DUPLICADOS =====';

SELECT
    stockitem_id,
    COUNT(*) AS cantidad
FROM dbo.PhLSM_StockReservation
WHERE source_type = 1
AND source_id = @ExpeditionId
AND status = 1
GROUP BY stockitem_id
HAVING COUNT(*) > 1;