SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.usp_GetLowBinInventory') IS NOT NULL
   DROP PROCEDURE dbo.usp_GetLowBinInventory
GO
-- =============================================
-- Author:		Eric Miles
-- Create date: 8/22/2008
-- Description:	
-- =============================================
CREATE PROCEDURE usp_GetLowBinInventory 
	-- Add the parameters for the stored procedure here
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT		m.[Id] as 'MachinesId',
				mb.[Bin] as 'MachineBinsBin', mb.[CurrentQty] as 'MachineBinsCurrentQty',
				mb.[MinQty] as 'MachineBinsMinQty',
				p.[PartNum] as 'ProductsPartNum', p.[Description] as 'ProductsDescription',
				p.[Price] as 'ProductsPrice', p.[OrderQty] as 'ProductsOrderQty',
				v.[Name] as 'VendorsName', v.[Phone] as 'VendorsPhone'
	FROM		MachineBins mb
					JOIN Machines m ON m.[MachineNo] = mb.[MachineNo]
					JOIN Products p ON p.[ProductNo] = mb.[ProductNo]
					LEFT OUTER JOIN Vendors v ON v.[VendorNo] = p.[VendorNo]
	WHERE		m.[Active] = 1 AND
				p.[ProductNo] <> 1 AND
				mb.[CurrentQty] < mb.[MinQty]
	ORDER BY	m.[Id], mb.[Bin]
END
GO
