create view vi_li_invoice as SELECT TAB.ID             AS ID,
       TAB.CONTRACTNUMBER AS CONTRACTNUMBER,
       TAB.PAYMENTNUMBER  AS PAYMENTNUMBER,
       TAB.CUSTOMER_ID    AS CUSTOMER_ID,
       TAB.CUSTOMERNAME   AS CUSTOMERNAME,
       TAB.PLANLIST       AS PLANLIST,
       TAB.PLANDATE       AS PLANDATE,
       TAB.PLANMONEY      AS PLANMONEY,
       TAB.FACTDATE       AS FACTDATE,
       TAB.FACTMONEY      AS FACTMONEY,
       TAB.INVOICEMONEY   AS INVOICEMONEY,
       TAB.FEETYPE        AS FEETYPE,
       TAB.INVOICEOBJECT  AS INVOICEOBJECT,
       TAB.TAXREGCODE     AS TAXREGCODE,
       TAB.TAXBANK        AS TAXBANK,
       TAB.TAXACC         AS TAXACC,
       TAB.INVOICEPHONE   AS INVOICEPHONE,
       TAB.INVOICEADD     AS INVOICEADD,
       TAB.INVOICESTATUS  AS INVOICESTATUS,
       TAB.LEASEFORM      AS LEASEFORM,
       TAB.FEETYPECODE    AS FEETYPECODE
