-- dbo.ef_Vyroba_VratCisloSarze
CREATE FUNCTION dbo.ef_Vyroba_VratCisloSarze (@Datum DATETIME=NULL, @Typ TINYINT=1, @iso BIT=1)
RETURNS NVARCHAR(5)
AS
BEGIN

DECLARE
 @smena TINYINT=0,
 @pracDen NCHAR(1)=N'',
 @tyden NCHAR(2)=N'',
 @retVal NVARCHAR(5),
 @den TINYINT,
 @NYden TINYINT


SET @Datum = ISNULL(@Datum, GETDATE())
SELECT @smena=Smena, @Datum=Datum FROM dbo.ef_GetDatumASmenu (@Datum)

SET @NYden = DATEPART(weekday, '20240101') -- pondeli

--SET @pracDen = CONVERT(nchar(1), (DATEPART(weekday, @Datum) + @@DATEFIRST - 2) % 7 + 1)
SET @den = DATEPART(weekday, @Datum)
IF (@NYden=1) -- system ma nastaveno pondeli=1, Rootvin potrebuje nedele=1
 BEGIN
  IF (@den=7) -- nedele=7 -> nedele=1
    SET @den = 1
   ELSE
    SET @den = @den + 1
 END
SET @pracDen = CONVERT(nchar(1), @den)


IF (@typ=1)
 SET @tyden = IIF(@iso=0, FORMAT(DATEPART(week, @Datum), '00', 'cs-cz'), FORMAT(DATEPART(iso_week, @Datum), '00', 'cs-cz'))

IF (@typ=2)
 SET @tyden = IIF(@iso=0, FORMAT(DATEPART(week, @Datum), '00', 'en-gb'), FORMAT(DATEPART(iso_week, @Datum), '00', 'en-gb'))


SET @retVal = RIGHT(FORMAT(@Datum, 'yy'), 1) + @tyden + @pracDen + FORMAT(@smena, '0')
RETURN @retVal

END