-- ======================================
-- 2024-11-21: 需求调整，暂缓维护
-- ======================================



SELECT
    ${if(len(sDim)>0,sDim + ","," ")}
        ${if(FIND('合同金额', sIndex)>0," ROUND(SUM(T1.ct_amt ) / 10000,2) AS 合同金额,","")}
        ${if(FIND('订单金额', sIndex)>0," ROUND(SUM(T1.total_amt) / 10000,2) AS 订单金额,","")}
        ${if(FIND('回款金额', sIndex)>0," ROUND(SUM(T1.clc_amt) / 10000,2) AS 回款金额,","")}
        ${if(FIND('发货金额', sIndex)>0," ROUND(SUM(T1.deli_amt) / 10000,2) AS 发货金额,","")}
        ${if(FIND('发货件数', sIndex)>0," ROUND(SUM(T1.ship_num) / 10000,2) AS 发货件数,","")}
        ${if(FIND('发货瓶数', sIndex)>0," ROUND(SUM(T1.ship_botnum),0) AS 发货瓶数,","")}
        ${if(FIND('实物库存', sIndex)>0," ROUND(SUM(T1.real_amt) / 10000,2) AS 实物库存,","")}
        ${if(len(sIndex)>0&& FIND(sIndex,sIndex)>0," 1","")}
