﻿
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__r_getBalanceTrendByYear]
	@year int,
	
	@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;   

	set @year= ISNULL(@year, Year(@year));


	DECLARE @Admin [dbo].[fds__tt__admin_ReportAdminTable];

	--Admin Table
	INSERT INTO @Admin
	VALUES (
		'bbm'
		,'UmsatzTrend für Jahr ' + FORMAT(@year, '0000')
		,'UmsatzTrend für Jahr ' + FORMAT(@year, '0000')
		,'' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm')
		,''
		,'chart' --typ
		, '{ "charttype": "line", "x1_label_angle": -90, "color": "e0301e", "linewidth": 2, "valuelabels": false, "marker": "none", "colorpalette": "00B050;00B050;00B050", "x1_column": "date", "y1_column": "Value", "y1_title": "Actuals", "x1_title": "", "series_column": "series", "legend_position": "bottom", "show_datalabel": true, "datalabelfontsizescale": 0.6, "height": 600, "width": 1200, "x1_labelformat": "ddd, dd/MM"}'  --settings
		, ''
		, 0)

	SELECT * FROM @Admin;




	with inv as(
		select 
			[InvoiceBalance] = SUM(  ISNULL(_i.[InvoiceBalance],0) )
			,[Dateofcreation] = CAST([Datecreated] as date)
			,[seq] = ROW_NUMBER() OVER (ORDER BY CAST([Datecreated] as date))
		FROM [dbo].[fds__invoices] as _i where _i.IsFinal = 1 and YEAR([Datecreated]) = @year
		GROUP BY CAST([Datecreated] as date)
	)
	SELECT 
		--[Monat] = FORMAT( inv.[Dateofcreation], 'yyyy - MM', 'de')
			--,[Umsatz] = FORMAT(  ISNULL(inv.[InvoiceBalance],0) , '#0.00', 'de')
			[series] = 'Umsatz'
			,[date]		= CAST(inv.[Dateofcreation] as date)
			,[value]	= CAST( SUM(  ISNULL(inv.[InvoiceBalance],0) ) OVER (ORDER BY [seq]) as numeric(9,2))
	FROM inv
	ORDER BY [seq]

END