-- ==========================================
-- 작업일자: YYYY-MM-DD [쿼리 작성 및 실행 날짜를 기록합니다.]
-- 작 성 자: [실행자 이름을 기록합니다.]
-- 작업목적: [회계장부로 원가계산기능을 순차적으로 검증하며 중간산출자료등을 회계팀에 제공할 목적으로 작성된 쿼리입니다.]
-- 요청자/티켓번호: [회계팀의 요청시 검토하는 용도입니다.]
-- 데이타베이스 : MS-SQL
-- ==========================================

DROP TABLE #temp_t23a10

DECLARE    
	@area CHAR(1) ,
	@saupbu 	CHAR(2),
	@frdate 	CHAR(8),
	@todate 	CHAR(8)
-- AS     
	SELECT @area = '9', @saupbu = '10', @frdate = '20230101', @todate = '20230131'


	DECLARE @totamt		DECIMAL(20,0)
	DECLARE @yy		CHAR(4)
	SET @yy = SUBSTRING(@frdate,1,4)

    CREATE  TABLE  #temp_t23a10
    ( 
		area		CHAR(1) null,
		apvdate		CHAR(8) null,
		apvnum		CHAR(6)	null,
		apvseq		CHAR(6)	null,
		saupbu		CHAR(2) null,
		acntcode	CHAR(7) null,			
		vendorcd	VARCHAR(20) null,
		pjtno		VARCHAR(20) null,
		apvdramt	DECIMAL,
		apvcramt	DECIMAL,
		hyundae		CHAR(1) null,
		dr_cr		CHAR(1) null
    )  
        
--============ 조정후 시작(2011.11.16)

		INSERT
			#temp_t23a10
		SELECT 
			area,apvdate,apvnum,apvseq,saupbu,acntcode,vendorcd,pjtno,apvdramt,apvcramt,hyundae,dr_cr
		FROM 
			ACC.dbo.t23a10 
        WHERE   area   = @area 
          AND   saupbu = @saupbu 
          AND   apvdate BETWEEN SUBSTRING(@todate,1,4) AND @todate 
                        
		UPDATE
			a 
		SET
			acntcode = (CASE WHEN b.c_acntcode <> '' THEN b.c_acntcode ELSE a.acntcode END)
			, pjtno = (CASE WHEN b.c_pjtno <> '' THEN b.c_pjtno ELSE a.pjtno END)
		FROM	
			#temp_t23a10 a
		JOIN
			ACC.dbo.t23w70 b
		ON
			a.apvdate = b.apvdate and a.apvnum = b.apvnum and a.apvseq = b.apvseq		                       
			
--============ 조정후 종료
		
	/* 지움 */
	DELETE 
		ACC.dbo.t23w20        -- 원가계산마스타
	WHERE
		yy = @yy 

	
	DECLARE @temp_s1 TABLE
	(
	    pjtno    		VARCHAR(10)
	    , acntcode 	VARCHAR(7)
	    , mon      		VARCHAR(2)
	    , slpamt   		DECIMAL(20,2)
	)

	DECLARE @temp_s2 TABLE
	(
	    pjtno    		VARCHAR(10)
	    , yy 			CHAR(4)
	    , hg_gijun     	CHAR(1)
	)

	INSERT
		@temp_s2
	SELECT 
		c.pjtno_rep pjtno, a.yy, a.hg_gijun
	FROM 
		ACC.dbo.t23w60 a
	JOIN
		ACC.dbo.t23w50 c
	ON
		a.yy = c.yy and a.pjtno = c.pjtno_rep
	WHERE 
		a.yy = @yy
	UNION
	SELECT 
		c.pjtno_slip pjtno, a.yy, a.hg_gijun
	FROM 
		ACC.dbo.t23w60 a
	JOIN
		ACC.dbo.t23w50 c
	ON
		a.yy = c.yy and a.pjtno = c.pjtno_rep
	WHERE 
		a.yy = @yy
        
  
