SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.usp_GetTransactionsByMachineExt') IS NOT NULL
   DROP PROCEDURE dbo.usp_GetTransactionsByMachineExt
GO
-- =============================================
-- Author:		Shyang Tan
-- Create date: 10/28/2013
-- Description:	
-- =============================================
CREATE PROCEDURE usp_GetTransactionsByMachineExt 
	-- Add the parameters for the stored procedure here
	@startDT datetime = 0, 
	@endDT datetime = 0	
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', t.Bin as 'Bin', u.[Name] as 'UsersName', c.Id as 'CardsId',
				p.PartNum as 'ProductsPartNum', p.Description as 'ProductsDescription',
				t.Quantity as 'Quantity', v.Name as 'Vendor',
				p.PackageQty as 'ProductsPackageQty', p.Price as 'ProductsPrice',
				t.TransactionDT as 'TransactionsTransactionDT'
	FROM		Transactions t
					JOIN Machines m on m.MachineNo = t.MachineNo
					JOIN Products p on p.ProductNo = t.ProductNo
					JOIN Cards c on c.CardNo = t.CardNo
					LEFT JOIN Users u on u.CardNo = c.CardNo
					LEFT JOIN Vendors v on v.VendorNo = p.VendorNo
	WHERE		t.TransactionDT >= @startDT AND t.TransactionDT <= @endDT
	GROUP BY	m.Id, u.[Name], c.Id, t.Bin, p.PartNum, p.Description, v.Name,
				t.Quantity, p.PackageQty, p.Price, t.TransactionDT
	ORDER BY	m.Id, t.TransactionDT

END
GO
