set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 2012-07-05
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[usp_GetTransactionsByMachine] 
	-- 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', u.[Name] as 'UsersName', c.Id as 'CardsId',
				p.PartNum as 'ProductsPartNum', p.Description as 'ProductsDescription',
				t.Quantity as 'Quantity',
				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
	WHERE		t.TransactionDT >= @startDT AND t.TransactionDT <= @endDT
	GROUP BY	m.Id, u.[Name], c.Id, p.PartNum, p.Description,
				t.Quantity, p.PackageQty, p.Price, t.TransactionDT
	ORDER BY	m.Id, t.TransactionDT
END


