
/****** Object:  UserDefinedFunction [dbo].[FARSI_DATE3]    Script Date: 28/11/1404 03:08:51 ب.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


--Usage: ([dbo].[FARSI_DATE2]([Date]))
-- DROP FUNCTION [dbo].[FARSI_DATE3]
CREATE FUNCTION [dbo].[FARSI_DATETIME]
(@DATE DATETIME) RETURNS CHAR(20)
WITH SCHEMABINDING
AS
BEGIN
	DECLARE @RYEAR  int 
	DECLARE @RMONTH int 
	DECLARE @RDAY   int 
	DECLARE @FARSIDate VARCHAR(25)
	DECLARE @TIME as VARCHAR(12)
	if @DATE < '2007-1-30'
		RETURN ''

	SET @TIME = ' '+RIGHT('0' + CAST(DATEPART(HOUR, @DATE) AS VARCHAR), 2) + ':' +
        RIGHT('0' + CAST(DATEPART(MINUTE, @DATE) AS VARCHAR), 2) + ':' +
        RIGHT('0' + CAST(DATEPART(SECOND, @DATE) AS VARCHAR), 2)
	Select @FARSIDate=PDATE From dbo.G2PDATE where GDATE>=@DATE and GDATE < DATEADD(DAY, DATEDIFF(DAY, -1, @DATE), 0)
	if not @FARSIDate is null 
		return @FARSIDate+@TIME
	DECLARE @FARSIMONTH CHAR(2)
	DECLARE @FARSIDAY CHAR(2)
	
    SET @RYEAR  = DATEPART(yyyy, @DATE)
    --SET @RYEAR  = DATEPART(yyyy, @DATE)-@RYEAR*100
	SET @RMONTH = DATEPART(mm,@DATE)
	SET @RDAY   = DATEPART(dd, @DATE)

--	SET @FARSIDATE=+dbo.MyStr(@RDAY,2)+'/'+dbo.MyStr(@RMONTH,2)+'/'+dbo.MyStr(@RYEAR,4)
--	RETURN(@FARSIDATE)

	DECLARE @SAL    int
	DECLARE @MAH    int
	DECLARE @ROZ    int

	DECLARE @a  Int
	DECLARE @c  Int
	DECLARE @Y  Int
	DECLARE @i  Int
	DECLARE @j  Int
	DECLARE @TEMP  INT
	
	SET @A=79
	
	SET @Y = @RYear
	
	IF (@Y < 1964) Or (@Y > 2028)
	BEGIN
		RETURN('0000/00/00')
	END
	ELSE
	BEGIN
		IF @Y > 1963 And @Y < 1996 
		BEGIN
		    IF (@Y % 4) = 0 
		    BEGIN
		        SET @a = @a + 1
		        SET @c = dbo.CheckTableFarsi_Date(2,@RMonth - 1) + @RDay
		    END
		    Else
		    BEGIN
		        SET @c = dbo.CheckTableFarsi_Date(1,@RMonth - 1) + @RDay
		    END
		END
		Else
		BEGIN
			If (@Y % 4) = 0
			BEGIN
		        SET @c = dbo.CheckTableFarsi_Date(3,@RMonth - 1) + @RDay
			END
			Else
			BEGIN
				If (@Y % 4) = 1 
				BEGIN
			        SET @a = @a + 1
			        SET @c = dbo.CheckTableFarsi_Date(4,@RMonth - 1) + @RDay
				END
				Else
				BEGIN
			        SET @c = dbo.CheckTableFarsi_Date(1,@RMonth - 1) + @RDay
				END
			END
		End
		
		If @c > @a 
		BEGIN
		    SET @i = 9
		    SET @c = @c - @a
		    SET @sal = @Y - 621
		    While dbo.CheckTableFarsi_Date(5,@I) > @c And @i > 0
			BEGIN
		        SET @i = @i - 1
		    END
		    If dbo.CheckTableFarsi_Date(5,@I) = @c And @i > 0 
			BEGIN
				SET @i = @i - 1
			END
		    SET @roz = @c - dbo.CheckTableFarsi_Date(5,@I)
		    SET @mah = @i + 1
		END
		Else
		BEGIN
		    SET @j = 2
		    SET @c = @c + 10
		    SET @sal = @Y - 622
		    While dbo.CheckTableFarsi_Date(6,@J) > @c And @j > 0
			BEGIN
		        SET @j = @j - 1
		    END
		    If dbo.CheckTableFarsi_Date(6,@J) = @c And @j > 0 
			BEGIN
				SET @j = @j - 1
			END
		    SET @roz = @c - dbo.CheckTableFarsi_Date(6,@J)
		    SET @mah = @j + 10
		END
	END

--	IF LEN(LTRIM(RTRIM(STR(@MAH)))) =1 
--		SET @FARSIMONTH='0'+LTRIM(RTRIM(STR(@MAH)))
--	ELSE
--		SET @FARSIMONTH=LTRIM(RTRIM(STR(@MAH)))

--	IF LEN(LTRIM(RTRIM(STR(@ROZ)))) =1 
--		SET @FARSIDAY='0'+LTRIM(RTRIM(STR(@ROZ)))
--	ELSE
--		SET @FARSIDAY=LTRIM(RTRIM(STR(@ROZ)))

--	SET @FARSIDATE=RTRIM(LTRIM(STR(@SAL)))+'/'+@FARSIMONTH+'/'+@FARSIDAY
        --not in 1400! set @SAL = @SAL % 100 -- remove GARN!

	--DECLARE @TarikhBaGharn bit
	--DECLARE @SALLen int
	--SET @SALLen=4
	--select @TarikhBaGharn=TheValue from AppSetting  where Name like '%Company.TarikhBaGharn%'
	--if @TarikhBaGharn<>1
	--BEGIN
	--	set @SAL = @SAL % 100 -- remove GARN!
	--	SET @SALLen=2
	--END

	--set @SAL=@SAL+1000

	if (@SAL=1385 and @MAH=1 and @ROZ=1) OR (@SAL=1384 and @MAH=1 and @ROZ=1)
		return ''
	else
	BEGIN
		SET @FARSIDATE=dbo.MyStr(@SAL,4)+'/'+dbo.MyStr(@MAH,2)+'/'+dbo.MyStr(@ROZ,2)
		RETURN(@FARSIDATE+@TIME)
	END
	return ''
END