ALTER PROCEDURE CBKTTMTOPYPOTGAJIAMOUNT
(
	@EmployeeID VARCHAR(20)
	,@StartDate VARCHAR(20)
	,@EndDate VARCHAR(20)
)
AS

--DECLARE @EmployeeID VARCHAR(20) = '10000194';
--DECLARE @StartDate VARCHAR(20) = '20221001';
--DECLARE @EndDate VARCHAR(20) = '20221031';

DECLARE @Bulan DECIMAL(18,0)
DECLARE @Hari DECIMAL(18,0)

SELECT  @Bulan = DATEDIFF(MONTH, DATEADD(YEAR, 0 , MIN(pa18.StartDate)), MAX(pa18.EndDate)) - (CASE WHEN DATEADD(MONTH,DATEDIFF(MONTH, DATEADD(YEAR, 0 , MIN(pa18.StartDate)), MAX(pa18.EndDate)), DATEADD(YEAR, 0 , MIN(pa18.StartDate))) > MAX(pa18.EndDate) THEN 1 ELSE 0 END) 
		,@Hari = DATEDIFF(DAY, DATEADD(MONTH, @Bulan , MIN(pa18.StartDate)), MAX(pa18.EndDate)) - (CASE WHEN DATEADD(DAY, DATEDIFF(DAY, DATEADD(MONTH, @Bulan , MIN(pa18.StartDate)), MAX(pa18.EndDate)),  DATEADD(MONTH, @Bulan , MIN(pa18.StartDate))) > MAX(pa18.EndDate) THEN 1 ELSE 0 END)
		--pa18.EmployeeID, pa18.StartDate, pa18.EndDate
FROM PHRPA0018 AS pa18
WHERE pa18.AbsenceType = '05'
      AND pa18.EmployeeID = @EmployeeID
      AND pa18.EndDate <= @EndDate
GROUP BY pa18.EmployeeID
SELECT @Bulan, @Hari

DECLARE @SumMD4Amount DECIMAL(22,0)
SELECT DISTINCT  @SumMD4Amount = CAST(REPLACE(ISNULL(dbo.GetPEN(Amount), '0'), ',', '.') AS DECIMAL(22,0))
FROM dbo.PHRPA0004 AS pa4
WHERE pa4.EmployeeID = @EmployeeID
	AND pa4.StartDate <= @EndDate AND pa4.EndDate >= @EndDate

DECLARE @SumMD5Amount DECIMAL(22,0) = 0
SELECT DISTINCT  @SumMD5Amount = CAST(REPLACE(ISNULL(dbo.GetPEN(Amount), '0'), ',', '.') AS DECIMAL(22,0))
FROM dbo.PHRPA0005 AS pa5
WHERE pa5.EmployeeID = @EmployeeID
	AND pa5.StartDate <= @EndDate AND pa5.EndDate >= @EndDate

DECLARE @SumAmount DECIMAL(22,0) = (@SumMD4Amount + @SumMD5Amount)
DECLARE @PotGaji DECIMAL(22,0)
IF(@Bulan >= 12)
BEGIN
	IF(@Hari > 0)
	BEGIN
		SET @PotGaji =  @SumAmount/2
	END
	ELSE
	BEGIN
		SET @PotGaji = 0
	END
END
ELSE
BEGIN
	SET @PotGaji = 0
END

SELECT @PotGaji AS PotGaji

--/* rumus perhitungan bulan hari
----SELECT dbo.GetDateDiffTahunBulanHari('20211110', '20221001')
----DECLARE @FromDate VARCHAR(20) = '20211110'
----DECLARE @ToDate VARCHAR(20) = '20221001'
----DECLARE @Months INT, @Days INT, @tmpFromDate DATETIME
----SET @tmpFromDate = DATEADD(YEAR, 0 , @FromDate) SELECT @tmpFromDate
----SET @Months =  DATEDIFF(MONTH, DATEADD(YEAR, 0 , @FromDate), @ToDate) - (CASE WHEN DATEADD(MONTH,DATEDIFF(MONTH, DATEADD(YEAR, 0 , @FromDate), @ToDate), DATEADD(YEAR, 0 , @FromDate)) > @ToDate THEN 1 ELSE 0 END) 
----SELECT @Months
----SET @tmpFromDate = DATEADD(MONTH, @Months , @FromDate) SELECT @tmpFromDate
----SET @Days =  DATEDIFF(DAY, DATEADD(MONTH, @Months , @FromDate), @ToDate) - (CASE WHEN DATEADD(DAY, DATEDIFF(DAY, DATEADD(MONTH, @Months , @FromDate), @ToDate),  DATEADD(MONTH, @Months , @FromDate)) > @ToDate THEN 1 ELSE 0 END)
----SELECT @Days
--*/
