-- ======================================
-- 表格-现金收入情况-收入类型【已核对】
-- ======================================
WITH base_date AS
(SELECT
     stat_date
     ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售酒类回款'  THEN amt ELSE 0 END) / 10000 AS wine_amt -- 酒类回款
                        ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售非酒类回款'  THEN amt ELSE 0 END) / 10000 AS nowine_amt -- 非酒类回款
                        ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt -- 合计
                   from dm.dm_fim_fud_indicator a
                   where corp_code not in ('0101','0103','0102')
                    ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
                     AND trade_direction = '收入'
                     AND indi_type = '执行'
                     AND plan_item_name IN('对外销售酒类回款','对外销售非酒类回款')
                     AND exists (
                       select
                           1
                       from
                           dm.dm_bi_user_permisson
                       where
                           bi_user = '${fine_username}'
                         and per_model IN ('all','fim')
                         and (
                           is_all = 1
                               OR per_code = a.corp_code  -- 事业部
                           )
)
GROUP BY stat_date
)
SELECT
-- 当月
SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
    THEN wine_amt ELSE 0 END) AS wine_amt_m -- 当月对外销售酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
          THEN nowine_amt ELSE 0 END) AS nowine_amt_m -- 当月对外销售非酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
        THEN amt ELSE 0 END) AS amt_m -- 当月合计
-- 当年
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
    THEN wine_amt ELSE 0 END) AS wine_amt_y -- 当年对外销售酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
                  THEN nowine_amt ELSE 0 END) AS nowine_amt_y -- 当年对外销售非酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_y -- 当年合计
-- 去年当月
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
    THEN wine_amt ELSE 0 END) AS wine_amt_last_m -- 去年当月对外销售酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
                  THEN nowine_amt ELSE 0 END) AS nowine_amt_last_m -- 去年当月对外销售非酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_last_m -- 去年当月合计

-- 去年
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN wine_amt ELSE 0 END) AS wine_amt_last_y -- 去年对外销售酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN nowine_amt ELSE 0 END) AS nowine_amt_last_y -- 去年对外销售非酒类回款
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_last_y -- 去年合计
FROM base_date
;


-- ======================================
-- 表格-现金支出情况-【已核对】
-- ======================================
WITH base_date AS(
select
    plan_item_name
     ,stat_date
     ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
  AND trade_direction = '支出'
  AND indi_type = '执行'
  AND plan_item_name NOT IN ('内部调拨支出','偿还贷款')
  AND owner_bank_account <> '22195101040012008'
    ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
GROUP BY plan_item_name,stat_date
)
SELECT
    plan_item_name
-- 当月
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
                 THEN amt ELSE 0 END) AS amt_m -- 当月支出金额
-- 去年当月
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_last_m -- 去年当月支出金额
-- 当年
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_y -- 当年支出金额

-- 去年
,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM-dd")}'
              THEN amt ELSE 0 END) AS amt_last_y -- 去年支出金额

FROM base_date
GROUP BY plan_item_name
ORDER BY SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
                      THEN amt ELSE 0 END) DESC
;

-- ======================================
-- 表格-资金余额情况-【已验证】
-- ======================================

