<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.style24.scm.biz.dao.TssSettleDao">

	<!-- 제휴채널정산 목록 -->
	<select id="getAfLinkFeeList" parameterType="AfLinkFee" resultType="AfLinkFee">
		/* TssSettle.getAfLinkFeeList */
		SELECT AF_LINK_CD                                         /*채널코드*/
		     , AF_LINK_NM                                         /*채널명*/
		     , OCCUR_DT                                           /*발생일시*/
		     , ORD_NO                                             /*주문번호*/
		     , ORD_DTL_STAT                                       /*주문상세상태*/
		     , SUM(SELL_AMT)                     AS SELL_AMT      /*판매금액(상품금액.배송비제외)*/
		     , SUM(CPN1_DC_AMT +
		           TMTB1_DC_AMT + TMTB2_DC_AMT +
		           GOODS_CPN_DC_AMT +
		           CART_CPN_DC_AMT)              AS CPN_DC_AMT     /*쿠폰할인금액*/
		     , SUM(PNT_DC_AMT + PRE_PNT_DC_AMT)  AS PNT_DC_AMT     /*포인트할인금액*/
		     , SUM(GFCD_USE_AMT)                 AS GFCD_USE_AMT   /*상품권사용금액*/
		     , SUM(SELL_AMT - (CPN1_DC_AMT + TMTB1_DC_AMT + TMTB2_DC_AMT + GOODS_CPN_DC_AMT + CART_CPN_DC_AMT + PG_CPN_AMT)
		                    - (PNT_DC_AMT + PRE_PNT_DC_AMT)
		                    - GFCD_USE_AMT)      AS PAY_AMT        /*결제금액(VAT포함)*/
		     , SUM(TRUNCATE((SELL_AMT - (CPN1_DC_AMT + TMTB1_DC_AMT + TMTB2_DC_AMT + GOODS_CPN_DC_AMT + CART_CPN_DC_AMT + PG_CPN_AMT)
		                              - (PNT_DC_AMT + PRE_PNT_DC_AMT)
		                              - GFCD_USE_AMT
		                    ) / 1.1,0))          AS SALES_AMT      /*매출금액(VAT제외). 건별로 1.1로 나누고 소숫점을 절사하여 합계를 낸다.*/
		FROM   (
		        SELECT OD.AF_LINK_CD                                                                                                           /*제휴링크코드*/
		             , AL.AF_LINK_NM                                                                                                           /*제휴링크명*/
		             , DATE_FORMAT(ODIH.REG_DT,'%Y-%m-%d %H:%i:%S')                                                        AS OCCUR_DT         /*발생일자*/
		             , ODIH.ORD_NO                                                                                                             /*주문번호*/
		             , ODIH.ORD_DTL_NO                                                                                                         /*주문상세번호*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10','결제완료',IFNULL(FN_GET_CODE_NM('G685',OCD.CHG_STAT),'주문취소')) AS ORD_DTL_STAT     /*주문상세상태*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',ODIH.ORD_AMT,-1 * ODIH.CNCL_RTN_AMT)                               AS SELL_AMT         /*판매금액(상품금액.배송비제외)*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.CPN1_DC_AMT                                           AS CPN1_DC_AMT      /*1차쿠폰(즉시할인쿠폰)할인금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.TMTB1_DC_AMT                                          AS TMTB1_DC_AMT     /*다다익선할인금액(수량)*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.TMTB2_DC_AMT                                          AS TMTB2_DC_AMT     /*다다익선할인금액(금액)*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.GOODS_CPN_DC_AMT                                      AS GOODS_CPN_DC_AMT /*상품쿠폰할인금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.CART_CPN_DC_AMT                                       AS CART_CPN_DC_AMT  /*장바구니할인금액*/
		             , 0                                                                                                   AS PG_CPN_AMT       /*PG쿠폰금액.TODO:바로 아래 쿼리문으로 대체해야 함*/
		             -- , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.PG_CPN_AMT                                         AS PG_CPN_AMT       /*PG쿠폰금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.PNT_DC_AMT                                            AS PNT_DC_AMT       /*포인트할인금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.PRE_PNT_DC_AMT                                        AS PRE_PNT_DC_AMT   /*선포인트할인금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_10',1,-1) * ODIH.GFCD_USE_AMT                                          AS GFCD_USE_AMT     /*상품권사용금액*/
		        FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		        INNER JOIN TB_ORDER_DETAIL OD ON ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		        INNER JOIN TB_AF_LINK AL ON OD.AF_LINK_CD = AL.AF_LINK_CD
		        LEFT OUTER JOIN TB_ORDER_CHANGE_DETAIL OCD ON ODIH.ORD_DTL_NO = OCD.ORD_DTL_NO
		                                                  AND OCD.CHG_STAT IN ('G685_18','G685_40','G685_60')	/*취소완료,교환완료,반품완료*/
		        WHERE  ODIH.ORD_DTL_STAT IN ('G720_10','G720_30','G720_40') /*판매-결제완료,환입-취소완료,환입-품절취소*/
		        AND    ODIH.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{startDt},'%Y-%m-%d')
		        AND    ODIH.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{endDt},'%Y-%m-%d'),INTERVAL 1 DAY)
		        <if test="ordNo != null and ordNo != ''">
		        AND    ODIH.ORD_NO = #{ordNo}
		        </if>
		        <if test="afLinkCd != null and afLinkCd != ''">
		        AND    AL.AF_LINK_CD = #{afLinkCd}
		        </if>
		        AND    AL.AF_CHANNEL = #{afChannel}
		       ) U
		GROUP  BY AF_LINK_CD, AF_LINK_NM, OCCUR_DT, ORD_NO, ORD_DTL_STAT
		ORDER  BY AF_LINK_CD, AF_LINK_NM, OCCUR_DT, ORD_NO, ORD_DTL_STAT
	</select>
	
	<!-- 입점업체 상품정산 목록 -->
	<select id="getSupplyCompanyGoodsFeeList" parameterType="SupplyCompFee" resultType="SupplyCompFee">
		/* TsaSettle.getSupplyCompanyGoodsFeeList */
		SELECT ODIH.ORD_NO                                                                                     /*주문번호*/
		     , ODIH.ORD_DTL_NO                                                                                 /*주문상세번호*/
		     , ODIH.SUPPLY_VENDOR_CD                                                                           /*공급벤더코드*/
		     , ODIH.SUPPLY_VENDOR_NM                                                                           /*공급벤더명*/
		     , ODIH.SUPPLY_COMP_CD                                                                             /*공급업체코드*/
		     , ODIH.SUPPLY_COMP_NM                                                                             /*공급업체명*/
		     , ODIH.SETTLE_DAY                                                                                 /*정산주기*/
		     , ODIH.SETTLE_DAY_NM                                                                              /*정산주기명*/
		     , ODIH.ORD_DT                                                                                     /*주문일시*/
		     , ODIH.SETTLE_GB                                                                                  /*정산구분*/
		     , ODIH.SETTLE_GB_NM                                                                               /*정산구분명*/
		     , ODIH.OCCUR_DT                                                                                   /*발생일시*/
		     , ODIH.GOODS_CD                                                                                   /*상품코드*/
		     , ODIH.GOODS_NM                                                                                   /*상품명*/
		     , ODIH.BRAND_CD                                                                                   /*브랜드코드*/
		     , ODIH.BRAND_ENM                                                                                  /*브랜드명*/
		     , ODIH.ITEM_CD                                                                                    /*단품코드*/
		     , ODIH.OPT_CD1                                                                                    /*옵션코드1*/
		     , ODIH.OPT_CD2                                                                                    /*옵션코드2*/
		     , ODIH.SELL_PRICE                                                                                 /*판매가*/
		     , ODIH.SELL_QTY                                                                                   /*판매수량*/
		     , ODIH.SELL_AMT                                                                                   /*판매금액(ORD_AMT에는 CPN1_DC_AMT가 빠진 금액이 들어가 있음)*/
		     , IF(ODIH.CPN1_DC_AMT = -0,0,ODIH.CPN1_DC_AMT)
		       + IF(ODIH.GOODS_CPN_DC_AMT = -0,0,ODIH.GOODS_CPN_DC_AMT)
		       + IF(ODIH.CART_CPN_DC_AMT = -0,0,ODIH.CART_CPN_DC_AMT)               AS CPN_DC_AMT              /*쿠폰사용금액*/
		     , IF(ODIH.SELF_CPN_DC_AMT = -0,0,ODIH.SELF_CPN_DC_AMT)                 AS SELF_CPN_DC_AMT         /*자사쿠폰분담액*/
		     , IF(ODIH.SUPPLY_COMP_CPN_DC_AMT = -0,0,ODIH.SUPPLY_COMP_CPN_DC_AMT)   AS SUPPLY_COMP_CPN_DC_AMT  /*입점쿠폰분담액*/
		     -- , IF(ODIH.PNT_DC_AMT = -0,0,ODIH.PNT_DC_AMT)                        AS PNT_DC_AMT              /*포인트사용금액*/
		     , IF(ODIH.TMTB_DC_AMT = -0,0,ODIH.TMTB_DC_AMT)                         AS TMTB_DC_AMT             /*다다익선할인금액*/
		     , IF(ODIH.SELF_TMTB_DC_AMT = -0,0,ODIH.SELF_TMTB_DC_AMT)               AS SELF_TMTB_DC_AMT        /*자사다다익선분담액*/
		     , IF(ODIH.SUPPLY_COMP_TMTB_DC_AMT = -0,0,ODIH.SUPPLY_COMP_TMTB_DC_AMT) AS SUPPLY_COMP_TMTB_DC_AMT /*입점다다익선분담액*/
		     -- , IF(ODIH.GFCD_USE_AMT = -0,0,ODIH.GFCD_USE_AMT)                    AS GFCD_USE_AMT            /*상품권사용금액*/
		     , CASE WHEN ODIH.SELL_QTY = 0 THEN 0
		            ELSE
		                (ODIH.SELL_AMT
		                 - (ODIH.CPN1_DC_AMT + ODIH.GOODS_CPN_DC_AMT + ODIH.CART_CPN_DC_AMT)
		                 - ODIH.TMTB_DC_AMT
		                ) / ODIH.SELL_QTY
		       END                                                                  AS REAL_SELL_PRICE        /*실판매가*/
		     , ODIH.SELL_AMT
		       - (ODIH.CPN1_DC_AMT + ODIH.GOODS_CPN_DC_AMT + ODIH.CART_CPN_DC_AMT)
		       - ODIH.TMTB_DC_AMT                                                   AS REAL_SELL_AMT          /*실판매금액(=상품총액. 쿠폰과 다다익선만 차감. 상품권과 포인트는 포함)*/
		     , ODIH.SELL_FEE_RATE                                                                             /*판매수수료율*/
		     , ROUND(ODIH.SELL_AMT * (ODIH.SELL_FEE_RATE / 100),0)                  AS SELL_FEE_AMT           /*수수료(판매금액 * 판매수수료율)*/
		     , ODIH.SELL_AMT
		       - ROUND(ODIH.SELL_AMT * (ODIH.SELL_FEE_RATE / 100),0)
		       + (ODIH.SELF_CPN_DC_AMT + ODIH.SELF_TMTB_DC_AMT)                     AS SETTLE_AMT             /*정산대상액(판매금액 - 수수료 + 자사다다익선분담액 + 자사쿠폰분담액)*/
		FROM   (
		        SELECT ODIH.ORD_DTL_ITEM_HST_SQ                                                                                    /*주문상세단품이력일련번호*/
		             , ODIH.ORD_NO                                                                                                 /*주문번호*/
		             , ODIH.ORD_DTL_NO                                                                                             /*주문상세번호*/
		             , SC.SUPPLY_VENDOR_CD                                                                                         /*공급벤더코드*/
		             , SV.SUPPLY_VENDOR_NM                                                                                         /*공급벤더명*/
		             , OD.SUPPLY_COMP_CD                                                                                           /*공급업체코드*/
		             , SC.SUPPLY_COMP_NM                                                                                           /*공급업체명*/
		             , SV.SETTLE_DAY                                                                                               /*정산주기*/
		             , FN_GET_CODE_NM('G075',SV.SETTLE_DAY)                                             AS SETTLE_DAY_NM           /*정산주기명*/
		             , DATE_FORMAT(O.PAY_DT,'%Y-%m-%d %H:%i:%S')                                        AS ORD_DT                  /*주문일시*/
		             , ODIH.ORD_DTL_STAT                                                                AS SETTLE_GB               /*정산구분*/
		             , FN_GET_CODE_NM('G720',ODIH.ORD_DTL_STAT)                                         AS SETTLE_GB_NM            /*정산구분명*/
		             , DATE_FORMAT(ODIH.REG_DT,'%Y-%m-%d %H:%i:%S')                                     AS OCCUR_DT                /*발생일시*/
		             , OD.GOODS_CD                                                                                                 /*상품코드*/
		             , G.GOODS_NM                                                                                                  /*상품명*/
		             , G.BRAND_CD                                                                                                  /*브랜드코드*/
		             , B.BRAND_ENM                                                                                                 /*브랜드명*/
		             , ODIH.ITEM_CD                                                                                                /*단품코드*/
		             , ODIH.OPT_CD1                                                                                                /*옵션코드1*/
		             , ODIH.OPT_CD2                                                                                                /*옵션코드2*/
		             , CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                        IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                    ELSE
		                        ODIH.ITEM_PRICE
		               END                                                                              AS SELL_PRICE              /*판매가(정산용)*/
		             , (CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' THEN 1 ELSE -1 END)
		               *
		               CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20'/*판매-배송중*/ THEN
		                             ODIH.ORD_QTY - ODIH.CNCL_RTN_QTY
		                         ELSE /*환입-반품완료,환입-교환완료*/
		                             ODIH.CNCL_RTN_QTY
		                    END AS SIGNED INT)                                                          AS SELL_QTY                /*판매수량*/
		             , (CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' THEN 1 ELSE -1 END)
		               *
		               CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20'/*판매-배송중*/ THEN
		                             ((ODIH.ORD_QTY - ODIH.CNCL_RTN_QTY) * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                                             IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                                         ELSE
		                                                                             ODIH.ITEM_PRICE
		                                                                    END))
		                         ELSE /*환입-반품완료,환입-교환완료*/
		                             (ODIH.CNCL_RTN_QTY * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                            IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                        ELSE
		                                                            ODIH.ITEM_PRICE
		                                                   END))
		                    END AS SIGNED INT)                                                          AS SELL_AMT                /*판매금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * ODIH.CPN1_DC_AMT                        AS CPN1_DC_AMT             /*즉시사용쿠폰금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * ODIH.GOODS_CPN_DC_AMT                   AS GOODS_CPN_DC_AMT        /*상품쿠폰사용금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * ODIH.CART_CPN_DC_AMT                    AS CART_CPN_DC_AMT         /*주문서쿠폰사용금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * 
		               (((100 - IFNULL(CB1.BURDEN_RATE,0)) / 100) * ODIH.CPN1_DC_AMT
		                + ((100 - IFNULL(CB2.BURDEN_RATE,0)) / 100) * ODIH.GOODS_CPN_DC_AMT
		                + ((100 - IFNULL(CB3.BURDEN_RATE,0)) / 100) * ODIH.CART_CPN_DC_AMT)             AS SELF_CPN_DC_AMT         /*자사쿠폰분담액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) *
		               ((IFNULL(CB1.BURDEN_RATE,0) / 100) * ODIH.CPN1_DC_AMT
		                + (IFNULL(CB2.BURDEN_RATE,0) / 100) * ODIH.GOODS_CPN_DC_AMT
		                + (IFNULL(CB3.BURDEN_RATE,0) / 100) * ODIH.CART_CPN_DC_AMT)                     AS SUPPLY_COMP_CPN_DC_AMT  /*입점쿠폰분담액*/
		             -- , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * ODIH.PNT_DC_AMT                      AS PNT_DC_AMT              /*포인트사용금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * (ODIH.TMTB1_DC_AMT + ODIH.TMTB2_DC_AMT) AS TMTB_DC_AMT             /*다다익선할인금액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) *
		               (((100 - IFNULL(TB1.TMTB_BURDEN_RATE,0)) / 100) * ODIH.TMTB1_DC_AMT
		                + ((100 - IFNULL(TB2.TMTB_BURDEN_RATE,0)) / 100) * ODIH.TMTB2_DC_AMT)           AS SELF_TMTB_DC_AMT        /*자사다다익선분담액*/
		             , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) *
		               ((IFNULL(TB1.TMTB_BURDEN_RATE,0) / 100) * ODIH.TMTB1_DC_AMT
		                + (IFNULL(TB2.TMTB_BURDEN_RATE,0) / 100) * ODIH.TMTB2_DC_AMT)                   AS SUPPLY_COMP_TMTB_DC_AMT /*입점다다익선분담액*/
		             -- , IF(ODIH.ORD_DTL_STAT = 'G720_20',1,-1) * ODIH.GFCD_USE_AMT                    AS GFCD_USE_AMT            /*상품권사용금액*/
		             , CASE WHEN O.MALL_GB = 'G011_10' /*자사몰*/ THEN
		                        OD.SELL_FEE_RATE
		                    ELSE /*제휴몰*/
		                        IFNULL(E.SELL_FEE_RATE,0)
		               END                                                                              AS SELL_FEE_RATE           /*판매수수료율*/
		        FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		        INNER JOIN TB_ORDER_DETAIL OD ON ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		        INNER JOIN TB_ORDER O ON ODIH.ORD_NO = O.ORD_NO
		        INNER JOIN TB_SUPPLY_COMPANY SC ON OD.SUPPLY_COMP_CD = SC.SUPPLY_COMP_CD
		        INNER JOIN TB_SUPPLY_VENDOR SV ON SC.SUPPLY_VENDOR_CD = SV.SUPPLY_VENDOR_CD
		        INNER JOIN TB_GOODS G ON OD.GOODS_CD = G.GOODS_CD
		        INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		        LEFT OUTER JOIN TB_EXTMALL E ON OD.EXTMALL_ID = E.EXTMALL_ID
		        LEFT OUTER JOIN TB_COUPON_BURDEN CB1 ON OD.CPN1_CPN_SQ = CB1.CPN_ID
		                                            AND OD.SUPPLY_COMP_CD = CB1.SUPPLY_COMP_CD
		        LEFT OUTER JOIN TB_COUPON_BURDEN CB2 ON OD.GOODS_CPN_SQ = CB2.CPN_ID
		                                            AND OD.SUPPLY_COMP_CD = CB2.SUPPLY_COMP_CD
		        LEFT OUTER JOIN TB_COUPON_BURDEN CB3 ON OD.CART_CPN_SQ = CB3.CPN_ID
		                                            AND OD.SUPPLY_COMP_CD = CB3.SUPPLY_COMP_CD
		        LEFT OUTER JOIN TB_TMTB_BURDEN TB1 ON OD.TMTB1_SQ = TB1.TMTB_SQ
		                                          AND OD.SUPPLY_COMP_CD = TB1.TMTB_SUPPLY_CD
		        LEFT OUTER JOIN TB_TMTB_BURDEN TB2 ON OD.TMTB2_SQ = TB2.TMTB_SQ
		                                          AND OD.SUPPLY_COMP_CD = TB2.TMTB_SUPPLY_CD
		        LEFT OUTER JOIN TB_EXTMALL_USAC_PRICE EUP ON ODIH.ORD_DTL_ITEM_HST_SQ = EUP.ORD_DTL_ITEM_HST_SQ
		        WHERE  1 = 1
		        AND    ODIH.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{salesStdt},'%Y-%m-%d')
		        AND    ODIH.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{salesEddt},'%Y-%m-%d'), INTERVAL 1 DAY)
		        AND    ODIH.ORD_DTL_STAT IN ('G720_20','G720_50','G720_60') /*판매-배송중,환입-반품완료,환입-교환완료*/
		        AND    SC.SUPPLY_VENDOR_CD = #{supplyVendorCd}
		        <if test="supplyCompCd != null and supplyCompCd != ''">
		        AND    SC.SUPPLY_COMP_CD = #{supplyCompCd}
		        </if>
		       ) ODIH
		ORDER  BY ODIH.ORD_DTL_ITEM_HST_SQ
	</select>
	
	<!-- 입점업체 배송비정산 목록 -->
	<select id="getSupplyCompanyDeliveryFeeList" parameterType="SupplyCompFee" resultType="SupplyCompFee">
		/* TsaSettle.getSupplyCompanyDeliveryFeeList */
		SELECT DATE_FORMAT(OD.OCCUR_DT,'%Y-%m-%d')       AS OCCUR_DT           /*발생(출고/회수)일시*/
		     , OD.SUPPLY_VENDOR_CD                                             /*공급벤더코드*/
		     , OD.SUPPLY_VENDOR_NM                                             /*공급벤더명*/
		     , OD.SUPPLY_COMP_CD                                               /*공급업체코드*/
		     , OD.SUPPLY_COMP_NM                                               /*공급업체명*/
		     , DF.DELV_FEE_GB                                                  /*배송비구분*/
		     , FN_GET_CODE_NM('G018',DF.DELV_FEE_GB)     AS DELV_FEE_GB_NM     /*배송비구분명*/
		     , OD.ORD_NO                                                       /*주문번호*/
		     , SUM(DF.DELV_FEE)                          AS DELV_FEE           /*배송비*/
		FROM   (
		        SELECT DISTINCT
		               ODIH.REG_DT         AS OCCUR_DT  /*발생일시*/
		             , SC.SUPPLY_VENDOR_CD              /*공급벤더코드*/
		             , SV.SUPPLY_VENDOR_NM              /*공급벤더명*/
		             , OD.SUPPLY_COMP_CD                /*공급업체코드*/
		             , SC.SUPPLY_COMP_NM                /*공급업체명*/
		             , ODIH.ORD_NO                      /*주문번호*/
		             , ODIH.ORD_DTL_STAT   AS SETTLE_GB /*정산구분*/
		        FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		        INNER JOIN TB_ORDER_DETAIL OD ON ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		        INNER JOIN TB_GOODS G ON OD.GOODS_CD = G.GOODS_CD
		        INNER JOIN TB_SUPPLY_COMPANY SC ON OD.SUPPLY_COMP_CD = SC.SUPPLY_COMP_CD
		        INNER JOIN TB_SUPPLY_VENDOR SV ON SC.SUPPLY_VENDOR_CD = SV.SUPPLY_VENDOR_CD
		        WHERE  1 = 1
		        AND    ODIH.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{salesStdt},'%Y-%m-%d')
		        AND    ODIH.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{salesEddt},'%Y-%m-%d'), INTERVAL 1 DAY)
		        AND    ODIH.ORD_DTL_STAT IN ('G720_20','G720_50','G720_60') /*판매-배송중,환입-반품완료,환입-교환완료*/
		        AND    SC.SUPPLY_VENDOR_CD = #{supplyVendorCd}
		        <if test="supplyCompCd != null and supplyCompCd != ''">
		        AND    OD.SUPPLY_COMP_CD = #{supplyCompCd}
		        </if>
		       ) OD
		     , TB_DELIVERY_FEE DF
		WHERE  OD.SUPPLY_COMP_CD = DF.SUPPLY_COMP_CD
		AND    OD.ORD_NO = DF.ORD_NO
		AND    OD.OCCUR_DT >= DF.REG_DT /*출고기준의 일자 보다 배송비 발생일자가 적은 넘만*/
		GROUP  BY DATE_FORMAT(OD.OCCUR_DT,'%Y-%m-%d')
		        , OD.SUPPLY_VENDOR_CD
		        , OD.SUPPLY_VENDOR_NM
		        , OD.SUPPLY_COMP_CD
		        , OD.SUPPLY_COMP_NM
		        , DF.DELV_FEE_GB
		        , OD.ORD_NO
		ORDER  BY DATE_FORMAT(OD.OCCUR_DT,'%Y-%m-%d'), OD.SUPPLY_VENDOR_CD, OD.SUPPLY_COMP_CD, DF.DELV_FEE_GB
	</select>
	
	<!-- 입점업체 정산확정 목록 -->
	<select id="getSupplyCompanyFeeConfirmList" parameterType="SupplyCompFee" resultType="SupplyCompFee">
		/* TsaSettle.getSupplyCompanyFeeConfirmList */
		SELECT U.USAC_YM                                                                                                 /*정산연월*/
		     , U.SUPPLY_VENDOR_CD                                                                                        /*공급벤더코드*/
		     , SV.SUPPLY_VENDOR_NM                                                                                       /*공급벤더명*/
		     , U.SELL_QTY                                                                                                /*판매수량*/
		     , U.REAL_SELL_AMT                                                                                           /*실판매금액(=상품총액)*/
		     , U.SELL_FEE_AMT                                                                                            /*수수료*/
		     , U.CPN_DC_AMT                                                                                              /*쿠폰할인금액*/
		     , U.SELF_CPN_DC_AMT                                                                                         /*자사쿠폰분담액*/
		     , U.SUPPLY_COMP_CPN_DC_AMT                                                                                  /*입점쿠폰분담액*/
		     , U.TMTB_DC_AMT                                                                                             /*다다익선할인금액*/
		     , U.SELF_TMTB_DC_AMT                                                                                        /*자사다다익선분담액*/
		     , U.SUPPLY_COMP_TMTB_DC_AMT                                                                                 /*입점다다익선분담액*/
		     , U.BILL_AMT                                                                                                /*계산서발행금액*/
		     , U.SUPPLY_AMT                                                                                              /*공급금액*/
		     , U.TAX_AMT                                                                                                 /*세액*/
		     , U.DELV_FEE                                                                                                /*배송비*/
		     , IFNULL(UR2.ETC_DEDUCT_AMT,0)                                                            AS ETC_DEDUCT_AMT /*기타차감*/
		     , (U.REAL_SELL_AMT - U.SELL_FEE_AMT + U.SELF_CPN_DC_AMT + U.SELF_TMTB_DC_AMT)
		       + U.DELV_FEE - IFNULL(UR2.ETC_DEDUCT_AMT,0)                                             AS GIVE_AMT       /*지급금액*/
		     , IFNULL(UR1.RECEIVABLE_AMT,0)                                                            AS RECEIVABLE_AMT /*미수금*/
		     , (U.REAL_SELL_AMT - U.SELL_FEE_AMT + U.SELF_CPN_DC_AMT + U.SELF_TMTB_DC_AMT)
		       + U.DELV_FEE - IFNULL(UR2.ETC_DEDUCT_AMT,0)
		       - IFNULL(UR1.RECEIVABLE_AMT,0)                                                          AS TOT_GIVE_AMT   /*총지급금액*/
		FROM   (
		        SELECT DATE_FORMAT(U.USAC_DT,'%Y%m')                    AS USAC_YM                 /*정산연월*/
		             , U.SUPPLY_VENDOR_CD                                                          /*공급벤더코드*/
		             , SUM(U.SELL_QTY)                                  AS SELL_QTY                /*판매수량*/
		             , SUM(U.REAL_SELL_AMT)                             AS REAL_SELL_AMT           /*실판매금액(=상품총액)*/
		             , SUM(U.SELL_FEE_AMT)                              AS SELL_FEE_AMT            /*수수료*/
		             , SUM(U.CPN_DC_AMT)                                AS CPN_DC_AMT              /*쿠폰할인금액*/
		             , SUM(U.SELF_CPN_DC_AMT)                           AS SELF_CPN_DC_AMT         /*자사쿠폰분담액*/
		             , SUM(U.SUPPLY_COMP_CPN_DC_AMT)                    AS SUPPLY_COMP_CPN_DC_AMT  /*입점쿠폰분담액*/
		             , SUM(U.TMTB_DC_AMT)                               AS TMTB_DC_AMT             /*다다익선할인금액*/
		             , SUM(U.SELF_TMTB_DC_AMT)                          AS SELF_TMTB_DC_AMT        /*자사다다익선분담액*/
		             , SUM(U.SUPPLY_COMP_TMTB_DC_AMT)                   AS SUPPLY_COMP_TMTB_DC_AMT /*입점다다익선분담액*/
		             , SUM(U.BILL_AMT)                                  AS BILL_AMT                /*계산서발행금액*/
		             , SUM(U.SUPPLY_AMT)                                AS SUPPLY_AMT              /*공급금액*/
		             , SUM(U.TAX_AMT)                                   AS TAX_AMT                 /*세액*/
		             , SUM(U.DELV_FEE)                                  AS DELV_FEE                /*배송비*/
		        FROM   TB_USAC U
		        WHERE  1 = 1
		        AND    U.USAC_DT <![CDATA[>=]]> REPLACE(CONCAT(#{settleYm},'-01'),'-','')
		        AND    U.USAC_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(CONCAT(#{settleYm},'-01'),'%Y-%m-%d'), INTERVAL 1 MONTH)
		        AND    U.SUPPLY_VENDOR_CD = #{supplyVendorCd}
		        GROUP  BY DATE_FORMAT(U.USAC_DT,'%Y%m'), U.SUPPLY_VENDOR_CD
		       ) U
		INNER JOIN TB_SUPPLY_VENDOR SV ON U.SUPPLY_VENDOR_CD = SV.SUPPLY_VENDOR_CD
		LEFT OUTER JOIN TB_USAC_RECEIVE UR1 ON U.USAC_YM = UR1.USAC_YM
		                                   AND U.SUPPLY_VENDOR_CD = UR1.SUPPLY_VENDOR_CD
		LEFT OUTER JOIN (
		                 SELECT OCCUR_YM         AS USAC_YM
		                      , SUPPLY_VENDOR_CD
		                      , SUM(REWARD_AMT)  AS ETC_DEDUCT_AMT
		                 FROM   TB_USAC_DEDUCT
		                 WHERE  OCCUR_YM = REPLACE(#{settleYm},'-','')
		                 GROUP  BY OCCUR_YM, SUPPLY_VENDOR_CD
		                ) UR2 ON U.USAC_YM = UR2.USAC_YM
		                     AND U.SUPPLY_VENDOR_CD = UR2.SUPPLY_VENDOR_CD
	</select>

</mapper>