create view vi_li_invoice_rent as SELECT T.CONTRACT_ID    AS CONTRACT_ID,
       T.CONTRACT_NO    AS CONTRACT_NO,
       T.PAYMENT_NUMBER AS PAYMENT_NUMBER,
       T.CUSTOMER_ID    AS CUSTOMER_ID,
       T.CUSTOMER_NAME  AS CUSTOMER_NAME,
       T.PLAN_LIST      AS PLAN_LIST,
       T.PLAN_ID        AS PLAN_ID,
       T.PLAN_DATE      AS PLAN_DATE,
       T.PLAN_MONEY     AS PLAN_MONEY,
       T.INVOICE_MONEY  AS INVOICE_MONEY,
       T.TAX_TYPE       AS TAX_TYPE,
       T.CERT_ID        AS CERT_ID,
       T.ACCOUNT        AS ACCOUNT,
       T.ACC_NUMBER     AS ACC_NUMBER,
       T.BANK_NAME      AS BANK_NAME,
       T.TELEPHONE      AS TELEPHONE,
       T.CUSTOMER_TYPE  AS CUSTOMER_TYPE
FROM (
    (
        SELECT LCI.ID                                                              AS CONTRACT_ID,
               LCI.CONTRACT_NO                                                     AS CONTRACT_NO,
               LRI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LRI.PLAN_LIST                                                       AS PLAN_LIST,
               LRI.PLAN_ID                                                         AS PLAN_ID,
               max(LRI.HIRE_DATE)                                                  AS PLAN_DATE,
               sum(LRI.INTEREST)                                                   AS PLAN_MONEY,
               sum(LRI.INTEREST)                                                   AS INVOICE_MONEY,
               '利息'                                                                AS TAX_TYPE,
               CI.CERTID                                                           AS CERT_ID,
               CAC.ACCOUNT                                                         AS ACCOUNT,
               CAC.ACC_NUMBER                                                      AS ACC_NUMBER,
               CAC.BANK_NAME                                                       AS BANK_NAME,
               (CASE WHEN (CI.CUSTOMERTYPE = '03') THEN CP.MOBILE ELSE CF.TEL END) AS TELEPHONE,
               CI.CUSTOMERTYPE                                                     AS CUSTOMER_TYPE
        FROM ((((((APZL.LC_RENT_INCOME LRI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LRI.CONTRACT_ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON ((LUL.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.CUSTOMER_INFO CI ON ((CI.CUSTOMERID = LUL.CUSTOMER_ID))) LEFT JOIN APZL.CUSTOMER_PERSON CP ON ((CP.CUSTOMERID = CI.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_FAMILY CF ON ((CF.CUSTOMERID = CI.CUSTOMERID)))
        LEFT JOIN APZL.CUSTOMER_ACCOUNT CAC ON ((CAC.CUSTOMERID = CI.CUSTOMERID)))
        WHERE ((LCI.LEAS_FORM = '02') AND (LCI.CONTRACT_STATUS = '31') AND (LCI.BUSINESSTYPE = '1') AND (LUL.IS_MAIN = 'Y') AND
               (LRI.INTEREST <> 0.00) AND (NOT (LRI.PLAN_ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.PLAN_ID FROM APZL.LI_RENT_INVOICE_INFO WHERE (APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2')
            ))))
        GROUP BY LRI.CONTRACT_ID, LRI.PLAN_LIST
    )
    UNION ALL
    (
        SELECT LCI.ID                                                              AS CONTRACT_ID,
               LCI.CONTRACT_NO                                                     AS CONTRACT_NO,
               LRI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LRI.PLAN_LIST                                                       AS PLAN_LIST,
               LRI.PLAN_ID                                                         AS PLAN_ID,
               max(LRI.HIRE_DATE)                                                  AS PLAN_DATE,
               sum(LRI.PENALTY)                                                    AS PLAN_MONEY,
               sum(LRI.PENALTY)                                                    AS INVOICE_MONEY,
               '逾期利息'                                                              AS TAX_TYPE,
               CI.CERTID                                                           AS CERT_ID,
               CAC.ACCOUNT                                                         AS ACCOUNT,
               CAC.ACC_NUMBER                                                      AS ACC_NUMBER,
               CAC.BANK_NAME                                                       AS BANK_NAME,
               (CASE WHEN (CI.CUSTOMERTYPE = '03') THEN CP.MOBILE ELSE CF.TEL END) AS TELEPHONE,
               CI.CUSTOMERTYPE                                                     AS CUSTOMER_TYPE
        FROM ((((((APZL.LC_RENT_INCOME LRI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LRI.CONTRACT_ID))) LEFT JOIN APZL.LB_UNION_LESSEE LUL ON ((LUL.CONTRACT_ID = LCI.ID))) LEFT JOIN APZL.CUSTOMER_INFO CI ON ((CI.CUSTOMERID = LUL.CUSTOMER_ID))) LEFT JOIN APZL.CUSTOMER_PERSON CP ON ((CP.CUSTOMERID = CI.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_FAMILY CF ON ((CF.CUSTOMERID = CI.CUSTOMERID)))
        LEFT JOIN APZL.CUSTOMER_ACCOUNT CAC ON ((CAC.CUSTOMERID = CI.CUSTOMERID)))
        WHERE ((LCI.LEAS_FORM = '02') AND (LCI.CONTRACT_STATUS = '31') AND (LCI.BUSINESSTYPE = '1') AND (LUL.IS_MAIN = 'Y') AND
               (LRI.PENALTY <> 0.00) AND (NOT (LRI.PLAN_ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.PLAN_ID FROM APZL.LI_RENT_INVOICE_INFO WHERE (APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2')
            ))))
        GROUP BY LRI.CONTRACT_ID, LRI.PLAN_LIST
    )
) T
ORDER BY T.CONTRACT_ID, T.PLAN_LIST;