WITH base_data AS(
SELECT amt_type
                        , stat_date
                        , SUM(CASE WHEN nvl(a.is_usable, 1) = 1 THEN amt ELSE 0 END) / 10000 AS amt
                   FROM dm.dm_fim_fud_balance a
                            LEFT JOIN dwr.dim_corp c ON a.corp_code = c.corp_code
                            LEFT JOIN dwr.dim_bank_account dba ON DECODE(LENGTH(a.owner_bank_account), 17,
                                                                         SUBSTRING(a.owner_bank_account, 3),
                                                                         a.owner_bank_account) =
                                                                  DECODE(LENGTH(dba.accountnumber), '17',
                                                                         SUBSTRING(dba.accountnumber, 3),
                                                                         dba.accountnumber)
                       AND dba.account_owner_class = 'langjiu'
                   WHERE a.corp_code NOT IN ('0101', '0103', '0102')
                     AND nvl(a.is_usable, 1) = 1
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}

                  --   AND if(dba.accountnumber IN ('20351059900100000512291', '22195101040012008', '信用证保证金'),
              --              '保证金户', category_name) <> '保证金户'
                     AND exists (
                       select
                           1
                       from
                           dm.dm_bi_user_permisson
                       where
                           bi_user = '${fine_username}'
                         and per_model IN ('all','fim')
                         and (
                           is_all = 1
                               OR per_code = a.corp_code  -- 事业部
                           )
                   )
                   GROUP BY amt_type, stat_date
                   UNION ALL
                   SELECT '冻结资金'  AS amt_type
                        , stat_date
                        , SUM(CASE WHEN nvl(a.is_usable, 1) = 1 THEN amt ELSE 0 END) / 10000 AS amt
                   FROM dm.dm_fim_fud_balance a
                            LEFT JOIN dwr.dim_corp c ON a.corp_code = c.corp_code
                            LEFT JOIN dwr.dim_bank_account dba ON DECODE(LENGTH(a.owner_bank_account), 17,
                                                                         SUBSTRING(a.owner_bank_account, 3),
                                                                         a.owner_bank_account) =
                                                                  DECODE(LENGTH(dba.accountnumber), '17',
                                                                         SUBSTRING(dba.accountnumber, 3),
                                                                         dba.accountnumber)
                       AND dba.account_owner_class = 'langjiu'
                   WHERE a.corp_code NOT IN ('0101', '0103', '0102')
                     AND nvl(a.is_usable, 0) = 0
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}

              --       AND if(dba.accountnumber IN ('20351059900100000512291', '22195101040012008', '信用证保证金'),
                 --           '保证金户', category_name) <> '保证金户'
                     AND exists (
                       select
                           1
                       from
                           dm.dm_bi_user_permisson
                       where
                           bi_user = '${fine_username}'
                         and per_model IN ('all','fim')
                         and (
                           is_all = 1
                               OR per_code = a.corp_code  -- 事业部
                           )
                   )
                   GROUP BY stat_date
)
SELECT
     -- 当月
 SUM(CASE WHEN amt_type = '现金' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS cash_amt_m -- 当月现金余额
,SUM(CASE WHEN amt_type = '票据' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS bill_amt_m -- 当月票据余额
,SUM(CASE WHEN amt_type = '冻结资金' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS frozen_amt_m -- 当月冻结资金
-- 当年
,SUM(CASE WHEN amt_type = '现金' AND  to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS cash_amt_y -- 当年现金余额
,SUM(CASE WHEN amt_type = '票据' AND  to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS bill_amt_y -- 当年票据余额
,SUM(CASE WHEN  amt_type = '冻结资金' AND  to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
          THEN amt ELSE 0 END) AS frozen_amt_y -- 当年冻结资金
-- 去年当月
,SUM(CASE WHEN amt_type = '现金' AND  to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS  cash_amt_last_m -- 去年当月现金余额
,SUM(CASE WHEN amt_type = '票据' AND  to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS bill_amt_last_m -- 去年当月票据余额
,SUM(CASE WHEN amt_type = '冻结资金' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS frozen_amt_last_m -- 去年当月冻结资金

-- 去年
,SUM(CASE WHEN amt_type = '现金' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS cash_amt_last_y -- 去年现金余额
,SUM(CASE WHEN amt_type = '票据' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS bill_amt_last_y -- 去年票据余额
,SUM(CASE WHEN amt_type = '冻结资金' AND to_char(stat_date,'yyyy-MM') = '${FORMAT(YEARDELTA(sStart_date,-1),"yyyy-MM")}'
          THEN amt ELSE 0 END) AS frozen_amt_last_y -- 去年冻结资金
FROM base_data

;



-- ======================================
-- 表格-融资-手工填写
-- ======================================






-- ======================================
-- 图表-资金收支存总览
-- ======================================


-- 【图表_资金收支总览收入支出】

-- 银行收支
with day_amt as (
    select
    trade_time,
    trade_direction,
    plan_item_name,
    amt
    from dm.v_ag_fim_bank_account_flow a
    where trade_time>=date'2021-01-01'
    AND owner_bank_account <> '22195101040012008'
    AND corp_code not in ('0101','0103','0102')
    ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
    AND to_char(trade_time,'yyyy-MM-dd') = '${sStart_date}'
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
    union all
    select
    trade_time,
    trade_direction,
    plan_item_name,
    amt
    from dm.v_ag_fim_draft_flow a
    where trade_time>=date'2021-01-01'
    AND owner_bank_account <> '22195101040012008'
    AND corp_code not in ('0101','0103','0102')
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
      AND to_char(trade_time,'yyyy-MM-dd') = '${sStart_date}'
      AND exists (
        select
        1
        from
        dm.dm_bi_user_permisson
        where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
       OR per_code = a.corp_code  -- 事业部
        )
    )
),
m_y_amt AS (
    SELECT
    stat_date
    ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt_in
    ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt_out
    from dm.dm_fim_fud_indicator a
    where corp_code not in ('0101','0103','0102')
      AND trade_direction IN('收入','支出')
      AND indi_type = '执行'
      AND plan_item_name NOT IN ('内部调拨收入','取得贷款','内部调拨支出','偿还贷款')
      AND owner_bank_account <> '22195101040012008'
        ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
      AND to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}'
      AND exists (
        select
            1
        from
            dm.dm_bi_user_permisson
        where
            bi_user = '${fine_username}'
          and per_model IN ('all','fim')
          and (
            is_all = 1
                OR per_code = a.corp_code  -- 事业部
            )
    )
    GROUP BY stat_date
)
SELECT
SUM(today_in_amt) AS today_in_amt       -- 今日收入
,SUM(today_out_amt) AS today_out_amt    -- 今日支出
,SUM(year_in_amt) AS year_in_amt        -- 年收入
,SUM(year_out_amt) AS year_out_amt      -- 年度支出
,SUM(month_in_amt) AS month_in_amt      -- 月收入
,SUM(month_out_amt) AS month_out_amt    -- 月支出
FROM
(
select
SUM(CASE WHEN trade_direction = '收入' AND plan_item_name NOT IN ('内部调拨收入','取得贷款') THEN amt ELSE 0 END ) / 10000 AS today_in_amt -- 今日收入
,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') THEN amt ELSE 0 END ) / 10000 AS today_out_amt -- 今日支出
,0  AS year_in_amt -- 年收入
,0  AS year_out_amt -- 年度支出
,0  AS month_in_amt -- 月收入
,0  AS month_out_amt -- 月支出
FROM day_amt
WHERE trade_direction IN('收入','支出')
UNION ALL
SELECT
0   AS today_in_amt -- 今日收入
,0  AS today_out_amt -- 今日支出
,SUM(amt_in)  AS year_in_amt -- 年收入
,SUM(amt_out)  AS year_out_amt -- 年度支出
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' THEN amt_in ELSE 0 END)  AS month_in_amt -- 月收入
,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}' THEN amt_out ELSE 0 END)   AS month_out_amt -- 月支出
FROM m_y_amt
)T





-- ======================================
-- 资金余额
-- ======================================

-- 【图表_资金收支总览资金】
with base_data AS (
        select
            0  AS begin_year_amt -- 年初可用资金余额
             ,SUM(CASE WHEN a.is_usable  = 1 THEN a.amt ELSE 0 END ) / 10000 AS amt -- 12月末资金余额
             ,SUM(CASE WHEN a.create_by = 'xj' AND  a.is_usable  = 0 THEN a.amt ELSE 0 END ) / 10000 AS sec_amt-- 12月末保证金余额
        from dm.dm_fim_fud_balance a
                 left join dwr.dim_corp c on a.corp_code=c.corp_code
                 left join dwr.dim_bank_account dba on decode(length(a.owner_bank_account), 17,
                                                              substring(a.owner_bank_account, 3),
                                                              a.owner_bank_account)=decode(length(dba.accountnumber), '17',
                                                                                           substring(dba.accountnumber, 3),
                                                                                           dba.accountnumber)
            and dba.account_owner_class='langjiu'
        where a.corp_code not in ('0101','0103','0102')
          AND to_char(stat_date,'yyyy-MM') =  '${FORMAT(sStart_date,"yyyy-MM")}'
            ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
          AND exists (
            select
                1
            from
                dm.dm_bi_user_permisson
            where
                bi_user = '${fine_username}'
              and per_model IN ('all','fim')
              and (
                is_all = 1
                    OR per_code = a.corp_code  -- 事业部
                )
        )
        UNION ALL
        SELECT
            SUM(CASE WHEN a.is_usable  = 1 THEN a.amt ELSE 0 END ) / 10000  AS begin_year_amt -- 年初可用资金余额
             ,0 AS amt -- 12月末资金余额
             ,0 AS sec_amt-- 12月末保证金余额
        from dm.dm_fim_fud_balance a
                 left join dwr.dim_corp c on a.corp_code=c.corp_code
                 left join dwr.dim_bank_account dba on decode(length(a.owner_bank_account), 17,
                                                              substring(a.owner_bank_account, 3),
                                                              a.owner_bank_account)=decode(length(dba.accountnumber), '17',
                                                                                           substring(dba.accountnumber, 3),
                                                                                           dba.accountnumber)and dba.account_owner_class='langjiu'
        where a.corp_code not in ('0101','0103','0102')
          AND to_char(stat_date,'yyyy-MM') =  '${FORMAT(MONTHDELTA(DATEINYEAR(sStart_date,1),-1),"yyyy-MM")}'
            ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
          AND exists (
            select
                1
            from
                dm.dm_bi_user_permisson
            where
                bi_user = '${fine_username}'
              and per_model IN ('all','fim')
              and (
                is_all = 1
                    OR per_code = a.corp_code  -- 事业部
                )
        )
)
SELECT
SUM(begin_year_amt) AS begin_year_amt -- 年初可用资金余额
,SUM(amt) AS amt -- 12月末资金余额
,SUM(sec_amt) AS sec_amt -- 12月末保证金余额
FROM base_data


-- ======================================
-- 资金分析_可用资金
-- ======================================

-- 存款类型占比
SELECT amt_type
     , SUM(CASE WHEN nvl(a.is_usable, 1) = 1 THEN amt ELSE 0 END) / 10000 AS amt
FROM dm.dm_fim_fud_balance a
WHERE a.corp_code NOT IN ('0101', '0103', '0102')
AND nvl(a.is_usable, 1) = 1
  AND to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
GROUP BY amt_type
ORDER BY amt_type
-- 银行余额分布

SELECT
amt_type
,nvl(dba.bank_name,'未知银行') as bank_name
,SUM(CASE WHEN nvl(a.is_usable, 1) = 1 THEN amt ELSE 0 END) / 10000 AS amt
FROM dm.dm_fim_fud_balance a
left join dwr.dim_bank_account dba
on decode(length(a.owner_bank_account), 17,substring(a.owner_bank_account, 3),
a.owner_bank_account)=decode(length(dba.accountnumber), '17',
substring(dba.accountnumber, 3),
dba.accountnumber)
and dba.account_owner_class='langjiu'
WHERE a.corp_code NOT IN ('0101', '0103', '0102')
  AND nvl(a.is_usable, 1) = 1
  AND to_char(stat_date,'yyyy-MM') = '${FORMAT(sStart_date,"yyyy-MM")}'
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
GROUP BY amt_type,nvl(dba.bank_name,'未知银行')
ORDER BY decode('amt_type','票据',1,'现金',2),SUM(CASE WHEN nvl(a.is_usable, 1) = 1 THEN amt ELSE 0 END) DESC



-- ======================================
-- 资金收入
-- ======================================
-- 收入类型

WITH base_date AS
         (SELECT
              stat_date
               ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售酒类回款'  THEN amt ELSE 0 END) / 10000 AS wine_amt -- 酒类回款
               ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售非酒类回款'  THEN amt ELSE 0 END) / 10000 AS nowine_amt -- 非酒类回款
               ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt -- 合计
          from dm.dm_fim_fud_indicator a
          where corp_code not in ('0101','0103','0102')
              ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
                     AND trade_direction = '收入'
                     AND indi_type = '执行'
                     AND plan_item_name IN('对外销售酒类回款','对外销售非酒类回款')
                     AND exists (
                       select
                           1
                       from
                           dm.dm_bi_user_permisson
                       where
                           bi_user = '${fine_username}'
                         and per_model IN ('all','fim')
                         and (
                           is_all = 1
                               OR per_code = a.corp_code  -- 事业部
                           )
)
          GROUP BY stat_date
         )
SELECT
-- 当月
    SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}'
                 THEN wine_amt ELSE 0 END) AS wine_amt_m -- 当月对外销售酒类回款
     ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}'
                   THEN nowine_amt ELSE 0 END) AS nowine_amt_m -- 当月对外销售非酒类回款
     ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}'
                   THEN amt ELSE 0 END) AS amt_m -- 当月合计
FROM base_date

-- 年度各项目收入



SELECT
    plan_item_name
     ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt_in
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
  AND trade_direction = '收入'
  AND indi_type = '执行'
  AND plan_item_name NOT IN ('内部调拨收入','取得贷款')
  AND owner_bank_account <> '22195101040012008'
    ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
  AND to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}'
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)

