<?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.oqpo.api.mapper.salesmng.SalesMngMapper">

    <select id="selectStoreSalesGridList" resultType="com.oqpo.api.web.dto.response.salesmng.StoreSalesGridResponse">
        /* SalesMngMapper.selectStoreSalesGridList */
        SELECT base.brand_id, base.brand_nm
            , base.store_id, base.store_nm
            , base.whs_qty	-- 구매수량
            , base.supply_amt  -- 공급가액
            , base.store_amt   -- 매출금액
            , base.income_amt  -- 수입금액
            , base.whs_qty1	-- 판품수량
            , base.supply_amt1	-- 반품공급가액
            , base.store_amt1  -- 반품금액
            , base.income_amt1	-- 반품손실금액
            , base.income_amt + base.income_amt1 AS tot_income_amt -- 총매출금액
        FROM (
            SELECT a.BRAND_ID, FN_BRAND_NM(a.brand_id) AS brand_nm
                , a.STORE_ID, FN_STORE_NM(a.brand_id, a.store_id) AS store_nm
                , SUM(a.whs_qty) AS whs_qty
                , SUM(a.supply_amt) AS supply_amt
                , SUM(a.base_amt) AS base_amt
                , SUM(a.store_amt) AS store_amt
                , SUM(a.store_amt - a.supply_amt) AS income_amt
                , SUM(a.whs_qty1) AS whs_qty1
                , SUM(a.supply_amt1) AS supply_amt1
                , SUM(a.base_amt1) AS base_amt1
                , SUM(a.store_amt1) AS store_amt1
                , SUM(a.store_amt1 - a.supply_amt1) AS income_amt1
            FROM (
                SELECT smbi.BRAND_ID, smbi.STORE_ID, smdp.ITEM_ID, smdp.ITEM_NM
                    , smdp.WHS_QTY
                    , smdp.WHS_QTY * biui.UNIT_AMT AS supply_amt
                    , smdp.WHS_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt
                    , smdp.WHS_QTY * siui.UNIT_AMT AS store_amt
                    , 0 AS whs_qty1
                    , 0 AS supply_amt1
                    , 0 AS base_amt1
                    , 0 AS store_amt1
                FROM stl_mgnt_base_info smbi
                INNER JOIN stl_mgnt_dtl_ptcl smdp ON smbi.STTL_MGNT_UNQ_NO = smdp.STTL_MGNT_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON smdp.ITEM_ID = biui.ITEM_ID AND smbi.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON smdp.ITEM_ID = siui.ITEM_ID AND smbi.BRAND_ID = siui.BRAND_ID AND smbi.STORE_ID = siui.STORE_ID
                INNER JOIN pch_req_base_info prbi ON smdp.PCH_REQ_UNQ_NO = prbi.PCH_REQ_UNQ_NO
                WHERE 1 = 1
                AND smbi.BRAND_ID = #{sBrandId}
                <if test="sStoreId != null and sStoreId != ''">
                    AND smbi.STORE_ID = #{sStoreId}
                </if>
                AND smbi.STTL_ST_CD IN ('ST10', 'ST20')
                AND prbi.PCH_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
                AND prbi.PCH_REQ_DT BETWEEN #{fromDt} AND #{toDt}
                UNION ALL
                SELECT pim.BRAND_ID, pim.STORE_ID, pdp.ITEM_ID, pdp.ITEM_NM
                    , 0 AS whs_qty
                    , 0 AS supply_amt
                    , 0 AS base_amt
                    , 0 AS store_amt
                    , -1 * pdp.RODR_QTY AS WHS_QTY1
                    , -1 * pdp.RODR_QTY * biui.UNIT_AMT AS supply_amt1
                    , -1 * pdp.RODR_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt1
                    , -1 * pdp.RODR_QTY * siui.UNIT_AMT AS store_amt1
                FROM pick_info_mgnt pim 
                INNER JOIN pick_dtl_ptcl pdp ON pim.COL_PICK_UNQ_NO = pdp.COL_PICK_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON pdp.ITEM_ID = biui.ITEM_ID AND pim.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON pdp.ITEM_ID = siui.ITEM_ID AND pim.BRAND_ID = siui.BRAND_ID AND pim.STORE_ID = siui.STORE_ID
                INNER JOIN rtn_req_base_info rrbi ON pdp.RTN_REQ_UNQ_NO = rrbi.RTN_REQ_UNQ_NO
                WHERE 1 = 1
                AND pim.BRAND_ID = #{sBrandId}
                <if test="sStoreId != null and sStoreId != ''">
                    AND pim.STORE_ID = #{sStoreId}
                </if>
                AND pim.pick_st_cd IN ('PICK0', 'PICK1')
                AND rrbi.RTN_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
                AND rrbi.RTN_REQ_DT BETWEEN #{fromDt} AND #{toDt}
            ) a
            GROUP BY a.BRAND_ID, a.STORE_ID
        ) base
        WHERE 1 = 1
        ORDER BY base.store_nm ASC
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectStoreSalesGridCnt" resultType="int">
        /* SalesMngMapper.selectStoreSalesGridCnt */
        SELECT COUNT(*)
        FROM (
            SELECT a.BRAND_ID, FN_BRAND_NM(a.brand_id) AS brand_nm
            , a.STORE_ID, FN_STORE_NM(a.brand_id, a.store_id) AS store_nm
            , SUM(a.whs_qty) AS whs_qty
            , SUM(a.supply_amt) AS supply_amt
            , SUM(a.base_amt) AS base_amt
            , SUM(a.store_amt) AS store_amt
            , SUM(a.store_amt - a.supply_amt) AS income_amt
            , SUM(a.whs_qty1) AS whs_qty1
            , SUM(a.supply_amt1) AS supply_amt1
            , SUM(a.base_amt1) AS base_amt1
            , SUM(a.store_amt1) AS store_amt1
            , SUM(a.store_amt1 - a.supply_amt1) AS income_amt1
            FROM (
            SELECT smbi.BRAND_ID, smbi.STORE_ID, smdp.ITEM_ID, smdp.ITEM_NM
            , smdp.WHS_QTY
            , smdp.WHS_QTY * biui.UNIT_AMT AS supply_amt
            , smdp.WHS_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt
            , smdp.WHS_QTY * siui.UNIT_AMT AS store_amt
            , 0 AS whs_qty1
            , 0 AS supply_amt1
            , 0 AS base_amt1
            , 0 AS store_amt1
            FROM stl_mgnt_base_info smbi
            INNER JOIN stl_mgnt_dtl_ptcl smdp ON smbi.STTL_MGNT_UNQ_NO = smdp.STTL_MGNT_UNQ_NO
            INNER JOIN brand_item_unit_info biui ON smdp.ITEM_ID = biui.ITEM_ID AND smbi.BRAND_ID = biui.BRAND_ID
            INNER JOIN store_item_unit_info siui ON smdp.ITEM_ID = siui.ITEM_ID AND smbi.BRAND_ID = siui.BRAND_ID AND smbi.STORE_ID = siui.STORE_ID
            INNER JOIN pch_req_base_info prbi ON smdp.PCH_REQ_UNQ_NO = prbi.PCH_REQ_UNQ_NO
            WHERE 1 = 1
            AND smbi.BRAND_ID = #{sBrandId}
            <if test="sStoreId != null and sStoreId != ''">
                AND smbi.STORE_ID = #{sStoreId}
            </if>
            AND smbi.STTL_ST_CD IN ('ST10', 'ST20')
            AND prbi.PCH_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
            AND prbi.PCH_REQ_DT BETWEEN #{fromDt} AND #{toDt}
            UNION ALL
            SELECT pim.BRAND_ID, pim.STORE_ID, pdp.ITEM_ID, pdp.ITEM_NM
                    , 0 AS whs_qty
                    , 0 AS supply_amt
                    , 0 AS base_amt
                    , 0 AS store_amt
                    , -1 * pdp.RODR_QTY AS WHS_QTY1
                    , -1 * pdp.RODR_QTY * biui.UNIT_AMT AS supply_amt1
                    , -1 * pdp.RODR_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt1
                    , -1 * pdp.RODR_QTY * siui.UNIT_AMT AS store_amt1
                FROM pick_info_mgnt pim 
                INNER JOIN pick_dtl_ptcl pdp ON pim.COL_PICK_UNQ_NO = pdp.COL_PICK_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON pdp.ITEM_ID = biui.ITEM_ID AND pim.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON pdp.ITEM_ID = siui.ITEM_ID AND pim.BRAND_ID = siui.BRAND_ID AND pim.STORE_ID = siui.STORE_ID
                INNER JOIN rtn_req_base_info rrbi ON pdp.RTN_REQ_UNQ_NO = rrbi.RTN_REQ_UNQ_NO
                WHERE 1 = 1
                AND pim.BRAND_ID = #{sBrandId}
            <if test="sStoreId != null and sStoreId != ''">
                AND pim.STORE_ID = #{sStoreId}
            </if>
            AND pim.pick_st_cd IN ('PICK0', 'PICK1')
            AND rrbi.RTN_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
            AND rrbi.RTN_REQ_DT BETWEEN #{fromDt} AND #{toDt}
            ) a
            GROUP BY a.BRAND_ID, a.STORE_ID
        ) base
        WHERE 1 = 1
    </select>

    <select id="selectItemSalesGridList" resultType="com.oqpo.api.web.dto.response.salesmng.ItemSalesGridResponse">
        /* SalesMngMapper.selectItemSalesGridList */
        SELECT base.brand_id, base.brand_nm
            , base.item_id, base.ITEM_NM
            , base.whs_qty	-- 구매수량
            , base.supply_amt  -- 공급가액
            , base.store_amt   -- 매출금액
            , base.income_amt  -- 수입금액
            , base.whs_qty1	-- 판품수량
            , base.supply_amt1	-- 반품공급가액
            , base.store_amt1  -- 반품금액
            , base.income_amt1	-- 반품손실금액
            , base.income_amt + base.income_amt1 AS tot_income_amt -- 총매출금액
        FROM (
            SELECT a.BRAND_ID, FN_BRAND_NM(a.brand_id) AS brand_nm
                , a.ITEM_ID, a.item_nm
                , SUM(a.whs_qty) AS whs_qty
                , SUM(a.supply_amt) AS supply_amt
                , SUM(a.base_amt) AS base_amt
                , SUM(a.store_amt) AS store_amt
                , SUM(a.store_amt - a.supply_amt) AS income_amt
                , SUM(a.whs_qty1) AS whs_qty1
                , SUM(a.supply_amt1) AS supply_amt1
                , SUM(a.base_amt1) AS base_amt1
                , SUM(a.store_amt1) AS store_amt1
                , SUM(a.store_amt1 - a.supply_amt1) AS income_amt1
            FROM (
                SELECT smbi.BRAND_ID, smbi.STORE_ID, smdp.ITEM_ID, smdp.ITEM_NM
                    , smdp.WHS_QTY
                    , smdp.WHS_QTY * biui.UNIT_AMT AS supply_amt
                    , smdp.WHS_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt
                    , smdp.WHS_QTY * siui.UNIT_AMT AS store_amt
                    , 0 AS whs_qty1
                    , 0 AS supply_amt1
                    , 0 AS base_amt1
                    , 0 AS store_amt1
                FROM stl_mgnt_base_info smbi
                INNER JOIN stl_mgnt_dtl_ptcl smdp ON smbi.STTL_MGNT_UNQ_NO = smdp.STTL_MGNT_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON smdp.ITEM_ID = biui.ITEM_ID AND smbi.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON smdp.ITEM_ID = siui.ITEM_ID AND smbi.BRAND_ID = siui.BRAND_ID AND smbi.STORE_ID = siui.STORE_ID
                INNER JOIN pch_req_base_info prbi ON smdp.PCH_REQ_UNQ_NO = prbi.PCH_REQ_UNQ_NO
                WHERE 1 = 1
                AND smbi.BRAND_ID = #{sBrandId}
                <if test="sStoreId != null and sStoreId != ''">
                    AND smbi.STORE_ID = #{sStoreId}
                </if>
                <if test="sItemNm != null and sItemNm != ''">
                    AND smdp.ITEM_NM LIKE CONCAT('%',#{sItemNm},'%')
                </if>
                AND smbi.STTL_ST_CD IN ('ST10', 'ST20')
                AND prbi.PCH_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
                AND prbi.PCH_REQ_DT BETWEEN #{fromDt} AND #{toDt}
                UNION ALL
                SELECT pim.BRAND_ID, pim.STORE_ID, pdp.ITEM_ID, pdp.ITEM_NM
                    , 0 AS whs_qty
                    , 0 AS supply_amt
                    , 0 AS base_amt
                    , 0 AS store_amt
                    , -1 * pdp.RODR_QTY AS WHS_QTY1
                    , -1 * pdp.RODR_QTY * biui.UNIT_AMT AS supply_amt1
                    , -1 * pdp.RODR_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt1
                    , -1 * pdp.RODR_QTY * siui.UNIT_AMT AS store_amt1
                FROM pick_info_mgnt pim 
                INNER JOIN pick_dtl_ptcl pdp ON pim.COL_PICK_UNQ_NO = pdp.COL_PICK_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON pdp.ITEM_ID = biui.ITEM_ID AND pim.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON pdp.ITEM_ID = siui.ITEM_ID AND pim.BRAND_ID = siui.BRAND_ID AND pim.STORE_ID = siui.STORE_ID
                INNER JOIN rtn_req_base_info rrbi ON pdp.RTN_REQ_UNQ_NO = rrbi.RTN_REQ_UNQ_NO
                WHERE 1 = 1
                AND pim.BRAND_ID = #{sBrandId}
                <if test="sStoreId != null and sStoreId != ''">
                    AND pim.STORE_ID = #{sStoreId}
                </if>
                <if test="sItemNm != null and sItemNm != ''">
                    AND pdp.ITEM_NM LIKE CONCAT('%',#{sItemNm},'%')
                </if>
                AND pim.pick_st_cd IN ('PICK0', 'PICK1')
                AND rrbi.RTN_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
                AND rrbi.RTN_REQ_DT BETWEEN #{fromDt} AND #{toDt}
            ) a
            GROUP BY a.BRAND_ID, a.ITEM_ID
        ) base
        WHERE 1 = 1
        ORDER BY base.ITEM_NM ASC
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectItemSalesGridCnt" resultType="int">
        /* SalesMngMapper.selectItemSalesGridCnt */
        SELECT COUNT(*)
        FROM (
        SELECT a.BRAND_ID, FN_BRAND_NM(a.brand_id) AS brand_nm
        , a.ITEM_ID, a.item_nm
        , SUM(a.whs_qty) AS whs_qty
        , SUM(a.supply_amt) AS supply_amt
        , SUM(a.base_amt) AS base_amt
        , SUM(a.store_amt) AS store_amt
        , SUM(a.store_amt - a.supply_amt) AS income_amt
        , SUM(a.whs_qty1) AS whs_qty1
        , SUM(a.supply_amt1) AS supply_amt1
        , SUM(a.base_amt1) AS base_amt1
        , SUM(a.store_amt1) AS store_amt1
        , SUM(a.store_amt1 - a.supply_amt1) AS income_amt1
        FROM (
        SELECT smbi.BRAND_ID, smbi.STORE_ID, smdp.ITEM_ID, smdp.ITEM_NM
        , smdp.WHS_QTY
        , smdp.WHS_QTY * biui.UNIT_AMT AS supply_amt
        , smdp.WHS_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt
        , smdp.WHS_QTY * siui.UNIT_AMT AS store_amt
        , 0 AS whs_qty1
        , 0 AS supply_amt1
        , 0 AS base_amt1
        , 0 AS store_amt1
        FROM stl_mgnt_base_info smbi
        INNER JOIN stl_mgnt_dtl_ptcl smdp ON smbi.STTL_MGNT_UNQ_NO = smdp.STTL_MGNT_UNQ_NO
        INNER JOIN brand_item_unit_info biui ON smdp.ITEM_ID = biui.ITEM_ID AND smbi.BRAND_ID = biui.BRAND_ID
        INNER JOIN store_item_unit_info siui ON smdp.ITEM_ID = siui.ITEM_ID AND smbi.BRAND_ID = siui.BRAND_ID AND smbi.STORE_ID = siui.STORE_ID
        INNER JOIN pch_req_base_info prbi ON smdp.PCH_REQ_UNQ_NO = prbi.PCH_REQ_UNQ_NO
        WHERE 1 = 1
        AND smbi.BRAND_ID = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND smbi.STORE_ID = #{sStoreId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            AND smdp.ITEM_NM LIKE CONCAT('%',#{sItemNm},'%')
        </if>
        AND smbi.STTL_ST_CD IN ('ST10', 'ST20')
        AND prbi.PCH_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
        AND prbi.PCH_REQ_DT BETWEEN #{fromDt} AND #{toDt}
        UNION ALL
        SELECT pim.BRAND_ID, pim.STORE_ID, pdp.ITEM_ID, pdp.ITEM_NM
                    , 0 AS whs_qty
                    , 0 AS supply_amt
                    , 0 AS base_amt
                    , 0 AS store_amt
                    , -1 * pdp.RODR_QTY AS WHS_QTY1
                    , -1 * pdp.RODR_QTY * biui.UNIT_AMT AS supply_amt1
                    , -1 * pdp.RODR_QTY * biui.STORE_SALE_UNIT_AMT AS base_amt1
                    , -1 * pdp.RODR_QTY * siui.UNIT_AMT AS store_amt1
                FROM pick_info_mgnt pim 
                INNER JOIN pick_dtl_ptcl pdp ON pim.COL_PICK_UNQ_NO = pdp.COL_PICK_UNQ_NO
                INNER JOIN brand_item_unit_info biui ON pdp.ITEM_ID = biui.ITEM_ID AND pim.BRAND_ID = biui.BRAND_ID
                INNER JOIN store_item_unit_info siui ON pdp.ITEM_ID = siui.ITEM_ID AND pim.BRAND_ID = siui.BRAND_ID AND pim.STORE_ID = siui.STORE_ID
                INNER JOIN rtn_req_base_info rrbi ON pdp.RTN_REQ_UNQ_NO = rrbi.RTN_REQ_UNQ_NO
                WHERE 1 = 1
                AND pim.BRAND_ID = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND pim.STORE_ID = #{sStoreId}
        </if>
        <if test="sItemNm != null and sItemNm != ''">
            AND pdp.ITEM_NM LIKE CONCAT('%',#{sItemNm},'%')
        </if>
        AND pim.pick_st_cd IN ('PICK0', 'PICK1')
        AND rrbi.RTN_REQ_DT BETWEEN siui.CONT_STT_DT AND siui.CONT_END_DT
        AND rrbi.RTN_REQ_DT BETWEEN #{fromDt} AND #{toDt}
        ) a
        GROUP BY a.BRAND_ID, a.ITEM_ID
        ) base
        WHERE 1 = 1
    </select>

</mapper>