--==============================================================================================
-- 원가계산 공사진행율로 인식
-- 직접비와 간접비를 산출한다.
-- 직접비는 재료비는 프로젝트별 하나의 계정 월별로 합계액을 구한다.
--          노무비는 프로젝트별 급여, 상여, 기타(나머지는 기타로)별 월별로 합계액을 구한다.
--          경비는  프로젝트별 각 계정별 월별로 합계액을 구한다.
--          외주비는  프로젝트별 하나의 계정 월별로 합계액을 구한다.
-- 간접비는 간접비 배부기준에서 불러와서 체크된 것에 한해서 프로젝트별 계정별 월별에 배부한다.
--==============================================================================================
/*  재료비 직접비 집계 -->월별로 재료비 하나로만 관리*/
	INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'91'), 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '91'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '91%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'91'), 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '91'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '91%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno


/*  노무비 직접비 집계 --> 프로젝트별 계정과목별 월별 금액*/
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9200101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '92%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9200101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '92%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno


/*  경 비   직접비 집계 */
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9300101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '93%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9300101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '93%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno

/*  외주비  직접비 집계 */
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'94') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '94'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '94%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'94') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '94'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '94%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '1'
	ORDER BY 
		a.pjtno


/* 전표등록된 공사코드 ---> 원가계산용 공사코드로 변경 */
    UPDATE 
		@temp_s1 
	SET
       	pjtno = b.pjtno_rep
      FROM   
		@temp_s1 a 
	JOIN 
		ACC.dbo.t23w50 b
	ON
		b.yy = @yy and a.pjtno = b.pjtno_slip

/* 원가계산 테이블에 저장 ---> 프로젝트별 계정별 월별 직접비 금액*/
    INSERT 
		ACC.dbo.t23w20
		(yy, pjtno, acntcode, indi, [month], amt, rate, gijun)
    SELECT 
		@yy
		, pjtno, acntcode
		, indi = '1'--직접비
		, mon
		, amt = ISNULL(SUM(slpamt),0)
		, rate = 0
		, gijun = '1'    -- 공사진행율 인식
	FROM  
		@temp_s1
	WHERE
		pjtno <> 'AA'
	GROUP BY
		pjtno, acntcode, mon
	ORDER BY 
		pjtno, acntcode, mon

--=================================================================
-- 원가계산 세금계산서 발행금액으로 인식(직접비만 산출한다.)
--=================================================================
	DELETE @temp_s1

/*  재료비 직접비 집계 -->월별로 재료비 하나로만 관리*/
	INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'91'), 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '91'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '91%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'91'), 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '91'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '91%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno


/*  노무비 직접비 집계 --> 프로젝트별 계정과목별 월별 금액*/
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9200101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '92%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9200101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '92%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno


/*  경 비   직접비 집계 */
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9300101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '93%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'9300101') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '93%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno

/*  외주비  직접비 집계 */
    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'94') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '94'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '94%'
			AND apvdate >= @frdate
			AND apvdate <= @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno

    INSERT  
		@temp_s1
	SELECT 
		a.pjtno, 
		acntcode = ISNULL(b.acntcode,'94') , 
		mon = ISNULL(b.mon,'01'), 
		slpamt = ISNULL(b.slpamt,0)
	FROM 
		@temp_s2 a 
	JOIN
	(
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode = '94'
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			ACC.dbo.t23a90 
		WHERE   
			acntcode  LIKE  '94%'
-- 			AND apvdate >= @frdate
			AND apvdate = @todate
			AND saupbu =  @saupbu
			AND area   = @area 
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
	) b 
	ON 
		a.pjtno = b.pjtno
	WHERE 
		a.yy = @yy
		AND a.hg_gijun = '2'
	ORDER BY 
		a.pjtno


/* 전표등록된 공사코드 ---> 원가계산용 공사코드로 변경 */
    UPDATE 
		@temp_s1 
	SET
       	pjtno = b.pjtno_rep
      FROM   
		@temp_s1 a 
	JOIN 
		ACC.dbo.t23w50 b
	ON
		b.yy = @yy and a.pjtno = b.pjtno_slip

