USE [phronCare_OperationsHub]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Procedure: PhLSM_Stock_GetAvailabilityByStockItemIds
-- Module:    Logistics / Stock (PhLSM)
-- Purpose:   Returns quantity and availability data
--            for the requested stock items.
-- Author:    Leandro Rojas
-- Created:   2026-03-09
-- =============================================

CREATE OR ALTER PROCEDURE [dbo].[PhLSM_Stock_GetAvailabilityByStockItemIds]
(
    @StockItemIds dbo.PhLSM_StockItemIdList READONLY
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        si.id AS StockitemId,
        si.quantity AS Quantity,
        ISNULL(si.reserved_quantity, 0) AS ReservedQuantity,
        si.quantity - ISNULL(si.reserved_quantity, 0) AS AvailableQuantity,
        si.serial AS Serial
    FROM dbo.PhLSM_StockItem si
        INNER JOIN @StockItemIds ids
            ON ids.stockitem_id = si.id;
END
GO