
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_adMotoRaportFaktury]
	@rok int,
	@miesiac int
AS
DECLARE @prowizjaPoz DECIMAL(9,4)
DECLARE @prowizjaKat money

exec sp_GetProcentProwizjiByAgencja 6, @rok, @miesiac, @prowizjaPoz OUTPUT
exec sp_GetProcentProwizjiByAgencja 4, @rok, @miesiac, @prowizjaKat OUTPUT


DECLARE @Wplywy TABLE
(
	IdFaktury INT,
	NumerFaktury INT,
	Agent nvarchar(100),
	SymbolFaktury nvarchar(100),
	DataWplaty DATETIME,
	Netto MONEY,
	DataFaktury DATETIME,
	Prowizja MONEY,
	Agencja nvarchar(100)
)

INSERT INTO @Wplywy
SELECT 
ID_FAKTURY AS IdFaktury, 
NumerFaktury, 
LOWER(Symbol) AS Agent, 
NR_FK AS SymbolFaktury, 
dataWplaty AS DataWplaty, 
NETTO AS Netto, 
DATA_WYSTAWIENIA AS DataFaktury, 
ROUND(NETTO * 0.5, 2) AS Prowizja, 
Agencja
FROM WplywyByTytul
WHERE (YEAR(dataWplaty) = @rok) 
AND (MONTH(dataWplaty) = @miesiac) 
AND Agencja='adMoto'



SELECT * FROM @Wplywy 
ORDER BY DataWplaty, Agent, NumerFaktury
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