/* 원가계산 테이블에 저장 ---> 프로젝트별 계정별 월별 직접비 금액*/
       INSERT 
		ACC.dbo.t23w20
		(yy, pjtno, acntcode, indi, [month], amt, rate, gijun)
       SELECT 
		@yy
		, pjtno, acntcode
		, indi = '1'--직접비
		, mon
		, amt = ISNULL(SUM(slpamt),0)
		, rate = 0
		, gijun = '2'    -- 세금계산서 발행금액 인식
	FROM  
		@temp_s1
	WHERE
		pjtno <> 'AA'
	GROUP BY
		pjtno, acntcode, mon
	ORDER BY 
		pjtno, acntcode, mon

/* ------------------------------------------------------------------- */
/* 간접비는 원가배부기준이 1인것에만 프로젝트별 계정별 월별로 배부한다.*/
/* 간접비는 공사코드가 AA인 것만 합치면 된다.                          */
/* 간접비는 직접비의 비율에 따라서 배부한다.                           */
/* ------------------------------------------------------------------- */
	DELETE @temp_s1

/* 재료비 간접집계 -- 하나의 계정으로 관리*/
    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode = '91'
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		#temp_t23a10  
    WHERE   
		acntcode  LIKE  '91%'
		AND saupbu = @saupbu
		AND area   = @area   
		AND apvdate >= @frdate
		AND apvdate <= @todate
--		AND pjtno = 'AA'
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, SUBSTRING(apvdate,5,2)

    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode = '91'
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		ACC.dbo.t23a90  
    WHERE   
		acntcode  LIKE  '91%'
		AND saupbu = @saupbu
		AND area   = @area   
-- 		AND apvdate >= @frdate
		AND apvdate = @todate
--		AND pjtno = 'AA'
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, SUBSTRING(apvdate,5,2)	
       
/* 노무비 간접비 집계 -- 급여, 상여, 기타로 관리하기로 했는데 일단 계정별로 */
    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		#temp_t23a10  
    WHERE   
		acntcode  LIKE  '92%'
		AND saupbu = @saupbu
		AND area   = @area   
		AND apvdate >= @frdate
		AND apvdate <= @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, acntcode, SUBSTRING(apvdate,5,2)

    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		ACC.dbo.t23a90  
    WHERE   
		acntcode  LIKE  '92%'
		AND saupbu = @saupbu
		AND area   = @area   
-- 		AND apvdate >= @frdate
		AND apvdate = @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, acntcode, SUBSTRING(apvdate,5,2)	

       
/* 경비 간접비 집계 --계정별로 합계를 구한다. */
    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		#temp_t23a10  
    WHERE   
		acntcode  LIKE  '93%'
		AND saupbu = @saupbu
		AND area   = @area   
		AND apvdate >= @frdate
		AND apvdate <= @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, acntcode, SUBSTRING(apvdate,5,2)

    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		ACC.dbo.t23a90  
    WHERE   
		acntcode  LIKE  '93%'
		AND saupbu = @saupbu
		AND area   = @area   
-- 		AND apvdate >= @frdate
		AND apvdate = @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, acntcode, SUBSTRING(apvdate,5,2)	


/* 외주비 간접비 집계 --하나의 계정으로 합계를 구한다. */
    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode = '94'
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		#temp_t23a10  
    WHERE   
		acntcode  LIKE  '94%'
		AND saupbu = @saupbu
		AND area   = @area   
		AND apvdate >= @frdate
		AND apvdate <= @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, SUBSTRING(apvdate,5,2)

    INSERT  
		@temp_s1
    SELECT  
		pjtno
		, acntcode = '94'
		, mon = SUBSTRING(apvdate,5,2)
		, slpamt = SUM(apvdramt)
    FROM    
		ACC.dbo.t23a90  
    WHERE   
		acntcode  LIKE  '94%'
		AND saupbu = @saupbu
		AND area   = @area   
-- 		AND apvdate >= @frdate
		AND apvdate = @todate
--		AND pjtno = 'AA' 
		AND (pjtno = 'AA' OR pjtno IN (select pjtno_slip from ACC.dbo.t23w50 where yy = substring(@frdate,1,4) and pjtno_rep = 'AA'))
	GROUP BY 
		pjtno, SUBSTRING(apvdate,5,2)	
       
