WITH corp AS (
    SELECT
        corp_code -- 公司编码
         ,corp_name  -- 公司名称
    FROM dwr.dim_corp a
    WHERE corp_type = '实体'
      AND	(parent_corp_code LIKE '02%' OR corp_code = '0301')
      AND  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.corp_code
			)
)
        ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
)
SELECT
     TO_CHAR(bill_date, 'YYYY-MM-DD') AS   bill_date           -- 制单日期
     ,TO_CHAR(audit_date, 'YYYY-MM-DD') AS audit_date          -- 终审日期
     ,TO_CHAR(pay_time, 'YYYY-MM-DD HH24:MI:SS') AS pay_time            -- 付款时间
     ,bill_code           -- 单据号
     ,bill_status         -- 单据状态
     ,budget_code         -- 预算编号
     ,plan_code           -- 资金计划编号
     ,corp_code           -- 公司编码
     ,dept_code           -- 部门编码
     ,fund_year           -- 资金年度
     ,fund_month          -- 资金月度
     ,fee_type            -- 费用类型
     ,plan_item_code      -- 资金项目编号
     ,plan_item_name      -- 资金项目名称
     ,fund_item           -- 支出项目
     ,owner_bank_account  -- 本方帐号
     ,opp_account_name    -- 对方户名
     ,opp_bank_account    -- 对方帐号
     ,opp_name            -- 对方名称
     ,amt  / 10000 AS amt             -- 金额
     ,remark              -- 备注
     ,budget_type         -- 预算类型
     ,fund_content        -- 支出内容
FROM dwi.dwi_fim_oa_payment a
JOIN
(
    SELECT
    src_bill_codes
    FROM dwi.dwi_fim_voucher
    WHERE 1=1
    ${if(len(sPk)=0,"","AND pk = '" + sPk +"'")}
    GROUP BY src_bill_codes
)b
ON a.bill_code = b.src_bill_codes
WHERE a.corp_code in(SELECT corp_code FROM corp)
AND  a.bill_date  >= CONCAT('${sStart_mon}','-01')
AND  a.bill_date  <= CONCAT('${sEnd_mon}','-30')

${if(len(sBill)=0,"","AND bill_code IN ('" + sBill +"')")}



SELECT
    CONCAT('数据更新时间:',LEFT(MAX(update_time),19)) AS update_time
FROM dwi.dwi_fim_oa_payment
