-- ==========================================
-- 작업일자: 2026-06-25 
-- 작 성 자: 류호성
-- 작업목적: [실행예산과 프로젝트투입현황의 실행예산 차이를 분석하여 오류자료를 도출한다.]
-- 요청자/티켓번호: [김광현전무 / 없음]
-- 데이타베이스 : MS-SQL
-- ==========================================

declare
	@pjt_no		VARCHAR(8)   -- 프로젝트 번호
	, @yymm		VARCHAR(6)	
--AS
SELECT @pjt_no = 'V172T502', @yymm = '202605'
-- SELECT @pjt_no = 'V172T406', @yymm = '202605'

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

declare @yy char(4), @mm char(2), @bef_yy char(4), @bef_mm char(2), @yyyymmdd char(8)
set @yy = substring(@yymm, 1, 4)
set @mm = substring(@yymm, 5, 2) 
set @yyyymmdd = convert(char(8), getdate(), 112)
IF (@yymm <> '999999')
BEGIN
	set @yyyymmdd = convert(char(8), dateadd(month, 1, cast(@yy + '-' + @mm + '-01' as date)), 112)
	set @bef_yy = substring(convert(char(8), dateadd(month, 1, cast(@yy + '-' + @mm + '-01' as date)), 112), 1,4)
	set @bef_mm = substring(convert(char(8), dateadd(month, 1, cast(@yy + '-' + @mm + '-01' as date)), 112), 5,2)
END
ELSE
BEGIN
	set @yyyymmdd = convert(char(8), getdate(), 112)
	set @yy = substring(@yyyymmdd, 1, 4)
	set @mm = substring(@yyyymmdd, 5, 2) 
	set @bef_yy = substring(convert(char(8), dateadd(month, 1, cast(@yy + '-' + @mm + '-01' as date)), 112), 1,4)
	set @bef_mm = substring(convert(char(8), dateadd(month, 1, cast(@yy + '-' + @mm + '-01' as date)), 112), 5,2)
END

-- [0] 구매발주내역을 조회한다.
select 
    b.main
    ,	[act_desc]    = (SELECT ISNULL(x.act_desc,'') FROM HPOIMS.dbo.ys_activity x WHERE x.main=b.main AND x.sub='000'), '' ven_name, '' cont_desc
    ,	a.*
from 
    HPOIMS.dbo.gm_po_detail a 
join 
    HPOIMS.dbo.gm_por_detail b 
on 
    a.por_no = b.por_no and a.por_seq = b.por_seq
join
    HPOIMS.dbo.gm_po c
on
    a.po_no = c.po_no
join
    HPOIMS.dbo.gy_contract d
on
    c.cont_no = d.cont_no
where 
    a.po_no in (select new_po_no from HPOIMS.dbo.gy_contract_detail where cont_no in(select cont_no from HPOIMS.dbo.gy_contract where pjt_no = @pjt_no))
order by b.main

-- [1] 계약상세내역을 조회한다. 
select 
    * 
from 
    HPOIMS.dbo.gy_contract_detail 
where 
    cont_no in (select cont_no from HPOIMS.dbo.gy_contract where pjt_no = @pjt_no)

-- [2] 구매발주내역과 계약상세내역을 엑셀로 비교하여 차이를 도출한다. [참고: 엑셀자료]