-- ==========================================
-- 작업일자: YYYY-MM-DD [쿼리 작성 및 실행 날짜를 기록합니다.]
-- 작 성 자: [실행자 이름을 기록합니다.]
-- 작업목적: [회계년도에 대한 원가, 판관비 전표를 조회하여 다량의 자료를 엑셀파일로 제공합니다.]
-- 요청자/티켓번호: [회계부서의 요청에 따라 제공합니다.]
-- 데이타베이스 : MS-SQL
-- ==========================================

declare @fr_date char(8), @to_date char(8)

-- [0] 변경할 데이터 설정
select @fr_date = '20240101', @to_date = '20241231'

-- [1] 해당 기간의 경비와 판관비 전표 내역을 조회
select
	a.apvdate, a.apvnum, a.apvseq, a.dtcode, dtname=(select dept_nm from HRM.dbo.hr_dept where dept_cd = a.dtcode), a.acntcode
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and substring(a.acntcode, 1, 5)+'00' = acntcode) 
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and a.acntcode = acntcode) 
	, a.apvdramt, a.apvcramt
	, replace(a. slpfree, '"',''), a.pjtno
	, pjtname=(select pjt_name from HPOIMS.dbo.ys_project where a.pjtno = pjt_no)
	, a.vendorcd, venname=(select replace(ven_name,'"','') from HPOIMS.dbo.gm_vendor where a.vendorcd = ven_code)
	, case when a.billchk = '1' then '영수증' when a.billchk = '2' then '세금계산서' when a.billchk = '3' then '기타증빙' else '증빙없음' end billchk_nm
from 
	ACC.dbo.t23a10 a 
where 
	a.area = '9' 
	and a.apvdate between @fr_date and @to_date
	and a.acntcode is not null
	and substring(a.acntcode,1,1) in ('6','9')
order by
	a.apvdate, a.apvnum, a.apvseq

-- [2] 해당 기간의 경비 전표 내역을 조회
select
	a.apvdate, a.apvnum, a.apvseq, a.dtcode, dtname=(select dept_nm from HRM.dbo.hr_dept where dept_cd = a.dtcode), a.acntcode
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and substring(a.acntcode, 1, 5)+'00' = acntcode) 
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and a.acntcode = acntcode) 
	, a.apvdramt, a.apvcramt
	, replace(a. slpfree, '"',''), a.pjtno
	, pjtname=(select pjt_name from HPOIMS.dbo.ys_project where a.pjtno = pjt_no)
	, a.vendorcd, venname=(select replace(ven_name,'"','') from HPOIMS.dbo.gm_vendor where a.vendorcd = ven_code)
	, case when a.billchk = '1' then '영수증' when a.billchk = '2' then '세금계산서' when a.billchk = '3' then '기타증빙' else '증빙없음' end billchk_nm
from 
	ACC.dbo.t23a10 a 
where 
	a.area = '9' 
	and a.apvdate between @fr_date and @to_date
	and a.acntcode is not null
	and a.acntcode like '9%'
order by
	a.apvdate, a.apvnum, a.apvseq

-- [3] 해당 기간의 법인카드 전표 내역을 증빙으로 구분하여 조회
select 
	a.apvdate, a.apvnum, a.apvseq, a.dtcode, dtname=(select dept_nm from HRM.dbo.hr_dept where dept_cd = a.dtcode), a.acntcode
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and substring(a.acntcode, 1, 5)+'00' = acntcode) 
	, acntname = (select replace(acntname, ' ', '') from ACC.dbo.t23c10 where c10_yy = '2024' and a.acntcode = acntcode) 
	, a.apvdramt, a.apvcramt
	, replace(a. slpfree, '"',''), a.pjtno
	, pjtname=(select pjt_name from HPOIMS.dbo.ys_project where a.pjtno = pjt_no)
	, a.vendorcd, venname=(select replace(ven_name,'"','') from HPOIMS.dbo.gm_vendor where a.vendorcd = ven_code)
	, case when a.billchk = '1' then '영수증' when a.billchk = '2' then '세금계산서' when a.billchk = '3' then '기타증빙' else '증빙없음' end billchk_nm
from 
	ACC.dbo.t23a10 a
where 
	area+apvdate+apvnum in (select area+apvdate+apvnum from ACC.dbo.t23a10 where area = '9' and apvdate between @fr_date and @to_date and acntcode = '2100502' and dr_cr = '2')
	and dr_cr = '1'