/*  간접비 합계 ------------------------------------------------
       SELECT  pjtno, acntcode, mon, amt=ISNULL(SUM(slpamt),0) 
         into  #temp_s3
         FROM  @temp_s1
        group  by pjtno, acntcode, mon
*/

/************************************************************************/
/*  간접비 배부         ------------------------------------------------*/
/************************************************************************/
    DECLARE @chk_baebu_yn1 char(1),@chk_baebu_yn2 char(1),@chk_baebu_yn3 char(1),@chk_baebu_yn4 char(1)
    SELECT @chk_baebu_yn1 = cd_name4 FROM ACC.dbo.t23c90 WHERE group_code = '05' AND sub_code = '1'
    SELECT @chk_baebu_yn2 = cd_name4 FROM ACC.dbo.t23c90 WHERE group_code = '05' AND sub_code = '2'
    SELECT @chk_baebu_yn3 = cd_name4 FROM ACC.dbo.t23c90 WHERE group_code = '05' AND sub_code = '3'
    SELECT @chk_baebu_yn4 = cd_name4 FROM ACC.dbo.t23c90 WHERE group_code = '05' AND sub_code = '4'

/* 직접비 합 구함 :::: 프로젝트별 월별 계정별 합계 */
/* 간접비 배부기준에 따라서 직접비 합이 틀려진다. 간접비배부에 해당하는 계정에 대한 합(A)을 구한다.*/
/* 간접비 배부에 해당하는 프로젝트별 합(B)을 구해서 B/A*100을 하면 비율이 구해진다.*/
/*
        SELECT pjtno, month
            , SUM(case when acntcode like '91%' then amt else 0 end) tot1
            , SUM(case when acntcode like '92%' then amt else 0 end) tot2
            , SUM(case when acntcode like '93%' then amt else 0 end) tot3
            , SUM(case when acntcode like '94%' then amt else 0 end) tot4
            , rate=convert(decimal(20,13),0)
            into   @totamt1
        FROM   ACC.dbo.t23w20  
       WHERE  area=@area 
            AND saupbu=@saupbu 
            AND indi = '1'     --직접비기준
            AND gijun = '1'    --공사진행율기준
        GROUP BY pjtno, month        
*/        
--        SELECT * FROM @temp_s1
--        SELECT SUM(slpamt) FROM @temp_s1 GROUP BY SUBSTRING(acntcode,1,2)
--        SELECT * FROM @totamt1 ORDER BY acntcode, pjtno
--        SELECT * FROM @totamt2 ORDER BY pjtno, acntcode

       -- 이거는 사용 안해도 될듯...
	DECLARE @totamt1 TABLE
	(
		pjtno		VARCHAR(10),
		acntcode	VARCHAR(7),
		[month]		VARCHAR(2),
		totamt		DECIMAL,
		rate			DECIMAL(20,13)
	)

    INSERT INTO   
		@totamt1
    SELECT 
		pjtno, acntcode, [month], totamt=SUM(amt), rate=0
    FROM   
		ACC.dbo.t23w20  
    WHERE  
		yy = @yy
              AND indi = '1'     --직접비기준
              AND gijun = '1'    --공사진행율기준
    GROUP BY 
		pjtno, acntcode, [month]
    ORDER BY 
		acntcode


       -- 이거는 일단 간접비 배부기준을 92, 93으로 셋팅함 원래는 테이블에서 가져와야 하는데...처리 안됨
	DECLARE @totamt2 TABLE
	(
		pjtno		VARCHAR(10),
		[month]		VARCHAR(2),
		totamt		DECIMAL,
		amt92		DECIMAL,
		amt93		DECIMAL,
		rate			DECIMAL(20,13)
	)

    INSERT INTO   
		@totamt2
    SELECT 
		pjtno, [month]