GROUP BY plan_item_name
ORDER BY plan_item_name


-- 收入趋势
with re as (
    select
        trade_time,
        trade_direction,
        plan_item_name,
        amt
    from dm.v_ag_fim_bank_account_flow a
    where trade_time>=date'2021-01-01'
      AND owner_bank_account <> '22195101040012008'
      AND corp_code not in ('0101','0103','0102')
        ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
    union all
    select
        trade_time,
        trade_direction,
        plan_item_name,
        amt
    from dm.v_ag_fim_draft_flow a
    where trade_time>=date'2021-01-01'
      AND owner_bank_account <> '22195101040012008'
      AND corp_code not in ('0101','0103','0102')
        ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
      AND exists (
        select
        1
        from
        dm.dm_bi_user_permisson
        where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
       OR per_code = a.corp_code  -- 事业部
        )
        )
)
-- 近15天
${if(sData_type=1,"","/*")}
select
    to_char(trade_time,'MM-dd') AS trade_time
    ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name NOT IN ('内部调拨收入','取得贷款') THEN amt ELSE 0 END ) / 10000 AS 收入
     ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') THEN amt ELSE 0 END ) / 10000 AS 支出
from re
WHERE trade_direction IN('收入','支出')
  AND to_char(trade_time,'yyyy-MM-dd') >=  '${FORMAT(DATEDELTA(sStart_date,-15),"yyyy-MM-dd")}'