FROM
    (
-- 销售订单(订单金额)
        SELECT
            bu_name        -- 事业部名称
             ,region_name    -- 大区名称
             ,office_name    -- 办事处名称
             ,city_unit_name -- 城市单元名称
             ,province_name  -- 省
             ,city_name      -- 市
             ,pl_name        -- 产品线名称
             ,pi_name        -- 品项
             ,product_name   -- 产品名称
             ,deal_name      -- 经销商名称
             ,src_deal_name  -- 子经销商名称
             ,0     AS  ct_amt  -- 合同金额
             ,SUM(COALESCE(d_amt,0)) + SUM(COALESCE(e_amt,0)) + SUM(COALESCE(f_amt,0)) + SUM(COALESCE(fz_amt,0)) + SUM(COALESCE(q_amt,0)) AS total_amt -- 订单金额
             ,0     AS clc_amt      -- 回款金额
             ,0     AS deli_amt     -- 发货金额
             ,0     AS ship_num     -- 发货件数
             ,0     AS ship_botnum  -- 发货瓶数
             ,0     AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_order  -- 销售订单视图
        WHERE order_time  >=  '${CONCATENATE(sStart_date," 00:00:00.000")}'
        AND order_time < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
        ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
        ${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
        -- 办事处
        ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
        ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
        ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
        ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
        ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
        ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
        ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
        ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称

        UNION ALL
-- 合同汇总
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,sum(ct_amt) as ct_amt -- 合同金额
                ,0      AS total_amt  -- 订单金额
                ,0      AS clc_amt -- 回款金额
                ,0      AS deli_amt -- 发货金额
                ,0      AS ship_num -- 发货件数
                ,0      AS ship_botnum-- 发货瓶数
                ,0      AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_contract  -- 合同表  产品+合同ID
        WHERE signed_date >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND signed_date < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
            -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
            -- 大区
            ${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
            -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
            -- 城市单元
            ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
            -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
            -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
            -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
            -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
            -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
            -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}

        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
        UNION ALL
        -- 销售回款
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,'' AS pi_name        -- 品项
                ,'' AS product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0         AS ct_amt -- 合同金额
                ,0         AS total_amt  -- 订单金额
                ,SUM(COALESCE(clc_amt,0)) AS clc_amt -- 回款金额
                ,0         AS deli_amt -- 发货金额
                ,0         AS ship_num -- 发货件数
                ,0         AS ship_botnum-- 发货瓶数
                ,0         AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_collection -- 销售回款
        WHERE 1=1
          AND  bill_date  >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND  bill_date  < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称

        UNION ALL
-- 销售发货
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0         AS ct_amt -- 合同金额
                ,0         AS total_amt  -- 订单金额
                ,0         AS clc_amt -- 回款金额
                ,SUM(deli_amt) AS deli_amt   -- 发货金额
                ,SUM(deli_num / pack_base_num)     AS ship_num -- 发货件数
                ,SUM(pack_base_num)     AS ship_botnum-- 发货瓶数
                ,0     AS real_amt     -- 实物库存
        FROM dm.v_ag_sal_deliver
        WHERE 1=1
          AND deli_time  >= '${CONCATENATE(sStart_date," 00:00:00.000")}'
          AND deli_time  < '${CONCATENATE(sEnd_date," 00:00:00.000")}'
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
        UNION ALL
        -- 实物库存
        SELECT
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
                ,0     AS ct_amt -- 合同金额
                ,0      AS total_amt  -- 订单金额
                ,0      AS clc_amt -- 回款金额
                ,0     AS deli_amt   -- 发货金额
                ,0     AS ship_num -- 发货件数
                ,0     AS ship_botnum-- 发货瓶数
                ,SUM(end_num * price)  AS real_amt   -- 实物库存金额
        FROM dm.v_ag_sal_deal_stock
        WHERE stock_year=left('${sEnd_date}',4)
        AND stock_month < right('${sEnd_date}',2)
        AND stock_month >= right('${sStart_date}',2)
        -- 事业部
            ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
            ${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
        -- 办事处
            ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
            ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 省
            ${if(len(sProvince) == 0,"","and province_name IN ('"+ sProvince+"')")}
        -- 市
            ${if(len(sCity) == 0,"","and city_name IN ('"+ sCity+"')")}
        -- 经销商
            ${if(sIs_srcdeal == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
        -- 产品线
            ${if(len(sPl) == 0,"","and pl_name IN ('"+ sPl+"')")}
        -- 品项
            ${if(len(sPi_name) == 0,"","and pi_name IN ('"+ sPi_name+"')")}
        -- 产品
            ${if(len(sProd) == 0,"","and product_code IN ('"+sProd+"')")}
        GROUP BY
            bu_name        -- 事业部名称
                ,region_name    -- 大区名称
                ,office_name    -- 办事处名称
                ,city_unit_name -- 城市单元名称
                ,province_name  -- 省
                ,city_name      -- 市
                ,pl_name        -- 产品线名称
                ,pi_name        -- 品项
                ,product_name   -- 产品名称
                ,deal_name      -- 经销商名称
                ,src_deal_name  -- 子经销商名称
    )T1
    ${if(len(sDim)>0," GROUP BY " + sDim ," ")}


-- ======================================
-- 数据更新时间
-- ======================================

-- 存在多个表，取表最近的更新时间

SELECT
CONCAT('数据更新日期:',LEFT(MAX(T.update_time),19)) AS update_time
FROM
(
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_order  --  dm.v_ag_sal_order  -- 销售订单视图主表
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_contract -- dm.v_ag_sal_contract  -- 合同表  产品+合同ID
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_collection -- dm.v_ag_sal_collection -- 销售回款
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_deliver -- dm.v_ag_sal_deliver -- 销售发货
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwr.fact_sal_dealer_stock -- dm.v_ag_sal_deal_stock -- 实物库存
)T
;

-- ======================================
-- 销售业绩组合表
-- ======================================
-- 使用 ： dm.dm_sal_indicator_day    -- 销售种子指标按日汇总表
-- 20241121更新： 与销售业绩月度汇总功能相似，待确认最终报表方案

select


    bu.dept_name        as bu_name -- 事业部名称
   -- ,d.dept_name as p_bu_name  -- 产品所属事业部名称
    ,dmd.secd_dept_name as region_name -- 大区名称
    ,dmd.thrd_dept_name as office_name -- 办事处名称
    ,dmd.dept_name as city_unit_name -- 城市单元名称
    ,max(pv.town_name) as province_name  -- 省
    ,max(cit.town_name) as city_name  -- 市
    ,p.pl_name   -- 产品线名称
    ,p.pi_name   -- 品项名称
  -- ,p.report_pi_name  -- 报表品项
    ,p.product_name  -- 产品名称
    ,deal.deal_name  -- 主经销商名称
    ,sd.deal_name as src_deal_name  -- 子经销商名称

   -- p.spec, -- 规格

    -- 合同金额
    -- 订单金额
    -- 回款金额
    -- 发货金额
    -- 发货件数
    -- 发货瓶数
    -- 实物库存

FROM
(
        SELECT
        bu_code
        ,region_code
        ,office_code
        ,city_unit_code
        ,deal_code
        ,src_deal_code
        ,product_code
        ,province_code
        ,city_code
        ,CASE WHEN indi_type IN ('合同','合同计划')
              THEN (amt_bu + amt_region + amt_office + amt_city_unit + amt_dealer)
              ELSE 0 END AS ct_amt -- 合同金额
        ,CASE WHEN indi_type IN ('订单','电商订单')
              THEN (amt_bu + amt_region + amt_office + amt_city_unit + amt_dealer)
              ELSE 0 END AS total_amt -- 订单金额
        ,CASE WHEN indi_type IN ('回款','电商订单')
              THEN (amt_bu + amt_region + amt_office + amt_city_unit + amt_dealer)
              ELSE 0 END AS clc_amt -- 回款金额
        ,CASE WHEN indi_type IN ('发货','费用发货','电商订单')
              THEN (amt_bu + amt_region + amt_office + amt_city_unit + amt_dealer)
              ELSE 0 END AS deli_amt --发货金额
        ,CASE WHEN indi_type IN ('发货','费用发货','电商订单')
              THEN (num_bu  + num_region +  num_office +  num_city_unit +  num_dealer)
              ELSE 0 END AS total_num -- 发货瓶数
        -- 发货件数=瓶数/pack_base_num
        -- 发货瓶数?
        ,CASE WHEN indi_type IN ('实物库存') -- 使用金额
              THEN (num_bu  + num_region + num_office +  num_city_unit +  num_dealer)
              ELSE 0 END AS real_amt  -- 实物库存
           -- 在途库存
           -- 实物库存
           -- 已填仓未下单
           -- 未下单往来余额
           -- 未发货
           -- 融资未填仓
           -- 费用余额

        -- 其他库存
        FROM dm.dm_sal_indicator_day  -- 销售种子指标按日汇总表
        WHERE indi_type NOT LIKE '%目标%'
        -- 统计日期
        AND stat_date >= '${CONCATENATE(sStart_date," 00:00:00")}'
        AND stat_date <= '${CONCATENATE(sEnd_date," 00:00:00")}'
        ${if(len(sIndex) == 0,"","and indi_type IN ('"+sIndex+"')")}
        -- 事业部
        ${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
        -- 大区
        ${if(len(sRegion) == 0,"","and region_code IN ('"+ sRegion+"')")}
        -- 办事处
        ${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
        -- 城市单元
        ${if(len(sCu) == 0,"","and city_unit_code IN ('"+sCu+"')")}
        -- 产品
        ${if(len(sProduct) == 0,"","and product_code IN ('"+ sProduct +"')")}
        -- 产品线
        ${if(len(sPl_name) == 0,"","and p.pl_name IN ('"+ sPl_name +"')")}
        -- 子主经销商
        ${if(sSub_main == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}

)a
left join dwr.dim_product p on a.product_code=p.product_code
left join dwr.dim_department dmd on a.city_unit_code=dmd.dept_code
left join dwr.dim_department bu on a.bu_code=bu.dept_code
left join dwr.dim_department d on p.bu_code=d.dept_code
left join dwr.dim_dealer deal on a.deal_code=deal.deal_code
left join dwr.dim_dealer sd on a.src_deal_code=sd.deal_code
left join dwr.dim_area pv on a.province_code=pv.town_code
left join dwr.dim_area cit on a.city_code=cit.town_code
left join sdi_xdata.mdm_dim_sorted st on a.region_code=st.sorted_value and st.sorted_type='大区'
where exists (
          select
          1
from
	dm.dm_bi_user_permisson
where
	bi_user = '${fine_username}'
	and per_model IN ('all','sal')
	and (
			is_all = 1
			OR per_code = a.bu_code
			OR per_code = a.region_code
			OR per_code = a.office_code
			OR per_code = a.city_unit_code
			)
)

-- 品项
${if(len(sPi_name) == 0,"","and p.pi_name IN ('"+ sPi_name+"')")}
-- 省
${if(len(sProvince) == 0,"","and pv.province_name IN ('"+ sProvince+"')")}
-- 市
${if(len(sCity) == 0,"","and cit.city_name IN ('"+ sCity+"')")}

order by nvl(st.sorted_no,99)

-- SQL分页:
-- limit ${pPage_rows} offset (${pJump_page}-1)*${pPage_rows}