FROM (
    SELECT LFII.ID                                                                                                                AS ID,
           LCI.CONTRACT_NUMBER                                                                                                    AS CONTRACTNUMBER,
           LFP.PAYMENT_NUMBER                                                                                                     AS PAYMENTNUMBER,
           LUL.CUSTOMER_ID                                                                                                        AS CUSTOMER_ID,
           LUL.CUSTOMER_NAME                                                                                                      AS CUSTOMERNAME,
           LFP.PLAN_LIST                                                                                                          AS PLANLIST,
           LFP.PLAN_DATE                                                                                                          AS PLANDATE,
           format(LFP.PLAN_MONEY, 2)                                                                                              AS PLANMONEY,
           ''                                                                                                                     AS FACTDATE,
           ''                                                                                                                     AS FACTMONEY,
           format((((LFII.INVOICE_MONEY - LFII.TEMPORARY_NOINVOICE) - LFII.NOINVOICE) - ifnull(LFIDI.SUMINVOICEDMONEY, 0.00)), 2) AS INVOICEMONEY,
           FEECODE.ITEMNAME                                                                                                       AS FEETYPE,
           INVOICECODE.ITEMNAME                                                                                                   AS INVOICETYPE,
           LFII.INVOICE_OBJECT                                                                                                    AS INVOICEOBJECT,
           LFII.TAX_REG_CODE                                                                                                      AS TAXREGCODE,
           LFII.TAX_BANK                                                                                                          AS TAXBANK,
           LFII.TAX_ACC                                                                                                           AS TAXACC,
           LFII.INVOICE_PHONE                                                                                                     AS INVOICEPHONE,
           LFII.INVOICE_ADD                                                                                                       AS INVOICEADD,
           (CASE LFII.INVOICE_STATUS
                WHEN '0' THEN '未申请'
                WHEN '1' THEN '已申请'
                WHEN '2' THEN '已退回'
                WHEN '3' THEN '部分未申请'
                WHEN '4' THEN '暂不开票'
                WHEN '5' THEN '已开票' END)                                                                                          AS INVOICESTATUS,
           LEASECODE.ITEMNAME                                                                                                     AS LEASEFORM,
           'fund'                                                                                                                 AS FEETYPECODE
    FROM (((((((APZL.LI_FUND_INVOICE_INFO LFII LEFT JOIN APZL.LC_FUND_PLAN LFP ON ((LFP.ID = LFII.PLAN_ID))) LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LFP.CONTRACT_ID = convert(LCI.ID USING UTF8)))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON (((LUL.CONTRACT_ID = LCI.ID) AND (LUL.IS_MAIN = 'Y')))) LEFT JOIN (
        SELECT APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.FUND_INVOICE_ID    AS FUND_INVOICE_ID,
               sum(APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.INVOICE_MONEY) AS SUMINVOICEDMONEY
        FROM APZL.LI_FUND_INVOICE_DOWNLOAD_INFO
        GROUP BY APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.FUND_INVOICE_ID
    ) LFIDI ON ((LFIDI.FUND_INVOICE_ID = LFII.ID))) LEFT JOIN APZL.CODE_LIBRARY FEECODE ON (((FEECODE.ITEMNO = LFP.FEE_TYPE) AND (FEECODE.CODENO = 'FeeType')))) LEFT JOIN APZL.CODE_LIBRARY LEASECODE ON (((LEASECODE.ITEMNO = LCI.LEASE_FORM) AND (LEASECODE.CODENO = 'leas_form'))))
    LEFT JOIN APZL.CODE_LIBRARY INVOICECODE ON (((INVOICECODE.ITEMNO = LFII.INVOICE_TYPE) AND (INVOICECODE.CODENO = 'invoice_type'))))
    WHERE (isnull(LFII.INCOME_ID) AND (LFII.PLAN_ID IS NOT NULL) AND (LFII.BILL_TYPE = 'invoice'))
    UNION ALL
    SELECT LFII.ID                                                                                                                AS ID,
           LCI.CONTRACT_NUMBER                                                                                                    AS CONTRACTNUMBER,
           LFI.PAYMENT_NUMBER                                                                                                     AS PAYMENTNUMBER,
           LUL.CUSTOMER_ID                                                                                                        AS CUSTOMER_ID,
           LUL.CUSTOMER_NAME                                                                                                      AS CUSTOMERNAME,
           LFI.CHARGE_LIST                                                                                                        AS PLANLIST,
           LFP.PLAN_DATE                                                                                                          AS PLANDATE,
           format(LFP.PLAN_MONEY, 2)                                                                                              AS PLANMONEY,
           LFI.FACT_DATE                                                                                                          AS FACTDATE,
           format(LFI.FACT_MONEY, 2)                                                                                              AS FACTMONEY,
           format((((LFII.INVOICE_MONEY - LFII.TEMPORARY_NOINVOICE) - LFII.NOINVOICE) - ifnull(LFIDI.SUMINVOICEDMONEY, 0.00)), 2) AS INVOICEMONEY,
           FEECODE.ITEMNAME                                                                                                       AS FEETYPE,
           INVOICECODE.ITEMNAME                                                                                                   AS INVOICETYPE,
           LFII.INVOICE_OBJECT                                                                                                    AS INVOICEOBJECT,
           LFII.TAX_REG_CODE                                                                                                      AS TAXREGCODE,
           LFII.TAX_BANK                                                                                                          AS TAXBANK,
           LFII.TAX_ACC                                                                                                           AS TAXACC,
           LFII.INVOICE_PHONE                                                                                                     AS INVOICEPHONE,
           LFII.INVOICE_ADD                                                                                                       AS INVOICEADD,
           (CASE LFII.INVOICE_STATUS
                WHEN '0' THEN '未申请'
                WHEN '1' THEN '已申请'
                WHEN '2' THEN '已退回'
                WHEN '3' THEN '部分未申请'
                WHEN '4' THEN '暂不开票'
                WHEN '5' THEN '已开票' END)                                                                                          AS INVOICESTATUS,
           LEASECODE.ITEMNAME                                                                                                     AS LEASEFORM,
           'fund'                                                                                                                 AS FEETYPECODE
    FROM ((((((((APZL.LI_FUND_INVOICE_INFO LFII LEFT JOIN APZL.LC_FUND_INCOME LFI ON ((LFII.INCOME_ID = LFI.ID))) LEFT JOIN APZL.LC_FUND_PLAN LFP ON ((LFP.ID = LFI.PLAN_ID))) LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LFI.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON (((LUL.CONTRACT_ID = LCI.ID) AND (LUL.IS_MAIN = 'Y')))) LEFT JOIN (
        SELECT APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.FUND_INVOICE_ID    AS FUND_INVOICE_ID,
               sum(APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.INVOICE_MONEY) AS SUMINVOICEDMONEY
        FROM APZL.LI_FUND_INVOICE_DOWNLOAD_INFO
        GROUP BY APZL.LI_FUND_INVOICE_DOWNLOAD_INFO.FUND_INVOICE_ID
    ) LFIDI ON ((LFIDI.FUND_INVOICE_ID = LFII.ID))) LEFT JOIN APZL.CODE_LIBRARY FEECODE ON (((FEECODE.ITEMNO = LFI.FEE_TYPE) AND (FEECODE.CODENO = 'FeeType')))) LEFT JOIN APZL.CODE_LIBRARY LEASECODE ON (((LEASECODE.ITEMNO = LCI.LEASE_FORM) AND (LEASECODE.CODENO = 'leas_form'))))
    LEFT JOIN APZL.CODE_LIBRARY INVOICECODE ON (((INVOICECODE.ITEMNO = LFII.INVOICE_TYPE) AND (INVOICECODE.CODENO = 'invoice_type'))))
    WHERE ((LFII.INCOME_ID IS NOT NULL) AND isnull(LFII.PLAN_ID) AND (LFII.BILL_TYPE = 'invoice'))
    UNION ALL
    SELECT LRII.ID                                                                                                                       AS ID,
           LCI.CONTRACT_NUMBER                                                                                                           AS CONTRACTNUMBER,
           LRP.PAYMENT_NUMBER                                                                                                            AS PAYMENTNUMBER,
           LUL.CUSTOMER_ID                                                                                                               AS CUSTOMER_ID,
           LUL.CUSTOMER_NAME                                                                                                             AS CUSTOMERNAME,
           LRP.PLAN_LIST                                                                                                                 AS PLANLIST,
           LRP.PLAN_DATE                                                                                                                 AS PLANDATE,
           format((CASE LRII.TAX_TYPE WHEN 'rent' THEN LRP.RENT WHEN 'corpus' THEN LRP.CORPUS WHEN 'interest' THEN LRP.INTEREST END), 2) AS PLANMONEY,
           ''                                                                                                                            AS FACTDATE,
           ''                                                                                                                            AS FACTMONEY,
           format((((LRII.INVOICE_MONEY - LRII.TEMPORARY_NOINVOICE) - LRII.NOINVOICE) - ifnull(LRIDI.SUMINVOICEDMONEY, 0.00)),
                  2)                                                                                                                     AS INVOICEMONEY,
           (CASE LRII.TAX_TYPE WHEN 'rent' THEN '租金' WHEN 'corpus' THEN '本金' WHEN 'interest' THEN '利息' END)                              AS FEETYPE,
           INVOICECODE.ITEMNAME                                                                                                          AS INVOICETYPE,
           LRII.INVOICE_OBJECT                                                                                                           AS INVOICEOBJECT,
           LRII.TAX_REG_CODE                                                                                                             AS TAXREGCODE,
           LRII.TAX_BANK                                                                                                                 AS TAXBANK,
           LRII.TAX_ACC                                                                                                                  AS TAXACC,
           LRII.INVOICE_PHONE                                                                                                            AS INVOICEPHONE,
           LRII.INVOICE_ADD                                                                                                              AS INVOICEADD,
           (CASE LRII.INVOICE_STATUS
                WHEN '0' THEN '未申请'
                WHEN '1' THEN '已申请'
                WHEN '2' THEN '已退回'
                WHEN '3' THEN '部分未申请'
                WHEN '4' THEN '暂不开票'
                WHEN '5'
                    THEN '已开票' END)                                                                                                      AS INVOICESTATUS,
           LEASECODE.ITEMNAME                                                                                                            AS LEASEFORM,
           LRII.TAX_TYPE                                                                                                                 AS FEETYPECODE
    FROM ((((((APZL.LI_RENT_INVOICE_INFO LRII LEFT JOIN APZL.LC_RENT_PLAN LRP ON ((LRP.ID = LRII.PLAN_ID))) LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LRP.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON (((LUL.CONTRACT_ID = LCI.ID) AND (LUL.IS_MAIN = 'Y')))) LEFT JOIN (
        SELECT APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID    AS RENT_INVOICE_ID,
               sum(APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.INVOICE_MONEY) AS SUMINVOICEDMONEY
        FROM APZL.LI_RENT_INVOICE_DOWNLOAD_INFO
        GROUP BY APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID
    ) LRIDI ON ((LRIDI.RENT_INVOICE_ID = LRII.ID))) LEFT JOIN APZL.CODE_LIBRARY LEASECODE ON (((LEASECODE.ITEMNO = LCI.LEASE_FORM) AND (LEASECODE.CODENO = 'leas_form'))))
    LEFT JOIN APZL.CODE_LIBRARY INVOICECODE ON (((INVOICECODE.ITEMNO = LRII.INVOICE_TYPE) AND (INVOICECODE.CODENO = 'invoice_type'))))
    WHERE (isnull(LRII.INCOME_ID) AND (LRII.PLAN_ID IS NOT NULL) AND (LRII.BILL_TYPE = 'invoice'))
    UNION ALL
    SELECT LRII.ID                                                                                                                   AS ID,
           LCI.CONTRACT_NUMBER                                                                                                       AS CONTRACTNUMBER,
           LRI.PAYMENT_NUMBER                                                                                                        AS PAYMENTNUMBER,
           LUL.CUSTOMER_ID                                                                                                           AS CUSTOMER_ID,
           LUL.CUSTOMER_NAME                                                                                                         AS CUSTOMERNAME,
           LRI.HIRE_LIST                                                                                                             AS PLANLIST,
           VLRP.PLAN_DATE                                                                                                            AS PLANDATE,
           format((CASE LRII.TAX_TYPE
                       WHEN 'rent' THEN VLRP.RENT
                       WHEN 'corpus' THEN VLRP.CORPUS
                       WHEN 'interest' THEN VLRP.INTEREST
                       WHEN 'penalty' THEN VLRP.PENALTY END), 2)                                                                     AS PLANMONEY,
           LRI.HIRE_DATE                                                                                                             AS FACTDATE,
           format((CASE LRII.TAX_TYPE
                       WHEN 'rent' THEN LRI.RENT
                       WHEN 'corpus' THEN LRI.CORPUS
                       WHEN 'interest' THEN LRI.INTEREST
                       WHEN 'penalty' THEN LRI.PENALTY END), 2)                                                                      AS FACTMONEY,
           format((((LRII.INVOICE_MONEY - LRII.TEMPORARY_NOINVOICE) - LRII.NOINVOICE) - ifnull(LRIDI.SUMINVOICEDMONEY, 0.00)), 2)    AS INVOICEMONEY,
           (CASE LRII.TAX_TYPE WHEN 'rent' THEN '租金' WHEN 'corpus' THEN '本金' WHEN 'interest' THEN '利息' WHEN 'penalty' THEN '罚息' END) AS FEETYPE,
           INVOICECODE.ITEMNAME                                                                                                      AS INVOICETYPE,
           LRII.INVOICE_OBJECT                                                                                                       AS INVOICEOBJECT,
           LRII.TAX_REG_CODE                                                                                                         AS TAXREGCODE,
           LRII.TAX_BANK                                                                                                             AS TAXBANK,
           LRII.TAX_ACC                                                                                                              AS TAXACC,
           LRII.INVOICE_PHONE                                                                                                        AS INVOICEPHONE,
           LRII.INVOICE_ADD                                                                                                          AS INVOICEADD,
           (CASE LRII.INVOICE_STATUS
                WHEN '0' THEN '未申请'
                WHEN '1' THEN '已申请'
                WHEN '2' THEN '已退回'
                WHEN '3' THEN '部分未申请'
                WHEN '4' THEN '暂不开票'
                WHEN '5' THEN '已开票' END)                                                                                             AS INVOICESTATUS,
           LEASECODE.ITEMNAME                                                                                                        AS LEASEFORM,
           LRII.TAX_TYPE                                                                                                             AS FEETYPECODE
    FROM (((((((APZL.LI_RENT_INVOICE_INFO LRII LEFT JOIN APZL.LC_RENT_INCOME LRI ON ((LRI.ID = LRII.INCOME_ID))) LEFT JOIN APZL.VI_LC_RENT_PLAN VLRP ON ((VLRP.ID = LRI.PLAN_ID))) LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LRI.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON (((LUL.CONTRACT_ID = LCI.ID) AND (LUL.IS_MAIN = 'Y')))) LEFT JOIN (
        SELECT APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID    AS RENT_INVOICE_ID,
               sum(APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.INVOICE_MONEY) AS SUMINVOICEDMONEY
        FROM APZL.LI_RENT_INVOICE_DOWNLOAD_INFO
        GROUP BY APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID
    ) LRIDI ON ((LRIDI.RENT_INVOICE_ID = LRII.ID))) LEFT JOIN APZL.CODE_LIBRARY LEASECODE ON (((LEASECODE.ITEMNO = LCI.LEASE_FORM) AND (LEASECODE.CODENO = 'leas_form'))))
    LEFT JOIN APZL.CODE_LIBRARY INVOICECODE ON (((INVOICECODE.ITEMNO = LRII.INVOICE_TYPE) AND (INVOICECODE.CODENO = 'invoice_type'))))
    WHERE ((LRII.INCOME_ID IS NOT NULL) AND isnull(LRII.PLAN_ID) AND (LRII.BILL_TYPE = 'invoice'))
    UNION ALL
    SELECT LRII.ID                                                                                                                AS ID,
           LCI.CONTRACT_NUMBER                                                                                                    AS CONTRACTNUMBER,
           LCC.PAYMENT_NUMBER                                                                                                     AS PAYMENTNUMBER,
           LUL.CUSTOMER_ID                                                                                                        AS CUSTOMER_ID,
           LUL.CUSTOMER_NAME                                                                                                      AS CUSTOMERNAME,
           ''                                                                                                                     AS PLANLIST,
           ''                                                                                                                     AS PLANDATE,
           format((LCC.EQUIP_AMT - ifnull(LCC.FIRST_PAYMENT, 0)), 2)                                                              AS PLANMONEY,
           ''                                                                                                                     AS FACTDATE,
           ''                                                                                                                     AS FACTMONEY,
           format((((LRII.INVOICE_MONEY - LRII.TEMPORARY_NOINVOICE) - LRII.NOINVOICE) - ifnull(LRIDI.SUMINVOICEDMONEY, 0.00)), 2) AS INVOICEMONEY,
           '本金一次性'                                                                                                                AS FEETYPE,
           INVOICECODE.ITEMNAME                                                                                                   AS INVOICETYPE,
           LRII.INVOICE_OBJECT                                                                                                    AS INVOICEOBJECT,
           LRII.TAX_REG_CODE                                                                                                      AS TAXREGCODE,
           LRII.TAX_BANK                                                                                                          AS TAXBANK,
           LRII.TAX_ACC                                                                                                           AS TAXACC,
           LRII.INVOICE_PHONE                                                                                                     AS INVOICEPHONE,
           LRII.INVOICE_ADD                                                                                                       AS INVOICEADD,
           (CASE LRII.INVOICE_STATUS
                WHEN '0' THEN '未申请'
                WHEN '1' THEN '已申请'
                WHEN '2' THEN '已退回'
                WHEN '3' THEN '部分未申请'
                WHEN '4' THEN '暂不开票'
                WHEN '5' THEN '已开票' END)                                                                                          AS INVOICESTATUS,
           LEASECODE.ITEMNAME                                                                                                     AS LEASEFORM,
           'corpusone'                                                                                                            AS FEETYPECODE
    FROM ((((((APZL.LI_RENT_INVOICE_INFO LRII LEFT JOIN APZL.LC_CONTRACT_CONDITION LCC ON ((LRII.PAYMENT_NUMBER = LCC.PAYMENT_NUMBER))) LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCC.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON (((LUL.CONTRACT_ID = LCI.ID) AND (LUL.IS_MAIN = 'Y')))) LEFT JOIN (
        SELECT APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID    AS RENT_INVOICE_ID,
               sum(APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.INVOICE_MONEY) AS SUMINVOICEDMONEY
        FROM APZL.LI_RENT_INVOICE_DOWNLOAD_INFO
        GROUP BY APZL.LI_RENT_INVOICE_DOWNLOAD_INFO.RENT_INVOICE_ID
    ) LRIDI ON ((LRIDI.RENT_INVOICE_ID = LRII.ID))) LEFT JOIN APZL.CODE_LIBRARY LEASECODE ON (((LEASECODE.ITEMNO = LCI.LEASE_FORM) AND (LEASECODE.CODENO = 'leas_form'))))
    LEFT JOIN APZL.CODE_LIBRARY INVOICECODE ON (((INVOICECODE.ITEMNO = LRII.INVOICE_TYPE) AND (INVOICECODE.CODENO = 'invoice_type'))))
    WHERE (isnull(LRII.INCOME_ID) AND isnull(LRII.PLAN_ID) AND (LRII.PAYMENT_NUMBER IS NOT NULL) AND (LRII.BILL_TYPE = 'invoice'))
) TAB
WHERE ((1 = 1) AND (TAB.INVOICESTATUS = '已申请'))
ORDER BY TAB.PAYMENTNUMBER;