AND to_char(trade_time,'yyyy-MM-dd') <=  '${FORMAT(sStart_date,"yyyy-MM-dd")}'

GROUP BY  to_char(trade_time,'MM-dd')
ORDER BY  to_char(trade_time,'MM-dd')
${if(sData_type=1,"","*/")}

-- 近12个月
    ${if(sData_type=2,"","/*")}
select
    to_char(trade_time,'MM') AS trade_time
     ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name NOT IN ('内部调拨收入','取得贷款') THEN amt ELSE 0 END ) / 10000 AS 收入
     ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') THEN amt ELSE 0 END ) / 10000 AS 支出
from re
WHERE trade_direction IN('收入','支出')
  AND to_char(trade_time,'yyyy-MM') >=  '${FORMAT(MONTHDELTA(sStart_date,-12),"yyyy-MM")}'
  AND to_char(trade_time,'yyyy-MM') <=  '${FORMAT(sStart_date,"yyyy-MM")}'

GROUP BY  to_char(trade_time,'MM')
ORDER BY to_char(trade_time,'MM')
${if(sData_type=2,"","*/")}


-- ======================================
-- 图表-资金支出
-- ======================================

-- 项目支出

WITH base_date AS(
    select
        plan_item_name
         ,stat_date
         ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt
    from dm.dm_fim_fud_indicator a
    where corp_code not in ('0101','0103','0102')
      AND trade_direction = '支出'
      AND indi_type = '执行'
      AND plan_item_name NOT IN ('内部调拨支出','偿还贷款')
      AND owner_bank_account <> '22195101040012008'
        ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
      AND exists (
        select
            1
        from
            dm.dm_bi_user_permisson
        where
            bi_user = '${fine_username}'
          and per_model IN ('all','fim')
          and (
            is_all = 1
                OR per_code = a.corp_code  -- 事业部
            )
    )
    GROUP BY plan_item_name,stat_date
)
SELECT
    plan_item_name

