<?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.TssPosDao">

	<!-- 매장로그인정보 조회   -->
	<select id="getStoreLoginInfo" parameterType="Pos" resultType="Pos">
		/*TssPos.getStoreLoginInfo*/
		SELECT DELV_LOC_CD
		     , DELV_LOC_NM
		     , DELV_LOC_CLSF
		     , SUPPLY_COMP_CD
		     , DELV_FEE_RATE
		FROM   TB_DELIVERY_LOC
		WHERE  DELV_LOC_CD   = #{delvLocCd}
		 AND    DELV_LOC_CLSF = 'G024_21' -- 직송매장
		AND    USE_YN        = 'Y'
	</select>

	<!-- 출고대기목록 조회   -->
	<select id="getPosDeliveryList" parameterType="Pos" resultType="Pos">
		/*TssPos.getPosDeliveryList*/
		SELECT 
			  A.ORD_NO 
			, A.ORD_NM 
			, B.ORD_DTL_NO
			, C.OPT_CD1 
			, C.OPT_CD2
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY  AS SALE_QTY
			, (B.ORD_QTY - B.CNCL_RTN_QTY)  AS ORD_QTY
			, DATE_FORMAT(A.ORD_DT,'%Y%m%d%H%i%s') AS ORD_DT
			, D.RECIP_NM 
			, D.RECIP_PHNNO 
			, D.RECIP_TELNO 
			, D.RECIP_ZIPCODE 
			, D.RECIP_BASE_ADDR 
			, D.RECIP_DTL_ADDR 
			, CONCAT(D.RECIP_BASE_ADDR ,' ',D.RECIP_DTL_ADDR  ) AS RECIP_ADDR
			, D.DELV_MEMO 
			, B.GOODS_CD 
			, E.GOODS_NM 
			, E.GOODS_NUM 
			, (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG I WHERE I.GOODS_CD = B.GOODS_CD AND I.COLOR_CD = C.OPT_CD1 AND I.DEFAULT_IMG_YN = 'Y') AS IMG_PATH1
			, G.DELV_AR_GB 
			, G.DELV_AR_GB AS DELV_AR_GB_Y  
			, G.DELV_AR_GB AS DELV_AR_GB_N
			, B.ORD_DTL_STAT 
			, B.DELV_STDT 
			, B.INVOICE_NO 
			, B.GOODS_TYPE
			, FN_GET_CODE_NM('G056', B.GOODS_TYPE) AS GOODS_TYPE_NM
			, CASE WHEN B.SHIP_COMP_CD IS NULL THEN 'D1029' ELSE B.SHIP_COMP_CD END AS SHIP_COMP_CD
			, G.REJECT_REASON 
			, CASE WHEN (B.ORD_DTL_STAT = 'G013_30' OR B.ORD_DTL_STAT = 'G013_40') AND G.DELV_AR_GB ='Y' THEN 'Y' ELSE 'N' END  AS DELV_AR_GB_CNL
			, #{delvLocCd}   AS DELV_LOC_CD
			, #{delvFeeRate} AS DELV_FEE_RATE
		  FROM TB_ORDER             A
		  JOIN TB_ORDER_DETAIL      B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_DTL_NO = C.ORD_DTL_NO 
		  JOIN TB_DELIVERY_ADDR     D ON B.DELV_ADDR_SQ = D.DELV_ADDR_SQ 
		  JOIN TB_GOODS             E ON B.GOODS_CD = E.GOODS_CD 
		  JOIN TB_DELIVERY_ASSIGN   G ON B.ORD_NO = G.ORD_NO AND B.ORD_DTL_NO = G.ORD_DTL_NO AND B.DELV_LOC_CD = G.DELV_LOC_CD 
		 WHERE 1=1
		   AND B.DELV_LOC_CD  = #{delvLocCd} 
			<if test="ordDtlStat != null and ordDtlStat != ''">
			  <if test='ordDtlStat == "all"'>
			AND    B.ORD_DTL_STAT IN ('G013_30','G013_40')
			</if>
			<if test='ordDtlStat != "all"'>
			AND    B.ORD_DTL_STAT = #{ordDtlStat}
			</if>
			</if>
			<if test="delvArGb != null and delvArGb != ''">
			AND    G.DELV_AR_GB = #{delvArGb}
			</if>
			<if test="ordNo != null and ordNo != ''"> 
			AND    A.ORD_NO = #{ordNo}
			</if>
			<if test="waitOrdStDate != null and waitOrdStDate != '' and waitOrdEdDate != null and waitOrdEdDate != ''">
			AND    G.REG_DT BETWEEN DATE_FORMAT(replace(#{waitOrdStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{waitOrdEdDate},'-',''),'%Y%m%d%H%i%s'), INTERVAL 1 DAY)
			</if>
			<if test="deliveryStDate != null and deliveryStDate != '' and deliveryEdDate != null and deliveryEdDate != ''">
			AND    B.REG_DT BETWEEN DATE_FORMAT(replace(#{deliveryStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{deliveryEdDate},'-',''),'%Y%m%d%H%i%s'),INTERVAL 1 DAY)
			</if>
			<if test="confirmStDate != null and confirmStDate != '' and confirmEdDate != null and confirmEdDate != ''">
			AND    B.REG_DT BETWEEN DATE_FORMAT(replace(#{confirmStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{confirmEdDate},'-',''),'%Y%m%d%H%i%s'),INTERVAL 1 DAY)
			</if>			
			<if test="recipNm != null and recipNm != ''">
			AND    D.RECIP_NM = #{recipNm}
			</if>
			AND    E.SELF_GOODS_YN = 'Y'
			AND    E.SUPPLY_COMP_CD = #{supplyCompCd}

		   
	</select>

	<!-- 확정대기목록 조회   -->
	<select id="getPosConfirmList" parameterType="Pos" resultType="Pos">
		/*TssPos.getPosConfirmList*/
		SELECT 
			  A.ORD_NO 
			, A.ORD_NM 
			, B.ORD_DTL_NO
			, C.OPT_CD1 
			, C.OPT_CD2
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY  AS SALE_QTY
			, (B.ORD_QTY - B.CNCL_RTN_QTY)  AS ORD_QTY
			, DATE_FORMAT(A.ORD_DT,'%Y%m%d%H%i%s') AS ORD_DT
			, D.RECIP_NM 
			, D.RECIP_PHNNO 
			, D.RECIP_TELNO 
			, D.RECIP_ZIPCODE 
			, D.RECIP_BASE_ADDR 
			, D.RECIP_DTL_ADDR 
			, CONCAT(D.RECIP_BASE_ADDR ,' ',D.RECIP_DTL_ADDR  ) AS RECIP_ADDR
			, D.DELV_MEMO 
			, B.GOODS_CD 
			, E.GOODS_NM 
			, E.GOODS_NUM 
			, (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG I WHERE I.GOODS_CD = B.GOODS_CD AND I.COLOR_CD = C.OPT_CD1 AND I.DEFAULT_IMG_YN = 'Y') AS IMG_PATH1
			, G.DELV_AR_GB 
			, G.DELV_AR_GB AS DELV_AR_GB_Y  
			, G.DELV_AR_GB AS DELV_AR_GB_N
			, B.ORD_DTL_STAT 
			, B.DELV_STDT 
			, B.INVOICE_NO 
			, B.GOODS_TYPE
			, FN_GET_CODE_NM('G056', B.GOODS_TYPE) AS GOODS_TYPE_NM
			, CASE WHEN B.SHIP_COMP_CD IS NULL THEN 'D1029' ELSE B.SHIP_COMP_CD END AS SHIP_COMP_CD
			, G.REJECT_REASON 
			, CASE WHEN (B.ORD_DTL_STAT = 'G013_30' OR B.ORD_DTL_STAT = 'G013_40') AND G.DELV_AR_GB ='Y' THEN 'Y' ELSE 'N' END  AS DELV_AR_GB_CNL
			, #{delvLocCd}   AS DELV_LOC_CD
			, #{delvFeeRate} AS DELV_FEE_RATE
		  FROM TB_ORDER             A
		  JOIN TB_ORDER_DETAIL      B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_DTL_NO = C.ORD_DTL_NO 
		  JOIN TB_DELIVERY_ADDR     D ON B.DELV_ADDR_SQ = D.DELV_ADDR_SQ 
		  JOIN TB_GOODS             E ON B.GOODS_CD = E.GOODS_CD 
		  JOIN TB_DELIVERY_ASSIGN   G ON B.ORD_NO = G.ORD_NO AND B.ORD_DTL_NO = G.ORD_DTL_NO AND B.DELV_LOC_CD = G.DELV_LOC_CD 
		 WHERE 1=1
		   AND B.DELV_LOC_CD  = #{delvLocCd} 
			<if test="ordDtlStat != null and ordDtlStat != ''">
			  <if test='ordDtlStat == "all"'>
			AND    B.ORD_DTL_STAT IN ('G013_40','G013_50','G013_60')
			</if>
			<if test='ordDtlStat != "all"'>
			AND    B.ORD_DTL_STAT = #{ordDtlStat}
			</if>
			</if>
			<if test="delvArGb != null and delvArGb != ''">
			AND    G.DELV_AR_GB = #{delvArGb}
			</if>
			<if test="ordNo != null and ordNo != ''"> 
			AND    A.ORD_NO = #{ordNo}
			</if>
			<if test="waitOrdStDate != null and waitOrdStDate != '' and waitOrdEdDate != null and waitOrdEdDate != ''">
			AND    G.REG_DT BETWEEN DATE_FORMAT(replace(#{waitOrdStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{waitOrdEdDate},'-',''),'%Y%m%d%H%i%s'), INTERVAL 1 DAY)
			</if>
			<if test="deliveryStDate != null and deliveryStDate != '' and deliveryEdDate != null and deliveryEdDate != ''">
			AND    B.REG_DT BETWEEN DATE_FORMAT(replace(#{deliveryStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{deliveryEdDate},'-',''),'%Y%m%d%H%i%s'),INTERVAL 1 DAY)
			</if>
			<if test="confirmStDate != null and confirmStDate != '' and confirmEdDate != null and confirmEdDate != ''">
			AND    B.REG_DT BETWEEN DATE_FORMAT(replace(#{confirmStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{confirmEdDate},'-',''),'%Y%m%d%H%i%s'),INTERVAL 1 DAY)
			</if>			
			<if test="recipNm != null and recipNm != ''">
			AND    D.RECIP_NM = #{recipNm}
			</if>
			AND    E.SELF_GOODS_YN = 'Y'
			AND    E.SUPPLY_COMP_CD = #{supplyCompCd}

		   
	</select>
	
	<!--출고지정 수정 -->
	<update id="updateDeliveryAssign" parameterType="Pos">
		/*TssPos.updateDeliveryAssign*/
		UPDATE TB_DELIVERY_ASSIGN
		SET    DELV_AR_GB = #{delvArGb}
		     , DELV_AR_NO = #{delvArNo}
		     , DELV_AR_DT = NOW()
		     , UPD_DT = NOW()
		     , UPD_NO = #{updNo}
		<if test="rejectReason != null and rejectReason != ''">
		     , REJECT_REASON = #{rejectReason}
		</if>
		WHERE  ORD_DTL_NO = #{ordDtlNo}
		AND    DELV_LOC_CD = #{delvLocCd}
		AND    DELV_AR_GB = 'P'
	</update>

	<!--출고지정 HST-->
	<insert id="createDeliveryAssignHst">
		/* TssPos.createDeliveryAssignHst */
		INSERT INTO	TB_DELIVERY_ASSIGN_HST (
			  DELV_ASSIGN_SQ
			, ORD_DTL_NO
			, DELV_LOC_CD
			, ASSIGN_QTY
			, DELV_AR_GB
			, DELV_AR_NO
			, DELV_AR_DT
			, REJECT_REASON
			, REG_NO
			, REG_DT
			, ORD_NO
			, UPD_NO
			, UPD_DT
			, HST_REG_DT
		)
		VALUES (
			  (SELECT MAX(DELV_ASSIGN_SQ) FROM TB_DELIVERY_ASSIGN A WHERE A.ORD_NO = #{ordNo} AND A.ORD_DTL_NO = #{ordDtlNo} AND A.DELV_LOC_CD = #{delvLocCd} )
			, #{ordDtlNo}
			, #{delvLocCd}
			, #{assignQty}
			, #{delvArGb}
			, #{delvArNo}
			, NOW()
			, #{rejectReason}
			, #{regNo}
			, NOW()
			, #{ordNo}
			, #{regNo}
			, NOW()
			, NOW()
		)
	</insert>

	<!--주문상세 변경-->
	<update id="updateOrderDetail" parameterType="Pos">
		/* TssPos.updateOrderDetail */
		UPDATE TB_ORDER_DETAIL
		SET   UPD_DT = NOW()
			, UPD_NO = #{updNo}
			, ORD_DTL_STAT = #{ordDtlStat}
		<if test='delvArGb == "Y"'>
			, DELV_LOC_CD      = #{delvLocCd}
			, DELV_ASSIGN_DT   = NOW()
			, DELV_ASSIGN_STAT = 'Y'
		</if>
		<if test='delvArGb == "N"'>
			, DELV_LOC_CD       = NULL
			, DELV_ASSIGN_DT    = NULL
			, DELV_ASSIGN_STAT  = 'P'
			, SHIP_COMP_CD      = NULL
			, INVOICE_NO        = NULL
			, INVOICE_SEND_YN   = 'N'
		</if>
		<if test='invoiceNo != "" and invoiceNo != null '>
			, SHIP_COMP_CD    = #{shipCompCd}
			, INVOICE_NO      = #{invoiceNo}
			, INVOICE_SEND_YN = 'Y'
		</if>
		WHERE ORD_DTL_NO = #{ordDtlNo}
	</update>
	
	<!-- 출고금지상품 등록 -->
	<insert id="createBangoods" parameterType="Pos">
		/* TssPos.createBangoods */
		INSERT INTO TB_DELIVERY_BAN_GOODS (
			  DELV_LOC_CD
			, GOODS_CD
			, OPT_CD1
			, OPT_CD2
			, DELV_BAN_STDT
			, DELV_BAN_EDDT
			, DEL_YN
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
		)
		VALUES (
			  #{delvLocCd}
			, #{goodsCd}
			, #{optCd1}
			, #{optCd2}
			, DATE_FORMAT(replace(#{delvBanStdt},'-',''),'%Y-%m-%d')
			, DATE_FORMAT(replace(#{delvBanEddt},'-',''),'%Y-%m-%d')
			, 'N'
			, #{regNo}
			, NOW()
			, #{updNo}
			, NOW()
		)
	</insert>	
	
	<!-- 주문상세 아이템 조회   -->
	<select id="getOrdDtlItemList" parameterType="Pos" resultType="Pos">
		/*TssPos.getOrdDtlItemList*/
		SELECT 
			  B.DELV_LOC_CD 
			, C.ITEM_CD AS GOODS_CD
			, C.OPT_CD1 
			, C.OPT_CD2 
		  FROM TB_ORDER A 
		  JOIN TB_ORDER_DETAIL B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		 WHERE B.ORD_DTL_NO  = #{ordDtlNo}
	</select>
	
	<!-- 출고처 체크    -->
	<select id="getDelvLocChk" parameterType="Pos" resultType="Pos">
		/*TssPos.getDelvLocChk*/
		SELECT CASE WHEN COUNT(*) > 0 THEN 'S' ELSE 'F' END AS DELV_LOC_CD_CHK
			 , CASE WHEN ORD_DTL_STAT ='G013_35' THEN 'Y' ELSE 'N'  END AS CHK_YN
		  FROM TB_ORDER_DETAIL
		 WHERE ORD_DTL_NO  = #{ordDtlNo} 
		   AND DELV_LOC_CD = #{delvLocCd} 
	</select>
	
	<!-- 주문상세상태 조회  -->
	<select id="getOrderDtlStat"  resultType="Pos">
		/* TssPos.getOrderDtlStat */
		SELECT A.ORD_NO
		     , B.ORD_DTL_NO
		     , B.INVOICE_NO
		     , B.ORD_DTL_STAT
		     , B.DELV_LOC_CD
		FROM   TB_ORDER A
		     , TB_ORDER_DETAIL B
		WHERE  A.ORD_NO = B.ORD_NO
		AND    B.ORD_DTL_NO = #{ordDtlNo}
	</select>	
	
	<!--CnPlus 엑셀 다운로드 조회 -->
	<select id="getOrderExcelList" parameterType="Pos" resultType="paramMap">
		/* TssPos.getOrderExcelList */
        SELECT ' '                                   AS RSRVT_GB     -- 예약구분: ( 일반/ 반품) 공란이면 선택한 형식적용
             , ' '                                   AS SCHDL_CLDT   -- 집하예정일 (공란이면 자동 적용)
             , C.RECIP_NM                                           -- 받는분 성명
             , C.RECIP_PHNNO                                        -- 받는분 전화번호 
             , C.RECIP_TELNO                                        -- 받는분기타연락처 
             , C.RECIP_ZIPCODE                                      -- 받는분 우편번호 
             , CONCAT(C.RECIP_BASE_ADDR,' ',C.RECIP_DTL_ADDR)  AS RECIP_ADDR   -- 받는분 주소(전체)
             , ' ' AS INVOICE_NO                                         -- 운송장번호 
             , A.ORD_NO                                             -- 고객주문번호 
             , CONCAT(D.GOODS_NM,'/',M.WMS_COLOR_NM ,'/',G.OPT_CD2 ,'  x', ((B.ORD_QTY-B.CNCL_RTN_QTY) * G.ITEM_QTY))  AS GOODS_NM  -- 품목명 
             , ' '                                   AS BOX_QTY      -- 박스수량 (공란이면 1로인식)
             , ' '                                   AS BOX_TYPE     -- 박스타입 (공란이면 화면기준적용) 
             , ' '                                   AS FEE          -- 기본운임 (운임합계) 
             , C.DELV_MEMO                           AS DELV_MEMO    -- 배달메시지1
             , ' '                                   AS DELV_MEMO2   -- 배달메시지2 
             , CONCAT(D.GOODS_CD,'/',M.WMS_COLOR_NM ,'/',G.OPT_CD2 ,'  x', ((B.ORD_QTY-B.CNCL_RTN_QTY) * G.ITEM_QTY)) AS GOODS_CD
        FROM  TB_ORDER A
        JOIN  TB_ORDER_DETAIL B       ON A.ORD_NO = B.ORD_NO 
        JOIN  TB_ORDER_DETAIL_ITEM G  ON B.ORD_NO = G.ORD_NO AND B.ORD_DTL_NO = G.ORD_DTL_NO 
        JOIN  TB_DELIVERY_ADDR C      ON B.DELV_ADDR_SQ = C.DELV_ADDR_SQ 
        JOIN  TB_GOODS D              ON D.GOODS_CD = G.ITEM_CD 
        JOIN  TB_WMS_COLOR_MAPPING M  ON B.SUPPLY_COMP_CD = M.SUPPLY_COMP_CD  AND G.OPT_CD1 = M.COLOR_CD 
       WHERE B.ORD_DTL_NO IN
		<foreach collection="ordDtlNoArr" item="item" index="index"  open="(" close=")" separator=",">
		      #{item}
		</foreach>
	</select>
	
    <!-- 주문상세송장 -  송장정보 저장 -->
    <insert id="insertTbOderDetailInvoice" parameterType="Pos">
        /* TssPos.insertTbOderDetailInvoice */
		INSERT INTO TB_ORDER_DETAIL_INVOICE
		(
		      ORD_NO
		    , ORD_DTL_NO
		    , INVOICE_NO
		    , ADD_INVOICE_YN
		    , SWT_TRC_SEND_YN
		    , DEL_YN
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		)
		VALUES
		(
		      #{ordNo}
		    , #{ordDtlNo}
		    , #{invoiceNo}
		    , 'N'
		    , 'N'
		    , #{delYn}
		    , #{regNo}
		    , NOW()
		    , #{regNo}
		    , NOW()
		)
    </insert>
    		
	<!--출고지정 취소 -->
	<update id="updateDeliveryAssignCancel" parameterType="Pos">
		/*TsaDelivery.updateDeliveryAssignCancel*/
		UPDATE TB_DELIVERY_ASSIGN
		SET    DELV_AR_GB = #{delvArGb}
		     , DELV_AR_NO = #{delvArNo}
		     , DELV_AR_DT = NOW()
		     , UPD_DT     = NOW()
		     , UPD_NO     = #{updNo}
		<if test="rejectReason != null and rejectReason != ''">
		     , REJECT_REASON = #{rejectReason}
		</if>
		WHERE  ORD_DTL_NO = #{ordDtlNo}
		AND    DELV_LOC_CD = #{delvLocCd}
		AND    DELV_AR_GB = 'Y'
	</update>
	
	<!-- ERP 주문상세 아이템 조회   -->
	<select id="getSendErpList" parameterType="Pos" resultType="Pos">
		/*TssPos.getSendErpList*/
		SELECT 
			  B.DELV_LOC_CD                               AS CD_RTOUTSHOP
			, C.ITEM_CD                                   AS CD_STYLE
			, C.OPT_CD1                                   AS CD_COLOR
			, C.OPT_CD2                                   AS CD_SIZE
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY   AS QT_RTOUT
			, B.INVOICE_NO                                AS DS_INVOICE
			, DATE_FORMAT(NOW(),'%Y%m%d%H%i%s')           AS NO_IF
			, DATE_FORMAT(NOW(),'%Y%m%d')                 AS DT_SALE
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY   AS QT_SALE
			, C.ORD_AMT - C.CNCL_RTN_AMT                  AS AM_ACSALE
			, ''                                          AS DS_REMARK
			, C.ORD_DTL_ITEM_SQ 
		  FROM TB_ORDER A 
		  JOIN TB_ORDER_DETAIL B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		 WHERE B.ORD_DTL_NO  = #{ordDtlNo}
	</select>
		
	<!-- 정산 조회   -->
	<select id="getPosUsacList" parameterType="Pos" resultType="Pos">
		/*TssPos.getPosUsacList*/
		SELECT 
			  B.DELV_LOC_CD                               AS CD_RTOUTSHOP
			, C.ITEM_CD                                   AS CD_STYLE
			, C.OPT_CD1                                   AS CD_COLOR
			, C.OPT_CD2                                   AS CD_SIZE
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY   AS QT_RTOUT
			, B.INVOICE_NO                                AS DS_INVOICE
			, DATE_FORMAT(NOW(),'%Y%m%d%H%i%s')           AS NO_IF
			, DATE_FORMAT(NOW(),'%Y%m%d')                 AS DT_SALE
			, (B.ORD_QTY - B.CNCL_RTN_QTY) * C.ITEM_QTY   AS QT_SALE
			, C.ORD_AMT - C.CNCL_RTN_AMT                  AS AM_ACSALE
			, ''                                          AS DS_REMARK
			, C.ORD_DTL_ITEM_SQ 
		  FROM TB_ORDER A 
		  JOIN TB_ORDER_DETAIL B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		 WHERE B.ORD_DTL_NO  = #{ordDtlNo}
	</select>
			
	<!-- 매장거부 목록 -->
	<select id="getDeliveryAssignList" parameterType="Pos" resultType="Pos">
		/* TssPos.getDeliveryAssignList */
		SELECT DA.ORD_NO
		    , FN_GET_CODE_NM('G300', DA.REJECT_REASON) AS REJECT_REASON
		    , G.BRAND_CD
		    , GI.SYS_IMG_NM AS IMG_PATH1
		    , G.GOODS_NM
		    , ODI.ITEM_CD AS GOODS_CD
		    , ODI.OPT_CD1 
		    , ODI.OPT_CD2		    
		    , DATE_FORMAT(DA.DELV_AR_DT, '%Y-%m-%d %H:%i:%s') AS DELV_BAN_STDT
		FROM TB_DELIVERY_ASSIGN DA
		JOIN TB_ORDER_DETAIL_ITEM ODI ON DA.ORD_DTL_NO = ODI.ORD_DTL_NO 
		JOIN TB_GOODS G               ON ODI.ITEM_CD = G.GOODS_CD 
		LEFT OUTER JOIN TB_GOODS_IMG GI ON G.GOODS_CD = GI.GOODS_CD AND GI.DEFAULT_IMG_YN ='Y'
		WHERE DA.DELV_AR_GB ='N'
		AND   G.SUPPLY_COMP_CD = #{supplyCompCd}
		<if test='goodsCd != null and goodsCd != ""'>
		AND G.GOODS_CD LIKE CONCAT(UPPER(#{goodsCd}), '%')
		</if>
		AND DA.DELV_LOC_CD = #{delvLocCd}
		<if	test="banDelvStDate != null and banDelvStDate != '' and banDelvEdDate != null and banDelvEdDate != ''">
		AND DA.DELV_AR_DT BETWEEN DATE_FORMAT(replace(#{banDelvStDate},'-',''),'%Y%m%d%H%i%s') AND DATE_ADD(DATE_FORMAT(replace(#{banDelvEdDate},'-',''),'%Y%m%d%H%i%s'), INTERVAL 1 DAY)
		</if>
		ORDER BY DA.DELV_AR_DT DESC
	</select>			
	
	<!-- WMS 출고요청 대상 조회      -->	
	<select id="getWmsDeliveryList" resultType="Pos">
		/*TssPos.getWmsDeliveryList*/
		SELECT 
			  A.ORD_NO                            AS OrderNo
			, B.DELV_LOC_CD                       AS  DeliveryTypeCd
			, '정상'                               AS  IssueTypeCd
			, '30'                                AS StatusCd -- 배송지시
			, D.CUST_ID                           AS LoginId
			, A.ORD_NM                            AS CustomerName
			, A.ORD_DT                            AS DateOrdered
			, FN_GET_CODE_NM('G110',D.CUST_GRADE) AS  Grade 
			, E.RECIP_NM                          AS Receiver
			, E.RECIP_TELNO                       AS ReceiverTelNum
			, E.RECIP_PHNNO                       AS ReceiverCellNum
			, E.RECIP_ZIPCODE                     AS DeliveryPostalCode
			, E.RECIP_BASE_ADDR                   AS DeliveryAddr1
			, E.RECIP_DTL_ADDR                    AS DeliveryAddr2
			, CASE WHEN B.SHOT_DELV_YN ='Y' THEN '14' ELSE '05' END                               AS LogisticsNo
			, CASE WHEN B.SHOT_DELV_YN ='Y' THEN '디디로직스'  ELSE 'CJ 대한통운' END                  AS LogisticsName
			, E.DELV_MEMO                         AS DeliveryMemo
			, CASE WHEN (SELECT COUNT(*) -1 FROM TB_ORDER_DETAIL WHERE ORD_NO = A.ORD_NO) > 0 THEN CONCAT(F.GOODS_NM,' 외  ', (SELECT COUNT(*) -1 FROM TB_ORDER_DETAIL WHERE ORD_NO = A.ORD_NO) , '건') ELSE F.GOODS_NM END   AS ProductSummary
			,  0                                  AS IsCancel
			,  0                                  AS IsChangeAddress
			,  0                                  AS IsReOrder
			, CASE WHEN A.MALL_GB = 'G011_20' THEN B.EXTMALL_ID ELSE 'APISHOP_0054' END                  AS OrderPath -- 주문경로 (확인필요!)
			, CASE WHEN B.SHOT_DELV_YN ='Y' THEN '20' ELSE '10' END                                 	 AS DeliveryClassCd 
			, CASE WHEN B.ORD_EXCH_GB ='O'  THEN '10' ELSE '20' END                                 	 AS ShippingTypeCd
			, B.GIFT_PACK_YN                      AS IsBeautyDelivery
			, ''                                                                                         AS DeliveryAddrNew  -- 도로명 주소 (확인필요!)
			, (SELECT PROVIDER_NO FROM TB_SUPPLY_COMPANY BB WHERE BB.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD ) AS ProviderNo
			, C.PRODUCT_NO                        AS ProductNo
			, C.PRODUCT_CODE                      AS ProductCode
			, (SELECT CC.GOODS_NM FROM TB_GOODS CC WHERE CC.GOODS_CD = C.ITEM_CD) AS ProductName    -- 상품명 
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN '세트' ELSE '일반' END 		  AS ProductTypeCd  -- 상품타입
			, C.OPT_CD                            AS SKUCode       
			, CONCAT(C.OPT_CD1,'/',C.OPT_CD2)     AS SKUValue
			, B.CURR_PRICE + B.OPT_ADD_PRICE      AS UnitPrice
			, (B.ORD_QTY -B.CNCL_RTN_QTY) * C.ITEM_QTY  AS Qty
			, B.DELV_LOC_CD                       AS StockMgmtTypeCd
			, 'N'                                 AS GiftYn  -- 사은품여부 
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN B.GOODS_CD ELSE '' END  AS SetProductCode -- 세트상품코드 
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN 'Y' ELSE 'N' END        AS SetProductYn   -- 세트상품여부 
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN F.GOODS_NM ELSE '' END  AS SetproductName -- 세트상품명 
			, B.ORD_DTL_NO                        AS OrderDtlNo
			, 0                                   AS Cqty
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN (B.ORD_QTY -B.CNCL_RTN_QTY) ELSE 0 END  AS SetQty        -- 세트상품 수량
			, CASE WHEN B.GOODS_TYPE = 'G056_S' THEN C.ITEM_QTY ELSE 0 END                   AS SetItemQty    -- 세트 구성수량
			, (SELECT MAX(CASE WHEN PG_GB ='NAVER_ORDER' THEN 'NAVER' ELSE 'KCP' END)   FROM TB_PAYMENT WHERE ORD_NO= A.ORD_NO AND PAY_GB ='O'  ) AS PAY_GB 
			, (SELECT WMS_SHIP_COMP_CD FROM TB_SHIP_COMPANY WHERE SHIP_COMP_CD = B.SHIP_COMP_CD) AS WMS_SHIP_COMP_CD
			, B.INVOICE_NO
		  FROM TB_ORDER              A 
		  JOIN TB_ORDER_DETAIL       B ON A.ORD_NO = B.ORD_NO 
		  JOIN TB_ORDER_DETAIL_ITEM  C ON B.ORD_NO = C.ORD_NO AND B.ORD_DTL_NO = C.ORD_DTL_NO 
		  LEFT JOIN TB_CUSTOMER      D ON A.CUST_NO = D.CUST_NO 
		  JOIN TB_DELIVERY_ADDR      E ON B.DELV_ADDR_SQ = E.DELV_ADDR_SQ 
		  JOIN TB_GOODS              F ON B.GOODS_CD = F.GOODS_CD 
		  JOIN TB_DELIVERY_LOC       G ON B.DELV_LOC_CD = G.DELV_LOC_CD AND B.SUPPLY_COMP_CD = G.SUPPLY_COMP_CD
		 WHERE A.ORD_NO        = #{ordNo} 
		   AND B.ORD_DTL_STAT  = 'G013_50' -- 출고처지정
		   AND G.DELV_LOC_CLSF = 'G024_21' -- 직송매장
		  ORDER BY B.ORD_DTL_NO 
		   
	</select>		
		
</mapper>