-- ==========================================
-- 작업일자: YYYY-MM-DD [쿼리 작성 및 실행 날짜를 기록합니다.]
-- 작 성 자: [실행자 이름을 기록합니다.]
-- 작업목적: [실행예산 소요예산 = 구매청구서 금액 + 직접비 금액 비교하여 다른 것 조회하여 내용 확인 후 수정합니다.]
-- 요청자/티켓번호: [사업부서의 요청 혹은 주기적으로 실행하여 데이터를 일치 시킵니다.]
-- 데이타베이스 : MS-SQL
-- ==========================================

--  [1] 원화인 경우 실행예산 소요예산 = 구매청구서 금액 + 직접비 금액 비교하여 다른 것 조회하기
select
	a.*
	, a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) x_amt
	, (b.soyo_amt+b.input_amt)
	, a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) - (b.soyo_amt+b.input_amt)
from
	HPOIMS.dbo.ys_pbudget a
join
(
	select 
		*
	from
	(
		select
			a.pjt_no, a.main, a.sub, a.bud_seq
			, sum(a.bud_amt) bud_amt
			, sum(a.bud_x_amt) bud_x_amt
			, sum(a.bud_e_amt) bud_e_amt
			, sum(a.soyo_amt) soyo_amt
			, sum(a.input_amt) input_amt
		from
		(
			SELECT 
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ISNULL(a.closing_yn,'N')
				, bud_amt = round(ISNULL(a.amt*a.base_rate,0),0)
				, bud_x_amt = round(ISNULL(a.x_amt*a.base_rate,0),0) 
				, bud_e_amt = round(ISNULL(a.e_amt*a.base_rate,0),0)
				, soyo_amt = 0
				, input_amt = 0      
			FROM 
				HPOIMS.dbo.ys_pbudget a
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
-- 				, soyo_amt = SUM(ISNULL(a.amt,0)*ISNULL(b.base_rate,1) + CASE WHEN b.fob IS NULL THEN 0 ELSE FLOOR(ISNULL(a.amt,0)*ISNULL(b.fob,1)*b.base_rate) END)
				, soyo_amt = SUM(ISNULL(a.w_amt,0))
				, input_amt = 0
			FROM 
				HPOIMS.dbo.gm_por_detail a
			left outer join
				HPOIMS.dbo.ys_project_rate b
			on
				a.pjt_no = b.pjt_no and a.curr = b.curr
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = 0
-- 				, input_amt = SUM(ISNULL(a.amt,0)*ISNULL(b.base_rate,1) + CASE WHEN b.fob IS NULL THEN 0 ELSE FLOOR(ISNULL(a.amt,0)*ISNULL(b.fob,1)*b.base_rate) END)
				, input_amt = SUM(ISNULL(a.amt,0))
			FROM 
				HPOIMS.dbo.ys_direct_cost a
			left outer join
				HPOIMS.dbo.ys_project_rate b
			on
				a.pjt_no = b.pjt_no and a.curr = b.curr
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
		) a
		GROUP BY 
			a.pjt_no, a.main, a.sub, a.bud_seq
	) a
-- 	where
-- 		(a.bud_x_amt*a.base_rate) <> (a.soyo_amt+a.input_amt)
		
) b
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.pjt_no like '%' 
 	and a.main <> 'AB00'
  	and a.curr = 'WON'
	and ROUND(isnull(a.x_amt,0)*a.base_rate,0) <> (b.soyo_amt+b.input_amt)

-- 	and a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0)
-- 		<> 
-- 		(b.soyo_amt+b.input_amt)
--	and abs((a.x_amt*a.base_rate) - (b.soyo_amt+b.input_amt)) > 1 
-- 	and a.item_name = 'ManDay 지원비'

--  [2] 외자인 경우 실행예산 소요예산 = 구매청구서 금액 + 직접비 금액 비교하여 다른 것 조회하기
select
	a.*
	, round(a.x_amt*a.base_rate,-1) + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) x_amt
	, (b.soyo_amt+b.input_amt)
	, a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) - (b.soyo_amt+b.input_amt)
from
	HPOIMS.dbo.ys_pbudget a
join
(
	select 
		*
	from
	(
		select
			a.pjt_no, a.main, a.sub, a.bud_seq
			, sum(a.bud_amt) bud_amt
			, sum(a.bud_x_amt) bud_x_amt
			, sum(a.bud_e_amt) bud_e_amt
			, sum(a.soyo_amt) soyo_amt
			, sum(a.input_amt) input_amt
		from
		(
			SELECT 
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ISNULL(a.closing_yn,'N')
				, bud_amt = round(ISNULL(a.amt*a.base_rate,0),0)
				, bud_x_amt = round(ISNULL(a.x_amt*a.base_rate,0),0) 
				, bud_e_amt = round(ISNULL(a.e_amt*a.base_rate,0),0)
				, soyo_amt = 0
				, input_amt = 0      
			FROM 
				HPOIMS.dbo.ys_pbudget a
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
-- 				, soyo_amt = SUM(ISNULL(a.amt,0)*ISNULL(b.base_rate,1) + CASE WHEN b.fob IS NULL THEN 0 ELSE FLOOR(ISNULL(a.amt,0)*ISNULL(b.fob,1)*b.base_rate) END)
				, soyo_amt = SUM(ISNULL(a.w_amt,0))
				, input_amt = 0
			FROM 
				HPOIMS.dbo.gm_por_detail a
			left outer join
				HPOIMS.dbo.ys_project_rate b
			on
				a.pjt_no = b.pjt_no and a.curr = b.curr
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = 0
-- 				, input_amt = SUM(ISNULL(a.amt,0)*ISNULL(b.base_rate,1) + CASE WHEN b.fob IS NULL THEN 0 ELSE FLOOR(ISNULL(a.amt,0)*ISNULL(b.fob,1)*b.base_rate) END)
				, input_amt = SUM(ISNULL(a.amt,0))
			FROM 
				HPOIMS.dbo.ys_direct_cost a
			left outer join
				HPOIMS.dbo.ys_project_rate b
			on
				a.pjt_no = b.pjt_no and a.curr = b.curr
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
		) a
		GROUP BY 
			a.pjt_no, a.main, a.sub, a.bud_seq
	) a
-- 	where
-- 		(a.bud_x_amt*a.base_rate) <> (a.soyo_amt+a.input_amt)
		
) b
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.pjt_no like 'V%' 
 	and a.main <> 'AB00'