-- 当年
     ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
                   THEN amt ELSE 0 END) AS amt_y -- 当年支出金额
FROM base_date
GROUP BY plan_item_name
ORDER BY SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(sStart_date,"yyyy")}' AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(sStart_date,"yyyy-MM-dd")}'
                      THEN amt ELSE 0 END) DESC
LIMIT 10
;



-- 支出趋势
with re as (
    select
        trade_time,
        trade_direction,
        plan_item_name,
        amt
    from dm.v_ag_fim_bank_account_flow a
    where trade_time>=date'2021-01-01'
      AND owner_bank_account <> '22195101040012008'
      AND corp_code not in ('0101','0103','0102')
        ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
        )
)
    union all
    select
        trade_time,
        trade_direction,
        plan_item_name,
        amt
    from dm.v_ag_fim_draft_flow a
    where trade_time>=date'2021-01-01'
      AND owner_bank_account <> '22195101040012008'
      AND corp_code not in ('0101','0103','0102')
        ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")}
      AND exists (
        select
        1
        from
        dm.dm_bi_user_permisson
        where
        bi_user = '${fine_username}'
      and per_model IN ('all','fim')
      and (
        is_all = 1
       OR per_code = a.corp_code  -- 事业部
        )
        )
)
-- 近15天
    ${if(sData_type_out=1,"","/*")}