--		, totamt = (SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END)) 
		, totamt = case 
			when @todate < '20130531' 
			then 
				(SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END)) 
			else
			(SUM(CASE WHEN acntcode like '92%' THEN (case when amt>0 then amt else 0 end) ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN (case when amt>0 then amt else 0 end) ELSE 0 END)) 
			end
		, amt92 = SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)
		, amt93 = SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END) 
		, rate=CONVERT(DECIMAL(20,13),0)
    FROM   
		ACC.dbo.t23w20  
    WHERE  
		yy = @yy
        AND indi = '1'     --직접비기준
        AND gijun = '1'    --공사진행율기준
    GROUP BY  pjtno, [month]

 /* pjt별 배부율 할당계산 */
    UPDATE 
		@totamt2
    SET 
		rate = CONVERT(DECIMAL(20,13), (a.totamt/b.total))
    FROM 
		@totamt2 a 
	INNER JOIN 
	(
		SELECT  
			[month]
			, total = SUM(totamt)
		FROM  
			@totamt2
		GROUP BY 
			[month]
	) b
        on a.[month] = b.[month]

SELECT * FROM @totamt2 order by pjtno
SELECT [month], sum(totamt) FROM @totamt2 GROUP BY [month] -- 11	1986898860

       -- 감가상각비, 퇴충에 대해서 처리
	DECLARE @totamt3 TABLE
	(
		pjtno		VARCHAR(10),
 		[month]		VARCHAR(2),
		totamt		DECIMAL,
		amt92		DECIMAL,
		amt93		DECIMAL,
		rate			DECIMAL(20,13)
	)

	INSERT INTO   
		@totamt3
	SELECT 
		pjtno
		, [month] = SUBSTRING(@todate,5,2) 
--		, totamt = (SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END)) 
		, totamt = case
			when @todate < '20130531' 
			then
				(SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END)) 
			else
				(SUM(CASE WHEN acntcode like '92%' THEN (case when amt>0 then amt else 0 end) ELSE 0 END)+SUM(CASE WHEN acntcode like '93%' THEN (case when amt>0 then amt else 0 end) ELSE 0 END)) 
			end
		, amt92 = SUM(CASE WHEN acntcode like '92%' THEN amt ELSE 0 END)
		, amt93 = SUM(CASE WHEN acntcode like '93%' THEN amt ELSE 0 END) 
		, rate=CONVERT(DECIMAL(20,13),0)
	FROM   
		ACC.dbo.t23w20  
	WHERE  
		yy = @yy
		AND indi = '1'     --직접비기준
		AND gijun = '1'    --공사진행율기준
	GROUP BY  pjtno

 -- SELECT * FROM @totamt3 order by pjtno
 -- SELECT sum(totamt) FROM @totamt3 -- 18731700085

 /* pjt별 배부율 할당계산 */
	UPDATE 
		@totamt3
	SET 
		rate = CONVERT(DECIMAL(20,13), (totamt/(SELECT  totamt=SUM(totamt) FROM  @totamt3)))

	
 -- SELECT * FROM @totamt2 order by pjtno
 SELECT * FROM @totamt3 order by pjtno
 SELECT substring(acntcode,1,5) acntcode, mon, sum(slpamt) slpamt FROM @temp_s1 where acntcode like '92%' group by substring(acntcode,1, 5), mon
 -- 노무비 배부기준
 SELECT pjtno,substring(acntcode,1,5) acntcode, mon, sum(slpamt) slpamt FROM @temp_s1 where acntcode like '92%' group by pjtno,substring(acntcode,1, 5), mon
 -- 경비 배부기준
 SELECT pjtno,substring(acntcode,1,5) acntcode, mon, sum(slpamt) slpamt FROM @temp_s1 where acntcode like '93%' group by pjtno,substring(acntcode,1, 5), mon
 -- SELECT substring(acntcode,1,2) acntcode, mon, sum(slpamt) slpamt FROM @temp_s1 where substring(acntcode,1,5) in ('92001', '92002') group by substring(acntcode,1, 2), mon
/*
		SELECT  
			pjtno = ISNULL(pjtno,'')
			, acntcode
			, mon = SUBSTRING(apvdate,5,2)
			, slpamt = SUM(apvdramt)
		FROM    
			#temp_t23a10 
		WHERE   
			acntcode  LIKE  '93%'
			AND apvdate >= '20230101'
			AND apvdate <= '20230131'
			AND saupbu =  '10'
			AND area   = '9'
			and pjtno = 'V172T004'
		GROUP   BY  
			pjtno, acntcode, SUBSTRING(apvdate,5,2)
*/