-- ==========================================
-- 작업일자: YYYY-MM-DD [쿼리 작성 및 실행 날짜를 기록합니다.]
-- 작 성 자: [실행자 이름을 기록합니다.]
-- 작업목적: [기간이 긴 경우 많은 데이터를 조회할 때 실무자의 요청에 의해 조회하여 엑셀파일로 제공합니다.]
-- 요청자/티켓번호: [요청 부서 / 요청자 또는 이슈 번호를 기록합니다.]
-- ==========================================
declare
	  @f_year  			CHAR(4)      
	, @f_month 			CHAR(2)    
	, @t_year  			CHAR(4)  
	, @t_month 			CHAR(2)
	, @pjt_no			VARCHAR(8)
	, @suju_bumun_dae	VARCHAR(12) 
	, @suju_bumun_jung	VARCHAR(12) 

select @f_year = '2015', @f_month = '01', @t_year = '2020', @t_month = '12', @pjt_no = 'V121T606%', @suju_bumun_dae='%', @suju_bumun_jung='%'

SET NOCOUNT ON

	DECLARE @f_date DATETIME -- FROM Date
	DECLARE @t_date DATETIME -- To Date

	SET @f_date = @f_month + '/' + '01' + '/' + @f_year
	SET @t_date = @t_month + '/' + '01' + '/' + @t_year
	SET @t_date = DATEADD(month,1,@t_date)
	SET @t_date = @t_date - 1
	--print @f_date
	--print @t_date
	   
	IF(@f_date > @t_date)
	BEGIN
		RAISERROR ('FROM Date가 To Date 보다 큼니다. 확인하여 주십시요', 16, 1)
	--	RETURN -1
	END

	DECLARE @t_TAB TABLE (
		pjt_no   	CHAR(8)         	-- 공사번호
	,	yymm	CHAR(8)			-- 투입연월
	,	main	CHAR(4)
	,	sub		CHAR(4)
	,	bud_seq INT
	,	s_amt    	DECIMAL(12,0)   	-- 소요예산
	,	g_amt    	DECIMAL(12,0)   	-- 기성투입실적
	,	d_amt    	DECIMAL(12,0)   	-- 직접투입실적
	,	m_amt    DECIMAL(12,0)   	-- MD투입실적
	,	t_amt    	DECIMAL(12,0)   	-- 총투입실적
	)

	INSERT INTO 
		@t_TAB	
	-- 직접경비(소요예산, 투입실적)
	SELECT 
		pjt_no=A.pjt_no   -- 공사번호
		, yymm = SUBSTRING(CONVERT(CHAR(8),A.bud_date,112), 1,6)
		, main = A.main
		, sub = A.sub
		, bud_seq = A.bud_seq
		, s_amt=SUM(A.app_rate*A.amt)  -- 소요예산	
		, g_amt = 0
		, d_amt = SUM(CASE WHEN A.main = 'AB29' AND A.sub = 'A2B' THEN 0 ELSE A.app_rate*A.amt END)  -- 투입실적
		, m_amt = SUM(CASE WHEN A.main = 'AB29' AND A.sub = 'A2B' THEN A.app_rate*A.amt END)  -- 투입실적
		, t_amt=SUM(A.app_rate*A.amt)  -- 투입실적
	FROM
		HPOIMS.dbo.ys_direct_cost A
	JOIN
		HPOIMS.dbo.ys_project B
	ON
		A.pjt_no = B.pjt_no	
	WHERE 
		A.bud_date >= @f_date 
		AND A.bud_date <= @t_date
		AND ISNULL(A.cont_no,'') = '' -- 기성처리분은 제외시키다.
		AND ISNULL(A.ctr_yn,'') = 'Y' -- 통제분만 포함 시킨다.
		AND A.pjt_no LIKE @pjt_no + '%'	-- 2007.03.07 Add
		AND ISNULL(B.suju_bumun_dae,'') LIKE @suju_bumun_dae + '%'
		AND ISNULL(B.suju_bumun_jung,'') LIKE @suju_bumun_jung + '%'
	GROUP BY 
		A.pjt_no, SUBSTRING(CONVERT(CHAR(8),A.bud_date,112), 1,6), A.main, A.sub, A.bud_seq

	INSERT INTO 
		@t_TAB
	-- 기성신청정보
	SELECT 
		pjt_no=A.pjt_no
		, yymm = A.year+A.month
		, main = A.main
		, sub = A.sub
		, bud_seq = A.bud_seq
		, s_amt=0
		, g_amt=SUM(gs_amt)
		, d_amt=0
		, m_amt = 0 
		, t_amt=SUM(gs_amt)
	FROM 
		HPOIMS.dbo.uvw_gs_po_input A
	JOIN
		HPOIMS.dbo.ys_project B
	ON
		A.pjt_no = B.pjt_no	
	WHERE 
		(A.month+'/28/'+ A.year) >= @f_date 
		AND (A.month+'/28/'+ A.year) <= @t_date
		AND A.pjt_no LIKE @pjt_no + '%'	-- 2007.03.07 Add
		AND ISNULL(B.suju_bumun_dae,'') LIKE @suju_bumun_dae + '%'
		AND ISNULL(B.suju_bumun_jung,'') LIKE @suju_bumun_jung + '%'
	GROUP BY 
		A.pjt_no, A.year+A.month, A.main, A.sub, A.bud_seq

	INSERT INTO @t_TAB
	-- 외자입고정보
	SELECT 
		pjt_no=A.pjt_no
		, yymm = A.year+A.month
		, main = A.main
		, sub = A.sub
		, bud_seq = A.bud_seq
		, s_amt=0
		, g_amt=SUM(A.rec_amt)
		, d_amt=0
		, m_amt=0
		, t_amt=SUM(A.rec_amt)
	FROM 
		HPOIMS.dbo.uvw_im_po_input A
	JOIN
		HPOIMS.dbo.ys_project B
	ON
		A.pjt_no = B.pjt_no	
	WHERE 
		(A.month+'/28/'+ A.year) >= @f_date 
		AND (A.month+'/28/'+ A.year) <= @t_date
		AND A.pjt_no LIKE @pjt_no + '%'	-- 2007.03.07 Add
		AND ISNULL(B.suju_bumun_dae,'') LIKE @suju_bumun_dae + '%'
		AND ISNULL(B.suju_bumun_jung,'') LIKE @suju_bumun_jung + '%'
	GROUP BY 
		A.pjt_no, A.year+A.month, A.main, A.sub, A.bud_seq

	SELECT 
		[공사번호]=A.pjt_no
	,	[공사명]  =(SELECT x.pjt_name FROM HPOIMS.dbo.ys_project x WHERE x.pjt_no = A.pjt_no)
	,	[연월] = A.yymm
	, 	A.main, A.sub, A.bud_seq
	,	[예산명]=(select item_name from HPOIMS.dbo.ys_pbudget where A.pjt_no = pjt_no and A.main = main and A.sub = replace(sub,' ', '') and A.bud_seq = bud_seq)
	,	[총투입금액]=ISNULL(A.t_amt,0)
	,	[부문]=(B.bumun_gbn )
	FROM 
	(
		SELECT 
			pjt_no=x.pjt_no
			, 	yymm = x.yymm
			, 	main = x.main
			, 	sub = x.sub
			, 	bud_seq = x.bud_seq
			 ,	s_amt=SUM(x.s_amt)
			 ,	g_amt=SUM(x.g_amt)
			 ,	d_amt=SUM(x.d_amt)
			 ,	m_amt=SUM(x.m_amt)
			 ,	t_amt=SUM(x.t_amt)
		 FROM 
			@t_TAB x
		 GROUP BY 
			x.pjt_no, x.yymm, x.main, x.sub, x.bud_seq
	) A
	LEFT OUTER JOIN
	(
		SELECT pjt_no, bumun_gbn=(SELECT fname FROM SYS.dbo.cm_codes WHERE type = 'YS14' and code = bumun_gbn) FROM HPOIMS.dbo.ys_project WHERE bumun_gbn IS NOT NULL
	) B
	ON
		A.pjt_no = B.pjt_no
	ORDER BY	
		A.pjt_no, A.yymm, A.main, A.sub, A.bud_seq


SET NOCOUNT OFF