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','fim')
	and (
			is_all = 1
			OR per_code = a.corp_code
			)
)
        ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
)
SELECT
    bill_date      AS  制单日期
     ,pk
     ,bill_emp_code     as 制单人工号
     ,corp_code      as 公司编码
     ,acnt_book_name    as 主体账簿
     ,vouch_type       as 凭证类别
     ,vouch_code      as  凭证号
     ,acnt_year         AS 账期年度
     ,acnt_mon          AS 账期月度
     ,main_abstract     AS 主表摘要
     ,entry_code       AS 分录号
     ,sub_abstract     AS 子表摘要
     ,subject_code     AS 科目编码
     ,subject_name     AS 科目名称
     ,subject_full_name  AS 科目全称
     ,aux_content      AS 辅助核算
     ,debit_amt  / 10000    AS 借方金额
     ,credit_amt   / 10000  AS  贷方金额
     ,src_bill_codes  AS 来源单据号
     ,acnt_date     AS 记账日期
FROM dwi.dwi_fim_voucher
where corp_code in(SELECT corp_code FROM corp)
    ${if(len(sBill)=0," ","AND src_bill_codes = '" + sBill + "'")}
AND acnt_year = LEFT('${sStart_mon}',4)
AND acnt_mon >= RIGHT('${sStart_mon}',2)
AND acnt_mon <= RIGHT('${sEnd_mon}',2)

    ${if(sFee_type='销售费用',"","/*")}
  AND subject_code like '6601%'
    ${nvl(switch(sSubject
    ,"0000","AND subject_code like '6601%' AND (subject_code not like '660114%'
                 AND subject_code not like '660127%'
                 AND subject_code NOT like '660150%'
                 AND subject_code NOT like '660115%'
                 AND subject_code NOT like '660117%'
                 AND subject_code NOT like '660136%'
                 AND subject_code NOT like '660130%'
                 AND subject_code NOT like '660125%'
                 AND subject_code NOT like '660167%'
                 AND subject_code NOT like '660121%'
                 AND subject_code NOT like '660164%'
                )"
    ,"660114,660127","AND (subject_code like '660114%' or subject_code like '660127%')")
    ,"and subject_code like '" + sSubject +"%'")}
    ${if(sFee_type='销售费用',"","*/")}


    ${if(sFee_type='管理费用',"","/*")}
  AND subject_code like '6602%'

    ${if(sSubject='合计',"",nvl(switch(sSubject
       ,"0000","AND subject_code NOT LIKE '660297%'
             AND subject_code NOT LIKE  '660219%'
             AND subject_code NOT LIKE  '660289%'
             AND subject_code NOT LIKE  '660243%'
             AND subject_code NOT LIKE  '660226%'
             AND subject_code NOT LIKE  '660234%'
             AND subject_code NOT LIKE  '660221%'
             AND subject_code NOT LIKE  '660227%'
             AND subject_code NOT LIKE  '660232%'
             AND subject_code NOT LIKE  '660233%'
             AND subject_code NOT LIKE  '660231%'
             AND subject_code NOT LIKE  '660273%'
             AND subject_code NOT LIKE  '660290%'
             AND subject_code NOT LIKE  '660237%'
             AND subject_code NOT LIKE  '660287%'
             AND subject_code NOT LIKE  '660228%'"
       )
       ,"and subject_code like '" + sSubject +"%'"))}
    ${if(sFee_type='管理费用',"","*/")}


    ${if(sFee_type='研发费用',"","/*")}
  AND subject_code like '6604%'
    ${if(sSubject='合计',"",nvl(switch(sSubject
,"0000","AND subject_code NOT like '660401%'
             AND subject_code NOT like '660403%'
             AND subject_code NOT like '660409%'
             AND subject_code NOT like '660408%'
             AND subject_code NOT like '660402%'"
             )
,"and subject_code like '" + sSubject +"%'"))}

    ${if(sFee_type='研发费用',"","*/")}


    ${if(sFee_type='财务费用',"","/*")}
  AND subject_code like '6603%'
    ${if(sSubject='000000',"","AND subject_code like '" + sSubject + "%'")}
    ${if(sFee_type='财务费用',"","*/")}
order by
    bill_date    -- 制单日期
        ,pk
        ,bill_emp_code      -- 制单人工号
        ,corp_code          -- 公司编码
        ,acnt_book_name     -- 主体账簿
        ,vouch_type         -- 凭证类别
        ,vouch_code         -- 凭证号
        ,acnt_year          -- 账期年度
        ,acnt_mon           -- 账期月度
        ,main_abstract      -- 主表摘要
        ,entry_code         -- 分录号
        ,sub_abstract       -- 子表摘要
        ,subject_code       -- 科目编码
        ,subject_name       -- 科目名称
        ,subject_full_name  -- 科目全称
        ,aux_content       -- 辅助核算
        ,src_bill_codes     -- 来源单据号
        ,acnt_date          -- 记账日期
limit ${sPage_rows} offset (${pJump_Page}-1)*${sPage_rows}
;

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




SELECT
CONCAT('数据更新时间:',LEFT(MAX(update_time),19)) AS update_time
FROM dwi.dwi_fim_voucher
where subject_code like '6601%'


-- ======================================
-- 查询OA可下钻单据号
-- ======================================


SELECT
    a.bill_code -- 单据号
     ,b.subject_name
FROM dwi.dwi_fim_oa_payment a
         LEFT JOIN
     (
         SELECT
             *
         FROM dwi.dwi_fim_voucher
     )b
     on a.bill_code = b.src_bill_codes
where b.src_bill_codes is not NULL
  and a.bill_date >= '2024-01-01'
  and b.subject_code LIKE  '6601%'
