SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.usp_GetReorderByProduct') IS NOT NULL
   DROP PROCEDURE dbo.usp_GetReorderByProduct
GO
-- =============================================
-- Author:		Shyang Tan
-- Create date: 6/11/2010
-- Description:	
-- =============================================
CREATE PROCEDURE usp_GetReorderByProduct 
	-- 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',
				p.[PartNum] as 'ProductsPartNum',
				p.[Description] as 'ProductsDescription',
				mb.[Bin] as 'MachineBinsBin', mb.[MinQty] as 'MachineBinsMinQty', 
				mb.[Capacity] as 'MachineBinsMaxQty',
				mb.[CurrentQty] as 'MachineBinsCurrentQty',
				p.[PackageQty] as 'ProductsPackageQty',
				p.[OrderQty] as 'ProductsOrderQty',
			    p.[Price] as 'ProductsPrice'

	FROM		MachineBins mb
					JOIN Machines m ON m.[MachineNo] = mb.[MachineNo]
					JOIN Products p ON p.[ProductNo] = mb.[ProductNo]
	WHERE		m.[Active] = 1 AND
				p.[ProductNo] <> 1
	ORDER BY	m.[Id], p.[PartNum], mb.[Bin]


END
GO
