
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		marek
-- Create date: 2009/04/16
-- Description:	Faktury dla partnerow - Poznan, Katowice
-- =============================================
ALTER PROCEDURE [dbo].[sp_FindPartnerInvoices]
	@year int,
	@month int,
	@partner varchar(3)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

SELECT DISTINCT 
F.ID_FAKTURY AS Id,
F.NUMER AS Number, 
F.NUMER_ROZ AS AgentShortCut, 
F.NUMER_ROK AS [Year], 
F.DATA_WYSTAWIENIA AS Date, 
CAST(COALESCE(F.suma_zaplat, 0) AS DECIMAL(9,2)) AS TotalPaid, 
F.KOREKTA AS Correction, 
F.waluta_miano AS Currency,  
F.EKSPORT AS Exported, 
FPK.NrTygodnia AS WeekNumber, 
CAST(Sum(FPK.Netto) AS DECIMAL(9,2)) AS Amount
FROM FAKTURY AS F 
LEFT OUTER JOIN FakturyPoznanKatowice AS FPK ON FPK.ID = F.ID_FAKTURY
WHERE 
F.NUMER_ROZ = @partner 
AND DATEPART(YEAR, F.DATA_WYSTAWIENIA)=@year 
AND DATEPART(MONTH, F.DATA_WYSTAWIENIA)=@month
GROUP BY
F.NUMER, 
F.NUMER_ROZ, 
F.NUMER_ROK, 
F.DATA_WYSTAWIENIA, 
F.ZAPLACONO, 
F.suma_zaplat, 
F.KOREKTA, 
F.waluta_miano, 
F.EKSPORT, 
FPK.NrTygodnia, 
F.ID_FAKTURY

END
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

