-- ==========================================
-- 작업일자: YYYY-MM-DD [쿼리 작성 및 실행 날짜를 기록합니다.]
-- 작 성 자: [실행자 이름을 기록합니다.]
-- 작업목적: [예산승인오류가 발생했을 시 확인합니다.]
-- 요청자/티켓번호: [예산승인부서의 요청시 검토하는 용도입니다.]
-- 데이타베이스 : MS-SQL
-- ==========================================

declare	@pjt_no			CHAR(8)
,	@bud_gbn		CHAR(1)
,	@chasu			TINYINT
,	@update_id		CHAR(7)
,	@change_why_gbn	CHAR(7)
,   @cnt int

	DECLARE @tmp_pbudget TABLE
	(
		pjt_no		CHAR(8)
		, main		CHAR(4)
		, sub		CHAR(4)
		, bud_seq	INT
		, x_amt		DECIMAL
	)

	select @pjt_no = 'V172T305', @bud_gbn='M'

	-- POR
	INSERT
		@tmp_pbudget
	SELECT 
		a.pjt_no, a.main, a.sub, a.bud_seq, isnull(SUM(b.amt),0) x_amt -- SUM(b.w_amt) 
	FROM 
		HPOIMS.dbo.gm_por_detail a
	LEFT OUTER JOIN
	(
		SELECT 
			por_no, por_seq, SUM(amt) amt, SUM(w_amt) w_amt , SUM(qty) qty 
		FROM 
			HPOIMS.dbo.gm_po_detail 
		GROUP BY 
			por_no, por_seq 
	)b
	ON
		a.por_no = b.por_no and a.por_seq = b.por_seq
	WHERE 
		a.pjt_no = @pjt_no
--		AND a.qty = b.qty
		AND a.w_amt > b.w_amt
	GROUP BY
		a.pjt_no, a.main, a.sub, a.bud_seq

	INSERT
		@tmp_pbudget
	SELECT 
		a.pjt_no, a.main, a.sub, a.bud_seq, isnull(SUM(b.w_amt),0) x_amt -- SUM(b.w_amt) 
	FROM 
		HPOIMS.dbo.gm_por_detail a
	LEFT OUTER JOIN
	(
		SELECT 
			por_no, por_seq, SUM(amt) amt, SUM(w_amt) w_amt , SUM(qty) qty 
		FROM 
			HPOIMS.dbo.gm_po_detail 
		GROUP BY 
			por_no, por_seq 
	)b
	ON
		a.por_no = b.por_no AND a.por_seq = b.por_seq
	WHERE 
		a.pjt_no = @pjt_no
--		AND a.qty = b.qty
		AND a.w_amt <= b.w_amt
	GROUP BY
		a.pjt_no, a.main, a.sub, a.bud_seq

	INSERT
		@tmp_pbudget
	SELECT 
		a.pjt_no, a.main, a.sub, a.bud_seq, isnull(SUM(b.w_amt),0) x_amt
	FROM 
		HPOIMS.dbo.gm_por_detail a
	LEFT OUTER JOIN
	(
		SELECT 
			por_no, por_seq, SUM(amt) amt, SUM(w_amt) w_amt , SUM(qty) qty 
		FROM 
			HPOIMS.dbo.gm_po_detail 
		GROUP BY 
			por_no, por_seq 
	)b
	ON
		a.por_no = b.por_no AND a.por_seq = b.por_seq
	WHERE 
		a.pjt_no = @pjt_no
		AND b.w_amt IS NULL
	GROUP BY
		a.pjt_no, a.main, a.sub, a.bud_seq


	-- 직접비	
	INSERT
		@tmp_pbudget
	SELECT 
		a.pjt_no, a.main, a.sub, a.bud_seq, SUM(a.amt*a.base_rate) x_amt
	FROM 
		HPOIMS.dbo.ys_direct_cost a
	WHERE 
		a.pjt_no = @pjt_no
	GROUP BY
		a.pjt_no, a.main, a.sub, a.bud_seq


	SELECT 
		@cnt = COUNT(*)
	FROM 
	(
		SELECT pjt_no, main, sub, bud_seq, SUM(x_amt) x_amt FROM @tmp_pbudget GROUP BY pjt_no, main, sub, bud_seq
	)b
	LEFT OUTER JOIN 
		HPOIMS.dbo.ys_mbudget a
	ON
		a.pjt_no = b.pjt_no 
		AND a.main = b.main 
		AND a.sub = b.sub 
		AND a.bud_seq = b.bud_seq
		AND a.bud_gbn = @bud_gbn -- 'M'
	WHERE 
		b.pjt_no = @pjt_no 
		AND ISNULL(a.w_amt,0) < ISNULL(b.x_amt,0)
		AND b.main+b.sub not in ('AB33001', 'AB33002')	-- 원가안분 인 경우에는 제외(2011.05.04)

	SELECT 
		b.pjt_no, b.main, b.sub, b.bud_seq, ISNULL(a.w_amt,0) m_amt, ISNULL(b.x_amt,0) x_amt
	FROM 
	(
		SELECT pjt_no, main, sub, bud_seq, SUM(x_amt) x_amt FROM @tmp_pbudget GROUP BY pjt_no, main, sub, bud_seq
	)b
	LEFT OUTER JOIN 
		HPOIMS.dbo.ys_mbudget a
	ON
		a.pjt_no = b.pjt_no 
		AND a.main = b.main 
		AND a.sub = b.sub 
		AND a.bud_seq = b.bud_seq
		AND a.bud_gbn = @bud_gbn -- 'M'
	WHERE 
		b.pjt_no = @pjt_no 
		AND ISNULL(a.w_amt,0) < ISNULL(b.x_amt,0)
		AND b.main+b.sub not in ('AB33001', 'AB33002')	-- 원가안분 인 경우에는 제외(2011.05.04)
		