create view vi_li_invoice_others as SELECT T.CONTRACT_ID    AS CONTRACT_ID,
       T.CONTRACT_NO    AS CONTRACT_NO,
       T.CUSTOMER_ID    AS CUSTOMER_ID,
       T.CUSTOMER_NAME  AS CUSTOMER_NAME,
       T.PAYMENT_NUMBER AS PAYMENT_NUMBER,
       T.PLAN_PRICE     AS PLAN_PRICE,
       T.INVOICE_PRICE  AS INVOICE_PRICE,
       T.MONEYTYPE      AS MONEYTYPE,
       T.TYPESS         AS TYPESS,
       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,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LFI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               sum(LFI.FACT_MONEY)                                                 AS PLAN_PRICE,
               sum(LFI.FACT_MONEY)                                                 AS INVOICE_PRICE,
               'myhj'                                                              AS MONEYTYPE,
               '名义货价'                                                              AS TYPESS,
               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_FUND_INCOME LFI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LFI.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
               (LFI.FEE_TYPE = 'feetype4') AND (LFI.PAY_STATUS = 'have_paid') AND (NOT (LCI.ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.CONTRACT_ID
                FROM APZL.LI_RENT_INVOICE_INFO
                WHERE ((APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2') AND
                       ((APZL.LI_RENT_INVOICE_INFO.PLAN_ID = '') OR isnull(APZL.LI_RENT_INVOICE_INFO.PLAN_ID)))
            ))))
        GROUP BY LCI.ID
    )
    UNION ALL
    (
        SELECT LCI.ID                                                              AS CONTRACT_ID,
               LCI.CONTRACT_NO                                                     AS CONTRACT_NO,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LFI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               sum(LFI.FACT_MONEY)                                                 AS PLAN_PRICE,
               sum(LFI.FACT_MONEY)                                                 AS INVOICE_PRICE,
               'gps'                                                               AS MONEYTYPE,
               'GPS安装服务费'                                                          AS TYPESS,
               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_FUND_INCOME LFI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LFI.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
               (LFI.FEE_TYPE = 'feetype24') AND (LFI.PAY_STATUS = 'have_paid') AND (NOT (LCI.ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.CONTRACT_ID
                FROM APZL.LI_RENT_INVOICE_INFO
                WHERE ((APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2') AND
                       ((APZL.LI_RENT_INVOICE_INFO.PLAN_ID = '') OR isnull(APZL.LI_RENT_INVOICE_INFO.PLAN_ID)))
            ))))
        GROUP BY LCI.ID
    )
    UNION ALL
    (
        SELECT LCI.ID                                                              AS CONTRACT_ID,
               LCI.CONTRACT_NO                                                     AS CONTRACT_NO,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LFI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               sum(LFI.FACT_MONEY)                                                 AS PLAN_PRICE,
               sum(LFI.FACT_MONEY)                                                 AS INVOICE_PRICE,
               'sxf'                                                               AS MONEYTYPE,
               '手续费'                                                               AS TYPESS,
               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_FUND_INCOME LFI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LFI.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
               (LFI.FEE_TYPE = 'feetype1') AND (LFI.PAY_STATUS = 'have_paid') AND (NOT (LCI.ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.CONTRACT_ID
                FROM APZL.LI_RENT_INVOICE_INFO
                WHERE ((APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2') AND
                       ((APZL.LI_RENT_INVOICE_INFO.PLAN_ID = '') OR isnull(APZL.LI_RENT_INVOICE_INFO.PLAN_ID)))
            ))))
        GROUP BY LCI.ID
    )
    UNION ALL
    (
        SELECT LCI.ID                                                              AS CONTRACT_ID,
               LCI.CONTRACT_NO                                                     AS CONTRACT_NO,
               LUL.CUSTOMER_ID                                                     AS CUSTOMER_ID,
               LUL.CUSTOMER_NAME                                                   AS CUSTOMER_NAME,
               LFI.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER,
               sum(LFI.FACT_MONEY)                                                 AS PLAN_PRICE,
               sum(LFI.FACT_MONEY)                                                 AS INVOICE_PRICE,
               'wyj'                                                               AS MONEYTYPE,
               '违约金'                                                               AS TYPESS,
               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_FUND_INCOME LFI LEFT JOIN APZL.LB_CONTRACT_INFO LCI ON ((LCI.ID = LFI.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
               (LFI.FEE_TYPE = 'feetype30') AND (LFI.PAY_STATUS = 'have_paid') AND (NOT (LCI.ID IN (
                SELECT APZL.LI_RENT_INVOICE_INFO.CONTRACT_ID
                FROM APZL.LI_RENT_INVOICE_INFO
                WHERE ((APZL.LI_RENT_INVOICE_INFO.INVOICE_STATUS <> '2') AND
                       ((APZL.LI_RENT_INVOICE_INFO.PLAN_ID = '') OR isnull(APZL.LI_RENT_INVOICE_INFO.PLAN_ID)))
            ))))
        GROUP BY LCI.ID
    )
) T
ORDER BY T.CONTRACT_ID;

