﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__r_getBalanceByMonth]
	@authuser varchar(100)
AS
BEGIN
	SET NOCOUNT ON;

    DECLARE @today date = GETDATE()
	DECLARE @tomorrow date = DATEADD(DAY,1,@today);

	IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
		THROW 60000, N'not authorized', 1;  


	DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];

	--Admin Table
	INSERT INTO @Admin
	VALUES (
		'bbm'
		,'Umsatz nach Monaten'
		,'Umsatz nach Monaten'
		,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
		,''
		,'table' --typ
		, ''  --settings
		, ''
		, 0)

	SELECT * FROM @Admin;

	WITH inv as (
		SELECT 
				[Dateofcreation] = i.[DateCreatEd]
				,i.[InvoiceBalance]
				,[InvoiceBalanceNetto] = i.[InvoiceBalance_net]
			FROM [dbo].[fds__invoices] as i 
			WHERE i.[isFinal] = 1
	)
	SELECT 
		[Monat] = FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de')
		, [∑ Umsätze] = FORMAT(  SUM(  ISNULL(inv.[InvoiceBalance],0) ), '#,0.00 €', 'de')
		, [style:∑ Umsätze]  = 'text-align: right'
		, [∑ Umsätze_netto] = FORMAT(  SUM(  ISNULL(inv.[InvoiceBalanceNetto],0) ), '#,0.00 €', 'de')
		, [style:∑ Umsätze_netto]  = 'text-align: right'
	FROM inv
	GROUP BY FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de');

END