--  	and a.curr = 'WON'
-- 	and a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0)
-- 		<> 
-- 		(b.soyo_amt+b.input_amt)
	and  round(a.x_amt*a.base_rate, -1) + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) <> (b.soyo_amt+b.input_amt)
	and abs(a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0) - (b.soyo_amt+b.input_amt)) > 1 
-- 	and a.item_name = 'ManDay 지원비'

/*

-- 실행예산 소요예산 = 구매청구서 금액 + 직접비 금액 비교하여 다른 것 업데이트 하기
begin tran

update
	HPOIMS.dbo.ys_pbudget
set
	x_amt = (b.soyo_amt+b.input_amt)
from
	HPOIMS.dbo.ys_pbudget a
join
(
	select 
		*
	from
	(
		select
			a.pjt_no, a.main, a.sub, a.bud_seq
			, sum(a.bud_amt) bud_amt
			, sum(a.bud_x_amt) bud_x_amt
			, sum(a.bud_e_amt) bud_e_amt
			, sum(a.soyo_amt) soyo_amt
			, sum(a.input_amt	) input_amt
		from
		(
			SELECT 
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ISNULL(a.closing_yn,'N')
				, bud_amt = ISNULL(a.amt,0)
				, bud_x_amt = ISNULL(a.x_amt,0)
				, bud_e_amt = ISNULL(a.e_amt,0)
				, soyo_amt = 0
				, input_amt = 0      
			FROM 
				HPOIMS.dbo.ys_pbudget a
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = SUM(ISNULL(a.w_amt,0))
				, input_amt = 0
			FROM 
				HPOIMS.dbo.gm_por_detail a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = 0
				, input_amt = SUM(ISNULL(a.amt,0))
			FROM 
				HPOIMS.dbo.ys_direct_cost a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
		) a
		GROUP BY 
			a.pjt_no, a.main, a.sub, a.bud_seq
	) a
	where
		a.bud_x_amt <> (a.soyo_amt+a.input_amt)
) b
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.pjt_no like '%' 
 	and a.main <> 'AB00'
	and a.curr = 'WON'
	and ROUND(isnull(a.x_amt,0)*a.base_rate,0) <> (b.soyo_amt+b.input_amt)

commit tran

rollback tran


begin tran

update
	HPOIMS.dbo.ys_pbudget
set
	x_amt = (b.soyo_amt+b.input_amt)
from
	HPOIMS.dbo.ys_pbudget a
join
(
	select 
		*
	from
	(
		select
			a.pjt_no, a.main, a.sub, a.bud_seq
			, sum(a.bud_amt) bud_amt
			, sum(a.bud_x_amt) bud_x_amt
			, sum(a.bud_e_amt) bud_e_amt
			, sum(a.soyo_amt) soyo_amt
			, sum(a.input_amt	) input_amt
		from
		(
			SELECT 
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ISNULL(a.closing_yn,'N')
				, bud_amt = ISNULL(a.amt,0)
				, bud_x_amt = ISNULL(a.x_amt,0)
				, bud_e_amt = ISNULL(a.e_amt,0)
				, soyo_amt = 0
				, input_amt = 0      
			FROM 
				HPOIMS.dbo.ys_pbudget a
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = SUM(ISNULL(a.w_amt,0))
				, input_amt = 0
			FROM 
				HPOIMS.dbo.gm_por_detail a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = 0
				, input_amt = SUM(ISNULL(a.amt,0))
			FROM 
				HPOIMS.dbo.ys_direct_cost a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
		) a
		GROUP BY 
			a.pjt_no, a.main, a.sub, a.bud_seq
	) a
-- 	where
-- 		a.bud_x_amt <> (a.soyo_amt+a.input_amt)
) b
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.pjt_no like '%' 
 	and a.main <> 'AB00'
	and a.curr = 'WON'
	and ROUND(isnull(a.x_amt,0)*a.base_rate,0) <> (b.soyo_amt+b.input_amt)

--  	and a.pjt_no like 'B%' 
	and ROUND(a.x_amt*a.base_rate,0) <> (b.soyo_amt+b.input_amt)

-- 	and a.x_amt*a.base_rate + round(((a.x_amt*a.base_rate) * isnull((select fob from HPOIMS.dbo.ys_project_rate where pjt_no = a.pjt_no and curr = a.curr),0)),0)
-- 		<> 
-- 		(b.soyo_amt+b.input_amt)

rollback tran
*/