select
    to_char(trade_time,'MM-dd') AS trade_time
     ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name NOT IN ('内部调拨收入','取得贷款') THEN amt ELSE 0 END ) / 10000 AS 收入
     ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') THEN amt ELSE 0 END ) / 10000 AS 支出
from re
WHERE trade_direction IN('收入','支出')
  AND to_char(trade_time,'yyyy-MM-dd') >=  '${FORMAT(DATEDELTA(sStart_date,-15),"yyyy-MM-dd")}'
  AND to_char(trade_time,'yyyy-MM-dd') <=  '${FORMAT(sStart_date,"yyyy-MM-dd")}'

GROUP BY  to_char(trade_time,'MM-dd')
ORDER BY  to_char(trade_time,'MM-dd')
              ${if(sData_type_out=1,"","*/")}

-- 近12个月
    ${if(sData_type_out=2,"","/*")}
select
    to_char(trade_time,'MM') AS trade_time
     ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name NOT IN ('内部调拨收入','取得贷款') THEN amt ELSE 0 END ) / 10000 AS 收入
     ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') THEN amt ELSE 0 END ) / 10000 AS 支出
from re
WHERE trade_direction IN('收入','支出')
  AND to_char(trade_time,'yyyy-MM') >=  '${FORMAT(MONTHDELTA(sStart_date,-12),"yyyy-MM")}'
  AND to_char(trade_time,'yyyy-MM') <=  '${FORMAT(sStart_date,"yyyy-MM")}'

GROUP BY  to_char(trade_time,'MM')
ORDER BY to_char(trade_time,'MM')
             ${if(sData_type_out=2,"","*/")}